Libksieve

generateglobalscriptjob.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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
13using namespace KSieveCore;
14GenerateGlobalScriptJob::GenerateGlobalScriptJob(const QUrl &url, QObject *parent)
15 : QObject(parent)
16 , mCurrentUrl(url)
17{
18}
19
20GenerateGlobalScriptJob::~GenerateGlobalScriptJob()
21{
22 kill();
23}
24
25void 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
39void GenerateGlobalScriptJob::addUserActiveScripts(const QStringList &lstScript)
40{
41 mListUserActiveScripts = lstScript;
42}
43
44void GenerateGlobalScriptJob::setForceActivateUserScript(bool f)
45{
46 mForceActivateUserScript = f;
47}
48
49void 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 ????
59void 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('/') + QLatin1StringView("MASTER"));
88 mMasterJob = KManageSieve::SieveJob::put(url, masterScript, true, true);
89 connect(mMasterJob, &KManageSieve::SieveJob::result, this, &GenerateGlobalScriptJob::slotPutMasterResult);
90}
91
92void 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
105void 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 : std::as_const(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('/') + QLatin1StringView("USER"));
125 mUserJob = KManageSieve::SieveJob::put(url, userScript, mForceActivateUserScript, false);
126 connect(mUserJob, &KManageSieve::SieveJob::result, this, &GenerateGlobalScriptJob::slotPutUserResult);
127}
128
129void 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
142void GenerateGlobalScriptJob::disableAllOtherScripts()
143{
144 // TODO
145 Q_EMIT success();
146}
147
148#include "moc_generateglobalscriptjob.cpp"
A job to manage sieve scripts.
Definition sievejob.h:31
void kill(KJob::KillVerbosity verbosity=KJob::Quietly)
Kills the sieve job.
Definition sievejob.cpp:246
QString errorString() const
A human-readable error message.
Definition sievejob.cpp:268
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.
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
QString i18n(const char *text, const TYPE &arg...)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
RemoveFilename
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.