| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/gui/formfeeddetails.h" |
| 4 | |
| 5 | #include "database/databasequeries.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "exceptions/applicationexception.h" |
| 8 | #include "gui/guiutilities.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/textfactory.h" |
| 11 | #include "services/abstract/gui/multifeededitcheckbox.h" |
| 12 | #include "services/abstract/rootitem.h" |
| 13 | |
| 14 | #include "ui_formfeeddetails.h" |
| 15 | |
| 16 | #include <QMenu> |
| 17 | #include <QNetworkReply> |
| 18 | #include <QPair> |
| 19 | #include <QPushButton> |
| 20 | #include <QTextCodec> |
| 21 | |
| 22 | FormFeedDetails::FormFeedDetails(ServiceRoot* service_root, QWidget* parent) |
| 23 | : QDialog(parent), m_ui(new Ui::FormFeedDetails()), m_serviceRoot(service_root) { |
| 24 | initialize(); |
| 25 | createConnections(); |
| 26 | } |
| 27 | |
| 28 | FormFeedDetails::~FormFeedDetails() = default; |
| 29 | |
| 30 | void FormFeedDetails::activateTab(int index) { |
| 31 | m_ui->m_tabWidget->setCurrentIndex(index); |
| 32 | } |
| 33 | |
| 34 | void FormFeedDetails::clearTabs() { |
| 35 | m_ui->m_tabWidget->clear(); |
| 36 | } |
| 37 | |
| 38 | void FormFeedDetails::insertCustomTab(QWidget* custom_tab, const QString& title, int index) { |
| 39 | if (index < 0) { |
| 40 | m_ui->m_tabWidget->addTab(custom_tab, title); |
| 41 | } |
| 42 | else { |
| 43 | m_ui->m_tabWidget->insertTab(index, custom_tab, title); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void FormFeedDetails::apply() { |
| 48 | QList<Feed*> fds = feeds<Feed>(); |
| 49 | |
| 50 | for (Feed* fd : fds) { |
| 51 | // Setup common data for the feed. |
| 52 | if (isChangeAllowed(m_ui->m_mcbAutoDownloading)) { |
| 53 | fd->setAutoUpdateType(static_cast<Feed::AutoUpdateType>(m_ui->m_cmbAutoUpdateType |
| 54 | ->itemData(m_ui->m_cmbAutoUpdateType->currentIndex()) |
| 55 | .toInt())); |
| 56 | fd->setAutoUpdateInterval(int(m_ui->m_spinAutoUpdateInterval->value())); |
| 57 | } |
| 58 | |
| 59 | if (isChangeAllowed(m_ui->m_mcbOpenArticlesAutomatically)) { |
| 60 | fd->setOpenArticlesDirectly(m_ui->m_cbOpenArticlesAutomatically->isChecked()); |
| 61 | } |
| 62 | |
| 63 | if (isChangeAllowed(m_ui->m_mcbFeedRtl)) { |
| 64 | fd->setRtlBehavior(m_ui->m_cmbRtlBehavior->currentData().value<RtlBehavior>()); |
| 65 | } |
| 66 | |
| 67 | m_ui->m_wdgArticleLimiting->saveFeed(fd, m_isBatchEdit); |
| 68 | |
| 69 | if (isChangeAllowed(m_ui->m_mcbDisableFeed)) { |
| 70 | fd->setIsSwitchedOff(m_ui->m_cbDisableFeed->isChecked()); |
| 71 | } |
| 72 | |
| 73 | if (isChangeAllowed(m_ui->m_mcbSuppressFeed)) { |
| 74 | fd->setIsQuiet(m_ui->m_cbSuppressFeed->isChecked()); |
| 75 | } |
| 76 | |
| 77 | if (!m_creatingNew) { |
| 78 | // We need to make sure that common data are saved. |
| 79 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 80 | |
| 81 | DatabaseQueries::createOverwriteFeed(database, fd, m_serviceRoot->accountId(), fd->parent()->id()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (!m_creatingNew) { |
| 86 | m_serviceRoot->itemChanged(feeds<RootItem>()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | QDialogButtonBox* FormFeedDetails::buttonBox() const { |
| 91 | return m_ui->m_buttonBox; |
| 92 | } |
| 93 | |
| 94 | bool FormFeedDetails::isChangeAllowed(MultiFeedEditCheckBox* mcb) const { |
| 95 | return !m_isBatchEdit || mcb->isChecked(); |
| 96 | } |
| 97 | |
| 98 | void FormFeedDetails::onAutoUpdateTypeChanged(int new_index) { |
| 99 | Feed::AutoUpdateType auto_update_type = |
| 100 | static_cast<Feed::AutoUpdateType>(m_ui->m_cmbAutoUpdateType->itemData(new_index).toInt()); |
| 101 | |
| 102 | switch (auto_update_type) { |
| 103 | case Feed::AutoUpdateType::DontAutoUpdate: |
| 104 | case Feed::AutoUpdateType::DefaultAutoUpdate: |
| 105 | m_ui->m_spinAutoUpdateInterval->setEnabled(false); |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | m_ui->m_spinAutoUpdateInterval->setEnabled(true); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void FormFeedDetails::createConnections() { |
| 114 | connect(m_ui->m_buttonBox, &QDialogButtonBox::accepted, this, &FormFeedDetails::acceptIfPossible); |
| 115 | connect(m_ui->m_cmbAutoUpdateType, |
| 116 | static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 117 | this, |
| 118 | &FormFeedDetails::onAutoUpdateTypeChanged); |
| 119 | } |
| 120 | |
| 121 | void FormFeedDetails::loadFeedData() { |
| 122 | Feed* fd = feed<Feed>(); |
| 123 | |
| 124 | if (m_isBatchEdit) { |
| 125 | // We hook batch selectors. |
| 126 | m_ui->m_mcbAutoDownloading->addActionWidget(m_ui->m_wdgAutoUpdate); |
| 127 | m_ui->m_mcbOpenArticlesAutomatically->addActionWidget(m_ui->m_cbOpenArticlesAutomatically); |
| 128 | m_ui->m_mcbDisableFeed->addActionWidget(m_ui->m_cbDisableFeed); |
| 129 | m_ui->m_mcbSuppressFeed->addActionWidget(m_ui->m_cbSuppressFeed); |
| 130 | m_ui->m_mcbFeedRtl->addActionWidget(m_ui->m_cmbRtlBehavior); |
| 131 | } |
| 132 | else { |
| 133 | // We hide batch selectors. |
| 134 | for (auto* cb : findChildren<MultiFeedEditCheckBox*>()) { |
| 135 | cb->hide(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | m_ui->m_wdgArticleLimiting->setForAppWideFeatures(false, m_isBatchEdit); |
| 140 | |
| 141 | if (m_creatingNew) { |
| 142 | GuiUtilities::applyDialogProperties(*this, |
| 143 | qApp->icons()->fromTheme(QSL("application-rss+xml")), |
| 144 | tr("Add new feed")); |
| 145 | } |
| 146 | else { |
| 147 | if (!m_isBatchEdit) { |
| 148 | GuiUtilities::applyDialogProperties(*this, fd->fullIcon(), tr("Edit \"%1\"").arg(fd->title())); |
| 149 | } |
| 150 | else { |
| 151 | GuiUtilities::applyDialogProperties(*this, |
| 152 | qApp->icons()->fromTheme(QSL("application-rss+xml")), |
| 153 | tr("Edit %n feeds", nullptr, m_feeds.size())); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | m_ui->m_cmbAutoUpdateType |
| 158 | ->setCurrentIndex(m_ui->m_cmbAutoUpdateType->findData(QVariant::fromValue(int(fd->autoUpdateType())))); |
| 159 | m_ui->m_spinAutoUpdateInterval->setValue(fd->autoUpdateInterval()); |
| 160 | m_ui->m_cbOpenArticlesAutomatically->setChecked(fd->openArticlesDirectly()); |
| 161 | m_ui->m_cmbRtlBehavior->setCurrentIndex(m_ui->m_cmbRtlBehavior->findData(QVariant::fromValue(fd->rtlBehavior()))); |
| 162 | m_ui->m_cbDisableFeed->setChecked(fd->isSwitchedOff()); |
| 163 | m_ui->m_cbSuppressFeed->setChecked(fd->isQuiet()); |
| 164 | |
| 165 | Feed::ArticleIgnoreLimit art_limit = Feed::ArticleIgnoreLimit(fd->articleIgnoreLimit()); |
| 166 | |
| 167 | /* |
| 168 | art_limit.m_addAnyArticlesToDb = fd->addAnyDatetimeArticles(); |
| 169 | art_limit.m_avoidOldArticles = |
| 170 | (fd->datetimeToAvoid().isValid() && fd->datetimeToAvoid().toMSecsSinceEpoch() > 0) || fd->hoursToAvoid() > 0; |
| 171 | art_limit.m_dtToAvoid = fd->datetimeToAvoid(); |
| 172 | art_limit.m_hoursToAvoid = fd->hoursToAvoid(); |
| 173 | |
| 174 | art_limit.m_doNotRemoveStarred = false; |
| 175 | art_limit.m_doNotRemoveUnread = false; |
| 176 | art_limit.m_keepCountOfArticles = 4; |
| 177 | art_limit.m_moveToBinDontPurge = false; |
| 178 | */ |
| 179 | |
| 180 | m_ui->m_wdgArticleLimiting->load(art_limit, true); |
| 181 | } |
| 182 | |
| 183 | void FormFeedDetails::acceptIfPossible() { |
| 184 | try { |
| 185 | apply(); |
| 186 | accept(); |
| 187 | } |
| 188 | catch (const ApplicationException& ex) { |
| 189 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 190 | {tr("Cannot save feed properties"), |
| 191 | tr("Cannot save changes: %1").arg(ex.message()), |
| 192 | QSystemTrayIcon::MessageIcon::Critical}, |
| 193 | {}, |
| 194 | {}, |
| 195 | this); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void FormFeedDetails::initialize() { |
| 200 | m_ui->setupUi(this); |
| 201 | |
| 202 | // Setup auto-update options. |
| 203 | m_ui->m_spinAutoUpdateInterval->setMode(TimeSpinBox::Mode::MinutesSeconds); |
| 204 | m_ui->m_spinAutoUpdateInterval->setValue(DEFAULT_AUTO_UPDATE_INTERVAL); |
| 205 | |
| 206 | m_ui->m_cmbAutoUpdateType->addItem(tr("Fetch articles using global interval"), |
| 207 | QVariant::fromValue(int(Feed::AutoUpdateType::DefaultAutoUpdate))); |
| 208 | m_ui->m_cmbAutoUpdateType->addItem(tr("Fetch articles every"), |
| 209 | QVariant::fromValue(int(Feed::AutoUpdateType::SpecificAutoUpdate))); |
| 210 | m_ui->m_cmbAutoUpdateType->addItem(tr("Disable auto-fetching of articles"), |
| 211 | QVariant::fromValue(int(Feed::AutoUpdateType::DontAutoUpdate))); |
| 212 | |
| 213 | m_ui->m_cmbRtlBehavior->addItem(tr("Left-to-right"), QVariant::fromValue(int(RtlBehavior::NoRtl))); |
| 214 | m_ui->m_cmbRtlBehavior->addItem(tr("Right-to-left (everywhere)"), QVariant::fromValue(int(RtlBehavior::Everywhere))); |
| 215 | m_ui->m_cmbRtlBehavior->addItem(tr("Right-to-left (everywhere except feed list)"), |
| 216 | QVariant::fromValue(int(RtlBehavior::EverywhereExceptFeedList))); |
| 217 | m_ui->m_cmbRtlBehavior->addItem(tr("Right-to-left (only in article viewer)"), |
| 218 | QVariant::fromValue(int(RtlBehavior::OnlyViewer))); |
| 219 | } |