File indexing completed on 2024-12-22 04:56:53

0001 /*
0002     SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "newmailnotifiersettingswidget.h"
0008 #include "newmailnotifieragentsettings.h"
0009 #include "newmailnotifierselectcollectionwidget.h"
0010 
0011 #include "kdepim-runtime-version.h"
0012 
0013 #include <KAboutData>
0014 #include <KLineEditEventHandler>
0015 #include <KLocalizedString>
0016 #include <KNotifyConfigWidget>
0017 #include <QLineEdit>
0018 #include <QPushButton>
0019 
0020 #include <QCheckBox>
0021 #include <QComboBox>
0022 #include <QGroupBox>
0023 #include <QLabel>
0024 #include <QTabWidget>
0025 #include <QVBoxLayout>
0026 #include <QWhatsThis>
0027 
0028 #include <KLazyLocalizedString>
0029 #include <KSharedConfig>
0030 
0031 static KLazyLocalizedString textToSpeakMessage = kli18n(
0032     "<qt>"
0033     "<p>Here you can define message. "
0034     "You can use:</p>"
0035     "<ul>"
0036     "<li>%s set subject</li>"
0037     "<li>%f set from</li>"
0038     "</ul>"
0039     "</qt>");
0040 
0041 NewMailNotifierSettingsWidget::NewMailNotifierSettingsWidget(const KSharedConfigPtr &config, QWidget *parent, const QVariantList &args)
0042     : Akonadi::AgentConfigurationBase(config, parent, args)
0043     , mSelectCollection(new NewMailNotifierSelectCollectionWidget(parent))
0044 {
0045     NewMailNotifierAgentSettings::instance(config);
0046     setObjectName(QLatin1StringView("NewMailNotifierSettingsWidget"));
0047     auto tab = new QTabWidget(parent);
0048     parent->layout()->addWidget(tab);
0049 
0050     auto settings = new QWidget;
0051     auto vbox = new QVBoxLayout(settings);
0052 
0053     auto grp = new QGroupBox(i18n("Choose which fields to show:"), parent);
0054     vbox->addWidget(grp);
0055     auto groupboxLayout = new QVBoxLayout;
0056     grp->setLayout(groupboxLayout);
0057 
0058     mShowPhoto = new QCheckBox(i18n("Show Photo"), parent);
0059     mShowPhoto->setObjectName(QLatin1StringView("mShowPhoto"));
0060     groupboxLayout->addWidget(mShowPhoto);
0061 
0062     mShowFrom = new QCheckBox(i18n("Show From"), parent);
0063     mShowFrom->setObjectName(QLatin1StringView("mShowFrom"));
0064     groupboxLayout->addWidget(mShowFrom);
0065 
0066     mShowSubject = new QCheckBox(i18n("Show Subject"), parent);
0067     mShowSubject->setObjectName(QLatin1StringView("mShowSubject"));
0068     groupboxLayout->addWidget(mShowSubject);
0069 
0070     mShowFolders = new QCheckBox(i18n("Show Folders"), parent);
0071     mShowFolders->setObjectName(QLatin1StringView("mShowFolders"));
0072     groupboxLayout->addWidget(mShowFolders);
0073 
0074     mExcludeMySelf = new QCheckBox(i18n("Do not notify when email was sent by me"), parent);
0075     mExcludeMySelf->setObjectName(QLatin1StringView("mExcludeMySelf"));
0076     vbox->addWidget(mExcludeMySelf);
0077 
0078     mKeepPersistentNotification = new QCheckBox(i18n("Keep Persistent Notification"), parent);
0079     mKeepPersistentNotification->setObjectName(QLatin1StringView("mKeepPersistentNotification"));
0080     vbox->addWidget(mKeepPersistentNotification);
0081 
0082     mAllowToShowMail = new QCheckBox(i18n("Show Action Buttons"), parent);
0083     mAllowToShowMail->setObjectName(QLatin1StringView("mAllowToShowMail"));
0084     vbox->addWidget(mAllowToShowMail);
0085 
0086     auto hboxLayout = new QHBoxLayout;
0087     hboxLayout->setObjectName(QLatin1StringView("hboxLayout"));
0088     vbox->addLayout(hboxLayout);
0089 
0090     mReplyMail = new QCheckBox(i18n("Reply Mail"), parent);
0091     mReplyMail->setObjectName(QLatin1StringView("mReplyMail"));
0092     hboxLayout->addWidget(mReplyMail);
0093     mReplyMail->setEnabled(false);
0094 
0095     mReplyMailTypeComboBox = new QComboBox(parent);
0096     mReplyMailTypeComboBox->setObjectName(QLatin1StringView("mReplyMailTypeComboBox"));
0097     mReplyMailTypeComboBox->setEnabled(false);
0098     mReplyMailTypeComboBox->addItems({i18n("Reply to Author"), i18n("Reply to All")});
0099     hboxLayout->addWidget(mReplyMailTypeComboBox);
0100     hboxLayout->addStretch(1);
0101 
0102     connect(mAllowToShowMail, &QCheckBox::clicked, this, [this](bool enabled) {
0103         updateReplyMail(enabled);
0104     });
0105 
0106     vbox->addStretch();
0107     tab->addTab(settings, i18n("Display"));
0108 #if HAVE_TEXT_TO_SPEECH_SUPPORT
0109     auto textSpeakWidget = new QWidget;
0110     vbox = new QVBoxLayout;
0111     textSpeakWidget->setLayout(vbox);
0112     mTextToSpeak = new QCheckBox(i18n("Enabled"), parent);
0113     mTextToSpeak->setObjectName(QLatin1StringView("mTextToSpeak"));
0114     vbox->addWidget(mTextToSpeak);
0115 
0116     auto howIsItWork = new QLabel(i18n("<a href=\"whatsthis\">How does this work?</a>"), parent);
0117     howIsItWork->setObjectName(QLatin1StringView("howIsItWork"));
0118     howIsItWork->setTextInteractionFlags(Qt::LinksAccessibleByMouse);
0119     howIsItWork->setContextMenuPolicy(Qt::NoContextMenu);
0120     vbox->addWidget(howIsItWork);
0121     connect(howIsItWork, &QLabel::linkActivated, this, &NewMailNotifierSettingsWidget::slotHelpLinkClicked);
0122 
0123     auto textToSpeakLayout = new QHBoxLayout;
0124     textToSpeakLayout->setContentsMargins({});
0125     auto lab = new QLabel(i18n("Message:"), parent);
0126     lab->setObjectName(QLatin1StringView("labmessage"));
0127     textToSpeakLayout->addWidget(lab);
0128     mTextToSpeakSetting = new QLineEdit(parent);
0129     mTextToSpeakSetting->setObjectName(QLatin1StringView("mTextToSpeakSetting"));
0130     mTextToSpeakSetting->setClearButtonEnabled(true);
0131     KLineEditEventHandler::catchReturnKey(mTextToSpeakSetting);
0132 
0133     mTextToSpeakSetting->setWhatsThis(textToSpeakMessage.toString());
0134     textToSpeakLayout->addWidget(mTextToSpeakSetting);
0135     vbox->addLayout(textToSpeakLayout);
0136     vbox->addStretch();
0137     tab->addTab(textSpeakWidget, i18n("Text to Speak"));
0138     connect(mTextToSpeak, &QCheckBox::toggled, mTextToSpeakSetting, &QLineEdit::setEnabled);
0139 #endif
0140     mNotify = new KNotifyConfigWidget(parent);
0141     mNotify->setObjectName(QLatin1StringView("mNotify"));
0142     mNotify->setApplication(QStringLiteral("akonadi_newmailnotifier_agent"));
0143     tab->addTab(mNotify, i18n("Notify"));
0144 
0145     mSelectCollection->setObjectName(QLatin1StringView("mSelectCollection"));
0146     tab->addTab(mSelectCollection, i18n("Folders"));
0147 
0148     KAboutData aboutData = KAboutData(QStringLiteral("newmailnotifieragent"),
0149                                       i18n("New Mail Notifier Agent"),
0150                                       QStringLiteral(KDEPIM_RUNTIME_VERSION),
0151                                       i18n("Notify about new mails."),
0152                                       KAboutLicense::GPL_V2,
0153                                       i18n("Copyright (C) 2013-%1 Laurent Montel", QStringLiteral("2023")));
0154 
0155     aboutData.setProductName(QByteArrayLiteral("Akonadi/New Mail Notifier"));
0156     aboutData.addAuthor(i18n("Laurent Montel"), i18n("Maintainer"), QStringLiteral("montel@kde.org"));
0157     aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0158     setKAboutData(aboutData);
0159 }
0160 
0161 NewMailNotifierSettingsWidget::~NewMailNotifierSettingsWidget()
0162 {
0163     delete NewMailNotifierAgentSettings::self();
0164 }
0165 
0166 void NewMailNotifierSettingsWidget::updateReplyMail(bool enabled)
0167 {
0168     mReplyMail->setEnabled(enabled);
0169     mReplyMailTypeComboBox->setEnabled(enabled);
0170 }
0171 
0172 void NewMailNotifierSettingsWidget::load()
0173 {
0174     Akonadi::AgentConfigurationBase::load();
0175 
0176     auto settings = NewMailNotifierAgentSettings::self();
0177     settings->load();
0178 
0179     mShowPhoto->setChecked(settings->showPhoto());
0180     mShowFrom->setChecked(settings->showFrom());
0181     mShowSubject->setChecked(settings->showSubject());
0182     mShowFolders->setChecked(settings->showFolder());
0183     mExcludeMySelf->setChecked(settings->excludeEmailsFromMe());
0184     mAllowToShowMail->setChecked(settings->showButtonToDisplayMail());
0185     mKeepPersistentNotification->setChecked(settings->keepPersistentNotification());
0186 #if HAVE_TEXT_TO_SPEECH_SUPPORT
0187     mTextToSpeak->setChecked(settings->textToSpeakEnabled());
0188     mTextToSpeakSetting->setEnabled(mTextToSpeak->isChecked());
0189     mTextToSpeakSetting->setText(settings->textToSpeak());
0190 #endif
0191     mReplyMail->setChecked(settings->replyMail());
0192     mReplyMailTypeComboBox->setCurrentIndex(settings->replyMailType());
0193 
0194     updateReplyMail(mAllowToShowMail->isChecked());
0195 }
0196 
0197 bool NewMailNotifierSettingsWidget::save() const
0198 {
0199     mSelectCollection->updateCollectionsRecursive();
0200     auto settings = NewMailNotifierAgentSettings::self();
0201     settings->setShowPhoto(mShowPhoto->isChecked());
0202     settings->setShowFrom(mShowFrom->isChecked());
0203     settings->setShowSubject(mShowSubject->isChecked());
0204     settings->setShowFolder(mShowFolders->isChecked());
0205     settings->setExcludeEmailsFromMe(mExcludeMySelf->isChecked());
0206     settings->setShowButtonToDisplayMail(mAllowToShowMail->isChecked());
0207     settings->setKeepPersistentNotification(mKeepPersistentNotification->isChecked());
0208 #if HAVE_TEXT_TO_SPEECH_SUPPORT
0209     settings->setTextToSpeakEnabled(mTextToSpeak->isChecked());
0210     settings->setTextToSpeak(mTextToSpeakSetting->text());
0211 #endif
0212     settings->setReplyMail(mReplyMail->isChecked());
0213     settings->setReplyMailType(mReplyMailTypeComboBox->currentIndex());
0214     settings->save();
0215     mNotify->save();
0216 
0217     return Akonadi::AgentConfigurationBase::save();
0218 }
0219 
0220 void NewMailNotifierSettingsWidget::slotHelpLinkClicked(const QString &)
0221 {
0222     const QString help = textToSpeakMessage.toString();
0223     QWhatsThis::showText(QCursor::pos(), help);
0224 }
0225 
0226 #include "moc_newmailnotifiersettingswidget.cpp"