Switched from XML to JSON.
authorJan Dumon <j.dumon@option.com>
Thu, 11 Mar 2010 19:52:49 +0000 (20:52 +0100)
committerJan Dumon <j.dumon@option.com>
Thu, 11 Mar 2010 19:52:49 +0000 (20:52 +0100)
debian/control
grr.pro
src/googlereader.cpp
src/googlereader.h

index 28f26d2..9709d34 100644 (file)
@@ -2,7 +2,7 @@ Source: grr
 Section: user/network
 Priority: optional
 Maintainer: Jan Dumon <jan@crossbar.net>
-Build-Depends: debhelper (>= 4.0.0), libqt4-maemo5-dev (>= 4.6.2~git20100212), libgconf2-dev (>= 2.13.5), libx11-dev
+Build-Depends: debhelper (>= 4.0.0), libqt4-maemo5-dev (>= 4.6.2~git20100212), libgconf2-dev (>= 2.13.5), libx11-dev, libqjson-dev
 Standards-Version: 3.6.0
 
 Package: grr
diff --git a/grr.pro b/grr.pro
index 944aa51..574c142 100644 (file)
--- a/grr.pro
+++ b/grr.pro
@@ -7,7 +7,7 @@ LEXSOURCES  += #LEXS#
 YACCSOURCES += #YACCS#
 
 INCLUDEPATH +=
-LIBS        +=
+LIBS        += -lqjson
 DEFINES     +=
 
 # All generated files goes same directory
index 9cf88ba..3d7ab15 100644 (file)
@@ -6,9 +6,10 @@
 #include <QBuffer>
 #include <QTimer>
 #include <QDateTime>
-#include <QDomDocument>
 #include <QDebug>
 
