Added possibility to add new feeds, but not new categories (on the way).

Leonardo Robol [2011-10-24 13:25]
Added possibility to add new feeds, but not new categories (on the way).
Filename
Larss.pro
include/editfeeddialog.h
include/mainwindow.h
larss/editfeeddialog.cpp
larss/feedmodel.cpp
larss/feedpoller.cpp
larss/mainwindow.cpp
ui/editfeeddialog.ui
ui/mainwindow.ui
diff --git a/Larss.pro b/Larss.pro
index 483e055..dd58d63 100644
--- a/Larss.pro
+++ b/Larss.pro
@@ -14,13 +14,16 @@ SOURCES += larss/main.cpp\
         larss/mainwindow.cpp \
     larss/feedmodel.cpp \
     larss/rssparser.cpp \
-    larss/feedpoller.cpp
+    larss/feedpoller.cpp \
+    larss/editfeeddialog.cpp

 HEADERS  += include/mainwindow.h \
     include/feedmodel.h \
     include/rssparser.h \
-    include/feedpoller.h
+    include/feedpoller.h \
+    include/editfeeddialog.h

-FORMS    += ui/mainwindow.ui
+FORMS    += ui/mainwindow.ui \
+    ui/editfeeddialog.ui

 INCLUDEPATH += ./include
diff --git a/include/editfeeddialog.h b/include/editfeeddialog.h
new file mode 100644
index 0000000..fd95659
--- /dev/null
+++ b/include/editfeeddialog.h
@@ -0,0 +1,43 @@
+#ifndef EDITFEEDDIALOG_H
+#define EDITFEEDDIALOG_H
+
+#include <QDialog>
+#include "feedmodel.h"
+
+namespace Ui {
+    class EditFeedDialog;
+}
+
+namespace Larss {
+
+    class EditFeedDialog : public QDialog
+    {
+        Q_OBJECT
+
+    public:
+        explicit EditFeedDialog(QWidget *parent, FeedModel *feedModel);
+        ~EditFeedDialog();
+
+        /**
+         * @brief Get the name of the New feed.
+         */
+        QString getFeedName();
+
+        /**
+         * @brief Get the URL selected by the user.
+         */
+        QString getFeedUrl();
+
+        /**
+         * @brief Return the ID of the selected category.
+         */
+        quint64 getCategoryId();
+
+    private:
+        Ui::EditFeedDialog *ui;
+        FeedModel *feedModel;
+    };
+
+}
+
+#endif // EDITFEEDDIALOG_H
diff --git a/include/mainwindow.h b/include/mainwindow.h
index 6c71c04..e326cc0 100644
--- a/include/mainwindow.h
+++ b/include/mainwindow.h
@@ -30,6 +30,8 @@ private slots:

     void on_newsTableView_activated(const QModelIndex &index);

+    void on_actionAdd_Feed_triggered();
+
 private:
     Ui::MainWindow *ui;
     void do_exit();
diff --git a/larss/editfeeddialog.cpp b/larss/editfeeddialog.cpp
new file mode 100644
index 0000000..db426f7
--- /dev/null
+++ b/larss/editfeeddialog.cpp
@@ -0,0 +1,39 @@
+#include "editfeeddialog.h"
+#include "ui_editfeeddialog.h"
+#include "feedmodel.h"
+
+using namespace Larss;
+
+EditFeedDialog::EditFeedDialog(QWidget *parent, FeedModel *feedModel) :
+    QDialog(parent),
+    ui(new Ui::EditFeedDialog)
+{
+    this->feedModel = feedModel;
+    ui->setupUi(this);
+
+    // Load model in the ComboBox
+    ui->categoryComboBox->setModel(feedModel);
+}
+
+EditFeedDialog::~EditFeedDialog()
+{
+    delete ui;
+}
+
+QString
+EditFeedDialog::getFeedName()
+{
+    return ui->newFeedName->text();
+}
+
+QString
+EditFeedDialog::getFeedUrl()
+{
+    return ui->newFeedUrl->text();
+}
+
+quint64
+EditFeedDialog::getCategoryId()
+{
+    return ui->categoryComboBox->currentIndex() + 1;
+}
diff --git a/larss/feedmodel.cpp b/larss/feedmodel.cpp
index 7382f1e..104e010 100644
--- a/larss/feedmodel.cpp
+++ b/larss/feedmodel.cpp
@@ -191,6 +191,11 @@ FeedModel::addFeed(QString name, QString url, quint32 category_id)
     bool successful = query.exec();
     if (successful)
         reset();
+    else
+    {
+        qDebug() << "Query failed: " << query.executedQuery() << query.lastError();
+    }
+
     return successful;
 }

