cpasterplugin.cpp 13.2 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
23
24
25
** 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.
**
** 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
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "cpasterplugin.h"

32
#include "pasteview.h"
33
#include "kdepasteprotocol.h"
34
#include "pastebindotcomprotocol.h"
35
#include "pastebindotcaprotocol.h"
36
#include "fileshareprotocol.h"
37
#include "pasteselectdialog.h"
38
#include "settingspage.h"
39
#include "settings.h"
Konstantin Tokarev's avatar
Konstantin Tokarev committed
40
#include "urlopenprotocol.h"
con's avatar
con committed
41

42
#include <coreplugin/actionmanager/actionmanager.h>
43
44
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
45
#include <coreplugin/id.h>
con's avatar
con committed
46
#include <coreplugin/coreconstants.h>
47
#include <coreplugin/editormanager/editormanager.h>
48
#include <coreplugin/mimedatabase.h>
49
#include <coreplugin/icore.h>
con's avatar
con committed
50
#include <coreplugin/messagemanager.h>
51
#include <utils/qtcassert.h>
52
#include <utils/fileutils.h>
53
#include <texteditor/basetexteditor.h>
con's avatar
con committed
54

55
56
57
58
59
60
#include <QtPlugin>
#include <QDebug>
#include <QDir>
#include <QAction>
#include <QApplication>
#include <QClipboard>
Konstantin Tokarev's avatar
Konstantin Tokarev committed
61
62
#include <QInputDialog>
#include <QUrl>
con's avatar
con committed
63
64
65
66

using namespace Core;
using namespace TextEditor;

