1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Qt 4.8: rsslisting.cpp Example File (xml/rsslisting/rsslisting.cpp)</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
</ul>
</div>
</div>
<div class="content mainContent">
<h1 class="title">rsslisting.cpp Example File</h1>
<span class="small-subtitle">xml/rsslisting/rsslisting.cpp</span>
<!-- $$$xml/rsslisting/rsslisting.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp"> <span class="comment">/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/</span>
<span class="comment">/*
rsslisting.cpp
Provides a widget for displaying news items from RDF news sources.
RDF is an XML-based format for storing items of information (see
http://www.w3.org/RDF/ for details).
The widget itself provides a simple user interface for specifying
the URL of a news source, and controlling the downloading of news.
The widget downloads and parses the XML asynchronously, feeding the
data to an XML reader in pieces. This allows the user to interrupt
its operation, and also allows very large data sources to be read.
*/</span>
<span class="preprocessor">#include <QtCore></span>
<span class="preprocessor">#include <QtGui></span>
<span class="preprocessor">#include <QtNetwork></span>
<span class="preprocessor">#include "rsslisting.h"</span>
<span class="comment">/*
Constructs an RSSListing widget with a simple user interface, and sets
up the XML reader to use a custom handler class.
The user interface consists of a line edit, a push button, and a
list view widget. The line edit is used for entering the URLs of news
sources; the push button starts the process of reading the
news.
*/</span>
RSSListing<span class="operator">::</span>RSSListing(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
: <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)<span class="operator">,</span> currentReply(<span class="number">0</span>)
{
<span class="preprocessor">#ifdef Q_OS_SYMBIAN</span>
<span class="comment">// Set Internet Access Point</span>
<span class="type"><a href="qnetworkconfigurationmanager.html">QNetworkConfigurationManager</a></span> manager;
<span class="keyword">const</span> <span class="type">bool</span> canStartIAP <span class="operator">=</span> manager<span class="operator">.</span>capabilities() <span class="operator">&</span> <span class="type"><a href="qnetworkconfigurationmanager.html">QNetworkConfigurationManager</a></span><span class="operator">::</span>CanStartAndStopInterfaces;
<span class="comment">// Is there default access point, use it</span>
<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span> cfg <span class="operator">=</span> manager<span class="operator">.</span>defaultConfiguration();
<span class="keyword">if</span> (<span class="operator">!</span>cfg<span class="operator">.</span>isValid() <span class="operator">|</span><span class="operator">|</span> <span class="operator">!</span>canStartIAP) {
<span class="comment">// Available Access Points not found</span>
<span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> <span class="string">"Error"</span><span class="operator">,</span> <span class="string">"No access point"</span>);
<span class="keyword">return</span>;
}
m_session <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span>(cfg);
m_session<span class="operator">-</span><span class="operator">></span>open();
m_session<span class="operator">-</span><span class="operator">></span>waitForOpened();
<span class="preprocessor">#endif</span>
lineEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="keyword">this</span>);
lineEdit<span class="operator">-</span><span class="operator">></span>setText(<span class="string">"http://labs.qt.nokia.com/blogs/feed"</span>);
fetchButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"Fetch"</span>)<span class="operator">,</span> <span class="keyword">this</span>);
treeWidget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtreewidget.html">QTreeWidget</a></span>(<span class="keyword">this</span>);
connect(treeWidget<span class="operator">,</span> SIGNAL(itemActivated(<span class="type"><a href="qtreewidgetitem.html">QTreeWidgetItem</a></span><span class="operator">*</span><span class="operator">,</span><span class="type">int</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(itemActivated(<span class="type"><a href="qtreewidgetitem.html">QTreeWidgetItem</a></span><span class="operator">*</span>)));
<span class="type"><a href="qstringlist.html">QStringList</a></span> headerLabels;
headerLabels <span class="operator"><</span><span class="operator"><</span> tr(<span class="string">"Title"</span>) <span class="operator"><</span><span class="operator"><</span> tr(<span class="string">"Link"</span>);
treeWidget<span class="operator">-</span><span class="operator">></span>setHeaderLabels(headerLabels);
treeWidget<span class="operator">-</span><span class="operator">></span>header()<span class="operator">-</span><span class="operator">></span>setResizeMode(<span class="type"><a href="qheaderview.html">QHeaderView</a></span><span class="operator">::</span>ResizeToContents);
connect(<span class="operator">&</span>manager<span class="operator">,</span> SIGNAL(finished(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span><span class="operator">*</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(finished(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span><span class="operator">*</span>)));
connect(lineEdit<span class="operator">,</span> SIGNAL(returnPressed())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(fetch()));
connect(fetchButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(fetch()));
<span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>);
<span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>hboxLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
hboxLayout<span class="operator">-</span><span class="operator">></span>addWidget(lineEdit);
hboxLayout<span class="operator">-</span><span class="operator">></span>addWidget(fetchButton);
layout<span class="operator">-</span><span class="operator">></span>addLayout(hboxLayout);
layout<span class="operator">-</span><span class="operator">></span>addWidget(treeWidget);
setWindowTitle(tr(<span class="string">"RSS listing example"</span>));
<span class="preprocessor">#if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)</span>
resize(<span class="number">640</span><span class="operator">,</span><span class="number">480</span>);
<span class="preprocessor">#endif</span>
}
<span class="comment">/*
Starts the network request and connects the needed signals
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>get(<span class="keyword">const</span> <span class="type"><a href="qurl.html">QUrl</a></span> <span class="operator">&</span>url)
{
<span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span> request(url);
<span class="keyword">if</span> (currentReply) {
currentReply<span class="operator">-</span><span class="operator">></span>disconnect(<span class="keyword">this</span>);
currentReply<span class="operator">-</span><span class="operator">></span>deleteLater();
}
currentReply <span class="operator">=</span> manager<span class="operator">.</span>get(request);
connect(currentReply<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readyRead()));
connect(currentReply<span class="operator">,</span> SIGNAL(metaDataChanged())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(metaDataChanged()));
connect(currentReply<span class="operator">,</span> SIGNAL(error(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span><span class="operator">::</span>NetworkError))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(error(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span><span class="operator">::</span>NetworkError)));
}
<span class="comment">/*
Starts fetching data from a news source specified in the line
edit widget.
The line edit is made read only to prevent the user from modifying its
contents during the fetch; this is only for cosmetic purposes.
The fetch button is disabled, the list view is cleared, and we
define the last list view item to be 0, meaning that there are no
existing items in the list.
A URL is created with the raw contents of the line edit and
a get is initiated.
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>fetch()
{
lineEdit<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">true</span>);
fetchButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>);
treeWidget<span class="operator">-</span><span class="operator">></span>clear();
xml<span class="operator">.</span>clear();
<span class="type"><a href="qurl.html">QUrl</a></span> url(lineEdit<span class="operator">-</span><span class="operator">></span>text());
get(url);
}
<span class="type">void</span> RSSListing<span class="operator">::</span>metaDataChanged()
{
<span class="type"><a href="qurl.html">QUrl</a></span> redirectionTarget <span class="operator">=</span> currentReply<span class="operator">-</span><span class="operator">></span>attribute(<span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span><span class="operator">::</span>RedirectionTargetAttribute)<span class="operator">.</span>toUrl();
<span class="keyword">if</span> (redirectionTarget<span class="operator">.</span>isValid()) {
get(redirectionTarget);
}
}
<span class="comment">/*
Reads data received from the RDF source.
We read all the available data, and pass it to the XML
stream reader. Then we call the XML parsing function.
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>readyRead()
{
<span class="type">int</span> statusCode <span class="operator">=</span> currentReply<span class="operator">-</span><span class="operator">></span>attribute(<span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span><span class="operator">::</span>HttpStatusCodeAttribute)<span class="operator">.</span>toInt();
<span class="keyword">if</span> (statusCode <span class="operator">></span><span class="operator">=</span> <span class="number">200</span> <span class="operator">&</span><span class="operator">&</span> statusCode <span class="operator"><</span> <span class="number">300</span>) {
<span class="type"><a href="qbytearray.html">QByteArray</a></span> data <span class="operator">=</span> currentReply<span class="operator">-</span><span class="operator">></span>readAll();
xml<span class="operator">.</span>addData(data);
parseXml();
}
}
<span class="comment">/*
Finishes processing an HTTP request.
The default behavior is to keep the text edit read only.
If an error has occurred, the user interface is made available
to the user for further input, allowing a new fetch to be
started.
If the HTTP get request has finished, we make the
user interface available to the user for further input.
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>finished(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span> <span class="operator">*</span>reply)
{
Q_UNUSED(reply);
lineEdit<span class="operator">-</span><span class="operator">></span>setReadOnly(<span class="keyword">false</span>);
fetchButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">true</span>);
}
<span class="comment">/*
Parses the XML data and creates treeWidget items accordingly.
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>parseXml()
{
<span class="keyword">while</span> (<span class="operator">!</span>xml<span class="operator">.</span>atEnd()) {
xml<span class="operator">.</span>readNext();
<span class="keyword">if</span> (xml<span class="operator">.</span>isStartElement()) {
<span class="keyword">if</span> (xml<span class="operator">.</span>name() <span class="operator">=</span><span class="operator">=</span> <span class="string">"item"</span>)
linkString <span class="operator">=</span> xml<span class="operator">.</span>attributes()<span class="operator">.</span>value(<span class="string">"rss:about"</span>)<span class="operator">.</span>toString();
currentTag <span class="operator">=</span> xml<span class="operator">.</span>name()<span class="operator">.</span>toString();
} <span class="keyword">else</span> <span class="keyword">if</span> (xml<span class="operator">.</span>isEndElement()) {
<span class="keyword">if</span> (xml<span class="operator">.</span>name() <span class="operator">=</span><span class="operator">=</span> <span class="string">"item"</span>) {
<span class="type"><a href="qtreewidgetitem.html">QTreeWidgetItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtreewidgetitem.html">QTreeWidgetItem</a></span>;
item<span class="operator">-</span><span class="operator">></span>setText(<span class="number">0</span><span class="operator">,</span> titleString);
item<span class="operator">-</span><span class="operator">></span>setText(<span class="number">1</span><span class="operator">,</span> linkString);
treeWidget<span class="operator">-</span><span class="operator">></span>addTopLevelItem(item);
titleString<span class="operator">.</span>clear();
linkString<span class="operator">.</span>clear();
}
} <span class="keyword">else</span> <span class="keyword">if</span> (xml<span class="operator">.</span>isCharacters() <span class="operator">&</span><span class="operator">&</span> <span class="operator">!</span>xml<span class="operator">.</span>isWhitespace()) {
<span class="keyword">if</span> (currentTag <span class="operator">=</span><span class="operator">=</span> <span class="string">"title"</span>)
titleString <span class="operator">+</span><span class="operator">=</span> xml<span class="operator">.</span>text()<span class="operator">.</span>toString();
<span class="keyword">else</span> <span class="keyword">if</span> (currentTag <span class="operator">=</span><span class="operator">=</span> <span class="string">"link"</span>)
linkString <span class="operator">+</span><span class="operator">=</span> xml<span class="operator">.</span>text()<span class="operator">.</span>toString();
}
}
<span class="keyword">if</span> (xml<span class="operator">.</span>error() <span class="operator">&</span><span class="operator">&</span> xml<span class="operator">.</span>error() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qxmlstreamreader.html">QXmlStreamReader</a></span><span class="operator">::</span>PrematureEndOfDocumentError) {
<a href="qtglobal.html#qWarning">qWarning</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"XML ERROR:"</span> <span class="operator"><</span><span class="operator"><</span> xml<span class="operator">.</span>lineNumber() <span class="operator"><</span><span class="operator"><</span> <span class="string">": "</span> <span class="operator"><</span><span class="operator"><</span> xml<span class="operator">.</span>errorString();
}
}
<span class="comment">/*
Open the link in the browser
*/</span>
<span class="type">void</span> RSSListing<span class="operator">::</span>itemActivated(<span class="type"><a href="qtreewidgetitem.html">QTreeWidgetItem</a></span> <span class="operator">*</span> item)
{
<span class="type"><a href="qdesktopservices.html">QDesktopServices</a></span><span class="operator">::</span>openUrl(<span class="type"><a href="qurl.html">QUrl</a></span>(item<span class="operator">-</span><span class="operator">></span>text(<span class="number">1</span>)));
}
<span class="type">void</span> RSSListing<span class="operator">::</span>error(<span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span><span class="operator">::</span>NetworkError)
{
<a href="qtglobal.html#qWarning">qWarning</a>(<span class="string">"error retrieving RSS feed"</span>);
currentReply<span class="operator">-</span><span class="operator">></span>disconnect(<span class="keyword">this</span>);
currentReply<span class="operator">-</span><span class="operator">></span>deleteLater();
currentReply <span class="operator">=</span> <span class="number">0</span>;
}</pre>
</div>
<!-- @@@xml/rsslisting/rsslisting.cpp -->
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|