1 /****************************************************************************
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
27 function RSSListing(parent)
29 QWidget.call(this, parent);
31 this.xml = new QXmlStreamReader();
34 this.titleString = "";
35 this.connectionId = -1;
37 this.lineEdit = new QLineEdit(this);
38 this.lineEdit.text = "http://labs.trolltech.com/blogs/feed";
40 this.fetchButton = new QPushButton(tr("Fetch"), this);
41 this.abortButton = new QPushButton(tr("Abort"), this);
42 this.abortButton.enabled = false;
44 this.treeWidget = new QTreeWidget(this);
45 this.treeWidget["itemActivated(QTreeWidgetItem*, int)"].connect(
46 this, this.itemActivated);
47 var headerLabels = new Array();
48 headerLabels.push(tr("Title"));
49 headerLabels.push(tr("Link"));
50 this.treeWidget.setHeaderLabels(headerLabels);
51 this.treeWidget.header().setResizeMode(QHeaderView.ResizeToContents);
53 this.http = new QHttp(this);
54 this.http.readyRead.connect(this, this.readData);
55 this.http.requestFinished.connect(this, this.finished);
57 this.lineEdit.returnPressed.connect(this, this.fetch);
58 this.fetchButton.clicked.connect(this, this.fetch);
59 this.abortButton.clicked.connect(this.http, this.http.abort);
61 var layout = new QVBoxLayout(this);
63 var hboxLayout = new QHBoxLayout();
65 // ### working around problem with addWidget() binding
66 hboxLayout.addWidget(this.lineEdit, 0, Qt.AlignLeft);
67 hboxLayout.addWidget(this.fetchButton, 0, Qt.AlignLeft);
68 hboxLayout.addWidget(this.abortButton, 0, Qt.AlignLeft);
70 layout.addLayout(hboxLayout);
71 layout.addWidget(this.treeWidget, 0, Qt.AlignLeft);
73 this.windowTitle = tr("RSS listing example");
77 RSSListing.prototype = new QWidget();
79 RSSListing.prototype.fetch = function()
81 this.lineEdit.readOnly = true;
82 this.fetchButton.enabled = false;
83 this.abortButton.enabled = true;
84 this.treeWidget.clear();
88 var url = new QUrl(this.lineEdit.text);
90 this.http.setHost(url.host());
91 this.connectionId = this.http.get(url.path());
94 RSSListing.prototype.readData = function(resp)
96 if (resp.statusCode() != 200)
99 this.xml.addData(this.http.readAll());
104 RSSListing.prototype.finished = function(id, error)
107 print("Received error during HTTP fetch."); // ### qWarning()
108 this.lineEdit.readOnly = false;
109 this.abortButton.enabled = false;
110 this.fetchButton.enabled = true;
111 } else if (id == this.connectionId) {
112 this.lineEdit.readOnly = false;
113 this.abortButton.enabled = false;
114 this.fetchButton.enabled = true;
118 RSSListing.prototype.parseXml = function()
120 while (!this.xml.atEnd()) {
122 if (this.xml.isStartElement()) {
123 if (this.xml.name() == "item")
124 this.linkString = this.xml.attributes().value("rss:about").toString();
125 this.currentTag = this.xml.name().toString();
126 } else if (this.xml.isEndElement()) {
127 if (this.xml.name() == "item") {
129 var item = new QTreeWidgetItem();
130 item.setText(0, this.titleString);
131 item.setText(1, this.linkString);
132 this.treeWidget.addTopLevelItem(item);
134 this.titleString = "";
135 this.linkString = "";
138 } else if (this.xml.isCharacters() && !this.xml.isWhitespace()) {
139 if (this.currentTag == "title")
140 this.titleString += this.xml.text().toString();
141 else if (this.currentTag == "link")
142 this.linkString += this.xml.text().toString();
145 if (this.xml.hasError() && (this.xml.error() != QXmlStreamReader.PrematureEndOfDocumentError)) {
146 print("XML ERROR:", this.xml.lineNumber() + ":", this.xml.errorString());
151 RSSListing.prototype.itemActivated = function(item)
153 QDesktopServices.openUrl(new QUrl(item.text(1)));
157 var rsslisting = new RSSListing();
159 QCoreApplication.exec();