protocol.cpp 7.36 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
32
#include "protocol.h"
33

34
35
#include <utils/networkaccessmanager.h>

36
37
#include <cpptools/cpptoolsconstants.h>
#include <qmljseditor/qmljseditorconstants.h>
38
39
#include <coreplugin/icore.h>
#include <coreplugin/dialogs/ioptionspage.h>
40

41
#include <QtNetwork/QNetworkRequest>
42
#include <QtNetwork/QNetworkReply>
43
44

#include <QtCore/QUrl>
45
#include <QtCore/QDebug>
46
#include <QtCore/QVariant>
47

48
#include <QtGui/QMessageBox>
49
#include <QtGui/QApplication>
50
51
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
52

53
namespace CodePaster {
con's avatar
con committed
54

55
56
57
58
59
60
61
62
63
Protocol::Protocol()
        : QObject()
{
}

Protocol::~Protocol()
{
}

64
bool Protocol::hasSettings() const
65
{
66
    return false;
67
}
con's avatar
con committed
68

69
bool Protocol::checkConfiguration(QString *)
70
71
72
73
{
    return true;
}

74
Core::IOptionsPage *Protocol::settingsPage() const
75
76
77
{
    return 0;
}
con's avatar
con committed
78

79
void Protocol::list()
con's avatar
con committed
80
{
81
82
    qFatal("Base Protocol list() called");
}
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Protocol::ContentType Protocol::contentType(const QString &mt)
{
    if (mt  == QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))
        return C;
    if (mt == QLatin1String(QmlJSEditor::Constants::QML_MIMETYPE)
        || mt == QLatin1String(QmlJSEditor::Constants::JS_MIMETYPE))
        return JavaScript;
    if (mt == QLatin1String("text/x-patch"))
        return Diff;
    if (mt == QLatin1String("text/xml") || mt == QLatin1String("application/xml"))
        return Xml;
    return Text;
}

QString Protocol::fixNewLines(QString data)
{
    // Copied from cpaster. Otherwise lineendings will screw up
    // HTML requires "\r\n".
    if (data.contains(QLatin1String("\r\n")))
        return data;
    if (data.contains(QLatin1Char('\n'))) {
        data.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
        return data;
    }
    if (data.contains(QLatin1Char('\r')))
        data.replace(QLatin1Char('\r'), QLatin1String("\r\n"));
    return data;
}

QString Protocol::textFromHtml(QString data)
{
    data.remove(QLatin1Char('\r'));
    data.replace(QLatin1String("&lt;"), QString(QLatin1Char('<')));
    data.replace(QLatin1String("&gt;"), QString(QLatin1Char('>')));
    data.replace(QLatin1String("&amp;"), QString(QLatin1Char('&')));
123
    data.replace(QLatin1String("&quot;"), QString(QLatin1Char('"')));
124
125
    return data;
}
126

127
bool Protocol::ensureConfiguration(Protocol *p, QWidget *parent)
128
129
130
131
132
133
134
135
{
    QString errorMessage;
    bool ok = false;
    while (true) {
        if (p->checkConfiguration(&errorMessage)) {
            ok = true;
            break;
        }
136
137
        // Cancel returns empty error message.
        if (errorMessage.isEmpty() || !showConfigurationError(p, errorMessage, parent))
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
            break;
    }
    return ok;
}

bool Protocol::showConfigurationError(const Protocol *p,
                                      const QString &message,
                                      QWidget *parent,
                                      bool showConfig)
{
    if (!p->settingsPage())
        showConfig = false;

    if (!parent)
        parent = Core::ICore::instance()->mainWindow();
    const QString title = tr("%1 - Configuration Error").arg(p->name());
    QMessageBox mb(QMessageBox::Warning, title, message, QMessageBox::Cancel, parent);
    QPushButton *settingsButton = 0;
    if (showConfig)
        settingsButton = mb.addButton(tr("Settings..."), QMessageBox::AcceptRole);
    mb.exec();
    bool rc = false;
    if (mb.clickedButton() == settingsButton)
        rc = Core::ICore::instance()->showOptionsDialog(p->settingsPage()->category(),
                                                        p->settingsPage()->id(),
                                                        parent);
    return rc;
}


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// ------------ NetworkAccessManagerProxy
NetworkAccessManagerProxy::NetworkAccessManagerProxy()
{
}

NetworkAccessManagerProxy::~NetworkAccessManagerProxy()
{
}

QNetworkReply *NetworkAccessManagerProxy::httpGet(const QString &link)
{
    QUrl url(link);
    QNetworkRequest r(url);
    return networkAccessManager()->get(r);
}

QNetworkReply *NetworkAccessManagerProxy::httpPost(const QString &link, const QByteArray &data)
{
    QUrl url(link);
    QNetworkRequest r(url);
188
189
190
    // Required for Qt 4.8
    r.setHeader(QNetworkRequest::ContentTypeHeader,
                QVariant(QByteArray("application/x-www-form-urlencoded")));
191
192
193
194
195
196
    return networkAccessManager()->post(r, data);
}

QNetworkAccessManager *NetworkAccessManagerProxy::networkAccessManager()
{
    if (m_networkAccessManager.isNull())
197
        m_networkAccessManager.reset(new Utils::NetworkAccessManager);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    return m_networkAccessManager.data();
}

// --------- NetworkProtocol

NetworkProtocol::NetworkProtocol(const NetworkAccessManagerProxyPtr &nw) :
    m_networkAccessManager(nw)
{
}

NetworkProtocol::~NetworkProtocol()
{
}

212
213
214
215
216
217
218
219
220
bool NetworkProtocol::httpStatus(QString url, QString *errorMessage)
{
    // Connect to host and display a message box, using its event loop.
    errorMessage->clear();
    const QString httpPrefix = QLatin1String("http://");
    if (!url.startsWith(httpPrefix)) {
        url.prepend(httpPrefix);
        url.append(QLatin1Char('/'));
    }
221
    QScopedPointer<QNetworkReply> reply(httpGet(url));
222
223
224
225
226
    QMessageBox box(QMessageBox::Information,
                    tr("Checking connection"),
                    tr("Connecting to %1...").arg(url),
                    QMessageBox::Cancel,
                    Core::ICore::instance()->mainWindow());
227
    connect(reply.data(), SIGNAL(finished()), &box, SLOT(close()));
228
229
230
231
232
    QApplication::setOverrideCursor(Qt::WaitCursor);
    box.exec();
    QApplication::restoreOverrideCursor();
    // User canceled, discard and be happy.
    if (!reply->isFinished()) {
233
234
        QNetworkReply *replyPtr = reply.take();
        connect(replyPtr, SIGNAL(finished()), replyPtr, SLOT(deleteLater()));
235
236
237
238
239
240
241
242
243
244
        return false;
    }
    // Passed
    if (reply->error() == QNetworkReply::NoError)
        return true;
    // Error.
    *errorMessage = reply->errorString();
    return false;
}

245
} //namespace CodePaster