File indexing completed on 2024-12-22 05:01:03

0001 /*
0002    SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kmknotify.h"
0008 #include "kmkernel.h"
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 #include <KNotifyConfigWidget>
0014 #include <KSeparator>
0015 #include <KWindowConfig>
0016 #include <QComboBox>
0017 #include <QDialogButtonBox>
0018 #include <QPushButton>
0019 #include <QStandardPaths>
0020 #include <QVBoxLayout>
0021 #include <QWindow>
0022 
0023 using namespace KMail;
0024 
0025 KMKnotify::KMKnotify(QWidget *parent)
0026     : QDialog(parent)
0027     , m_comboNotify(new QComboBox(this))
0028     , m_notifyWidget(new KNotifyConfigWidget(this))
0029 {
0030     setWindowTitle(i18nc("@title:window", "Notification"));
0031     auto mainLayout = new QVBoxLayout(this);
0032 
0033     m_comboNotify->setSizeAdjustPolicy(QComboBox::AdjustToContents);
0034     mainLayout->addWidget(m_comboNotify);
0035 
0036     mainLayout->addWidget(m_notifyWidget);
0037     m_comboNotify->setFocus();
0038 
0039     mainLayout->addWidget(new KSeparator(this));
0040 
0041     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0042     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0043     okButton->setDefault(true);
0044     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0045     connect(buttonBox, &QDialogButtonBox::accepted, this, &KMKnotify::accept);
0046     connect(buttonBox, &QDialogButtonBox::rejected, this, &KMKnotify::reject);
0047 
0048     mainLayout->addWidget(buttonBox);
0049 
0050     connect(m_comboNotify, &QComboBox::activated, this, &KMKnotify::slotComboChanged);
0051     connect(okButton, &QPushButton::clicked, this, &KMKnotify::slotOk);
0052     connect(m_notifyWidget, &KNotifyConfigWidget::changed, this, &KMKnotify::slotConfigChanged);
0053     initCombobox();
0054     readConfig();
0055 }
0056 
0057 KMKnotify::~KMKnotify()
0058 {
0059     writeConfig();
0060 }
0061 
0062 void KMKnotify::slotConfigChanged(bool changed)
0063 {
0064     m_changed = changed;
0065 }
0066 
0067 void KMKnotify::slotComboChanged(int index)
0068 {
0069     QString text(m_comboNotify->itemData(index).toString());
0070     if (m_changed) {
0071         m_notifyWidget->save();
0072         m_changed = false;
0073     }
0074     m_notifyWidget->setApplication(text);
0075 }
0076 
0077 void KMKnotify::setCurrentNotification(const QString &name)
0078 {
0079     const int index = m_comboNotify->findData(name);
0080     if (index > -1) {
0081         m_comboNotify->setCurrentIndex(index);
0082         slotComboChanged(index);
0083     }
0084 }
0085 
0086 void KMKnotify::initCombobox()
0087 {
0088     const QStringList lstNotify = QStringList() << QStringLiteral("kmail2.notifyrc") << QStringLiteral("akonadi_maildispatcher_agent.notifyrc")
0089                                                 << QStringLiteral("akonadi_mailfilter_agent.notifyrc") << QStringLiteral("akonadi_archivemail_agent.notifyrc")
0090                                                 << QStringLiteral("akonadi_sendlater_agent.notifyrc")
0091                                                 << QStringLiteral("akonadi_newmailnotifier_agent.notifyrc")
0092                                                 << QStringLiteral("akonadi_followupreminder_agent.notifyrc") << QStringLiteral("messageviewer.notifyrc");
0093     for (const QString &notify : lstNotify) {
0094         const QString fullPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1StringView("knotifications6/") + notify);
0095 
0096         if (!fullPath.isEmpty()) {
0097             const int slash = fullPath.lastIndexOf(QLatin1Char('/'));
0098             QString appname = fullPath.right(fullPath.length() - slash - 1);
0099             appname.remove(QLatin1StringView(".notifyrc"));
0100             if (!appname.isEmpty()) {
0101                 KConfig config(fullPath, KConfig::NoGlobals, QStandardPaths::AppLocalDataLocation);
0102                 KConfigGroup globalConfig(&config, QStringLiteral("Global"));
0103                 const QString icon = globalConfig.readEntry(QStringLiteral("IconName"), QStringLiteral("misc"));
0104                 const QString description = globalConfig.readEntry(QStringLiteral("Comment"), appname);
0105                 m_comboNotify->addItem(QIcon::fromTheme(icon), description, appname);
0106             }
0107         }
0108     }
0109 
0110     m_comboNotify->model()->sort(0);
0111     if (m_comboNotify->count() > 0) {
0112         m_comboNotify->setCurrentIndex(0);
0113         m_notifyWidget->setApplication(m_comboNotify->itemData(0).toString());
0114     }
0115 }
0116 
0117 void KMKnotify::slotOk()
0118 {
0119     if (m_changed) {
0120         m_notifyWidget->save();
0121     }
0122 }
0123 
0124 void KMKnotify::readConfig()
0125 {
0126     create(); // ensure a window is created
0127     windowHandle()->resize(QSize(600, 400));
0128     KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("KMKnotifyDialog"));
0129     KWindowConfig::restoreWindowSize(windowHandle(), group);
0130     resize(windowHandle()->size()); // workaround for QTBUG-40584
0131 }
0132 
0133 void KMKnotify::writeConfig()
0134 {
0135     KConfigGroup notifyDialog(KSharedConfig::openStateConfig(), QStringLiteral("KMKnotifyDialog"));
0136     KWindowConfig::saveWindowSize(windowHandle(), notifyDialog);
0137     notifyDialog.sync();
0138 }
0139 
0140 #include "moc_kmknotify.cpp"