2013-11-17 16:41:44 +01:00
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
|
|
|
#include "qtsingleapplication/qtsingleapplication.h"
|
|
|
|
|
|
|
|
#include "core/defs.h"
|
2013-11-19 21:25:55 +01:00
|
|
|
#include "core/datetime.h"
|
2013-11-09 19:04:00 +01:00
|
|
|
#include "core/messagesmodel.h"
|
2013-11-15 22:09:38 +01:00
|
|
|
#include "core/databasefactory.h"
|
2013-11-17 16:41:44 +01:00
|
|
|
#include "gui/iconthemefactory.h"
|
2013-11-09 19:04:00 +01:00
|
|
|
|
|
|
|
|
2013-11-15 22:09:38 +01:00
|
|
|
MessagesModel::MessagesModel(QObject *parent)
|
2013-11-24 14:44:45 +01:00
|
|
|
: QSqlTableModel(parent,
|
|
|
|
DatabaseFactory::getInstance()->addConnection("MessagesModel")) {
|
2013-11-13 21:49:47 +01:00
|
|
|
setObjectName("MessagesModel");
|
2013-11-15 22:09:38 +01:00
|
|
|
|
2013-11-17 16:41:44 +01:00
|
|
|
setupFonts();
|
2013-11-18 21:45:15 +01:00
|
|
|
setupIcons();
|
2013-11-17 16:41:44 +01:00
|
|
|
setupHeaderData();
|
2013-11-18 21:45:15 +01:00
|
|
|
|
|
|
|
// Set desired table and edit strategy.
|
2013-11-20 21:54:30 +01:00
|
|
|
setEditStrategy(QSqlTableModel::OnManualSubmit);
|
2013-11-18 21:45:15 +01:00
|
|
|
setTable("Messages");
|
|
|
|
|
|
|
|
loadMessages(QList<int>());
|
2013-11-15 22:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MessagesModel::~MessagesModel() {
|
|
|
|
qDebug("Destroying MessagesModel instance.");
|
2013-11-13 21:49:47 +01:00
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
void MessagesModel::setupIcons() {
|
|
|
|
m_favoriteIcon = IconThemeFactory::getInstance()->fromTheme("mail-mark-important");
|
|
|
|
m_readIcon = IconThemeFactory::getInstance()->fromTheme("mail-mark-read");
|
|
|
|
m_unreadIcon = IconThemeFactory::getInstance()->fromTheme("mail-mark-unread");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessagesModel::fetchAll() {
|
|
|
|
while (canFetchMore()) {
|
|
|
|
fetchMore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-17 16:41:44 +01:00
|
|
|
void MessagesModel::setupFonts() {
|
|
|
|
m_normalFont = QtSingleApplication::font("MessagesView");
|
2013-11-19 21:25:55 +01:00
|
|
|
|
2013-11-17 16:41:44 +01:00
|
|
|
m_boldFont = m_normalFont;
|
|
|
|
m_boldFont.setBold(true);
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
void MessagesModel::loadMessages(const QList<int> feed_ids) {
|
2013-11-20 21:54:30 +01:00
|
|
|
// Submit changes first.
|
|
|
|
submitAll();
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
// Conversion of parameter.
|
2013-11-20 21:54:30 +01:00
|
|
|
m_currentFeeds = feed_ids;
|
2013-11-18 21:45:15 +01:00
|
|
|
QStringList stringy_ids;
|
|
|
|
stringy_ids.reserve(feed_ids.count());
|
|
|
|
|
|
|
|
foreach (int feed_id, feed_ids) {
|
|
|
|
stringy_ids.append(QString::number(feed_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: časem povolit.
|
2013-11-19 21:25:55 +01:00
|
|
|
//setFilter(QString("feed IN (%1) AND deleted = 0").arg(stringy_ids.join(',')));
|
2013-11-18 21:45:15 +01:00
|
|
|
select();
|
|
|
|
fetchAll();
|
|
|
|
}
|
|
|
|
|
2013-11-24 14:44:45 +01:00
|
|
|
int MessagesModel::messageId(int row_index) const {
|
|
|
|
return record(row_index).value(MSG_DB_ID_INDEX).toInt();
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:25:55 +01:00
|
|
|
Message MessagesModel::messageAt(int row_index) const {
|
|
|
|
QSqlRecord rec = record(row_index);
|
|
|
|
Message message;
|
|
|
|
|
2013-11-20 21:54:30 +01:00
|
|
|
// Fill Message object with details.
|
2013-11-19 21:25:55 +01:00
|
|
|
message.m_author = rec.value(MSG_DB_AUTHOR_INDEX).toString();
|
|
|
|
message.m_contents = rec.value(MSG_DB_CONTENTS_INDEX).toString();
|
|
|
|
message.m_title = rec.value(MSG_DB_TITLE_INDEX).toString();
|
|
|
|
message.m_url = rec.value(MSG_DB_URL_INDEX).toString();
|
|
|
|
message.m_updated = DateTime::fromString(rec.value(MSG_DB_DUPDATED_INDEX).toString());
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2013-11-13 21:49:47 +01:00
|
|
|
void MessagesModel::setupHeaderData() {
|
2013-11-18 21:45:15 +01:00
|
|
|
m_headerData << tr("Id") << tr("Read") << tr("Deleted") << tr("Important") <<
|
|
|
|
tr("Feed") << tr("Title") << tr("Url") << tr("Author") <<
|
|
|
|
tr("Created on") << tr("Updated on") << tr("Contents");
|
2013-11-15 22:09:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
Qt::ItemFlags MessagesModel::flags(const QModelIndex &idx) const {
|
|
|
|
Q_UNUSED(idx);
|
2013-11-15 22:09:38 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
if (m_isInEditingMode) {
|
|
|
|
// NOTE: Editing of model must be temporarily enabled here.
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
}
|
2013-11-13 21:49:47 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 21:54:30 +01:00
|
|
|
QVariant MessagesModel::data(int row, int column, int role) const {
|
|
|
|
return data(index(row, column), role);
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
QVariant MessagesModel::data(const QModelIndex &idx, int role) const {
|
|
|
|
switch (role) {
|
2013-11-24 18:42:37 +01:00
|
|
|
// Human readable data for viewing.
|
2013-11-19 21:25:55 +01:00
|
|
|
case Qt::DisplayRole: {
|
|
|
|
int index_column = idx.column();
|
|
|
|
if (index_column != MSG_DB_IMPORTANT_INDEX &&
|
|
|
|
index_column != MSG_DB_READ_INDEX) {
|
|
|
|
return QSqlTableModel::data(idx, role);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return RAW data for EditRole.
|
2013-11-18 21:45:15 +01:00
|
|
|
case Qt::EditRole:
|
|
|
|
return QSqlTableModel::data(idx, role);
|
2013-11-17 16:41:44 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
case Qt::FontRole:
|
|
|
|
return record(idx.row()).value(MSG_DB_READ_INDEX).toInt() == 1 ?
|
|
|
|
m_normalFont :
|
|
|
|
m_boldFont;
|
2013-11-17 16:41:44 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
case Qt::DecorationRole: {
|
|
|
|
int index_column = idx.column();
|
2013-11-17 16:41:44 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
if (index_column == MSG_DB_READ_INDEX) {
|
|
|
|
return record(idx.row()).value(MSG_DB_READ_INDEX).toInt() == 1 ?
|
|
|
|
m_readIcon :
|
|
|
|
m_unreadIcon;
|
|
|
|
}
|
2013-11-19 21:25:55 +01:00
|
|
|
else if (index_column == MSG_DB_IMPORTANT_INDEX) {
|
|
|
|
return record(idx.row()).value(MSG_DB_IMPORTANT_INDEX).toInt() == 1 ?
|
|
|
|
m_favoriteIcon :
|
|
|
|
QVariant();
|
|
|
|
}
|
2013-11-18 21:45:15 +01:00
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 16:41:44 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2013-11-17 16:41:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
bool MessagesModel::setData(const QModelIndex &idx, const QVariant &value, int role) {
|
|
|
|
m_isInEditingMode = true;
|
|
|
|
bool set_data_result = QSqlTableModel::setData(idx, value, role);
|
|
|
|
m_isInEditingMode = false;
|
2013-11-17 16:41:44 +01:00
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
return set_data_result;
|
2013-11-17 16:41:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 21:54:30 +01:00
|
|
|
bool MessagesModel::setMessageRead(int row_index, int read) {
|
|
|
|
return setData(index(row_index, MSG_DB_READ_INDEX),
|
|
|
|
read);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessagesModel::setMessageDeleted(int row_index, int deleted) {
|
|
|
|
return setData(index(row_index, MSG_DB_DELETED_INDEX),
|
|
|
|
deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessagesModel::switchMessageImportance(int row_index) {
|
|
|
|
QModelIndex target_index = index(row_index, MSG_DB_IMPORTANT_INDEX);
|
|
|
|
int current_importance = data(target_index).toInt();
|
|
|
|
|
|
|
|
return current_importance == 1 ?
|
|
|
|
setData(target_index, 0) :
|
|
|
|
setData(target_index, 1);
|
|
|
|
}
|
|
|
|
|
2013-11-24 14:44:45 +01:00
|
|
|
bool MessagesModel::switchBatchMessageImportance(const QModelIndexList &messages) {
|
|
|
|
// Submit changes first.
|
|
|
|
submitAll();
|
|
|
|
|
|
|
|
if (!database().transaction()) {
|
|
|
|
qWarning("Starting transaction for batch message importance switch failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlDatabase db_handle = database();
|
|
|
|
int message_id, importance;
|
|
|
|
QSqlQuery query_delete_msg(db_handle);
|
|
|
|
if (!query_delete_msg.prepare("UPDATE messages SET important = :important "
|
|
|
|
"WHERE id = :id")) {
|
|
|
|
qWarning("Query preparation failed for message importance switch.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QModelIndex &message, messages) {
|
|
|
|
message_id = messageId(message.row());
|
|
|
|
importance = record(message.row()).value(MSG_DB_IMPORTANT_INDEX).toInt();
|
|
|
|
|
|
|
|
query_delete_msg.bindValue(":id", message_id);
|
|
|
|
query_delete_msg.bindValue(":important",
|
|
|
|
importance == 1 ? 0 : 1);
|
|
|
|
query_delete_msg.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit changes.
|
|
|
|
if (db_handle.commit()) {
|
|
|
|
// FULLY reload the model if underlying data is changed.
|
|
|
|
select();
|
|
|
|
fetchAll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return db_handle.rollback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessagesModel::setBatchMessagesDeleted(const QModelIndexList &messages, int deleted) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessagesModel::setBatchMessagesRead(const QModelIndexList &messages, int read) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-13 21:49:47 +01:00
|
|
|
QVariant MessagesModel::headerData(int section,
|
|
|
|
Qt::Orientation orientation,
|
|
|
|
int role) const {
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2013-11-18 21:45:15 +01:00
|
|
|
// Display textual headers for all columns except "read" and
|
|
|
|
// "important" columns.
|
|
|
|
if (section != MSG_DB_READ_INDEX && section != MSG_DB_IMPORTANT_INDEX) {
|
2013-11-17 20:42:02 +01:00
|
|
|
return m_headerData.at(section);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
// Return RAW data for these roles.
|
2013-11-13 21:49:47 +01:00
|
|
|
case Qt::ToolTipRole:
|
2013-11-17 16:41:44 +01:00
|
|
|
case Qt::EditRole:
|
2013-11-13 21:49:47 +01:00
|
|
|
return m_headerData.at(section);
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
// Display icons for "read" and "important" columns.
|
2013-11-17 20:42:02 +01:00
|
|
|
case Qt::DecorationRole: {
|
|
|
|
switch (section) {
|
2013-11-18 21:45:15 +01:00
|
|
|
case MSG_DB_READ_INDEX:
|
2013-11-17 20:42:02 +01:00
|
|
|
return m_readIcon;
|
|
|
|
|
2013-11-18 21:45:15 +01:00
|
|
|
case MSG_DB_IMPORTANT_INDEX:
|
2013-11-17 20:42:02 +01:00
|
|
|
return m_favoriteIcon;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 21:49:47 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2013-11-09 19:04:00 +01:00
|
|
|
}
|