Skip to content

Commit 307b00a

Browse files
committed
Release version 0.1.0
1 parent ed13356 commit 307b00a

15 files changed

+676
-48
lines changed

GistPlugin.json.in

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
2-
\"Name\" : \"GistPlugin\",
3-
\"Version\" : \"0.0.1\",
4-
\"CompatVersion\" : \"0.0.1\",
5-
\"Vendor\" : \"JustCxx\",
2+
\"Name\" : \"GitHubGist\",
3+
\"Version\" : \"3.4.0\",
4+
\"CompatVersion\" : \"3.4.0\",
5+
\"Vendor\" : \"Vasiliy I.Kolesnikov\",
66
\"Copyright\" : \"(C) Vasiliy I.Kolesnikov\",
77
\"License\" : \"The MIT License (MIT)\",
88
\"Description\" : \"GitHub Gist service integration plugin\",
9-
\"Url\" : \"https://github.com/justCxx\",
9+
\"Url\" : \"https://github.com/justCxx/QtcGistPlugin\",
1010
$$dependencyList
1111
}
12-

gistplugin.pro

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
DEFINES += GISTPLUGIN_LIBRARY
22

3+
QT += network
4+
35
# GistPlugin files
46

5-
SOURCES += src/gistplugin.cpp
7+
SOURCES += \
8+
src/gistplugin.cpp \
9+
src/gistmanager.cpp \
10+
src/optionspage.cpp \
11+
src/settings.cpp
612

7-
HEADERS += src/gistplugin.h \
8-
src/gistplugin_global.h \
9-
src/gistpluginconstants.h
13+
HEADERS += \
14+
src/gistplugin.h \
15+
src/gistplugin_global.h \
16+
src/gistpluginconstants.h \
17+
src/gistmanager.h \
18+
src/optionspage.h \
19+
src/settings.h
1020

1121
# Qt Creator linking
1222

1323
## set the QTC_SOURCE environment variable to override the setting here
1424
QTCREATOR_SOURCES = $$(QTC_SOURCE)
15-
isEmpty(QTCREATOR_SOURCES):QTCREATOR_SOURCES=/home/dust/dev/tools/qt-builds/qt-creator/qt-creator
25+
isEmpty(QTCREATOR_SOURCES):QTCREATOR_SOURCES=
1626

1727
## set the QTC_BUILD environment variable to override the setting here
1828
IDE_BUILD_TREE = $$(QTC_BUILD)
19-
isEmpty(IDE_BUILD_TREE):IDE_BUILD_TREE=/home/dust/dev/tools/qt-builds/qt-creator/qt-creator
29+
isEmpty(IDE_BUILD_TREE):IDE_BUILD_TREE=
2030

2131
## uncomment to build plugin into user config directory
2232
## <localappdata>/plugins/<ideversion>
@@ -32,14 +42,24 @@ isEmpty(IDE_BUILD_TREE):IDE_BUILD_TREE=/home/dust/dev/tools/qt-builds/qt-creator
3242

3343
QTC_PLUGIN_NAME = GistPlugin
3444
QTC_LIB_DEPENDS += \
45+
extensionsystem \
46+
utils
3547
# nothing here at this time
3648

3749
QTC_PLUGIN_DEPENDS += \
38-
coreplugin
50+
coreplugin \
51+
projectexplorer \
52+
texteditor
3953

4054
QTC_PLUGIN_RECOMMENDS += \
4155
# optional plugin dependencies. nothing here at this time
4256

4357
###### End _dependencies.pri contents ######
4458

4559
include($$QTCREATOR_SOURCES/src/qtcreatorplugin.pri)
60+
61+
FORMS += \
62+
src/optionspage.ui
63+
64+
RESOURCES += \
65+
gistplugin.qrc

gistplugin.qrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>images/Github-icon.png</file>
4+
<file>images/octocat.png</file>
5+
</qresource>
6+
</RCC>

images/Github-icon.png

4.32 KB
Loading

images/octocat.png

28.1 KB
Loading

