Added method to feed interface to obtain all undeleted messages.

This commit is contained in:
Martin Rotter 2015-11-04 06:56:26 +01:00
parent d89787741c
commit 7dd09cfd89
8 changed files with 135 additions and 103 deletions

View File

@ -413,6 +413,7 @@ set(APP_SOURCES
src/core/parsingfactory.cpp
src/core/feeddownloader.cpp
src/core/feedsselection.cpp
src/core/message.cpp
# ABSTRACT service sources.
src/services/abstract/serviceentrypoint.cpp

View File

@ -305,30 +305,8 @@ QList<Feed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {
QList<Message> FeedsModel::messagesForFeeds(const QList<Feed*> &feeds) {
QList<Message> messages;
QSqlDatabase database = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings);
QSqlQuery query_read_msg(database);
query_read_msg.setForwardOnly(true);
query_read_msg.prepare("SELECT title, url, author, date_created, contents "
"FROM Messages "
"WHERE is_deleted = 0 AND feed = :feed;");
foreach (Feed *feed, feeds) {
query_read_msg.bindValue(QSL(":feed"), feed->id());
if (query_read_msg.exec()) {
while (query_read_msg.next()) {
Message message;
message.m_feedId = feed->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();
messages.append(message);
}
}
foreach (const Feed *feed, feeds) {
messages.append(feed->undeletedMessages());
}
return messages;

50
src/core/message.cpp Executable file
View File

@ -0,0 +1,50 @@
#include "core/message.h"
Enclosure::Enclosure(const QString &url, const QString &mime) : m_url(url), m_mimeType(mime) {
}
QList<Enclosure> Enclosures::decodeEnclosuresFromString(const QString &enclosures_data) {
QList<Enclosure> enclosures;
foreach (const QString &single_enclosure, enclosures_data.split(ENCLOSURES_OUTER_SEPARATOR, QString::SkipEmptyParts)) {
Enclosure enclosure;
if (single_enclosure.contains(ECNLOSURES_INNER_SEPARATOR)) {
QStringList mime_url = single_enclosure.split(ECNLOSURES_INNER_SEPARATOR);
enclosure.m_mimeType = QByteArray::fromBase64(mime_url.at(0).toLocal8Bit());
enclosure.m_url = QByteArray::fromBase64(mime_url.at(1).toLocal8Bit());
}
else {
enclosure.m_url = QByteArray::fromBase64(single_enclosure.toLocal8Bit());
}
enclosures.append(enclosure);
}
return enclosures;
}
QString Enclosures::encodeEnclosuresToString(const QList<Enclosure> &enclosures) {
QStringList enclosures_str;
foreach (const Enclosure &enclosure, enclosures) {
if (enclosure.m_mimeType.isEmpty()) {
enclosures_str.append(enclosure.m_url.toLocal8Bit().toBase64());
}
else {
enclosures_str.append(QString(enclosure.m_mimeType.toLocal8Bit().toBase64()) +
ECNLOSURES_INNER_SEPARATOR +
enclosure.m_url.toLocal8Bit().toBase64());
}
}
return enclosures_str.join(QString(ENCLOSURES_OUTER_SEPARATOR));
}
Message::Message() {
m_title = m_url = m_author = m_contents = "";
m_feedId = 0;
m_enclosures = QList<Enclosure>();
}

44
src/core/message.h Executable file
View File

@ -0,0 +1,44 @@
#ifndef MESSAGE_H
#define MESSAGE_H
#include "definitions/definitions.h"
#include <QDateTime>
#include <QStringList>
// Represents single enclosuresh
struct Enclosure {
QString m_url;
QString m_mimeType;
explicit Enclosure(const QString &url = QString(), const QString &mime = QString());
};
// Represents single enclosure.
class Enclosures {
public:
static QList<Enclosure> decodeEnclosuresFromString(const QString &enclosures_data);
static QString encodeEnclosuresToString(const QList<Enclosure> &enclosures);
};
// Represents single message.
class Message {
public:
explicit Message();
QString m_title;
QString m_url;
QString m_author;
QString m_contents;
QDateTime m_created;
int m_feedId;
QList<Enclosure> m_enclosures;
// Is true if "created" date was obtained directly
// from the feed, otherwise is false
bool m_createdFromFeed;
};
#endif // MESSAGE_H

View File

@ -21,89 +21,13 @@
#include "definitions/definitions.h"
#include "core/feedsselection.h"
#include "core/message.h"
#include <QSqlTableModel>
#include <QFont>
#include <QIcon>
#include <QDateTime>
// Represents single enclosuresh
struct Enclosure {
QString m_url;
QString m_mimeType;
explicit Enclosure(const QString &url = QString(), const QString &mime = QString()) : m_url(url), m_mimeType(mime) {
}
};
// Represents single enclosure.
class Enclosures {
public:
static QList<Enclosure> decodeEnclosuresFromString(const QString &enclosures_data) {
QList<Enclosure> enclosures;
foreach (const QString &single_enclosure, enclosures_data.split(ENCLOSURES_OUTER_SEPARATOR, QString::SkipEmptyParts)) {
Enclosure enclosure;
if (single_enclosure.contains(ECNLOSURES_INNER_SEPARATOR)) {
QStringList mime_url = single_enclosure.split(ECNLOSURES_INNER_SEPARATOR);
enclosure.m_mimeType = QByteArray::fromBase64(mime_url.at(0).toLocal8Bit());
enclosure.m_url = QByteArray::fromBase64(mime_url.at(1).toLocal8Bit());
}
else {
enclosure.m_url = QByteArray::fromBase64(single_enclosure.toLocal8Bit());
}
enclosures.append(enclosure);
}
return enclosures;
}
static QString encodeEnclosuresToString(const QList<Enclosure> &enclosures) {
QStringList enclosures_str;
foreach (const Enclosure &enclosure, enclosures) {
if (enclosure.m_mimeType.isEmpty()) {
enclosures_str.append(enclosure.m_url.toLocal8Bit().toBase64());
}
else {
enclosures_str.append(QString(enclosure.m_mimeType.toLocal8Bit().toBase64()) +
ECNLOSURES_INNER_SEPARATOR +
enclosure.m_url.toLocal8Bit().toBase64());
}
}
return enclosures_str.join(QString(ENCLOSURES_OUTER_SEPARATOR));
}
};
// Represents single message.
class Message {
public:
explicit Message() {
m_title = m_url = m_author = m_contents = "";
m_feedId = 0;
m_enclosures = QList<Enclosure>();
}
QString m_title;
QString m_url;
QString m_author;
QString m_contents;
QDateTime m_created;
int m_feedId;
QList<Enclosure> m_enclosures;
// Is true if "created" date was obtained directly
// from the feed, otherwise is false
bool m_createdFromFeed;
};
class MessagesModel : public QSqlTableModel {
Q_OBJECT

View File

@ -20,6 +20,8 @@
#include "core/rootitem.h"
#include "core/message.h"
// Base class for "feed" nodes.
class Feed : public RootItem {
@ -58,7 +60,8 @@ class Feed : public RootItem {
// Updates counts of all/unread messages for this feed.
virtual void updateCounts(bool including_total_count = true, bool update_feed_statuses = true) = 0;
// Get ALL undeleted messages from this feed in one single list.
virtual QList<Message> undeletedMessages() const = 0;
inline int autoUpdateInitialInterval() const {
return m_autoUpdateInitialInterval;

View File

@ -110,6 +110,36 @@ void StandardFeed::editViaDialog() {
*/
}
QList<Message> StandardFeed::undeletedMessages() const {
QList<Message> messages;
QSqlDatabase database = qApp->database()->connection("StandardFeed", DatabaseFactory::FromSettings);
QSqlQuery query_read_msg(database);
query_read_msg.setForwardOnly(true);
query_read_msg.prepare("SELECT title, url, author, date_created, contents "
"FROM Messages "
"WHERE is_deleted = 0 AND feed = :feed;");
query_read_msg.bindValue(QSL(":feed"), id());
if (query_read_msg.exec()) {
while (query_read_msg.next()) {
Message message;
message.m_feedId = 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();
messages.append(message);
}
}
return messages;
}
QString StandardFeed::typeToString(StandardFeed::Type type) {
switch (type) {
case Atom10:

View File

@ -68,6 +68,8 @@ class StandardFeed : public Feed {
void editViaDialog();
QList<Message> undeletedMessages() const;
// Obtains data related to this feed.
QVariant data(int column, int role) const;
@ -157,7 +159,7 @@ class StandardFeed : public Feed {
private:
void init();
private:
private:
bool m_passwordProtected;
QString m_username;
QString m_password;