Libksieve

generateglobalscriptjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "generateglobalscriptjob.h"
8 
9 #include "kmanagesieve/sievejob.h"
10 
11 #include <KLocalizedString>
12 
13 using namespace KSieveUi;
14 GenerateGlobalScriptJob::GenerateGlobalScriptJob(const QUrl &url, QObject *parent)
15  : QObject(parent)
16  , mCurrentUrl(url)
17 {
18 }
19 
20 GenerateGlobalScriptJob::~GenerateGlobalScriptJob()
21 {
22  kill();
23 }
24 
25 void GenerateGlobalScriptJob::kill()
26 {
27  if (mMasterJob) {
28  mMasterJob->kill();
29  }
30  mMasterJob = nullptr;
31 
32  if (mUserJob) {
33  mUserJob->kill();
34  mUserJob = nullptr;
35  }
36  mUserJob = nullptr;
37 }
38 
39 void GenerateGlobalScriptJob::addUserActiveScripts(const QStringList &lstScript)
40 {
41  mListUserActiveScripts = lstScript;
42 }
43 
44 void GenerateGlobalScriptJob::setForceActivateUserScript(bool f)
45 {
46  mForceActivateUserScript = f;
47 }
48 
49 void GenerateGlobalScriptJob::start()
50 {
51  if (mCurrentUrl.isEmpty()) {
52  Q_EMIT error(i18n("Path is not specified."));
53  return;
54  }
55  writeUserScript();
56 }
57 
58 // TODO why it's not activated ????
59 void GenerateGlobalScriptJob::writeMasterScript()
60 {
61  const QString masterScript = QStringLiteral(
62  "# MASTER\n"
63  "#\n"
64  "# This file is authoritative for your system and MUST BE KEPT ACTIVE.\n"
65  "#\n"
66  "# Altering it is likely to render your account dysfunctional and may\n"
67  "# be violating your organizational or corporate policies.\n"
68  "# \n"
69  "# For more information on the mechanism and the conventions behind\n"
70  "# this script, see http://wiki.kolab.org/KEP:14\n"
71  "#\n"
72  "\n"
73  "require [\"include\"];\n"
74  "\n"
75  "# OPTIONAL: Includes for all or a group of users\n"
76  "# include :global \"all-users\";\n"
77  "# include :global \"this-group-of-users\";\n"
78  "\n"
79  "# The script maintained by the general management system\n"
80  "include :personal :optional \"MANAGEMENT\";\n"
81  "\n"
82  "# The script(s) maintained by one or more editors available to the user\n"
83  "include :personal :optional \"USER\";\n");
84 
85  QUrl url(mCurrentUrl);
86  url = url.adjusted(QUrl::RemoveFilename);
87  url.setPath(url.path() + QLatin1Char('/') + QLatin1String("MASTER"));
88  mMasterJob = KManageSieve::SieveJob::put(url, masterScript, true, true);
89  connect(mMasterJob, &KManageSieve::SieveJob::result, this, &GenerateGlobalScriptJob::slotPutMasterResult);
90 }
91 
92 void GenerateGlobalScriptJob::slotPutMasterResult(KManageSieve::SieveJob *job, bool success)
93 {
94  if (!success) {
95  Q_EMIT error(
96  i18n("Error writing \"MASTER\" script on server.\n"
97  "The server responded:\n%1",
98  job->errorString()));
99  return;
100  }
101  mMasterJob = nullptr;
102  writeUserScript();
103 }
104 
105 void GenerateGlobalScriptJob::writeUserScript()
106 {
107  QString userScript = QStringLiteral(
108  "# USER Management Script\n"
109  "#\n"
110  "# This script includes the various active sieve scripts\n"
111  "# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n"
112  "# \n"
113  "# For more information, see http://wiki.kolab.org/KEP:14#USER\n"
114  "#\n"
115  "\n"
116  "require [\"include\"];\n");
117 
118  for (const QString &activeScript : qAsConst(mListUserActiveScripts)) {
119  userScript += QStringLiteral("\ninclude :personal \"%1\";").arg(activeScript);
120  }
121 
122  QUrl url(mCurrentUrl);
123  url = url.adjusted(QUrl::RemoveFilename);
124  url.setPath(url.path() + QLatin1Char('/') + QLatin1String("USER"));
125  mUserJob = KManageSieve::SieveJob::put(url, userScript, mForceActivateUserScript, false);
126  connect(mUserJob, &KManageSieve::SieveJob::result, this, &GenerateGlobalScriptJob::slotPutUserResult);
127 }
128 
129 void GenerateGlobalScriptJob::slotPutUserResult(KManageSieve::SieveJob *job, bool success)
130 {
131  mUserJob = nullptr;
132  if (!success) {
133  Q_EMIT error(
134  i18n("Error writing \"User\" script on server.\n"
135  "The server responded:\n%1",
136  job->errorString()));
137  return;
138  }
139  disableAllOtherScripts();
140 }
141 
142 void GenerateGlobalScriptJob::disableAllOtherScripts()
143 {
144  // TODO
145  Q_EMIT success();
146 }
RemoveFilename
static SieveJob * put(const QUrl &destination, const QString &script, bool makeActive, bool wasActive)
Stores a sieve script on an IMAP server.
Definition: sievejob.cpp:278
void result(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted for all kind of jobs when they have finished.
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:268
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A job to manage sieve scripts.
Definition: sievejob.h:37
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.