| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "articleamountcontrol.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | |
| 7 | ArticleAmountControl::ArticleAmountControl(QWidget* parent) : QWidget(parent) { |
| 8 | m_ui.setupUi(this); |
| 9 | |
| 10 | m_ui.m_helpIgnoring |
| 11 | ->setHelpText(tr("Setting any limitations here will instruct %1 to ignore " |
| 12 | "some incoming articles. The logic runs AFTER any article filters so even if your article filter " |
| 13 | "accepts particular article, it can still subsequently ignored and not added to database.") |
| 14 | .arg(QSL(APP_NAME)), |
| 15 | false); |
| 16 | m_ui.m_helpLimit->setHelpText(tr("All excessive articles are removed automatically by the application, usually after " |
| 17 | "particular feed is fetched. Articles are either completely purged (including " |
| 18 | "articles from recycle bin) from internal " |
| 19 | "database or are just moved to recycle bin."), |
| 20 | false); |
| 21 | |
| 22 | m_ui.m_spinArticleCount->setSpecialValueText(tr("all articles")); |
| 23 | m_ui.m_cbArticleLimittingCustomize->setChecked(true); |
| 24 | m_ui.m_dtDateTimeToAvoid->setEnabled(false); |
| 25 | m_ui.m_spinHoursAvoid->setEnabled(false); |
| 26 | m_ui.m_spinHoursAvoid->setMode(TimeSpinBox::Mode::DaysHours); |
| 27 | m_ui.m_dtDateTimeToAvoid |
| 28 | ->setDisplayFormat(qApp->localization()->loadedLocale().dateTimeFormat(QLocale::FormatType::ShortFormat)); |
| 29 | |
| 30 | connect(m_ui.m_cbAddAnyDateArticles, &QCheckBox::toggled, this, [this](bool checked) { |
| 31 | m_ui.m_gbAvoidOldArticles->setEnabled(!checked); |
| 32 | }); |
| 33 | |
| 34 | // Ignoring articles. |
| 35 | connect(m_ui.m_cbAddAnyDateArticles, &QCheckBox::toggled, this, &ArticleAmountControl::changed); |
| 36 | connect(m_ui.m_gbAvoidOldArticles, &QGroupBox::toggled, this, &ArticleAmountControl::changed); |
| 37 | connect(m_ui.m_dtDateTimeToAvoid, &QDateTimeEdit::dateTimeChanged, this, &ArticleAmountControl::changed); |
| 38 | connect(m_ui.m_spinHoursAvoid, |
| 39 | QOverload<double>::of(&TimeSpinBox::valueChanged), |
| 40 | this, |
| 41 | &ArticleAmountControl::changed); |
| 42 | connect(m_ui.m_rbAvoidAbsolute, &QRadioButton::toggled, this, &ArticleAmountControl::changed); |
| 43 | connect(m_ui.m_rbAvoidRelative, &QRadioButton::toggled, this, &ArticleAmountControl::changed); |
| 44 | |
| 45 | // Limitting articles. |
| 46 | connect(m_ui.m_spinArticleCount, |
| 47 | QOverload<int>::of(&QSpinBox::valueChanged), |
| 48 | this, |
| 49 | &ArticleAmountControl::updateArticleCountSuffix); |
| 50 | connect(m_ui.m_cbArticleLimittingCustomize, &QCheckBox::toggled, this, &ArticleAmountControl::changed); |
| 51 | connect(m_ui.m_spinArticleCount, QOverload<int>::of(&QSpinBox::valueChanged), this, &ArticleAmountControl::changed); |
| 52 | connect(m_ui.m_cbMoveToBinNoPurge, &QCheckBox::toggled, this, &ArticleAmountControl::changed); |
| 53 | connect(m_ui.m_cbNoRemoveImportant, &QCheckBox::toggled, this, &ArticleAmountControl::changed); |
| 54 | connect(m_ui.m_cbNoRemoveUnread, &QCheckBox::toggled, this, &ArticleAmountControl::changed); |
| 55 | } |
| 56 | |
| 57 | void ArticleAmountControl::setForAppWideFeatures(bool app_wide, bool batch_edit) { |
| 58 | if (app_wide) { |
| 59 | m_ui.m_cbAddAnyDateArticles->setVisible(false); |
| 60 | m_ui.m_cbArticleLimittingCustomize->setVisible(false); |
| 61 | } |
| 62 | else { |
| 63 | connect(m_ui.m_cbAddAnyDateArticles, &QCheckBox::toggled, m_ui.m_wdgAvoidOldArticles, &QGroupBox::setDisabled); |
| 64 | connect(m_ui.m_cbArticleLimittingCustomize, |
| 65 | &QCheckBox::toggled, |
| 66 | m_ui.m_wdgArticleLimittingCustomize, |
| 67 | &QGroupBox::setEnabled); |
| 68 | } |
| 69 | |
| 70 | if (batch_edit) { |
| 71 | // We hook batch selectors. |
| 72 | m_ui.m_mcbAddAnyDateArticles->addActionWidget(m_ui.m_cbAddAnyDateArticles); |
| 73 | m_ui.m_mcbAvoidOldArticles->addActionWidget(m_ui.m_wdgAvoidOldArticles); |
| 74 | |
| 75 | m_ui.m_mcbArticleLimittingCustomize->addActionWidget(m_ui.m_cbArticleLimittingCustomize); |
| 76 | m_ui.m_mcbArticleLimittingSetup->addActionWidget(m_ui.m_wdgArticleLimittingCustomize); |
| 77 | } |
| 78 | else { |
| 79 | // We hide batch selectors. |
| 80 | for (auto* cb : findChildren<MultiFeedEditCheckBox*>()) { |
| 81 | cb->hide(); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void ArticleAmountControl::load(const Feed::ArticleIgnoreLimit& setup, bool always_avoid) { |
| 87 | // Ignoring articles. |
| 88 | if (setup.m_dtToAvoid.isValid() && setup.m_dtToAvoid.toMSecsSinceEpoch() > 0) { |
| 89 | m_ui.m_rbAvoidAbsolute->setChecked(true); |
| 90 | m_ui.m_dtDateTimeToAvoid->setDateTime(setup.m_dtToAvoid); |
| 91 | } |
| 92 | else if (setup.m_hoursToAvoid > 0) { |
| 93 | m_ui.m_rbAvoidRelative->setChecked(true); |
| 94 | m_ui.m_spinHoursAvoid->setValue(setup.m_hoursToAvoid); |
| 95 | } |
| 96 | |
| 97 | if (always_avoid) { |
| 98 | m_ui.m_gbAvoidOldArticles->setChecked(m_ui.m_rbAvoidAbsolute->isChecked() || m_ui.m_rbAvoidRelative->isChecked()); |
| 99 | } |
| 100 | else { |
| 101 | m_ui.m_gbAvoidOldArticles->setChecked(setup.m_avoidOldArticles); |
| 102 | } |
| 103 | |
| 104 | m_ui.m_cbAddAnyDateArticles->setChecked(setup.m_addAnyArticlesToDb); |
| 105 | |
| 106 | // Limitting articles. |
| 107 | m_ui.m_cbArticleLimittingCustomize->setChecked(setup.m_customizeLimitting); |
| 108 | m_ui.m_spinArticleCount->setValue(setup.m_keepCountOfArticles); |
| 109 | m_ui.m_cbMoveToBinNoPurge->setChecked(setup.m_moveToBinDontPurge); |
| 110 | m_ui.m_cbNoRemoveImportant->setChecked(setup.m_doNotRemoveStarred); |
| 111 | m_ui.m_cbNoRemoveUnread->setChecked(setup.m_doNotRemoveUnread); |
| 112 | } |
| 113 | |
| 114 | Feed::ArticleIgnoreLimit ArticleAmountControl::save() const { |
| 115 | Feed::ArticleIgnoreLimit setup; |
| 116 | |
| 117 | // Ignoring articles. |
| 118 | setup.m_addAnyArticlesToDb = m_ui.m_cbAddAnyDateArticles->isChecked(); |
| 119 | setup.m_avoidOldArticles = m_ui.m_gbAvoidOldArticles->isChecked(); |
| 120 | |
| 121 | if (m_ui.m_rbAvoidAbsolute->isChecked()) { |
| 122 | setup.m_dtToAvoid = m_ui.m_dtDateTimeToAvoid->dateTime(); |
| 123 | } |
| 124 | else if (m_ui.m_rbAvoidRelative->isChecked()) { |
| 125 | setup.m_hoursToAvoid = int(m_ui.m_spinHoursAvoid->value()); |
| 126 | } |
| 127 | |
| 128 | // Limitting articles. |
| 129 | setup.m_customizeLimitting = m_ui.m_cbArticleLimittingCustomize->isChecked(); |
| 130 | setup.m_keepCountOfArticles = m_ui.m_spinArticleCount->value(); |
| 131 | setup.m_moveToBinDontPurge = m_ui.m_cbMoveToBinNoPurge->isChecked(); |
| 132 | setup.m_doNotRemoveStarred = m_ui.m_cbNoRemoveImportant->isChecked(); |
| 133 | setup.m_doNotRemoveUnread = m_ui.m_cbNoRemoveUnread->isChecked(); |
| 134 | |
| 135 | return setup; |
| 136 | } |
| 137 | |
| 138 | bool isChangeAllowed(MultiFeedEditCheckBox* mcb, bool batch_edit) { |
| 139 | return !batch_edit || mcb->isChecked(); |
| 140 | } |
| 141 | |
| 142 | void ArticleAmountControl::saveFeed(Feed* fd, bool batch_edit) const { |
| 143 | Feed::ArticleIgnoreLimit& art = fd->articleIgnoreLimit(); |
| 144 | |
| 145 | if (isChangeAllowed(m_ui.m_mcbAddAnyDateArticles, batch_edit)) { |
| 146 | art.m_addAnyArticlesToDb = m_ui.m_cbAddAnyDateArticles->isChecked(); |
| 147 | } |
| 148 | |
| 149 | if (isChangeAllowed(m_ui.m_mcbAvoidOldArticles, batch_edit)) { |
| 150 | if (m_ui.m_gbAvoidOldArticles->isChecked()) { |
| 151 | if (m_ui.m_rbAvoidAbsolute->isChecked()) { |
| 152 | art.m_dtToAvoid = m_ui.m_dtDateTimeToAvoid->dateTime(); |
| 153 | art.m_hoursToAvoid = 0; |
| 154 | } |
| 155 | else { |
| 156 | art.m_dtToAvoid = {}; |
| 157 | art.m_hoursToAvoid = m_ui.m_spinHoursAvoid->value(); |
| 158 | } |
| 159 | } |
| 160 | else { |
| 161 | art.m_dtToAvoid = {}; |
| 162 | art.m_hoursToAvoid = 0; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (isChangeAllowed(m_ui.m_mcbArticleLimittingCustomize, batch_edit)) { |
| 167 | art.m_customizeLimitting = m_ui.m_cbArticleLimittingCustomize->isChecked(); |
| 168 | } |
| 169 | |
| 170 | if (isChangeAllowed(m_ui.m_mcbArticleLimittingSetup, batch_edit)) { |
| 171 | art.m_keepCountOfArticles = m_ui.m_spinArticleCount->value(); |
| 172 | art.m_doNotRemoveStarred = m_ui.m_cbNoRemoveImportant->isChecked(); |
| 173 | art.m_doNotRemoveUnread = m_ui.m_cbNoRemoveUnread->isChecked(); |
| 174 | art.m_moveToBinDontPurge = m_ui.m_cbMoveToBinNoPurge->isChecked(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void ArticleAmountControl::updateArticleCountSuffix(int count) { |
| 179 | m_ui.m_spinArticleCount->setSuffix(QSL(" ") + tr("newest article(s)", nullptr, count)); |
| 180 | } |