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 "feedpoller.h"
  2. #include <QtXml>
  3. #include "feedmodel.h"
  4. #include "feednode.h"
  5.  
  6. using namespace Larss;
  7.  
  8. FeedPoller::FeedPoller(QObject *parent, RssParser *parser, FeedModel *model) :
  9. QThread(parent)
  10. {
  11. this->parser = parser;
  12. this->model = model;
  13.  
  14. workQueue = new QList<QModelIndex> ();
  15. nowLoading = 0;
  16.  
  17. rssContent = new QHash<quint32, QString>();
  18. poll_active = true;
  19.  
  20. // Create the QNetworkAccessManager and connect the loaded signal
  21. // with our handler.
  22. manager = new QNetworkAccessManager ();
  23. manager->connect(manager, SIGNAL(finished(QNetworkReply*)),
  24. this, SLOT(networkManagerReplyFinished(QNetworkReply*)));
  25.  
  26. // We don't have a flooded bubble queue since there are no bubble queue
  27. // yet.
  28. floodedBubbleQueue = false;
  29. }
  30.  
  31. void
  32. FeedPoller::run()
  33. {
  34.  
  35. // Create the timer that will call the function every second.
  36. QTimer *timer = new QTimer ();
  37. timer->setInterval(1000);
  38. timer->connect(timer, SIGNAL(timeout()),
  39. this, SLOT(poll()));
  40. timer->start();
  41.  
  42. QTimer* updaterTimer = new QTimer();
  43. updaterTimer->setInterval(10 * 60000);
  44. updaterTimer->connect(updaterTimer, SIGNAL(timeout()),
  45. this, SLOT(queueAll()));
  46. updaterTimer->start();
  47. queueAll();
  48.  
  49. /* Connect timer to their terminations */
  50. QObject::connect(this, SIGNAL(finished()),
  51. timer, SLOT(stop()));
  52. QObject::connect(this, SIGNAL(finished()),
  53. updaterTimer, SLOT(stop()));
  54.  
  55. QThread::exec();
  56. }
  57.  
  58. bool
  59. FeedPoller::poll()
  60. {
  61. // Poll indefinitely until we are requested to exit.
  62. if (nowLoading == 0)
  63. {
  64. if (workQueue->isEmpty())
  65. return false;
  66. else
  67. {
  68. QModelIndex next_item = workQueue->takeFirst();
  69. FeedNode *node = model->itemFromIndex(next_item);
  70. nowLoading = node->id();
  71. startLoadingFeed(node->name());
  72. manager->get(QNetworkRequest(QUrl(model->getUrl(next_item))));
  73. return true;
  74. }
  75. }
  76.  
  77. return false;
  78. }
  79.  
  80. void
  81. FeedPoller::stopPolling ()
  82. {
  83. poll_active = false;
  84. QThread::exit();
  85. }
  86.  
  87. void
  88. FeedPoller::queueWork(const QModelIndex &index)
  89. {
  90. if (!index.isValid())
  91. return;
  92. FeedNode *node = model->itemFromIndex(index);
  93. if (node->type() != FeedNode::Feed)
  94. return;
  95. workQueue->append(index);
  96. }
  97.  
  98. void
  99. FeedPoller::queueAll()
  100. {
  101. QStandardItem *rootItem = model->invisibleRootItem();
  102. for(qint32 i = 0; i < rootItem->rowCount(); i++)
  103. {
  104. // Get the category
  105. FeedNode *categoryNode = model->itemFromIndex(rootItem->child(i, 0)->index());
  106.  
  107. // Get all the feed in that category
  108. for(qint32 j = 0; j < categoryNode->rowCount(); j++)
  109. {
  110. QModelIndex feedIndex = categoryNode->child(j, 0)->index ();
  111. queueWork(feedIndex);
  112. }
  113. }
  114. }
  115.  
  116. void
  117. FeedPoller::networkManagerReplyFinished(QNetworkReply *reply)
  118. {
  119. // Assume that the string is UTF-8 encoded. This is likely to be
  120. // true, but I should check it in some way.
  121. rssContent->insert (nowLoading, QString::fromUtf8(reply->readAll()));
  122.  
  123. // Now update the database with the new data obtained.
  124. QDomDocument doc;
  125. if (doc.setContent(rssContent->value(nowLoading)))
  126. {
  127. // Get transaction to support, otherwise the db will die
  128. parser->db.transaction();
  129.  
  130. // Try to catch other news_feed with the same link, so preload all of them.
  131. QSqlQuery query(parser->db);
  132. query.prepare ("SELECT link from news WHERE feed=:feed");
  133. query.bindValue("feed", nowLoading);
  134. if (!query.exec ())
  135. return;
  136.  
  137. QStringList links;
  138. if (query.first())
  139. {
  140. links.append(query.value(0).toString());
  141. while (query.next())
  142. links.append(query.value(0).toString());
  143. }
  144.  
  145. QDomElement doc_el = doc.documentElement();
  146. QDomNodeList items = doc_el.elementsByTagName("item");
  147.  
  148. for (quint32 i = 0; i < items.length(); i++)
  149. {
  150. // Get the i-th news
  151. QDomNode item = items.item(i);
  152. QDomElement element = item.toElement();
  153.  
  154. // Get the data in it
  155. QString link = element.elementsByTagName("link").item(0).firstChild().nodeValue();
  156. QString title = element.elementsByTagName("title").item(0).firstChild().nodeValue();
  157. QString description = element.elementsByTagName("description").item(0).firstChild().nodeValue();
  158. QString content = description;
  159. if (element.elementsByTagName("content:encoded").length() > 0)
  160. content = element.elementsByTagName("content:encoded").item(0).firstChild().nodeValue();
  161.  
  162. // Check if this is RSS2 or not
  163. uint pubDate;
  164. QString guid;
  165. QString pubDateTimeContent;
  166.  
  167. // Check if we have Guid, in RSS 1.0 was not present.
  168. if (element.elementsByTagName("guid").length() != 0)
  169. guid = element.elementsByTagName("guid").item(0).firstChild().nodeValue();
  170. else
  171. guid = link;
  172.  
  173. // Check if we have pubDate
  174. if (element.elementsByTagName("pubDate").length() != 0)
  175. {
  176. pubDateTimeContent = element.elementsByTagName("pubDate").item(0).firstChild().nodeValue();
  177. pubDate = pubDateToDateTime(pubDateTimeContent).toTime_t();
  178. }
  179. else
  180. {
  181. pubDate = QDateTime::currentDateTimeUtc().toTime_t();
  182. }
  183.  
  184. if (!links.contains(link))
  185. {
  186. // That means that no results were found, so let's insert this one.
  187. QSqlRecord record = parser->record();
  188. record.setValue("time", pubDate);
  189. record.setValue("read", 0);
  190. record.setValue("title", title);
  191. record.setValue("link", link);
  192. record.setValue("description", description);
  193. record.setValue("content", content);
  194. record.setValue("feed", nowLoading);
  195.  
  196. if (!parser->insertRecord(-1, record))
  197. qDebug () << "Error inserting record";
  198.  
  199. // Notify of the newly inserted record.
  200. if (floodedBubbleQueue)
  201. {
  202. // Check if enought time has passed.
  203. QDateTime now = QDateTime::currentDateTimeUtc();
  204. if (now >= lastShownBubble.addSecs(10))
  205. {
  206. lastShownBubble = now;
  207. newElementsNotification(record.value("title").toString(),
  208. record.value("description").toString());
  209. floodedBubbleQueue = false;
  210. }
  211. }
  212. else
  213. {
  214. QDateTime now = QDateTime::currentDateTimeUtc();
  215. if (now <= lastShownBubble.addSecs(10))
  216. {
  217. floodedBubbleQueue = true;
  218. lastShownBubble = now;
  219. newElementsNotification(tr("New items to read"),
  220. tr("New elements have been loaded in Larss."));
  221. }
  222. else
  223. {
  224. lastShownBubble = QDateTime::currentDateTimeUtc();
  225. newElementsNotification(record.value("title").toString(),
  226. record.value("description").toString());
  227. }
  228. }
  229.  
  230. // Yield to try making the UI responsive.
  231. yieldCurrentThread();
  232. }
  233. }
  234.  
  235. if (!parser->submitAll())
  236. qDebug() << "Error submitting new data";
  237.  
  238. // Commit the changes.
  239. parser->db.commit();
  240. }
  241. else
  242. qDebug () << "Error parsing the document";
  243.  
  244. nowLoading = 0;
  245. finishedLoadingFeed("");
  246.  
  247. return;
  248.  
  249. // Check if there is work in the queue
  250. if (!workQueue->isEmpty())
  251. {
  252. QModelIndex next_item = workQueue->takeFirst();
  253. FeedNode *node = (FeedNode*) next_item.internalPointer();
  254. nowLoading = node->id();
  255. startLoadingFeed(node->name());
  256. manager->get(QNetworkRequest(QUrl(model->getUrl(next_item))));
  257. }
  258. }
  259.  
  260. QDateTime
  261. FeedPoller::pubDateToDateTime (QString pubDate)
  262. {
  263. QString pubDateToParse;
  264. // If the Day is not present in pubDate set a fake
  265. // Day on top of it to make parsing working.
  266. if (!pubDate.contains(","))
  267. pubDateToParse = QString("Sun, %1").arg(pubDate);
  268. else
  269. pubDateToParse = pubDate;
  270. // Parsing the data, take Sun, 12 Oct 2011 15:21:12 GMT
  271. // pieces[0] is Sun --- pieces[1] is 12 --- pieces[2] is Oct
  272. // pieces[3] is 2011 --- pieces[4] is 15:21:12 --- pieces[6] is GMT
  273. QStringList pieces = pubDateToParse.split(" ");
  274. QDateTime date;
  275.  
  276. int month, year, day;
  277.  
  278. // Set the day
  279. day = pieces.at(1).toInt();
  280.  
  281. // Set the month
  282. if (pieces.at(2) == "Jan")
  283. month = 1;
  284. else if (pieces.at(2) == "Feb")
  285. month = 2;
  286. else if (pieces.at(2) == "Mar")
  287. month = 3;
  288. else if (pieces.at(2) == "Apr")
  289. month = 4;
  290. else if (pieces.at(2) == "May")
  291. month = 5;
  292. else if (pieces.at(2) == "Jun")
  293. month = 6;
  294. else if (pieces.at(2) == "Jul")
  295. month = 7;
  296. else if (pieces.at(2) == "Aug")
  297. month = 8;
  298. else if (pieces.at(2) == "Sep")
  299. month = 9;
  300. else if (pieces.at(2) == "Oct")
  301. month = 10;
  302. else if (pieces.at(2) == "Nov")
  303. month = 11;
  304. else if (pieces.at(2) == "Dec")
  305. month = 12;
  306.  
  307. // Set the year
  308. year = pieces.at(3).toInt();
  309.  
  310. date.setDate(QDate (year, month, day));
  311.  
  312. // Set the hour
  313. date.setTime(QTime::fromString(pieces[4], "hh:mm:ss"));
  314.  
  315. return date;
  316. }