From 0615dcf0842790f61445aad714628804a19b5500 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Sun, 17 Dec 2017 17:42:49 +0200 Subject: [PATCH] add --default-layout option for command line --this option enables the user to load the default layout from start. --- app/dockcorona.cpp | 27 ++++++++++++++------------- app/dockcorona.h | 4 +++- app/importer.cpp | 25 +++++++++++++++++++++++++ app/importer.h | 8 +++++++- app/layoutmanager.cpp | 16 +++++++++++----- app/layoutmanager.h | 4 ++-- app/main.cpp | 9 ++++++++- 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/app/dockcorona.cpp b/app/dockcorona.cpp index ad43f4349..649699403 100644 --- a/app/dockcorona.cpp +++ b/app/dockcorona.cpp @@ -57,8 +57,9 @@ namespace Latte { -DockCorona::DockCorona(QObject *parent) +DockCorona::DockCorona(bool defaultLayoutOnStartup, QObject *parent) : Plasma::Corona(parent), + m_defaultLayoutOnStartup(defaultLayoutOnStartup), m_activityConsumer(new KActivities::Consumer(this)), m_screenPool(new ScreenPool(KSharedConfig::openConfig(), this)), m_globalShortcuts(new GlobalShortcuts(this)), @@ -150,20 +151,20 @@ void DockCorona::load() QString loadLayoutName = ""; - if (!assignedLayout.isEmpty() && assignedLayout != m_universalSettings->currentLayoutName()) { - loadLayoutName = assignedLayout; - } else { - loadLayoutName = m_universalSettings->currentLayoutName(); - } - - if (!m_layoutManager->layoutExists(loadLayoutName)) { - QString defaultLayoutName = m_layoutManager->defaultLayoutName(); - - if (!m_layoutManager->layoutExists(defaultLayoutName)) { - m_layoutManager->importDefaultLayout(); + if (!m_defaultLayoutOnStartup) { + if (!assignedLayout.isEmpty() && assignedLayout != m_universalSettings->currentLayoutName()) { + loadLayoutName = assignedLayout; + } else { + loadLayoutName = m_universalSettings->currentLayoutName(); } - loadLayoutName = defaultLayoutName; + if (!m_layoutManager->layoutExists(loadLayoutName)) { + loadLayoutName = m_layoutManager->defaultLayoutName(); + m_layoutManager->importDefaultLayout(false); + } + } else { + loadLayoutName = m_layoutManager->importer()->uniqueLayoutName(m_layoutManager->defaultLayoutName()); + m_layoutManager->importDefaultLayout(true); } m_layoutManager->switchToLayout(loadLayoutName); diff --git a/app/dockcorona.h b/app/dockcorona.h index 0ccfe250b..020225850 100644 --- a/app/dockcorona.h +++ b/app/dockcorona.h @@ -62,7 +62,7 @@ class DockCorona : public Plasma::Corona { Q_CLASSINFO("D-Bus Interface", "org.kde.LatteDock") public: - DockCorona(QObject *parent = nullptr); + DockCorona(bool defaultLayoutOnStartup = false, QObject *parent = nullptr); virtual ~DockCorona(); int numScreens() const override; @@ -136,6 +136,8 @@ private: QStringList appletsIds(); bool m_activitiesStarting{true}; + //! this is used to enforce loading the default layout on startup + bool m_defaultLayoutOnStartup{false}; //! this is used to check if a dock with tasks in it will be loaded on startup bool m_tasksWillBeLoaded{false}; //! this is used to record the first dock having tasks in it. It is used diff --git a/app/importer.cpp b/app/importer.cpp index 0ee2c9f0d..ab4be2ba9 100644 --- a/app/importer.cpp +++ b/app/importer.cpp @@ -463,4 +463,29 @@ QString Importer::nameOfConfigFile(const QString &fileName) return layoutName; } +bool Importer::layoutExists(QString layoutName) +{ + return QFile::exists(QDir::homePath() + "/.config/latte/" + layoutName + ".layout.latte"); +} + +QString Importer::uniqueLayoutName(QString name) +{ + int pos_ = name.lastIndexOf(QRegExp(QString("[-][0-9]+"))); + + if (layoutExists(name) && pos_ > 0) { + name = name.left(pos_); + } + + int i = 2; + + QString namePart = name; + + while (layoutExists(name)) { + name = namePart + "-" + QString::number(i); + i++; + } + + return name; +} + } diff --git a/app/importer.h b/app/importer.h index 121df9245..7d97a1adc 100644 --- a/app/importer.h +++ b/app/importer.h @@ -65,9 +65,15 @@ public: bool exportFullConfiguration(QString file); static Importer::LatteFileVersion fileVersion(QString file); - static QString nameOfConfigFile(const QString &fileName); + static bool importHelper(QString fileName); + static QString nameOfConfigFile(const QString &fileName); + static QString uniqueLayoutName(QString name); + private: + //! check if this layout exists already in the latte directory + static bool layoutExists(QString layoutName); + //! checks if this old layout can be imported. If it can it returns //! the new layout path and an empty string if it cant QString layoutCanBeImported(QString oldAppletsPath, QString newName, QString exportDirectory = QString()); diff --git a/app/layoutmanager.cpp b/app/layoutmanager.cpp index 9ca8f9e18..603524129 100644 --- a/app/layoutmanager.cpp +++ b/app/layoutmanager.cpp @@ -357,9 +357,9 @@ QString LayoutManager::newLayout(QString layoutName, QString preset) return newLayoutPath; } -void LayoutManager::importDefaultLayout() +void LayoutManager::importDefaultLayout(bool newInstanceIfPresent) { - importPreset(1); + importPreset(1, newInstanceIfPresent); } void LayoutManager::importPresets(bool includeDefault) @@ -371,11 +371,11 @@ void LayoutManager::importPresets(bool includeDefault) } for (int i = start; i <= 4; ++i) { - importPreset(i); + importPreset(i, false); } } -void LayoutManager::importPreset(int presetNo) +void LayoutManager::importPreset(int presetNo, bool newInstanceIfPresent) { QByteArray presetNameOrig = QString("preset" + QString::number(presetNo)).toUtf8(); QString presetPath = m_corona->kPackage().filePath(presetNameOrig); @@ -383,7 +383,13 @@ void LayoutManager::importPreset(int presetNo) QByteArray presetNameChars = presetName.toUtf8(); presetName = i18n(presetNameChars); - QString newLayoutFile = QDir::homePath() + "/.config/latte/" + presetName + ".layout.latte"; + QString newLayoutFile = ""; + + if (newInstanceIfPresent) { + newLayoutFile = QDir::homePath() + "/.config/latte/" + m_importer->uniqueLayoutName(presetName) + ".layout.latte"; + } else { + newLayoutFile = QDir::homePath() + "/.config/latte/" + presetName + ".layout.latte"; + } if (!QFile(newLayoutFile).exists()) { QFile(presetPath).copy(newLayoutFile); diff --git a/app/layoutmanager.h b/app/layoutmanager.h index 59af28b3d..22a97b669 100644 --- a/app/layoutmanager.h +++ b/app/layoutmanager.h @@ -81,7 +81,7 @@ public: QStringList activities(); - void importDefaultLayout(); + void importDefaultLayout(bool newInstanceIfPresent = false); void importPresets(bool includeDefault = false); public slots: @@ -110,7 +110,7 @@ private: void confirmDynamicSwitch(); void setMenuLayouts(QStringList layouts); void showInfoWindow(QString info, int duration); - void importPreset(int presetNo); + void importPreset(int presetNo, bool newInstanceIfPresent = false); QString layoutPath(QString layoutName); diff --git a/app/main.cpp b/app/main.cpp index d38d5fc60..a32c480d2 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -83,6 +83,7 @@ int main(int argc, char **argv) , {{"d", "debug"}, i18nc("command line", "Show the debugging messages on stdout.")} , {"graphics", i18nc("command line", "Draw boxes around of the applets.")} , {"with-window", i18nc("command line", "Open a window with much debug information.")} + , {"default-layout", i18nc("command line", "Import and load default layout.")} , {"import", i18nc("command line", "Import configuration."), i18nc("command line: import", "file_name")} , {"mask", i18nc("command line" , "Show messages of debugging for the mask (Only useful to devs).")} , {"timers", i18nc("command line", "Show messages for debugging the timers (Only useful to devs).")} @@ -146,7 +147,13 @@ int main(int argc, char **argv) KCrash::setDrKonqiEnabled(true); KCrash::setFlags(KCrash::AutoRestart | KCrash::AlwaysDirectly); - Latte::DockCorona corona; + bool defaultLayoutOnStartup = false; + + if (parser.isSet(QStringLiteral("default-layout"))) { + defaultLayoutOnStartup = true; + } + + Latte::DockCorona corona(defaultLayoutOnStartup); KDBusService service(KDBusService::Unique); return app.exec();