File: kupkcm.cpp

package info (click to toggle)
kup-backup 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 8,422; xml: 311; makefile: 6; sh: 3
file content (356 lines) | stat: -rw-r--r-- 13,724 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
//
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "kupkcm.h"
#include "backupplan.h"
#include "backupplanwidget.h"
#include "kupdaemon.h"
#include "kupsettings.h"
#include "planstatuswidget.h"

#include <QCheckBox>
#include <QDBusInterface>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QStackedLayout>

#include <KAboutData>
#include <KConfigDialogManager>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KPluginFactory>
#include <KProcess>

#if QT_VERSION_MAJOR == 5
#include <Kdelibs4ConfigMigrator>
#endif

K_PLUGIN_CLASS_WITH_JSON(KupKcm, "kcm_kup.json")

#if QT_VERSION_MAJOR == 5
KupKcm::KupKcm(QWidget *pParent, const QVariantList &pArgs)
    : KCModule(pParent, pArgs)
#else
KupKcm::KupKcm(QObject *pParent, const KPluginMetaData &md, const QVariantList &pArgs)
    : KCModule(pParent, md)
#endif
    , mSourcePageToShow(0)
{
#if QT_VERSION_MAJOR == 5
    KAboutData lAbout(QStringLiteral("kcm_kup"),
                      i18n("Kup Configuration Module"),
                      QStringLiteral("0.9.1"),
                      i18n("Configuration of backup plans for the Kup backup system"),
                      KAboutLicense::GPL,
                      i18n("Copyright (C) 2011-2020 Simon Persson"));
    lAbout.addAuthor(i18n("Simon Persson"), i18n("Maintainer"), "simon.persson@mykolab.com");
    lAbout.setTranslator(xi18nc("NAME OF TRANSLATORS", "Your names"), xi18nc("EMAIL OF TRANSLATORS", "Your emails"));
    setAboutData(new KAboutData(lAbout));
#endif
    setObjectName(QStringLiteral("kcm_kup")); // needed for the kconfigdialogmanager magic
    setButtons((Apply | buttons()) & ~Default);

    KProcess lBupProcess;
    lBupProcess << QStringLiteral("bup") << QStringLiteral("version");
    lBupProcess.setOutputChannelMode(KProcess::MergedChannels);
    int lExitCode = lBupProcess.execute();
    if (lExitCode >= 0) {
        mBupVersion = QString::fromUtf8(lBupProcess.readAllStandardOutput());
        KProcess lPar2Process;
        lPar2Process << QStringLiteral("bup") << QStringLiteral("fsck") << QStringLiteral("--par2-ok");
        mPar2Available = lPar2Process.execute() == 0;
    } else {
        mPar2Available = false;
    }

    KProcess lRsyncProcess;
    lRsyncProcess << QStringLiteral("rsync") << QStringLiteral("--version");
    lRsyncProcess.setOutputChannelMode(KProcess::MergedChannels);
    lExitCode = lRsyncProcess.execute();
    if (lExitCode >= 0) {
        QString lOutput = QString::fromLocal8Bit(lRsyncProcess.readLine());
        mRsyncVersion = lOutput.split(QLatin1Char(' '), Qt::SkipEmptyParts).at(2);
    }

    if (mBupVersion.isEmpty() && mRsyncVersion.isEmpty()) {
        auto lSorryIcon = new QLabel;
        lSorryIcon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-error")).pixmap(64, 64));
        QString lInstallMessage = i18n(
            "<h2>Backup programs are missing</h2>"
            "<p>Before you can activate any backup plan you need to "
            "install either of</p>"
            "<ul><li>bup, for versioned backups</li>"
            "<li>rsync, for synchronized backups</li></ul>");
        auto lSorryText = new QLabel(lInstallMessage);
        lSorryText->setWordWrap(true);
        auto lHLayout = new QHBoxLayout;
        lHLayout->addWidget(lSorryIcon);
        lHLayout->addWidget(lSorryText, 1);
#if QT_VERSION_MAJOR == 5
        setLayout(lHLayout);
#else
        widget()->setLayout(lHLayout);
#endif
    } else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        Kdelibs4ConfigMigrator lMigrator(QStringLiteral("kup"));
        lMigrator.setConfigFiles(QStringList() << QStringLiteral("kuprc"));
        lMigrator.migrate();
#endif

        mConfig = KSharedConfig::openConfig(QStringLiteral("kuprc"));
        mSettings = new KupSettings(mConfig, this);
        for (int i = 0; i < mSettings->mNumberOfPlans; ++i) {
            mPlans.append(new BackupPlan(i + 1, mConfig, this));
            mConfigManagers.append(nullptr);
            mPlanWidgets.append(nullptr);
            mStatusWidgets.append(nullptr);
        }
        createSettingsFrontPage();
        addConfig(mSettings, mFrontPage);
        mStackedLayout = new QStackedLayout;
        mStackedLayout->addWidget(mFrontPage);
#if QT_VERSION_MAJOR == 5
        setLayout(mStackedLayout);
#else
        widget()->setLayout(mStackedLayout);
#endif
        QListIterator<QVariant> lIter(pArgs);
        while (lIter.hasNext()) {
            QVariant lVariant = lIter.next();
            if (lVariant.type() == QVariant::String) {
                QString lArgument = lVariant.toString();
                if (lArgument == QStringLiteral("show_sources") && lIter.hasNext()) {
                    mSourcePageToShow = lIter.next().toString().toInt();
                }
            }
        }
    }
}

#if QT_VERSION_MAJOR == 5
QSize KupKcm::sizeHint() const
{
    return {800, 600};
}
#endif

void KupKcm::load()
{
    if (mBupVersion.isEmpty() && mRsyncVersion.isEmpty()) {
        return;
    }
    // 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();
    // this call is needed because it could have been set true before, now load() is called
    // because user pressed reset button. need to manually reset the "changed" state to false
    // in this case.
    unmanagedWidgetChangeState(false);
    if (mSourcePageToShow > 0) {
        mStackedLayout->setCurrentIndex(mSourcePageToShow);
        mPlanWidgets[mSourcePageToShow - 1]->showSourcePage();
        mSourcePageToShow = 0; // only trigger on first load after startup.
    }
}

void KupKcm::save()
{
    int lPlansRemoved = 0;
    for (int i = 0; i < mPlans.count(); ++i) {
        auto *lPlan = mPlans.at(i);
        auto *lManager = mConfigManagers.at(i);
        if (lManager == nullptr) {
            lPlan->setDefaults();
            lPlan->save();
            delete mPlans.takeAt(i);
            mConfigManagers.removeAt(i);
            mStatusWidgets.removeAt(i);
            delete mPlanWidgets.takeAt(i);
            ++lPlansRemoved;
            --i;
            continue;
        }
        if (lPlansRemoved != 0) {
            // 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.
            // This also removes all entries from the config file under the old plan
            // number group.
            lPlan->setDefaults();
            lPlan->save();

            lPlan->setPlanNumber(i + 1);
        }
        mPlanWidgets.at(i)->saveExtraData();
        lManager->updateSettings();
        mStatusWidgets.at(i)->updateIcon();
        if (lPlan->mDestinationType == 1 && lPlan->mExternalUUID.isEmpty()) {
#if QT_VERSION_MAJOR == 5
            QWidget *wid = this;
#else
            QWidget *wid = widget();
#endif
            KMessageBox::information(wid,
                                     xi18nc("@info %1 is the name of the backup plan",
                                            "%1 does not have a destination!<nl/>"
                                            "No backups will be saved by this plan.",
                                            lPlan->mDescription),
                                     xi18nc("@title:window", "Warning"),
                                     QString(),
                                     KMessageBox::Dangerous);
        }
    }
    mSettings->mNumberOfPlans = mPlans.count();
    mSettings->save();

    KCModule::save();

    QDBusInterface lInterface(KUP_DBUS_SERVICE_NAME, KUP_DBUS_OBJECT_PATH);
    if (lInterface.isValid()) {
        lInterface.call(QStringLiteral("reloadConfig"));
    } else {
        KProcess::startDetached(QStringLiteral("kup-daemon"));
    }
}

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::createSettingsFrontPage()
{
    mFrontPage = new QWidget;
    auto lHLayout = new QHBoxLayout;
    auto lVLayout = new QVBoxLayout;
    auto lScrollArea = new QScrollArea;
    auto lCentralWidget = new QWidget(lScrollArea);
    mVerticalLayout = new QVBoxLayout;
    lScrollArea->setWidget(lCentralWidget);
    lScrollArea->setWidgetResizable(true);
    lScrollArea->setFrameStyle(QFrame::NoFrame);

    auto lAddPlanButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-add")), xi18nc("@action:button", "Add New Plan"));
    connect(lAddPlanButton, &QPushButton::clicked, this, [this] {
        mPlans.append(new BackupPlan(mPlans.count() + 1, mConfig, this));
        if (mBupVersion.isEmpty())
            mPlans.last()->mBackupType = 1;
        mConfigManagers.append(nullptr);
        mPlanWidgets.append(nullptr);
        mStatusWidgets.append(nullptr);
        createPlanWidgets(mPlans.count() - 1);
        updateChangedStatus();
        emit mStatusWidgets.at(mPlans.count() - 1)->configureMe();
    });

    mEnableCheckBox = new QCheckBox(xi18nc("@option:check", "Backups Enabled"));
    mEnableCheckBox->setObjectName(QStringLiteral("kcfg_Backups enabled"));
    connect(mEnableCheckBox, &QCheckBox::toggled, lAddPlanButton, &QPushButton::setEnabled);

    lHLayout->addWidget(mEnableCheckBox);
    lHLayout->addStretch();
    lHLayout->addWidget(lAddPlanButton);
    lVLayout->addLayout(lHLayout);
    lVLayout->addWidget(lScrollArea);
    mFrontPage->setLayout(lVLayout);

    auto lFilediggerButton = new QPushButton(xi18nc("@action:button", "Open and restore from existing backups"));
    connect(lFilediggerButton, &QPushButton::clicked, [] {
        KProcess::startDetached(QStringLiteral("kup-filedigger"));
    });
    mVerticalLayout->addWidget(lFilediggerButton);
    mVerticalLayout->addStretch(1);
    lCentralWidget->setLayout(mVerticalLayout);
}

