summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <[email protected]>2015-10-06 13:41:38 +0200
committerMontel Laurent <[email protected]>2015-10-06 13:59:16 +0200
commitc1a7fcc0dd7c5c3a6a18e917055814123c5aa438 (patch)
treeb6b0b97e6780687b95e8499851f2ba84212faa6e
parent761be70dc116145afbbc8b6df11a539a1640e7f9 (diff)
We need a new widget for it
-rw-r--r--pimcommon/src/customtools/shorturlplugin/CMakeLists.txt1
-rw-r--r--pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.cpp221
-rw-r--r--pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.h78
3 files changed, 300 insertions, 0 deletions
diff --git a/pimcommon/src/customtools/shorturlplugin/CMakeLists.txt b/pimcommon/src/customtools/shorturlplugin/CMakeLists.txt
index c8e9373..ab39f3c 100644
--- a/pimcommon/src/customtools/shorturlplugin/CMakeLists.txt
+++ b/pimcommon/src/customtools/shorturlplugin/CMakeLists.txt
@@ -6,6 +6,7 @@ set(pimcommon_shorturlplugin_SRCS
set(pimcommon_shorturlplugin_shorturllib_SRCS
shorturl/abstractshorturl.cpp
shorturl/shorturlwidget.cpp
+ shorturl/shorturlwidgetng.cpp
shorturl/engine/tinyurlshorturl.cpp
shorturl/engine/googleshorturl.cpp
shorturl/shorturlconfigurewidget.cpp
diff --git a/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.cpp b/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.cpp
new file mode 100644
index 0000000..b7459d2
--- /dev/null
+++ b/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.cpp
@@ -0,0 +1,221 @@
+/*
+ Copyright (c) 2013-2015 Montel Laurent <[email protected]>
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License, version 2, as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "shorturlwidgetng.h"
+#include "shorturlutils.h"
+#include "shorturl/abstractshorturl.h"
+#include "shorturl/shorturlconfiguredialog.h"
+#include "Libkdepim/ProgressIndicatorLabel"
+
+#include <KLineEdit>
+#include <KLocalizedString>
+#include <KMessageBox>
+#include <KToggleAction>
+#include <KRun>
+#include <QIcon>
+#include <QNetworkConfigurationManager>
+
+#include <QDebug>
+#include <QLabel>
+#include <QGridLayout>
+#include <QPushButton>
+#include <QApplication>
+#include <QClipboard>
+#include <QToolButton>
+#include <QPointer>
+#include <KActionCollection>
+
+using namespace PimCommon;
+
+ShortUrlWidgetNg::ShortUrlWidgetNg(QWidget *parent)
+ : QWidget(parent),
+ mShorturlServiceName(Q_NULLPTR),
+ mEngine(Q_NULLPTR)
+{
+ loadEngine();
+ QGridLayout *grid = new QGridLayout;
+ grid->setMargin(2);
+ setLayout(grid);
+
+ QToolButton *closeBtn = new QToolButton(this);
+ closeBtn->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
+ closeBtn->setIconSize(QSize(16, 16));
+ closeBtn->setToolTip(i18n("Close"));
+#ifndef QT_NO_ACCESSIBILITY
+ closeBtn->setAccessibleName(i18n("Close"));
+#endif
+ closeBtn->setAutoRaise(true);
+ connect(closeBtn, &QToolButton::clicked, this, &ShortUrlWidgetNg::slotCloseWidget);
+
+ grid->addWidget(closeBtn, 0, 0);
+
+ mIndicatorLabel = new KPIM::ProgressIndicatorLabel(i18n("In progress to generate short url..."));
+ grid->addWidget(mIndicatorLabel, 0, 1);
+
+ QPushButton *configure = new QPushButton(i18n("Configure..."));
+ connect(configure, &QPushButton::clicked, this, &ShortUrlWidgetNg::slotConfigure);
+ grid->addWidget(configure, 0, 2);
+
+ mShorturlServiceName = new QLabel(mEngine->shortUrlName());
+ grid->addWidget(mShorturlServiceName, 1, 1);
+
+ mConvertButton = new QPushButton(i18n("Convert"));
+ grid->addWidget(mConvertButton, 1, 2);
+ connect(mConvertButton, &QPushButton::clicked, this, &ShortUrlWidgetNg::slotConvertUrl);
+
+ mInsertShortUrl = new QPushButton(i18n("Insert Short URL"));
+ connect(mInsertShortUrl, &QPushButton::clicked, this, &ShortUrlWidgetNg::slotInsertShortUrl);
+ grid->addWidget(mInsertShortUrl, 2, 2);
+
+ QLabel *lab = new QLabel(i18n("Original url:"));
+ grid->addWidget(lab, 3, 0);
+
+ mOriginalUrl = new KLineEdit;
+ mOriginalUrl->setClearButtonEnabled(true);
+ mOriginalUrl->setTrapReturnKey(true);
+ connect(mOriginalUrl, &KLineEdit::textChanged, this, &ShortUrlWidgetNg::slotOriginalUrlChanged);
+ connect(mOriginalUrl, &KLineEdit::returnPressed, this, &ShortUrlWidgetNg::slotConvertUrl);
+ grid->addWidget(mOriginalUrl, 3, 1);
+
+ mCopyToClipboard = new QPushButton(i18n("Copy to clipboard"));
+ connect(mCopyToClipboard, &QPushButton::clicked, this, &ShortUrlWidgetNg::slotPasteToClipboard);
+ grid->addWidget(mCopyToClipboard, 3, 2);
+
+ lab = new QLabel(i18n("Short url:"));
+ grid->addWidget(lab, 4, 0);
+
+ mShortUrl = new QLineEdit;
+ connect(mShortUrl, &QLineEdit::textChanged, this, &ShortUrlWidgetNg::slotShortUrlChanged);
+ mShortUrl->setReadOnly(true);
+ grid->addWidget(mShortUrl, 4, 1);
+
+ mOpenShortUrl = new QPushButton(i18n("Open Short URL"));
+ connect(mOpenShortUrl, &QPushButton::clicked, this, &ShortUrlWidgetNg::slotOpenShortUrl);
+ grid->addWidget(mOpenShortUrl, 4, 2);
+
+ grid->setRowStretch(5, 1);
+ mConvertButton->setEnabled(false);
+ mCopyToClipboard->setEnabled(false);
+ mInsertShortUrl->setEnabled(false);
+ mOpenShortUrl->setEnabled(false);
+
+ mNetworkConfigurationManager = new QNetworkConfigurationManager();
+}
+
+ShortUrlWidgetNg::~ShortUrlWidgetNg()
+{
+ delete mNetworkConfigurationManager;
+}
+
+void ShortUrlWidgetNg::slotInsertShortUrl()
+{
+ const QString shortUrl = mShortUrl->text();
+ if (!shortUrl.isEmpty()) {
+ Q_EMIT insertText(shortUrl);
+ }
+}
+
+void ShortUrlWidgetNg::slotConfigure()
+{
+ QPointer<ShortUrlConfigureDialog> dlg = new ShortUrlConfigureDialog(this);
+ if (dlg->exec()) {
+ loadEngine();
+ }
+ delete dlg;
+}
+
+void ShortUrlWidgetNg::settingsUpdated()
+{
+ loadEngine();
+}
+
+void ShortUrlWidgetNg::loadEngine()
+{
+ delete mEngine;
+ mEngine = PimCommon::ShortUrlUtils::loadEngine(this);
+ if (mShorturlServiceName) {
+ mShorturlServiceName->setText(mEngine->shortUrlName());
+ }
+ connect(mEngine, &AbstractShortUrl::shortUrlDone, this, &ShortUrlWidgetNg::slotShortUrlDone);
+ connect(mEngine, &AbstractShortUrl::shortUrlFailed, this, &ShortUrlWidgetNg::slotShortUrlFailed);
+}
+
+void ShortUrlWidgetNg::slotConvertUrl()
+{
+ if (!mNetworkConfigurationManager->isOnline()) {
+ KMessageBox::information(this, i18n("No network connection detected, we cannot shorten url."), i18n("No network"));
+ return;
+ }
+ if (mOriginalUrl->text().isEmpty()) {
+ return;
+ }
+ mIndicatorLabel->start();
+ mEngine->shortUrl(mOriginalUrl->text());
+ mShortUrl->clear();
+ mEngine->start();
+}
+
+void ShortUrlWidgetNg::slotPasteToClipboard()
+{
+ const QString shortUrl = mShortUrl->text();
+ if (!shortUrl.isEmpty()) {
+ QApplication::clipboard()->setText(shortUrl);
+ }
+}
+
+void ShortUrlWidgetNg::slotOriginalUrlChanged(const QString &text)
+{
+ mConvertButton->setEnabled(!text.isEmpty());
+}
+
+void ShortUrlWidgetNg::slotShortUrlChanged(const QString &text)
+{
+ mCopyToClipboard->setEnabled(!text.isEmpty());
+ mInsertShortUrl->setEnabled(!text.isEmpty());
+ mOpenShortUrl->setEnabled(!text.isEmpty());
+}
+
+void ShortUrlWidgetNg::slotShortUrlDone(const QString &url)
+{
+ mShortUrl->setText(url);
+ mIndicatorLabel->stop();
+}
+
+void ShortUrlWidgetNg::slotShortUrlFailed(const QString &errMsg)
+{
+ KMessageBox::error(this, i18n("An error occurs: \"%1\"", errMsg));
+ mIndicatorLabel->stop();
+}
+
+void ShortUrlWidgetNg::slotCloseWidget()
+{
+ mOriginalUrl->clear();
+ mShortUrl->clear();
+ mIndicatorLabel->stop();
+
+
+ Q_EMIT toolsWasClosed();
+}
+
+
+void ShortUrlWidgetNg::slotOpenShortUrl()
+{
+ const QString shortUrl = mShortUrl->text();
+ if (!shortUrl.isEmpty()) {
+ new KRun(QUrl(shortUrl), this);
+ }
+}
diff --git a/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.h b/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.h
new file mode 100644
index 0000000..aec732a
--- /dev/null
+++ b/pimcommon/src/customtools/shorturlplugin/shorturl/shorturlwidgetng.h
@@ -0,0 +1,78 @@
+/*
+ Copyright (c) 2013-2015 Montel Laurent <[email protected]>
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License, version 2, as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef ShortUrlWidgetNg_H
+#define ShortUrlWidgetNg_H
+
+#include <QWidget>
+
+class KLineEdit;
+class QPushButton;
+class QLabel;
+class QLineEdit;
+class QNetworkConfigurationManager;
+namespace KPIM
+{
+class ProgressIndicatorLabel;
+}
+
+namespace PimCommon
+{
+class AbstractShortUrl;
+class ShortUrlWidgetNg : public QWidget
+{
+ Q_OBJECT
+public:
+ explicit ShortUrlWidgetNg(QWidget *parent = Q_NULLPTR);
+ ~ShortUrlWidgetNg();
+
+ void setStandalone(bool b);
+public Q_SLOTS:
+ void settingsUpdated();
+
+private Q_SLOTS:
+ void slotConvertUrl();
+ void slotPasteToClipboard();
+ void slotOriginalUrlChanged(const QString &text);
+ void slotShortUrlChanged(const QString &text);
+ void slotShortUrlDone(const QString &url);
+ void slotShortUrlFailed(const QString &errMsg);
+ void slotCloseWidget();
+ void slotConfigure();
+ void slotInsertShortUrl();
+ void slotOpenShortUrl();
+
+Q_SIGNALS:
+ void toolsWasClosed();
+ void insertText(const QString &Url);
+
+private:
+ void loadEngine();
+ QLabel *mShorturlServiceName;
+ KLineEdit *mOriginalUrl;
+ QLineEdit *mShortUrl;
+ QPushButton *mConvertButton;
+ QPushButton *mCopyToClipboard;
+ QPushButton *mInsertShortUrl;
+ QPushButton *mOpenShortUrl;
+ AbstractShortUrl *mEngine;
+ KPIM::ProgressIndicatorLabel *mIndicatorLabel;
+ QNetworkConfigurationManager *mNetworkConfigurationManager;
+};
+}
+
+#endif // ShortUrlWidgetNg_H