67
68
69
70
namespace CodePaster {

/*!
   \class CodePaster::CodePasterService
71
72
   \brief The CodePasterService class is a service registered with PluginManager
   that provides CodePaster \c post() functionality.
73
74

   \sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke
hjk's avatar
hjk committed
75
   \sa VcsBase::VcsBaseEditorWidget
76
77
78
79
80
81
82
83
84
*/

CodePasterService::CodePasterService(QObject *parent) :
    QObject(parent)
{
}

void CodePasterService::postText(const QString &text, const QString &mimeType)
{
85
    QTC_ASSERT(CodepasterPlugin::instance(), return);
86
87
88
89
90
    CodepasterPlugin::instance()->post(text, mimeType);
}

void CodePasterService::postCurrentEditor()
{
91
    QTC_ASSERT(CodepasterPlugin::instance(), return);
92
93
94
95
96
    CodepasterPlugin::instance()->postEditor();
}

void CodePasterService::postClipboard()
{
97
    QTC_ASSERT(CodepasterPlugin::instance(), return);
98
99
100
101
102
103
    CodepasterPlugin::instance()->postClipboard();
}

// ---------- CodepasterPlugin
CodepasterPlugin *CodepasterPlugin::m_instance = 0;

104
105
106
CodepasterPlugin::CodepasterPlugin() :
    m_settings(new Settings),
    m_postEditorAction(0), m_postClipboardAction(0), m_fetchAction(0)
con's avatar
con committed
107
{
108
    CodepasterPlugin::m_instance = this;
con's avatar
con committed
109
110
111
112
}

CodepasterPlugin::~CodepasterPlugin()
{
113
    delete m_urlOpen;
114
    qDeleteAll(m_protocols);
115
    CodepasterPlugin::m_instance = 0;
con's avatar
con committed
116
117
}

hjk's avatar
hjk committed
118
bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMessage)
con's avatar
con committed
119
{
120
    Q_UNUSED(arguments)
hjk's avatar
hjk committed
121
    Q_UNUSED(errorMessage)
con's avatar
con committed
122
123

    // Create the globalcontext list to register actions accordingly
124
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
con's avatar
con committed
125
126

    // Create the settings Page
hjk's avatar
hjk committed
127
    m_settings->fromSettings(Core::ICore::settings());
128
129
    SettingsPage *settingsPage = new SettingsPage(m_settings);
    addAutoReleasedObject(settingsPage);
con's avatar
con committed
130

131
    // Create the protocols and append them to the Settings
132
133
134
    Protocol *protos[] =  { new PasteBinDotComProtocol,
                            new PasteBinDotCaProtocol,
                            new KdePasteProtocol,
135
                            new FileShareProtocol
136
137
                           };
    const int count = sizeof(protos) / sizeof(Protocol *);
138
    for (int i = 0; i < count; ++i) {
139
        connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
140
141
        connect(protos[i], SIGNAL(fetchDone(QString,QString,bool)),
                this, SLOT(finishFetch(QString,QString,bool)));
142
        settingsPage->addProtocol(protos[i]->name());
143
        if (protos[i]->hasSettings())
144
            addAutoReleasedObject(protos[i]->settingsPage());
145
146
147
        m_protocols.append(protos[i]);
    }

148
    m_urlOpen = new UrlOpenProtocol;
Konstantin Tokarev's avatar
Konstantin Tokarev committed
149
150
151
    connect(m_urlOpen, SIGNAL(fetchDone(QString,QString,bool)),
            this, SLOT(finishFetch(QString,QString,bool)));

con's avatar
con committed
152
153
    //register actions

154
    Core::ActionContainer *toolsContainer =
Eike Ziller's avatar
Eike Ziller committed
155
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
156

157
    Core::ActionContainer *cpContainer =
158
        Core::ActionManager::createMenu("CodePaster");
159
    cpContainer->menu()->setTitle(tr("&Code Pasting"));
con's avatar
con committed
160
161
    toolsContainer->addMenu(cpContainer);

con's avatar
con committed
162
    Core::Command *command;
con's avatar
con committed
163

164
    m_postEditorAction = new QAction(tr("Paste Snippet..."), this);
Eike Ziller's avatar
Eike Ziller committed
165
    command = Core::ActionManager::registerAction(m_postEditorAction, "CodePaster.Post", globalcontext);
166
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+C,Meta+P") : tr("Alt+C,Alt+P")));
167
168
169
170
    connect(m_postEditorAction, SIGNAL(triggered()), this, SLOT(postEditor()));
    cpContainer->addAction(command);

    m_postClipboardAction = new QAction(tr("Paste Clipboard..."), this);
Eike Ziller's avatar
Eike Ziller committed
171
    command = Core::ActionManager::registerAction(m_postClipboardAction, "CodePaster.PostClipboard", globalcontext);
172
    connect(m_postClipboardAction, SIGNAL(triggered()), this, SLOT(postClipboard()));
con's avatar
con committed
173
174
    cpContainer->addAction(command);

175
    m_fetchAction = new QAction(tr("Fetch Snippet..."), this);
Eike Ziller's avatar
Eike Ziller committed
176
    command = Core::ActionManager::registerAction(m_fetchAction, "CodePaster.Fetch", globalcontext);
177
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+C,Meta+F") : tr("Alt+C,Alt+F")));
con's avatar
con committed
178
179
180
    connect(m_fetchAction, SIGNAL(triggered()), this, SLOT(fetch()));
    cpContainer->addAction(command);

Konstantin Tokarev's avatar
Konstantin Tokarev committed
181
    m_fetchUrlAction = new QAction(tr("Fetch from URL..."), this);
Eike Ziller's avatar
Eike Ziller committed
182
    command = Core::ActionManager::registerAction(m_fetchUrlAction, "CodePaster.FetchUrl", globalcontext);
Konstantin Tokarev's avatar
Konstantin Tokarev committed
183
184
185
    connect(m_fetchUrlAction, SIGNAL(triggered()), this, SLOT(fetchUrl()));
    cpContainer->addAction(command);

186
187
    addAutoReleasedObject(new CodePasterService);

con's avatar
con committed
188
189
190
191
192
193
194
    return true;
}

void CodepasterPlugin::extensionsInitialized()
{
}

195
ExtensionSystem::IPlugin::ShutdownFlag CodepasterPlugin::aboutToShutdown()
196
197
{
    // Delete temporary, fetched files
198
    foreach (const QString &fetchedSnippet, m_fetchedSnippets) {
199
200
201
202
        QFile file(fetchedSnippet);
        if (file.exists())
            file.remove();
    }
203
    return SynchronousShutdown;
204
205
}

