File indexing completed on 2024-04-21 03:51:03

0001 /*
0002     blocking options for Parley
0003     SPDX-FileCopyrightText: 2005 Peter Hedlund <peter.hedlund@kdemail.net>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "blockoptions.h"
0008 
0009 #include <QCheckBox>
0010 
0011 #include <KComboBox>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 
0015 #include "prefs.h"
0016 #include <KLazyLocalizedString>
0017 struct ListRef {
0018     const KLazyLocalizedString text;
0019     long int num;
0020 };
0021 
0022 static ListRef date_itemlist[] = {{kli18n("Do not Care"), 0},
0023 
0024                                   {kli18n("30 Min"), 30 * 60},
0025                                   {kli18n("1 Hour"), 1 * 60 * 60},
0026                                   {kli18n("2 Hours"), 2 * 60 * 60},
0027                                   {kli18n("4 Hours"), 4 * 60 * 60},
0028                                   {kli18n("8 Hours"), 8 * 60 * 60},
0029                                   {kli18n("12 Hours"), 12 * 60 * 60},
0030                                   {kli18n("18 Hours"), 18 * 60 * 60},
0031 
0032                                   {kli18n("1 Day"), 1 * 60 * 60 * 24},
0033                                   {kli18n("2 Days"), 2 * 60 * 60 * 24},
0034                                   {kli18n("3 Days"), 3 * 60 * 60 * 24},
0035                                   {kli18n("4 Days"), 4 * 60 * 60 * 24},
0036                                   {kli18n("5 Days"), 5 * 60 * 60 * 24},
0037                                   {kli18n("6 Days"), 6 * 60 * 60 * 24},
0038 
0039                                   {kli18n("1 Week"), 1 * 60 * 60 * 24 * 7},
0040                                   {kli18n("2 Weeks"), 2 * 60 * 60 * 24 * 7},
0041                                   {kli18n("3 Weeks"), 3 * 60 * 60 * 24 * 7},
0042                                   {kli18n("4 Weeks"), 4 * 60 * 60 * 24 * 7},
0043 
0044                                   {kli18n("1 Month"), 1 * 60 * 60 * 24 * 30},
0045                                   {kli18n("2 Months"), 2 * 60 * 60 * 24 * 30},
0046                                   {kli18n("3 Months"), 3 * 60 * 60 * 24 * 30},
0047                                   {kli18n("4 Months"), 4 * 60 * 60 * 24 * 30},
0048                                   {kli18n("5 Months"), 5 * 60 * 60 * 24 * 30},
0049                                   {kli18n("6 Months"), 6 * 60 * 60 * 24 * 30},
0050                                   {kli18n("10 Months"), 10 * 60 * 60 * 24 * 30},
0051                                   {kli18n("12 Months"), 12 * 60 * 60 * 24 * 30},
0052                                   {KLazyLocalizedString(), 0}};
0053 
0054 BlockOptions::BlockOptions(QWidget *parent)
0055     : QWidget(parent)
0056 {
0057     setupUi(this);
0058     connect(expire1, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0059     connect(block1, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0060     connect(expire2, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0061     connect(block2, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0062     connect(expire3, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0063     connect(block3, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0064     connect(expire4, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0065     connect(block4, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0066     connect(expire5, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0067     connect(block5, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0068     connect(expire6, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0069     connect(block6, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0070     connect(expire7, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0071     connect(block7, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &BlockOptions::slotComboBoxActivated);
0072 
0073     connect(kcfg_Block, &QCheckBox::toggled, this, &BlockOptions::slotBlockToggled);
0074     connect(kcfg_Expire, &QCheckBox::toggled, this, &BlockOptions::slotExpireToggled);
0075 
0076     fillWidgets();
0077     updateWidgets();
0078 }
0079 
0080 void BlockOptions::fillWidgets()
0081 {
0082     fillComboBox(block1);
0083     fillComboBox(block2);
0084     fillComboBox(block3);
0085     fillComboBox(block4);
0086     fillComboBox(block5);
0087     fillComboBox(block6);
0088     fillComboBox(block7);
0089 
0090     m_blockComboList.append(block1);
0091     m_blockComboList.append(block2);
0092     m_blockComboList.append(block3);
0093     m_blockComboList.append(block4);
0094     m_blockComboList.append(block5);
0095     m_blockComboList.append(block6);
0096     m_blockComboList.append(block7);
0097 
0098     fillComboBox(expire1);
0099     fillComboBox(expire2);
0100     fillComboBox(expire3);
0101     fillComboBox(expire4);
0102     fillComboBox(expire5);
0103     fillComboBox(expire6);
0104     fillComboBox(expire7);
0105 
0106     m_expireComboList.append(expire1);
0107     m_expireComboList.append(expire2);
0108     m_expireComboList.append(expire3);
0109     m_expireComboList.append(expire4);
0110     m_expireComboList.append(expire5);
0111     m_expireComboList.append(expire6);
0112     m_expireComboList.append(expire7);
0113 }
0114 
0115 void BlockOptions::updateWidgets()
0116 {
0117     updateComboBox(Prefs::blockItem(KV_LEV1_GRADE), block1);
0118     updateComboBox(Prefs::blockItem(KV_LEV2_GRADE), block2);
0119     updateComboBox(Prefs::blockItem(KV_LEV3_GRADE), block3);
0120     updateComboBox(Prefs::blockItem(KV_LEV4_GRADE), block4);
0121     updateComboBox(Prefs::blockItem(KV_LEV5_GRADE), block5);
0122     updateComboBox(Prefs::blockItem(KV_LEV6_GRADE), block6);
0123     updateComboBox(Prefs::blockItem(KV_LEV7_GRADE), block7);
0124 
0125     updateComboBox(Prefs::expireItem(KV_LEV1_GRADE), expire1);
0126     updateComboBox(Prefs::expireItem(KV_LEV2_GRADE), expire2);
0127     updateComboBox(Prefs::expireItem(KV_LEV3_GRADE), expire3);
0128     updateComboBox(Prefs::expireItem(KV_LEV4_GRADE), expire4);
0129     updateComboBox(Prefs::expireItem(KV_LEV5_GRADE), expire5);
0130     updateComboBox(Prefs::expireItem(KV_LEV6_GRADE), expire6);
0131     updateComboBox(Prefs::expireItem(KV_LEV7_GRADE), expire7);
0132 
0133     bool block = Prefs::block();
0134     kcfg_Block->setChecked(block);
0135     block1->setEnabled(block);
0136     block2->setEnabled(block);
0137     block3->setEnabled(block);
0138     block4->setEnabled(block);
0139     block5->setEnabled(block);
0140     block6->setEnabled(block);
0141     block7->setEnabled(block);
0142 
0143     bool expire = Prefs::expire();
0144     kcfg_Expire->setChecked(expire);
0145     expire1->setEnabled(expire);
0146     expire2->setEnabled(expire);
0147     expire3->setEnabled(expire);
0148     expire4->setEnabled(expire);
0149     expire5->setEnabled(expire);
0150     expire6->setEnabled(expire);
0151     expire7->setEnabled(expire);
0152 }
0153 
0154 void BlockOptions::fillComboBox(KComboBox *cb)
0155 {
0156     ListRef *ref = date_itemlist;
0157 
0158     cb->clear();
0159     while (!ref->text.isEmpty()) {
0160         cb->addItem(KLocalizedString(ref->text).toString());
0161         ref++;
0162     }
0163 }
0164 
0165 void BlockOptions::updateComboBox(int value, KComboBox *cb)
0166 {
0167     ListRef *ref = date_itemlist;
0168     int index = 0;
0169     while (!ref->text.isEmpty()) {
0170         if (value == ref->num)
0171             index = ref - date_itemlist;
0172         ref++;
0173     }
0174     cb->setCurrentIndex(index);
0175 }
0176 
0177 void BlockOptions::slotBlockToggled(bool state)
0178 {
0179     block1->setEnabled(state);
0180     block2->setEnabled(state);
0181     block3->setEnabled(state);
0182     block4->setEnabled(state);
0183     block5->setEnabled(state);
0184     block6->setEnabled(state);
0185     block7->setEnabled(state);
0186     if (state) {
0187         checkValidity();
0188     }
0189 }
0190 
0191 void BlockOptions::slotExpireToggled(bool state)
0192 {
0193     expire1->setEnabled(state);
0194     expire2->setEnabled(state);
0195     expire3->setEnabled(state);
0196     expire4->setEnabled(state);
0197     expire5->setEnabled(state);
0198     expire6->setEnabled(state);
0199     expire7->setEnabled(state);
0200     if (state) {
0201         checkValidity();
0202     }
0203 }
0204 
0205 void BlockOptions::slotComboBoxActivated(int)
0206 {
0207     Q_EMIT widgetModified();
0208     checkValidity();
0209 }
0210 
0211 void BlockOptions::checkValidity()
0212 {
0213     QString message;
0214     if (kcfg_Block->isChecked()) {
0215         bool found = false;
0216         for (int i = 1; i <= 6; i++) {
0217             if (date_itemlist[m_blockComboList[i - 1]->currentIndex()].num != 0 && date_itemlist[m_blockComboList[i]->currentIndex()].num != 0
0218                 && date_itemlist[m_blockComboList[i - 1]->currentIndex()].num >= date_itemlist[m_blockComboList[i]->currentIndex()].num) {
0219                 if (!found)
0220                     message.append(i18n("Illogical blocking times.\n"));
0221                 found = true;
0222                 message.append(i18n("The time for level %1 should be lower than the time for level %2.\n", i, i + 1));
0223             }
0224         }
0225     }
0226 
0227     if (kcfg_Expire->isChecked()) {
0228         bool found = false;
0229         for (int i = 1; i <= 6; i++) {
0230             if (date_itemlist[m_expireComboList[i - 1]->currentIndex()].num != 0 && date_itemlist[m_expireComboList[i]->currentIndex()].num != 0
0231                 && date_itemlist[m_expireComboList[i - 1]->currentIndex()].num >= date_itemlist[m_expireComboList[i]->currentIndex()].num) {
0232                 if (!found)
0233                     message.append(i18n("\nIllogical expiration times.\n"));
0234                 found = true;
0235                 message.append(i18n("The time for level %1 should be lower than the time for level %2.\n", i, i + 1));
0236             }
0237         }
0238     }
0239 
0240     if ((message.length() == 0) && kcfg_Block->isChecked() && kcfg_Expire->isChecked()) {
0241         bool found = false;
0242         for (int i = 0; i <= 6; i++) {
0243             if (m_expireComboList[i]->currentIndex() >= 0 && m_blockComboList[i]->currentIndex() >= 0
0244                 && date_itemlist[m_expireComboList[i]->currentIndex()].num != 0 && date_itemlist[m_blockComboList[i]->currentIndex()].num != 0
0245                 && date_itemlist[m_blockComboList[i]->currentIndex()].num >= date_itemlist[m_expireComboList[i]->currentIndex()].num) {
0246                 if (!found)
0247                     message.append(i18n("\nIllogical blocking vs. expiration times.\n"));
0248                 found = true;
0249                 message.append(i18n("The blocking time at level %1 should be lower than the expiration time.\n", i + 1));
0250             }
0251         }
0252     }
0253 
0254     if (message.length() != 0)
0255         KMessageBox::error(this, message, i18n("Illogical Values"));
0256 }
0257 
0258 bool BlockOptions::isDefault()
0259 {
0260     if (block7->currentIndex() < 0 || block6->currentIndex() < 0 || block5->currentIndex() < 0 || block4->currentIndex() < 0 || block3->currentIndex() < 0
0261         || block2->currentIndex() < 0 || block1->currentIndex() < 0 || expire7->currentIndex() < 0 || expire6->currentIndex() < 0 || expire5->currentIndex() < 0
0262         || expire4->currentIndex() < 0 || expire3->currentIndex() < 0 || expire2->currentIndex() < 0 || expire1->currentIndex() < 0)
0263         return false;
0264 
0265     return date_itemlist[block7->currentIndex()].num == 5184000 && date_itemlist[block6->currentIndex()].num == 2592000
0266         && date_itemlist[block5->currentIndex()].num == 1209600 && date_itemlist[block4->currentIndex()].num == 604800
0267         && date_itemlist[block3->currentIndex()].num == 345600 && date_itemlist[block2->currentIndex()].num == 172800
0268         && date_itemlist[block1->currentIndex()].num == 86400 && date_itemlist[expire7->currentIndex()].num == 10368000
0269         && date_itemlist[expire6->currentIndex()].num == 5184000 && date_itemlist[expire5->currentIndex()].num == 2592000
0270         && date_itemlist[expire4->currentIndex()].num == 1209600 && date_itemlist[expire3->currentIndex()].num == 604800
0271         && date_itemlist[expire2->currentIndex()].num == 345600 && date_itemlist[expire1->currentIndex()].num == 172800;
0272 }
0273 
0274 void BlockOptions::updateSettings()
0275 {
0276     if (block7->currentIndex() < 0 || block6->currentIndex() < 0 || block5->currentIndex() < 0 || block4->currentIndex() < 0 || block3->currentIndex() < 0
0277         || block2->currentIndex() < 0 || block1->currentIndex() < 0 || expire7->currentIndex() < 0 || expire6->currentIndex() < 0 || expire5->currentIndex() < 0
0278         || expire4->currentIndex() < 0 || expire3->currentIndex() < 0 || expire2->currentIndex() < 0 || expire1->currentIndex() < 0)
0279         return;
0280 
0281     Prefs::setBlockItem(KV_LEV7_GRADE, date_itemlist[qMax(block7->currentIndex(), 0)].num);
0282     Prefs::setBlockItem(KV_LEV6_GRADE, date_itemlist[qMax(block6->currentIndex(), 0)].num);
0283     Prefs::setBlockItem(KV_LEV5_GRADE, date_itemlist[qMax(block5->currentIndex(), 0)].num);
0284     Prefs::setBlockItem(KV_LEV4_GRADE, date_itemlist[qMax(block4->currentIndex(), 0)].num);
0285     Prefs::setBlockItem(KV_LEV3_GRADE, date_itemlist[qMax(block3->currentIndex(), 0)].num);
0286     Prefs::setBlockItem(KV_LEV2_GRADE, date_itemlist[qMax(block2->currentIndex(), 0)].num);
0287     Prefs::setBlockItem(KV_LEV1_GRADE, date_itemlist[qMax(block1->currentIndex(), 0)].num);
0288 
0289     Prefs::setExpireItem(KV_LEV7_GRADE, date_itemlist[qMax(expire7->currentIndex(), 0)].num);
0290     Prefs::setExpireItem(KV_LEV6_GRADE, date_itemlist[qMax(expire6->currentIndex(), 0)].num);
0291     Prefs::setExpireItem(KV_LEV5_GRADE, date_itemlist[qMax(expire5->currentIndex(), 0)].num);
0292     Prefs::setExpireItem(KV_LEV4_GRADE, date_itemlist[qMax(expire4->currentIndex(), 0)].num);
0293     Prefs::setExpireItem(KV_LEV3_GRADE, date_itemlist[qMax(expire3->currentIndex(), 0)].num);
0294     Prefs::setExpireItem(KV_LEV2_GRADE, date_itemlist[qMax(expire2->currentIndex(), 0)].num);
0295     Prefs::setExpireItem(KV_LEV1_GRADE, date_itemlist[qMax(expire1->currentIndex(), 0)].num);
0296 }
0297 
0298 bool BlockOptions::hasChanged()
0299 {
0300     if (block7->currentIndex() < 0 || block6->currentIndex() < 0 || block5->currentIndex() < 0 || block4->currentIndex() < 0 || block3->currentIndex() < 0
0301         || block2->currentIndex() < 0 || block1->currentIndex() < 0 || expire7->currentIndex() < 0 || expire6->currentIndex() < 0 || expire5->currentIndex() < 0
0302         || expire4->currentIndex() < 0 || expire3->currentIndex() < 0 || expire2->currentIndex() < 0 || expire1->currentIndex() < 0)
0303         return false;
0304 
0305     return date_itemlist[block7->currentIndex()].num != Prefs::blockItem(KV_LEV7_GRADE)
0306         || date_itemlist[block6->currentIndex()].num != Prefs::blockItem(KV_LEV6_GRADE)
0307         || date_itemlist[block5->currentIndex()].num != Prefs::blockItem(KV_LEV5_GRADE)
0308         || date_itemlist[block4->currentIndex()].num != Prefs::blockItem(KV_LEV4_GRADE)
0309         || date_itemlist[block3->currentIndex()].num != Prefs::blockItem(KV_LEV3_GRADE)
0310         || date_itemlist[block2->currentIndex()].num != Prefs::blockItem(KV_LEV2_GRADE)
0311         || date_itemlist[block1->currentIndex()].num != Prefs::blockItem(KV_LEV1_GRADE)
0312         || date_itemlist[expire7->currentIndex()].num != Prefs::expireItem(KV_LEV7_GRADE)
0313         || date_itemlist[expire6->currentIndex()].num != Prefs::expireItem(KV_LEV6_GRADE)
0314         || date_itemlist[expire5->currentIndex()].num != Prefs::expireItem(KV_LEV5_GRADE)
0315         || date_itemlist[expire4->currentIndex()].num != Prefs::expireItem(KV_LEV4_GRADE)
0316         || date_itemlist[expire3->currentIndex()].num != Prefs::expireItem(KV_LEV3_GRADE)
0317         || date_itemlist[expire2->currentIndex()].num != Prefs::expireItem(KV_LEV2_GRADE)
0318         || date_itemlist[expire1->currentIndex()].num != Prefs::expireItem(KV_LEV1_GRADE);
0319 }
0320 
0321 #include "moc_blockoptions.cpp"