Commit 10809cdd authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Some love for the Cpaster plugin

- Put classes into namespace.
- Fix settings not storing the selected protocol.
- Refactor settings by introducing standard handling.
- Create PasteSelectDialog class and fix list/refresh handling.
- No longer stuff the QListWidget into the protocol for listing, wire
via signals.
parent 0befb98c
......@@ -57,7 +57,7 @@ CodePasterProtocol::~CodePasterProtocol()
QString CodePasterProtocol::name() const
{
return "CodePaster";
return QLatin1String("CodePaster");
}
bool CodePasterProtocol::canList() const
......@@ -98,12 +98,11 @@ void CodePasterProtocol::fetch(const QString &id)
fetchId = id;
}
void CodePasterProtocol::list(QListWidget *listWidget)
void CodePasterProtocol::list()
{
QString hostName = m_page->hostName();
if (!isValidHostName(hostName))
return;
this->listWidget = listWidget;
QString link = QLatin1String("http://");
link += hostName;
link += QLatin1String("/?command=browse&format=raw");
......@@ -170,11 +169,9 @@ void CodePasterProtocol::listFinished()
if (listReply->error()) {
ICore::instance()->messageManager()->printToOutputPane(listReply->errorString(), true);
} else {
QByteArray data = listReply->readAll();
listWidget->clear();
QStringList lines = QString(data).split(QLatin1Char('\n'));
listWidget->addItems(lines);
listWidget = 0;
const QByteArray data = listReply->readAll();
const QStringList lines = QString::fromAscii(data).split(QLatin1Char('\n'));
emit listDone(name(), lines);
}
listReply->deleteLater();
listReply = 0;
......
......@@ -36,7 +36,6 @@
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
class QListWidget;
class QNetworkReply;
QT_END_NAMESPACE
......@@ -58,7 +57,7 @@ public:
Core::IOptionsPage *settingsPage();
void fetch(const QString &id);
void list(QListWidget *listWidget);
void list();
void paste(const QString &text,
const QString &username = QString(),
const QString &comment = QString(),
......@@ -75,7 +74,6 @@ private:
QNetworkAccessManager manager;
QNetworkReply *reply;
QNetworkReply *listReply;
QListWidget *listWidget;
QString fetchId;
};
......
......@@ -12,7 +12,9 @@ HEADERS += cpasterplugin.h \
codepastersettings.h \
pastebindotcomprotocol.h \
pastebindotcomsettings.h \
pastebindotcaprotocol.h
pastebindotcaprotocol.h \
settings.h \
pasteselectdialog.h
SOURCES += cpasterplugin.cpp \
settingspage.cpp \
protocol.cpp \
......@@ -21,7 +23,9 @@ SOURCES += cpasterplugin.cpp \
codepastersettings.cpp \
pastebindotcomprotocol.cpp \
pastebindotcomsettings.cpp \
pastebindotcaprotocol.cpp
pastebindotcaprotocol.cpp \
settings.cpp \
pasteselectdialog.cpp
FORMS += settingspage.ui \
pasteselect.ui \
pasteview.ui \
......
......@@ -29,14 +29,14 @@
#include "cpasterplugin.h"
#include "ui_pasteselect.h"
#include "splitter.h"
#include "pasteview.h"
#include "codepasterprotocol.h"
#include "pastebindotcomprotocol.h"
#include "pastebindotcaprotocol.h"
#include "pasteselectdialog.h"
#include "settingspage.h"
#include "settings.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
......@@ -65,8 +65,7 @@ using namespace CodePaster;
using namespace Core;
using namespace TextEditor;
CodepasterPlugin::CodepasterPlugin()
: m_settingsPage(0)
CodepasterPlugin::CodepasterPlugin() : m_settings(new Settings)
{
}
......@@ -85,8 +84,9 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
globalcontext << UniqueIDManager::instance()->uniqueIdentifier(Core::Constants::C_GLOBAL);
// Create the settings Page
m_settingsPage = new SettingsPage();
addAutoReleasedObject(m_settingsPage);
m_settings->fromSettings(Core::ICore::instance()->settings());
SettingsPage *settingsPage = new SettingsPage(m_settings);
addAutoReleasedObject(settingsPage);
// Create the protocols and append them to the Settings
Protocol *protos[] = { new CodePasterProtocol(),
......@@ -97,7 +97,7 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
connect(protos[i], SIGNAL(fetchDone(QString,QString,bool)),
this, SLOT(finishFetch(QString,QString,bool)));
m_settingsPage->addProtocol(protos[i]->name());
settingsPage->addProtocol(protos[i]->name());
if (protos[i]->hasSettings())
addAutoReleasedObject(protos[i]->settingsPage());
m_protocols.append(protos[i]);
......@@ -176,14 +176,14 @@ void CodepasterPlugin::post()
data = textEditor->contents();
FileDataList lst = splitDiffToFiles(data.toLatin1());
QString username = m_settingsPage->username();
QString username = m_settings->username;
QString description;
QString comment;
QString protocolName;
PasteView view(0);
foreach (Protocol *p, m_protocols) {
view.addProtocol(p->name(), p->name() == m_settingsPage->defaultProtocol());
view.addProtocol(p->name(), p->name() == m_settings->protocol);
}
if (!view.show(username, description, comment, lst))
......@@ -214,55 +214,23 @@ void CodepasterPlugin::post()
void CodepasterPlugin::fetch()
{
QDialog dialog(ICore::instance()->mainWindow());
Ui_PasteSelectDialog ui;
ui.setupUi(&dialog);
foreach(const Protocol *protocol, m_protocols)
ui.protocolBox->addItem(protocol->name());
ui.protocolBox->setCurrentIndex(ui.protocolBox->findText(m_settingsPage->defaultProtocol()));
ui.listWidget->addItems(QStringList() << tr("This protocol supports no listing"));
ui.listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
#ifndef Q_WS_MACX
ui.listWidget->setFrameStyle(QFrame::NoFrame);
#endif // Q_WS_MACX
QFont listFont = ui.listWidget->font();
listFont.setFamily("Courier");
listFont.setStyleHint(QFont::TypeWriter);
ui.listWidget->setFont(listFont);
// ### 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;
}
}
PasteSelectDialog dialog(m_protocols, ICore::instance()->mainWindow());
dialog.setProtocol(m_settings->protocol);
int result = dialog.exec();
if (!result)
if (dialog.exec() != QDialog::Accepted)
return;
QStringList list = ui.pasteEdit->text().split(QLatin1Char(' '));
if (list.isEmpty())
const QString pasteID = dialog.pasteId();
if (pasteID.isEmpty())
return;
QString pasteID = list.first();
// Get Protocol
foreach(Protocol *protocol, m_protocols) {
if (protocol->name() == ui.protocolBox->currentText()) {
protocol->fetch(pasteID);
break;
}
}
m_protocols[dialog.protocolIndex()]->fetch(pasteID);
}
void CodepasterPlugin::finishPost(const QString &link)
{
if (m_settingsPage->copyToClipBoard())
if (m_settings->copyToClipboard)
QApplication::clipboard()->setText(link);
ICore::instance()->messageManager()->printToOutputPane(link,
m_settingsPage->displayOutput());
m_settings->displayOutput);
}
// Extract the characters that can be used for a file name from a title
......
......@@ -33,17 +33,17 @@
#include <extensionsystem/iplugin.h>
#include <QtCore/QStringList>
#include <QtCore/QSharedPointer>
QT_BEGIN_NAMESPACE
class QAction;
QT_END_NAMESPACE
class Protocol;
namespace CodePaster {
class SettingsPage;
class CustomFetcher;
class CustomPoster;
struct Settings;
class Protocol;
class CodepasterPlugin : public ExtensionSystem::IPlugin
{
......@@ -66,9 +66,9 @@ public slots:
bool error);
private:
const QSharedPointer<Settings> m_settings;
QAction *m_postAction;
QAction *m_fetchAction;
SettingsPage *m_settingsPage;
QList<Protocol*> m_protocols;
QStringList m_fetchedSnippets;
};
......
......@@ -33,7 +33,7 @@
#include <QtNetwork/QNetworkReply>
using namespace Core;
namespace CodePaster {
PasteBinDotCaProtocol::PasteBinDotCaProtocol()
{
connect(&http, SIGNAL(requestFinished(int,bool)),
......@@ -101,3 +101,4 @@ void PasteBinDotCaProtocol::fetchFinished()
reply = 0;
emit fetchDone(title, content, error);
}
} // namespace CodePaster
......@@ -35,6 +35,7 @@
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QHttp>
namespace CodePaster {
class PasteBinDotCaProtocol : public Protocol
{
Q_OBJECT
......@@ -64,4 +65,5 @@ private:
int postId;
};
} // namespace CodePaster
#endif // PASTEBINDOTCAPROTOCOL_H
......@@ -36,6 +36,7 @@
using namespace Core;
namespace CodePaster {
PasteBinDotComProtocol::PasteBinDotComProtocol()
{
settings = new PasteBinDotComSettings();
......@@ -127,3 +128,4 @@ Core::IOptionsPage* PasteBinDotComProtocol::settingsPage()
{
return settings;
}
} // namespace CodePaster
......@@ -35,6 +35,7 @@
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QHttp>
namespace CodePaster {
class PasteBinDotComSettings;
class PasteBinDotComProtocol : public Protocol
......@@ -70,5 +71,5 @@ private:
QHttp http;
int postId;
};
} // namespace CodePaster
#endif // PASTEBINDOTCOMPROTOCOL_H
......@@ -35,6 +35,7 @@
#include <QtCore/QSettings>
#include <QtCore/QCoreApplication>
namespace CodePaster {
PasteBinDotComSettings::PasteBinDotComSettings()
{
m_settings = Core::ICore::instance()->settings();
......@@ -94,3 +95,4 @@ QString PasteBinDotComSettings::hostPrefix() const
{
return m_hostPrefix;
}
} //namespace CodePaster
......@@ -40,6 +40,7 @@ QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace CodePaster {
class PasteBinDotComSettings : public Core::IOptionsPage
{
Q_OBJECT
......@@ -64,5 +65,5 @@ private:
QSettings *m_settings;
QString m_hostPrefix;
};
} // namespace CodePaster
#endif
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "pasteselectdialog.h"
#include "protocol.h"
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtGui/QPushButton>
namespace CodePaster {
PasteSelectDialog::PasteSelectDialog(const QList<Protocol*> &protocols,
QWidget *parent) :
QDialog(parent),
m_protocols(protocols)
{
m_ui.setupUi(this);
foreach(const Protocol *protocol, protocols) {
m_ui.protocolBox->addItem(protocol->name());
connect(protocol, SIGNAL(listDone(QString,QStringList)),
this, SLOT(listDone(QString,QStringList)));
}
connect(m_ui.protocolBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(protocolChanged(int)));
m_refreshButton = m_ui.buttons->addButton(tr("Refresh"), QDialogButtonBox::ActionRole);
connect(m_refreshButton, SIGNAL(clicked()), this, SLOT(list()));
m_ui.listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
#ifndef Q_WS_MACX
m_ui.listWidget->setFrameStyle(QFrame::NoFrame);
#endif // Q_WS_MACX
// Proportional formatting of columns for CodePaster
QFont listFont = m_ui.listWidget->font();
listFont.setFamily(QLatin1String("Courier"));
listFont.setStyleHint(QFont::TypeWriter);
m_ui.listWidget->setFont(listFont);
}
PasteSelectDialog::~PasteSelectDialog()
{
}
QString PasteSelectDialog::pasteId() const
{
QString id = m_ui.pasteEdit->text();
const int blankPos = id.indexOf(QLatin1Char(' '));
if (blankPos != -1)
id.truncate(blankPos);
return id;
}
void PasteSelectDialog::setProtocol(const QString &p)
{
const int index = m_ui.protocolBox->findText(p);
if (index >= 0) {
if (index != m_ui.protocolBox->currentIndex()) {
m_ui.protocolBox->setCurrentIndex(index);
} else {
// Trigger a refresh
protocolChanged(index);
}
}
}
QString PasteSelectDialog::protocol() const
{
return m_ui.protocolBox->currentText();
}
int PasteSelectDialog::protocolIndex() const
{
return m_ui.protocolBox->currentIndex();
}
void PasteSelectDialog::listDone(const QString &name, const QStringList &items)
{
// Set if the protocol is still current
if (name == protocol()) {
m_ui.listWidget->clear();
m_ui.listWidget->addItems(items);
}
}
void PasteSelectDialog::list()
{
const int index = protocolIndex();
QTC_ASSERT(m_protocols.at(index)->canList(), return);
m_ui.listWidget->clear();
m_ui.listWidget->addItem(new QListWidgetItem(tr("Waiting for items")));
m_protocols[index]->list();
}
void PasteSelectDialog::protocolChanged(int i)
{
const bool canList = m_protocols.at(i)->canList();
m_refreshButton->setEnabled(canList);
if (canList) {
list();
} else {
m_ui.listWidget->clear();
m_ui.listWidget->addItem(new QListWidgetItem(tr("This protocol does not support listing")));
}
}
} // namespace CodePaster
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef PASTESELECTDIALOG_H
#define PASTESELECTDIALOG_H
#include "ui_pasteselect.h"
#include <QtCore/QList>
QT_FORWARD_DECLARE_CLASS(QPushButton)
namespace CodePaster {
class Protocol;
class PasteSelectDialog : public QDialog
{
Q_OBJECT
public:
explicit PasteSelectDialog(const QList<Protocol*> &protocols,
QWidget *parent = 0);
~PasteSelectDialog();
QString pasteId() const;
QString protocol() const;
void setProtocol(const QString &);
int protocolIndex() const;
signals:
private slots:
void protocolChanged(int);
void list();
void listDone(const QString &name, const QStringList &items);
private:
const QList<Protocol*> m_protocols;
Ui_PasteSelectDialog m_ui;
QPushButton *m_refreshButton;
};
} // namespace CodePaster
#endif // PASTESELECTDIALOG_H
......@@ -28,7 +28,7 @@
**************************************************************************/
#include "protocol.h"
#include <QtCore/qglobal.h>
namespace CodePaster {
Protocol::Protocol()
: QObject()
......@@ -59,7 +59,9 @@ Core::IOptionsPage* Protocol::settingsPage()
return 0;
}
void Protocol::list(QListWidget*)
void Protocol::list()
{
qFatal("Base Protocol list() called");
}
} //namespace CodePaster
......@@ -30,12 +30,13 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <coreplugin/dialogs/ioptionspage.h>
#include <QtCore/QObject>
QT_BEGIN_NAMESPACE
class QListWidget;
QT_END_NAMESPACE
namespace Core {
class IOptionsPage;
}
namespace CodePaster {
class Protocol : public QObject
{
Q_OBJECT
......@@ -52,7 +53,7 @@ public:
virtual Core::IOptionsPage* settingsPage();
virtual void fetch(const QString &id) = 0;
virtual void list(QListWidget *listWidget);
virtual void list();
virtual void paste(const QString &text,
const QString &username = QString(),
const QString &comment = QString(),
......@@ -63,6 +64,9 @@ signals:
void fetchDone(const QString &titleDescription,
const QString &content,
bool error);
void listDone(const QString &name, const QStringList &result);
};
} //namespace CodePaster
#endif // PROTOCOL_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in