206
207
void CodepasterPlugin::postEditor()
{
208
209
210
211
212
    IEditor *editor = EditorManager::currentEditor();
    if (!editor)
        return;
    const IDocument *document = editor->document();
    const QString mimeType = document->mimeType();
213
    QString data;
214
    if (const BaseTextEditor *textEditor = qobject_cast<const BaseTextEditor *>(editor))
215
216
        data = textEditor->selectedText();
    if (data.isEmpty()) {
217
        if (auto textDocument = qobject_cast<const BaseTextDocument *>(document))
218
            data = textDocument->plainText();
219
220
    }
    post(data, mimeType);
221
222
223
224
225
226
227
228
229
230
}

void CodepasterPlugin::postClipboard()
{
    QString subtype = QLatin1String("plain");
    const QString text = qApp->clipboard()->text(subtype, QClipboard::Clipboard);
    if (!text.isEmpty())
        post(text, QString());
}

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
static inline void fixSpecialCharacters(QString &data)
{
    QChar *uc = data.data();
    QChar *e = uc + data.size();

    for (; uc != e; ++uc) {
        switch (uc->unicode()) {
        case 0xfdd0: // QTextBeginningOfFrame
        case 0xfdd1: // QTextEndOfFrame
        case QChar::ParagraphSeparator:
        case QChar::LineSeparator:
            *uc = QLatin1Char('\n');
            break;
        case QChar::Nbsp:
            *uc = QLatin1Char(' ');
            break;
        default:
            break;
        }
    }
}

253
void CodepasterPlugin::post(QString data, const QString &mimeType)
con's avatar
con committed
254
{
255
    fixSpecialCharacters(data);
256
257

    const QString username = m_settings->username;
258

259
    PasteView view(m_protocols, mimeType, ICore::dialogParent());
260
    view.setProtocol(m_settings->protocol);
con's avatar
con committed
261

262
    const FileDataList diffChunks = splitDiffToFiles(data);
263
    const int dialogResult = diffChunks.isEmpty() ?
264
265
        view.show(username, QString(), QString(), m_settings->expiryDays, data) :
        view.show(username, QString(), QString(), m_settings->expiryDays, diffChunks);
266
267
268
269
    // Save new protocol in case user changed it.
    if (dialogResult == QDialog::Accepted
        && m_settings->protocol != view.protocol()) {
        m_settings->protocol = view.protocol();
hjk's avatar
hjk committed
270
        m_settings->toSettings(Core::ICore::settings());
271
    }
con's avatar
con committed
272
273
}

Konstantin Tokarev's avatar
Konstantin Tokarev committed
274
275
276
277
278
void CodepasterPlugin::fetchUrl()
{
    QUrl url;
    do {
        bool ok = true;
279
        url = QUrl(QInputDialog::getText(ICore::dialogParent(), tr("Fetch from URL"), tr("Enter URL:"), QLineEdit::Normal, QString(), &ok));
Konstantin Tokarev's avatar
Konstantin Tokarev committed
280
281
282
283
284
285
        if (!ok)
            return;
    } while (!url.isValid());
    m_urlOpen->fetch(url.toString());
}

con's avatar
con committed
286
287
void CodepasterPlugin::fetch()
{
288
    PasteSelectDialog dialog(m_protocols, ICore::dialogParent());
289
    dialog.setProtocol(m_settings->protocol);
con's avatar
con committed
290

291
    if (dialog.exec() != QDialog::Accepted)
con's avatar
con committed
292
        return;
293
294
295
    // Save new protocol in case user changed it.
    if (m_settings->protocol != dialog.protocol()) {
        m_settings->protocol = dialog.protocol();
hjk's avatar
hjk committed
296
        m_settings->toSettings(Core::ICore::settings());
297
298
    }

299
300
    const QString pasteID = dialog.pasteId();
    if (pasteID.isEmpty())
con's avatar
con committed
301
        return;
302
303
304
    Protocol *protocol = m_protocols[dialog.protocolIndex()];
    if (Protocol::ensureConfiguration(protocol))
        protocol->fetch(pasteID);
con's avatar
con committed
305
306
}

