From cdc5478b7b941a1347c33e89122cb39996fab4c5 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Sat, 22 Jul 2017 19:49:28 +0300 Subject: [PATCH] import correctly old layouts from old config files --- app/importer.cpp | 27 ++++++++++---- app/importer.h | 6 +-- app/layoutconfigdialog.cpp | 76 +++++++++++++++++++++++++++++++++++--- app/layoutconfigdialog.h | 2 +- app/layoutmanager.cpp | 5 +++ app/layoutmanager.h | 1 + 6 files changed, 100 insertions(+), 17 deletions(-) diff --git a/app/importer.cpp b/app/importer.cpp index a549511fe..5c1e2114d 100644 --- a/app/importer.cpp +++ b/app/importer.cpp @@ -78,9 +78,9 @@ bool Importer::updateOldConfiguration() return true; } -bool Importer::importOldLayout(QString oldAppletsPath, QString newName, bool alternative) +bool Importer::importOldLayout(QString oldAppletsPath, QString newName, bool alternative, QString exportDirectory) { - QString newLayoutPath = layoutCanBeImported(oldAppletsPath, newName); + QString newLayoutPath = layoutCanBeImported(oldAppletsPath, newName, exportDirectory); qDebug() << "New Layout Should be created: " << newLayoutPath; KSharedConfigPtr oldFile = KSharedConfig::openConfig(oldAppletsPath); @@ -166,7 +166,7 @@ bool Importer::importOldLayout(QString oldAppletsPath, QString newName, bool alt } //! update also the layout settings correctly - LayoutSettings newLayout(this, newLayoutPath); + LayoutSettings newLayout(this, newLayoutPath, newName); newLayout.setVersion(2); newLayout.setSyncLaunchers(syncLaunchers); newLayout.setGlobalLaunchers(globalLaunchers); @@ -175,12 +175,14 @@ bool Importer::importOldLayout(QString oldAppletsPath, QString newName, bool alt if (alternative) { newLayout.setColor("purple"); + } else { + newLayout.setColor("blue"); } return true; } -QString Importer::layoutCanBeImported(QString oldAppletsPath, QString newName) +QString Importer::layoutCanBeImported(QString oldAppletsPath, QString newName, QString exportDirectory) { QFile oldAppletsrc(oldAppletsPath); @@ -198,9 +200,9 @@ QString Importer::layoutCanBeImported(QString oldAppletsPath, QString newName) return QString(); } - QDir layoutDir(QDir::homePath() + "/.config/latte"); + QDir layoutDir(exportDirectory.isNull() ? QDir::homePath() + "/.config/latte" : exportDirectory); - if (!layoutDir.exists()) { + if (!layoutDir.exists() && exportDirectory.isNull()) { QDir(QDir::homePath() + "/.config").mkdir("latte"); } @@ -346,7 +348,6 @@ Importer::LatteFileVersion Importer::fileVersion(QString file) return Importer::UnknownFileType; } - QTemporaryDir archiveTempDir; QDir tempDir{archiveTempDir.path()}; @@ -396,4 +397,16 @@ Importer::LatteFileVersion Importer::fileVersion(QString file) return Importer::UnknownFileType; } +QString Importer::nameOfConfigFile(const QString &fileName) +{ + int lastSlash = fileName.lastIndexOf("/"); + QString tempLayoutFile = fileName; + QString layoutName = tempLayoutFile.remove(0, lastSlash + 1); + + int ext = layoutName.lastIndexOf(".latterc"); + layoutName = layoutName.remove(ext, 8); + + return layoutName; +} + } diff --git a/app/importer.h b/app/importer.h index 495e2c094..15cd287bf 100644 --- a/app/importer.h +++ b/app/importer.h @@ -55,7 +55,7 @@ public: //! alternative: old files can contain both a Default and an Alternative layout //! false: imports only Default layout //! true: imports only Alternative layout - bool importOldLayout(QString oldAppletsPath, QString newName, bool alternative = false); + bool importOldLayout(QString oldAppletsPath, QString newName, bool alternative = false, QString exportDirectory = QString()); //! imports and old configuration file (tar archive) that contains //! both an applets file and a latterc file with the screens @@ -63,11 +63,11 @@ public: bool importOldConfiguration(QString oldConfigPath, QString newName = QString()); static Importer::LatteFileVersion fileVersion(QString file); - + static QString nameOfConfigFile(const QString &fileName); private: //! 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 layoutCanBeImported(QString oldAppletsPath, QString newName, QString exportDirectory = QString()); LayoutManager *m_manager; }; diff --git a/app/layoutconfigdialog.cpp b/app/layoutconfigdialog.cpp index 4d9f53d1d..fe82f0d5b 100644 --- a/app/layoutconfigdialog.cpp +++ b/app/layoutconfigdialog.cpp @@ -37,9 +37,13 @@ #include #include +#include +#include +#include #include #include + namespace Latte { LayoutConfigDialog::LayoutConfigDialog(QWidget *parent, LayoutManager *manager) @@ -206,8 +210,41 @@ void LayoutConfigDialog::on_importButton_clicked() connect(fileDialog, &QFileDialog::fileSelected , this, [&](const QString & file) { - if (Importer::fileVersion(file) == Importer::LayoutVersion2) { + Importer::LatteFileVersion version = Importer::fileVersion(file); + + if (version == Importer::LayoutVersion2) { addLayoutForFile(file); + } else if (version == Importer::ConfigVersion1) { + KTar archive(file, QStringLiteral("application/x-tar")); + archive.open(QIODevice::ReadOnly); + + //! if the file isnt a tar archive + if (archive.isOpen()) { + QDir tempDir{uniqueTempDirectory()}; + + const auto archiveRootDir = archive.directory(); + + foreach (auto &name, archiveRootDir->entries()) { + auto fileEntry = archiveRootDir->file(name); + fileEntry->copyTo(tempDir.absolutePath()); + } + + QString name = Importer::nameOfConfigFile(file); + + QString applets(tempDir.absolutePath() + "/" + "lattedock-appletsrc"); + + if (QFile(applets).exists()) { + if (m_manager->importer()->importOldLayout(applets, name, false, tempDir.absolutePath())) { + addLayoutForFile(tempDir.absolutePath() + "/" + name + ".layout.latte", name, false); + } + + QString alternativeName = name + "-" + i18nc("layout", "Alternative"); + + if (m_manager->importer()->importOldLayout(applets, alternativeName, false, tempDir.absolutePath())) { + addLayoutForFile(tempDir.absolutePath() + "/" + alternativeName + ".layout.latte", alternativeName, false); + } + } + } } }); @@ -315,16 +352,21 @@ void LayoutConfigDialog::restoreDefaults() } } -void LayoutConfigDialog::addLayoutForFile(QString file, QString layoutName) +void LayoutConfigDialog::addLayoutForFile(QString file, QString layoutName, bool newTempDirectory) { if (layoutName.isEmpty()) { layoutName = LayoutSettings::layoutName(file); } - QString tempDir = uniqueTempDirectory(); + QString copiedId; - QString copiedId = tempDir + "/" + layoutName + ".layout.latte"; - QFile(file).copy(copiedId); + if (newTempDirectory) { + QString tempDir = uniqueTempDirectory(); + copiedId = tempDir + "/" + layoutName + ".layout.latte"; + QFile(file).copy(copiedId); + } else { + copiedId = file; + } LayoutSettings *settings = new LayoutSettings(this, copiedId); m_layouts[copiedId] = settings; @@ -339,6 +381,11 @@ void LayoutConfigDialog::addLayoutForFile(QString file, QString layoutName) insertLayoutInfoAtRow(row, copiedId, color, layoutName, menu, QStringList()); ui->layoutsView->selectRow(row); + + //NOTE: The pointer is automatically deleted when the event is closed + auto notification = new KNotification("import-done", KNotification::CloseOnTimeout); + notification->setText(i18nc("import-done", "Layout: %0 imported successfully\n").arg(layoutName)); + notification->sendEvent(); } void LayoutConfigDialog::loadLayouts() @@ -425,6 +472,12 @@ void LayoutConfigDialog::insertLayoutInfoAtRow(int row, QString path, QString co font.setBold(false); } + if (path.startsWith("/tmp/")) { + font.setItalic(true); + } else { + font.setItalic(false); + } + m_model->setData(m_model->index(row, 2), QVariant(name), Qt::DisplayRole); m_model->setData(m_model->index(row, 2), font, Qt::FontRole); @@ -484,6 +537,12 @@ void LayoutConfigDialog::currentRowChanged(const QModelIndex ¤t, const QMo } else { ui->removeButton->setEnabled(true); } + + if (id.startsWith("/tmp/")) { + ui->switchButton->setEnabled(false); + } else { + ui->switchButton->setEnabled(true); + } } void LayoutConfigDialog::recalculateAvailableActivities() @@ -625,6 +684,12 @@ bool LayoutConfigDialog::saveAllChanges() if (tId == fromRenamePaths[i]) { m_model->setData(m_model->index(j, 0), newFile, Qt::DisplayRole); + m_initLayoutPaths.append(newFile); + + QFont font = qvariant_cast(m_model->data(m_model->index(j, 2), Qt::FontRole)); + + font.setItalic(false); + m_model->setData(m_model->index(j, 2), font, Qt::FontRole); } } } @@ -645,7 +710,6 @@ bool LayoutConfigDialog::saveAllChanges() return true; } - bool LayoutConfigDialog::idExistsInModel(QString id) { for (int i = 0; i < m_model->rowCount(); ++i) { diff --git a/app/layoutconfigdialog.h b/app/layoutconfigdialog.h index fde770922..b0d00bf71 100644 --- a/app/layoutconfigdialog.h +++ b/app/layoutconfigdialog.h @@ -68,7 +68,7 @@ private slots: void itemChanged(QStandardItem *item); private: - void addLayoutForFile(QString file, QString layoutName = QString()); + void addLayoutForFile(QString file, QString layoutName = QString(), bool newTempDirectory = true); void loadLayouts(); void recalculateAvailableActivities(); void insertLayoutInfoAtRow(int row, QString path, QString color, QString name, bool menu, QStringList activities); diff --git a/app/layoutmanager.cpp b/app/layoutmanager.cpp index 46ccbcea5..7d972df9e 100644 --- a/app/layoutmanager.cpp +++ b/app/layoutmanager.cpp @@ -94,6 +94,11 @@ DockCorona *LayoutManager::corona() return m_corona; } +Importer *LayoutManager::importer() +{ + return m_importer; +} + QAction *LayoutManager::toggleLayoutAction() { return m_toggleLayoutAction; diff --git a/app/layoutmanager.h b/app/layoutmanager.h index ed4c77f97..79de6c87d 100644 --- a/app/layoutmanager.h +++ b/app/layoutmanager.h @@ -57,6 +57,7 @@ public: ~LayoutManager() override; DockCorona *corona(); + Importer *importer(); void load();