protocol.cpp 8.15 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** 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.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
#include "protocol.h"
30

31
32
#include <utils/networkaccessmanager.h>

33
#include <cpptools/cpptoolsconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
34
35
#include <designer/designerconstants.h>
#include <glsleditor/glsleditorconstants.h>
36
#include <qmljstools/qmljstoolsconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
37
#include <resourceeditor/resourceeditorconstants.h>
38
39
#include <coreplugin/icore.h>
#include <coreplugin/dialogs/ioptionspage.h>
40

41
42
#include <QNetworkRequest>
#include <QNetworkReply>
43

44
45
46
#include <QUrl>
#include <QDebug>
#include <QVariant>
47

48
49
50
#include <QMessageBox>
#include <QApplication>
#include <QPushButton>
51

52
namespace CodePaster {
con's avatar
con committed
53

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

Protocol::~Protocol()
{
}

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

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

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

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

83
84
Protocol::ContentType Protocol::contentType(const QString &mt)
{
Tobias Hunger's avatar
Tobias Hunger committed
85
    if (mt == QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE)
86
        || mt == QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)
Tobias Hunger's avatar
Tobias Hunger committed
87
88
89
90
91
        || mt == QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE)
        || mt == QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT)
        || mt == QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG)
        || mt == QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT_ES)
        || mt == QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG_ES))
92
        return C;
Tobias Hunger's avatar
Tobias Hunger committed
93
94
95
96
97
    if (mt == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
        return Cpp;
98
99
100
    if (mt == QLatin1String(QmlJSTools::Constants::QML_MIMETYPE)
        || mt == QLatin1String(QmlJSTools::Constants::JS_MIMETYPE)
        || mt == QLatin1String(QmlJSTools::Constants::JSON_MIMETYPE))
101
102
103
        return JavaScript;
    if (mt == QLatin1String("text/x-patch"))
        return Diff;
Tobias Hunger's avatar
Tobias Hunger committed
104
105
106
107
    if (mt == QLatin1String("text/xml")
        || mt == QLatin1String("application/xml")
        || mt == QLatin1String(ResourceEditor::Constants::C_RESOURCE_MIMETYPE)
        || mt == QLatin1String(Designer::Constants::FORM_MIMETYPE))
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        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('&')));
133
    data.replace(QLatin1String("&quot;"), QString(QLatin1Char('"')));
134
135
    return data;
}
136

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

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

    if (!parent)
hjk's avatar
hjk committed
162
        parent = Core::ICore::mainWindow();
163
164
165
166
167
168
169
170
    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)
hjk's avatar
hjk committed
171
172
        rc = Core::ICore::showOptionsDialog(p->settingsPage()->category(),
                                            p->settingsPage()->id(), parent);
173
174
175
176
    return rc;
}


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// ------------ 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);
197
198
199
    // Required for Qt 4.8
    r.setHeader(QNetworkRequest::ContentTypeHeader,
                QVariant(QByteArray("application/x-www-form-urlencoded")));
200
201
202
203
204
205
    return networkAccessManager()->post(r, data);
}

QNetworkAccessManager *NetworkAccessManagerProxy::networkAccessManager()
{
    if (m_networkAccessManager.isNull())
206
        m_networkAccessManager.reset(new Utils::NetworkAccessManager);
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    return m_networkAccessManager.data();
}

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

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

NetworkProtocol::~NetworkProtocol()
{
}

221
222
223
224
225
226
227
228
229
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('/'));
    }
230
    QScopedPointer<QNetworkReply> reply(httpGet(url));
231
232
233
234
    QMessageBox box(QMessageBox::Information,
                    tr("Checking connection"),
                    tr("Connecting to %1...").arg(url),
                    QMessageBox::Cancel,
hjk's avatar
hjk committed
235
                    Core::ICore::mainWindow());
236
    connect(reply.data(), SIGNAL(finished()), &box, SLOT(close()));
237
238
239
240
241
    QApplication::setOverrideCursor(Qt::WaitCursor);
    box.exec();
    QApplication::restoreOverrideCursor();
    // User canceled, discard and be happy.
    if (!reply->isFinished()) {
242
243
        QNetworkReply *replyPtr = reply.take();
        connect(replyPtr, SIGNAL(finished()), replyPtr, SLOT(deleteLater()));
244
245
246
247
248
249
250
251
252
253
        return false;
    }
    // Passed
    if (reply->error() == QNetworkReply::NoError)
        return true;
    // Error.
    *errorMessage = reply->errorString();
    return false;
}

254
} //namespace CodePaster