1 /*
2    SPDX-FileCopyrightText: 2014-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "kmlaunchexternalcomponent.h"
8 #include <Akonadi/AgentConfigurationDialog>
9 #include <Akonadi/AgentManager>
10 #include <KLocalizedString>
11 #include <KMessageBox>
12 
13 #include "archivemailagentinterface.h"
14 #include "followupreminderinterface.h"
15 #include "mailmergeagentinterface.h"
16 #include "sendlateragentinterface.h"
17 #include "util.h"
18 #include <MailCommon/FilterManager>
19 
20 #include <KDialogJobUiDelegate>
21 #include <KIO/CommandLauncherJob>
22 #include <QPointer>
23 
24 #include "kmail_debug.h"
25 #include <QProcess>
26 #include <QStandardPaths>
27 
KMLaunchExternalComponent(QWidget * parentWidget,QObject * parent)28 KMLaunchExternalComponent::KMLaunchExternalComponent(QWidget *parentWidget, QObject *parent)
29     : QObject(parent)
30     , mParentWidget(parentWidget)
31 {
32 }
33 
34 KMLaunchExternalComponent::~KMLaunchExternalComponent() = default;
35 
slotConfigureAutomaticArchiving()36 void KMLaunchExternalComponent::slotConfigureAutomaticArchiving()
37 {
38     auto agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_archivemail_agent"));
39     if (agent.isValid()) {
40         QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(agent, mParentWidget);
41         dlg->exec();
42         delete dlg;
43     } else {
44         KMessageBox::error(mParentWidget, i18n("Archive Mail Agent was not registered."));
45     }
46 }
47 
slotConfigureSendLater()48 void KMLaunchExternalComponent::slotConfigureSendLater()
49 {
50     auto agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_sendlater_agent"));
51     if (agent.isValid()) {
52         QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(agent, mParentWidget);
53         dlg->exec();
54         delete dlg;
55     } else {
56         KMessageBox::error(mParentWidget, i18n("Send Later Agent was not registered."));
57     }
58 }
59 
slotConfigureMailMerge()60 void KMLaunchExternalComponent::slotConfigureMailMerge()
61 {
62     auto agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_mailmerge_agent"));
63     if (agent.isValid()) {
64         QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(agent, mParentWidget);
65         dlg->exec();
66         delete dlg;
67     } else {
68         KMessageBox::error(mParentWidget, i18n("Mail Merge Agent was not registered."));
69     }
70 }
71 
slotConfigureFollowupReminder()72 void KMLaunchExternalComponent::slotConfigureFollowupReminder()
73 {
74     auto agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_followupreminder_agent"));
75     if (agent.isValid()) {
76         QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(agent, mParentWidget);
77         dlg->exec();
78         delete dlg;
79     } else {
80         KMessageBox::error(mParentWidget, i18n("Followup Reminder Agent was not registered."));
81     }
82 }
83 
slotStartCertManager()84 void KMLaunchExternalComponent::slotStartCertManager()
85 {
86     const QString exec = QStandardPaths::findExecutable(QStringLiteral("kleopatra"));
87     if (exec.isEmpty() || !QProcess::startDetached(exec, QStringList())) {
88         KMessageBox::error(mParentWidget,
89                            i18n("Could not start certificate manager; "
90                                 "please make sure you have Kleopatra properly installed."),
91                            i18n("KMail Error"));
92     }
93 }
94 
slotImportWizard()95 void KMLaunchExternalComponent::slotImportWizard()
96 {
97     const QString path = QStandardPaths::findExecutable(QStringLiteral("akonadiimportwizard"));
98     if (path.isEmpty() || !QProcess::startDetached(path, QStringList())) {
99         KMessageBox::error(mParentWidget,
100                            i18n("Could not start the import wizard. "
101                                 "Please make sure you have ImportWizard properly installed."),
102                            i18n("Unable to start import wizard"));
103     }
104 }
105 
slotExportData()106 void KMLaunchExternalComponent::slotExportData()
107 {
108     const QString path = QStandardPaths::findExecutable(QStringLiteral("pimdataexporter"));
109     if (path.isEmpty() || !QProcess::startDetached(path, QStringList())) {
110         KMessageBox::error(mParentWidget,
111                            i18n("Could not start \"PIM Data Exporter\" program. "
112                                 "Please check your installation."),
113                            i18n("Unable to start \"PIM Data Exporter\" program"));
114     }
115 }
116 
slotRunAddressBook()117 void KMLaunchExternalComponent::slotRunAddressBook()
118 {
119     auto job = new KIO::CommandLauncherJob(QStringLiteral("kaddressbook"), {}, this);
120     job->setDesktopName(QStringLiteral("org.kde.kaddressbook"));
121     job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, mParentWidget->window()));
122     job->start();
123 }
124 
slotImport()125 void KMLaunchExternalComponent::slotImport()
126 {
127     const QStringList lst = {QStringLiteral("--mode"), QStringLiteral("manual")};
128     const QString path = QStandardPaths::findExecutable(QStringLiteral("akonadiimportwizard"));
129     if (path.isEmpty() || !QProcess::startDetached(path, lst)) {
130         KMessageBox::error(mParentWidget,
131                            i18n("Could not start the ImportWizard. "
132                                 "Please make sure you have ImportWizard properly installed."),
133                            i18n("Unable to start ImportWizard"));
134     }
135 }
136 
slotAccountWizard()137 void KMLaunchExternalComponent::slotAccountWizard()
138 {
139     const QStringList lst = {QStringLiteral("--type"), QStringLiteral("message/rfc822")};
140 
141     const QString path = QStandardPaths::findExecutable(QStringLiteral("accountwizard"));
142     if (path.isEmpty() || !QProcess::startDetached(path, lst)) {
143         KMessageBox::error(mParentWidget,
144                            i18n("Could not start the account wizard. "
145                                 "Please make sure you have AccountWizard properly installed."),
146                            i18n("Unable to start account wizard"));
147     }
148 }
149 
slotFilterLogViewer()150 void KMLaunchExternalComponent::slotFilterLogViewer()
151 {
152     MailCommon::FilterManager::instance()->showFilterLogDialog(static_cast<qlonglong>(mParentWidget->winId()));
153 }
154