Skip to content

Commit 02f65f5

Browse files
committed
Merged branch 'develop'
2 parents f2e324f + 0f249b3 commit 02f65f5

File tree

6 files changed

+140
-15
lines changed

6 files changed

+140
-15
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ Makefile*
3636
# Operating System Files
3737
# =========================
3838

39+
# Linux
40+
#==========================
41+
42+
# KDE
43+
.directory
44+
3945
# OSX
4046
# =========================
4147

@@ -77,4 +83,4 @@ $RECYCLE.BIN/
7783
*.msp
7884

7985
# Windows shortcuts
80-
*.lnk
86+
*.lnk

src/gistmanager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <utils/networkaccessmanager.h>
44

5+
#include <QFile>
6+
#include <QFileInfo>
57
#include <QJsonArray>
68
#include <QJsonDocument>
79
#include <QJsonObject>
@@ -54,7 +56,36 @@ void GistManager::postGist(const QString &text, const QString &gistName,
5456
postData.insert(JSON_PUBLIC, publicFlag);
5557

5658
QNetworkReply *reply = m_nam->post(request, QJsonDocument(postData).toJson());
59+
connect(reply, &QNetworkReply::finished, this, &GistManager::apiResponse);
60+
}
61+
62+
void GistManager::postGist(const QStringList &files, const QString &gistName, bool publicFlag)
63+
{
64+
QNetworkRequest request(QUrl(API_BASE_URL + API_GIST));
65+
request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("application/json"));
66+
67+
if (!m_settings->isAnonymous()) {
68+
setAuthHeader(&request);
69+
}
70+
71+
QJsonObject gistFilesJson;
5772

73+
for (auto fileName : files) {
74+
QFile file(fileName);
75+
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
76+
QFileInfo fileInfo(file);
77+
QJsonObject gistFileJson;
78+
gistFileJson.insert(JSON_CONTENT, QLatin1String(file.readAll()));
79+
gistFilesJson.insert(fileInfo.fileName(), gistFileJson);
80+
}
81+
}
82+
83+
QJsonObject postData;
84+
postData.insert(JSON_FILES, gistFilesJson);
85+
postData.insert(JSON_DESCR, gistName);
86+
postData.insert(JSON_PUBLIC, publicFlag);
87+
88+
QNetworkReply *reply = m_nam->post(request, QJsonDocument(postData).toJson());
5889
connect(reply, &QNetworkReply::finished, this, &GistManager::apiResponse);
5990
}
6091

src/gistmanager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class GistManager : public QObject
2323
void postGist(const QString &text, const QString &gistName,
2424
const QString &fileName, bool publicFlag = false);
2525

26+
void postGist(const QStringList &files, const QString &gistName,
27+
bool publicFlag = false);
28+
2629
signals:
2730
void gistPosted(const QString &name, const QString &url);
2831
void apiError(const QString &error);

src/gistplugin.cpp

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <coreplugin/messagemanager.h>
1616
#include <texteditor/texteditor.h>
1717
#include <texteditor/textdocument.h>
18+
#include <projectexplorer/project.h>
19+
#include <projectexplorer/projectexplorer.h>
20+
#include <projectexplorer/projectnodes.h>
21+
#include <projectexplorer/projecttree.h>
1822

1923
#include <QtPlugin>
2024
#include <QAction>
@@ -25,6 +29,7 @@
2529

2630
using namespace Core;
2731
using namespace TextEditor;
32+
using namespace ProjectExplorer;
2833

2934
namespace GitHubGist {
3035
namespace Internal {
@@ -61,8 +66,8 @@ bool GistPlugin::initialize(const QStringList &arguments, QString *errorString)
6166
connect(m_gistManager, &GistManager::apiError, this, &GistPlugin::showMessage);
6267
connect(m_gistManager, &GistManager::gistPosted, this, &GistPlugin::gistCreated);
6368

64-
createMenu();
65-
createOptionsPage();
69+
initMenus();
70+
initOptionsPage();
6671

6772
return true;
6873
}
@@ -133,7 +138,7 @@ static inline void fixSpecialCharacters(QString &data)
133138
}
134139
}
135140

136-
void GistPlugin::createGist()
141+
void GistPlugin::createGistFromText()
137142
{
138143
QString text;
139144
QString fileName;
@@ -150,50 +155,123 @@ void GistPlugin::createGist()
150155
m_gistManager->postGist(text, fileName, fileName, publicFlag);
151156
}
152157

