viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #include "feedmodel.h"
  2. #include <QAbstractItemModel>
  3. #include <QSqlDatabase>
  4. #include <QSqlQuery>
  5. #include <QSqlError>
  6. #include <QDebug>
  7. #include <QStringList>
  8. #include <QLabel>
  9.  
  10. using namespace Larss;
  11.  
  12. FeedModel::FeedModel(QSqlDatabase db, QObject *parent) : QAbstractItemModel (parent)
  13. {
  14. this->db = db;
  15.  
  16. // Check that the right tables are present in the db
  17. if (!db.tables().contains("categories"))
  18. {
  19. QSqlQuery query(db);
  20. query.prepare("CREATE TABLE categories (id INTEGER PRIMARY KEY, name TEXT);");
  21. if (!query.exec())
  22. qDebug() << "Error while creating the categories table in the db";
  23. }
  24.  
  25. if (!db.tables().contains("feeds"))
  26. {
  27. QSqlQuery query(db);
  28. query.prepare("CREATE TABLE feeds (id INTEGER PRIMARY KEY, category INTEGER, name TEXT, url TEXT);");
  29. if (!query.exec())
  30. qDebug() << "Error while creating the feeds table in the db";
  31. }
  32. }
  33.  
  34. FeedModel::~FeedModel()
  35. {
  36. }
  37.  
  38.  
  39. QModelIndex
  40. FeedModel::index(int row, int column, const QModelIndex &parent) const
  41. {
  42. if (parent.internalId() == 0)
  43. {
  44. QSqlQuery query (db);
  45. query.prepare("SELECT id from categories ORDER by id;");
  46. if (query.exec())
  47. {
  48. if (!query.first())
  49. return QModelIndex();
  50. for (int i = 0; i < row; i++)
  51. {
  52. if (!query.next ())
  53. return QModelIndex();
  54. }
  55. return createIndex(row, column, query.value(0).toInt());
  56. }
  57. else
  58. return QModelIndex();
  59. }
  60. else
  61. {
  62. QSqlQuery query(db);
  63. query.prepare ("SELECT id from feeds WHERE category=:category ORDER BY id;");
  64. query.bindValue("category", parent.internalId());
  65. if (query.exec())
  66. {
  67. if (!query.first())
  68. return QModelIndex();
  69. else
  70. {
  71. for(int i = 0; i < row; i++)
  72. {
  73. if (!query.next())
  74. return QModelIndex();
  75. }
  76. return createIndex(row, column, query.value(0).toInt() + FEEDMODEL_MAX_CATEGORIES);
  77. }
  78. }
  79. else
  80. return QModelIndex();
  81. }
  82. }
  83.  
  84. bool
  85. FeedModel::setData(const QModelIndex &index, const QVariant &value, int role)
  86. {
  87. if (role != Qt::EditRole)
  88. return false;
  89.  
  90. QSqlQuery query(db);
  91.  
  92. if (index.internalId() < FEEDMODEL_MAX_CATEGORIES && index.internalId() != 0)
  93. {
  94. // We are trying to modify a category
  95. query.prepare("UPDATE categories SET name=:value WHERE id=:id;");
  96. query.bindValue("value", value.toString());
  97. query.bindValue("id", index.internalId());
  98. }
  99. else
  100. {
  101. // We are trying to modify a feed
  102. query.prepare("UPDATE feeds SET name=:value WHERE id=:id;");
  103. query.bindValue("value", value.toString());
  104. query.bindValue("id", index.internalId() - FEEDMODEL_MAX_CATEGORIES);
  105. }
  106.  
  107. if (!query.exec())
  108. {
  109. qDebug() << "Query failed" << query.lastError() << query.executedQuery();
  110. return false;
  111. }
  112. else
  113. {
  114. // Emit the datachanged signal
  115. dataChanged(index, index);
  116. return true;
  117. }
  118. }
  119.  
  120. Qt::ItemFlags
  121. FeedModel::flags(const QModelIndex &index) const
  122. {
  123. Q_UNUSED(index);
  124. return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
  125. }
  126.  
  127. int
  128. FeedModel::rowCount(const QModelIndex &parent) const
  129. {
  130. if (!parent.isValid())
  131. {
  132. // Categories count
  133. QSqlQuery query(db);
  134. query.prepare ("SELECT id from categories ORDER by id;");
  135. if (query.exec())
  136. {
  137. int row_count = 1;
  138. if (!query.first())
  139. return 0;
  140. else
  141. while (query.next())
  142. row_count++;
  143. return row_count;
  144. }
  145. else
  146. return 0;
  147. }
  148. else
  149. {
  150. int category_id = parent.internalId();
  151. QSqlQuery query(db);
  152. query.prepare("SELECT id from feeds where category=:category;");
  153. query.bindValue("category", category_id);
  154. if (query.exec())
  155. {
  156. int row_count = 1;
  157. if (!query.first())
  158. return 0;
  159. else
  160. while (query.next())
  161. row_count++;
  162. return row_count;
  163. }
  164. else
  165. return 0;
  166. }
  167. }
  168.  
  169. bool
  170. FeedModel::addCategory(QString name)
  171. {
  172. QSqlQuery query(db);
  173. query.prepare("INSERT INTO categories VALUES (NULL, :name);");
  174. query.bindValue("name", name);
  175.  
  176. bool successful = query.exec();
  177. if (successful)
  178. reset();
  179. return successful;
  180. }
  181.  
  182. bool
  183. FeedModel::addFeed(QString name, QString url, quint32 category_id)
  184. {
  185. QSqlQuery query(db);
  186. query.prepare("INSERT INTO feeds VALUES (NULL, :category, :name, :url);");
  187. query.bindValue("category", category_id);
  188. query.bindValue("name", name);
  189. query.bindValue("url", url);
  190.  
  191. bool successful = query.exec();
  192. if (successful)
  193. reset();
  194. return successful;
  195. }
  196.  
  197. int
  198. FeedModel::columnCount(const QModelIndex &parent) const
  199. {
  200. Q_UNUSED(parent);
  201. return 1;
  202. }
  203.  
  204. QVariant
  205. FeedModel::data(const QModelIndex &index, int role) const
  206. {
  207. if (role == Qt::DisplayRole)
  208. {
  209. if (index.internalId() == 0)
  210. return QString ("Root");
  211. if (index.internalId() < FEEDMODEL_MAX_CATEGORIES)
  212. {
  213. QSqlQuery query(db);
  214. query.prepare ("SELECT id, name from categories WHERE id=:category;");
  215. query.bindValue("category", index.internalId());
  216. if (query.exec())
  217. {
  218. if (!query.first())
  219. return QVariant(QVariant::Invalid);
  220. else
  221. {
  222. return query.value(1).toString();
  223. }
  224. }
  225. else
  226. return QVariant(QVariant::Invalid);
  227. }
  228. else
  229. {
  230. QSqlQuery query(db);
  231. query.prepare ("SELECT id, category, name, url from feeds WHERE id=:feed;");
  232. query.bindValue("feed", index.internalId() - FEEDMODEL_MAX_CATEGORIES);
  233. if (query.exec())
  234. {
  235. if (query.first())
  236. return query.value(2).toString();
  237. else
  238. return QVariant(QVariant::Invalid);
  239. }
  240. else
  241. return QVariant(QVariant::Invalid);
  242. }
  243. }
  244. else
  245. return QVariant (QVariant::Invalid);
  246. }
  247.  
  248. QModelIndex
  249. FeedModel::parent(const QModelIndex &child) const
  250. {
  251. if (!child.isValid())
  252. return QModelIndex ();
  253.  
  254. quint32 row;
  255. quint32 id = child.internalId();
  256.  
  257. if (id == 0)
  258. return QModelIndex();
  259. else if (id < FEEDMODEL_MAX_CATEGORIES)
  260. {
  261. // Get the position of the category
  262. QSqlQuery query (db);
  263. query.prepare ("SELECT id from category;");
  264. if (query.exec ())
  265. {
  266. if (query.first ())
  267. row = 1;
  268. else
  269. return QModelIndex ();
  270. while (query.next ())
  271. {
  272. row++;
  273. if ((quint64) query.value(0).toInt() == id)
  274. break;
  275. }
  276.  
  277. return createIndex (row, 1, 0);
  278. }
  279. else
  280. return QModelIndex();
  281. }
  282. else
  283. {
  284. quint32 category_id;
  285. // We have a feed here, that actually has a real parent.
  286. // We need to get the ID of the category
  287. id -= FEEDMODEL_MAX_CATEGORIES;
  288. QSqlQuery query (db);
  289. query.prepare ("SELECT category from feeds WHERE id=:id;");
  290. query.bindValue("id", id);
  291. if (query.exec())
  292. {
  293. if (!query.first ())
  294. return QModelIndex();
  295. else
  296. {
  297. category_id = query.value(0).toInt();
  298.  
  299. // We need to get the position of the feed in the category
  300. query.prepare("SELECT id from feeds WHERE category=:category;");
  301. query.bindValue("category", category_id);
  302. if (query.exec())
  303. {
  304. row = 1;
  305. if (!query.first())
  306. return QModelIndex();
  307. else
  308. {
  309. while (query.next())
  310. row++;
  311. return createIndex(row, 1, category_id);
  312. }
  313. }
  314. else
  315. return QModelIndex();
  316. }
  317. }
  318. else
  319. return QModelIndex();
  320. }
  321. }
  322.  
  323. QVariant
  324. FeedModel::headerData(int section, Qt::Orientation orientation, int role) const
  325. {
  326. // We have a header data only for the first column, horizontal mode.
  327. if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section == 0)
  328. return tr("Feed");
  329. else
  330. return QVariant ();
  331. }
  332.  
  333. QString
  334. FeedModel::getUrl(const QModelIndex &index)
  335. {
  336. quint64 id = index.internalId();
  337. if (id < FEEDMODEL_MAX_CATEGORIES)
  338. return "";
  339. else
  340. {
  341. QSqlQuery query(db);
  342. query.prepare("SELECT url from feeds WHERE id=:id;");
  343. query.bindValue("id", id - FEEDMODEL_MAX_CATEGORIES);
  344. if (query.exec())
  345. {
  346. if (query.first())
  347. return query.value(0).toString();
  348. else
  349. return "";
  350. }
  351. else
  352. return "";
  353. }
  354. }
  355.