codepasterprotocol.cpp 6.62 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
30
31
32
33
34
35
36
37
38
39
**
**************************************************************************/

#include "codepasterprotocol.h"
#include "codepastersettings.h"
#include "cpasterplugin.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/messageoutputwindow.h>

40
41
#include <utils/qtcassert.h>

42
43
#include <QtGui/QListWidget>
#include <QtNetwork/QNetworkReply>
44
45
46
#include <QtCore/QDebug>

enum { debug = 0 };
47

48
namespace CodePaster {
49

50
51
CodePasterProtocol::CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw) :
    NetworkProtocol(nw),
52
53
54
55
56
    m_page(new CodePaster::CodePasterSettingsPage),
    m_pasteReply(0),
    m_fetchReply(0),
    m_listReply(0),
    m_fetchId(-1)
57
58
59
60
61
62
63
64
65
{
}

CodePasterProtocol::~CodePasterProtocol()
{
}

QString CodePasterProtocol::name() const
{
66
    return QLatin1String("CodePaster");
67
68
}

69
unsigned CodePasterProtocol::capabilities() const
70
{
71
    return ListCapability|PostCommentCapability|PostDescriptionCapability;
72
73
}

74
bool CodePasterProtocol::checkConfiguration(QString *errorMessage)
75
{
76
77
    const QString hostName = m_page->hostName();
    if (hostName.isEmpty()) {
78
79
        if (errorMessage) {
            *errorMessage =
80
#ifdef Q_OS_MAC
81
                       tr("No Server defined in the CodePaster preferences.");
82
#else
83
                       tr("No Server defined in the CodePaster options.");
84
#endif
85
        }
86
87
        return false;
    }
88
89
90
91
92
93
94
    // Check the host once. Note that it can be modified in the settings page.
    if (m_hostChecked == hostName)
        return true;
    const bool ok = httpStatus(m_page->hostName(), errorMessage);
    if (ok)
        m_hostChecked = hostName;
    return ok;
95
96
}

97
98
void CodePasterProtocol::fetch(const QString &id)
{
99
100
    QTC_ASSERT(!m_fetchReply, return; )

101
    QString hostName = m_page->hostName();
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    const QString httpPrefix = "http://";
    QString link;
    // Did the user enter a complete URL instead of an id?
    if (id.startsWith(httpPrefix)) {
        // Append 'raw' format option
        link = id;
        link += QLatin1String("&format=raw");
        const int idPos = id.lastIndexOf(QLatin1Char('='));
        m_fetchId = idPos != -1 ? id.mid(idPos + 1) : id;
    } else {
        link = httpPrefix;
        link.append(hostName);
        link.append("/?format=raw&id=");
        link.append(id);
        m_fetchId = id;
    }
118
    m_fetchReply = httpGet(link);
119
    connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
120
121
}

122
void CodePasterProtocol::list()
123
{
124
125
    QTC_ASSERT(!m_listReply, return; )

126
    QString hostName = m_page->hostName();
127
    QString link = QLatin1String("http://");
128
    link += hostName;
129
    link += QLatin1String("/?command=browse&format=raw");
130
    m_listReply = httpGet(link);
131
    connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
132
133
134
}

void CodePasterProtocol::paste(const QString &text,
135
                               ContentType /* ct */,
136
137
138
                               const QString &username,
                               const QString &comment,
                               const QString &description)
139
{
140
141
    QTC_ASSERT(!m_pasteReply, return; )
    const QString hostName = m_page->hostName();
142

143
    QByteArray data = "command=processcreate&submit=submit&highlight_type=0&description=";
144
    data += QUrl::toPercentEncoding(description);
145
    data += "&comment=";
146
    data += QUrl::toPercentEncoding(comment);
147
    data += "&code=";
148
    data += QUrl::toPercentEncoding(fixNewLines(text));
149
    data += "&poster=";
150
    data += QUrl::toPercentEncoding(username);
151

152
    m_pasteReply = httpPost(QLatin1String("http://") + hostName, data);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
}

void CodePasterProtocol::pasteFinished()
{
    if (m_pasteReply->error()) {
        qWarning("Error pasting: %s", qPrintable(m_pasteReply->errorString()));
    } else {
        // Cut out the href-attribute
        QString contents = QString::fromAscii(m_pasteReply->readAll());
        int hrefPos = contents.indexOf(QLatin1String("href=\""));
        if (hrefPos != -1) {
            hrefPos += 6;
            const int endPos = contents.indexOf(QLatin1Char('"'), hrefPos);
            if (endPos != -1)
                emit pasteDone(contents.mid(hrefPos, endPos - hrefPos));
        }
    }
    m_pasteReply->deleteLater();
    m_pasteReply = 0;
173
174
175
176
177
178
179
}

bool CodePasterProtocol::hasSettings() const
{
    return true;
}

180
Core::IOptionsPage *CodePasterProtocol::settingsPage() const
181
182
183
184
185
186
{
    return m_page;
}

void CodePasterProtocol::fetchFinished()
{
187
188
    QString title;
    QString content;
189
    bool error = m_fetchReply->error();
190
    if (error) {
191
        content = m_fetchReply->errorString();
192
    } else {
193
194
195
        content = m_fetchReply->readAll();
        if (debug)
            qDebug() << content;
196
197
198
199
        if (content.contains("<B>No such paste!</B>")) {
            content = tr("No such paste");
            error = true;
        }
200
        title = QString::fromLatin1("Codepaster: %1").arg(m_fetchId);
201
    }
202
203
    m_fetchReply->deleteLater();
    m_fetchReply = 0;
204
    emit fetchDone(title, content, error);
205
206
207
208
}

void CodePasterProtocol::listFinished()
{
209
210
    if (m_listReply->error()) {
        Core::ICore::instance()->messageManager()->printToOutputPane(m_listReply->errorString(), true);
211
    } else {
212
        const QByteArray data = m_listReply->readAll();
213
214
        const QStringList lines = QString::fromAscii(data).split(QLatin1Char('\n'));
        emit listDone(name(), lines);
215
    }
216
217
    m_listReply->deleteLater();
    m_listReply = 0;
218
219
}

220
} // namespace CodePaster