Work on adding feeds.

This commit is contained in:
Martin Rotter 2015-12-21 08:45:31 +01:00
parent 8f5dec672b
commit 806a5a0475
6 changed files with 122 additions and 2 deletions

View File

@ -31,4 +31,13 @@
// Get feed tree.
#define GFT_TYPE_CATEGORY "category"
// Subscribe to feed.
#define STF_UNKNOWN -1
#define STF_UPDATED 0
#define STF_INVALID_URL 2
#define STF_UNREACHABLE_URL 5
#define STF_URL_NO_FEED 3
#define STF_URL_MANY_FEEDS 4
#define STF_INSERTED 1
#endif // DEFINITIONS_H

View File

@ -198,9 +198,23 @@ void FormEditFeed::saveFeed() {
}
void FormEditFeed::addNewFeed() {
// Store feed online and if successfull, then store into DB/model.
TtRssFeed *new_feed= new TtRssFeed();
RootItem *parent = static_cast<RootItem*>(m_ui->m_cmbParentCategory->itemData(m_ui->m_cmbParentCategory->currentIndex()).value<void*>());
// TODO: todo
new_feed->setAutoUpdateType(static_cast<Feed::AutoUpdateType>(m_ui->m_cmbAutoUpdateType->itemData(m_ui->m_cmbAutoUpdateType->currentIndex()).toInt()));
new_feed->setAutoUpdateInitialInterval(m_ui->m_spinAutoUpdateInterval->value());
if (new_feed->addItself(parent, m_ui->m_txtUrl->lineEdit()->text(), m_ui->m_gbAuthentication->isChecked(),
m_ui->m_txtUsername->lineEdit()->text(), m_ui->m_txtPassword->lineEdit()->text())) {
m_root->requestItemReassignment(new_feed, parent);
accept();
}
else {
delete new_feed;
qApp->showGuiMessage(tr("Cannot add feed"),
tr("Feed was not added due to error."),
QSystemTrayIcon::Critical, this, true);
}
}
void FormEditFeed::loadCategories(const QList<Category*> categories, RootItem *root_item) {

View File

@ -28,6 +28,7 @@
#include "network-web/networkfactory.h"
#include <QPair>
#include <QVariant>
TtRssNetworkFactory::TtRssNetworkFactory()
@ -229,6 +230,43 @@ TtRssUpdateArticleResponse TtRssNetworkFactory::updateArticles(const QStringList
return result;
}
TtRssSubscribeToFeedResponse TtRssNetworkFactory::subscribeToFeed(const QString &url, int category_id,
bool protectd, const QString &username,
const QString &password) {
QtJson::JsonObject json;
json["op"] = "subscribeToFeed";
json["sid"] = m_sessionId;
json["feed_url"] = url;
json["category_id"] = category_id;
if (protectd) {
json["login"] = username;
json["password"] = password;
}
QByteArray result_raw;
NetworkResult network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw,
m_authIsUsed, m_authUsername, m_authPassword);
TtRssSubscribeToFeedResponse result(QString::fromUtf8(result_raw));
if (result.isNotLoggedIn()) {
// We are not logged in.
login();
json["sid"] = m_sessionId;
network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw,
m_authIsUsed, m_authUsername, m_authPassword);
result = TtRssSubscribeToFeedResponse(QString::fromUtf8(result_raw));
}
if (network_reply.first != QNetworkReply::NoError) {
qWarning("TT-RSS: updateArticle failed with error %d.", network_reply.first);
}
m_lastError = network_reply.first;
return result;
}
/*
TtRssGetConfigResponse TtRssNetworkFactory::getConfig() {
QtJson::JsonObject json;
@ -533,3 +571,20 @@ TtRssGetConfigResponse::TtRssGetConfigResponse(const QString &raw_content) : TtR
TtRssGetConfigResponse::~TtRssGetConfigResponse() {
}
*/
TtRssSubscribeToFeedResponse::TtRssSubscribeToFeedResponse(const QString &raw_content) : TtRssResponse(raw_content) {
}
TtRssSubscribeToFeedResponse::~TtRssSubscribeToFeedResponse() {
}
int TtRssSubscribeToFeedResponse::code() const {
if (m_rawContent.contains(QSL("content"))) {
return m_rawContent["content"].toMap()["code"].toInt();
}
else {
return STF_UNKNOWN;
}
}

View File

@ -28,6 +28,7 @@
class RootItem;
class TtRssFeed;
class TtRssResponse {
public:
@ -83,6 +84,14 @@ class TtRssUpdateArticleResponse : public TtRssResponse {
int articlesUpdated() const;
};
class TtRssSubscribeToFeedResponse : public TtRssResponse {
public:
explicit TtRssSubscribeToFeedResponse(const QString &raw_content = QString());
virtual ~TtRssSubscribeToFeedResponse();
int code() const;
};
/*
class TtRssGetConfigResponse : public TtRssResponse {
public:
@ -156,6 +165,9 @@ class TtRssNetworkFactory {
TtRssUpdateArticleResponse updateArticles(const QStringList &ids, UpdateArticle::OperatingField field,
UpdateArticle::Mode mode);
TtRssSubscribeToFeedResponse subscribeToFeed(const QString &url, int category_id, bool protectd = false,
const QString &username = QString(), const QString &password = QString());
//TtRssGetConfigResponse getConfig();
private:

View File

@ -25,6 +25,7 @@
#include "gui/dialogs/formmain.h"
#include "services/tt-rss/definitions.h"
#include "services/tt-rss/ttrssserviceroot.h"
#include "services/tt-rss/ttrsscategory.h"
#include "services/tt-rss/gui/formeditfeed.h"
#include "services/tt-rss/network/ttrssnetworkfactory.h"
@ -237,6 +238,33 @@ void TtRssFeed::setCustomId(int custom_id) {
m_customId = custom_id;
}
bool TtRssFeed::addItself(RootItem *parent, const QString &url, bool protectd,
const QString &username, const QString &password) {
TtRssServiceRoot *root = parent->kind() == RootItemKind::Category ?
qobject_cast<TtRssCategory*>(parent)->serviceRoot() :
qobject_cast<TtRssServiceRoot*>(parent);
int category_id = parent->kind() == RootItemKind::ServiceRoot ?
0 :
qobject_cast<TtRssCategory*>(parent)->customId();
TtRssSubscribeToFeedResponse response = root->network()->subscribeToFeed(url, category_id, protectd, username, password);
if (response.code() == STF_INSERTED || response.code() == STF_UPDATED) {
// Feed added on TT-RSS server, get its ID.
// TODO: ted potrebujeme vyplnit customID toho přidaného kanálu
// asi přes getFeeds
// TODO: todo
return true;
}
else {
return false;
}
}
bool TtRssFeed::editItself(TtRssFeed *new_feed_data) {
QSqlDatabase database = qApp->database()->connection("aa", DatabaseFactory::FromSettings);
QSqlQuery query_update(database);

View File

@ -54,6 +54,8 @@ class TtRssFeed : public Feed {
int customId() const;
void setCustomId(int custom_id);
bool addItself(RootItem *parent, const QString &url, bool protectd = false,
const QString &username = QString(), const QString &password = QString());
bool editItself(TtRssFeed *new_feed_data);
private: