Changeset 846 for trunk/examples/xml/rsslisting/rsslisting.cpp
- Timestamp:
- May 5, 2011, 5:36:53 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.7.2 (added) merged: 845 /branches/vendor/nokia/qt/current merged: 844 /branches/vendor/nokia/qt/4.6.3 removed
- Property svn:mergeinfo changed
-
trunk/examples/xml/rsslisting/rsslisting.cpp
r651 r846 1 1 /**************************************************************************** 2 2 ** 3 ** Copyright (C) 201 0Nokia Corporation and/or its subsidiary(-ies).3 ** Copyright (C) 201 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** All rights reserved. 5 5 ** Contact: Nokia Corporation ([email protected]) … … 7 7 ** This file is part of the examples of the Qt Toolkit. 8 8 ** 9 ** $QT_BEGIN_LICENSE:LGPL$ 10 ** Commercial Usage 11 ** Licensees holding valid Qt Commercial licenses may use this file in 12 ** accordance with the Qt Commercial License Agreement provided with the 13 ** Software or, alternatively, in accordance with the terms contained in 14 ** a written agreement between you and Nokia. 15 ** 16 ** GNU Lesser General Public License Usage 17 ** Alternatively, this file may be used under the terms of the GNU Lesser 18 ** General Public License version 2.1 as published by the Free Software 19 ** Foundation and appearing in the file LICENSE.LGPL included in the 20 ** packaging of this file. Please review the following information to 21 ** ensure the GNU Lesser General Public License version 2.1 requirements 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 23 ** 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 ** 28 ** GNU General Public License Usage 29 ** Alternatively, this file may be used under the terms of the GNU 30 ** General Public License version 3.0 as published by the Free Software 31 ** Foundation and appearing in the file LICENSE.GPL included in the 32 ** packaging of this file. Please review the following information to 33 ** ensure the GNU General Public License version 3.0 requirements will be 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 ** 36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at [email protected]. 9 ** $QT_BEGIN_LICENSE:BSD$ 10 ** You may use this file under the terms of the BSD license as follows: 11 ** 12 ** "Redistribution and use in source and binary forms, with or without 13 ** modification, are permitted provided that the following conditions are 14 ** met: 15 ** * Redistributions of source code must retain the above copyright 16 ** notice, this list of conditions and the following disclaimer. 17 ** * Redistributions in binary form must reproduce the above copyright 18 ** notice, this list of conditions and the following disclaimer in 19 ** the documentation and/or other materials provided with the 20 ** distribution. 21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 22 ** the names of its contributors may be used to endorse or promote 23 ** products derived from this software without specific prior written 24 ** permission. 25 ** 26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 38 37 ** $QT_END_LICENSE$ 39 38 ** … … 67 66 up the XML reader to use a custom handler class. 68 67 69 The user interface consists of a line edit, two push buttons, and a68 The user interface consists of a line edit, , and a 70 69 list view widget. The line edit is used for entering the URLs of news 71 sources; the push button s start and abortthe process of reading the70 sources; the push button the process of reading the 72 71 news. 73 72 */ 74 73 75 74 RSSListing::RSSListing(QWidget *parent) 76 : QWidget(parent) 75 : QWidget(parent) 77 76 { 78 77 lineEdit = new QLineEdit(this); … … 80 79 81 80 fetchButton = new QPushButton(tr("Fetch"), this); 82 abortButton = new QPushButton(tr("Abort"), this);83 abortButton->setEnabled(false);84 81 85 82 treeWidget = new QTreeWidget(this); … … 91 88 treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents); 92 89 93 connect(&http, SIGNAL(readyRead(QHttpResponseHeader)), 94 this, SLOT(readData(QHttpResponseHeader))); 95 96 connect(&http, SIGNAL(requestFinished(int,bool)), 97 this, SLOT(finished(int,bool))); 90 connect(&manager, SIGNAL(finished(QNetworkReply*)), 91 this, SLOT(finished(QNetworkReply*))); 98 92 99 93 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch())); 100 94 connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch())); 101 connect(abortButton, SIGNAL(clicked()), &http, SLOT(abort()));102 95 103 96 QVBoxLayout *layout = new QVBoxLayout(this); … … 107 100 hboxLayout->addWidget(lineEdit); 108 101 hboxLayout->addWidget(fetchButton); 109 hboxLayout->addWidget(abortButton);110 102 111 103 layout->addLayout(hboxLayout); … … 117 109 118 110 /* 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 119 127 Starts fetching data from a news source specified in the line 120 128 edit widget. … … 122 130 The line edit is made read only to prevent the user from modifying its 123 131 contents during the fetch; this is only for cosmetic purposes. 124 The fetch button is disabled, and the abort button is enabled to allow 125 the user to interrupt processing. The list view is cleared, and we 132 The fetch button is disabled, the list view is cleared, and we 126 133 define the last list view item to be 0, meaning that there are no 127 134 existing items in the list. 128 135 129 The HTTP handler is supplied with the raw contents of the line edit and 130 a fetch is initiated. We keep the ID value returned by the HTTP handler 131 for future reference. 136 A URL is created with the raw contents of the line edit and 137 a get is initiated. 132 138 */ 133 139 … … 136 142 lineEdit->setReadOnly(true); 137 143 fetchButton->setEnabled(false); 138 abortButton->setEnabled(true);139 144 treeWidget->clear(); 140 145 … … 142 147 143 148 QUrl url(lineEdit->text()); 144 145 http.setHost(url.host()); 146 connectionId = http.get(url.path()); 149 get(url); 150 } 151 152 void RSSListing::metaDataChanged() 153 { 154 QUrl redirectionTarget = currentReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); 155 if (redirectionTarget.isValid()) { 156 get(redirectionTarget); 157 } 147 158 } 148 159 … … 152 163 We read all the available data, and pass it to the XML 153 164 stream reader. Then we call the XML parsing function. 154 155 If parsing fails for any reason, we abort the fetch. 156 */ 157 158 void RSSListing::readData(const QHttpResponseHeader &resp) 159 { 160 if (resp.statusCode() != 200) 161 http.abort(); 162 else { 163 xml.addData(http.readAll()); 165 */ 166 167 void RSSListing::readyRead() 168 { 169 int statusCode = currentReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 170 if (statusCode >= 200 && statusCode < 300) { 171 QByteArray data = currentReply->readAll(); 172 xml.addData(data); 164 173 parseXml(); 165 174 } … … 179 188 */ 180 189 181 void RSSListing::finished(int id, bool error) 182 { 183 if (error) { 184 qWarning("Received error during HTTP fetch."); 185 lineEdit->setReadOnly(false); 186 abortButton->setEnabled(false); 187 fetchButton->setEnabled(true); 188 } 189 else if (id == connectionId) { 190 lineEdit->setReadOnly(false); 191 abortButton->setEnabled(false); 192 fetchButton->setEnabled(true); 193 } 190 void RSSListing::finished(QNetworkReply *reply) 191 { 192 Q_UNUSED(reply); 193 lineEdit->setReadOnly(false); 194 fetchButton->setEnabled(true); 194 195 } 195 196 … … 227 228 if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) { 228 229 qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString(); 229 http.abort();230 230 } 231 231 } … … 238 238 QDesktopServices::openUrl(QUrl(item->text(1))); 239 239 } 240 241 242 243 244 245 246 247
Note:
See TracChangeset
for help on using the changeset viewer.