newmailnotifiersettingsdialog.cpp 8.68 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    Copyright (c) 2013-2017 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "newmailnotifiersettingsdialog.h"
21
#include "newmailnotifierattribute.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "newmailnotifierselectcollectionwidget.h"
Laurent Montel's avatar
Laurent Montel committed
23
24
#include "newmailnotifieragentsettings.h"

Laurent Montel's avatar
Port++    
Laurent Montel committed
25
#include "kdepim-runtime-version.h"
26

Laurent Montel's avatar
Laurent Montel committed
27
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
28
#include <KNotifyConfigWidget>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QLineEdit>
30
#include <KCheckableProxyModel>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QPushButton>
32
#include <KHelpMenu>
Laurent Montel's avatar
Port++    
Laurent Montel committed
33
#include <kaboutdata.h>
Laurent Montel's avatar
Laurent Montel committed
34
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
35
36
37

#include <QTabWidget>
#include <QCheckBox>
Laurent Montel's avatar
Laurent Montel committed
38
#include <QGroupBox>
Laurent Montel's avatar
Laurent Montel committed
39
#include <QVBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
40
41
#include <QLabel>
#include <QWhatsThis>
Laurent Montel's avatar
Port++    
Laurent Montel committed
42
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
43

Laurent Montel's avatar
Laurent Montel committed
44
45
46
47
#include <AkonadiWidgets/CollectionView>
#include <AkonadiCore/RecursiveCollectionFilterProxyModel>
#include <AkonadiCore/CollectionFilterProxyModel>
#include <AkonadiCore/CollectionModifyJob>
Laurent Montel's avatar
Laurent Montel committed
48
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
49
50
#include <QDialogButtonBox>
#include <KConfigGroup>
51

Laurent Montel's avatar
Laurent Montel committed
52
53
static const char *textToSpeakMessage =
    I18N_NOOP("<qt>"
Laurent Montel's avatar
Laurent Montel committed
54
55
56
57
58
59
              "<p>Here you can define message. "
              "You can use:</p>"
              "<ul>"
              "<li>%s set subject</li>"
              "<li>%f set from</li>"
              "</ul>"
Laurent Montel's avatar
Laurent Montel committed
60
              "</qt>");
Laurent Montel's avatar
Laurent Montel committed
61

