Changeset View
Changeset View
Standalone View
Standalone View
agents/followupreminderagent/followupreminderutil.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2014-2020 Laurent Montel <montel@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program; see the file COPYING. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "followupreminderutil.h" | ||||
21 | #include "followupreminderinfo.h" | ||||
22 | #include <AkonadiCore/ServerManager> | ||||
23 | | ||||
24 | #include <QDBusInterface> | ||||
25 | #include "followupreminderagentsettings.h" | ||||
26 | | ||||
27 | namespace { | ||||
28 | QString serviceName() | ||||
29 | { | ||||
30 | return Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Agent, | ||||
31 | QStringLiteral("akonadi_followupreminder_agent")); | ||||
32 | } | ||||
33 | | ||||
34 | QString dbusPath() | ||||
35 | { | ||||
36 | return QStringLiteral("/FollowUpReminder"); | ||||
37 | } | ||||
38 | } | ||||
39 | | ||||
40 | bool FollowUpReminder::FollowUpReminderUtil::followupReminderAgentWasRegistered() | ||||
41 | { | ||||
42 | QDBusInterface interface(serviceName(), dbusPath()); | ||||
43 | return interface.isValid(); | ||||
44 | } | ||||
45 | | ||||
46 | bool FollowUpReminder::FollowUpReminderUtil::followupReminderAgentEnabled() | ||||
47 | { | ||||
48 | return FollowUpReminderAgentSettings::self()->enabled(); | ||||
49 | } | ||||
50 | | ||||
51 | void FollowUpReminder::FollowUpReminderUtil::reload() | ||||
52 | { | ||||
53 | QDBusInterface interface(serviceName(), dbusPath()); | ||||
54 | if (interface.isValid()) { | ||||
55 | interface.call(QStringLiteral("reload")); | ||||
56 | } | ||||
57 | } | ||||
58 | | ||||
59 | void FollowUpReminder::FollowUpReminderUtil::forceReparseConfiguration() | ||||
60 | { | ||||
61 | FollowUpReminderAgentSettings::self()->save(); | ||||
62 | FollowUpReminderAgentSettings::self()->config()->reparseConfiguration(); | ||||
63 | } | ||||
64 | | ||||
65 | KSharedConfig::Ptr FollowUpReminder::FollowUpReminderUtil::defaultConfig() | ||||
66 | { | ||||
67 | return KSharedConfig::openConfig(QStringLiteral("akonadi_followupreminder_agentrc"), KConfig::SimpleConfig); | ||||
68 | } | ||||
69 | | ||||
70 | void FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(KSharedConfig::Ptr config, FollowUpReminder::FollowUpReminderInfo *info, bool forceReload) | ||||
71 | { | ||||
72 | if (!info || !info->isValid()) { | ||||
73 | return; | ||||
74 | } | ||||
75 | | ||||
76 | KConfigGroup general = config->group(QStringLiteral("General")); | ||||
77 | int value = general.readEntry("Number", 0); | ||||
78 | int identifier = info->uniqueIdentifier(); | ||||
79 | if (identifier == -1) { | ||||
80 | identifier = value; | ||||
81 | } | ||||
82 | ++value; | ||||
83 | | ||||
84 | const QString groupName = FollowUpReminder::FollowUpReminderUtil::followUpReminderPattern().arg(identifier); | ||||
85 | // first, delete all filter groups: | ||||
86 | const QStringList filterGroups = config->groupList(); | ||||
87 | for (const QString &group : filterGroups) { | ||||
88 | if (group == groupName) { | ||||
89 | config->deleteGroup(group); | ||||
90 | } | ||||
91 | } | ||||
92 | KConfigGroup group = config->group(groupName); | ||||
93 | info->writeConfig(group, identifier); | ||||
94 | | ||||
95 | general.writeEntry("Number", value); | ||||
96 | | ||||
97 | config->sync(); | ||||
98 | config->reparseConfiguration(); | ||||
99 | if (forceReload) { | ||||
100 | reload(); | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | bool FollowUpReminder::FollowUpReminderUtil::removeFollowupReminderInfo(KSharedConfig::Ptr config, const QList<qint32> &listRemove, bool forceReload) | ||||
105 | { | ||||
106 | if (listRemove.isEmpty()) { | ||||
107 | return false; | ||||
108 | } | ||||
109 | | ||||
110 | bool needSaveConfig = false; | ||||
111 | KConfigGroup general = config->group(QStringLiteral("General")); | ||||
112 | int value = general.readEntry("Number", 0); | ||||
113 | | ||||
114 | for (qint32 identifier : listRemove) { | ||||
115 | const QString groupName = FollowUpReminder::FollowUpReminderUtil::followUpReminderPattern().arg(identifier); | ||||
116 | const QStringList filterGroups = config->groupList(); | ||||
117 | for (const QString &group : filterGroups) { | ||||
118 | if (group == groupName) { | ||||
119 | config->deleteGroup(group); | ||||
120 | --value; | ||||
121 | needSaveConfig = true; | ||||
122 | } | ||||
123 | } | ||||
124 | } | ||||
125 | if (needSaveConfig) { | ||||
126 | general.writeEntry("Number", value); | ||||
127 | | ||||
128 | config->sync(); | ||||
129 | config->reparseConfiguration(); | ||||
130 | if (forceReload) { | ||||
131 | reload(); | ||||
132 | } | ||||
133 | } | ||||
134 | return needSaveConfig; | ||||
135 | } | ||||
136 | | ||||
137 | QString FollowUpReminder::FollowUpReminderUtil::followUpReminderPattern() | ||||
138 | { | ||||
139 | return QStringLiteral("FollowupReminderItem %1"); | ||||
140 | } |