protocol.cpp 7.82 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** 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
    if (mt == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)
        || mt == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)
95
        || mt == QLatin1String(CppTools::Constants::OBJECTIVE_C_SOURCE_MIMETYPE)
Tobias Hunger's avatar
Tobias Hunger committed
96 97
        || mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
        return Cpp;
98
    if (mt == QLatin1String(QmlJSTools::Constants::QML_MIMETYPE)
99 100
        || mt == QLatin1String(QmlJSTools::Constants::QMLPROJECT_MIMETYPE)
        || mt == QLatin1String(QmlJSTools::Constants::QBS_MIMETYPE)
101 102
        || mt == QLatin1String(QmlJSTools::Constants::JS_MIMETYPE)
        || mt == QLatin1String(QmlJSTools::Constants::JSON_MIMETYPE))
103 104 105
        return JavaScript;
    if (mt == QLatin1String("text/x-patch"))
        return Diff;
Tobias Hunger's avatar
Tobias Hunger committed
106 107 108 109
    if (mt == QLatin1String("text/xml")
        || mt == QLatin1String("application/xml")
        || mt == QLatin1String(ResourceEditor::Constants::C_RESOURCE_MIMETYPE)
        || mt == QLatin1String(Designer::Constants::FORM_MIMETYPE))
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        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('&')));
135
    data.replace(QLatin1String("&quot;"), QString(QLatin1Char('"')));
136 137
    return data;
}
138

139
bool Protocol::ensureConfiguration(Protocol *p, QWidget *parent)
140 141 142 143 144 145 146 147
{
    QString errorMessage;
    bool ok = false;
    while (true) {
        if (p->checkConfiguration(&errorMessage)) {
            ok = true;
            break;
        }
148 149
        // Cancel returns empty error message.
        if (errorMessage.isEmpty() || !showConfigurationError(p, errorMessage, parent))
150 151 152 153 154 155 156 157 158 159 160 161 162 163
            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
164
        parent = Core::ICore::mainWindow();
165 166 167 168
    const QString title = tr("%1 - Configuration Error").arg(p->name());
    QMessageBox mb(QMessageBox::Warning, title, message, QMessageBox::Cancel, parent);
    QPushButton *settingsButton = 0;
    if (showConfig)
169
        settingsButton = mb.addButton(Core::ICore::msgShowOptionsDialog(), QMessageBox::AcceptRole);
170 171 172
    mb.exec();
    bool rc = false;
    if (mb.clickedButton() == settingsButton)
hjk's avatar
hjk committed
173 174
        rc = Core::ICore::showOptionsDialog(p->settingsPage()->category(),
                                            p->settingsPage()->id(), parent);
175 176 177
    return rc;
}

178
// --------- NetworkProtocol
179

180
QNetworkReply *NetworkProtocol::httpGet(const QString &link)
181 182 183
{
    QUrl url(link);
    QNetworkRequest r(url);
184
    return Utils::NetworkAccessManager::instance()->get(r);
185 186
}

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

NetworkProtocol::~NetworkProtocol()
{
}

201 202 203 204 205 206 207 208 209
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('/'));
    }
210
    QScopedPointer<QNetworkReply> reply(httpGet(url));
211 212 213 214
    QMessageBox box(QMessageBox::Information,
                    tr("Checking connection"),
                    tr("Connecting to %1...").arg(url),
                    QMessageBox::Cancel,
hjk's avatar
hjk committed
215
                    Core::ICore::mainWindow());
216
    connect(reply.data(), SIGNAL(finished()), &box, SLOT(close()));
217 218 219 220 221
    QApplication::setOverrideCursor(Qt::WaitCursor);
    box.exec();
    QApplication::restoreOverrideCursor();
    // User canceled, discard and be happy.
    if (!reply->isFinished()) {
222 223
        QNetworkReply *replyPtr = reply.take();
        connect(replyPtr, SIGNAL(finished()), replyPtr, SLOT(deleteLater()));
224 225 226 227 228 229 230 231 232 233
        return false;
    }
    // Passed
    if (reply->error() == QNetworkReply::NoError)
        return true;
    // Error.
    *errorMessage = reply->errorString();
    return false;
}

234
} //namespace CodePaster