1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "pastebindotcaprotocol.h"
35 #include <utils/qtcassert.h>
37 #include <QtNetwork/QNetworkReply>
38 #include <QtCore/QXmlStreamReader>
39 #include <QtCore/QXmlStreamAttributes>
40 #include <QtCore/QStringList>
42 static const char urlC[] = "http://pastebin.ca/";
44 namespace CodePaster {
45 PasteBinDotCaProtocol::PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw) :
54 unsigned PasteBinDotCaProtocol::capabilities() const
56 return ListCapability | PostDescriptionCapability;
59 void PasteBinDotCaProtocol::fetch(const QString &id)
61 QTC_ASSERT(!m_fetchReply, return)
62 const QString url = QLatin1String(urlC);
63 const QString rawPostFix = QLatin1String("raw/");
64 // Create link as ""http://pastebin.ca/raw/[id]"
65 // If we get a complete URL, just insert 'raw', else build URL.
67 if (link.startsWith(url)) {
68 const int lastSlashPos = link.lastIndexOf(QLatin1Char('/'));
69 if (lastSlashPos != -1)
70 link.insert(lastSlashPos + 1, rawPostFix);
72 link.insert(0, rawPostFix);
75 m_fetchReply = httpGet(link);
76 connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
80 void PasteBinDotCaProtocol::paste(const QString &text,
82 const QString &username,
83 const QString & /* comment */,
84 const QString &description)
86 QTC_ASSERT(!m_pasteReply, return)
87 QByteArray data = "content=";
88 data += QUrl::toPercentEncoding(fixNewLines(text));
89 data += "&description=";
90 data += QUrl::toPercentEncoding(description);
91 data += "&type=1&expiry=1%20day&name=";
92 data += QUrl::toPercentEncoding(username);
94 const QString link = QLatin1String(urlC) + QLatin1String("quiet-paste.php");
95 m_pasteReply = httpPost(link, data);
96 connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
99 void PasteBinDotCaProtocol::pasteFinished()
101 if (m_pasteReply->error()) {
102 qWarning("Pastebin.ca protocol error: %s", qPrintable(m_pasteReply->errorString()));
104 /// returns ""SUCCESS:[id]""
105 const QByteArray data = m_pasteReply->readAll();
106 const QString link = QString::fromLatin1(urlC) + QString::fromAscii(data).remove(QLatin1String("SUCCESS:"));
107 emit pasteDone(link);
109 m_pasteReply->deleteLater();
113 void PasteBinDotCaProtocol::fetchFinished()
117 bool error = m_fetchReply->error();
119 content = m_fetchReply->errorString();
121 title = QString::fromLatin1("Pastebin.ca: %1").arg(m_fetchId);
122 const QByteArray data = m_fetchReply->readAll();
123 content = QString::fromUtf8(data);
124 content.remove(QLatin1Char('\r'));
126 m_fetchReply->deleteLater();
128 emit fetchDone(title, content, error);
131 void PasteBinDotCaProtocol::list()
133 QTC_ASSERT(!m_listReply, return);
134 m_listReply = httpGet(QLatin1String(urlC));
135 connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
138 bool PasteBinDotCaProtocol::checkConfiguration(QString *errorMessage)
140 if (m_hostChecked) // Check the host once.
142 const bool ok = httpStatus(QLatin1String(urlC), errorMessage);
144 m_hostChecked = true;
148 /* Quick & dirty: Parse the <div>-elements with the "Recent Posts" listing
151 <div class="menutitle"><h2>Recent Posts</h2></div>
152 <div class="items" id="idmenurecent-collapse">
153 <div class='recentlink'>
154 <a href="/[id]" class="rjt" rel="/preview.php?id=[id]">[nameTitle]</a>
155 <div class='recentdetail'>[time spec]</div>
157 ...<h2>Create a New Pastebin Post</h2>
160 static inline QStringList parseLists(QIODevice *io)
162 enum State { OutsideRecentLink, InsideRecentLink };
166 const QString classAttribute = QLatin1String("class");
167 const QString divElement = QLatin1String("div");
168 const QString anchorElement = QLatin1String("a");
170 // Start parsing at the 'recent posts' entry as the HTML above is not well-formed
171 // as of 8.4.2010. This will then terminate with an error.
172 QByteArray data = io->readAll();
173 const QByteArray recentPosts("<h2>Recent Posts</h2></div>");
174 const int recentPostsPos = data.indexOf(recentPosts);
175 if (recentPostsPos == -1)
177 data.remove(0, recentPostsPos + recentPosts.size());
178 QXmlStreamReader reader(data);
179 State state = OutsideRecentLink;
180 while (!reader.atEnd()) {
181 switch(reader.readNext()) {
182 case QXmlStreamReader::StartElement:
183 // Inside a <div> of an entry: Anchor or description
184 if (state == InsideRecentLink && reader.name() == anchorElement) { // Anchor
185 // Strip host from link
186 QString link = reader.attributes().value(QLatin1String("href")).toString();
187 if (link.startsWith(QLatin1Char('/')))
189 const QString nameTitle = reader.readElementText();
190 rc.push_back(link + QLatin1Char(' ') + nameTitle);
191 } else if (state == OutsideRecentLink && reader.name() == divElement) { // "<div>" state switching
192 if (reader.attributes().value(classAttribute) == QLatin1String("recentlink"))
193 state = InsideRecentLink;
199 } // while reader.atEnd()
203 void PasteBinDotCaProtocol::listFinished()
205 const bool error = m_listReply->error();
207 qWarning("pastebin.ca list failed: %s", qPrintable(m_listReply->errorString()));
209 emit listDone(name(), parseLists(m_listReply));
211 m_listReply->deleteLater();
215 } // namespace CodePaster