Astyle kdelibs
[kdepim.git] / kmail / configuredialog / configureaccountpage.cpp
blob5f770e06b50cea406af28c3233223d4495ab52e7
1 /*
2 Copyright (c) 2013, 2014 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "configureaccountpage.h"
19 #include "dialog/kmknotify.h"
20 #include "newmailnotifierinterface.h"
21 #include "kmkernel.h"
22 #include "settings/globalsettings.h"
23 #include "configagentdelegate.h"
24 #include "messagecomposer/settings/messagecomposersettings.h"
25 #include "mailcommon/folder/accountconfigorderdialog.h"
26 #include "pimcommon/widgets/configureimmutablewidgetutils.h"
27 using namespace PimCommon::ConfigureImmutableWidgetUtils;
28 #include <MailTransport/mailtransport/transportmanagementwidget.h>
29 using MailTransport::TransportManagementWidget;
30 #include "ui_accountspagereceivingtab.h"
31 #include "mailcommon/util/mailutil.h"
33 #include <AkonadiCore/agentfilterproxymodel.h>
34 #include <AkonadiCore/agentinstancemodel.h>
35 #include <AkonadiCore/agenttype.h>
36 #include <AkonadiCore/agentmanager.h>
37 #include <AkonadiWidgets/agenttypedialog.h>
38 #include <AkonadiCore/agentinstancecreatejob.h>
40 #include <KLocalizedString>
41 #include <KMessageBox>
42 #include <KComboBox>
43 #include <KWindowSystem>
44 #include <QDebug>
46 #include <QVBoxLayout>
47 #include <QGridLayout>
48 #include <QGroupBox>
49 #include <QMenu>
50 #include <KConfigGroup>
51 #include <QLabel>
53 QString AccountsPage::helpAnchor() const
55 return QString::fromLatin1("configure-accounts");
58 AccountsPage::AccountsPage(QWidget *parent)
59 : ConfigModuleWithTabs(parent)
62 // "Receiving" tab:
64 mReceivingTab = new ReceivingTab();
65 addTab(mReceivingTab, i18nc("@title:tab Tab page where the user configures accounts to receive mail", "Receiving"));
66 connect(mReceivingTab, SIGNAL(accountListChanged(QStringList)),
67 this, SIGNAL(accountListChanged(QStringList)));
70 // "Sending" tab:
72 mSendingTab = new SendingTab();
73 addTab(mSendingTab, i18nc("@title:tab Tab page where the user configures accounts to send mail", "Sending"));
76 AccountsPageSendingTab::~AccountsPageSendingTab()
80 QString AccountsPage::SendingTab::helpAnchor() const
82 return QString::fromLatin1("configure-accounts-sending");
85 AccountsPageSendingTab::AccountsPageSendingTab(QWidget *parent)
86 : ConfigModuleTab(parent)
88 QVBoxLayout *vlay = new QVBoxLayout(this);
89 //TODO PORT QT5 vlay->setSpacing( QDialog::spacingHint() );
90 //TODO PORT QT5 vlay->setMargin( QDialog::marginHint() );
91 // label: zero stretch ### FIXME more
92 vlay->addWidget(new QLabel(i18n("Outgoing accounts (add at least one):"), this));
94 TransportManagementWidget *tmw = new TransportManagementWidget(this);
95 tmw->layout()->setContentsMargins(0, 0, 0, 0);
96 vlay->addWidget(tmw);
98 // "Common options" groupbox:
99 QGroupBox *group = new QGroupBox(i18n("Common Options"), this);
100 vlay->addWidget(group);
102 // a grid layout for the contents of the "common options" group box
103 QGridLayout *glay = new QGridLayout();
104 group->setLayout(glay);
105 //TODO PORT QT5 glay->setSpacing( QDialog::spacingHint() );
106 glay->setColumnStretch(2, 10);
108 // "confirm before send" check box:
109 mConfirmSendCheck = new QCheckBox(i18n("Confirm &before send"), group);
110 glay->addWidget(mConfirmSendCheck, 0, 0, 1, 2);
111 connect(mConfirmSendCheck, SIGNAL(stateChanged(int)),
112 this, SLOT(slotEmitChanged()));
114 mCheckSpellingBeforeSending = new QCheckBox(i18n("Check spelling before sending"), group);
115 glay->addWidget(mCheckSpellingBeforeSending, 1, 0, 1, 2);
116 connect(mCheckSpellingBeforeSending, SIGNAL(stateChanged(int)),
117 this, SLOT(slotEmitChanged()));
119 // "send on check" combo:
120 mSendOnCheckCombo = new KComboBox(group);
121 mSendOnCheckCombo->setEditable(false);
122 mSendOnCheckCombo->addItems(QStringList()
123 << i18n("Never Automatically")
124 << i18n("On Manual Mail Checks")
125 << i18n("On All Mail Checks"));
126 glay->addWidget(mSendOnCheckCombo, 2, 1);
127 connect(mSendOnCheckCombo, SIGNAL(activated(int)),
128 this, SLOT(slotEmitChanged()));
130 // "default send method" combo:
131 mSendMethodCombo = new KComboBox(group);
132 mSendMethodCombo->setEditable(false);
133 mSendMethodCombo->addItems(QStringList()
134 << i18n("Send Now")
135 << i18n("Send Later"));
136 glay->addWidget(mSendMethodCombo, 3, 1);
137 connect(mSendMethodCombo, SIGNAL(activated(int)),
138 this, SLOT(slotEmitChanged()));
140 // labels:
141 QLabel *l = new QLabel(i18n("Send &messages in outbox folder:"), group);
142 l->setBuddy(mSendOnCheckCombo);
143 glay->addWidget(l, 2, 0);
145 QString msg = i18n(GlobalSettings::self()->sendOnCheckItem()->whatsThis().toUtf8());
146 l->setWhatsThis(msg);
147 mSendOnCheckCombo->setWhatsThis(msg);
149 l = new QLabel(i18n("Defa&ult send method:"), group);
150 l->setBuddy(mSendMethodCombo);
151 glay->addWidget(l, 3, 0);
154 void AccountsPage::SendingTab::doLoadFromGlobalSettings()
156 mSendOnCheckCombo->setCurrentIndex(GlobalSettings::self()->sendOnCheck());
159 void AccountsPage::SendingTab::doLoadOther()
161 mSendMethodCombo->setCurrentIndex(MessageComposer::MessageComposerSettings::self()->sendImmediate() ? 0 : 1);
162 loadWidget(mConfirmSendCheck, GlobalSettings::self()->confirmBeforeSendItem());
163 loadWidget(mCheckSpellingBeforeSending, GlobalSettings::self()->checkSpellingBeforeSendItem());
166 void AccountsPage::SendingTab::save()
168 GlobalSettings::self()->setSendOnCheck(mSendOnCheckCombo->currentIndex());
169 saveCheckBox(mConfirmSendCheck, GlobalSettings::self()->confirmBeforeSendItem());
170 saveCheckBox(mCheckSpellingBeforeSending, GlobalSettings::self()->checkSpellingBeforeSendItem());
171 MessageComposer::MessageComposerSettings::self()->setSendImmediate(mSendMethodCombo->currentIndex() == 0);
174 QString AccountsPage::ReceivingTab::helpAnchor() const
176 return QString::fromLatin1("configure-accounts-receiving");
179 AccountsPageReceivingTab::AccountsPageReceivingTab(QWidget *parent)
180 : ConfigModuleTab(parent)
182 mNewMailNotifierInterface = new OrgFreedesktopAkonadiNewMailNotifierInterface(QLatin1String("org.freedesktop.Akonadi.NewMailNotifierAgent"), QLatin1String("/NewMailNotifierAgent"), QDBusConnection::sessionBus(), this);
183 if (!mNewMailNotifierInterface->isValid()) {
184 qDebug() << " org.freedesktop.Akonadi.NewMailNotifierAgent not found. Please verify your installation";
186 mAccountsReceiving.setupUi(this);
188 //TODO PORT QT5 mAccountsReceiving.vlay->setSpacing( QDialog::spacingHint() );
189 //TODO PORT QT5 mAccountsReceiving.vlay->setMargin( QDialog::marginHint() );
191 mAccountsReceiving.mAccountsReceiving->setMimeTypeFilter(QStringList() << KMime::Message::mimeType());
192 mAccountsReceiving.mAccountsReceiving->setCapabilityFilter(QStringList() << QLatin1String("Resource"));
193 mAccountsReceiving.mAccountsReceiving->setExcludeCapabilities(QStringList() << QLatin1String("MailTransport") << QLatin1String("Notes"));
195 KConfig specialMailCollection(QLatin1String("specialmailcollectionsrc"));
196 if (specialMailCollection.hasGroup(QLatin1String("SpecialCollections"))) {
197 KConfigGroup grp = specialMailCollection.group(QLatin1String("SpecialCollections"));
198 mAccountsReceiving.mAccountsReceiving->setSpecialCollectionIdentifier(grp.readEntry(QLatin1String("DefaultResourceId")));
200 ConfigAgentDelegate *configDelegate = new ConfigAgentDelegate(mAccountsReceiving.mAccountsReceiving->view());
201 mAccountsReceiving.mAccountsReceiving->setItemDelegate(configDelegate);
202 connect(configDelegate, SIGNAL(optionsClicked(QString,QPoint)), this, SLOT(slotShowMailCheckMenu(QString,QPoint)));
204 //TODO PORT QT5 mAccountsReceiving.group->layout()->setMargin( QDialog::marginHint() );
205 //TODO PORT QT5 mAccountsReceiving.group->layout()->setSpacing( QDialog::spacingHint() );
207 connect(mAccountsReceiving.mBeepNewMailCheck, SIGNAL(stateChanged(int)),
208 this, SLOT(slotEmitChanged()));
210 connect(mAccountsReceiving.mVerboseNotificationCheck, SIGNAL(stateChanged(int)),
211 this, SLOT(slotEmitChanged()));
213 connect(mAccountsReceiving.mOtherNewMailActionsButton, SIGNAL(clicked()),
214 this, SLOT(slotEditNotifications()));
215 connect(mAccountsReceiving.customizeAccountOrder, SIGNAL(clicked()), this, SLOT(slotCustomizeAccountOrder()));
218 AccountsPageReceivingTab::~AccountsPageReceivingTab()
220 delete mNewMailNotifierInterface;
221 mRetrievalHash.clear();
224 void AccountsPageReceivingTab::slotCustomizeAccountOrder()
226 MailCommon::AccountConfigOrderDialog dlg(this);
227 dlg.exec();
230 void AccountsPageReceivingTab::slotShowMailCheckMenu(const QString &ident, const QPoint &pos)
232 QMenu *menu = new QMenu(this);
234 bool IncludeInManualChecks;
235 bool OfflineOnShutdown;
236 bool CheckOnStartup;
237 if (!mRetrievalHash.contains(ident)) {
238 const QString resourceGroupPattern(QLatin1String("Resource %1"));
240 KConfigGroup group;
241 KConfig *conf = 0;
242 if (KMKernel::self()) {
243 group = KConfigGroup(KMKernel::self()->config(), resourceGroupPattern.arg(ident));
244 } else {
245 conf = new KConfig(QLatin1String("kmail2rc"));
246 group = KConfigGroup(conf, resourceGroupPattern.arg(ident));
249 IncludeInManualChecks = group.readEntry("IncludeInManualChecks", true);
251 // Keep sync with kmkernel, don't forget to change there.
252 OfflineOnShutdown = group.readEntry("OfflineOnShutdown", ident.startsWith(QLatin1String("akonadi_pop3_resource")) ? true : false);
254 CheckOnStartup = group.readEntry("CheckOnStartup", false);
255 QSharedPointer<RetrievalOptions> opts(new RetrievalOptions(IncludeInManualChecks, OfflineOnShutdown, CheckOnStartup));
256 mRetrievalHash.insert(ident, opts);
257 delete conf;
258 } else {
259 QSharedPointer<RetrievalOptions> opts = mRetrievalHash.value(ident);
260 IncludeInManualChecks = opts->IncludeInManualChecks;
261 OfflineOnShutdown = opts->OfflineOnShutdown;
262 CheckOnStartup = opts->CheckOnStartup;
265 if (!MailCommon::Util::isVirtualCollection(ident)) {
266 QAction *manualMailCheck = new QAction(i18nc("Label to a checkbox, so is either checked/unchecked", "Include in Manual Mail Check"), menu);
267 manualMailCheck->setCheckable(true);
268 manualMailCheck->setChecked(IncludeInManualChecks);
269 manualMailCheck->setData(ident);
270 menu->addAction(manualMailCheck);
271 connect(manualMailCheck, SIGNAL(toggled(bool)), this, SLOT(slotIncludeInCheckChanged(bool)));
274 if (!MailCommon::Util::isLocalCollection(ident)) {
275 QAction *switchOffline = new QAction(i18nc("Label to a checkbox, so is either checked/unchecked", "Switch offline on KMail Shutdown"), menu);
276 switchOffline->setCheckable(true);
277 switchOffline->setChecked(OfflineOnShutdown);
278 switchOffline->setData(ident);
279 menu->addAction(switchOffline);
280 connect(switchOffline, SIGNAL(toggled(bool)), this, SLOT(slotOfflineOnShutdownChanged(bool)));
283 QAction *checkOnStartup = new QAction(i18n("Check mail on startup"), menu);
284 checkOnStartup->setCheckable(true);
285 checkOnStartup->setChecked(CheckOnStartup);
286 checkOnStartup->setData(ident);
287 menu->addAction(checkOnStartup);
289 connect(checkOnStartup, SIGNAL(toggled(bool)), this, SLOT(slotCheckOnStatupChanged(bool)));
291 menu->exec(mAccountsReceiving.mAccountsReceiving->view()->mapToGlobal(pos));
292 delete menu;
295 void AccountsPageReceivingTab::slotCheckOnStatupChanged(bool checked)
297 QAction *action = qobject_cast< QAction * >(sender());
298 const QString ident = action->data().toString();
300 QSharedPointer<RetrievalOptions> opts = mRetrievalHash.value(ident);
301 opts->CheckOnStartup = checked;
302 slotEmitChanged();
305 void AccountsPageReceivingTab::slotIncludeInCheckChanged(bool checked)
307 QAction *action = qobject_cast< QAction * >(sender());
308 const QString ident = action->data().toString();
310 QSharedPointer<RetrievalOptions> opts = mRetrievalHash.value(ident);
311 opts->IncludeInManualChecks = checked;
312 slotEmitChanged();
315 void AccountsPageReceivingTab::slotOfflineOnShutdownChanged(bool checked)
317 QAction *action = qobject_cast< QAction * >(sender());
318 QString ident = action->data().toString();
320 QSharedPointer<RetrievalOptions> opts = mRetrievalHash.value(ident);
321 opts->OfflineOnShutdown = checked;
322 slotEmitChanged();
325 void AccountsPage::ReceivingTab::slotEditNotifications()
327 QDBusInterface interface(QLatin1String("org.freedesktop.Akonadi.Agent.akonadi_newmailnotifier_agent"), QLatin1String("/NewMailNotifierAgent"));
328 if (interface.isValid()) {
329 interface.call(QLatin1String("showConfigureDialog"), (qlonglong)winId());
330 } else {
331 KMessageBox::error(this, i18n("New Mail Notifier Agent not registered. Please contact your administrator."));
335 void AccountsPage::ReceivingTab::doLoadFromGlobalSettings()
337 mAccountsReceiving.mVerboseNotificationCheck->setChecked(mNewMailNotifierInterface->verboseMailNotification());
340 void AccountsPage::ReceivingTab::doLoadOther()
342 mAccountsReceiving.mBeepNewMailCheck->setChecked(mNewMailNotifierInterface->beepOnNewMails());
345 void AccountsPage::ReceivingTab::save()
347 // Save Mail notification settings
348 mNewMailNotifierInterface->setBeepOnNewMails(mAccountsReceiving.mBeepNewMailCheck->isChecked());
349 mNewMailNotifierInterface->setVerboseMailNotification(mAccountsReceiving.mVerboseNotificationCheck->isChecked());
351 const QString resourceGroupPattern(QLatin1String("Resource %1"));
352 QHashIterator<QString, QSharedPointer<RetrievalOptions> > it(mRetrievalHash);
353 while (it.hasNext()) {
354 it.next();
355 KConfigGroup group;
356 KConfig *conf = 0;
357 if (KMKernel::self()) {
358 group = KConfigGroup(KMKernel::self()->config(), resourceGroupPattern.arg(it.key()));
359 } else {
360 conf = new KConfig(QLatin1String("kmail2rc"));
361 group = KConfigGroup(conf, resourceGroupPattern.arg(it.key()));
363 QSharedPointer<RetrievalOptions> opts = it.value();
364 group.writeEntry("IncludeInManualChecks", opts->IncludeInManualChecks);
365 group.writeEntry("OfflineOnShutdown", opts->OfflineOnShutdown);
366 group.writeEntry("CheckOnStartup", opts->CheckOnStartup);
367 delete conf;