Laurent Montel's avatar
Laurent Montel committed
62
NewMailNotifierSettingsDialog::NewMailNotifierSettingsDialog(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
63
    : QDialog(parent)
Laurent Montel's avatar
Laurent Montel committed
64
{
Laurent Montel's avatar
Laurent Montel committed
65
    setWindowTitle(i18n("New Mail Notifier settings"));
Laurent Montel's avatar
Laurent Montel committed
66
    setWindowIcon(QIcon::fromTheme(QStringLiteral("kmail")));
Laurent Montel's avatar
Laurent Montel committed
67
68
69
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, this);
Laurent Montel's avatar
Laurent Montel committed
70
71
72
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
Laurent Montel's avatar
Laurent Montel committed
73
74
    connect(buttonBox, &QDialogButtonBox::accepted, this, &NewMailNotifierSettingsDialog::slotOkClicked);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &NewMailNotifierSettingsDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
75
76

    QTabWidget *tab = new QTabWidget;
Laurent Montel's avatar
Laurent Montel committed
77
    mainLayout->addWidget(tab);
Laurent Montel's avatar
Laurent Montel committed
78
79
80
81
82

    QWidget *settings = new QWidget;
    QVBoxLayout *vbox = new QVBoxLayout;
    settings->setLayout(vbox);

Laurent Montel's avatar
Laurent Montel committed
83
    QGroupBox *grp = new QGroupBox(i18n("Choose which fields to show:"), this);
Laurent Montel's avatar
Laurent Montel committed
84
85
86
87
    vbox->addWidget(grp);
    QVBoxLayout *groupboxLayout = new QVBoxLayout;
    grp->setLayout(groupboxLayout);

Laurent Montel's avatar
Laurent Montel committed
88
    mShowPhoto = new QCheckBox(i18n("Show Photo"), this);
Laurent Montel's avatar
Laurent Montel committed
89
    mShowPhoto->setChecked(NewMailNotifierAgentSettings::showPhoto());
Laurent Montel's avatar
Laurent Montel committed
90
    groupboxLayout->addWidget(mShowPhoto);
Laurent Montel's avatar
Laurent Montel committed
91

Laurent Montel's avatar
Laurent Montel committed
92
    mShowFrom = new QCheckBox(i18n("Show From"), this);
Laurent Montel's avatar
Laurent Montel committed
93
    mShowFrom->setChecked(NewMailNotifierAgentSettings::showFrom());
Laurent Montel's avatar
Laurent Montel committed
94
    groupboxLayout->addWidget(mShowFrom);
Laurent Montel's avatar
Laurent Montel committed
95

Laurent Montel's avatar
Laurent Montel committed
96
    mShowSubject = new QCheckBox(i18n("Show Subject"), this);
Laurent Montel's avatar
Laurent Montel committed
97
    mShowSubject->setChecked(NewMailNotifierAgentSettings::showSubject());
Laurent Montel's avatar
Laurent Montel committed
98
    groupboxLayout->addWidget(mShowSubject);
Laurent Montel's avatar
Laurent Montel committed
99

Laurent Montel's avatar
Laurent Montel committed
100
    mShowFolders = new QCheckBox(i18n("Show Folders"), this);
Laurent Montel's avatar
Laurent Montel committed
101
    mShowFolders->setChecked(NewMailNotifierAgentSettings::showFolder());
Laurent Montel's avatar
Laurent Montel committed
102
    groupboxLayout->addWidget(mShowFolders);
103

Laurent Montel's avatar
Laurent Montel committed
104
    mExcludeMySelf = new QCheckBox(i18n("Do not notify when email was sent by me"), this);
105
106
107
    mExcludeMySelf->setChecked(NewMailNotifierAgentSettings::excludeEmailsFromMe());
    vbox->addWidget(mExcludeMySelf);

Laurent Montel's avatar
Laurent Montel committed
108
    mAllowToShowMail = new QCheckBox(i18n("Show button to display mail"), this);
Laurent Montel's avatar
Laurent Montel committed
109
110
111
    mAllowToShowMail->setChecked(NewMailNotifierAgentSettings::showButtonToDisplayMail());
    vbox->addWidget(mAllowToShowMail);

Laurent Montel's avatar
Laurent Montel committed
112
113
114
    vbox->addStretch();
    tab->addTab(settings, i18n("Display"));

Laurent Montel's avatar
Laurent Montel committed
115
#ifdef HAVE_TEXTTOSPEECH
116
117
118
    QWidget *textSpeakWidget = new QWidget;
    vbox = new QVBoxLayout;
    textSpeakWidget->setLayout(vbox);
Laurent Montel's avatar
Laurent Montel committed
119
    mTextToSpeak = new QCheckBox(i18n("Enabled"), this);
120
121
122
    mTextToSpeak->setChecked(NewMailNotifierAgentSettings::textToSpeakEnabled());
    vbox->addWidget(mTextToSpeak);

Laurent Montel's avatar
Laurent Montel committed
123
    QLabel *howIsItWork = new QLabel(i18n("<a href=\"whatsthis\">How does this work?</a>"), this);
Laurent Montel's avatar
Laurent Montel committed
124
    howIsItWork->setTextInteractionFlags(Qt::LinksAccessibleByMouse);
125
    howIsItWork->setContextMenuPolicy(Qt::NoContextMenu);
Laurent Montel's avatar
Laurent Montel committed
126
    vbox->addWidget(howIsItWork);
Laurent Montel's avatar
Laurent Montel committed
127
    connect(howIsItWork, &QLabel::linkActivated, this, &NewMailNotifierSettingsDialog::slotHelpLinkClicked);
Laurent Montel's avatar
Laurent Montel committed
128
129
130

    QHBoxLayout *textToSpeakLayout = new QHBoxLayout;
    textToSpeakLayout->setMargin(0);
Laurent Montel's avatar
Laurent Montel committed
131
    QLabel *lab = new QLabel(i18n("Message:"), this);
Laurent Montel's avatar
Laurent Montel committed
132
    textToSpeakLayout->addWidget(lab);
Laurent Montel's avatar
Laurent Montel committed
133
134
    mTextToSpeakSetting = new QLineEdit;
    mTextToSpeakSetting->setClearButtonEnabled(true);
135
    mTextToSpeakSetting->setText(NewMailNotifierAgentSettings::textToSpeak());
Laurent Montel's avatar
Laurent Montel committed
136
    mTextToSpeakSetting->setEnabled(mTextToSpeak->isChecked());
Laurent Montel's avatar
Laurent Montel committed
137
    mTextToSpeakSetting->setWhatsThis(i18n(textToSpeakMessage));
Laurent Montel's avatar
Laurent Montel committed
138
139
    textToSpeakLayout->addWidget(mTextToSpeakSetting);
    vbox->addLayout(textToSpeakLayout);
140
141
    vbox->addStretch();
    tab->addTab(textSpeakWidget, i18n("Text to Speak"));
Laurent Montel's avatar
Laurent Montel committed
142
    connect(mTextToSpeak, &QCheckBox::toggled, mTextToSpeakSetting, &QLineEdit::setEnabled);
143
#else
Laurent Montel's avatar
Laurent Montel committed
144
145
    mTextToSpeak = nullptr;
    mTextToSpeakSetting = nullptr;
146
#endif
Laurent Montel's avatar
Laurent Montel committed
147

Laurent Montel's avatar
Laurent Montel committed
148
    mNotify = new KNotifyConfigWidget(this);
Laurent Montel's avatar
Laurent Montel committed
149
    mNotify->setApplication(QStringLiteral("akonadi_newmailnotifier_agent"));
Laurent Montel's avatar
Laurent Montel committed
150
151
    tab->addTab(mNotify, i18n("Notify"));

Laurent Montel's avatar
Laurent Montel committed
152
    mSelectCollection = new NewMailNotifierSelectCollectionWidget(this);
Laurent Montel's avatar
Laurent Montel committed
153
    tab->addTab(mSelectCollection, i18n("Folders"));
154

Laurent Montel's avatar
Port++    
Laurent Montel committed
155
    KAboutData aboutData = KAboutData(
Laurent Montel's avatar
Laurent Montel committed
156
                               QStringLiteral("newmailnotifieragent"),
Laurent Montel's avatar
Laurent Montel committed
157
                               i18n("New Mail Notifier Agent"),
158
                               QStringLiteral(KDEPIM_RUNTIME_VERSION),
Laurent Montel's avatar
Laurent Montel committed
159
                               i18n("Notify about new mails."),
Laurent Montel's avatar
Laurent Montel committed
160
                               KAboutLicense::GPL_V2,
Laurent Montel's avatar
Laurent Montel committed
161
                               i18n("Copyright (C) 2013-2017 Laurent Montel"));
Laurent Montel's avatar
Laurent Montel committed
162
163

    aboutData.addAuthor(i18n("Laurent Montel"),
Laurent Montel's avatar
Laurent Montel committed
164
                        i18n("Maintainer"), QStringLiteral("montel@kde.org"));
Laurent Montel's avatar
Laurent Montel committed
165
166
    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"),
                            i18nc("EMAIL OF TRANSLATORS", "Your emails"));
