Make parsing of pubDate a little more flexible.

Leonardo Robol [2011-10-25 18:57]
Make parsing of pubDate a little more flexible.
Filename
larss/feedpoller.cpp
larss/main.cpp
larss/mainwindow.cpp
diff --git a/larss/feedpoller.cpp b/larss/feedpoller.cpp
index b8d6ce8..4628f13 100644
--- a/larss/feedpoller.cpp
+++ b/larss/feedpoller.cpp
@@ -159,16 +159,22 @@ FeedPoller::networkManagerReplyFinished(QNetworkReply *reply)
             uint pubDate;
             QString guid;
             QString pubDateTimeContent;
+
+            // Check if we have Guid, in RSS 1.0 was not present.
             if (element.elementsByTagName("guid").length() != 0)
-            {
                 guid = element.elementsByTagName("guid").item(0).firstChild().nodeValue();
+            else
+                guid = link;
+
+            // Check if we have pubDate
+            if (element.elementsByTagName("pubDate").length() != 0)
+            {
                 pubDateTimeContent = element.elementsByTagName("pubDate").item(0).firstChild().nodeValue();
                 pubDate = pubDateToDateTime(pubDateTimeContent).toTime_t();
             }
             else
             {
-                guid = link;
-                pubDate = QDateTime::currentDateTime().toTime_t();
+                pubDate = QDateTime::currentDateTimeUtc().toTime_t();
             }

             if (!links.contains(link))
@@ -219,10 +225,17 @@ FeedPoller::networkManagerReplyFinished(QNetworkReply *reply)
 QDateTime
 FeedPoller::pubDateToDateTime (QString pubDate)
 {
+    QString pubDateToParse;
+    // If the Day is not present in pubDate set a fake
+    // Day on top of it to make parsing working.
+    if (!pubDate.contains(","))
+        pubDateToParse = QString("Sun, %1").arg(pubDate);
+    else
+        pubDateToParse = pubDate;
     // Parsing the data, take Sun, 12 Oct 2011 15:21:12 GMT
     // pieces[0] is Sun  --- pieces[1] is 12  --- pieces[2] is Oct
     // pieces[3] is 2011 --- pieces[4] is 15:21:12 --- pieces[6] is GMT
-    QStringList pieces = pubDate.split(" ");
+    QStringList pieces = pubDateToParse.split(" ");
     QDateTime date;

     int month, year, day;
diff --git a/larss/main.cpp b/larss/main.cpp
index cd678d6..a6a4662 100644
--- a/larss/main.cpp
+++ b/larss/main.cpp
@@ -3,13 +3,15 @@

 int main(int argc, char *argv[])
 {
-    // Set some default values for our application
-    QCoreApplication::setApplicationName("Larss");
-    QCoreApplication::setOrganizationName("PHC");
-    QCoreApplication::setOrganizationDomain("phc.unipi.it");
-
     // Create the application and its main_window.
     QApplication larss_application(argc, argv);
+
+    // Set some default values for our application
+    larss_application.setApplicationName("Larss");
+    larss_application.setOrganizationName("PHC");
+    larss_application.setOrganizationDomain("phc.unipi.it");
+
+    // Create the MainWindow object
     Larss::MainWindow main_window;

     // Show the main window
diff --git a/larss/mainwindow.cpp b/larss/mainwindow.cpp
index 9c2ee14..1766f1b 100644
--- a/larss/mainwindow.cpp
+++ b/larss/mainwindow.cpp
@@ -14,11 +14,11 @@ MainWindow::MainWindow(QWidget *parent) :
 {
     ui->setupUi(this);

-    // Open the database
+    // Open the database, and create the directories for the data
+    // storage if they are not yet present in the filesystem.
     QString location = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
     if (!QFile::exists(location))
         QDir().mkpath(location);
-    qDebug() << "Data location: " << location;
     db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName(location + "/larss.db");
     db.open();
@@ -62,6 +62,15 @@ MainWindow::MainWindow(QWidget *parent) :
         resize (settings.value("width").toInt(),
                 settings.value("height").toInt());
     }
+
+    // Expand all the categories. This is will be like this until we do
+    // not implement a decent method to store open/close categories on close.
+    QStandardItem *rootItem = feedModel->invisibleRootItem();
+    for(qint32 i = 0; i < rootItem->rowCount(); i++)
+    {
+        QModelIndex index = rootItem->child(i, 0)->index();
+        ui->feedTreeView->expand(index);
+    }
 }

 MainWindow::~MainWindow()
@@ -78,6 +87,7 @@ MainWindow::loadingFeedStart(QString feedName)

 void MainWindow::closeEvent(QCloseEvent *event)
 {
+    Q_UNUSED(event);
     do_exit();
 }

@@ -156,7 +166,7 @@ bool Larss::MainWindow::eventFilter(QObject *object, QEvent *event)
         if (event->type() == QEvent::MouseButtonPress)
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
-            if (loadedNews != "")
+            if (mouseEvent->button() ==  Qt::LeftButton && loadedNews != "")
             {
                 ui->webView->load(QUrl(loadedNews));
             }
ViewGit