OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cpaster / pastebindotcaprotocol.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "pastebindotcaprotocol.h"
34
35 #include <utils/qtcassert.h>
36
37 #include <QtNetwork/QNetworkReply>
38 #include <QtCore/QXmlStreamReader>
39 #include <QtCore/QXmlStreamAttributes>
40 #include <QtCore/QStringList>
41
42 static const char urlC[] = "http://pastebin.ca/";
43
44 namespace CodePaster {
45 PasteBinDotCaProtocol::PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw) :
46     NetworkProtocol(nw),
47     m_fetchReply(0),
48     m_listReply(0),
49     m_pasteReply(0),
50     m_hostChecked(false)
51 {
52 }
53
54 unsigned PasteBinDotCaProtocol::capabilities() const
55 {
56     return ListCapability | PostDescriptionCapability;
57 }
58
59 void PasteBinDotCaProtocol::fetch(const QString &id)
60 {
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.
66     QString link = id;
67     if (link.startsWith(url)) {
68         const int lastSlashPos = link.lastIndexOf(QLatin1Char('/'));
69         if (lastSlashPos != -1)
70             link.insert(lastSlashPos + 1, rawPostFix);
71     } else {
72         link.insert(0, rawPostFix);
73         link.insert(0, url);
74     }
75     m_fetchReply = httpGet(link);
76     connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
77     m_fetchId = id;
78 }
79
80 void PasteBinDotCaProtocol::paste(const QString &text,
81                                   ContentType /* ct */,
82                                   const QString &username,
83                                   const QString & /* comment */,
84                                   const QString &description)
85 {
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);
93     // fire request
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()));
97 }
98
99 void PasteBinDotCaProtocol::pasteFinished()
100 {
101     if (m_pasteReply->error()) {
102         qWarning("Pastebin.ca protocol error: %s", qPrintable(m_pasteReply->errorString()));
103     } else {
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);
108     }
109     m_pasteReply->deleteLater();
110     m_pasteReply = 0;
111 }
112
113 void PasteBinDotCaProtocol::fetchFinished()
114 {
115     QString title;
116     QString content;
117     bool error = m_fetchReply->error();
118     if (error) {
119         content = m_fetchReply->errorString();
120     } else {
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'));
125     }
126     m_fetchReply->deleteLater();
127     m_fetchReply = 0;
128     emit fetchDone(title, content, error);
129 }
130
131 void PasteBinDotCaProtocol::list()
132 {
133     QTC_ASSERT(!m_listReply, return);
134     m_listReply = httpGet(QLatin1String(urlC));
135     connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
136 }
137
138 bool PasteBinDotCaProtocol::checkConfiguration(QString *errorMessage)
139 {
140     if (m_hostChecked) // Check the host once.
141         return true;
142     const bool ok = httpStatus(QLatin1String(urlC), errorMessage);
143     if (ok)
144         m_hostChecked = true;
145     return ok;
146 }
147
148 /* Quick & dirty: Parse the <div>-elements with the "Recent Posts" listing
149  * out of the page.
150 \code
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>
156         </div>
157  ...<h2>Create a New Pastebin Post</h2>
158 \endcode */
159
160 static inline QStringList parseLists(QIODevice *io)
161 {
162     enum State { OutsideRecentLink, InsideRecentLink };
163
164     QStringList rc;
165
166     const QString classAttribute = QLatin1String("class");
167     const QString divElement = QLatin1String("div");
168     const QString anchorElement = QLatin1String("a");
169
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)
176         return rc;
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('/')))
188                     link.remove(0, 1);
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;
194             } // divElement
195             break;
196        default:
197             break;
198         } // switch reader
199     } // while reader.atEnd()
200     return rc;
201 }
202
203 void PasteBinDotCaProtocol::listFinished()
204 {
205     const bool error = m_listReply->error();
206     if (error) {
207         qWarning("pastebin.ca list failed: %s", qPrintable(m_listReply->errorString()));
208     } else {
209         emit listDone(name(), parseLists(m_listReply));
210     }
211     m_listReply->deleteLater();
212     m_listReply = 0;
213 }
214
215 } // namespace CodePaster