mirror of
https://github.com/KDE/latte-dock.git
synced 2025-03-27 14:50:21 +03:00
feature #205, backup and restore
This commit is contained in:
parent
67102283bf
commit
52f1e949aa
@ -1,11 +1,19 @@
|
||||
#include "globalsettings.h"
|
||||
#include "../liblattedock/extras.h"
|
||||
|
||||
#include <QIcon>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QProcess>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include <KLocalizedString>
|
||||
#include <KConfig>
|
||||
#include <KArchive/KTar>
|
||||
#include <KArchive/KArchiveEntry>
|
||||
#include <KArchive/KArchiveDirectory>
|
||||
#include <KNotification>
|
||||
|
||||
namespace Latte {
|
||||
|
||||
@ -16,14 +24,12 @@ GlobalSettings::GlobalSettings(QObject *parent)
|
||||
|
||||
if (m_corona) {
|
||||
m_configGroup = m_corona->config()->group("General");
|
||||
|
||||
//! create the alternative session action
|
||||
const QIcon altIcon = QIcon::fromTheme("user-identity");
|
||||
m_altSessionAction = new QAction(altIcon, i18n("Alternative Session"), this);
|
||||
m_altSessionAction->setStatusTip(tr("Enable/Disable Alternative Session"));
|
||||
m_altSessionAction->setCheckable(true);
|
||||
connect(m_altSessionAction, &QAction::triggered, this, &GlobalSettings::enableAltSession);
|
||||
|
||||
connect(m_corona, &DockCorona::currentSessionChanged, this, &GlobalSettings::currentSessionChangedSlot);
|
||||
}
|
||||
}
|
||||
@ -109,6 +115,77 @@ void GlobalSettings::setCurrentSession(Dock::SessionType session)
|
||||
}
|
||||
}
|
||||
|
||||
bool GlobalSettings::importHelper(const QString &fileName)
|
||||
{
|
||||
if (!QFile::exists(fileName))
|
||||
return false;
|
||||
|
||||
KTar archive(fileName, QStringLiteral("application/x-tar"));
|
||||
archive.open(QIODevice::ReadOnly);
|
||||
|
||||
if (!archive.isOpen())
|
||||
return false;
|
||||
|
||||
const auto rootDir = archive.directory();
|
||||
QDir tempDir {QDir::tempPath() + "/latterc-unconmpressed"};
|
||||
|
||||
auto clean = [&]() {
|
||||
if (tempDir.exists()) {
|
||||
tempDir.removeRecursively();
|
||||
}
|
||||
};
|
||||
|
||||
if (rootDir) {
|
||||
|
||||
if (!tempDir.exists())
|
||||
tempDir.mkpath(tempDir.absolutePath());
|
||||
|
||||
foreach(auto &name, rootDir->entries()) {
|
||||
auto fileEntry = rootDir->file(name);
|
||||
|
||||
if (fileEntry && (fileEntry->name() == "lattedockrc"
|
||||
|| fileEntry->name() == "lattedock-appletsrc"))
|
||||
{
|
||||
if (!fileEntry->copyTo(tempDir.absolutePath())) {
|
||||
clean();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
qInfo() << i18nc("The file has a wrong format!!!", "import/export config");
|
||||
auto showMsgError = [&]() {
|
||||
auto msg = new QMessageBox;
|
||||
msg->setText(i18nc("The file has a wrong format", "import/export config"));
|
||||
msg->setDetailedText(i18nc("Do you want to open other file?", "import/export config"));
|
||||
msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
||||
|
||||
connect(msg, &QMessageBox::finished, msg, &QMessageBox::deleteLater);
|
||||
|
||||
msg->open();
|
||||
};
|
||||
showMsgError();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto latterc = QDir::homePath() + "/.config/lattedockrc";
|
||||
const auto appletsrc = QDir::homePath() + "/.config/lattedock-appletsrc";
|
||||
|
||||
// NOTE: I'm trying to avoid possible loss of information
|
||||
qInfo() << "Backing up old configuration files...";
|
||||
auto n = QString::number(qrand() % 256);
|
||||
QFile::copy(latterc, latterc + "." + n + ".bak");
|
||||
QFile::copy(appletsrc, appletsrc + "." + n + ".bak");
|
||||
|
||||
qInfo() << "Importing the new configuration...";
|
||||
if (QFile::remove(latterc) && QFile::remove(appletsrc)) {
|
||||
QFile::copy(tempDir.absolutePath() + "/lattedockrc" , latterc);
|
||||
QFile::copy(tempDir.absolutePath() + "/lattedock-appletsrc", appletsrc);
|
||||
}
|
||||
|
||||
archive.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
//!BEGIN configuration functions
|
||||
void GlobalSettings::load()
|
||||
@ -123,6 +200,121 @@ void GlobalSettings::save()
|
||||
}
|
||||
//!END configuration functions
|
||||
|
||||
void GlobalSettings::importConfiguration()
|
||||
{
|
||||
if (m_fileDialog) {
|
||||
m_fileDialog->close();
|
||||
m_fileDialog->deleteLater();
|
||||
}
|
||||
|
||||
m_fileDialog = new QFileDialog(nullptr, i18n("Import configuration")
|
||||
, QDir::homePath()
|
||||
, QStringLiteral("latteconf"));
|
||||
|
||||
m_fileDialog->setFileMode(QFileDialog::AnyFile);
|
||||
m_fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
|
||||
m_fileDialog->setDefaultSuffix("latteconf");
|
||||
m_fileDialog->setNameFilter(i18n("Latte Dock configuration file").append("(*.latterc)"));
|
||||
|
||||
connect(m_fileDialog.data(), &QFileDialog::finished
|
||||
, m_fileDialog.data(), &QFileDialog::deleteLater);
|
||||
|
||||
connect(m_fileDialog.data(), &QFileDialog::fileSelected
|
||||
, this, [&](const QString &file) {
|
||||
|
||||
if (!QFile::exists(file))
|
||||
return;
|
||||
|
||||
KTar archive(file, QStringLiteral("application/x-tar"));
|
||||
archive.open(QIODevice::ReadOnly);
|
||||
|
||||
auto showMsgError = [&]() {
|
||||
auto msg = new QMessageBox;
|
||||
msg->setText(i18nc("The file has a wrong format", "import/export config"));
|
||||
msg->setDetailedText(i18nc("Do you want to open other file?", "import/export config"));
|
||||
msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
||||
|
||||
connect(msg, &QMessageBox::accepted, this, &GlobalSettings::importConfiguration);
|
||||
connect(msg, &QMessageBox::finished, msg, &QMessageBox::deleteLater);
|
||||
|
||||
msg->open();
|
||||
};
|
||||
|
||||
if (!archive.isOpen()) {
|
||||
showMsgError();
|
||||
return;
|
||||
}
|
||||
|
||||
auto rootDir = archive.directory();
|
||||
|
||||
if (rootDir) {
|
||||
foreach(auto &name, rootDir->entries()) {
|
||||
auto fileEntry = rootDir->file(name);
|
||||
|
||||
if (fileEntry && (fileEntry->name() == "lattedockrc"
|
||||
|| fileEntry->name() == "lattedock-appletsrc"))
|
||||
{
|
||||
continue;
|
||||
} else {
|
||||
showMsgError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
archive.close();
|
||||
//NOTE: Restart latte for import the new configuration
|
||||
QProcess::startDetached(qGuiApp->applicationFilePath() + " --import \"" + file + "\"");
|
||||
qGuiApp->exit();
|
||||
|
||||
});
|
||||
|
||||
m_fileDialog->open();
|
||||
}
|
||||
|
||||
void GlobalSettings::exportConfiguration()
|
||||
{
|
||||
if (m_fileDialog) {
|
||||
m_fileDialog->close();
|
||||
m_fileDialog->deleteLater();
|
||||
}
|
||||
|
||||
m_fileDialog = new QFileDialog(nullptr, i18n("Export configuration")
|
||||
, QDir::homePath()
|
||||
, QStringLiteral("latterc"));
|
||||
m_fileDialog->setFileMode(QFileDialog::AnyFile);
|
||||
m_fileDialog->setAcceptMode(QFileDialog::AcceptSave);
|
||||
m_fileDialog->setDefaultSuffix("latterc");
|
||||
m_fileDialog->setNameFilter(i18n("Latte Dock configuration file").append("(*.latterc)"));
|
||||
|
||||
connect(m_fileDialog.data(), &QFileDialog::finished
|
||||
, m_fileDialog.data(), &QFileDialog::deleteLater);
|
||||
|
||||
connect(m_fileDialog.data(), &QFileDialog::fileSelected
|
||||
, this, [&](const QString &file) {
|
||||
if (QFile::exists(file) && !QFile::remove(file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
KTar archive(file, QStringLiteral("application/x-tar"));
|
||||
archive.open(QIODevice::WriteOnly);
|
||||
|
||||
std::unique_ptr<KConfig> config {m_corona->config()->copyTo(QDir::tempPath() + "/lattedock-appletsrc")};
|
||||
std::unique_ptr<KConfig> configApp {KSharedConfig::openConfig()->copyTo(QDir::tempPath() + "/lattedockrc")};
|
||||
|
||||
config->sync();
|
||||
configApp->sync();
|
||||
|
||||
archive.addLocalFile(config->name(), QStringLiteral("lattedock-appletsrc"));
|
||||
archive.addLocalFile(configApp->name(), QStringLiteral("lattedockrc"));
|
||||
archive.close();
|
||||
|
||||
QFile::remove(config->name());
|
||||
QFile::remove(configApp->name());
|
||||
});
|
||||
|
||||
m_fileDialog->open();
|
||||
}
|
||||
}
|
||||
|
||||
#include "moc_globalsettings.cpp"
|
||||
|
@ -24,6 +24,9 @@
|
||||
#include "dockcorona.h"
|
||||
#include "../liblattedock/dock.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QPointer>
|
||||
|
||||
#include <KConfigGroup>
|
||||
#include <KSharedConfig>
|
||||
|
||||
@ -56,6 +59,10 @@ public:
|
||||
Latte::Dock::SessionType currentSession() const;
|
||||
void setCurrentSession(Latte::Dock::SessionType session);
|
||||
|
||||
static bool importHelper(const QString& fileName);
|
||||
Q_INVOKABLE void importConfiguration();
|
||||
Q_INVOKABLE void exportConfiguration();
|
||||
|
||||
signals:
|
||||
void altSessionActionChanged();
|
||||
void currentSessionChanged();
|
||||
@ -72,6 +79,7 @@ private:
|
||||
bool m_exposeAltSession{false};
|
||||
QAction *m_altSessionAction{nullptr};
|
||||
DockCorona *m_corona{nullptr};
|
||||
QPointer<QFileDialog> m_fileDialog;
|
||||
|
||||
KConfigGroup m_configGroup;
|
||||
};
|
||||
|
20
app/main.cpp
20
app/main.cpp
@ -20,6 +20,7 @@
|
||||
|
||||
#include "dockcorona.h"
|
||||
#include "config-latte.h"
|
||||
#include "globalsettings.h"
|
||||
|
||||
#include <memory>
|
||||
#include <csignal>
|
||||
@ -77,6 +78,7 @@ int main(int argc, char **argv)
|
||||
, {"mask", i18nc("command line" , "Show messages of debugging for the mask (Only useful to devs).")}
|
||||
, {"graphics", i18nc("command line", "Draw boxes around of the applets.")}
|
||||
, {"with-window", i18nc("command line", "Open a window with much debug information.")}
|
||||
, {"import", i18nc("command line", "Import configuration."), i18nc("command line: import", "file_name")}
|
||||
});
|
||||
|
||||
parser.process(app);
|
||||
@ -84,12 +86,10 @@ int main(int argc, char **argv)
|
||||
QLockFile lockFile {QDir::tempPath() + "/latte-dock.lock"};
|
||||
|
||||
int timeout {100};
|
||||
|
||||
if (parser.isSet(QStringLiteral("replace"))) {
|
||||
qint64 pid{ -1};
|
||||
|
||||
if (parser.isSet(QStringLiteral("replace")) || parser.isSet(QStringLiteral("import"))) {
|
||||
qint64 pid{-1};
|
||||
if (lockFile.getLockInfo(&pid, nullptr, nullptr)) {
|
||||
kill(static_cast<__pid_t>(pid), SIGINT);
|
||||
kill(static_cast<pid_t>(pid), SIGINT);
|
||||
timeout = 3000;
|
||||
}
|
||||
}
|
||||
@ -99,6 +99,16 @@ int main(int argc, char **argv)
|
||||
qGuiApp->exit();
|
||||
}
|
||||
|
||||
if (parser.isSet(QStringLiteral("import"))) {
|
||||
bool imported = Latte::GlobalSettings::importHelper(parser.value(QStringLiteral("import")));
|
||||
|
||||
if (!imported) {
|
||||
qInfo() << i18n("The configuration cannot be imported");
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (parser.isSet(QStringLiteral("debug")) || parser.isSet(QStringLiteral("mask"))) {
|
||||
//! set pattern for debug messages
|
||||
//! [%{type}] [%{function}:%{line}] - %{message} [%{backtrace}]
|
||||
|
Loading…
x
Reference in New Issue
Block a user