Added libxi-dev dependency
[froff-onlinedoc] / 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         } else if(QString::compare(m_nodeStack.top(), "name", Qt::CaseInsensitive) == 0) {
39             if(m_nodeStack.count() == 3)
40                 m_docList->setAuthor(ch);
41         } else if(QString::compare(m_nodeStack.top(), "email", Qt::CaseInsensitive) == 0) {
42             if(m_nodeStack.count() == 3)
43                 m_docList->setEmail(ch);
44         }
45     } else {
46         if(m_docEntry == 0)
47             return true;
48
49         if(QString::compare(m_nodeStack.top(), "title", Qt::CaseInsensitive) == 0) {
50             m_docEntry->setTitle(ch);
51         } else if(QString::compare(m_nodeStack.top(), "name", Qt::CaseInsensitive) == 0) {
52             m_docEntry->setAuthor(ch);
53         } else if(QString::compare(m_nodeStack.top(), "resourceId", Qt::CaseInsensitive) == 0) {
54             m_docEntry->setId(ch);
55         }
56     }
57     return true;
58 }
59
60 bool GoogleContentHandler::endDocument()
61 {
62     //qDebug() << "GoogleContentHandler::endDocument()";
63     return true;
64 }
65
66 bool GoogleContentHandler::endElement(const QString & namespaceURI, const QString & localName, const QString & qName)
67 {
68     //printName(localName);
69     QString element = m_nodeStack.pop();
70     if(QString::compare(element, "entry") == 0) {
71         insideEntry = false;
72         m_docList->append(m_docEntry);
73         m_docEntry = 0;
74     }
75     return true;
76 }
77
78 bool GoogleContentHandler::endPrefixMapping(const QString & prefix)
79 {
80     return true;
81 }
82
83 QString GoogleContentHandler::errorString() const
84 {
85     return QString();
86 }
87
88 bool GoogleContentHandler::ignorableWhitespace(const QString & ch)
89 {
90     return true;
91 }
92
93 bool GoogleContentHandler::processingInstruction(const QString & target, const QString & data)
94 {
95     return true;
96 }
97
98 void GoogleContentHandler::setDocumentLocator(QXmlLocator * locator)
99 {
100 }
101
102 bool GoogleContentHandler::skippedEntity(const QString & name)
103 {
104     return true;
105 }
106
107 bool GoogleContentHandler::startDocument()
108 {
109     return true;
110 }
111
112 bool GoogleContentHandler::startElement(const QString & namespaceURI, const QString & localName,
113                                         const QString & qName, const QXmlAttributes & atts)
114 {
115     m_nodeStack.push(localName);
116
117     if((m_nodeStack.count() == 1) && (m_docList != 0)) {  //Feed element
118         m_docList->setEtag(atts.value("gd:etag"));
119     }
120
121     if(QString::compare(localName, "entry", Qt::CaseInsensitive) == 0) {
122         m_docEntry = new GoogleDocument();
123         m_docEntry->setEtag(atts.value("gd:etag"));
124         insideEntry = true;
125     }
126     if(insideEntry && (QString::compare(localName, "content", Qt::CaseInsensitive) == 0) && (m_docEntry != 0)) {
127         m_docEntry->setDocumentUrl(atts.value("src"));
128     }
129
130     //printName(localName);
131
132     return true;
133 }
134
135 bool GoogleContentHandler::startPrefixMapping(const QString & prefix, const QString & uri)
136 {
137     //qDebug() << "GoogleContentHandler::startPrefixMapping() " << prefix << uri;
138     return true;
139 }
140
141 void GoogleContentHandler::printName(const QString & name)
142 {
143     int count = m_nodeStack.count();
144     QString indent;
145     for(int i = 0; i < count; i++)
146         indent.append("\t");
147     indent.append(name);
148
149     if(insideEntry);
150 //        qDebug() << indent;
151 }