2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cpasterplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "ui_pasteselect.h"
|
|
|
|
|
|
|
|
|
|
#include "splitter.h"
|
2009-07-13 08:32:46 +02:00
|
|
|
#include "pasteview.h"
|
|
|
|
|
#include "codepasterprotocol.h"
|
|
|
|
|
#include "pastebindotcomprotocol.h"
|
2009-07-31 12:13:37 +02:00
|
|
|
#include "pastebindotcaprotocol.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.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>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/filemanager.h>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2009-01-20 11:52:04 +01:00
|
|
|
#include <coreplugin/messageoutputwindow.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/uniqueidmanager.h>
|
|
|
|
|
#include <texteditor/itexteditor.h>
|
|
|
|
|
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtCore/QtPlugin>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QMessageBox>
|
|
|
|
|
#include <QtGui/QMainWindow>
|
|
|
|
|
#include <QtGui/QListWidget>
|
|
|
|
|
|
|
|
|
|
using namespace CodePaster;
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
CodepasterPlugin::CodepasterPlugin()
|
2009-07-13 08:32:46 +02:00
|
|
|
: m_settingsPage(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodepasterPlugin::~CodepasterPlugin()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_message)
|
|
|
|
|
{
|
2009-07-13 17:35:17 +02:00
|
|
|
Q_UNUSED(arguments)
|
|
|
|
|
Q_UNUSED(error_message)
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Create the globalcontext list to register actions accordingly
|
|
|
|
|
QList<int> globalcontext;
|
2009-01-21 15:52:34 +01:00
|
|
|
globalcontext << UniqueIDManager::instance()->uniqueIdentifier(Core::Constants::C_GLOBAL);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Create the settings Page
|
|
|
|
|
m_settingsPage = new SettingsPage();
|
2009-07-31 17:11:42 +02:00
|
|
|
addAutoReleasedObject(m_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
|
|
|
|
|
Protocol *protos[] = { new CodePasterProtocol(),
|
|
|
|
|
new PasteBinDotComProtocol(),
|
2009-07-31 12:13:37 +02:00
|
|
|
new PasteBinDotCaProtocol(),
|
2009-07-13 08:32:46 +02:00
|
|
|
0};
|
|
|
|
|
for(int i=0; protos[i] != 0; ++i) {
|
|
|
|
|
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)));
|
2009-07-13 08:32:46 +02:00
|
|
|
m_settingsPage->addProtocol(protos[i]->name());
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
//register actions
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::ActionManager *actionManager = ICore::instance()->actionManager();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-14 12:39:59 +01:00
|
|
|
Core::ActionContainer *toolsContainer =
|
2008-12-02 12:01:29 +01:00
|
|
|
actionManager->actionContainer(Core::Constants::M_TOOLS);
|
|
|
|
|
|
2009-01-14 12:39:59 +01:00
|
|
|
Core::ActionContainer *cpContainer =
|
2008-12-02 12:01:29 +01:00
|
|
|
actionManager->createMenu(QLatin1String("CodePaster"));
|
|
|
|
|
cpContainer->menu()->setTitle(tr("&CodePaster"));
|
|
|
|
|
toolsContainer->addMenu(cpContainer);
|
|
|
|
|
|
2009-01-14 13:17:53 +01:00
|
|
|
Core::Command *command;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-02-20 17:22:16 +01:00
|
|
|
m_postAction = new QAction(tr("Paste Snippet..."), this);
|
|
|
|
|
command = actionManager->registerAction(m_postAction, "CodePaster.Post", globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(tr("Alt+C,Alt+P")));
|
|
|
|
|
connect(m_postAction, SIGNAL(triggered()), this, SLOT(post()));
|
|
|
|
|
cpContainer->addAction(command);
|
|
|
|
|
|
2009-02-20 17:22:16 +01:00
|
|
|
m_fetchAction = new QAction(tr("Fetch Snippet..."), this);
|
|
|
|
|
command = actionManager->registerAction(m_fetchAction, "CodePaster.Fetch", globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(tr("Alt+C,Alt+F")));
|
|
|
|
|
connect(m_fetchAction, SIGNAL(triggered()), this, SLOT(fetch()));
|
|
|
|
|
cpContainer->addAction(command);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodepasterPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodepasterPlugin::post()
|
|
|
|
|
{
|
2009-01-21 15:52:34 +01:00
|
|
|
IEditor* editor = EditorManager::instance()->currentEditor();
|
2008-12-02 12:01:29 +01:00
|
|
|
ITextEditor* textEditor = qobject_cast<ITextEditor*>(editor);
|
|
|
|
|
if (!textEditor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QString data = textEditor->selectedText();
|
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
|
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:
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
data = textEditor->contents();
|
|
|
|
|
|
|
|
|
|
FileDataList lst = splitDiffToFiles(data.toLatin1());
|
|
|
|
|
QString username = m_settingsPage->username();
|
|
|
|
|
QString description;
|
|
|
|
|
QString comment;
|
2009-07-13 08:32:46 +02:00
|
|
|
QString protocolName;
|
|
|
|
|
|
|
|
|
|
PasteView view(0);
|
|
|
|
|
foreach (Protocol *p, m_protocols) {
|
|
|
|
|
view.addProtocol(p->name(), p->name() == m_settingsPage->defaultProtocol());
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
if (!view.show(username, description, comment, lst))
|
|
|
|
|
return; // User canceled post
|
|
|
|
|
|
2009-08-10 16:31:56 +02:00
|
|
|
username = view.user();
|
|
|
|
|
description = view.description();
|
|
|
|
|
comment = view.comment();
|
|
|
|
|
data = view.content();
|
|
|
|
|
protocolName = view.protocol();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Copied from cpaster. Otherwise lineendings will screw up
|
|
|
|
|
if (!data.contains("\r\n")) {
|
|
|
|
|
if (data.contains('\n'))
|
|
|
|
|
data.replace('\n', "\r\n");
|
|
|
|
|
else if (data.contains('\r'))
|
|
|
|
|
data.replace('\r', "\r\n");
|
|
|
|
|
}
|
2009-07-13 08:32:46 +02:00
|
|
|
|
|
|
|
|
foreach(Protocol *protocol, m_protocols) {
|
|
|
|
|
if (protocol->name() == protocolName) {
|
|
|
|
|
protocol->paste(data, username, comment, description);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodepasterPlugin::fetch()
|
|
|
|
|
{
|
2009-05-11 17:52:15 +02:00
|
|
|
QDialog dialog(ICore::instance()->mainWindow());
|
2008-12-02 12:01:29 +01:00
|
|
|
Ui_PasteSelectDialog ui;
|
|
|
|
|
ui.setupUi(&dialog);
|
2009-07-13 08:32:46 +02:00
|
|
|
foreach(const Protocol *protocol, m_protocols)
|
|
|
|
|
ui.protocolBox->addItem(protocol->name());
|
|
|
|
|
ui.protocolBox->setCurrentIndex(ui.protocolBox->findText(m_settingsPage->defaultProtocol()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-07-13 08:32:46 +02:00
|
|
|
ui.listWidget->addItems(QStringList() << tr("This protocol supports no listing"));
|
2008-12-02 12:01:29 +01:00
|
|
|
ui.listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
2009-07-31 12:59:41 +02:00
|
|
|
#ifndef Q_WS_MACX
|
2008-12-02 12:01:29 +01:00
|
|
|
ui.listWidget->setFrameStyle(QFrame::NoFrame);
|
2009-07-31 12:59:41 +02:00
|
|
|
#endif // Q_WS_MACX
|
|
|
|
|
QFont listFont = ui.listWidget->font();
|
|
|
|
|
listFont.setFamily("Courier");
|
|
|
|
|
listFont.setStyleHint(QFont::TypeWriter);
|
|
|
|
|
ui.listWidget->setFont(listFont);
|
2009-07-13 08:32:46 +02:00
|
|
|
// ### TODO2: when we change the protocol, we need to relist
|
|
|
|
|
foreach(Protocol *protocol, m_protocols) {
|
|
|
|
|
if (protocol->name() == ui.protocolBox->currentText() && protocol->canList()) {
|
|
|
|
|
ui.listWidget->clear();
|
|
|
|
|
ui.listWidget->addItems(QStringList() << tr("Waiting for items"));
|
|
|
|
|
protocol->list(ui.listWidget);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
int result = dialog.exec();
|
|
|
|
|
if (!result)
|
|
|
|
|
return;
|
|
|
|
|
QStringList list = ui.pasteEdit->text().split(QLatin1Char(' '));
|
2009-07-13 08:32:46 +02:00
|
|
|
if (list.isEmpty())
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
2009-07-13 08:32:46 +02:00
|
|
|
QString pasteID = list.first();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-07-13 08:32:46 +02:00
|
|
|
// Get Protocol
|
|
|
|
|
foreach(Protocol *protocol, m_protocols) {
|
|
|
|
|
if (protocol->name() == ui.protocolBox->currentText()) {
|
|
|
|
|
protocol->fetch(pasteID);
|
|
|
|
|
break;
|
|
|
|
|
}
|
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
|
|
|
{
|
2009-07-13 08:32:46 +02:00
|
|
|
if (m_settingsPage->copyToClipBoard())
|
|
|
|
|
QApplication::clipboard()->setText(link);
|
|
|
|
|
ICore::instance()->messageManager()->printToOutputPane(link,
|
|
|
|
|
m_settingsPage->displayOutput());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-31 11:17:57 +02:00
|
|
|
void CodepasterPlugin::finishFetch(const QString &titleDescription,
|
|
|
|
|
const QString &content,
|
|
|
|
|
bool error)
|
|
|
|
|
{
|
|
|
|
|
QString title = titleDescription;
|
|
|
|
|
if (error) {
|
|
|
|
|
ICore::instance()->messageManager()->printToOutputPane(content, true);
|
|
|
|
|
} else {
|
2009-07-31 11:24:12 +02:00
|
|
|
EditorManager* manager = EditorManager::instance();
|
2009-08-12 10:41:30 +02:00
|
|
|
IEditor* editor = manager->openEditorWithContents(Core::Constants::K_DEFAULT_TEXT_EDITOR, &title, content);
|
2009-07-31 11:24:12 +02:00
|
|
|
manager->activateEditor(editor);
|
2009-07-31 11:17:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Q_EXPORT_PLUGIN(CodepasterPlugin)
|