153-
void GistPlugin::createMenu()
158+
void GistPlugin::createGistFromNode()
154159
{
160+
const Node* node = ProjectTree::currentNode();
161+
if (!node) {
162+
return;
163+
}
164+
165+
QStringList files = nodeFiles(node);
166+
if (!files.isEmpty()) {
167+
m_gistManager->postGist(files, ProjectTree::currentProject()->displayName());
168+
}
169+
}
170+
171+
static inline void addToMenu(Command *cmd, Id id) {
172+
ActionContainer *container = ActionManager::actionContainer(id);
173+
if (container) {
174+
container->addAction(cmd);
175+
}
176+
}
177+
178+
void GistPlugin::initMenus()
179+
{
180+
//! Tools->GitHubGist menu actions
155181
QAction *createPublicAction = new QAction(QIcon(QLatin1String(":/images/gist.png")),
156182
tr("Create gist"), this);
157183
createPublicAction->setProperty("public", true);
184+
connect(createPublicAction, &QAction::triggered, this, &GistPlugin::createGistFromText);
158185
Command *createPublicGistCmd = ActionManager::registerAction(createPublicAction,
159186
Constants::CREATE_PUBLIC_ACTION_ID,
160187
Context(Core::Constants::C_EDIT_MODE));
161188
createPublicGistCmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+G")));
162-
connect(createPublicAction, &QAction::triggered, this, &GistPlugin::createGist);
163189

164190
QAction *createSecretAction = new QAction(QIcon(QLatin1String(":/images/gist-secret.png")),
165191
tr("Create secret gist"), this);
166192
createSecretAction->setProperty("public", false);
193+
connect(createSecretAction, &QAction::triggered, this, &GistPlugin::createGistFromText);
167194
Command *createSecretGistCmd = ActionManager::registerAction(createSecretAction,
168195
Constants::CREATE_SECRET_ACTION_ID,
169196
Context(Core::Constants::C_EDIT_MODE));
170-
connect(createSecretAction, &QAction::triggered, this, &GistPlugin::createGist);
171197

172198
ActionContainer *gistsMenu = ActionManager::createMenu(Constants::GIST_TOOLS_MENU_ID);
173199
gistsMenu->menu()->setTitle(tr("GitHub Gist"));
174200
gistsMenu->addAction(createPublicGistCmd);
175201
gistsMenu->addAction(createSecretGistCmd);
176202
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(gistsMenu);
203+
204+
//! ProjectExplorer context menu actions
205+
QAction *gistFromNode = new QAction(tr("Create Gist"), this);
206+
connect(gistFromNode, &QAction::triggered, this, &GistPlugin::createGistFromNode);
207+
208+
Command *gistFromNodeCmd = ActionManager::registerAction(gistFromNode,
209+
Constants::CREATE_GIST_FROM_NODE,
210+
Context(Core::Constants::C_NAVIGATION_PANE));
211+
212+
addToMenu(gistFromNodeCmd, ProjectExplorer::Constants::M_FILECONTEXT);
213+
addToMenu(gistFromNodeCmd, ProjectExplorer::Constants::M_FOLDERCONTEXT);
214+
addToMenu(gistFromNodeCmd, ProjectExplorer::Constants::M_PROJECTCONTEXT);
215+
addToMenu(gistFromNodeCmd, ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
177216
}
178217

179-
void GistPlugin::createOptionsPage()
218+
void GistPlugin::initOptionsPage()
180219
{
181220
m_optionsPage = new OptionsPage(m_settings, this);
182221
addAutoReleasedObject(m_optionsPage);
183222
}
184223

185-
void GistPlugin::showMessage(const QString &message)
224+
void GistPlugin::showMessage(const QString &message) const
186225
{
187226
MessageManager::write(message);
188227
}
189228

190-
void GistPlugin::gistCreated(const QString &name, const QString &url)
229+
void GistPlugin::gistCreated(const QString &name, const QString &url) const
191230
{
192231
if (m_settings->autoCopyLink) {
193232
QApplication::clipboard()->setText(url);
194233
}
195234
showMessage(tr("Gist \"%1\" posted: ").arg(name) + url);
196235
}
197236

237+
QStringList GistPlugin::nodeFiles(const Node *node) const
238+
{
239+
if (!node) {
240+
return QStringList();
241+
}
242+
243+
QStringList files;
244+
245+
switch (node->nodeType()) {
246+
case FileNodeType: {
247+
const FileNode *file = static_cast<const FileNode *>(node);
248+
if (file) {
249+
files << file->path().toString();
250+
}
251+
break;
252+
}
253+
case ProjectNodeType:
254+
case FolderNodeType:
255+
case VirtualFolderNodeType: {
256+
const FolderNode *folder = static_cast<const FolderNode *>(node);
257+
if (folder) {
258+
for(auto subfolder : folder->subFolderNodes()) {
259+
files.append(nodeFiles(subfolder));
260+
}
261+
for(auto file : folder->fileNodes()) {
262+
//! TODO NOTE: This condition require for create gist from text/plain files only
263+
if (file->fileType() != FileType::UnknownFileType)
264+
files.append(nodeFiles(file));
265+
}
266+
}
267+
break;
268+
}
269+
default:
270+
break;
271+
}
272+
273+
return files;
274+
}
275+
198276
} // namespace Internal
199277
} // namespace GitHubGist

src/gistplugin.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ QT_BEGIN_NAMESPACE
1212
class QAction;
1313
QT_END_NAMESPACE
1414

15+
namespace ProjectExplorer {
16+
class Node;
17+
}
18+
1519
namespace GitHubGist {
1620
namespace Internal {
1721

@@ -31,13 +35,15 @@ class GistPlugin : public ExtensionSystem::IPlugin
3135
void extensionsInitialized();
3236
ShutdownFlag aboutToShutdown();
3337

34-
void createGist();
38+
void createGistFromText();
39+
void createGistFromNode();
3540

3641
private:
37-
void createMenu();
38-
void createOptionsPage();
39-
void showMessage(const QString &message);
40-
void gistCreated(const QString &name, const QString &url);
42+
void initMenus();
43+
void initOptionsPage();
44+
void showMessage(const QString &message) const;
45+
void gistCreated(const QString &name, const QString &url) const;
46+
QStringList nodeFiles(const ProjectExplorer::Node *node) const;
4147

4248
const QSharedPointer<Settings> m_settings;
4349
GistManager *m_gistManager;

src/gistpluginconstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Constants {
88

99
const char CREATE_PUBLIC_ACTION_ID[] = "GistPlugin.CreatePublicGist";
1010
const char CREATE_SECRET_ACTION_ID[] = "GistPlugin.CreateSecretGist";
11+
const char CREATE_GIST_FROM_NODE[] = "GistPlugin.CreateGistFromNode";
1112
const char GIST_TOOLS_MENU_ID[] = "GistPlugin.Menu";
1213

1314
const char GIST_SETTINGS_CATEGORY[] = "GithubGist";

0 commit comments

Comments
 (0)