File indexing completed on 2024-04-28 04:58:25

0001 /*
0002     kcmhistory.cpp
0003     SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0004     SPDX-FileCopyrightText: 2002 Stephan Binner <binner@kde.org>
0005 
0006     based on kcmtaskbar.cpp
0007     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 // Own
0013 #include "kcmhistory.h"
0014 
0015 // Qt
0016 #include <QCheckBox>
0017 #include <QPushButton>
0018 #include <QIcon>
0019 #include <QFontDialog>
0020 
0021 // KDE
0022 #include <KConfig>
0023 #include <KConfigGroup>
0024 // #include <KFontChooser>
0025 #include <KLocalizedString>
0026 #include <kmessagebox.h>
0027 #include <KPluginFactory>
0028 
0029 #include <konq_historyprovider.h>
0030 #include <konqhistorysettings.h>
0031 
0032 // Local
0033 
0034 K_PLUGIN_CLASS_WITH_JSON(HistorySidebarConfig, "kcmhistory.json")
0035 
0036 HistorySidebarConfig::HistorySidebarConfig(QObject *parent, const KPluginMetaData& md, const QVariantList &list)
0037     : KCModule(parent, md)
0038 {
0039     m_settings = KonqHistorySettings::self();
0040 
0041     if (!HistoryProvider::exists()) {
0042         new KonqHistoryProvider(this);
0043     }
0044 
0045     QVBoxLayout *topLayout = new QVBoxLayout(widget());
0046     dialog = new KonqSidebarHistoryDlg(widget());
0047 
0048     dialog->comboDefaultAction->addItem(i18nc("Automatically decide which action to perform", "Auto"));
0049     dialog->comboDefaultAction->addItem(i18nc("Open URL in new tab", "Open in new tab"));
0050     dialog->comboDefaultAction->addItem(i18nc("Open URL in current tab", "Open in current tab"));
0051     dialog->comboDefaultAction->addItem(i18nc("Open URL in new window", "Open in new window"));
0052 
0053     QString defaultActionToolTip = i18n("Action to carry out when opening an URL from history:<dl>"
0054         "<dl><dt>Auto:</dt><dd>opens the URL in a new tab unless the current tab is empty or an intro page</dd>"
0055         "<dt>Open URL in a new tab:</dt><dd>always opens the URL in a new tab</dd>"
0056         "<dt>Open URL in current tab:</dt><dd>always opens the URL in the current tab</dd>"
0057         "<dt>Open URL in new window:</dt><dd>always opens the URL in a new window</dd>"
0058         "</dl>"
0059     );
0060     dialog->setToolTip(defaultActionToolTip);
0061 
0062     dialog->spinEntries->setRange(0, INT_MAX);
0063     dialog->spinExpire->setRange(0, INT_MAX);
0064     dialog->spinExpire->setSuffix(i18n("days"));
0065 
0066     dialog->spinNewer->setRange(0, INT_MAX);
0067     dialog->spinOlder->setRange(0, INT_MAX);
0068 
0069     dialog->comboNewer->insertItem(KonqHistorySettings::MINUTES,
0070                                    i18np("Minute", "Minutes", 0));
0071     dialog->comboNewer->insertItem(KonqHistorySettings::DAYS,
0072                                    i18np("Day", "Days", 0));
0073 
0074     dialog->comboOlder->insertItem(KonqHistorySettings::MINUTES,
0075                                    i18np("Minute", "Minutes", 0));
0076     dialog->comboOlder->insertItem(KonqHistorySettings::DAYS,
0077                                    i18np("Day", "Days", 0));
0078 
0079     connect(dialog->comboDefaultAction, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &HistorySidebarConfig::configChanged);
0080 
0081     connect(dialog->cbExpire, &QAbstractButton::toggled, dialog->spinExpire, &QWidget::setEnabled);
0082     connect(dialog->spinExpire, QOverload<int>::of(&QSpinBox::valueChanged), this, &HistorySidebarConfig::slotExpireChanged);
0083 
0084     connect(dialog->spinNewer, QOverload<int>::of(&QSpinBox::valueChanged), this, &HistorySidebarConfig::slotNewerChanged);
0085     connect(dialog->spinOlder, QOverload<int>::of(&QSpinBox::valueChanged), this, &HistorySidebarConfig::slotOlderChanged);
0086 
0087     connect(dialog->btnFontNewer, &QAbstractButton::clicked, this, &HistorySidebarConfig::slotGetFontNewer);
0088     connect(dialog->btnFontOlder, &QAbstractButton::clicked, this, &HistorySidebarConfig::slotGetFontOlder);
0089     connect(dialog->btnClearHistory, &QAbstractButton::clicked, this, &HistorySidebarConfig::slotClearHistory);
0090 
0091     connect(dialog->cbDetailedTips, &QAbstractButton::toggled, this, &HistorySidebarConfig::configChanged);
0092     connect(dialog->cbExpire, &QAbstractButton::toggled, this, &HistorySidebarConfig::configChanged);
0093     connect(dialog->spinEntries, QOverload<int>::of(&QSpinBox::valueChanged), this, &HistorySidebarConfig::configChanged);
0094     connect(dialog->comboNewer, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &HistorySidebarConfig::configChanged);
0095     connect(dialog->comboOlder, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &HistorySidebarConfig::configChanged);
0096 
0097     dialog->show();
0098     topLayout->addWidget(dialog);
0099     load();
0100 }
0101 
0102 void HistorySidebarConfig::configChanged()
0103 {
0104     setNeedsSave(true);
0105 }
0106 
0107 void HistorySidebarConfig::load()
0108 {
0109     KConfig _config("konquerorrc");
0110     KConfigGroup config(&_config, "HistorySettings");
0111 
0112     dialog->comboDefaultAction->setCurrentIndex(config.readEntry("Default Action", 0));
0113 
0114     dialog->spinExpire->setValue(config.readEntry("Maximum age of History entries", 90));
0115     dialog->spinEntries->setValue(config.readEntry("Maximum of History entries", 500));
0116     dialog->cbExpire->setChecked(dialog->spinExpire->value() > 0);
0117 
0118     dialog->spinNewer->setValue(m_settings->m_valueYoungerThan);
0119     dialog->spinOlder->setValue(m_settings->m_valueOlderThan);
0120 
0121     dialog->comboNewer->setCurrentIndex(m_settings->m_metricYoungerThan);
0122     dialog->comboOlder->setCurrentIndex(m_settings->m_metricOlderThan);
0123 
0124     dialog->cbDetailedTips->setChecked(m_settings->m_detailedTips);
0125 
0126     m_fontNewer = m_settings->m_fontYoungerThan;
0127     m_fontOlder = m_settings->m_fontOlderThan;
0128 
0129     // enable/disable widgets
0130     dialog->spinExpire->setEnabled(dialog->cbExpire->isChecked());
0131 
0132     slotExpireChanged();
0133     slotNewerChanged(dialog->spinNewer->value());
0134     slotOlderChanged(dialog->spinOlder->value());
0135 
0136     setNeedsSave(false);
0137 }
0138 
0139 void HistorySidebarConfig::save()
0140 {
0141     quint32 age   = dialog->cbExpire->isChecked() ? dialog->spinExpire->value() : 0;
0142     quint32 count = dialog->spinEntries->value();
0143 
0144     KonqHistoryProvider::self()->emitSetMaxAge(age);
0145     KonqHistoryProvider::self()->emitSetMaxCount(count);
0146 
0147     m_settings->m_defaultAction = static_cast<KonqHistorySettings::Action>(dialog->comboDefaultAction->currentIndex());
0148 
0149     m_settings->m_valueYoungerThan = dialog->spinNewer->value();
0150     m_settings->m_valueOlderThan   = dialog->spinOlder->value();
0151 
0152     m_settings->m_metricYoungerThan = dialog->comboNewer->currentIndex();
0153     m_settings->m_metricOlderThan   = dialog->comboOlder->currentIndex();
0154 
0155     m_settings->m_detailedTips = dialog->cbDetailedTips->isChecked();
0156 
0157     m_settings->m_fontYoungerThan = m_fontNewer;
0158     m_settings->m_fontOlderThan   = m_fontOlder;
0159 
0160     m_settings->applySettings();
0161 
0162     setNeedsSave(false);
0163 }
0164 
0165 void HistorySidebarConfig::defaults()
0166 {
0167     dialog->comboDefaultAction->setCurrentIndex(0);
0168 
0169     dialog->spinEntries->setValue(500);
0170     dialog->cbExpire->setChecked(true);
0171     dialog->spinExpire->setValue(90);
0172 
0173     dialog->spinNewer->setValue(1);
0174     dialog->spinOlder->setValue(2);
0175 
0176     dialog->comboNewer->setCurrentIndex(KonqHistorySettings::DAYS);
0177     dialog->comboOlder->setCurrentIndex(KonqHistorySettings::DAYS);
0178 
0179     dialog->cbDetailedTips->setChecked(true);
0180 
0181     m_fontNewer = QFont();
0182     m_fontNewer.setItalic(true);
0183     m_fontOlder = QFont();
0184 }
0185 
0186 void HistorySidebarConfig::slotExpireChanged()
0187 {
0188     configChanged();
0189 }
0190 
0191 // change hour to days, minute to minutes and the other way round,
0192 // depending on the value of the spinbox, and synchronize the two spinBoxes
0193 // to enforce newer <= older.
0194 void HistorySidebarConfig::slotNewerChanged(int value)
0195 {
0196     dialog->comboNewer->setItemText(KonqHistorySettings::DAYS,
0197                                     i18np("Day", "Days", value));
0198     dialog->comboNewer->setItemText(KonqHistorySettings::MINUTES,
0199                                     i18np("Minute", "Minutes", value));
0200 
0201     if (dialog->spinNewer->value() > dialog->spinOlder->value()) {
0202         dialog->spinOlder->setValue(dialog->spinNewer->value());
0203     }
0204     configChanged();
0205 }
0206 
0207 void HistorySidebarConfig::slotOlderChanged(int value)
0208 {
0209     dialog->comboOlder->setItemText(KonqHistorySettings::DAYS,
0210                                     i18np("Day", "Days", value));
0211     dialog->comboOlder->setItemText(KonqHistorySettings::MINUTES,
0212                                     i18np("Minute", "Minutes", value));
0213 
0214     if (dialog->spinNewer->value() > dialog->spinOlder->value()) {
0215         dialog->spinNewer->setValue(dialog->spinOlder->value());
0216     }
0217 
0218     configChanged();
0219 }
0220 
0221 void HistorySidebarConfig::slotGetFontNewer()
0222 {
0223     bool ok = false;
0224     m_fontNewer = QFontDialog::getFont(&ok, m_fontNewer, widget());
0225     if (ok) {
0226         configChanged();
0227     }
0228 }
0229 
0230 void HistorySidebarConfig::slotGetFontOlder()
0231 {
0232     bool ok = false;
0233     m_fontOlder = QFontDialog::getFont(&ok, m_fontOlder, widget());
0234     if (ok) {
0235         configChanged();
0236     }
0237 }
0238 
0239 void HistorySidebarConfig::slotClearHistory()
0240 {
0241     KGuiItem guiitem = KStandardGuiItem::clear();
0242     guiitem.setIcon(QIcon::fromTheme("edit-clear-history"));
0243     if (KMessageBox::warningContinueCancel(widget(),
0244                                            i18n("Do you really want to clear "
0245                                                    "the entire history?"),
0246                                            i18nc("@title:window", "Clear History?"), guiitem)
0247             == KMessageBox::Continue) {
0248         KonqHistoryProvider::self()->emitClear();
0249     }
0250 }
0251 
0252 #include "kcmhistory.moc"