src/gistmanager.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "gistmanager.h"
2+
3+
#include <utils/networkaccessmanager.h>
4+
5+
#include <QJsonArray>
6+
#include <QJsonDocument>
7+
#include <QJsonObject>
8+
#include <QJsonValue>
9+
#include <QNetworkAccessManager>
10+
#include <QNetworkReply>
11+
#include <QNetworkRequest>
12+
13+
using namespace Gists::Internal;
14+
15+
QLatin1String API_BASE_URL("https://api.github.com");
16+
QLatin1String API_GIST("/gists");
17+
QLatin1String API_GIST_PUBLIC("/gists/public");
18+
QLatin1String API_USER_INFO("/user");
19+
20+
QLatin1String JSON_CONTENT("content");
21+
QLatin1String JSON_DESCR("description");
22+
QLatin1String JSON_FILES("files");
23+
QLatin1String JSON_HTML_URL("html_url");
24+
QLatin1String JSON_MESSAGE("message");
25+
QLatin1String JSON_PUBLIC("public");
26+
27+
GistManager::GistManager(const QSharedPointer<Settings> &settings, QObject *parent) :
28+
QObject(parent),
29+
m_nam(Utils::NetworkAccessManager::instance()),
30+
m_settings(settings)
31+
{
32+
}
33+
34+
void GistManager::postGist(const QString &text, const QString &gistName,
35+
const QString &fileName, bool publicFlag)
36+
{
37+
QNetworkRequest request(QUrl(API_BASE_URL + API_GIST));
38+
request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("application/json"));
39+
40+
if (!m_settings->isAnonymous()) {
41+
setAuthHeader(&request);
42+
}
43+
44+
QJsonObject file;
45+
file.insert(JSON_CONTENT, text);
46+
47+
QJsonObject files;
48+
files.insert(fileName, file);
49+
50+
QJsonObject postData;
51+
postData.insert(JSON_FILES, files);
52+
postData.insert(JSON_DESCR, gistName);
53+
postData.insert(JSON_PUBLIC, publicFlag);
54+
55+
QNetworkReply *reply = m_nam->post(request, QJsonDocument(postData).toJson());
56+
57+
connect(reply, &QNetworkReply::finished, this, &GistManager::apiResponse);
58+
}
59+
60+
/*!
61+
* \brief GistManager::makeAuthRequest
62+
*
63+
* HTTP Basic authentication:
64+
* header name: "Authorization"
65+
* header value: "Basic + base64(username:password)"
66+
*/
67+
68+
void GistManager::setAuthHeader(QNetworkRequest *request)
69+
{
70+
QByteArray headerData = QByteArray("Basic ")
71+
+ QString(QLatin1String("%1:%2"))
72+
.arg(m_settings->username)
73+
.arg(m_settings->token)
74+
.toLocal8Bit()
75+
.toBase64();
76+
request->setRawHeader("Authorization", headerData);
77+
}
78+
79+
void GistManager::apiResponse()
80+
{
81+
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
82+
83+
quint32 httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
84+
QJsonDocument jsonReply = QJsonDocument::fromJson(reply->readAll());
85+
86+
switch (httpStatusCode) {
87+
case 200: //! 200 OK
88+
break;
89+
case 201: { //! 201 Created
90+
QString name = jsonReply.object().value(JSON_DESCR).toString();
91+
QString url = jsonReply.object().value(JSON_HTML_URL).toString();
92+
emit gistPosted(name, url);
93+
break;
94+
}
95+
default:
96+
QString error = jsonReply.object().value(JSON_MESSAGE).toString();
97+
emit apiError(tr("Gist API ERROR: ") + error);
98+
break;
99+
}
100+
101+
reply->deleteLater();
102+
}
103+

src/gistmanager.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef GISTSMANAGER_H
2+
#define GISTSMANAGER_H
3+
4+
#include "settings.h"
5+
6+
#include <QObject>
7+
#include <QSharedPointer>
8+
9+
QT_BEGIN_NAMESPACE
10+
class QNetworkAccessManager;
11+
class QNetworkRequest;
12+
QT_END_NAMESPACE
13+
14+
namespace Gists {
15+
namespace Internal {
16+
17+
class GistManager : public QObject
18+
{
19+
Q_OBJECT
20+
public:
21+
explicit GistManager(const QSharedPointer<Settings> &settings, QObject *parent = 0);
22+
23+
void postGist(const QString &text, const QString &gistName,
24+
const QString &fileName, bool publicFlag = false);
25+
26+
void setUserName(const QString &name);
27+
void setOAuthToken(const QString &token);
28+
29+
signals:
30+
void gistPosted(const QString &name, const QString &url);
31+
void apiError(const QString &error);
32+
33+
private:
34+
void setAuthHeader(QNetworkRequest *request);
35+
void apiResponse();
36+
37+
QNetworkAccessManager *m_nam;
38+
const QSharedPointer<Settings> m_settings;
39+
};
40+
41+
} // namespace Internal
42+
} // namespace Gists
43+
44+
#endif // GISTSMANAGER_H

0 commit comments

Comments
 (0)