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. if (index.internalId() < FEEDMODEL_MAX_CATEGORIES && index.internalId() != 0)
  92. {
  93. // We are trying to modify a category
  94.  
  95. switch (index.column())
  96. {
  97. case 0:
  98. // ID
  99. query.prepare("UPDATE categories SET id=:value WHERE id=:id;");
  100. break;
  101. case 1:
  102. // Name
  103. query.prepare("UPDATE categories SET name=:value WHERE id=:id;");
  104. break;
  105. }
  106.  
  107. query.bindValue("value", value.toString());
  108. query.bindValue("id", index.internalId());
  109. }
  110. else
  111. {
  112. // We are trying to modify a feed
  113. switch (index.column())
  114. {
  115. case 0:
  116. // ID
  117. query.prepare("UPDATE categories SET id=:value WHERE id=:id;");
  118. break;
  119. case 1:
  120. // Category
  121. query.prepare("UPDATE categories SET category=:value WHERE id=:id;");
  122. break;
  123. case 2:
  124. // Name
  125. query.prepare("UPDATE categories SET name=:value WHERE id=:id;");
  126. break;
  127. case 3:
  128. // Url
  129. query.prepare("UPDATE categories SET url=:value WHERE id=:id;");
  130. break;
  131. }
  132.  
  133. query.bindValue("value", value);
  134. query.bindValue("id", index.internalId() - FEEDMODEL_MAX_CATEGORIES);
  135. }
  136.  
  137. if (!query.exec())
  138. {
  139. qDebug() << "Query failed" << query.lastError() << query.executedQuery();
  140. return false;
  141. }
  142. else
  143. {
  144. // Emit the datachanged signal
  145. return true;
  146. }
  147. }
  148.  
  149. int
  150. FeedModel::rowCount(const QModelIndex &parent) const
  151. {
  152. if (!parent.isValid())
  153. {
  154. // Categories count
  155. QSqlQuery query(db);
  156. query.prepare ("SELECT id from categories ORDER by id;");
  157. if (query.exec())
  158. {
  159. int row_count = 1;
  160. if (!query.first())
  161. return 0;
  162. else
  163. while (query.next())
  164. row_count++;
  165. return row_count;
  166. }
  167. else
  168. return 0;
  169. }
  170. else
  171. {
  172. int category_id = parent.internalId();
  173. QSqlQuery query(db);
  174. query.prepare("SELECT id from feeds where category=:category;");
  175. query.bindValue("category", category_id);
  176. if (query.exec())
  177. {
  178. int row_count = 1;
  179. if (!query.first())
  180. return 0;
  181. else
  182. while (query.next())
  183. row_count++;
  184. return row_count;
  185. }
  186. else
  187. return 0;
  188. }
  189. }
  190.  
  191. bool
  192. FeedModel::addCategory(QString name)
  193. {
  194. QSqlQuery query(db);
  195. query.prepare("INSERT INTO categories VALUES (NULL, :name);");
  196. query.bindValue("name", name);
  197.  
  198. bool successful = query.exec();
  199. if (successful)
  200. reset();
  201. return successful;
  202. }
  203.  
  204. bool
  205. FeedModel::addFeed(QString name, QString url, quint32 category_id)
  206. {
  207. QSqlQuery query(db);
  208. query.prepare("INSERT INTO feeds VALUES (NULL, :category, :name, :url);");
  209. query.bindValue("category", category_id);
  210. query.bindValue("name", name);
  211. query.bindValue("url", url);
  212.  
  213. bool successful = query.exec();
  214. if (successful)
  215. reset();
  216. return successful;
  217. }
  218.  
  219. int
  220. FeedModel::columnCount(const QModelIndex &parent) const
  221. {
  222. Q_UNUSED(parent);
  223. return 1;
  224. }
  225.  
  226. QVariant
  227. FeedModel::data(const QModelIndex &index, int role) const
  228. {
  229. if (role == Qt::DisplayRole)
  230. {
  231. if (index.internalId() == 0)
  232. return QString ("Root");
  233. if (index.internalId() < FEEDMODEL_MAX_CATEGORIES)
  234. {
  235. QSqlQuery query(db);
  236. query.prepare ("SELECT id, name from categories WHERE id=:category;");
  237. query.bindValue("category", index.internalId());
  238. if (query.exec())
  239. {
  240. if (!query.first())
  241. return QVariant(QVariant::Invalid);
  242. else
  243. {
  244. return query.value(1).toString();
  245. }
  246. }
  247. else
  248. return QVariant(QVariant::Invalid);
  249. }
  250. else
  251. {
  252. QSqlQuery query(db);
  253. query.prepare ("SELECT id, category, name, url from feeds WHERE id=:feed;");
  254. query.bindValue("feed", index.internalId() - FEEDMODEL_MAX_CATEGORIES);
  255. if (query.exec())
  256. {
  257. if (query.first())
  258. return query.value(2).toString();
  259. else
  260. return QVariant(QVariant::Invalid);
  261. }
  262. else
  263. return QVariant(QVariant::Invalid);
  264. }
  265. }
  266. else
  267. return QVariant (QVariant::Invalid);
  268. }
  269.  
  270. QModelIndex
  271. FeedModel::parent(const QModelIndex &child) const
  272. {
  273. if (!child.isValid())
  274. return QModelIndex ();
  275.  
  276. quint32 row;
  277. quint32 id = child.internalId();
  278.  
  279. if (id == 0)
  280. return QModelIndex();
  281. else if (id < FEEDMODEL_MAX_CATEGORIES)
  282. {
  283. // Get the position of the category
  284. QSqlQuery query (db);
  285. query.prepare ("SELECT id from category;");
  286. if (query.exec ())
  287. {
  288. if (query.first ())
  289. row = 1;
  290. else
  291. return QModelIndex ();
  292. while (query.next ())
  293. {
  294. row++;
  295. if ((quint64) query.value(0).toInt() == id)
  296. break;
  297. }
  298.  
  299. return createIndex (row, 1, 0);
  300. }
  301. else
  302. return QModelIndex();
  303. }
  304. else
  305. {
  306. quint32 category_id;
  307. // We have a feed here, that actually has a real parent.
  308. // We need to get the ID of the category
  309. id -= FEEDMODEL_MAX_CATEGORIES;
  310. QSqlQuery query (db);
  311. query.prepare ("SELECT category from feeds WHERE id=:id;");
  312. query.bindValue("id", id);
  313. if (query.exec())
  314. {
  315. if (!query.first ())
  316. return QModelIndex();
  317. else
  318. {
  319. category_id = query.value(0).toInt();
  320.  
  321. // We need to get the position of the feed in the category
  322. query.prepare("SELECT id from feeds WHERE category=:category;");
  323. query.bindValue("category", category_id);
  324. if (query.exec())
  325. {
  326. row = 1;
  327. if (!query.first())
  328. return QModelIndex();
  329. else
  330. {
  331. while (query.next())
  332. row++;
  333. return createIndex(row, 1, category_id);
  334. }
  335. }
  336. else
  337. return QModelIndex();
  338. }
  339. }
  340. else
  341. return QModelIndex();
  342. }
  343. }
  344.  
  345. QVariant
  346. FeedModel::headerData(int section, Qt::Orientation orientation, int role) const
  347. {
  348. // We have a header data only for the first column, horizontal mode.
  349. if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section == 0)
  350. return tr("Feed");
  351. else
  352. return QVariant ();
  353. }
  354.  
  355. QString
  356. FeedModel::getUrl(const QModelIndex &index)
  357. {
  358. quint64 id = index.internalId();
  359. if (id < FEEDMODEL_MAX_CATEGORIES)
  360. return "";
  361. else
  362. {
  363. QSqlQuery query(db);
  364. query.prepare("SELECT url from feeds WHERE id=:id;");
  365. query.bindValue("id", id - FEEDMODEL_MAX_CATEGORIES);
  366. if (query.exec())
  367. {
  368. if (query.first())
  369. return query.value(0).toString();
  370. else
  371. return "";
  372. }
  373. else
  374. return "";
  375. }
  376. }
  377.