| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/feedmessageviewer.h" |
| 4 | |
| 5 | #include "core/feedsproxymodel.h" |
| 6 | #include "core/messagesproxymodel.h" |
| 7 | #include "gui/dialogs/formmain.h" |
| 8 | #include "gui/feedsview.h" |
| 9 | #include "gui/messagepreviewer.h" |
| 10 | #include "gui/messagesview.h" |
| 11 | #include "gui/toolbars/feedstoolbar.h" |
| 12 | #include "gui/toolbars/messagestoolbar.h" |
| 13 | #include "gui/webbrowser.h" |
| 14 | #include "miscellaneous/settings.h" |
| 15 | #include "miscellaneous/templates.h" |
| 16 | |
| 17 | #include <QAction> |
| 18 | #include <QDebug> |
| 19 | #include <QLineEdit> |
| 20 | #include <QMenu> |
| 21 | #include <QPointer> |
| 22 | #include <QProgressBar> |
| 23 | #include <QSplitter> |
| 24 | #include <QStatusBar> |
| 25 | #include <QToolBar> |
| 26 | #include <QToolButton> |
| 27 | #include <QVBoxLayout> |
| 28 | #include <QWidgetAction> |
| 29 | |
| 30 | FeedMessageViewer::FeedMessageViewer(QWidget* parent) |
| 31 | : TabContent(parent), m_toolBarsEnabled(true), m_listHeadersEnabled(true), |
| 32 | m_toolBarFeeds(new FeedsToolBar(tr("Toolbar for feeds"), this)), |
| 33 | m_toolBarMessages(new MessagesToolBar(tr("Toolbar for articles"), this)), m_messagesView(new MessagesView(this)), |
| 34 | m_feedsView(new FeedsView(this)), m_messagesBrowser(new MessagePreviewer(this)) { |
| 35 | initialize(); |
| 36 | initializeViews(); |
| 37 | createConnections(); |
| 38 | } |
| 39 | |
| 40 | FeedMessageViewer::~FeedMessageViewer() { |
| 41 | qDebugNN << LOGSEC_GUI << "Destroying FeedMessageViewer instance."; |
| 42 | } |
| 43 | |
| 44 | WebBrowser* FeedMessageViewer::webBrowser() const { |
| 45 | return m_messagesBrowser->webBrowser(); |
| 46 | } |
| 47 | |
| 48 | FeedsView* FeedMessageViewer::feedsView() const { |
| 49 | return m_feedsView; |
| 50 | } |
| 51 | |
| 52 | MessagesView* FeedMessageViewer::messagesView() const { |
| 53 | return m_messagesView; |
| 54 | } |
| 55 | |
| 56 | MessagesToolBar* FeedMessageViewer::messagesToolBar() const { |
| 57 | return m_toolBarMessages; |
| 58 | } |
| 59 | |
| 60 | FeedsToolBar* FeedMessageViewer::feedsToolBar() const { |
| 61 | return m_toolBarFeeds; |
| 62 | } |
| 63 | |
| 64 | void FeedMessageViewer::saveSize() { |
| 65 | Settings* settings = qApp->settings(); |
| 66 | |
| 67 | settings->setValue(GROUP(GUI), GUI::FeedViewState, QString(m_feedsView->saveHeaderState().toBase64())); |
| 68 | settings->setValue(GROUP(GUI), GUI::MessageViewState, QString(m_messagesView->saveHeaderState().toBase64())); |
| 69 | |
| 70 | // Store "visibility" of toolbars and list headers. |
| 71 | settings->setValue(GROUP(GUI), GUI::ToolbarsVisible, m_toolBarsEnabled); |
| 72 | settings->setValue(GROUP(GUI), GUI::ListHeadersVisible, m_listHeadersEnabled); |
| 73 | } |
| 74 | |
| 75 | void FeedMessageViewer::loadSize() { |
| 76 | const Settings* settings = qApp->settings(); |
| 77 | |
| 78 | // Restore offsets of splitters. |
| 79 | m_feedSplitter->setSizes(toList<int>(settings->value(GROUP(GUI), SETTING(GUI::SplitterFeeds)))); |
| 80 | |
| 81 | if (settings->value(GROUP(GUI), SETTING(GUI::SplitterMessagesIsVertical)).toBool()) { |
| 82 | m_messageSplitter->setSizes(toList<int>(settings->value(GROUP(GUI), SETTING(GUI::SplitterMessagesVertical)))); |
| 83 | } |
| 84 | else { |
| 85 | switchMessageSplitterOrientation(); |
| 86 | } |
| 87 | |
| 88 | QString settings_feed_header = settings->value(GROUP(GUI), SETTING(GUI::FeedViewState)).toString(); |
| 89 | |
| 90 | if (!settings_feed_header.isEmpty()) { |
| 91 | m_feedsView->restoreHeaderState(QByteArray::fromBase64(settings_feed_header.toLocal8Bit())); |
| 92 | } |
| 93 | |
| 94 | QString settings_msg_header = settings->value(GROUP(GUI), SETTING(GUI::MessageViewState)).toString(); |
| 95 | |
| 96 | if (!settings_msg_header.isEmpty()) { |
| 97 | m_messagesView->restoreHeaderState(QByteArray::fromBase64(settings_msg_header.toLocal8Bit())); |
| 98 | } |
| 99 | else { |
| 100 | // Set default sort column. |
| 101 | m_messagesView->header()->setSortIndicator(MSG_DB_DCREATED_INDEX, Qt::SortOrder::DescendingOrder); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void FeedMessageViewer::loadMessageViewerFonts() { |
| 106 | m_messagesBrowser->reloadFontSettings(); |
| 107 | m_messagesView->reloadFontSettings(); |
| 108 | m_feedsView->reloadFontSettings(); |
| 109 | } |
| 110 | |
| 111 | bool FeedMessageViewer::areToolBarsEnabled() const { |
| 112 | return m_toolBarsEnabled; |
| 113 | } |
| 114 | |
| 115 | bool FeedMessageViewer::areListHeadersEnabled() const { |
| 116 | return m_listHeadersEnabled; |
| 117 | } |
| 118 | |
| 119 | void FeedMessageViewer::normalizeToolbarHeights() { |
| 120 | auto max_height = std::max(m_toolBarFeeds->height(), m_toolBarMessages->height()); |
| 121 | |
| 122 | m_toolBarFeeds->setMinimumHeight(max_height); |
| 123 | m_toolBarMessages->setMinimumHeight(max_height); |
| 124 | } |
| 125 | |
| 126 | void FeedMessageViewer::onFeedSplitterResized() { |
| 127 | qDebugNN << LOGSEC_GUI << "Feed splitter moved."; |
| 128 | |
| 129 | qApp->settings()->setValue(GROUP(GUI), GUI::SplitterFeeds, toVariant(m_feedSplitter->sizes())); |
| 130 | } |
| 131 | |
| 132 | void FeedMessageViewer::onMessageSplitterResized() { |
| 133 | qDebugNN << LOGSEC_GUI << "Message splitter moved."; |
| 134 | |
| 135 | QList<int> sizes = m_messageSplitter->sizes(); |
| 136 | |
| 137 | if (sizes.size() == 2 && (sizes[0] == 0 || sizes[1] == 0)) { |
| 138 | qWarningNN << LOGSEC_GUI << "Some of splitter position is 0."; |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | QVariant siz = toVariant(sizes); |
| 143 | |
| 144 | if (m_messageSplitter->orientation() == Qt::Orientation::Vertical) { |
| 145 | qApp->settings()->setValue(GROUP(GUI), GUI::SplitterMessagesVertical, siz); |
| 146 | } |
| 147 | else { |
| 148 | qApp->settings()->setValue(GROUP(GUI), GUI::SplitterMessagesHorizontal, siz); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void FeedMessageViewer::switchMessageSplitterOrientation() { |
| 153 | if (m_messageSplitter->orientation() == Qt::Orientation::Vertical) { |
| 154 | m_messageSplitter->setOrientation(Qt::Orientation::Horizontal); |
| 155 | m_messageSplitter->setSizes(toList<int>(qApp->settings()->value(GROUP(GUI), |
| 156 | SETTING(GUI::SplitterMessagesHorizontal)))); |
| 157 | } |
| 158 | else { |
| 159 | m_messageSplitter->setOrientation(Qt::Orientation::Vertical); |
| 160 | m_messageSplitter->setSizes(toList<int>(qApp->settings()->value(GROUP(GUI), |
| 161 | SETTING(GUI::SplitterMessagesVertical)))); |
| 162 | } |
| 163 | |
| 164 | qApp->settings()->setValue(GROUP(GUI), |
| 165 | GUI::SplitterMessagesIsVertical, |
| 166 | m_messageSplitter->orientation() == Qt::Orientation::Vertical); |
| 167 | } |
| 168 | |
| 169 | void FeedMessageViewer::setToolBarsEnabled(bool enable) { |
| 170 | m_toolBarsEnabled = enable; |
| 171 | m_toolBarFeeds->setVisible(enable); |
| 172 | m_toolBarMessages->setVisible(enable); |
| 173 | } |
| 174 | |
| 175 | void FeedMessageViewer::setListHeadersEnabled(bool enable) { |
| 176 | m_listHeadersEnabled = enable; |
| 177 | m_feedsView->header()->setVisible(enable); |
| 178 | m_messagesView->header()->setVisible(enable); |
| 179 | } |
| 180 | |
| 181 | void FeedMessageViewer::switchFeedComponentVisibility() { |
| 182 | auto* sen = qobject_cast<QAction*>(sender()); |
| 183 | |
| 184 | if (sen != nullptr) { |
| 185 | m_feedsWidget->setVisible(sen->isChecked()); |
| 186 | } |
| 187 | else { |
| 188 | m_feedsWidget->setVisible(!m_feedsWidget->isVisible()); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void FeedMessageViewer::changeMessageFilter(MessagesProxyModel::MessageListFilter filter) { |
| 193 | m_messagesView->changeFilter(filter); |
| 194 | } |
| 195 | |
| 196 | void FeedMessageViewer::changeFeedFilter(FeedsProxyModel::FeedListFilter filter) { |
| 197 | m_feedsView->changeFilter(filter); |
| 198 | } |
| 199 | |
| 200 | void FeedMessageViewer::toggleShowFeedTreeBranches() { |
| 201 | const QAction* origin = qobject_cast<QAction*>(sender()); |
| 202 | |
| 203 | m_feedsView->setRootIsDecorated(origin->isChecked()); |
| 204 | qApp->settings()->setValue(GROUP(Feeds), Feeds::ShowTreeBranches, origin->isChecked()); |
| 205 | } |
| 206 | |
| 207 | void FeedMessageViewer::toggleItemsAutoExpandingOnSelection() { |
| 208 | const QAction* origin = qobject_cast<QAction*>(sender()); |
| 209 | |
| 210 | qApp->settings()->setValue(GROUP(Feeds), Feeds::AutoExpandOnSelection, origin->isChecked()); |
| 211 | } |
| 212 | |
| 213 | void FeedMessageViewer::alternateRowColorsInLists() { |
| 214 | const QAction* origin = qobject_cast<QAction*>(sender()); |
| 215 | |
| 216 | m_feedsView->setAlternatingRowColors(origin->isChecked()); |
| 217 | m_messagesView->setAlternatingRowColors(origin->isChecked()); |
| 218 | |
| 219 | qApp->settings()->setValue(GROUP(GUI), GUI::AlternateRowColorsInLists, origin->isChecked()); |
| 220 | } |
| 221 | |
| 222 | void FeedMessageViewer::respondToMainWindowResizes() { |
| 223 | connect(qApp->mainForm(), &FormMain::windowResized, this, &FeedMessageViewer::onMessageSplitterResized); |
| 224 | } |
| 225 | |
| 226 | void FeedMessageViewer::displayMessage(const Message& message, RootItem* root) { |
| 227 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::EnableMessagePreview)).toBool()) { |
| 228 | m_messagesBrowser->loadMessage(message, root); |
| 229 | } |
| 230 | else if (m_articleViewerAlwaysVisible) { |
| 231 | m_messagesBrowser->showItemDetails(root); |
| 232 | } |
| 233 | else { |
| 234 | m_messagesBrowser->clear(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void FeedMessageViewer::loadMessageToFeedAndArticleList(Feed* feed, const Message& message) { |
| 239 | auto idx_src = m_feedsView->sourceModel()->indexForItem(feed); |
| 240 | auto idx_map = m_feedsView->model()->mapFromSource(idx_src); |
| 241 | auto is_visible = !m_feedsView->isIndexHidden(idx_map); |
| 242 | |
| 243 | if (!idx_map.isValid() || !is_visible) { |
| 244 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 245 | GuiMessage(tr("Filtered feed list"), |
| 246 | tr("Cannot select article in article list as your feed is filtered out from feed " |
| 247 | "list."), |
| 248 | QSystemTrayIcon::MessageIcon::Warning), |
| 249 | GuiMessageDestination(true, true)); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // TODO: expand properly |
| 254 | m_feedsView->setExpanded(idx_map, true); |
| 255 | m_feedsView->setCurrentIndex(idx_map); |
| 256 | QCoreApplication::processEvents(); |
| 257 | |
| 258 | auto idx_map_msg = m_messagesView->model()->indexFromMessage(message); |
| 259 | auto msg_is_visible = !m_messagesView->isRowHidden(idx_map_msg.row(), idx_map_msg); |
| 260 | |
| 261 | if (!idx_map_msg.isValid() || !msg_is_visible) { |
| 262 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 263 | GuiMessage(tr("Filtered article list"), |
| 264 | tr("Cannot select article as it seems your article list is filtered or the article " |
| 265 | "was deleted."), |
| 266 | QSystemTrayIcon::MessageIcon::Warning), |
| 267 | GuiMessageDestination(true, true)); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | m_messagesView->setCurrentIndex(idx_map_msg); |
| 272 | } |
| 273 | |
| 274 | void FeedMessageViewer::onMessageRemoved(RootItem* root) { |
| 275 | if (m_articleViewerAlwaysVisible) { |
| 276 | m_messagesBrowser->showItemDetails(root); |
| 277 | } |
| 278 | else { |
| 279 | m_messagesBrowser->clear(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void FeedMessageViewer::createConnections() { |
| 284 | // Filtering & searching. |
| 285 | connect(m_toolBarMessages, &MessagesToolBar::searchCriteriaChanged, m_messagesView, &MessagesView::searchMessages); |
| 286 | connect(m_toolBarFeeds, &FeedsToolBar::searchCriteriaChanged, m_feedsView, &FeedsView::filterItems); |
| 287 | connect(m_toolBarMessages, |
| 288 | &MessagesToolBar::messageHighlighterChanged, |
| 289 | m_messagesView, |
| 290 | &MessagesView::highlightMessages); |
| 291 | |
| 292 | connect(m_toolBarMessages, &MessagesToolBar::messageFilterChanged, this, &FeedMessageViewer::changeMessageFilter); |
| 293 | connect(m_toolBarFeeds, &FeedsToolBar::feedFilterChanged, this, &FeedMessageViewer::changeFeedFilter); |
| 294 | |
| 295 | connect(m_feedSplitter, &QSplitter::splitterMoved, this, &FeedMessageViewer::onFeedSplitterResized); |
| 296 | connect(m_messageSplitter, &QSplitter::splitterMoved, this, &FeedMessageViewer::onMessageSplitterResized); |
| 297 | |
| 298 | connect(m_messagesBrowser, |
| 299 | &MessagePreviewer::markMessageRead, |
| 300 | m_messagesView->sourceModel(), |
| 301 | &MessagesModel::setMessageReadById); |
| 302 | connect(m_messagesBrowser, |
| 303 | &MessagePreviewer::markMessageImportant, |
| 304 | m_messagesView->sourceModel(), |
| 305 | &MessagesModel::setMessageImportantById); |
| 306 | connect(m_messagesBrowser, |
| 307 | &MessagePreviewer::setMessageLabelIds, |
| 308 | m_messagesView->sourceModel(), |
| 309 | &MessagesModel::setMessageLabelsById); |
| 310 | |
| 311 | connect(m_messagesView, &MessagesView::currentMessageRemoved, this, &FeedMessageViewer::onMessageRemoved); |
| 312 | connect(m_messagesView, &MessagesView::currentMessageChanged, this, &FeedMessageViewer::displayMessage); |
| 313 | connect(m_messagesView, &MessagesView::openLinkMiniBrowser, m_messagesBrowser, &MessagePreviewer::loadUrl); |
| 314 | |
| 315 | // If user selects feeds, load their messages. |
| 316 | connect(m_feedsView, &FeedsView::itemSelected, m_messagesView, &MessagesView::loadItem); |
| 317 | connect(m_feedsView, &FeedsView::requestViewNextUnreadMessage, m_messagesView, &MessagesView::selectNextUnreadItem); |
| 318 | |
| 319 | // State of many messages is changed, then we need |
| 320 | // to reload selections. |
| 321 | connect(m_feedsView->sourceModel(), |
| 322 | &FeedsModel::reloadMessageListRequested, |
| 323 | m_messagesView, |
| 324 | &MessagesView::reloadSelections); |
| 325 | } |
| 326 | |
| 327 | void FeedMessageViewer::updateArticleViewerSettings() { |
| 328 | m_articleViewerAlwaysVisible = |
| 329 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::AlwaysDisplayItemPreview)).toBool(); |
| 330 | } |
| 331 | |
| 332 | MessagePreviewer* FeedMessageViewer::messagesBrowser() const { |
| 333 | return m_messagesBrowser; |
| 334 | } |
| 335 | |
| 336 | void FeedMessageViewer::initialize() { |
| 337 | // Initialize/populate toolbars. |
| 338 | m_toolBarFeeds->setFloatable(false); |
| 339 | m_toolBarFeeds->setMovable(false); |
| 340 | m_toolBarFeeds->setAllowedAreas(Qt::ToolBarArea::TopToolBarArea); |
| 341 | m_toolBarMessages->setFloatable(false); |
| 342 | m_toolBarMessages->setMovable(false); |
| 343 | m_toolBarMessages->setAllowedAreas(Qt::ToolBarArea::TopToolBarArea); |
| 344 | |
| 345 | updateArticleViewerSettings(); |
| 346 | |
| 347 | // if (!m_articleViewerAlwaysVisible) { |
| 348 | m_messagesBrowser->clear(); |
| 349 | //} |
| 350 | |
| 351 | // Now refresh visual setup. |
| 352 | refreshVisualProperties(); |
| 353 | } |
| 354 | |
| 355 | void FeedMessageViewer::initializeViews() { |
| 356 | m_feedsWidget = new QWidget(this); |
| 357 | m_messagesWidget = new QWidget(this); |
| 358 | m_feedSplitter = new QSplitter(Qt::Orientation::Horizontal, this); |
| 359 | m_messageSplitter = new QSplitter(Qt::Orientation::Vertical, this); |
| 360 | |
| 361 | // Instantiate needed components. |
| 362 | auto* central_layout = new QVBoxLayout(this); |
| 363 | auto* feed_layout = new QVBoxLayout(m_feedsWidget); |
| 364 | auto* message_layout = new QVBoxLayout(m_messagesWidget); |
| 365 | |
| 366 | // Set layout properties. |
| 367 | central_layout->setContentsMargins({}); |
| 368 | feed_layout->setContentsMargins({}); |
| 369 | message_layout->setContentsMargins({}); |
| 370 | |
| 371 | central_layout->setSpacing(0); |
| 372 | feed_layout->setSpacing(0); |
| 373 | message_layout->setSpacing(0); |
| 374 | |
| 375 | // Set views. |
| 376 | m_feedsView->setFrameStyle(QFrame::Shape::NoFrame); |
| 377 | m_messagesView->setFrameStyle(QFrame::Shape::NoFrame); |
| 378 | |
| 379 | // Setup message splitter. |
| 380 | m_messageSplitter->setObjectName(QSL("m_messageSplitter")); |
| 381 | m_messageSplitter->setHandleWidth(1); |
| 382 | m_messageSplitter->setOpaqueResize(true); |
| 383 | m_messageSplitter->setChildrenCollapsible(false); |
| 384 | m_messageSplitter->addWidget(m_messagesView); |
| 385 | m_messageSplitter->addWidget(m_messagesBrowser); |
| 386 | |
| 387 | // Assemble message-related components to single widget. |
| 388 | message_layout->addWidget(m_toolBarMessages); |
| 389 | message_layout->addWidget(m_messageSplitter); |
| 390 | |
| 391 | // Assemble feed-related components to another widget. |
| 392 | feed_layout->addWidget(m_toolBarFeeds); |
| 393 | feed_layout->addWidget(m_feedsView); |
| 394 | |
| 395 | // Assemble everything together. |
| 396 | m_feedSplitter->setHandleWidth(1); |
| 397 | m_feedSplitter->setOpaqueResize(true); |
| 398 | m_feedSplitter->setChildrenCollapsible(false); |
| 399 | m_feedSplitter->addWidget(m_feedsWidget); |
| 400 | m_feedSplitter->addWidget(m_messagesWidget); |
| 401 | |
| 402 | // Add toolbar and main feeds/messages widget to main layout. |
| 403 | central_layout->addWidget(m_feedSplitter); |
| 404 | setTabOrder(m_feedsView, m_messagesView); |
| 405 | setTabOrder(m_messagesView, m_toolBarFeeds); |
| 406 | setTabOrder(m_toolBarFeeds, m_toolBarMessages); |
| 407 | setTabOrder(m_toolBarMessages, m_messagesBrowser); |
| 408 | |
| 409 | // Set initial ratio of sizes. |
| 410 | m_feedSplitter->setStretchFactor(0, 1); |
| 411 | m_feedSplitter->setStretchFactor(1, 3); |
| 412 | } |
| 413 | |
| 414 | void FeedMessageViewer::refreshVisualProperties() { |
| 415 | const Qt::ToolButtonStyle button_style = |
| 416 | static_cast<Qt::ToolButtonStyle>(qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToolbarStyle)).toInt()); |
| 417 | |
| 418 | m_toolBarFeeds->setToolButtonStyle(button_style); |
| 419 | m_toolBarMessages->setToolButtonStyle(button_style); |
| 420 | |
| 421 | const int icon_size = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToolbarIconSize)).toInt(); |
| 422 | |
| 423 | if (icon_size > 0) { |
| 424 | m_toolBarFeeds->setIconSize({icon_size, icon_size}); |
| 425 | } |
| 426 | else { |
| 427 | m_toolBarFeeds->setIconSize({qApp->style()->pixelMetric(QStyle::PM_ToolBarIconSize), |
| 428 | qApp->style()->pixelMetric(QStyle::PM_ToolBarIconSize)}); |
| 429 | } |
| 430 | |
| 431 | m_toolBarMessages->setIconSize(m_toolBarFeeds->iconSize()); |
| 432 | } |