import correctly old layouts from old config files

pull/1/head
Michail Vourlakos 8 years ago
parent 858d975ea5
commit cdc5478b7b

@ -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;
}
}

@ -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;
};

@ -37,9 +37,13 @@
#include <QStandardItemModel>
#include <QTemporaryDir>
#include <KArchive/KTar>
#include <KArchive/KArchiveEntry>
#include <KArchive/KArchiveDirectory>
#include <KLocalizedString>
#include <KNotification>
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: <b>%0</b> 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 &current, 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<QFont>(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) {

@ -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);

@ -94,6 +94,11 @@ DockCorona *LayoutManager::corona()
return m_corona;
}
Importer *LayoutManager::importer()
{
return m_importer;
}
QAction *LayoutManager::toggleLayoutAction()
{
return m_toggleLayoutAction;

@ -57,6 +57,7 @@ public:
~LayoutManager() override;
DockCorona *corona();
Importer *importer();
void load();

Loading…
Cancel
Save