diff --git a/src/core/databasefactory.cpp b/src/core/databasefactory.cpp index 24835b5de..bcaa686c1 100644 --- a/src/core/databasefactory.cpp +++ b/src/core/databasefactory.cpp @@ -119,7 +119,13 @@ QSqlDatabase DatabaseFactory::initialize(const QString &connection_name) { } QSqlDatabase DatabaseFactory::addConnection(const QString &connection_name) { - return initialize(connection_name); + if (!m_initialized) { + return initialize(connection_name); + } + else { + return QSqlDatabase::addDatabase(DATABASE_DRIVER, + connection_name); + } } QSqlDatabase DatabaseFactory::getConnection(const QString &connection_name) { diff --git a/src/core/databasefactory.h b/src/core/databasefactory.h index d8dc74e89..08585ef46 100644 --- a/src/core/databasefactory.h +++ b/src/core/databasefactory.h @@ -16,12 +16,16 @@ class DatabaseFactory : public QObject { // Assemblies database file path. void assemblyDatabaseFilePath(); - // Returns true if database was initialized, otherwise false. + // Creates new connection, initializes database and + // returns opened connection. QSqlDatabase initialize(const QString &connection_name); // Path to database file. QString m_databasePath; + // Is database file initialized? + bool m_initialized; + // Private singleton value. static QPointer s_instance; diff --git a/src/core/defs.h.in b/src/core/defs.h.in index d3ae1948b..8c55a093a 100644 --- a/src/core/defs.h.in +++ b/src/core/defs.h.in @@ -87,7 +87,7 @@ #define APP_FLAGS_PATH APP_PREFIX + QString("/share/rssguard/flags") #define APP_ICON_PATH APP_PREFIX + QString("/share/icons/hicolor/128x128/apps/@APP_LOW_NAME@.png") #define APP_ICON_PLAIN_PATH APP_PREFIX + QString("/share/icons/hicolor/128x128/apps/@APP_LOW_NAME@_plain.png") -#elif defined(Q_OS_WIN) +#elif defined(Q_OS_WIN) || defined(Q_OS_OS2) #define APP_LANG_PATH QApplication::applicationDirPath() + QString("/l10n") #define APP_SKIN_PATH QApplication::applicationDirPath() + QString("/skins") #define APP_INFO_PATH QApplication::applicationDirPath() diff --git a/src/core/messagesmodel.cpp b/src/core/messagesmodel.cpp index 6372a9f97..5c93e8f7e 100644 --- a/src/core/messagesmodel.cpp +++ b/src/core/messagesmodel.cpp @@ -12,7 +12,8 @@ MessagesModel::MessagesModel(QObject *parent) - : QSqlTableModel(parent, DatabaseFactory::getInstance()->addConnection("MessagesModel")) { + : QSqlTableModel(parent, + DatabaseFactory::getInstance()->addConnection("MessagesModel")) { setObjectName("MessagesModel"); setupFonts(); @@ -68,6 +69,10 @@ void MessagesModel::loadMessages(const QList feed_ids) { fetchAll(); } +int MessagesModel::messageId(int row_index) const { + return record(row_index).value(MSG_DB_ID_INDEX).toInt(); +} + Message MessagesModel::messageAt(int row_index) const { QSqlRecord rec = record(row_index); Message message; @@ -177,6 +182,54 @@ bool MessagesModel::switchMessageImportance(int row_index) { setData(target_index, 1); } +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) { + +} + QVariant MessagesModel::headerData(int section, Qt::Orientation orientation, int role) const { diff --git a/src/core/messagesmodel.h b/src/core/messagesmodel.h index fd955cf91..50252f473 100644 --- a/src/core/messagesmodel.h +++ b/src/core/messagesmodel.h @@ -46,13 +46,24 @@ class MessagesModel : public QSqlTableModel { // Returns message at given index. Message messageAt(int row_index) const; + int messageId(int row_index) const; public slots: - // Message manipulators. + // CORE messages manipulators. + // NOTE: These are used to change properties of one message. + // NOTE: Model is NOT reset after these methods are applied. bool switchMessageImportance(int row_index); bool setMessageDeleted(int row_index, int deleted); bool setMessageRead(int row_index, int read); + // BATCH messages manipulators. + // NOTE: These methods are used for changing of attributes of + // many messages via DIRECT SQL calls. + // NOTE: Model is reset after these methods is applied. + bool switchBatchMessageImportance(const QModelIndexList &messages); + bool setBatchMessagesDeleted(const QModelIndexList &messages, int deleted); + bool setBatchMessagesRead(const QModelIndexList &messages, int read); + // Fetches ALL available data to the model. // NOTE: This is almost always needed when sorting // and makes the model more predictable.