File indexing completed on 2022-11-23 12:33:40

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2016 Gleb Baryshev <gleb.baryshev@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "settings_versionsync.h"
0007 #include "aboutdata.h"
0008 #include "settings.h"
0009 #include "tools.h"
0010 #include "ui_settings_versionsync.h"
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <QDir>
0014 #include <QLocale>
0015 #include <QPointer>
0016 #include <QtConcurrent/QtConcurrentRun>
0017 
0018 //! Compute size of .git folder and invoke VersionSyncPage::setHistorySize
0019 void showHistorySize(QPointer<VersionSyncPage> versionSyncPage)
0020 {
0021     QString git = Global::gitFolder();
0022     qint64 size = QDir(git).exists() ? Tools::computeSizeRecursively(git) : 0;
0023     if (!versionSyncPage.isNull())
0024         QMetaObject::invokeMethod(versionSyncPage.data(), "setHistorySize", Qt::QueuedConnection, Q_ARG(qint64, size));
0025 }
0026 
0027 VersionSyncPage::VersionSyncPage(QWidget *parent, const QVariantList &args)
0028     : KCModule(parent, args)
0029     , ui(new Ui::VersionSyncPage)
0030 {
0031     KAboutData *about = new AboutData();
0032     about->setComponentName("kcmbasket_config_version_sync");
0033     setAboutData(about);
0034 
0035     ui->setupUi(this);
0036 
0037 #ifdef WITH_LIBGIT2
0038     ui->labelWithoutVersionControlSupport->setVisible(false);
0039     QtConcurrent::run(showHistorySize, this);
0040 #else
0041     ui->checkBoxEnable->setEnabled(false);
0042     ui->groupBoxControl->setVisible(false);
0043 #endif
0044 
0045     connect(ui->checkBoxEnable, SIGNAL(toggled(bool)), this, SLOT(changed()));
0046     VersionSyncPage::load();
0047 }
0048 
0049 VersionSyncPage::~VersionSyncPage()
0050 {
0051     delete ui;
0052 }
0053 
0054 void VersionSyncPage::load()
0055 {
0056     ui->checkBoxEnable->setChecked(Settings::versionSyncEnabled());
0057 #ifdef WITH_LIBGIT2
0058     onCheckBoxEnableClicked();
0059 #endif
0060 }
0061 
0062 void VersionSyncPage::save()
0063 {
0064     Settings::setVersionSyncEnabled(ui->checkBoxEnable->isChecked());
0065 }
0066 
0067 void VersionSyncPage::defaults()
0068 {
0069     ui->checkBoxEnable->setChecked(false);
0070 }
0071 
0072 void VersionSyncPage::onCheckBoxEnableClicked()
0073 {
0074     ui->groupBoxControl->setEnabled(ui->checkBoxEnable->isChecked());
0075 }
0076 
0077 void VersionSyncPage::onButtonClearHistoryClicked()
0078 {
0079     if (KMessageBox::questionYesNo(this, i18n("Do you really want to remove old versions for all baskets?"), i18n("Version Sync")) == KMessageBox::Yes) {
0080         Tools::deleteRecursively(Global::gitFolder());
0081         ui->buttonClearHistory->setEnabled(false);
0082         setHistorySize(0);
0083         Global::initializeGitIfNeeded(Global::savesFolder()); // restore .git
0084     }
0085 }
0086 
0087 void VersionSyncPage::setHistorySize(qint64 size_bytes)
0088 {
0089     QString size_mb = QLocale().toString((float)size_bytes / 1024 / 1024, 'f', 2);
0090     ui->labelHistorySize->setText(i18n("This will free %1 MB", size_mb));
0091 }