/
kupkcm.cpp
251 lines (223 loc) · 8.91 KB
/
kupkcm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
* Copyright Simon Persson *
* simonpersson1@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "backupplan.h"
#include "backupplanwidget.h"
#include "kupdaemon.h"
#include "kupkcm.h"
#include "kupsettings.h"
#include "planstatuswidget.h"
#include <QCheckBox>
#include <QDBusInterface>
#include <QLabel>
#include <QScrollArea>
#include <QStackedLayout>
#include <KAboutData>
#include <KAssistantDialog>
#include <KConfigDialogManager>
#include <KLineEdit>
#include <KPluginFactory>
#include <KProcess>
#include <KPushButton>
K_PLUGIN_FACTORY(KupKcmFactory, registerPlugin<KupKcm>();)
K_EXPORT_PLUGIN(KupKcmFactory("kcm_kup"))
KupKcm::KupKcm(QWidget *pParent, const QList<QVariant> &pArgs)
: KCModule(KupKcmFactory::componentData(), pParent, pArgs)
{
KAboutData *lAboutData = new KAboutData("kcm_kup", "kcm_kup", ki18n("Kup Configuration Module"),
"0.1.0", ki18n("Configuration of backup plans for the Kup backup system"),
KAboutData::License_GPL, ki18n("Copyright 2011 Simon Persson"));
lAboutData->addAuthor(ki18n("Simon Persson"), ki18n("Maintainer"), "simonpersson1@gmail.com");
setAboutData(lAboutData);
setObjectName("kcm_kup"); //needed for the kconfigdialogmanager magic
setButtons((Apply | buttons()) & ~Default);
mStackedLayout = new QStackedLayout;
mConfig = KSharedConfig::openConfig("kuprc");
mSettings = new KupSettings(mConfig, this);
for(int i = 0; i < mSettings->mNumberOfPlans; ++i) {
mPlans.append(new BackupPlan(i+1, mConfig, this));
mConfigManagers.append(NULL);
mPlanWidgets.append(NULL);
mStatusWidgets.append(NULL);
}
createSettingsFrontPage();
addConfig(mSettings, mFrontPage);
mStackedLayout->addWidget(mFrontPage);
setLayout(mStackedLayout);
}
void KupKcm::load() {
// status will be set correctly after construction, set to checked here to
// match the enabled status of other widgets
mEnableCheckBox->setChecked(true);
for(int i = 0; i < mSettings->mNumberOfPlans; ++i) {
if(!mConfigManagers.at(i))
createPlanWidgets(i);
mConfigManagers.at(i)->updateWidgets();
}
for(int i = mSettings->mNumberOfPlans; i < mPlans.count();) {
completelyRemovePlan(i);
}
KCModule::load();
}
void KupKcm::save() {
KConfigDialogManager *lManager;
BackupPlan *lPlan;
int lPlansRemoved = 0;
for(int i=0; i < mPlans.count(); ++i) {
lPlan = mPlans.at(i);
lManager = mConfigManagers.at(i);
if(lManager != NULL) {
if(lPlansRemoved != 0) {
lPlan->removePlanFromConfig();
lPlan->setPlanNumber(i + 1);
// config manager does not detect a changed group name of the config items.
// To work around, read default settings - config manager will then notice
// changed values and save current widget status into the config using the
// new group name. If all settings for the plan already was default then
// nothing was saved anyway, either under old or new group name.
lPlan->setDefaults();
}
lManager->updateSettings();
mStatusWidgets.at(i)->updateIcon();
}
else {
lPlan->removePlanFromConfig();
delete mPlans.takeAt(i);
mConfigManagers.removeAt(i);
mStatusWidgets.removeAt(i);
mPlanWidgets.removeAt(i);
++lPlansRemoved;
--i;
}
}
mSettings->mNumberOfPlans = mPlans.count();
mSettings->writeConfig();
KCModule::save();
QDBusInterface lInterface(KUP_DBUS_SERVICE_NAME, KUP_DBUS_OBJECT_PATH);
if(lInterface.isValid()) {
lInterface.call(QLatin1String("reloadConfig"));
} else {
KProcess::execute(QLatin1String("kupdaemon")); // kuniqueapplication, should exit very quickly.
}
}
void KupKcm::updateChangedStatus() {
bool lHasUnmanagedChanged = false;
foreach(KConfigDialogManager *lConfigManager, mConfigManagers) {
if(!lConfigManager || lConfigManager->hasChanged()) {
lHasUnmanagedChanged = true;
break;
}
}
if(mPlanWidgets.count() != mSettings->mNumberOfPlans)
lHasUnmanagedChanged = true;
unmanagedWidgetChangeState(lHasUnmanagedChanged);
}
void KupKcm::showFrontPage() {
mStackedLayout->setCurrentIndex(0);
}
void KupKcm::removePlan() {
for(int i = 0; i < mStatusWidgets.count(); ++i) {
PlanStatusWidget *lStatusWidget = mStatusWidgets.at(i);
if(QObject::sender() == lStatusWidget) {
if(i < mSettings->mNumberOfPlans)
partiallyRemovePlan(i);
else
completelyRemovePlan(i);
updateChangedStatus();
break;
}
}
}
void KupKcm::configurePlan() {
for(int i = 0; i < mStatusWidgets.count(); ++i) {
PlanStatusWidget *lPlanWidget = mStatusWidgets.at(i);
if(QObject::sender() == lPlanWidget) {
mStackedLayout->setCurrentWidget(mPlanWidgets.at(i));
break;
}
}
}
void KupKcm::addPlan() {
mPlans.append(new BackupPlan(mPlans.count() + 1, mConfig, this));
mConfigManagers.append(NULL);
mPlanWidgets.append(NULL);
mStatusWidgets.append(NULL);
createPlanWidgets(mPlans.count() - 1);
updateChangedStatus();
}
void KupKcm::createSettingsFrontPage() {
mFrontPage = new QWidget;
QHBoxLayout *lHLayout = new QHBoxLayout;
QVBoxLayout *lVLayout = new QVBoxLayout;
QScrollArea *lScrollArea = new QScrollArea;
QWidget *lCentralWidget = new QWidget(lScrollArea);
mVerticalLayout = new QVBoxLayout;
lScrollArea->setWidget(lCentralWidget);
lScrollArea->setWidgetResizable(true);
lScrollArea->setFrameStyle(QFrame::NoFrame);
mAddPlanButton = new KPushButton(KIcon("list-add"), i18n("Add New Plan"));
connect(mAddPlanButton, SIGNAL(clicked()), this, SLOT(addPlan()));
mEnableCheckBox = new QCheckBox(i18n("Backups Enabled"));
mEnableCheckBox->setObjectName("kcfg_Backups enabled");
connect(mEnableCheckBox, SIGNAL(toggled(bool)), mAddPlanButton, SLOT(setEnabled(bool)));
lHLayout->addWidget(mEnableCheckBox);
lHLayout->addStretch();
lHLayout->addWidget(mAddPlanButton);
lVLayout->addLayout(lHLayout);
lVLayout->addWidget(lScrollArea);
mFrontPage->setLayout(lVLayout);
mVerticalLayout->addStretch(1);
lCentralWidget->setLayout(mVerticalLayout);
}
void KupKcm::createPlanWidgets(int pIndex) {
BackupPlanWidget *lPlanWidget = new BackupPlanWidget;
connect(lPlanWidget, SIGNAL(requestOverviewReturn()), this, SLOT(showFrontPage()));
KConfigDialogManager *lConfigManager = new KConfigDialogManager(lPlanWidget, mPlans.at(pIndex));
lConfigManager->setObjectName(objectName());
connect(lConfigManager, SIGNAL(widgetModified()), this, SLOT(updateChangedStatus()));
PlanStatusWidget *lStatusWidget = new PlanStatusWidget(mPlans.at(pIndex));
connect(lStatusWidget, SIGNAL(removeMe()), this, SLOT(removePlan()));
connect(lStatusWidget, SIGNAL(configureMe()), this, SLOT(configurePlan()));
connect(mEnableCheckBox, SIGNAL(toggled(bool)), lStatusWidget, SLOT(setEnabled(bool)));
connect(lPlanWidget->mDescriptionEdit, SIGNAL(textChanged(QString)),
lStatusWidget->mDescriptionLabel, SLOT(setText(QString)));
mConfigManagers[pIndex] = lConfigManager;
mPlanWidgets[pIndex] = lPlanWidget;
mStackedLayout->insertWidget(pIndex + 1, lPlanWidget);
mStatusWidgets[pIndex] = lStatusWidget;
mVerticalLayout->insertWidget(pIndex, lStatusWidget);
}
void KupKcm::completelyRemovePlan(int pIndex) {
mVerticalLayout->removeWidget(mStatusWidgets.at(pIndex));
mStackedLayout->removeWidget(mPlanWidgets.at(pIndex));
delete mConfigManagers.takeAt(pIndex);
delete mStatusWidgets.takeAt(pIndex);
delete mPlanWidgets.takeAt(pIndex);
delete mPlans.takeAt(pIndex);
}
void KupKcm::partiallyRemovePlan(int pIndex) {
mVerticalLayout->removeWidget(mStatusWidgets.at(pIndex));
mStackedLayout->removeWidget(mPlanWidgets.at(pIndex));
mConfigManagers.at(pIndex)->deleteLater();
mConfigManagers[pIndex] = NULL;
mStatusWidgets.at(pIndex)->deleteLater();
mStatusWidgets[pIndex] = NULL;
mPlanWidgets.at(pIndex)->deleteLater();
mPlanWidgets[pIndex] = NULL;
}