author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
/* |
|
42 |
rsslisting.cpp |
|
43 |
||
44 |
Provides a widget for displaying news items from RDF news sources. |
|
45 |
RDF is an XML-based format for storing items of information (see |
|
46 |
http://www.w3.org/RDF/ for details). |
|
47 |
||
48 |
The widget itself provides a simple user interface for specifying |
|
49 |
the URL of a news source, and controlling the downloading of news. |
|
50 |
||
51 |
The widget downloads and parses the XML asynchronously, feeding the |
|
52 |
data to an XML reader in pieces. This allows the user to interrupt |
|
53 |
its operation, and also allows very large data sources to be read. |
|
54 |
*/ |
|
55 |
||
56 |
||
57 |
#include <QtCore> |
|
58 |
#include <QtGui> |
|
59 |
#include <QtNetwork> |
|
60 |
||
61 |
#include "rsslisting.h" |
|
62 |
||
63 |
||
64 |
/* |
|
65 |
Constructs an RSSListing widget with a simple user interface, and sets |
|
66 |
up the XML reader to use a custom handler class. |
|
67 |
||
68 |
The user interface consists of a line edit, two push buttons, and a |
|
69 |
list view widget. The line edit is used for entering the URLs of news |
|
70 |
sources; the push buttons start and abort the process of reading the |
|
71 |
news. |
|
72 |
*/ |
|
73 |
||
74 |
RSSListing::RSSListing(QWidget *parent) |
|
75 |
: QWidget(parent) |
|
76 |
{ |
|
77 |
lineEdit = new QLineEdit(this); |
|
78 |
lineEdit->setText("http://labs.qt.nokia.com/blogs/feed"); |
|
79 |
||
80 |
fetchButton = new QPushButton(tr("Fetch"), this); |
|
81 |
abortButton = new QPushButton(tr("Abort"), this); |
|
82 |
abortButton->setEnabled(false); |
|
83 |
||
84 |
treeWidget = new QTreeWidget(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
0 | 86 |
this, SLOT(itemActivated(QTreeWidgetItem*))); |
87 |
QStringList headerLabels; |
|
88 |
headerLabels << tr("Title") << tr("Link"); |
|
89 |
treeWidget->setHeaderLabels(headerLabels); |
|
90 |
treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents); |
|
91 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
connect(&http, SIGNAL(readyRead(QHttpResponseHeader)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
93 |
this, SLOT(readData(QHttpResponseHeader))); |
0 | 94 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
95 |
connect(&http, SIGNAL(requestFinished(int,bool)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
this, SLOT(finished(int,bool))); |
0 | 97 |
|
98 |
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch())); |
|
99 |
connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch())); |
|
100 |
connect(abortButton, SIGNAL(clicked()), &http, SLOT(abort())); |
|
101 |
||
102 |
QVBoxLayout *layout = new QVBoxLayout(this); |
|
103 |
||
104 |
QHBoxLayout *hboxLayout = new QHBoxLayout; |
|
105 |
||
106 |
hboxLayout->addWidget(lineEdit); |
|
107 |
hboxLayout->addWidget(fetchButton); |
|
108 |
hboxLayout->addWidget(abortButton); |
|
109 |
||
110 |
layout->addLayout(hboxLayout); |
|
111 |
layout->addWidget(treeWidget); |
|
112 |
||
113 |
setWindowTitle(tr("RSS listing example")); |
|
114 |
resize(640,480); |
|
115 |
} |
|
116 |
||
117 |
/* |
|
118 |
Starts fetching data from a news source specified in the line |
|
119 |
edit widget. |
|
120 |
||
121 |
The line edit is made read only to prevent the user from modifying its |
|
122 |
contents during the fetch; this is only for cosmetic purposes. |
|
123 |
The fetch button is disabled, and the abort button is enabled to allow |
|
124 |
the user to interrupt processing. The list view is cleared, and we |
|
125 |
define the last list view item to be 0, meaning that there are no |
|
126 |
existing items in the list. |
|
127 |
||
128 |
The HTTP handler is supplied with the raw contents of the line edit and |
|
129 |
a fetch is initiated. We keep the ID value returned by the HTTP handler |
|
130 |
for future reference. |
|
131 |
*/ |
|
132 |
||
133 |
void RSSListing::fetch() |
|
134 |
{ |
|
135 |
lineEdit->setReadOnly(true); |
|
136 |
fetchButton->setEnabled(false); |
|
137 |
abortButton->setEnabled(true); |
|
138 |
treeWidget->clear(); |
|
139 |
||
140 |
xml.clear(); |
|
141 |
||
142 |
QUrl url(lineEdit->text()); |
|
143 |
||
144 |
http.setHost(url.host()); |
|
145 |
connectionId = http.get(url.path()); |
|
146 |
} |
|
147 |
||
148 |
/* |
|
149 |
Reads data received from the RDF source. |
|
150 |
||
151 |
We read all the available data, and pass it to the XML |
|
152 |
stream reader. Then we call the XML parsing function. |
|
153 |
||
154 |
If parsing fails for any reason, we abort the fetch. |
|
155 |
*/ |
|
156 |
||
157 |
void RSSListing::readData(const QHttpResponseHeader &resp) |
|
158 |
{ |
|
159 |
if (resp.statusCode() != 200) |
|
160 |
http.abort(); |
|
161 |
else { |
|
162 |
xml.addData(http.readAll()); |
|
163 |
parseXml(); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
/* |
|
168 |
Finishes processing an HTTP request. |
|
169 |
||
170 |
The default behavior is to keep the text edit read only. |
|
171 |
||
172 |
If an error has occurred, the user interface is made available |
|
173 |
to the user for further input, allowing a new fetch to be |
|
174 |
started. |
|
175 |
||
176 |
If the HTTP get request has finished, we make the |
|
177 |
user interface available to the user for further input. |
|
178 |
*/ |
|
179 |
||
180 |
void RSSListing::finished(int id, bool error) |
|
181 |
{ |
|
182 |
if (error) { |
|
183 |
qWarning("Received error during HTTP fetch."); |
|
184 |
lineEdit->setReadOnly(false); |
|
185 |
abortButton->setEnabled(false); |
|
186 |
fetchButton->setEnabled(true); |
|
187 |
} |
|
188 |
else if (id == connectionId) { |
|
189 |
lineEdit->setReadOnly(false); |
|
190 |
abortButton->setEnabled(false); |
|
191 |
fetchButton->setEnabled(true); |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
||
196 |
/* |
|
197 |
Parses the XML data and creates treeWidget items accordingly. |
|
198 |
*/ |
|
199 |
void RSSListing::parseXml() |
|
200 |
{ |
|
201 |
while (!xml.atEnd()) { |
|
202 |
xml.readNext(); |
|
203 |
if (xml.isStartElement()) { |
|
204 |
if (xml.name() == "item") |
|
205 |
linkString = xml.attributes().value("rss:about").toString(); |
|
206 |
currentTag = xml.name().toString(); |
|
207 |
} else if (xml.isEndElement()) { |
|
208 |
if (xml.name() == "item") { |
|
209 |
||
210 |
QTreeWidgetItem *item = new QTreeWidgetItem; |
|
211 |
item->setText(0, titleString); |
|
212 |
item->setText(1, linkString); |
|
213 |
treeWidget->addTopLevelItem(item); |
|
214 |
||
215 |
titleString.clear(); |
|
216 |
linkString.clear(); |
|
217 |
} |
|
218 |
||
219 |
} else if (xml.isCharacters() && !xml.isWhitespace()) { |
|
220 |
if (currentTag == "title") |
|
221 |
titleString += xml.text().toString(); |
|
222 |
else if (currentTag == "link") |
|
223 |
linkString += xml.text().toString(); |
|
224 |
} |
|
225 |
} |
|
226 |
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) { |
|
227 |
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString(); |
|
228 |
http.abort(); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
/* |
|
233 |
Open the link in the browser |
|
234 |
*/ |
|
235 |
void RSSListing::itemActivated(QTreeWidgetItem * item) |
|
236 |
{ |
|
237 |
QDesktopServices::openUrl(QUrl(item->text(1))); |
|
238 |
} |