protocol.h 4.89 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

33 34
#ifndef PROTOCOL_H
#define PROTOCOL_H
35

36
#include <QtCore/QObject>
37 38 39 40 41 42
#include <QtCore/QScopedPointer>
#include <QtCore/QSharedPointer>

QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
43
class QWidget;
44
QT_END_NAMESPACE
con's avatar
con committed
45

46
namespace Core {
hjk's avatar
hjk committed
47
class IOptionsPage;
48
}
49

50
namespace CodePaster {
hjk's avatar
hjk committed
51

52
class Protocol : public QObject
con's avatar
con committed
53 54
{
    Q_OBJECT
hjk's avatar
hjk committed
55

con's avatar
con committed
56
public:
hjk's avatar
hjk committed
57
    enum ContentType {
58 59 60
        Text, C, JavaScript, Diff, Xml
    };

61 62 63 64 65
    enum Capabilities  {
        ListCapability = 0x1,
        PostCommentCapability = 0x2,
        PostDescriptionCapability = 0x4
    };
66

67
    virtual ~Protocol();
con's avatar
con committed
68

69
    virtual QString name() const = 0;
con's avatar
con committed
70

71
    virtual unsigned capabilities() const = 0;
72
    virtual bool hasSettings() const;
73
    virtual Core::IOptionsPage *settingsPage() const;
con's avatar
con committed
74

75
    virtual bool checkConfiguration(QString *errorMessage = 0);
76
    virtual void fetch(const QString &id) = 0;
77
    virtual void list();
78
    virtual void paste(const QString &text,
79
                       ContentType ct = Text,
80 81 82
                       const QString &username = QString(),
                       const QString &comment = QString(),
                       const QString &description = QString()) = 0;
con's avatar
con committed
83

84 85 86
    // Convenience to determine content type from mime type
    static ContentType contentType(const QString &mimeType);

87 88 89 90 91 92 93
    // Show a configuration error and point user to settings.
    // Return true when settings changed.
    static bool showConfigurationError(const Protocol *p,
                                       const QString &message,
                                       QWidget *parent = 0,
                                       bool showConfig = true);
    // Ensure configuration is correct
94
    static bool ensureConfiguration(Protocol *p,
95 96
                                    QWidget *parent = 0);

97 98
signals:
    void pasteDone(const QString &link);
99 100 101
    void fetchDone(const QString &titleDescription,
                   const QString &content,
                   bool error);
102
    void listDone(const QString &name, const QStringList &result);
103 104

protected:
105
    Protocol();
106 107
    static QString textFromHtml(QString data);
    static QString fixNewLines(QString in);
108 109 110 111 112 113
};

/* Proxy for NetworkAccessManager that can be shared with
 * delayed initialization and conveniences
 * for HTTP-requests. */

hjk's avatar
hjk committed
114 115
class NetworkAccessManagerProxy
{
116
    Q_DISABLE_COPY(NetworkAccessManagerProxy)
hjk's avatar
hjk committed
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
public:
    NetworkAccessManagerProxy();
    ~NetworkAccessManagerProxy();

    QNetworkReply *httpGet(const QString &url);
    QNetworkReply *httpPost(const QString &link, const QByteArray &data);
    QNetworkAccessManager *networkAccessManager();

private:
    QScopedPointer<QNetworkAccessManager> m_networkAccessManager;
};

/* Network-based protocol: Provides access with delayed
 * initialization to a QNetworkAccessManager and conveniences
 * for HTTP-requests. */

hjk's avatar
hjk committed
134 135
class NetworkProtocol : public Protocol
{
136
    Q_OBJECT
hjk's avatar
hjk committed
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
public:
    virtual ~NetworkProtocol();

protected:
    typedef QSharedPointer<NetworkAccessManagerProxy> NetworkAccessManagerProxyPtr;

    explicit NetworkProtocol(const NetworkAccessManagerProxyPtr &nw);

    inline QNetworkReply *httpGet(const QString &url)
    { return m_networkAccessManager->httpGet(url); }

    inline QNetworkReply *httpPost(const QString &link, const QByteArray &data)
    { return m_networkAccessManager->httpPost(link, data); }

    inline QNetworkAccessManager *networkAccessManager()
    { return m_networkAccessManager->networkAccessManager(); }
154

155 156 157
    // Check connectivity of host, displaying a message box.
    bool httpStatus(QString url, QString *errorMessage);

158 159
private:
    const NetworkAccessManagerProxyPtr m_networkAccessManager;
con's avatar
con committed
160 161
};

162 163
} //namespace CodePaster

164
#endif // PROTOCOL_H