Parse the feed content in the Feed class.
authorJan Dumon <j.dumon@option.com>
Thu, 11 Mar 2010 20:30:38 +0000 (21:30 +0100)
committerJan Dumon <j.dumon@option.com>
Thu, 11 Mar 2010 20:30:38 +0000 (21:30 +0100)
src/googlereader.cpp
src/googlereader.h

index 3d7ab15..a10f783 100644 (file)
@@ -36,7 +36,64 @@ void Feed::fetch(bool cont) {
                url.addEncodedQueryItem("c", continuation.toUtf8());
 
        request.setUrl(url);
-       reader->getManager()->get(request);
+       reply = reader->getManager()->get(request);
+       connect(reply, SIGNAL(finished()), SLOT(fetchFinished()));
+}
+
+void Feed::fetchFinished() {
+       if (reply->error()) {
+               qDebug() << "Download of" << reply->url() << "failed:" << qPrintable(reply->errorString());
+               return;
+       }
+
+       QJson::Parser parser;
+       bool ok;
+       QVariantMap result = parser.parse(reply->readAll(), &ok).toMap();
+       QString continuation;
+
+       continuation = result["continuation"].toString();
+
+       foreach(QVariant l, result["items"].toList()) {
+               QVariantMap e = l.toMap();
+               Entry *entry = new Entry();;
+               QString content, summary;
+
+               entry->id = e["id"].toString();
+               entry->published = QDateTime::fromTime_t(e["published"].toUInt());
+               entry->author = e["author"].toString();
+               entry->source = (e["origin"].toMap())["streamId"].toString();
+               entry->link = (e["alternate"].toMap())["href"].toString();
+
+               content = (e["content"].toMap())["content"].toString();
+               summary = (e["summary"].toMap())["content"].toString();
+               if(content != "") entry->content = content; else entry->content = summary;
+
+               if(e["isReadStateLocked"].toBool())
+                       entry->flags |= ENTRY_FLAG_LOCKED | ENTRY_FLAG_READ;
+
+               QWebPage p;
+               p.mainFrame()->setHtml(e["title"].toString());
+               entry->title = p.mainFrame()->toPlainText();
+
+               foreach(QVariant c, e["categories"].toList()) {
+                       QString cat = c.toString();
+                       if(cat.endsWith("/state/com.google/read"))
+                               entry->flags |= ENTRY_FLAG_READ;
+                       else if(cat.endsWith("/state/com.google/starred"))
+                               entry->flags |= ENTRY_FLAG_STARRED;
+                       else if(cat.endsWith("/state/com.google/broadcast"))
+                               entry->flags |= ENTRY_FLAG_SHARED;
+               }
+
+               entry->feed = this;
+               addEntry(entry);
+       }
+
+       lastUpdated = QDateTime::currentDateTime();
+
+       emit updateFeedComplete();
+
+       reply->deleteLater();
 }
 
 GoogleReader::GoogleReader() {
@@ -134,77 +191,10 @@ void GoogleReader::downloadFinished(QNetworkReply *reply) {
                QByteArray data = reply->readAll();
                //qDebug() << "Result:" << data;
        }
-       else {
-               parseFeed(reply->readAll());
-       }
 
        reply->deleteLater();
 }
 
-void GoogleReader::parseFeed(QByteArray data) {
-       QJson::Parser parser;
-       bool ok;
-       QVariantMap result = parser.parse(data, &ok).toMap();
-       QString continuation, feedsource, id;
-       Feed *feed = NULL;
-
-       id = result["id"].toString();
-       continuation = result["continuation"].toString();
-
-       /* TODO: This hack should not be needed.. Figure out a cleaner way */
-       if(id.endsWith("/state/com.google/starred"))
-               feedsource = "user/-/state/com.google/starred";
-       else if(id.endsWith("/state/com.google/broadcast"))
-               feedsource = "user/-/state/com.google/broadcast";
-
-       foreach(QVariant l, result["items"].toList()) {
-               QVariantMap e = l.toMap();
-               Entry entry;
-               QString content, summary;
-
-               entry.id = e["id"].toString();
-               entry.published = QDateTime::fromTime_t(e["published"].toUInt());
-               entry.author = e["author"].toString();
-               entry.source = (e["origin"].toMap())["streamId"].toString();
-               entry.link = (e["alternate"].toMap())["href"].toString();
-
-               content = (e["content"].toMap())["content"].toString();
-               summary = (e["summary"].toMap())["content"].toString();
-               if(content != "") entry.content = content; else entry.content = summary;
-
-               if(e["isReadStateLocked"].toBool())
-                       entry.flags |= ENTRY_FLAG_LOCKED | ENTRY_FLAG_READ;
-
-               QWebPage p;
-               p.mainFrame()->setHtml(e["title"].toString());
-               entry.title = p.mainFrame()->toPlainText();
-
-               foreach(QVariant c, e["categories"].toList()) {
-                       QString cat = c.toString();
-                       if(cat.endsWith("/state/com.google/read"))
-                               entry.flags |= ENTRY_FLAG_READ;
-                       else if(cat.endsWith("/state/com.google/starred"))
-                               entry.flags |= ENTRY_FLAG_STARRED;
-                       else if(cat.endsWith("/state/com.google/broadcast"))
-                               entry.flags |= ENTRY_FLAG_SHARED;
-               }
-
-               if(!feed)
-                       feed = feeds.value(feedsource == "" ? entry.source : feedsource);
-
-               if(feed) {
-                       entry.feed = feed;
-                       feed->addEntry(new Entry(entry));
-               }
-       }
-
-       if(feed) {
-               feed->lastUpdated = QDateTime::currentDateTime();
-               feed->continuation = continuation;
-               feed->signalUpdated();
-       }
-}
-
 void GoogleReader::parseSubscriptions(QByteArray data) {
        QJson::Parser parser;
        bool ok;
@@ -371,11 +361,6 @@ void Feed::delEntry(Entry *entry) {
        entries.remove(entry->id);
 }
 
-void Feed::signalUpdated() {
-       //  TODO: Clean this up
-       emit updateFeedComplete();
-}
-
 void Feed::updateUnread(int i) {
        bool allRead = (unread == 0);
 
index 6c41656..a7d6e57 100644 (file)
@@ -45,7 +45,6 @@ class Feed : public QObject {
                void fetch(bool);
                void addEntry(Entry *);
                void delEntry(Entry *);
-               void signalUpdated(); // TODO: Clean this up...
                QList<Entry *> getEntries();
                int getEntriesSize() { return entries.size(); }
                void markRead();
@@ -58,6 +57,10 @@ class Feed : public QObject {
        private:
                QHash<QString, Entry *> entries;
                QBuffer buffer;
+               QNetworkReply *reply;
+
+       private slots:
+               void fetchFinished();
 };
     
 Q_DECLARE_METATYPE(Feed *)
@@ -154,7 +157,6 @@ class GoogleReader: public QObject {
                void getSID();
                void parseSubscriptions(QByteArray data);
                void parseUnread(QByteArray data);
-               void parseFeed(QByteArray data);
 
                QUrl login_url;
                QUrl subscriptions_url;