From 6abe99ed54bacebd7f742713636a4682664055d8 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 23 Nov 2015 19:31:03 +0100 Subject: [PATCH] Restoring of individual messages added. --- src/core/messagesmodel.cpp | 14 ++++-- src/core/messagesmodel.h | 4 +- src/gui/dialogs/formmain.cpp | 1 + src/gui/dialogs/formmain.ui | 5 ++ src/gui/feedmessageviewer.cpp | 2 + src/gui/messagesview.cpp | 46 ++++++++++++++++--- src/gui/messagesview.h | 1 + src/services/standard/standardserviceroot.cpp | 2 + 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/core/messagesmodel.cpp b/src/core/messagesmodel.cpp index 03f9a3ec3..61169c562 100755 --- a/src/core/messagesmodel.cpp +++ b/src/core/messagesmodel.cpp @@ -71,6 +71,8 @@ void MessagesModel::setupFonts() { m_boldFont.setBold(true); } + + void MessagesModel::loadMessages(RootItem *item) { m_selectedItem = item; @@ -110,6 +112,10 @@ RootItem::Importance MessagesModel::messageImportance(int row_index) const { return (RootItem::Importance) data(row_index, MSG_DB_IMPORTANT_INDEX, Qt::EditRole).toInt(); } +RootItem *MessagesModel::loadedItem() const { + return m_selectedItem; +} + void MessagesModel::updateDateFormat() { if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::UseCustomDate)).toBool()) { m_customDateFormat = qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomDateFormat)).toString(); @@ -360,7 +366,7 @@ bool MessagesModel::switchBatchMessageImportance(const QModelIndexList &messages } } -bool MessagesModel::setBatchMessagesDeleted(const QModelIndexList &messages, int deleted) { +bool MessagesModel::setBatchMessagesDeleted(const QModelIndexList &messages) { QStringList message_ids; QList message_ids_num; @@ -382,12 +388,10 @@ bool MessagesModel::setBatchMessagesDeleted(const QModelIndexList &messages, int query_read_msg.setForwardOnly(true); if (m_selectedItem->kind() != RootItemKind::Bin) { - sql_delete_query = QString(QSL("UPDATE Messages SET is_deleted = %2 WHERE id IN (%1);")).arg(message_ids.join(QSL(", ")), - QString::number(deleted)); + sql_delete_query = QString(QSL("UPDATE Messages SET is_deleted = 1 WHERE id IN (%1);")).arg(message_ids.join(QSL(", "))); } else { - sql_delete_query = QString(QSL("UPDATE Messages SET is_pdeleted = %2 WHERE id IN (%1);")).arg(message_ids.join(QSL(", ")), - QString::number(deleted)); + sql_delete_query = QString(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE id IN (%1);")).arg(message_ids.join(QSL(", "))); } if (query_read_msg.exec(sql_delete_query)) { diff --git a/src/core/messagesmodel.h b/src/core/messagesmodel.h index 4f8f9871c..872d92afa 100755 --- a/src/core/messagesmodel.h +++ b/src/core/messagesmodel.h @@ -55,6 +55,8 @@ class MessagesModel : public QSqlTableModel { int messageId(int row_index) const; RootItem::Importance messageImportance(int row_index) const; + RootItem *loadedItem() const; + void updateDateFormat(); void reloadWholeLayout(); @@ -71,7 +73,7 @@ class MessagesModel : public QSqlTableModel { // NOTE: Model is reset after one of these methods is applied and // changes ARE written to the database. bool switchBatchMessageImportance(const QModelIndexList &messages); - bool setBatchMessagesDeleted(const QModelIndexList &messages, int deleted); + bool setBatchMessagesDeleted(const QModelIndexList &messages); bool setBatchMessagesRead(const QModelIndexList &messages, RootItem::ReadStatus read); bool setBatchMessagesRestored(const QModelIndexList &messages); diff --git a/src/gui/dialogs/formmain.cpp b/src/gui/dialogs/formmain.cpp index f4c0bb91f..c433d3532 100755 --- a/src/gui/dialogs/formmain.cpp +++ b/src/gui/dialogs/formmain.cpp @@ -323,6 +323,7 @@ void FormMain::setupIcons() { m_ui->m_actionSelectPreviousMessage->setIcon(icon_theme_factory->fromTheme(QSL("go-up"))); m_ui->m_actionShowOnlyUnreadItems->setIcon(icon_theme_factory->fromTheme(QSL("mail-mark-unread"))); m_ui->m_actionExpandCollapseItem->setIcon(icon_theme_factory->fromTheme(QSL("expand-collapse"))); + m_ui->m_actionRestoreSelectedMessages->setIcon(icon_theme_factory->fromTheme(QSL("recycle-bin-restore-one"))); // Setup icons for underlying components: opened web browsers... foreach (WebBrowser *browser, WebBrowser::runningWebBrowsers()) { diff --git a/src/gui/dialogs/formmain.ui b/src/gui/dialogs/formmain.ui index 822e25ad4..606060f19 100755 --- a/src/gui/dialogs/formmain.ui +++ b/src/gui/dialogs/formmain.ui @@ -643,6 +643,11 @@ &Edit selected service account + + + &Restore selected messages + + diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index 1ac26e82e..897081281 100755 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -313,6 +313,8 @@ void FeedMessageViewer::createConnections() { this, SLOT(switchMessageSplitterOrientation())); connect(form_main->m_ui->m_actionShowOnlyUnreadItems, SIGNAL(toggled(bool)), this, SLOT(toggleShowOnlyUnreadFeeds())); + connect(form_main->m_ui->m_actionRestoreSelectedMessages, SIGNAL(triggered()), + m_messagesView, SLOT(restoreSelectedMessages())); } void FeedMessageViewer::initialize() { diff --git a/src/gui/messagesview.cpp b/src/gui/messagesview.cpp index d60d38938..5458c6b76 100755 --- a/src/gui/messagesview.cpp +++ b/src/gui/messagesview.cpp @@ -140,16 +140,18 @@ void MessagesView::contextMenuEvent(QContextMenuEvent *event) { return; } - if (m_contextMenu == NULL) { - // Context menu is not initialized, initialize. - initializeContextMenu(); - } + // Context menu is not initialized, initialize. + initializeContextMenu(); m_contextMenu->exec(event->globalPos()); } void MessagesView::initializeContextMenu() { - m_contextMenu = new QMenu(tr("Context menu for messages"), this); + if (m_contextMenu == NULL) { + m_contextMenu = new QMenu(tr("Context menu for messages"), this); + } + + m_contextMenu->clear(); m_contextMenu->addActions(QList() << qApp->mainForm()->m_ui->m_actionSendMessageViaEmail << qApp->mainForm()->m_ui->m_actionOpenSelectedSourceArticlesExternally << @@ -159,6 +161,10 @@ void MessagesView::initializeContextMenu() { qApp->mainForm()->m_ui->m_actionMarkSelectedMessagesAsUnread << qApp->mainForm()->m_ui->m_actionSwitchImportanceOfSelectedMessages << qApp->mainForm()->m_ui->m_actionDeleteSelectedMessages); + + if (m_sourceModel->loadedItem() != NULL && m_sourceModel->loadedItem()->kind() == RootItemKind::Bin) { + m_contextMenu->addAction(qApp->mainForm()->m_ui->m_actionRestoreSelectedMessages); + } } void MessagesView::mousePressEvent(QMouseEvent *event) { @@ -359,7 +365,35 @@ void MessagesView::deleteSelectedMessages() { QModelIndexList selected_indexes = selectionModel()->selectedRows(); QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes); - m_sourceModel->setBatchMessagesDeleted(mapped_indexes, 1); + m_sourceModel->setBatchMessagesDeleted(mapped_indexes); + sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder()); + + int row_count = m_sourceModel->rowCount(); + if (row_count > 0) { + QModelIndex last_item = current_index.row() < row_count ? + m_proxyModel->index(current_index.row(), MSG_DB_TITLE_INDEX) : + m_proxyModel->index(row_count - 1, MSG_DB_TITLE_INDEX); + + setCurrentIndex(last_item); + scrollTo(last_item); + reselectIndexes(QModelIndexList() << last_item); + } + else { + emit currentMessagesRemoved(); + } +} + +void MessagesView::restoreSelectedMessages() { + QModelIndex current_index = selectionModel()->currentIndex(); + + if (!current_index.isValid()) { + return; + } + + QModelIndexList selected_indexes = selectionModel()->selectedRows(); + QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes); + + m_sourceModel->setBatchMessagesRestored(mapped_indexes); sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder()); int row_count = m_sourceModel->rowCount(); diff --git a/src/gui/messagesview.h b/src/gui/messagesview.h index e2d854132..9c120ce71 100755 --- a/src/gui/messagesview.h +++ b/src/gui/messagesview.h @@ -72,6 +72,7 @@ class MessagesView : public QTreeView { void markSelectedMessagesUnread(); void switchSelectedMessagesImportance(); void deleteSelectedMessages(); + void restoreSelectedMessages(); void selectNextItem(); void selectPreviousItem(); diff --git a/src/services/standard/standardserviceroot.cpp b/src/services/standard/standardserviceroot.cpp index 48c04cf9e..7d439bc40 100755 --- a/src/services/standard/standardserviceroot.cpp +++ b/src/services/standard/standardserviceroot.cpp @@ -623,6 +623,8 @@ bool StandardServiceRoot::onAfterMessagesRestoredFromBin(RootItem *selected_item Q_UNUSED(selected_item) Q_UNUSED(message_db_ids) + updateCounts(true); + emit dataChanged(getSubTree()); emit readFeedsFilterInvalidationRequested(); return true;