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.  
  17. FeedModel::~FeedModel()
  18. {
  19. }
  20.  
  21.  
  22. QModelIndex
  23. FeedModel::index(int row, int column, const QModelIndex &parent) const
  24. {
  25. if (parent.internalId() == 0)
  26. {
  27. QSqlQuery query (db);
  28. query.prepare("SELECT id from categories;");
  29. if (query.exec())
  30. {
  31. if (!query.first())
  32. return QModelIndex();
  33. for (int i = 1; i < row; i++)
  34. {
  35. if (!query.next ())
  36. return QModelIndex();
  37. }
  38. return createIndex(row, column, query.value(0).toInt());
  39. }
  40. else
  41. return QModelIndex();
  42. }
  43. else
  44. {
  45. QSqlQuery query(db);
  46. query.prepare ("SELECT id from feeds WHERE category=:category ORDER BY id;");
  47. query.bindValue("category", parent.internalId());
  48. if (query.exec())
  49. {
  50. if (!query.first())
  51. return QModelIndex();
  52. else
  53. {
  54. for(int i = 1; i < row; i++)
  55. {
  56. if (!query.next())
  57. return QModelIndex();
  58. }
  59. return createIndex(row, column, query.value(0).toInt() + FEEDMODEL_MAX_CATEGORIES);
  60. }
  61. }
  62. else
  63. return QModelIndex();
  64. }
  65. }
  66.  
  67. int
  68. FeedModel::rowCount(const QModelIndex &parent) const
  69. {
  70. if (!parent.isValid())
  71. {
  72. // Categories count
  73. QSqlQuery query(db);
  74. query.prepare ("SELECT id from categories ORDER by id;");
  75. if (query.exec())
  76. {
  77. int row_count = 1;
  78. if (!query.first())
  79. return 0;
  80. else
  81. while (query.next())
  82. row_count++;
  83. return row_count;
  84. }
  85. else
  86. return 0;
  87. }
  88. else
  89. {
  90. int category_id = parent.internalId();
  91. QSqlQuery query(db);
  92. query.prepare("SELECT id from feeds where category=:category;");
  93. query.bindValue("category", category_id);
  94. if (query.exec())
  95. {
  96. int row_count = 1;
  97. if (!query.first())
  98. return 0;
  99. else
  100. while (query.next())
  101. row_count++;
  102. return row_count;
  103. }
  104. else
  105. return 0;
  106. }
  107. }
  108.  
  109. int
  110. FeedModel::columnCount(const QModelIndex &parent) const
  111. {
  112. return 1;
  113. }
  114.  
  115. QVariant
  116. FeedModel::data(const QModelIndex &index, int role) const
  117. {
  118. if (role == Qt::DisplayRole)
  119. {
  120. if (index.internalId() == 0)
  121. return QString ("Root");
  122. if (index.internalId() < FEEDMODEL_MAX_CATEGORIES)
  123. {
  124. QSqlQuery query(db);
  125. query.prepare ("SELECT name from categories WHERE id=:category;");
  126. query.bindValue("category", index.internalId());
  127. if (query.exec())
  128. {
  129. if (!query.first())
  130. return QVariant(QVariant::Invalid);
  131. else
  132. return query.value(0).toString();
  133. }
  134. else
  135. return QVariant(QVariant::Invalid);
  136. }
  137. else
  138. {
  139. QSqlQuery query(db);
  140. query.prepare ("SELECT name from feeds WHERE id=:feed;");
  141. query.bindValue("feed", index.internalId() - FEEDMODEL_MAX_CATEGORIES);
  142. if (query.exec())
  143. {
  144. if (query.first())
  145. return query.value(0).toString();
  146. else
  147. return QVariant(QVariant::Invalid);
  148. }
  149. else
  150. return QVariant(QVariant::Invalid);
  151. }
  152. }
  153. else
  154. return QVariant (QVariant::Invalid);
  155. }
  156.  
  157. QModelIndex
  158. FeedModel::parent(const QModelIndex &child) const
  159. {
  160. if (!child.isValid())
  161. return QModelIndex ();
  162.  
  163. quint32 row;
  164. quint32 id = child.internalId();
  165.  
  166. if (id == 0)
  167. return QModelIndex();
  168. else if (id < FEEDMODEL_MAX_CATEGORIES)
  169. {
  170. // Get the position of the category
  171. QSqlQuery query (db);
  172. query.prepare ("SELECT id from category;");
  173. if (query.exec ())
  174. {
  175. if (query.first ())
  176. row = 1;
  177. else
  178. return QModelIndex ();
  179. while (query.next ())
  180. {
  181. row++;
  182. if ((quint64) query.value(0).toInt() == id)
  183. break;
  184. }
  185.  
  186. return createIndex (row, 1, 0);
  187. }
  188. else
  189. return QModelIndex();
  190. }
  191. else
  192. {
  193. quint32 category_id;
  194. // We have a feed here, that actually has a real parent.
  195. // We need to get the ID of the category
  196. id -= FEEDMODEL_MAX_CATEGORIES;
  197. QSqlQuery query (db);
  198. query.prepare ("SELECT category from feeds WHERE id=:id;");
  199. query.bindValue("id", id);
  200. if (query.exec())
  201. {
  202. if (!query.first ())
  203. return QModelIndex();
  204. else
  205. {
  206. category_id = query.value(0).toInt();
  207.  
  208. // We need to get the position of the feed in the category
  209. query.prepare("SELECT id from feeds WHERE category=:category;");
  210. query.bindValue("category", category_id);
  211. if (query.exec())
  212. {
  213. row = 1;
  214. if (!query.first())
  215. return QModelIndex();
  216. else
  217. {
  218. while (query.next())
  219. row++;
  220. return createIndex(row, 1, category_id);
  221. }
  222. }
  223. else
  224. return QModelIndex();
  225. }
  226. }
  227. else
  228. return QModelIndex();
  229. }
  230. }
  231.  
  232. QVariant
  233. FeedModel::headerData(int section, Qt::Orientation orientation, int role) const
  234. {
  235. // We have a header data only for the first column, horizontal mode.
  236. if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section == 0)
  237. return tr("Feed");
  238. else
  239. return QVariant ();
  240. }
  241.  
  242. QString
  243. FeedModel::getUrl(const QModelIndex &index)
  244. {
  245. quint64 id = index.internalId();
  246. if (id < FEEDMODEL_MAX_CATEGORIES)
  247. return "";
  248. else
  249. {
  250. QSqlQuery query(db);
  251. query.prepare("SELECT url from feeds WHERE id=:id;");
  252. query.bindValue("id", id - FEEDMODEL_MAX_CATEGORIES);
  253. if (query.exec())
  254. {
  255. if (query.first())
  256. return query.value(0).toString();
  257. else
  258. return "";
  259. }
  260. else
  261. return "";
  262. }
  263. }
  264.