Commit 94babbd9 authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

CodePaster: Do better checking on hosts.

Do an initial connection check on the host
(once per host). Add utility function to
NetworkProtocol, displaying connection check
message box.
Move the 'Paste' handling into the PasteView
dialog, such that the checking happens there
and the dialog stays open if something fails.

Task-number: QTCREATORBUG-2117
parent a18c8f9e
......@@ -71,9 +71,10 @@ unsigned CodePasterProtocol::capabilities() const
return ListCapability|PostCommentCapability|PostDescriptionCapability;
}
bool CodePasterProtocol::checkConfiguration(QString *errorMessage) const
bool CodePasterProtocol::checkConfiguration(QString *errorMessage)
{
if (m_page->hostName().isEmpty()) {
const QString hostName = m_page->hostName();
if (hostName.isEmpty()) {
if (errorMessage) {
*errorMessage =
#ifdef Q_OS_MAC
......@@ -84,7 +85,13 @@ bool CodePasterProtocol::checkConfiguration(QString *errorMessage) const
}
return false;
}
return true;
// Check the host once. Note that it can be modified in the settings page.
if (m_hostChecked == hostName)
return true;
const bool ok = httpStatus(m_page->hostName(), errorMessage);
if (ok)
m_hostChecked = hostName;
return ok;
}
void CodePasterProtocol::fetch(const QString &id)
......
......@@ -53,7 +53,7 @@ public:
bool hasSettings() const;
Core::IOptionsPage *settingsPage() const;
virtual bool checkConfiguration(QString *errorMessage = 0) const;
virtual bool checkConfiguration(QString *errorMessage = 0);
void fetch(const QString &id);
void list();
void paste(const QString &text,
......@@ -72,6 +72,7 @@ private:
QNetworkReply *m_fetchReply;
QNetworkReply *m_listReply;
QString m_fetchId;
QString m_hostChecked;
};
} // namespace CodePaster
......
......@@ -208,25 +208,10 @@ void CodepasterPlugin::post(QString data, const QString &mimeType)
QString comment;
QString protocolName;
PasteView view(m_protocols, 0);
PasteView view(m_protocols, mimeType, 0);
view.setProtocol(m_settings->protocol);
if (!view.show(username, description, comment, lst))
return; // User canceled post
username = view.user();
description = view.description();
comment = view.comment();
data = view.content();
protocolName = view.protocol();
foreach(Protocol *protocol, m_protocols) {
if (protocol->name() == protocolName) {
const Protocol::ContentType ct = Protocol::contentType(mimeType);
if (Protocol::ensureConfiguration(protocol))
protocol->paste(data, ct, username, comment, description);
break;
}
}
view.show(username, description, comment, lst);
}
void CodepasterPlugin::fetch()
......
......@@ -43,7 +43,8 @@ PasteBinDotCaProtocol::PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr
NetworkProtocol(nw),
m_fetchReply(0),
m_listReply(0),
m_pasteReply(0)
m_pasteReply(0),
m_hostChecked(false)
{
}
......@@ -131,6 +132,16 @@ void PasteBinDotCaProtocol::list()
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
}
bool PasteBinDotCaProtocol::checkConfiguration(QString *errorMessage)
{
if (m_hostChecked) // Check the host once.
return true;
const bool ok = httpStatus(QLatin1String(urlC), errorMessage);
if (ok)
m_hostChecked = true;
return ok;
}
/* Quick & dirty: Parse the <div>-elements with the "Recent Posts" listing
* out of the page.
\code
......
......@@ -56,11 +56,15 @@ public slots:
void listFinished();
void pasteFinished();
protected:
virtual bool checkConfiguration(QString *errorMessage);
private:
QNetworkReply *m_fetchReply;
QNetworkReply *m_listReply;
QNetworkReply *m_pasteReply;
QString m_fetchId;
bool m_hostChecked;
};
} // namespace CodePaster
......
......@@ -55,7 +55,8 @@ PasteBinDotComProtocol::PasteBinDotComProtocol(const NetworkAccessManagerProxyPt
m_pasteReply(0),
m_listReply(0),
m_fetchId(-1),
m_postId(-1)
m_postId(-1),
m_hostChecked(false)
{
}
......@@ -69,6 +70,16 @@ unsigned PasteBinDotComProtocol::capabilities() const
return ListCapability;
}
bool PasteBinDotComProtocol::checkConfiguration(QString *errorMessage)
{
if (m_hostChecked) // Check the host once.
return true;
const bool ok = httpStatus(hostName(false), errorMessage);
if (ok)
m_hostChecked = true;
return ok;
}
QString PasteBinDotComProtocol::hostName(bool withSubDomain) const
{
......
......@@ -61,6 +61,9 @@ public slots:
void pasteFinished();
void listFinished();
protected:
virtual bool checkConfiguration(QString *errorMessage = 0);
private:
QString hostName(bool withSubDomain) const;
......@@ -71,6 +74,7 @@ private:
QString m_fetchId;
int m_postId;
bool m_hostChecked;
};
} // namespace CodePaster
#endif // PASTEBINDOTCOMPROTOCOL_H
......@@ -46,9 +46,12 @@ static const char widthKeyC[] = "PasteViewWidth";
namespace CodePaster {
// -------------------------------------------------------------------------------------------------
PasteView::PasteView(const QList<Protocol *> protocols,
QWidget *parent)
: QDialog(parent), m_protocols(protocols),
m_commentPlaceHolder(tr("<Comment>"))
const QString &mt,
QWidget *parent) :
QDialog(parent),
m_protocols(protocols),
m_commentPlaceHolder(tr("<Comment>")),
m_mimeType(mt)
{
m_ui.setupUi(this);
......@@ -140,7 +143,7 @@ int PasteView::show(const QString &user, const QString &description, const QStri
m_ui.uiDescription->selectAll();
// (Re)store dialog size
QSettings *settings = Core::ICore::instance()->settings();
const QSettings *settings = Core::ICore::instance()->settings();
const QString rootKey = QLatin1String(groupC) + QLatin1Char('/');
const int h = settings->value(rootKey + QLatin1String(heightKeyC), height()).toInt();
const int defaultWidth = m_ui.uiPatchView->columnIndicator() + 50;
......@@ -149,16 +152,31 @@ int PasteView::show(const QString &user, const QString &description, const QStri
resize(w, h);
const int ret = QDialog::exec();
if (ret == QDialog::Accepted) {
settings->beginGroup(QLatin1String(groupC));
settings->setValue(QLatin1String(heightKeyC), height());
settings->setValue(QLatin1String(widthKeyC), width());
settings->endGroup();
}
return ret;
}
void PasteView::accept()
{
const int index = m_ui.protocolBox->currentIndex();
if (index == -1)
return;
Protocol *protocol = m_protocols.at(index);
if (!Protocol::ensureConfiguration(protocol, this))
return;
const Protocol::ContentType ct = Protocol::contentType(m_mimeType);
protocol->paste(content(), ct, user(), comment(), description());
// Store settings and close
QSettings *settings = Core::ICore::instance()->settings();
settings->beginGroup(QLatin1String(groupC));
settings->setValue(QLatin1String(heightKeyC), height());
settings->setValue(QLatin1String(widthKeyC), width());
settings->endGroup();
QDialog::accept();
}
void PasteView::setProtocol(const QString &protocol)
{
const int index = m_ui.protocolBox->findText(protocol);
......
......@@ -42,6 +42,7 @@ class PasteView : public QDialog
Q_OBJECT
public:
explicit PasteView(const QList<Protocol *> protocols,
const QString &mimeType,
QWidget *parent);
~PasteView();
......@@ -56,6 +57,8 @@ public:
QByteArray content() const;
QString protocol() const;
virtual void accept();
private slots:
void contentChanged();
void protocolChanged(int);
......@@ -63,6 +66,7 @@ private slots:
private:
const QList<Protocol *> m_protocols;
const QString m_commentPlaceHolder;
const QString m_mimeType;
Ui::ViewDialog m_ui;
FileDataList m_parts;
......
......@@ -35,9 +35,13 @@
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QUrl>
#include <QtCore/QDebug>
#include <QtGui/QMessageBox>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
......@@ -57,7 +61,7 @@ bool Protocol::hasSettings() const
return false;
}
bool Protocol::checkConfiguration(QString *) const
bool Protocol::checkConfiguration(QString *)
{
return true;
}
......@@ -115,7 +119,7 @@ QString Protocol::textFromHtml(QString data)
return data;
}
bool Protocol::ensureConfiguration(const Protocol *p, QWidget *parent)
bool Protocol::ensureConfiguration(Protocol *p, QWidget *parent)
{
QString errorMessage;
bool ok = false;
......@@ -196,4 +200,36 @@ NetworkProtocol::~NetworkProtocol()
{
}
bool NetworkProtocol::httpStatus(QString url, QString *errorMessage)
{
// Connect to host and display a message box, using its event loop.
errorMessage->clear();
const QString httpPrefix = QLatin1String("http://");
if (!url.startsWith(httpPrefix)) {
url.prepend(httpPrefix);
url.append(QLatin1Char('/'));
}
QNetworkReply *reply = httpGet(url);
QMessageBox box(QMessageBox::Information,
tr("Checking connection"),
tr("Connecting to %1...").arg(url),
QMessageBox::Cancel,
Core::ICore::instance()->mainWindow());
connect(reply, SIGNAL(finished()), &box, SLOT(close()));
QApplication::setOverrideCursor(Qt::WaitCursor);
box.exec();
QApplication::restoreOverrideCursor();
// User canceled, discard and be happy.
if (!reply->isFinished()) {
connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater()));
return false;
}
// Passed
if (reply->error() == QNetworkReply::NoError)
return true;
// Error.
*errorMessage = reply->errorString();
return false;
}
} //namespace CodePaster
......@@ -67,7 +67,7 @@ public:
virtual bool hasSettings() const;
virtual Core::IOptionsPage *settingsPage() const;
virtual bool checkConfiguration(QString *errorMessage = 0) const;
virtual bool checkConfiguration(QString *errorMessage = 0);
virtual void fetch(const QString &id) = 0;
virtual void list();
virtual void paste(const QString &text,
......@@ -86,7 +86,7 @@ public:
QWidget *parent = 0,
bool showConfig = true);
// Ensure configuration is correct
static bool ensureConfiguration(const Protocol *p,
static bool ensureConfiguration(Protocol *p,
QWidget *parent = 0);
signals:
......@@ -143,6 +143,9 @@ protected:
inline QNetworkAccessManager *networkAccessManager()
{ return m_networkAccessManager->networkAccessManager(); }
// Check connectivity of host, displaying a message box.
bool httpStatus(QString url, QString *errorMessage);
private:
const NetworkAccessManagerProxyPtr m_networkAccessManager;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment