Added some initial network methods for adding/removed TT-RSS feeds.

This commit is contained in:
Martin Rotter 2015-12-22 07:09:41 +01:00
parent 806a5a0475
commit 284768c93f
8 changed files with 142 additions and 18 deletions

View File

@ -20,12 +20,9 @@
#include "definitions/definitions.h"
Feed::Feed(RootItem *parent) : RootItem(parent) {
m_status = Normal;
m_autoUpdateType = DefaultAutoUpdate;
m_autoUpdateInitialInterval = DEFAULT_AUTO_UPDATE_INTERVAL;
m_autoUpdateRemainingInterval = DEFAULT_AUTO_UPDATE_INTERVAL;
Feed::Feed(RootItem *parent)
: RootItem(parent), m_url(QString()), m_status(Normal), m_autoUpdateType(DefaultAutoUpdate),
m_autoUpdateInitialInterval(DEFAULT_AUTO_UPDATE_INTERVAL), m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL) {
setKind(RootItemKind::Feed);
}

View File

@ -86,7 +86,16 @@ class Feed : public RootItem {
m_status = status;
}
inline QString url() const {
return m_url;
}
inline void setUrl(const QString &url) {
m_url = url;
}
private:
QString m_url;
Status m_status;
AutoUpdateType m_autoUpdateType;
int m_autoUpdateInitialInterval;

View File

@ -54,7 +54,6 @@ StandardFeed::StandardFeed(RootItem *parent_item)
m_totalCount = 0;
m_unreadCount = 0;
m_encoding = QString();
m_url = QString();
}
StandardFeed::StandardFeed(const StandardFeed &other)
@ -67,8 +66,8 @@ StandardFeed::StandardFeed(const StandardFeed &other)
m_totalCount = other.countOfAllMessages();
m_unreadCount = other.countOfUnreadMessages();
m_encoding = other.encoding();
m_url = other.url();
setUrl(other.url());
setStatus(other.status());
setAutoUpdateType(other.autoUpdateType());
setAutoUpdateInitialInterval(other.autoUpdateInitialInterval());

View File

@ -138,14 +138,6 @@ class StandardFeed : public Feed {
m_encoding = encoding;
}
inline QString url() const {
return m_url;
}
inline void setUrl(const QString &url) {
m_url = url;
}
QNetworkReply::NetworkError networkError() const;
// Tries to guess feed hidden under given URL
@ -179,7 +171,6 @@ class StandardFeed : public Feed {
int m_unreadCount;
QString m_encoding;
QString m_url;
};
Q_DECLARE_METATYPE(StandardFeed::Type)

View File

@ -40,4 +40,8 @@
#define STF_URL_MANY_FEEDS 4
#define STF_INSERTED 1
// Unsubscribe from feed.
#define UFF_FEED_NOT_FOUND "FEED_NOT_FOUND"
#define UFF_OK "OK"
#endif // DEFINITIONS_H

View File

@ -267,6 +267,64 @@ TtRssSubscribeToFeedResponse TtRssNetworkFactory::subscribeToFeed(const QString
return result;
}
TtRssGetFeedsResponse TtRssNetworkFactory::getFeeds(int category_id) {
QtJson::JsonObject json;
json["op"] = "getFeeds";
json["sid"] = m_sessionId;
json["cat_id"] = category_id;
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);
TtRssGetFeedsResponse 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 = TtRssGetFeedsResponse(QString::fromUtf8(result_raw));
}
if (network_reply.first != QNetworkReply::NoError) {
qWarning("TT-RSS: getFeeds failed with error %d.", network_reply.first);
}
m_lastError = network_reply.first;
return result;
}
TtRssUnsubscribeFeedResponse TtRssNetworkFactory::unsubscribeFeed(int feed_id) {
QtJson::JsonObject json;
json["op"] = "unsubscribeFeed";
json["sid"] = m_sessionId;
json["cat_id"] = feed_id;
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);
TtRssUnsubscribeFeedResponse 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 = TtRssUnsubscribeFeedResponse(QString::fromUtf8(result_raw));
}
if (network_reply.first != QNetworkReply::NoError) {
qWarning("TT-RSS: getFeeds failed with error %d.", network_reply.first);
}
m_lastError = network_reply.first;
return result;
}
/*
TtRssGetConfigResponse TtRssNetworkFactory::getConfig() {
QtJson::JsonObject json;
@ -588,3 +646,49 @@ int TtRssSubscribeToFeedResponse::code() const {
return STF_UNKNOWN;
}
}
TtRssUnsubscribeFeedResponse::TtRssUnsubscribeFeedResponse(const QString &raw_content) : TtRssResponse(raw_content) {
}
TtRssUnsubscribeFeedResponse::~TtRssUnsubscribeFeedResponse() {
}
QString TtRssUnsubscribeFeedResponse::code() const {
if (m_rawContent.contains(QSL("content"))) {
QVariantMap map = m_rawContent["content"].toMap();
if (map.contains(QSL("error"))) {
return map["error"].toString();
}
else if (map.contains(QSL("status"))) {
return map["status"].toString();
}
}
return QString();
}
TtRssGetFeedsResponse::TtRssGetFeedsResponse(const QString &raw_content) {
}
TtRssGetFeedsResponse::~TtRssGetFeedsResponse() {
}
QList<TtRssFeed*> TtRssGetFeedsResponse::feeds() const {
QList<TtRssFeed*> feeds;
foreach (QVariant feed_var, m_rawContent["content"].toList()) {
QVariantMap feed_map = feed_var.toMap();
TtRssFeed *feed = new TtRssFeed();
feed->setTitle(feed_map["title"].toString());
feed->setUrl(feed_map["feed_url"].toString());
feed->setCustomId(feed_map["id"].toInt());
feeds.append(feed);
}
return feeds;
}

View File

@ -92,6 +92,23 @@ class TtRssSubscribeToFeedResponse : public TtRssResponse {
int code() const;
};
class TtRssUnsubscribeFeedResponse : public TtRssResponse {
public:
explicit TtRssUnsubscribeFeedResponse(const QString &raw_content = QString());
virtual ~TtRssUnsubscribeFeedResponse();
QString code() const;
};
class TtRssGetFeedsResponse : public TtRssResponse {
public:
explicit TtRssGetFeedsResponse(const QString &raw_content = QString());
virtual ~TtRssGetFeedsResponse();
QList<TtRssFeed*> feeds() const;
};
/*
class TtRssGetConfigResponse : public TtRssResponse {
public:
@ -168,6 +185,10 @@ class TtRssNetworkFactory {
TtRssSubscribeToFeedResponse subscribeToFeed(const QString &url, int category_id, bool protectd = false,
const QString &username = QString(), const QString &password = QString());
TtRssGetFeedsResponse getFeeds(int category_id);
TtRssUnsubscribeFeedResponse unsubscribeFeed(int feed_id);
//TtRssGetConfigResponse getConfig();
private:

View File

@ -246,7 +246,6 @@ bool TtRssFeed::addItself(RootItem *parent, const QString &url, bool protectd,
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) {