2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
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"
|
|
|
|
|
|
2020-05-18 12:00:50 +02:00
|
|
|
#include "dpastedotcomprotocol.h"
|
2010-04-23 15:06:53 +02:00
|
|
|
#include "fileshareprotocol.h"
|
2020-05-18 12:00:50 +02:00
|
|
|
#include "pastebindotcomprotocol.h"
|
2010-03-26 12:47:06 +01:00
|
|
|
#include "pasteselectdialog.h"
|
2020-05-18 12:00:50 +02:00
|
|
|
#include "pasteview.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>
|
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>
|
2020-01-24 18:34:57 +01:00
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
2015-02-04 09:32:46 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2017-01-19 16:44:22 +01:00
|
|
|
#include <utils/temporarydirectory.h>
|
2020-01-24 18:34:57 +01:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <texteditor/textdocument.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QClipboard>
|
2012-05-16 14:51:01 +04:00
|
|
|
#include <QInputDialog>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QMenu>
|
2012-05-16 14:51:01 +04:00
|
|
|
#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 {
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
class CodePasterPluginPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_DECLARE_TR_FUNCTIONS(CodePaster::CodepasterPlugin)
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
CodePasterPluginPrivate();
|
|
|
|
|
|
|
|
|
|
void post(CodePasterPlugin::PasteSources pasteSources);
|
|
|
|
|
void post(QString data, const QString &mimeType);
|
|
|
|
|
|
|
|
|
|
void pasteSnippet();
|
|
|
|
|
void fetch();
|
|
|
|
|
void finishPost(const QString &link);
|
|
|
|
|
void finishFetch(const QString &titleDescription,
|
|
|
|
|
const QString &content,
|
|
|
|
|
bool error);
|
|
|
|
|
|
|
|
|
|
void fetchUrl();
|
|
|
|
|
|
|
|
|
|
Settings m_settings;
|
|
|
|
|
QAction *m_postEditorAction = nullptr;
|
|
|
|
|
QAction *m_fetchAction = nullptr;
|
|
|
|
|
QAction *m_fetchUrlAction = nullptr;
|
|
|
|
|
|
|
|
|
|
PasteBinDotComProtocol pasteBinProto;
|
|
|
|
|
FileShareProtocol fileShareProto;
|
2020-05-18 12:00:50 +02:00
|
|
|
DPasteDotComProtocol dpasteProto;
|
2020-01-24 18:34:57 +01:00
|
|
|
|
|
|
|
|
const QList<Protocol *> m_protocols {
|
|
|
|
|
&pasteBinProto,
|
|
|
|
|
&fileShareProto,
|
2020-05-18 12:00:50 +02:00
|
|
|
&dpasteProto
|
2020-01-24 18:34:57 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-22 17:01:23 +01:00
|
|
|
SettingsPage m_settingsPage{&m_settings};
|
2020-01-24 18:34:57 +01:00
|
|
|
|
|
|
|
|
QStringList m_fetchedSnippets;
|
|
|
|
|
|
|
|
|
|
UrlOpenProtocol m_urlOpen;
|
|
|
|
|
|
|
|
|
|
CodePasterServiceImpl m_service{this};
|
|
|
|
|
};
|
|
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
/*!
|
2015-06-29 14:22:17 +02:00
|
|
|
\class CodePaster::Service
|
|
|
|
|
\brief The CodePaster::Service class is a service registered with PluginManager
|
2013-06-05 14:29:24 +02:00
|
|
|
that provides CodePaster \c post() functionality.
|
2011-04-21 15:21:50 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
CodePasterServiceImpl::CodePasterServiceImpl(CodePasterPluginPrivate *d)
|
|
|
|
|
: d(d)
|
|
|
|
|
{}
|
2011-04-21 15:21:50 +02:00
|
|
|
|
2015-06-29 14:22:17 +02:00
|
|
|
void CodePasterServiceImpl::postText(const QString &text, const QString &mimeType)
|
2011-04-21 15:21:50 +02:00
|
|
|
{
|
2020-01-24 18:34:57 +01:00
|
|
|
d->post(text, mimeType);
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-29 14:22:17 +02:00
|
|
|
void CodePasterServiceImpl::postCurrentEditor()
|
2011-04-21 15:21:50 +02:00
|
|
|
{
|
2020-01-24 18:34:57 +01:00
|
|
|
d->post(CodePasterPlugin::PasteEditor);
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-29 14:22:17 +02:00
|
|
|
void CodePasterServiceImpl::postClipboard()
|
2011-04-21 15:21:50 +02:00
|
|
|
{
|
2020-01-24 18:34:57 +01:00
|
|
|
d->post(CodePasterPlugin::PasteClipboard);
|
2011-04-21 15:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- CodepasterPlugin
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
CodePasterPlugin::~CodePasterPlugin()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2020-01-24 18:34:57 +01:00
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01: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
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
d = new CodePasterPluginPrivate;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodePasterPluginPrivate::CodePasterPluginPrivate()
|
|
|
|
|
{
|
|
|
|
|
// Connect protocols
|
2021-03-22 17:01:23 +01:00
|
|
|
if (!m_protocols.isEmpty()) {
|
|
|
|
|
for (Protocol *proto : m_protocols) {
|
|
|
|
|
m_settings.protocols.addOption(proto->name());
|
|
|
|
|
connect(proto, &Protocol::pasteDone, this, &CodePasterPluginPrivate::finishPost);
|
|
|
|
|
connect(proto, &Protocol::fetchDone, this, &CodePasterPluginPrivate::finishFetch);
|
|
|
|
|
}
|
|
|
|
|
m_settings.protocols.setDefaultValue(m_protocols.at(0)->name());
|
2020-01-24 18:34:57 +01:00
|
|
|
}
|
2020-01-16 12:46:45 +01:00
|
|
|
|
2021-03-22 17:01:23 +01:00
|
|
|
// Create the settings Page
|
|
|
|
|
m_settings.readSettings(ICore::settings());
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
connect(&m_urlOpen, &Protocol::fetchDone, this, &CodePasterPluginPrivate::finishFetch);
|
2012-05-16 14:51:01 +04:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
//register actions
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
ActionContainer *toolsContainer = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2020-01-24 18:34:57 +01: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");
|
2018-02-02 13:39:18 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+C,Meta+P") : tr("Alt+C,Alt+P")));
|
2020-01-24 18:34:57 +01:00
|
|
|
connect(m_postEditorAction, &QAction::triggered, this, &CodePasterPluginPrivate::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");
|
2018-02-02 13:39:18 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? tr("Meta+C,Meta+F") : tr("Alt+C,Alt+F")));
|
2020-01-24 18:34:57 +01:00
|
|
|
connect(m_fetchAction, &QAction::triggered, this, &CodePasterPluginPrivate::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");
|
2020-01-24 18:34:57 +01:00
|
|
|
connect(m_fetchUrlAction, &QAction::triggered, this, &CodePasterPluginPrivate::fetchUrl);
|
2012-05-16 14:51:01 +04:00
|
|
|
cpContainer->addAction(command);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
ExtensionSystem::IPlugin::ShutdownFlag CodePasterPlugin::aboutToShutdown()
|
2010-03-25 16:23:37 +01:00
|
|
|
{
|
|
|
|
|
// Delete temporary, fetched files
|
2020-01-24 18:34:57 +01:00
|
|
|
for (const QString &fetchedSnippet : qAsConst(d->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;
|
2018-11-04 23:43:17 +01:00
|
|
|
if (auto 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::post(CodePasterPlugin::PasteSources pasteSources)
|
2014-11-13 14:44:35 +01:00
|
|
|
{
|
|
|
|
|
QString data;
|
|
|
|
|
QString mimeType;
|
2020-01-24 18:34:57 +01:00
|
|
|
if (pasteSources & CodePasterPlugin::PasteEditor)
|
2014-11-13 14:44:35 +01:00
|
|
|
textFromCurrentEditor(&data, &mimeType);
|
2020-01-24 18:34:57 +01:00
|
|
|
if (data.isEmpty() && (pasteSources & CodePasterPlugin::PasteClipboard)) {
|
|
|
|
|
QString subType = "plain";
|
2017-04-24 17:01:10 +02:00
|
|
|
data = QGuiApplication::clipboard()->text(subType, QClipboard::Clipboard);
|
2014-11-13 14:44:35 +01:00
|
|
|
}
|
|
|
|
|
post(data, mimeType);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::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
|
|
|
|
2021-03-22 17:01:23 +01:00
|
|
|
const QString username = m_settings.username.value();
|
2009-07-13 08:32:46 +02:00
|
|
|
|
2014-02-28 09:00:24 +01:00
|
|
|
PasteView view(m_protocols, mimeType, ICore::dialogParent());
|
2021-03-22 17:01:23 +01:00
|
|
|
view.setProtocol(m_settings.protocols.stringValue());
|
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() ?
|
2021-11-22 15:35:07 +01:00
|
|
|
view.show(username, {}, {}, m_settings.expiryDays.value(), data) :
|
|
|
|
|
view.show(username, {}, {}, m_settings.expiryDays.value(), diffChunks);
|
2020-01-24 18:34:57 +01:00
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
// Save new protocol in case user changed it.
|
2021-03-22 17:01:23 +01:00
|
|
|
if (dialogResult == QDialog::Accepted && m_settings.protocols.value() != view.protocol()) {
|
|
|
|
|
m_settings.protocols.setValue(view.protocol());
|
|
|
|
|
m_settings.writeSettings(ICore::settings());
|
2011-02-28 15:41:42 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::fetchUrl()
|
2012-05-16 14:51:01 +04:00
|
|
|
{
|
|
|
|
|
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());
|
2020-01-24 18:34:57 +01:00
|
|
|
m_urlOpen.fetch(url.toString());
|
2012-05-16 14:51:01 +04:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::pasteSnippet()
|
2014-11-13 14:44:35 +01:00
|
|
|
{
|
2020-01-24 18:34:57 +01:00
|
|
|
post(CodePasterPlugin::PasteEditor | CodePasterPlugin::PasteClipboard);
|
2014-11-13 14:44:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::fetch()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-02-28 09:00:24 +01:00
|
|
|
PasteSelectDialog dialog(m_protocols, ICore::dialogParent());
|
2021-03-22 17:01:23 +01:00
|
|
|
dialog.setProtocol(m_settings.protocols.stringValue());
|
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.
|
2021-03-22 17:01:23 +01:00
|
|
|
if (m_settings.protocols.value() != dialog.protocol()) {
|
|
|
|
|
m_settings.protocols.setValue(dialog.protocol());
|
|
|
|
|
m_settings.writeSettings(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;
|
2021-04-13 09:48:43 +02:00
|
|
|
Protocol *protocol = m_protocols[dialog.protocol()];
|
2010-04-23 15:06:53 +02:00
|
|
|
if (Protocol::ensureConfiguration(protocol))
|
|
|
|
|
protocol->fetch(pasteID);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::finishPost(const QString &link)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2021-03-22 17:01:23 +01:00
|
|
|
if (m_settings.copyToClipboard.value())
|
2009-07-13 08:32:46 +02:00
|
|
|
QApplication::clipboard()->setText(link);
|
2021-03-22 17:01:23 +01:00
|
|
|
if (m_settings.displayOutput.value())
|
2020-12-01 14:41:17 +01:00
|
|
|
MessageManager::writeDisrupting(link);
|
|
|
|
|
else
|
2022-02-11 14:52:54 +01:00
|
|
|
MessageManager::writeFlashing(link);
|
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
|
2017-01-19 16:44:22 +01:00
|
|
|
QString pattern = Utils::TemporaryDirectory::masterDirectoryPath();
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 18:34:57 +01:00
|
|
|
void CodePasterPluginPrivate::finishFetch(const QString &titleDescription,
|
|
|
|
|
const QString &content,
|
|
|
|
|
bool error)
|
2009-07-31 11:17:57 +02:00
|
|
|
{
|
2010-03-25 16:23:37 +01:00
|
|
|
// Failure?
|
2009-07-31 11:17:57 +02:00
|
|
|
if (error) {
|
2020-12-01 14:41:17 +01:00
|
|
|
MessageManager::writeDisrupting(content);
|
2010-03-25 16:23:37 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2010-03-26 10:37:36 +01:00
|
|
|
if (content.isEmpty()) {
|
2020-12-01 14:41:17 +01:00
|
|
|
MessageManager::writeDisrupting(
|
|
|
|
|
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;
|
2017-03-02 12:07:11 +01:00
|
|
|
const Utils::MimeType mimeType = Utils::mimeTypeForData(byteContent);
|
2015-02-04 09:32:46 +01:00
|
|
|
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()) {
|
2020-12-01 14:41:17 +01:00
|
|
|
MessageManager::writeDisrupting(saver.errorString());
|
2010-03-26 10:37:36 +01:00
|
|
|
return;
|
2009-07-31 11:17:57 +02:00
|
|
|
}
|
2021-11-01 17:02:02 +01:00
|
|
|
const Utils::FilePath filePath = saver.filePath();
|
|
|
|
|
m_fetchedSnippets.push_back(filePath.toString());
|
2010-03-25 16:23:37 +01:00
|
|
|
// Open editor with title.
|
2021-11-01 17:02:02 +01:00
|
|
|
IEditor *editor = EditorManager::openEditor(filePath);
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(editor, return);
|
2015-02-25 14:36:03 +02:00
|
|
|
editor->document()->setPreferredDisplayName(titleDescription);
|
2009-07-31 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-21 15:21:50 +02:00
|
|
|
} // namespace CodePaster
|