+#include <qjson/parser.h>
+
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -26,8 +27,8 @@ void Feed::updateSubscription(Feed *feed) {
 
 void Feed::fetch(bool cont) {
        QNetworkRequest request;
-       QByteArray ba = "http://www.google.com/reader/atom/";
-
+       //QByteArray ba = "http://www.google.com/reader/atom/";
+       QByteArray ba = "http://www.google.com/reader/api/0/stream/contents/";
        ba.append(QUrl::toPercentEncoding(id));
        QUrl url = QUrl::fromEncoded(ba);
 
@@ -48,8 +49,8 @@ GoogleReader::GoogleReader() {
        SIDPending = false;
 
        login_url.setUrl("https://www.google.com/accounts/ClientLogin");
-       subscriptions_url.setUrl("http://www.google.com/reader/api/0/subscription/list");
-       unread_url.setUrl("http://www.google.com/reader/api/0/unread-count");
+       subscriptions_url.setUrl("http://www.google.com/reader/api/0/subscription/list?output=json");
+       unread_url.setUrl("http://www.google.com/reader/api/0/unread-count?output=json");
        edittag_url.setUrl("http://www.google.com/reader/api/0/edit-tag?client=-");
        token_url.setUrl("http://www.google.com/reader/api/0/token");
        markallread_url.setUrl("http://www.google.com/reader/api/0/mark-all-as-read?client=-");
@@ -114,11 +115,7 @@ void GoogleReader::downloadFinished(QNetworkReply *reply) {
                qDebug() << "token:" << token;
        }
        else if(url == subscriptions_url) {
-               QByteArray data = reply->readAll();
-               QDomDocument dom;
-               dom.setContent(data);
-               parseSubscriptions(dom);
-               emit updateSubscriptionsComplete();
+               parseSubscriptions(reply->readAll());
 
                /* TODO: Replace this with a proper state machine */
                if(updateUnreadPending) {
@@ -127,11 +124,7 @@ void GoogleReader::downloadFinished(QNetworkReply *reply) {
                }
        }
        else if(url == unread_url) {
-               QByteArray data = reply->readAll();
-               QDomDocument dom;
-               dom.setContent(data);
-               parseUnread(dom);
-               emit updateUnreadComplete();
+               parseUnread(reply->readAll());
        }
        else if(url == edittag_url) {
                QByteArray data = reply->readAll();
@@ -142,94 +135,66 @@ void GoogleReader::downloadFinished(QNetworkReply *reply) {
                //qDebug() << "Result:" << data;
        }
        else {
-               QByteArray data = reply->readAll();
-               QDomDocument dom;
-               dom.setContent(data);
-               parseFeed(dom);
+               parseFeed(reply->readAll());
        }
 
        reply->deleteLater();
 }
 
-void GoogleReader::parseFeed(QDomDocument dom) {
-       QDomElement set, e;
-       QDomNode n, c;
-       QString continuation, feedsource;
+void GoogleReader::parseFeed(QByteArray data) {
+       QJson::Parser parser;
+       bool ok;
+       QVariantMap result = parser.parse(data, &ok).toMap();
+       QString continuation, feedsource, id;
        Feed *feed = NULL;
 
-       set = dom.firstChildElement();
-
-       for(n = set.firstChild(); !n.isNull(); n = n.nextSibling()) {
-               e = n.toElement();
-               QString name = e.tagName();
-               if(name == "entry") {
-                       Entry entry;
-                       QString content, summary;
-
-                       QString locked = e.attribute("gr:is-read-state-locked", "false");
-                       if(locked == "true")
-                               entry.flags |= ENTRY_FLAG_LOCKED | ENTRY_FLAG_READ;
-
-                       entry.crawled = e.attribute("gr:crawl-timestamp-msec", "0").toULongLong();
-
-                       for(c = n.firstChild(); !c.isNull(); c = c.nextSibling()) {
-                               e = c.toElement();
-                               name = e.tagName();
-                               if(name == "id")
-                                       entry.id = e.text();
-                               else if(name == "title") {
-                                       QWebPage p;
-                                       p.mainFrame()->setHtml(e.text());
-                                       entry.title = p.mainFrame()->toPlainText();
-                               }
-                               else if(name == "published")
-                                       entry.published = QDateTime::fromString(e.text(), "yyyy-MM-dd'T'HH:mm:ss'Z'");
-                               else if(name == "link")
-                                       entry.link = QUrl(e.attribute("href", ""));
-                               else if(name == "source")
-                                       entry.source = e.attribute("gr:stream-id", "");
-                               else if(name == "content")
-                                       content = e.text();
-                               else if(name == "summary")
-                                       summary = e.text();
-                               else if(name == "author") {
-                                       e = c.firstChild().toElement();
-                                       entry.author = e.text();
-                                       if(entry.author == "(author unknown)")
-                                               entry.author = "";
-                               }
-                               else if(name == "category") {
-                                       QString label = e.attribute("label", "");
-                                       if(label == "read")
-                                               entry.flags |= ENTRY_FLAG_READ;
-                                       else if(label == "starred")
-                                               entry.flags |= ENTRY_FLAG_STARRED;
-                                       else if(label == "broadcast")
-                                               entry.flags |= ENTRY_FLAG_SHARED;
-                               }
-                       }
-
-                       if(content != "")
-                               entry.content = content;
-                       else if(summary != "")
-                               entry.content = summary;
-
-                       if(!feed)
-                               feed = feeds.value(feedsource == "" ? entry.source : feedsource);
-
-                       if(feed) {
-                               entry.feed = feed;
-                               feed->addEntry(new Entry(entry));
-                       }
-               }
-               else if(name == "gr:continuation") {
-                       continuation = e.text();
+       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;
                }
-               else if(name == "id") {
-                       if(e.text().endsWith("/state/com.google/starred"))
-                               feedsource = "user/-/state/com.google/starred";
-                       else if(e.text().endsWith("/state/com.google/broadcast"))
-                               feedsource = "user/-/state/com.google/broadcast";
+
+               if(!feed)
+                       feed = feeds.value(feedsource == "" ? entry.source : feedsource);
+
+               if(feed) {
+                       entry.feed = feed;
+                       feed->addEntry(new Entry(entry));
                }
        }
 
@@ -240,44 +205,30 @@ void GoogleReader::parseFeed(QDomDocument dom) {
        }
 }
 
-void GoogleReader::parseSubscriptions(QDomDocument dom) {
-       QDomElement set, e;
-       QDomNode n, c;
+void GoogleReader::parseSubscriptions(QByteArray data) {
+       QJson::Parser parser;
+       bool ok;
+       QVariantMap result = parser.parse(data, &ok).toMap();
 
        /* Clear the subscription updated flag */
        QHash<QString, Feed *>::iterator i;
        for(i = feeds.begin(); i != feeds.end(); ++i)
                i.value()->subscription_updated = false;
 
-       set = dom.firstChildElement();
-       set = set.firstChildElement("list");
-       set = set.firstChildElement("object");
-
-       for (; !set.isNull(); set = set.nextSiblingElement("object")) {
+       foreach(QVariant l, result["subscriptions"].toList()) {
+               QVariantMap subscription = l.toMap();
                Feed *feed = new Feed(this);
                Feed *existing_feed;
 
-               for(n = set.firstChild(); !n.isNull(); n = n.nextSibling()) {
-                       e = n.toElement();
-                       QString name = e.attribute("name");
-                       if(name == "id")
-                               feed->id = e.text();
-                       else if(name == "title")
-                               feed->title = e.text();
-                       else if(name == "sortid")
-                               feed->sortid = e.text();
-                       else if(name == "firstitemmsec")
-                               feed->firstitemmsec = e.text();
-                       else if(name == "categories") {
-                               for(c = n.firstChild().firstChild(); !c.isNull(); c = c.nextSibling()) {
-                                       e = c.toElement();
-                                       QString name = e.attribute("name");
-                                       if(name == "id")
-                                               feed->cat_id = e.text();
-                                       else if(name == "label")
-                                               feed->cat_label = e.text();
-                               }
-                       }
+               feed->id = subscription["id"].toString();
+               feed->title = subscription["title"].toString();
+               feed->sortid = subscription["sortid"].toString();
+               feed->firstitemmsec = subscription["firstitemmsec"].toString();
+
+               foreach(QVariant c, subscription["categories"].toList()) {
+                       QVariantMap cat = c.toMap();
+                       feed->cat_id = cat["id"].toString();
+                       feed->cat_label = cat["label"].toString();
                }
 
                existing_feed = feeds.value(feed->id);
@@ -303,44 +254,29 @@ void GoogleReader::parseSubscriptions(QDomDocument dom) {
        }
 
        lastUpdated = QDateTime::currentDateTime();
+       emit updateSubscriptionsComplete();
 }
 
-void GoogleReader::parseUnread(QDomDocument dom) {
-       QDomElement set, e;
-       QDomNode n, c;
-
-       set = dom.firstChildElement();
-       set = set.firstChildElement("list");
-       set = set.firstChildElement("object");
-
-       for (; !set.isNull(); set = set.nextSiblingElement("object")) {
-               QString id;
-               int count = 0;
-               ulong newestitem = 0;
-
-               for(n = set.firstChild(); !n.isNull(); n = n.nextSibling()) {
-                       e = n.toElement();
-                       QString name = e.attribute("name");
-                       if(name == "id")
-                               id = e.text();
-                       else if(name == "count")
-                               count =  e.text().toInt();
-                       else if(name == "newestItemTimestampUsec")
-                               newestitem = e.text().toULong();
-               }
+void GoogleReader::parseUnread(QByteArray data) {
+       QJson::Parser parser;
+       bool ok;
+       QVariantMap result = parser.parse(data, &ok).toMap();
+
+       foreach(QVariant l, result["unreadcounts"].toList()) {
+               QVariantMap unread = l.toMap();
+               QString id = unread["id"].toString();
+               int count = unread["count"].toInt();
+               ulong newestitem = unread["newestitem"].toUInt();
 
                Feed *f = feeds.value(id);
                if(f) {
                        f->unread = count;
                        f->newestitem = newestitem;
-                       //qDebug() << f->title << "->" << count;
-               }
-               else {
-                       //printf("%s not found\n", id.toLatin1().data());
                }
        }
 
        lastUpdated = QDateTime::currentDateTime();
+       emit updateUnreadComplete();
 }
 
 void GoogleReader::getSID() {
index cd47dd2..6c41656 100644 (file)
@@ -4,7 +4,6 @@
 #include <QObject>
 #include <QNetworkReply>
 #include <QNetworkRequest>
-#include <QDomDocument>
 #include <QBuffer>
 #include <QDateTime>
 
@@ -153,9 +152,9 @@ class GoogleReader: public QObject {
                bool SIDPending;
 
                void getSID();
-               void parseSubscriptions(QDomDocument dom);
-               void parseUnread(QDomDocument dom);
-               void parseFeed(QDomDocument dom);
+               void parseSubscriptions(QByteArray data);
+               void parseUnread(QByteArray data);
+               void parseFeed(QByteArray data);
 
                QUrl login_url;
                QUrl subscriptions_url;