void KupKcm::createPlanWidgets(int pIndex)
{
    auto lPlanWidget = new BackupPlanWidget(mPlans.at(pIndex), mBupVersion, mRsyncVersion, mPar2Available);
    connect(lPlanWidget, SIGNAL(requestOverviewReturn()), this, SLOT(showFrontPage()));
    auto lConfigManager = new KConfigDialogManager(lPlanWidget, mPlans.at(pIndex));
    lConfigManager->setObjectName(objectName());
    connect(lConfigManager, SIGNAL(widgetModified()), this, SLOT(updateChangedStatus()));
    auto lStatusWidget = new PlanStatusWidget(mPlans.at(pIndex));
    connect(lStatusWidget, &PlanStatusWidget::removeMe, this, [this] {
        int lIndex = mStatusWidgets.indexOf(qobject_cast<PlanStatusWidget *>(sender()));
        if (lIndex < mSettings->mNumberOfPlans)
            partiallyRemovePlan(lIndex);
        else
            completelyRemovePlan(lIndex);
        updateChangedStatus();
    });
    connect(lStatusWidget, &PlanStatusWidget::configureMe, this, [this] {
        int lIndex = mStatusWidgets.indexOf(qobject_cast<PlanStatusWidget *>(sender()));
        mStackedLayout->setCurrentIndex(lIndex + 1);
    });
    connect(lStatusWidget, &PlanStatusWidget::duplicateMe, this, [this] {
        int lIndex = mStatusWidgets.indexOf(qobject_cast<PlanStatusWidget *>(sender()));
        auto lNewPlan = new BackupPlan(mPlans.count() + 1, mConfig, this);
        lNewPlan->copyFrom(*mPlans.at(lIndex));
        mPlans.append(lNewPlan);
        mConfigManagers.append(nullptr);
        mPlanWidgets.append(nullptr);
        mStatusWidgets.append(nullptr);
        createPlanWidgets(mPlans.count() - 1);
        // crazy trick to make the config system realize that stuff has changed
        // and will need to be saved.
        lNewPlan->setDefaults();
        updateChangedStatus();
    });
    connect(mEnableCheckBox, &QCheckBox::toggled, lStatusWidget, &PlanStatusWidget::setEnabled);
    connect(lPlanWidget->mDescriptionEdit, &KLineEdit::textChanged, lStatusWidget->mDescriptionLabel, &QLabel::setText);

    mConfigManagers[pIndex] = lConfigManager;
    mPlanWidgets[pIndex] = lPlanWidget;
    mStackedLayout->insertWidget(pIndex + 1, lPlanWidget);
    mStatusWidgets[pIndex] = lStatusWidget;
    // always insert at end, before the file digger button and strech space at the bottom.
    mVerticalLayout->insertWidget(mVerticalLayout->count() - 2, lStatusWidget);
}

void KupKcm::completelyRemovePlan(int pIndex)
{
    delete mConfigManagers.takeAt(pIndex);
    delete mStatusWidgets.takeAt(pIndex);
    delete mPlanWidgets.takeAt(pIndex);
    delete mPlans.takeAt(pIndex);
}

void KupKcm::partiallyRemovePlan(int pIndex)
{
    mConfigManagers.at(pIndex)->deleteLater();
    mConfigManagers[pIndex] = nullptr;
    mStatusWidgets.at(pIndex)->deleteLater();
    mStatusWidgets[pIndex] = nullptr;
}

#include "kupkcm.moc"