diff --git a/CMakeLists.txt b/CMakeLists.txt index b6a65dec1..3a49c1871 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -440,6 +440,7 @@ set(APP_SOURCES src/services/tt-rss/ttrssserviceentrypoint.cpp src/services/tt-rss/ttrssserviceroot.cpp src/services/tt-rss/gui/formeditaccount.cpp + src/services/tt-rss/network/ttrssnetworkfactory.cpp # NETWORK-WEB sources. src/network-web/basenetworkaccessmanager.cpp diff --git a/src/gui/labelwithstatus.cpp b/src/gui/labelwithstatus.cpp old mode 100644 new mode 100755 index 5f15541b3..90db3a608 --- a/src/gui/labelwithstatus.cpp +++ b/src/gui/labelwithstatus.cpp @@ -26,6 +26,8 @@ LabelWithStatus::LabelWithStatus(QWidget *parent) : WidgetWithStatus(parent) { m_wdgInput = new QLabel(this); + qobject_cast(m_wdgInput)->setWordWrap(true); + // Set correct size for the tool button. int label_height = m_wdgInput->sizeHint().height(); m_btnStatus->setFixedSize(label_height, label_height); diff --git a/src/services/tt-rss/definitions.h b/src/services/tt-rss/definitions.h index e26ce4ff7..c77b7ff11 100755 --- a/src/services/tt-rss/definitions.h +++ b/src/services/tt-rss/definitions.h @@ -1,6 +1,9 @@ #ifndef DEFINITIONS_H #define DEFINITIONS_H +#define MINIMAL_API_LEVEL 10 +#define CONTENT_TYPE "application/json; charset=utf-8" + // Error when user needs to login before making an operation. #define NOT_LOGGED_IN "NOT_LOGGED_IN" diff --git a/src/services/tt-rss/gui/formeditaccount.cpp b/src/services/tt-rss/gui/formeditaccount.cpp index 5d840ffce..d6ab35621 100755 --- a/src/services/tt-rss/gui/formeditaccount.cpp +++ b/src/services/tt-rss/gui/formeditaccount.cpp @@ -19,6 +19,8 @@ #include "services/tt-rss/gui/formeditaccount.h" #include "services/tt-rss/ttrssserviceroot.h" +#include "services/tt-rss/network/ttrssnetworkfactory.h" +#include "miscellaneous/iconfactory.h" FormEditAccount::FormEditAccount(QWidget *parent) @@ -26,13 +28,23 @@ FormEditAccount::FormEditAccount(QWidget *parent) m_ui->setupUi(this); m_btnOk = m_ui->m_buttonBox->button(QDialogButtonBox::Ok); + setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint); + setWindowIcon(qApp->icons()->fromTheme(QSL("application-ttrss"))); + m_ui->m_txtPassword->lineEdit()->setPlaceholderText(tr("Password for your TT-RSS account.")); m_ui->m_txtUsername->lineEdit()->setPlaceholderText(tr("Username for your TT-RSS account.")); - m_ui->m_txtUrl->lineEdit()->setPlaceholderText(tr("URL of your TT-RSS instance WITHOUT trailing \"/api/\" string.")); + m_ui->m_txtUrl->lineEdit()->setPlaceholderText(tr("FULL URL of your TT-RSS instance WITH trailing \"/api/\" string.")); m_ui->m_lblTestResult->setStatus(WidgetWithStatus::Information, tr("No test done yet."), tr("Here, results of connection test are shown.")); + setTabOrder(m_ui->m_txtUrl->lineEdit(), m_ui->m_txtUsername->lineEdit()); + setTabOrder(m_ui->m_txtUsername->lineEdit(), m_ui->m_txtPassword->lineEdit()); + setTabOrder(m_ui->m_txtPassword->lineEdit(), m_ui->m_checkShowPassword); + setTabOrder(m_ui->m_checkShowPassword, m_ui->m_btnTestSetup); + setTabOrder(m_ui->m_btnTestSetup, m_ui->m_buttonBox); + + connect(m_ui->m_checkShowPassword, SIGNAL(toggled(bool)), this, SLOT(displayPassword(bool))); connect(m_ui->m_buttonBox, SIGNAL(accepted()), this, SLOT(onClickedOk())); connect(m_ui->m_buttonBox, SIGNAL(rejected()), this, SLOT(onClickedCancel())); connect(m_ui->m_txtPassword->lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(onPasswordChanged())); @@ -47,6 +59,7 @@ FormEditAccount::FormEditAccount(QWidget *parent) onUsernameChanged(); onUrlChanged(); checkOkButton(); + displayPassword(false); } FormEditAccount::~FormEditAccount() { @@ -65,8 +78,27 @@ void FormEditAccount::execForEdit(TtRssServiceRoot *existing_root) { exec(); } -void FormEditAccount::performTest() { +void FormEditAccount::displayPassword(bool display) { + m_ui->m_txtPassword->lineEdit()->setEchoMode(display ? QLineEdit::Normal : QLineEdit::Password); +} +void FormEditAccount::performTest() { + TtRssNetworkFactory factory; + + factory.setUsername(m_ui->m_txtUsername->lineEdit()->text()); + factory.setPassword(m_ui->m_txtPassword->lineEdit()->text()); + factory.setUrl(m_ui->m_txtUrl->lineEdit()->text()); + + LoginResult result = factory.login(); + + if (result.first == QNetworkReply::NoError) { + + } + else { + m_ui->m_lblTestResult->setStatus(WidgetWithStatus::Error, + tr("Network error, have you entered correct Tiny Tiny RSS API endpoint?"), + tr("Network error, have you entered correct Tiny Tiny RSS API endpoint?")); + } } void FormEditAccount::onClickedOk() { diff --git a/src/services/tt-rss/gui/formeditaccount.h b/src/services/tt-rss/gui/formeditaccount.h index 6de37e3db..5e278ec6a 100755 --- a/src/services/tt-rss/gui/formeditaccount.h +++ b/src/services/tt-rss/gui/formeditaccount.h @@ -41,6 +41,7 @@ class FormEditAccount : public QDialog { void execForEdit(TtRssServiceRoot *existing_root); private slots: + void displayPassword(bool display); void performTest(); void onClickedOk(); void onClickedCancel(); diff --git a/src/services/tt-rss/gui/formeditaccount.ui b/src/services/tt-rss/gui/formeditaccount.ui index 76da5638e..c604bac7e 100755 --- a/src/services/tt-rss/gui/formeditaccount.ui +++ b/src/services/tt-rss/gui/formeditaccount.ui @@ -6,7 +6,7 @@ 0 0 - 400 + 465 235 @@ -16,19 +16,6 @@ - - - - URL - - - m_txtUrl - - - - - - @@ -36,6 +23,19 @@ + + + + + 0 + 0 + + + + Qt::RightToLeft + + + @@ -77,21 +77,32 @@ + + + + Show password + + + - - - - - 0 - 0 - - - - Qt::RightToLeft - - + + + + + + URL + + + m_txtUrl + + + + + + + diff --git a/src/services/tt-rss/network/ttrssnetworkfactory.cpp b/src/services/tt-rss/network/ttrssnetworkfactory.cpp new file mode 100755 index 000000000..8c300cc77 --- /dev/null +++ b/src/services/tt-rss/network/ttrssnetworkfactory.cpp @@ -0,0 +1,142 @@ +// This file is part of RSS Guard. +// +// Copyright (C) 2011-2015 by Martin Rotter +// +// RSS Guard is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RSS Guard 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 RSS Guard. If not, see . + +#include "services/tt-rss/network/ttrssnetworkfactory.h" + +#include "definitions/definitions.h" +#include "services/tt-rss/definitions.h" +#include "network-web/networkfactory.h" + + +TtRssNetworkFactory::TtRssNetworkFactory() : m_url(QString()) { +} + +TtRssNetworkFactory::~TtRssNetworkFactory() { +} + +QString TtRssNetworkFactory::url() const { + return m_url; +} + +void TtRssNetworkFactory::setUrl(const QString &url) { + m_url = url; +} + +QString TtRssNetworkFactory::username() const { + return m_username; +} + +void TtRssNetworkFactory::setUsername(const QString &username) { + m_username = username; +} + +QString TtRssNetworkFactory::password() const { + return m_password; +} + +void TtRssNetworkFactory::setPassword(const QString &password) { + m_password = password; +} + +LoginResult TtRssNetworkFactory::login() { + QtJson::JsonObject json; + json["op"] = "login"; + json["user"] = m_username; + json["password"] = m_password; + + QByteArray result; + NetworkResult res = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result); + + if (res.first != QNetworkReply::NoError) { + return LoginResult(res.first, TtRssLoginResponse()); + } + else { + return LoginResult(res.first, TtRssLoginResponse(QString::fromUtf8(result))); + } +} + +TtRssResponse::TtRssResponse(const QString &raw_content) { + m_rawContent = QtJson::parse(raw_content).toMap(); +} + +TtRssResponse::~TtRssResponse() { +} + +bool TtRssResponse::isLoaded() const { + return !m_rawContent.empty(); +} + +int TtRssResponse::seq() const { + if (!isLoaded()) { + return -1; + } + else { + return m_rawContent["seq"].toInt(); + } +} + +int TtRssResponse::status() const { + if (!isLoaded()) { + return -1; + } + else { + return m_rawContent["status"].toInt(); + } +} + + +TtRssLoginResponse::TtRssLoginResponse(const QString &raw_content) : TtRssResponse(raw_content) { +} + +TtRssLoginResponse::~TtRssLoginResponse() { +} + +int TtRssLoginResponse::apiLevel() const { + if (!isLoaded()) { + return -1; + } + else { + return m_rawContent["content"].toMap()["api_level"].toInt(); + } +} + +QString TtRssLoginResponse::sessionId() const { + if (!isLoaded()) { + return QString(); + } + else { + return m_rawContent["content"].toMap()["session_id"].toString(); + } +} + +QString TtRssLoginResponse::error() const { + if (!isLoaded()) { + return QString(); + } + else { + return m_rawContent["content"].toMap()["error"].toString(); + } +} + +bool TtRssLoginResponse::hasError() const { + if (!isLoaded()) { + return false; + } + else { + return m_rawContent["content"].toMap().contains("error"); + } +} diff --git a/src/services/tt-rss/network/ttrssnetworkfactory.h b/src/services/tt-rss/network/ttrssnetworkfactory.h new file mode 100755 index 000000000..52f559ec5 --- /dev/null +++ b/src/services/tt-rss/network/ttrssnetworkfactory.h @@ -0,0 +1,78 @@ +// This file is part of RSS Guard. +// +// Copyright (C) 2011-2015 by Martin Rotter +// +// RSS Guard is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RSS Guard 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 RSS Guard. If not, see . + +#ifndef TTRSSNETWORKFACTORY_H +#define TTRSSNETWORKFACTORY_H + +#include "qt-json/json.h" + +#include +#include +#include + + +class TtRssResponse { + public: + explicit TtRssResponse(const QString &raw_content = QString()); + virtual ~TtRssResponse(); + + bool isLoaded() const; + + int seq() const; + int status() const; + + protected: + QtJson::JsonObject m_rawContent; +}; + +class TtRssLoginResponse : public TtRssResponse { + public: + explicit TtRssLoginResponse(const QString &raw_content = QString()); + virtual ~TtRssLoginResponse(); + + int apiLevel() const; + QString sessionId() const; + QString error() const; + bool hasError() const; +}; + +typedef QPair LoginResult; + +class TtRssNetworkFactory { + public: + explicit TtRssNetworkFactory(); + virtual ~TtRssNetworkFactory(); + + QString url() const; + void setUrl(const QString &url); + + QString username() const; + void setUsername(const QString &username); + + QString password() const; + void setPassword(const QString &password); + + // Operations. + LoginResult login(); + + private: + QString m_url; + QString m_username; + QString m_password; +}; + +#endif // TTRSSNETWORKFACTORY_H