Initial Commit.
[onlineservices] / googlecontenthandler.cpp
1 /*
2  *  Copyright (c) 2010 Mani Chandrasekar <maninc@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <QtXml>
20
21 #include "googledocumentlist.h"
22 #include "googledocument.h"
23 #include "googlecontenthandler.h"
24
25 GoogleContentHandler::GoogleContentHandler()
26         : m_docList(0),
27           m_docEntry(0)
28 {
29     m_docList = new GoogleDocumentList();
30 }
31
32 bool GoogleContentHandler::characters ( const QString & ch )
33 {
34     if (!insideEntry) {
35         if (QString::compare(m_nodeStack.top(), "title", Qt::CaseInsensitive) == 0) {
36             if (m_nodeStack.count() == 2)
37                 m_docList->setTitle(ch);
38         }
39         else if (QString::compare(m_nodeStack.top(), "name", Qt::CaseInsensitive) == 0) {
40             if (m_nodeStack.count() == 3)
41                 m_docList->setAuthor(ch);
42         }
43         else if (QString::compare(m_nodeStack.top(), "email", Qt::CaseInsensitive) == 0) {
44             if (m_nodeStack.count() == 3)
45                 m_docList->setEmail(ch);
46         }
47     }
48     else
49     {
50         if (m_docEntry == 0)
51             return true;
52
53         if (QString::compare(m_nodeStack.top(), "title", Qt::CaseInsensitive) == 0) {
54                 m_docEntry->setTitle(ch);
55         }
56         else if (QString::compare(m_nodeStack.top(), "name", Qt::CaseInsensitive) == 0) {
57                 m_docEntry->setAuthor(ch);
58         }
59         else if (QString::compare(m_nodeStack.top(), "resourceId", Qt::CaseInsensitive) == 0) {
60             m_docEntry->setId(ch);
61         }
62     }
63     return true;
64 }
65
66 bool GoogleContentHandler::endDocument ()
67 {
68     //qDebug() << "GoogleContentHandler::endDocument()";
69     return true;
70 }
71
72 bool GoogleContentHandler::endElement ( const QString & namespaceURI, const QString & localName, const QString & qName )
73 {
74     //printName(localName);
75     QString element = m_nodeStack.pop();
76     if (QString::compare(element, "entry") == 0) {
77         insideEntry = false;
78         m_docList->append(m_docEntry);
79         m_docEntry = 0;
80     }
81     return true;
82 }
83
84 bool GoogleContentHandler::endPrefixMapping ( const QString & prefix )
85 {
86     return true;
87 }
88
89 QString GoogleContentHandler::errorString () const
90 {
91     return QString();
92 }
93
94 bool GoogleContentHandler::ignorableWhitespace ( const QString & ch )
95 {
96     return true;
97 }
98
99 bool GoogleContentHandler::processingInstruction ( const QString & target, const QString & data )
100 {
101     return true;
102 }
103
104 void GoogleContentHandler::setDocumentLocator ( QXmlLocator * locator )
105 {
106 }
107
108 bool GoogleContentHandler::skippedEntity ( const QString & name )
109 {
110     return true;
111 }
112
113 bool GoogleContentHandler::startDocument ()
114 {
115     return true;
116 }
117
118 bool GoogleContentHandler::startElement ( const QString & namespaceURI, const QString & localName,
119                                           const QString & qName, const QXmlAttributes & atts )
120 {
121     m_nodeStack.push(localName);
122
123     if ((m_nodeStack.count() == 1) && (m_docList != 0)) { //Feed element
124         m_docList->setEtag(atts.value("gd:etag"));
125     }
126
127     if (QString::compare(localName, "entry", Qt::CaseInsensitive) == 0 ) {
128         m_docEntry = new GoogleDocument();
129         m_docEntry->setEtag(atts.value("gd:etag"));
130         insideEntry = true;
131     }
132     if ( insideEntry && (QString::compare(localName, "content", Qt::CaseInsensitive) == 0 ) && (m_docEntry != 0)) {
133         m_docEntry->setDocumentUrl(atts.value("src"));
134     }
135
136     //printName(localName);
137
138     return true;
139 }
140
141 bool GoogleContentHandler::startPrefixMapping ( const QString & prefix, const QString & uri )
142 {
143     //qDebug() << "GoogleContentHandler::startPrefixMapping() " << prefix << uri;
144     return true;
145 }
146
147 void GoogleContentHandler::printName(const QString & name)
148 {
149     int count = m_nodeStack.count();
150     QString indent;
151     for (int i=0; i < count; i++)
152         indent.append("\t");
153     indent.append(name);
154
155     if (insideEntry);
156 //        qDebug() << indent;
157 }