Laurent Montel's avatar
Port++    
Laurent Montel committed
167
168

    KHelpMenu *helpMenu = new KHelpMenu(this, aboutData, true);
Laurent Montel's avatar
Laurent Montel committed
169
    helpMenu->action(KHelpMenu::menuHelpContents)->setVisible(false);
170
    //Initialize menu
Laurent Montel's avatar
Laurent Montel committed
171
    QMenu *menu = helpMenu->menu();
Laurent Montel's avatar
Laurent Montel committed
172
    helpMenu->action(KHelpMenu::menuAboutApp)->setIcon(QIcon::fromTheme(QStringLiteral("kmail")));
Laurent Montel's avatar
Laurent Montel committed
173
    buttonBox->button(QDialogButtonBox::Help)->setMenu(menu);
Laurent Montel's avatar
Laurent Montel committed
174
175

    mainLayout->addWidget(buttonBox);
176
    readConfig();
Laurent Montel's avatar
Laurent Montel committed
177
178
179
180
}

NewMailNotifierSettingsDialog::~NewMailNotifierSettingsDialog()
{
181
    writeConfig();
Laurent Montel's avatar
Laurent Montel committed
182
183
}

184
185
186
187
static const char *myConfigGroupName = "NewMailNotifierDialog";

void NewMailNotifierSettingsDialog::readConfig()
{
Laurent Montel's avatar
Laurent Montel committed
188
    KConfigGroup group(KSharedConfig::openConfig(), myConfigGroupName);
189

Laurent Montel's avatar
Laurent Montel committed
190
191
192
    const QSize size = group.readEntry("Size", QSize(500, 300));
    if (size.isValid()) {
        resize(size);
193
194
195
196
197
    }
}

