2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cpasterplugin.h"
|
|
|
|
|
|
2009-07-13 08:32:46 +02:00
|
|
|
#include "pasteview.h"
|
2011-11-07 12:03:03 +01:00
|
|
|
#include "kdepasteprotocol.h"
|
2009-07-13 08:32:46 +02:00
|
|
|
#include "pastebindotcomprotocol.h"
|
2009-07-31 12:13:37 +02:00
|
|
|
#include "pastebindotcaprotocol.h"
|
2010-04-23 15:06:53 +02:00
|
|
|
#include "fileshareprotocol.h"
|
2010-03-26 12:47:06 +01:00
|
|
|
#include "pasteselectdialog.h"
|
2010-03-24 10:43:01 +01:00
|
|
|
#include "settingspage.h"
|
2010-03-26 12:47:06 +01:00
|
|
|
#include "settings.h"
|
2012-05-16 14:51:01 +04:00
|
|
|
#include "urlopenprotocol.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2011-09-05 16:10:37 +02:00
|
|
|
#include <coreplugin/id.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2015-02-04 09:32:46 +01:00
|
|
|
#include <utils/mimetypes/mimedatabase.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QtPlugin>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QClipboard>
|
2012-05-16 14:51:01 +04:00
|
|
|
#include <QInputDialog>
|
|
|
|
|
#include <QUrl>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
namespace CodePaster {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class CodePaster::CodePasterService
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The CodePasterService class is a service registered with PluginManager
|
|
|
|
|
that provides CodePaster \c post() functionality.
|
2011-04-21 15:21:50 +02:00
|
|
|
|
|
|
|
|
\sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke
|
2012-01-07 12:31:48 +01:00
|
|
|
\sa VcsBase::VcsBaseEditorWidget
|
2011-04-21 15:21:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
CodePasterService::CodePasterService(QObject *parent) :
|
|
|
|
|
QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodePasterService::postText(const QString &text, const QString &mimeType)
|
|
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(CodepasterPlugin::instance(), return);
|
2011-04-21 15:21:50 +02:00
|
|
|
CodepasterPlugin::instance()->post(text, mimeType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodePasterService::postCurrentEditor()
|
|
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(CodepasterPlugin::instance(), return);
|
2014-11-13 14:44:35 +01:00
|
|
|
CodepasterPlugin::instance()->post(CodepasterPlugin::PasteEditor);
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodePasterService::postClipboard()
|
|
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(CodepasterPlugin::instance(), return);
|
2014-11-13 14:44:35 +01:00
|
|
|
CodepasterPlugin::instance()->post(CodepasterPlugin::PasteClipboard);
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- CodepasterPlugin
|
|
|
|
|
CodepasterPlugin *CodepasterPlugin::m_instance = 0;
|
|
|
|
|
|
2010-03-30 12:05:24 +02:00
|
|
|
CodepasterPlugin::CodepasterPlugin() :
|
|
|
|
|
m_settings(new Settings),
|
2014-11-13 14:44:35 +01:00
|
|
|
m_postEditorAction(0), m_fetchAction(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-04-21 15:21:50 +02:00
|
|
|
CodepasterPlugin::m_instance = this;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodepasterPlugin::~CodepasterPlugin()
|
|
|
|
|
{
|
2012-06-28 12:16:19 +02:00
|
|
|
delete m_urlOpen;
|
2009-11-04 17:11:35 +01:00
|
|
|
qDeleteAll(m_protocols);
|
2011-04-21 15:21:50 +02:00
|
|
|
CodepasterPlugin::m_instance = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-09-21 13:05:15 +02:00
|
|
|
bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-07-13 17:35:17 +02:00
|
|
|
Q_UNUSED(arguments)
|
2011-09-21 13:05:15 +02:00
|
|
|
Q_UNUSED(errorMessage)
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Create the settings Page
|
2015-02-03 23:56:02 +02:00
|
|
|
m_settings->fromSettings(ICore::settings());
|
2010-03-26 12:47:06 +01:00
|
|
|
SettingsPage *settingsPage = new SettingsPage(m_settings);
|
|
|
|
|
addAutoReleasedObject(settingsPage);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-07-13 08:32:46 +02:00
|
|
|
// Create the protocols and append them to the Settings
|
2013-04-09 09:58:17 +02:00
|
|
|
Protocol *protos[] = { new PasteBinDotComProtocol,
|
|
|
|
|
new PasteBinDotCaProtocol,
|
|
|
|
|
new KdePasteProtocol,
|
2010-04-23 15:06:53 +02:00
|
|
|
new FileShareProtocol
|
2010-04-06 14:40:33 +02:00
|
|
|
};
|
|
|
|
|
const int count = sizeof(protos) / sizeof(Protocol *);
|
2012-11-28 20:44:03 +02:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2009-07-13 08:32:46 +02:00
|
|
|
connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
|
2009-07-31 11:17:57 +02:00
|
|
|
connect(protos[i], SIGNAL(fetchDone(QString,QString,bool)),
|
|
|
|
|
this, SLOT(finishFetch(QString,QString,bool)));
|
2010-03-26 12:47:06 +01:00
|
|
|
settingsPage->addProtocol(protos[i]->name());
|
2009-07-13 08:32:46 +02:00
|
|
|
if (protos[i]->hasSettings())
|
2009-07-31 17:11:42 +02:00
|
|
|
addAutoReleasedObject(protos[i]->settingsPage());
|
2009-07-13 08:32:46 +02:00
|
|
|
m_protocols.append(protos[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 09:58:17 +02:00
|
|
|
m_urlOpen = new UrlOpenProtocol;
|
2015-01-29 18:37:56 +01:00
|
|
|
connect(m_urlOpen, &Protocol::fetchDone, this, &CodepasterPlugin::finishFetch);
|
2012-05-16 14:51:01 +04:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
//register actions
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
ActionContainer *toolsContainer =
|
|
|
|
|
ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
ActionContainer *cpContainer =
|
|
|
|
|
ActionManager::createMenu("CodePaster");
|
2009-10-15 16:25:28 +02:00
|
|
|
cpContainer->menu()->setTitle(tr("&Code Pasting"));
|
2008-12-02 12:01:29 +01:00
|
|
|
toolsContainer->addMenu(cpContainer);
|
|
|
|
|
|
2015-02-03 23:56:02 +02:00
|
|
|
Command *command;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-03-30 12:05:24 +02:00
|
|
|
m_postEditorAction = new QAction(tr("Paste Snippet..."), this);
|
2015-02-19 11:35:47 +01:00
|
|
|
command = ActionManager::registerAction(m_postEditorAction, "CodePaster.Post");
|
2012-05-23 13:25:51 +02:00
|
|
|
command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+C,Meta+P") : tr("Alt+C,Alt+P")));
|
2014-11-13 14:44:35 +01:00
|
|
|
connect(m_postEditorAction, &QAction::triggered, this, &CodepasterPlugin::pasteSnippet);
|
2008-12-02 12:01:29 +01:00
|
|
|
cpContainer->addAction(command);
|
|
|
|
|
|
2009-02-20 17:22:16 +01:00
|
|
|
m_fetchAction = new QAction(tr("Fetch Snippet..."), this);
|
2015-02-19 11:35:47 +01:00
|
|
|
command = ActionManager::registerAction(m_fetchAction, "CodePaster.Fetch");
|
2012-05-23 13:25:51 +02:00
|
|
|
command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+C,Meta+F") : tr("Alt+C,Alt+F")));
|
2015-01-29 18:37:56 +01:00
|
|
|
connect(m_fetchAction, &QAction::triggered, this, &CodepasterPlugin::fetch);
|
2008-12-02 12:01:29 +01:00
|
|
|
cpContainer->addAction(command);
|
|
|
|
|
|
2012-05-16 14:51:01 +04:00
|
|
|
m_fetchUrlAction = new QAction(tr("Fetch from URL..."), this);
|
2015-02-19 11:35:47 +01:00
|
|
|
command = ActionManager::registerAction(m_fetchUrlAction, "CodePaster.FetchUrl");
|
2015-01-29 18:37:56 +01:00
|
|
|
connect(m_fetchUrlAction, &QAction::triggered, this, &CodepasterPlugin::fetchUrl);
|
2012-05-16 14:51:01 +04:00
|
|
|
cpContainer->addAction(command);
|
|
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
addAutoReleasedObject(new CodePasterService);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodepasterPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 13:36:47 +02:00
|
|
|
ExtensionSystem::IPlugin::ShutdownFlag CodepasterPlugin::aboutToShutdown()
|
2010-03-25 16:23:37 +01:00
|
|
|
{
|
|
|
|
|
// Delete temporary, fetched files
|
2012-11-28 20:44:03 +02:00
|
|
|
foreach (const QString &fetchedSnippet, m_fetchedSnippets) {
|
2010-03-25 16:23:37 +01:00
|
|
|
QFile file(fetchedSnippet);
|
|
|
|
|
if (file.exists())
|
|
|
|
|
file.remove();
|
|
|
|
|
}
|
2010-07-13 13:36:47 +02:00
|
|
|
return SynchronousShutdown;
|
2010-03-25 16:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-13 14:44:35 +01:00
|
|
|
static inline void textFromCurrentEditor(QString *text, QString *mimeType)
|
2010-03-30 12:05:24 +02:00
|
|
|
{
|
2014-07-02 15:00:00 +02:00
|
|
|
IEditor *editor = EditorManager::currentEditor();
|
|
|
|
|
if (!editor)
|
|
|
|
|
return;
|
|
|
|
|
const IDocument *document = editor->document();
|
2011-03-16 10:24:30 +01:00
|
|
|
QString data;
|
2014-07-23 19:10:38 +02:00
|
|
|
if (const BaseTextEditor *textEditor = qobject_cast<const BaseTextEditor *>(editor))
|
2014-07-02 15:00:00 +02:00
|
|
|
data = textEditor->selectedText();
|
|
|
|
|
if (data.isEmpty()) {
|
2014-11-13 09:11:56 +01:00
|
|
|
if (auto textDocument = qobject_cast<const TextDocument *>(document)) {
|
2014-07-02 15:00:00 +02:00
|
|
|
data = textDocument->plainText();
|
2014-11-13 09:11:56 +01:00
|
|
|
} else {
|
|
|
|
|
const QVariant textV = document->property("plainText"); // Diff Editor.
|
|
|
|
|
if (textV.type() == QVariant::String)
|
|
|
|
|
data = textV.toString();
|
|
|
|
|
}
|
2011-03-16 10:24:30 +01:00
|
|
|
}
|
2014-11-13 14:44:35 +01:00
|
|
|
if (!data.isEmpty()) {
|
|
|
|
|
*text = data;
|
|
|
|
|
*mimeType = document->mimeType();
|
|
|
|
|
}
|
2010-03-30 12:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
2010-03-29 14:54:25 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-13 14:44:35 +01:00
|
|
|
void CodepasterPlugin::post(PasteSources pasteSources)
|
|
|
|
|
{
|
|
|
|
|
QString data;
|
|
|
|
|
QString mimeType;
|
|
|
|
|
if (pasteSources & PasteEditor)
|
|
|
|
|
textFromCurrentEditor(&data, &mimeType);
|
|
|
|
|
if (data.isEmpty() && (pasteSources & PasteClipboard)) {
|
|
|
|
|
QString subType = QStringLiteral("plain");
|
|
|
|
|
data = qApp->clipboard()->text(subType, QClipboard::Clipboard);
|
|
|
|
|
}
|
|
|
|
|
post(data, mimeType);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 12:05:24 +02:00
|
|
|
void CodepasterPlugin::post(QString data, const QString &mimeType)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-03-29 14:54:25 +02:00
|
|
|
fixSpecialCharacters(data);
|
2011-02-28 15:41:42 +01:00
|
|
|
|
|
|
|
|
const QString username = m_settings->username;
|
2009-07-13 08:32:46 +02:00
|
|
|
|
2014-02-28 09:00:24 +01:00
|
|
|
PasteView view(m_protocols, mimeType, ICore::dialogParent());
|
2010-03-26 17:42:17 +01:00
|
|
|
view.setProtocol(m_settings->protocol);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-01-27 15:26:46 +01:00
|
|
|
const FileDataList diffChunks = splitDiffToFiles(data);
|
2011-04-21 15:21:50 +02:00
|
|
|
const int dialogResult = diffChunks.isEmpty() ?
|
2013-04-04 17:20:15 +02:00
|
|
|
view.show(username, QString(), QString(), m_settings->expiryDays, data) :
|
|
|
|
|
view.show(username, QString(), QString(), m_settings->expiryDays, diffChunks);
|
2011-04-21 15:21:50 +02:00
|
|
|
// Save new protocol in case user changed it.
|
|
|
|
|
if (dialogResult == QDialog::Accepted
|
|
|
|
|
&& m_settings->protocol != view.protocol()) {
|
|
|
|
|
m_settings->protocol = view.protocol();
|
2015-02-03 23:56:02 +02:00
|
|
|
m_settings->toSettings(ICore::settings());
|
2011-02-28 15:41:42 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-16 14:51:01 +04:00
|
|
|
void CodepasterPlugin::fetchUrl()
|
|
|
|
|
{
|
|
|
|
|
QUrl url;
|
|
|
|
|
do {
|
|
|
|
|
bool ok = true;
|
2014-02-28 09:00:24 +01:00
|
|
|
url = QUrl(QInputDialog::getText(ICore::dialogParent(), tr("Fetch from URL"), tr("Enter URL:"), QLineEdit::Normal, QString(), &ok));
|
2012-05-16 14:51:01 +04:00
|
|
|
if (!ok)
|
|
|
|
|
return;
|
|
|
|
|
} while (!url.isValid());
|
|
|
|
|
m_urlOpen->fetch(url.toString());
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-13 14:44:35 +01:00
|
|
|
void CodepasterPlugin::pasteSnippet()
|
|
|
|
|
{
|
|
|
|
|
post(PasteEditor | PasteClipboard);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void CodepasterPlugin::fetch()
|
|
|
|
|
{
|
2014-02-28 09:00:24 +01:00
|
|
|
PasteSelectDialog dialog(m_protocols, ICore::dialogParent());
|
2010-03-26 12:47:06 +01:00
|
|
|
dialog.setProtocol(m_settings->protocol);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-03-26 12:47:06 +01:00
|
|
|
if (dialog.exec() != QDialog::Accepted)
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
2011-04-21 15:21:50 +02:00
|
|
|
// Save new protocol in case user changed it.
|
|
|
|
|
if (m_settings->protocol != dialog.protocol()) {
|
|
|
|
|
m_settings->protocol = dialog.protocol();
|
2015-02-03 23:56:02 +02:00
|
|
|
m_settings->toSettings(ICore::settings());
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-03-26 12:47:06 +01:00
|
|
|
const QString pasteID = dialog.pasteId();
|
|
|
|
|
if (pasteID.isEmpty())
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
2010-04-23 15:06:53 +02:00
|
|
|
Protocol *protocol = m_protocols[dialog.protocolIndex()];
|
|
|
|
|
if (Protocol::ensureConfiguration(protocol))
|
|
|
|
|
protocol->fetch(pasteID);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-13 08:32:46 +02:00
|
|
|
void CodepasterPlugin::finishPost(const QString &link)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-03-26 12:47:06 +01:00
|
|
|
if (m_settings->copyToClipboard)
|
2009-07-13 08:32:46 +02:00
|
|
|
QApplication::clipboard()->setText(link);
|
2015-02-03 23:56:02 +02:00
|
|
|
MessageManager::write(link, m_settings->displayOutput ? MessageManager::ModeSwitch : MessageManager::Silent);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-25 16:23:37 +01:00
|
|
|
// 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));
|
2010-03-26 10:37:36 +01:00
|
|
|
if (rc.isEmpty()) {
|
2010-03-25 16:23:37 +01:00
|
|
|
rc = QLatin1String("qtcreator");
|
2010-03-26 10:37:36 +01:00
|
|
|
} else {
|
|
|
|
|
if (rc.size() > 15)
|
|
|
|
|
rc.truncate(15);
|
|
|
|
|
}
|
2010-03-25 16:23:37 +01:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a temp file pattern with extension or not
|
2011-03-09 12:17:56 +01:00
|
|
|
static inline QString tempFilePattern(const QString &prefix, const QString &extension)
|
2010-03-25 16:23:37 +01:00
|
|
|
{
|
|
|
|
|
// Get directory
|
|
|
|
|
QString pattern = QDir::tempPath();
|
2015-01-31 22:11:11 +02:00
|
|
|
const QChar slash = QLatin1Char('/');
|
|
|
|
|
if (!pattern.endsWith(slash))
|
|
|
|
|
pattern.append(slash);
|
2010-03-25 16:23:37 +01:00
|
|
|
// Prefix, placeholder, extension
|
|
|
|
|
pattern += prefix;
|
2011-03-09 12:17:56 +01:00
|
|
|
pattern += QLatin1String("_XXXXXX.");
|
|
|
|
|
pattern += extension;
|
2010-03-25 16:23:37 +01:00
|
|
|
return pattern;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-31 11:17:57 +02:00
|
|
|
void CodepasterPlugin::finishFetch(const QString &titleDescription,
|
|
|
|
|
const QString &content,
|
|
|
|
|
bool error)
|
|
|
|
|
{
|
2010-03-25 16:23:37 +01:00
|
|
|
// Failure?
|
2009-07-31 11:17:57 +02:00
|
|
|
if (error) {
|
2013-09-05 01:52:17 +02:00
|
|
|
MessageManager::write(content);
|
2010-03-25 16:23:37 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2010-03-26 10:37:36 +01:00
|
|
|
if (content.isEmpty()) {
|
2013-09-05 01:52:17 +02:00
|
|
|
MessageManager::write(tr("Empty snippet received for \"%1\".").arg(titleDescription));
|
2010-03-26 10:37:36 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2010-03-25 16:23:37 +01:00
|
|
|
// 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
|
2012-02-14 16:43:51 +01:00
|
|
|
// by looking at the file name of DocumentManager::currentFile() without expensive checking.
|
2010-03-26 10:37:36 +01:00
|
|
|
// Default to "txt".
|
2011-03-30 15:15:15 +02:00
|
|
|
QByteArray byteContent = content.toUtf8();
|
2010-03-26 10:37:36 +01:00
|
|
|
QString suffix;
|
2015-02-04 09:32:46 +01:00
|
|
|
Utils::MimeDatabase mdb;
|
|
|
|
|
const Utils::MimeType mimeType = mdb.mimeTypeForData(byteContent);
|
|
|
|
|
if (mimeType.isValid())
|
2010-03-26 10:37:36 +01:00
|
|
|
suffix = mimeType.preferredSuffix();
|
|
|
|
|
if (suffix.isEmpty())
|
|
|
|
|
suffix = QLatin1String("txt");
|
2011-03-09 12:17:56 +01:00
|
|
|
const QString filePrefix = filePrefixFromTitle(titleDescription);
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::TempFileSaver saver(tempFilePattern(filePrefix, suffix));
|
|
|
|
|
saver.setAutoRemove(false);
|
|
|
|
|
saver.write(byteContent);
|
|
|
|
|
if (!saver.finalize()) {
|
2013-09-05 01:52:17 +02:00
|
|
|
MessageManager::write(saver.errorString());
|
2010-03-26 10:37:36 +01:00
|
|
|
return;
|
2009-07-31 11:17:57 +02:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
const QString fileName = saver.fileName();
|
2010-03-25 16:23:37 +01:00
|
|
|
m_fetchedSnippets.push_back(fileName);
|
|
|
|
|
// Open editor with title.
|
2015-02-03 23:56:02 +02:00
|
|
|
IEditor *editor = EditorManager::openEditor(fileName);
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(editor, return);
|
2013-07-04 22:25:15 +02:00
|
|
|
editor->document()->setDisplayName(titleDescription);
|
2009-07-31 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
CodepasterPlugin *CodepasterPlugin::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace CodePaster
|