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 "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "editfeeddialog.h"
  4. #include "editcategorydialog.h"
  5. #include "unreadcountitemdelegate.h"
  6. #include <QDebug>
  7. #include <QtGui>
  8.  
  9. using namespace Larss;
  10.  
  11. MainWindow::MainWindow(QWidget *parent) :
  12. QMainWindow(parent),
  13. ui(new Ui::MainWindow)
  14. {
  15. ui->setupUi(this);
  16.  
  17. // Open the database, and create the directories for the data
  18. // storage if they are not yet present in the filesystem.
  19. QString location = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
  20. if (!QFile::exists(location))
  21. QDir().mkpath(location);
  22. db = QSqlDatabase::addDatabase("QSQLITE");
  23. db.setDatabaseName(location + "/larss.db");
  24. db.open();
  25.  
  26. // Load feedModel that will wrap the SQLite database
  27. feedModel = new FeedModel(db, this);
  28. rssParser = new RssParser(db, feedModel, this);
  29.  
  30. // Create the delegate that counts unread posts.
  31. ui->feedTreeView->setModel(feedModel);
  32. ui->feedTreeView->setItemDelegate (new UnReadCountItemDelegate(feedModel, rssParser));
  33.  
  34. // Load the RSSParser, hiding the unnecessary columns and applying
  35. // some custom style :)
  36. ui->newsTableView->setModel(rssParser);
  37. ui->newsTableView->setColumnHidden(0, true); // ID
  38. ui->newsTableView->setColumnHidden(1, true); // Feed ID
  39. ui->newsTableView->setColumnHidden(3, true); // Link
  40. ui->newsTableView->setColumnHidden(4, true); // Description
  41. ui->newsTableView->setColumnHidden(5, true); // Content
  42. ui->newsTableView->setColumnHidden(7, true); // Read state
  43. ui->newsTableView->horizontalHeader()->setResizeMode(2, QHeaderView::Stretch);
  44. ui->newsTableView->horizontalHeader()->setResizeMode(6, QHeaderView::ResizeToContents);
  45. ui->newsTableView->setAlternatingRowColors(true);
  46.  
  47. // Show nothing for now.
  48. rssParser->setFilter("1 = 0");
  49.  
  50. poller = new FeedPoller (this, rssParser, feedModel);
  51. poller->connect(poller, SIGNAL(startLoadingFeed(QString)), this,
  52. SLOT(loadingFeedStart(QString)));
  53. poller->start();
  54.  
  55. // Install event filter to handle particular events, such as mouseclicks that
  56. // triggers original article loading.
  57. ui->webViewTitleLabel->installEventFilter(this);
  58. loadedNews = "";
  59.  
  60. // Give the window the right size, if present in the
  61. // settings.
  62. QSettings settings;
  63. if (settings.contains("width") && settings.contains("height"))
  64. {
  65. resize (settings.value("width").toInt(),
  66. settings.value("height").toInt());
  67. }
  68.  
  69. // Expand all the categories. This is will be like this until we do
  70. // not implement a decent method to store open/close categories on close.
  71. QStandardItem *rootItem = feedModel->invisibleRootItem();
  72. for(qint32 i = 0; i < rootItem->rowCount(); i++)
  73. {
  74. QModelIndex index = rootItem->child(i, 0)->index();
  75. ui->feedTreeView->expand(index);
  76. }
  77.  
  78. // Create the systray icon
  79. QIcon larssIcon(":larss32.png");
  80. systrayIcon = new QSystemTrayIcon (larssIcon);
  81. systrayIcon->show();
  82.  
  83. // Set the icon for the window.
  84. poller->connect(poller, SIGNAL(newElementsNotification(QString,QString)),
  85. this, SLOT(showNewElement(QString,QString)));
  86. }
  87.  
  88. MainWindow::~MainWindow()
  89. {
  90. db.close();
  91. delete systrayIcon;
  92. delete ui;
  93. delete feedModel;
  94. delete rssParser;
  95. delete poller;
  96. }
  97.  
  98. void
  99. MainWindow::loadingFeedStart(QString feedName)
  100. {
  101. ui->statusBar->showMessage(tr("Updating feed '%1'...").arg(feedName), 2000);
  102. }
  103.  
  104. void
  105. MainWindow::showNewElement(QString title, QString body)
  106. {
  107. systrayIcon->showMessage(title, body);
  108. }
  109.  
  110. void MainWindow::closeEvent(QCloseEvent *event)
  111. {
  112. Q_UNUSED(event);
  113. do_exit();
  114. }
  115.  
  116. void MainWindow::do_exit()
  117. {
  118. // Kill the poller
  119. poller->stopPolling();
  120. poller->wait();
  121.  
  122. // Save window size
  123. QSettings settings;
  124. settings.setValue("width", width());
  125. settings.setValue("height", height());
  126.  
  127. QApplication::exit();
  128. }
  129.  
  130. void MainWindow::on_actionExit_activated()
  131. {
  132. // Exit the application
  133. do_exit();
  134. }
  135.  
  136. void Larss::MainWindow::on_feedTreeView_clicked(const QModelIndex &index)
  137. {
  138. // Set the active filter
  139. quint64 feed_id;
  140. if ((feed_id = rssParser->getFeed (index)))
  141. {
  142. rssParser->selectActiveFeed(feed_id);
  143.  
  144. // Reset the title
  145. ui->webViewTitleLabel->setText("");
  146. }
  147.  
  148.  
  149. }
  150.  
  151. void Larss::MainWindow::on_newsTableView_clicked(const QModelIndex &index)
  152. {
  153. // A row got activated, so open it in the webview.
  154. loadFeed(index);
  155. }
  156.  
  157. void Larss::MainWindow::on_newsTableView_activated(const QModelIndex &index)
  158. {
  159. on_newsTableView_clicked(index);
  160. }
  161.  
  162. void Larss::MainWindow::on_actionAdd_Feed_triggered()
  163. {
  164. Larss::EditFeedDialog dialog(this, feedModel);
  165. if (dialog.exec() == QDialog::Accepted)
  166. {
  167. feedModel->addFeed(dialog.getFeedName(),
  168. dialog.getFeedUrl(), dialog.getCategory());
  169. }
  170. }
  171.  
  172. void Larss::MainWindow::on_actionAdd_Category_triggered()
  173. {
  174. Larss::EditCategoryDialog dialog(this);
  175. if (dialog.exec() == QDialog::Accepted)
  176. {
  177. feedModel->addCategory(dialog.getCategoryName());
  178. }
  179. }
  180.  
  181. bool Larss::MainWindow::eventFilter(QObject *object, QEvent *event)
  182. {
  183. if (object == ui->webViewTitleLabel)
  184. {
  185. if (event->type() == QEvent::MouseButtonPress)
  186. {
  187. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  188. if (mouseEvent->button() == Qt::LeftButton && loadedNews != "")
  189. {
  190. ui->webView->load(QUrl(loadedNews));
  191. }
  192. return true;
  193. }
  194. else
  195. return false;
  196. }
  197. else
  198. return QMainWindow::eventFilter(object, event);
  199.  
  200. }
  201.  
  202. void Larss::MainWindow::loadFeed(const QModelIndex& index)
  203. {
  204. quint64 rowNumber = index.row();
  205. loadedNews = rssParser->getLink(index);
  206. ui->webView->setHtml(rssParser->getContent(index));
  207.  
  208. // Select the right title
  209. ui->webViewTitleLabel->setText(QString("<b>%1</b>").arg(rssParser->getTitle (index)));
  210.  
  211. // And then mark it as read
  212. rssParser->setReadStatus(index, true);
  213. ui->newsTableView->selectRow(rowNumber);
  214. }
  215.  
  216. void Larss::MainWindow::on_actionNext_unread_news_triggered()
  217. {
  218. QModelIndex index = ui->newsTableView->selectionModel()->currentIndex();
  219. QItemSelection startingFeedSelection = ui->feedTreeView->selectionModel()->selection();
  220. int startingNewsRow = ui->newsTableView->currentIndex().row();
  221. QModelIndex feedIndex = ui->feedTreeView->currentIndex();
  222. QModelIndex categoryIndex;
  223. int startingCategoryRow;
  224. int nextUnread;
  225.  
  226. // Store the category or the index that is selected now, to be sure that after
  227. // a complete round searching for new items we don't begin one time more.
  228. FeedNode *selectedNode = feedModel->itemFromIndex(feedIndex);
  229. int categoriesNumber = feedModel->rowCount();
  230.  
  231. if (selectedNode->type() == FeedNode::Category)
  232. {
  233. startingCategoryRow = feedIndex.row();
  234.  
  235. // Search for a feed, not a category
  236. while (feedModel->rowCount(feedIndex) == 0)
  237. {
  238. // Step to the next category, since this is empty
  239. if (feedIndex.row() == categoriesNumber - 1)
  240. feedIndex = feedModel->index(0, 0);
  241. else
  242. feedIndex = feedModel->index(feedIndex.row() + 1, 0);
  243.  
  244. // Check if that was the starting category, in that case they are all empty
  245. // so simply do nothing
  246. if (feedIndex.row() == startingCategoryRow)
  247. {
  248. ui->feedTreeView->selectionModel()->select(startingFeedSelection, QItemSelectionModel::SelectCurrent);
  249. on_feedTreeView_clicked(ui->feedTreeView->currentIndex());
  250. ui->newsTableView->selectRow(startingNewsRow);
  251. loadFeed (ui->newsTableView->selectionModel()->currentIndex());
  252. return;
  253. }
  254. }
  255.  
  256. // Now we have a category with at least one child, so select id
  257. categoryIndex = feedIndex;
  258. feedIndex = feedIndex.child(0, 0);
  259. }
  260. else
  261. categoryIndex = feedIndex.parent();
  262.  
  263. // Store the row of the starting catogory
  264. startingCategoryRow = categoryIndex.row();
  265.  
  266. while ((nextUnread = rssParser->getNextUnread(index)) <= 0)
  267. {
  268. // If we have an invalid feedIndex return
  269. if (!feedIndex.isValid())
  270. {
  271. ui->feedTreeView->selectionModel()->select(startingFeedSelection, QItemSelectionModel::SelectCurrent);
  272. on_feedTreeView_clicked(ui->feedTreeView->currentIndex());
  273. ui->newsTableView->selectRow(startingNewsRow);
  274. loadFeed (ui->newsTableView->selectionModel()->currentIndex());
  275. return;
  276. }
  277.  
  278. // Check if there is a category next in which to step, if there
  279. // are no more feeds in this.
  280. if (feedIndex.row() == feedModel->rowCount(categoryIndex) - 1)
  281. {
  282. // Continue to iterate over categories while we found a non
  283. // empty one OR we arrive to the starting one.
  284. do
  285. {
  286. if (categoryIndex.row() < feedModel->rowCount())
  287. categoryIndex = feedModel->index(categoryIndex.row() + 1, 0);
  288. else
  289. categoryIndex = feedModel->index(0, 0);
  290. } while (feedModel->rowCount(categoryIndex) == 0 &&
  291. categoryIndex.row() != startingCategoryRow);
  292.  
  293. // We have done a complete round
  294. if (categoryIndex.row() == startingCategoryRow)
  295. {
  296. ui->feedTreeView->selectionModel()->select(startingFeedSelection, QItemSelectionModel::SelectCurrent);
  297. on_feedTreeView_clicked(ui->feedTreeView->currentIndex());
  298. ui->newsTableView->selectRow(startingNewsRow);
  299. loadFeed (ui->newsTableView->selectionModel()->currentIndex());
  300. return;
  301. }
  302.  
  303. // Got the Feed and select the first element.
  304. feedIndex = categoryIndex.child(0, 0);
  305. }
  306. else
  307. {
  308. // Otherwise step to the next feed
  309. feedIndex = categoryIndex.child(feedIndex.row() + 1, 0);
  310. }
  311.  
  312. ui->feedTreeView->selectionModel()->select(QItemSelection(feedIndex, feedIndex), QItemSelectionModel::SelectCurrent);
  313. on_feedTreeView_clicked(feedIndex);
  314. index = rssParser->index(0, 0);
  315.  
  316. // This feed may be unread, so in that case, read it.
  317. if (rssParser->record(index.row()).value("read").toInt() == 0)
  318. {
  319. ui->newsTableView->selectRow(0);
  320. loadFeed(index);
  321. return;
  322. }
  323. }
  324.  
  325. // Selec the next unread news in the feed.
  326. if (nextUnread > 0)
  327. {
  328. ui->newsTableView->selectRow(nextUnread);
  329. loadFeed (ui->newsTableView->selectionModel()->currentIndex());
  330. }
  331. }
  332.  
  333.  
  334. void Larss::MainWindow::on_actionUpdate_this_feed_triggered()
  335. {
  336. QModelIndex index = ui->feedTreeView->selectionModel()->currentIndex();
  337. if (index.isValid())
  338. {
  339. poller->queueWork(index);
  340. }
  341. }
  342.  
  343. void Larss::MainWindow::on_feedTreeView_customContextMenuRequested(const QPoint &pos)
  344. {
  345. QModelIndex index = ui->feedTreeView->indexAt(pos);
  346. if (index.isValid())
  347. {
  348. // Check if this is a category or not.
  349. FeedNode *node = feedModel->itemFromIndex(index);
  350.  
  351. QMenu popupMenu (this);
  352.  
  353. if (node->type() == FeedNode::Feed)
  354. {
  355. popupMenu.addAction(tr("Update"), this, SLOT(on_actionUpdate_this_feed_triggered()));
  356. popupMenu.addAction(tr("Remove"), this,
  357. SLOT(removeSelectedFeed()));
  358. }
  359. else if (node->type() == FeedNode::Category)
  360. {
  361. popupMenu.addAction(tr("Remove"), this,
  362. SLOT(removeSelectedCategory()));
  363. }
  364.  
  365. popupMenu.exec(ui->feedTreeView->mapToGlobal(pos));
  366. }
  367. }
  368.  
  369. void Larss::MainWindow::removeSelectedFeed()
  370. {
  371. QModelIndex index = ui->feedTreeView->currentIndex();
  372.  
  373. // We need to remove all the feeds from that category
  374. rssParser->removeNewsForFeed(index);
  375.  
  376. // And then remove the feed itself.
  377. feedModel->removeElement(index.row(), index.parent());
  378. }
  379.  
  380. void Larss::MainWindow::removeSelectedCategory()
  381. {
  382. // Check that the category is empty before removing it
  383. QModelIndex index = ui->feedTreeView->currentIndex();
  384. if (feedModel->rowCount(index) > 0)
  385. {
  386. QMessageBox messageBox;
  387. messageBox.setText("Impossible to remove category");
  388. messageBox.setInformativeText("Please remove feeds in this category before trying to remove it.");
  389.  
  390. messageBox.exec ();
  391. }
  392. else
  393. {
  394. feedModel->removeElement(index.row());
  395. }
  396. }