Working on TT-RSS messages obtaining.

This commit is contained in:
Martin Rotter 2015-12-08 13:37:26 +01:00
parent c9bb406a50
commit 34e2f70219
9 changed files with 137 additions and 7 deletions

View File

@ -61,7 +61,7 @@ QString Enclosures::encodeEnclosuresToString(const QList<Enclosure> &enclosures)
}
Message::Message() {
m_title = m_url = m_author = m_contents = m_customId = "";
m_feedId = 0;
m_title = m_url = m_author = m_contents = m_feedId = m_customId = "";
m_enclosures = QList<Enclosure>();
m_isImportant = m_isImportant = false;
}

View File

@ -49,9 +49,12 @@ class Message {
QString m_author;
QString m_contents;
QDateTime m_created;
int m_feedId;
QString m_feedId;
QString m_customId;
bool m_isRead;
bool m_isImportant;
QList<Enclosure> m_enclosures;
// Is true if "created" date was obtained directly

View File

@ -139,7 +139,8 @@ Message MessagesModel::messageAt(int row_index) const {
message.m_enclosures = Enclosures::decodeEnclosuresFromString(rec.value(MSG_DB_ENCLOSURES_INDEX).toString());
message.m_title = rec.value(MSG_DB_TITLE_INDEX).toString();
message.m_url = rec.value(MSG_DB_URL_INDEX).toString();
message.m_feedId = rec.value(MSG_DB_FEED_INDEX).toInt();
message.m_feedId = rec.value(MSG_DB_FEED_INDEX).toString();
message.m_customId = rec.value(MSG_DB_CUSTOM_ID_INDEX).toString();
message.m_created = TextFactory::parseDateTime(rec.value(MSG_DB_DCREATED_INDEX).value<qint64>()).toLocalTime();
return message;

View File

@ -55,6 +55,7 @@ class Feed : public RootItem {
/////////////////////////////////////////
// Performs synchronous update and returns number of newly updated messages.
// NOTE: This is called from worker thread, not from main UI thread.
// NOTE: This should COMPLETELY download ALL messages from online source
// into locale "Messages" table, INCLUDING contents (or excerpts) of those
// messages.

View File

@ -11,6 +11,9 @@
#define UNKNOWN_METHOD "UNKNOWN_METHOD" // Given "op" is not recognized.
#define INCORRECT_USAGE "INCORRECT_USAGE" // Given "op" was used with bad parameters.
// Limitations
#define MAX_MESSAGES 200
// General return status codes.
#define API_STATUS_OK 0
#define API_STATUS_ERR 1

View File

@ -24,6 +24,7 @@
#include "services/tt-rss/ttrsscategory.h"
#include "miscellaneous/application.h"
#include "miscellaneous/iconfactory.h"
#include "miscellaneous/textfactory.h"
#include "network-web/networkfactory.h"
#include <QPair>
@ -133,6 +134,37 @@ TtRssGetFeedsCategoriesResponse TtRssNetworkFactory::getFeedsCategories(QNetwork
return result;
}
TtRssGetHeadlinesResponse TtRssNetworkFactory::getHeadlines(int feed_id, bool force_update, int limit, int skip,
bool show_content, bool include_attachments,
bool sanitize, QNetworkReply::NetworkError &error) {
QtJson::JsonObject json;
json["op"] = "getHeadlines";
json["sid"] = m_sessionId;
json["feed_id"] = feed_id;
json["force_update"] = force_update;
json["limit"] = limit;
json["skip"] = skip;
json["show_content"] = show_content;
json["include_attachments"] = include_attachments;
json["sanitize"] = sanitize;
QByteArray result_raw;
NetworkResult network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
TtRssGetHeadlinesResponse result(QString::fromUtf8(result_raw));
if (result.isNotLoggedIn()) {
// We are not logged in.
login(error);
json["sid"] = m_sessionId;
network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
result = TtRssGetHeadlinesResponse(QString::fromUtf8(result_raw));
}
error = network_reply.first;
return result;
}
TtRssResponse::TtRssResponse(const QString &raw_content) {
m_rawContent = QtJson::parse(raw_content).toMap();
}
@ -296,3 +328,46 @@ RootItem *TtRssGetFeedsCategoriesResponse::feedsCategories(bool obtain_icons, QS
return parent;
}
TtRssGetHeadlinesResponse::TtRssGetHeadlinesResponse(const QString &raw_content) : TtRssResponse(raw_content) {
}
TtRssGetHeadlinesResponse::~TtRssGetHeadlinesResponse() {
}
QList<Message> TtRssGetHeadlinesResponse::messages() {
QList<Message> messages;
foreach (QVariant item, m_rawContent["content"].toList()) {
QMap<QString,QVariant> mapped = item.toMap();
Message message;
message.m_author = mapped["author"].toString();
message.m_isRead = !mapped["unread"].toBool();
message.m_isImportant = mapped["marked"].toBool();
message.m_contents = mapped["content"].toString();
message.m_created = TextFactory::parseDateTime(mapped["updated"].value<qint64>());
message.m_createdFromFeed = true;
message.m_customId = mapped["id"].toString();
message.m_feedId = mapped["feed_id"].toString();
message.m_title = mapped["title"].toString();
message.m_url = mapped["link"].toString();
if (mapped.contains(QSL("attachments"))) {
// Process enclosures.
foreach (QVariant attachment, mapped["attachments"].toList()) {
QMap<QString,QVariant> mapped_attachemnt = attachment.toMap();
Enclosure enclosure;
enclosure.m_mimeType = mapped_attachemnt["content_type"].toString();
enclosure.m_url = mapped_attachemnt["content_url"].toString();
message.m_enclosures.append(enclosure);
}
}
messages.append(message);
}
return messages;
}

View File

@ -20,6 +20,8 @@
#include "qt-json/json.h"
#include "core/message.h"
#include <QString>
#include <QPair>
#include <QNetworkReply>
@ -64,6 +66,14 @@ class TtRssGetFeedsCategoriesResponse : public TtRssResponse {
RootItem *feedsCategories(bool obtain_icons, QString base_address = QString());
};
class TtRssGetHeadlinesResponse : public TtRssResponse {
public:
explicit TtRssGetHeadlinesResponse(const QString &raw_content = QString());
virtual ~TtRssGetHeadlinesResponse();
QList<Message> messages();
};
class TtRssNetworkFactory {
public:
explicit TtRssNetworkFactory();
@ -89,6 +99,11 @@ class TtRssNetworkFactory {
// Gets feeds from the server.
TtRssGetFeedsCategoriesResponse getFeedsCategories(QNetworkReply::NetworkError &error);
// Gets headlines (messages) from the server.
TtRssGetHeadlinesResponse getHeadlines(int feed_id, bool force_update, int limit, int skip,
bool show_content, bool include_attachments,
bool sanitize, QNetworkReply::NetworkError &error);
private:
QString m_url;
QString m_username;

View File

@ -22,7 +22,9 @@
#include "miscellaneous/databasefactory.h"
#include "miscellaneous/iconfactory.h"
#include "miscellaneous/textfactory.h"
#include "services/tt-rss/definitions.h"
#include "services/tt-rss/ttrssserviceroot.h"
#include "services/tt-rss/network/ttrssnetworkfactory.h"
#include <QSqlQuery>
@ -83,7 +85,30 @@ int TtRssFeed::countOfUnreadMessages() {
int TtRssFeed::update() {
// TODO: přes getHeadlines provede stažení kompletnich zprav.
return 0;
QNetworkReply::NetworkError error;
QList<Message> messages;
int newly_added_messages = 0;
int limit = MAX_MESSAGES;
int skip = 0;
do {
TtRssGetHeadlinesResponse headlines = serviceRoot()->network()->getHeadlines(customId(), true, limit, skip,
true, true, false, error);
if (error != QNetworkReply::NoError) {
setStatus(Feed::NetworkError);
return 0;
}
else {
QList<Message> new_messages = headlines.messages();
messages.append(new_messages);
skip += new_messages.size();
}
}
while (newly_added_messages > 0);
return updateMessages(messages);
}
QList<Message> TtRssFeed::undeletedMessages() const {
@ -92,7 +117,7 @@ QList<Message> TtRssFeed::undeletedMessages() const {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query_read_msg(database);
query_read_msg.setForwardOnly(true);
query_read_msg.prepare("SELECT title, url, author, date_created, contents, enclosures "
query_read_msg.prepare("SELECT title, url, author, date_created, contents, enclosures, custom_id "
"FROM Messages "
"WHERE is_deleted = 0 AND feed = :feed AND account_id = :account_id;");
@ -105,13 +130,14 @@ QList<Message> TtRssFeed::undeletedMessages() const {
while (query_read_msg.next()) {
Message message;
message.m_feedId = account_id;
message.m_feedId = QString::number(account_id);
message.m_title = query_read_msg.value(0).toString();
message.m_url = query_read_msg.value(1).toString();
message.m_author = query_read_msg.value(2).toString();
message.m_created = TextFactory::parseDateTime(query_read_msg.value(3).value<qint64>());
message.m_contents = query_read_msg.value(4).toString();
message.m_enclosures = Enclosures::decodeEnclosuresFromString(query_read_msg.value(5).toString());
message.m_customId = query_read_msg.value(6).toString();
messages.append(message);
}
@ -127,3 +153,7 @@ int TtRssFeed::customId() const {
void TtRssFeed::setCustomId(int custom_id) {
m_customId = custom_id;
}
int TtRssFeed::updateMessages(const QList<Message> &messages) {
return 0;
}

View File

@ -45,6 +45,8 @@ class TtRssFeed : public Feed {
void setCustomId(int custom_id);
private:
int updateMessages(const QList<Message> &messages);
int m_customId;
int m_totalCount;
int m_unreadCount;