void NewMailNotifierSettingsDialog::writeConfig()
{
Laurent Montel's avatar
Laurent Montel committed
198
199
    KConfigGroup group(KSharedConfig::openConfig(), myConfigGroupName);
    group.writeEntry("Size", size());
200
201
202
    group.sync();
}

Laurent Montel's avatar
Laurent Montel committed
203
204
205
void NewMailNotifierSettingsDialog::slotHelpLinkClicked(const QString &)
{
    const QString help =
Laurent Montel's avatar
Laurent Montel committed
206
        i18n(textToSpeakMessage);
Laurent Montel's avatar
Laurent Montel committed
207

Laurent Montel's avatar
Laurent Montel committed
208
    QWhatsThis::showText(QCursor::pos(), help);
Laurent Montel's avatar
Laurent Montel committed
209
210
}

Laurent Montel's avatar
Laurent Montel committed
211
212
void NewMailNotifierSettingsDialog::slotOkClicked()
{
Laurent Montel's avatar
Laurent Montel committed
213
    mSelectCollection->updateCollectionsRecursive();
214

Laurent Montel's avatar
Laurent Montel committed
215
216
217
218
    NewMailNotifierAgentSettings::setShowPhoto(mShowPhoto->isChecked());
    NewMailNotifierAgentSettings::setShowFrom(mShowFrom->isChecked());
    NewMailNotifierAgentSettings::setShowSubject(mShowSubject->isChecked());
    NewMailNotifierAgentSettings::setShowFolder(mShowFolders->isChecked());
219
    NewMailNotifierAgentSettings::setExcludeEmailsFromMe(mExcludeMySelf->isChecked());
Laurent Montel's avatar
Laurent Montel committed
220
#ifdef HAVE_TEXTTOSPEECH
221
222
    NewMailNotifierAgentSettings::setTextToSpeakEnabled(mTextToSpeak->isChecked());
    NewMailNotifierAgentSettings::setTextToSpeak(mTextToSpeakSetting->text());
223
#endif
Laurent Montel's avatar
Laurent Montel committed
224
    NewMailNotifierAgentSettings::setShowButtonToDisplayMail(mAllowToShowMail->isChecked());
Laurent Montel's avatar
Laurent Montel committed
225
    NewMailNotifierAgentSettings::self()->save();
Laurent Montel's avatar
Laurent Montel committed
226
227
228
229
    mNotify->save();
    accept();
}