diff --git a/larss/feedpoller.cpp b/larss/feedpoller.cpp
index 592d934..d25ff93 100644
--- a/larss/feedpoller.cpp
+++ b/larss/feedpoller.cpp
@@ -111,7 +111,9 @@ FeedPoller::networkManagerReplyFinished(QNetworkReply *reply)
             QString link = element.elementsByTagName("link").item(0).firstChild().nodeValue();
             QString title = element.elementsByTagName("title").item(0).firstChild().nodeValue();
             QString description = element.elementsByTagName("description").item(0).firstChild().nodeValue();
-            QString content = element.elementsByTagName("content:encoded").item(0).firstChild().nodeValue();
+            QString content = description;
+            if (element.elementsByTagName("content:encoded").length() > 0)
+                content = element.elementsByTagName("content:encoded").item(0).firstChild().nodeValue();

             // Check if this is RSS2 or not
             uint pubDate;
diff --git a/larss/mainwindow.cpp b/larss/mainwindow.cpp
index 3c9bf8d..f15ea34 100644
--- a/larss/mainwindow.cpp
+++ b/larss/mainwindow.cpp
@@ -1,5 +1,6 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
+#include "editfeeddialog.h"
 #include <QDebug>
 #include <QtGui>

@@ -19,7 +20,6 @@ MainWindow::MainWindow(QWidget *parent) :
     // Load feedModel that will wrap the SQLite database
     feedModel = new FeedModel(db, this);
     ui->feedTreeView->setModel(feedModel);
-    ui->feedTreeView->setEditTriggers(QTreeView::DoubleClicked);

     // Load the RSSParser, hiding the unnecessary columns
     rssParser = new RssParser(db, feedModel, this);
@@ -102,3 +102,14 @@ void Larss::MainWindow::on_newsTableView_activated(const QModelIndex &index)
 {
     on_newsTableView_clicked(index);
 }
+
+void Larss::MainWindow::on_actionAdd_Feed_triggered()
+{
+    Larss::EditFeedDialog dialog(this, feedModel);
+    if (dialog.exec() == QDialog::Accepted)
+    {
+        qDebug() << "Adding new feed";
+        feedModel->addFeed(dialog.getFeedName(),
+                           dialog.getFeedUrl(), dialog.getCategoryId());
+    }
+}
diff --git a/ui/editfeeddialog.ui b/ui/editfeeddialog.ui
new file mode 100644
index 0000000..387abc2
--- /dev/null
+++ b/ui/editfeeddialog.ui
@@ -0,0 +1,112 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>EditFeedDialog</class>
+ <widget class="QDialog" name="EditFeedDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>317</width>
+    <height>187</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QHBoxLayout" name="horizontalLayout">
+   <item>
+    <layout class="QVBoxLayout" name="verticalLayout">
+     <item>
+      <widget class="QLabel" name="label_2">
+       <property name="text">
+        <string>Insert the URL of the RSS feed.</string>
+       </property>
+       <property name="scaledContents">
+        <bool>false</bool>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <layout class="QGridLayout" name="gridLayout">
+       <item row="0" column="0">
+        <widget class="QLabel" name="label_4">
+         <property name="text">
+          <string>Name</string>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="1">
+        <widget class="QLineEdit" name="newFeedName"/>
+       </item>
+       <item row="1" column="0">
+        <widget class="QLabel" name="label">
+         <property name="text">
+          <string>URL</string>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="1">
+        <widget class="QLineEdit" name="newFeedUrl"/>
+       </item>
+       <item row="2" column="0">
+        <widget class="QLabel" name="label_3">
+         <property name="text">
+          <string>Category</string>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="1">
+        <widget class="QComboBox" name="categoryComboBox"/>
+       </item>
+      </layout>
+     </item>
+     <item>
+      <widget class="QDialogButtonBox" name="buttonBox">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="standardButtons">
+        <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>EditFeedDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>EditFeedDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui
index 83d5de3..7247ffb 100644
--- a/ui/mainwindow.ui
+++ b/ui/mainwindow.ui
@@ -98,6 +98,8 @@
     <property name="title">
      <string>File</string>
     </property>
+    <addaction name="actionAdd_Feed"/>
+    <addaction name="separator"/>
     <addaction name="actionExit"/>
    </widget>
    <addaction name="menuFile"/>
@@ -108,6 +110,11 @@
     <string>Exit</string>
    </property>
   </action>
+  <action name="actionAdd_Feed">
+   <property name="text">
+    <string>Add Feed</string>
+   </property>
+  </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
  <customwidgets>
ViewGit