307
void CodepasterPlugin::finishPost(const QString &link)
con's avatar
con committed
308
{
309
    if (m_settings->copyToClipboard)
310
        QApplication::clipboard()->setText(link);
hjk's avatar
hjk committed
311
    MessageManager::write(link, m_settings->displayOutput ? Core::MessageManager::ModeSwitch : Core::MessageManager::Silent);
con's avatar
con committed
312
313
}

314
315
316
317
318
319
320
321
322
323
// Extract the characters that can be used for a file name from a title
// "CodePaster.com-34" -> "CodePastercom34".
static inline QString filePrefixFromTitle(const QString &title)
{
    QString rc;
    const int titleSize = title.size();
    rc.reserve(titleSize);
    for (int i = 0; i < titleSize; i++)
        if (title.at(i).isLetterOrNumber())
            rc.append(title.at(i));
324
    if (rc.isEmpty()) {
325
        rc = QLatin1String("qtcreator");
326
327
328
329
    } else {
        if (rc.size() > 15)
            rc.truncate(15);
    }
330
331
332
333
    return rc;
}

// Return a temp file pattern with extension or not
334
static inline QString tempFilePattern(const QString &prefix, const QString &extension)
335
336
337
338
339
340
341
{
    // Get directory
    QString pattern = QDir::tempPath();
    if (!pattern.endsWith(QDir::separator()))
        pattern.append(QDir::separator());
    // Prefix, placeholder, extension
    pattern += prefix;
342
343
    pattern += QLatin1String("_XXXXXX.");
    pattern += extension;
344
345
346
    return pattern;
}

347
348
349
350
void CodepasterPlugin::finishFetch(const QString &titleDescription,
                                   const QString &content,
                                   bool error)
{
351
    // Failure?
352
    if (error) {
hjk's avatar
hjk committed
353
        MessageManager::write(content);
354
355
        return;
    }
356
    if (content.isEmpty()) {
hjk's avatar
hjk committed
357
        MessageManager::write(tr("Empty snippet received for \"%1\".").arg(titleDescription));
358
359
        return;
    }
360
361
362
    // If the mime type has a preferred suffix (cpp/h/patch...), use that for
    // the temporary file. This is to make it more convenient to "Save as"
    // for the user and also to be able to tell a patch or diff in the VCS plugins
363
    // by looking at the file name of DocumentManager::currentFile() without expensive checking.
364
    // Default to "txt".
365
    QByteArray byteContent = content.toUtf8();
366
    QString suffix;
hjk's avatar
hjk committed
367
    if (const MimeType mimeType = MimeDatabase::findByData(byteContent))
368
369
370
        suffix = mimeType.preferredSuffix();
    if (suffix.isEmpty())
         suffix = QLatin1String("txt");
371
    const QString filePrefix = filePrefixFromTitle(titleDescription);
372
373
374
375
    Utils::TempFileSaver saver(tempFilePattern(filePrefix, suffix));
    saver.setAutoRemove(false);
    saver.write(byteContent);
    if (!saver.finalize()) {
hjk's avatar
hjk committed
376
        MessageManager::write(saver.errorString());
377
        return;
378
    }
379
    const QString fileName = saver.fileName();
380
381
    m_fetchedSnippets.push_back(fileName);
    // Open editor with title.
Eike Ziller's avatar
Eike Ziller committed
382
    Core::IEditor *editor = EditorManager::openEditor(fileName);
383
    QTC_ASSERT(editor, return);
384
    editor->document()->setDisplayName(titleDescription);
385
386
}

387
388
389
390
391
392
393
394
CodepasterPlugin *CodepasterPlugin::instance()
{
    return m_instance;
}

} // namespace CodePaster

Q_EXPORT_PLUGIN(CodePaster::CodepasterPlugin)