1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-03-09 00:58:15 +03:00

support dynamic layouts loading

This commit is contained in:
Michail Vourlakos 2017-06-26 22:16:42 +03:00
parent b162407082
commit b417ae52c3
3 changed files with 121 additions and 11 deletions

View File

@ -107,6 +107,8 @@ DockCorona::~DockCorona()
m_docksScreenSyncTimer.stop();
cleanConfig();
//qDebug() << "corona config file:" << config()->name();
while (!containments().isEmpty()) {
//deleting a containment will remove it from the list due to QObject::destroyed connect in Corona
delete containments().first();
@ -114,6 +116,7 @@ DockCorona::~DockCorona()
m_globalSettings->deleteLater();
m_globalShortcuts->deleteLater();
m_screenPool->deleteLater();
qDeleteAll(m_dockViews);
qDeleteAll(m_waitingDockViews);
@ -122,7 +125,31 @@ DockCorona::~DockCorona()
disconnect(m_activityConsumer, &KActivities::Consumer::serviceStatusChanged, this, &DockCorona::load);
delete m_activityConsumer;
qDebug() << "deleted" << this;
if (!m_layoutDir.isNull()) {
qDebug() << "layout directory found:" << m_layoutDir;
QFile latterc(m_layoutDir + "/lattedockrc");
QFile appletsrc(m_layoutDir + "/lattedock-appletsrc");
if (latterc.exists() && appletsrc.exists()) {
qDebug() << "updating latte layout...";
const auto homeLatterc = QDir::homePath() + "/.config/lattedockrc";
const auto homeAppletsrc = QDir::homePath() + "/.config/lattedock-appletsrc";
QFile::copy(latterc.fileName() , homeLatterc);
QFile::copy(appletsrc.fileName() , homeAppletsrc);
QDir tempLayoutDir(m_layoutDir);
if (tempLayoutDir.exists() && m_layoutDir.startsWith("/tmp")) {
qDebug()<< "old layout directory should be deleted...";
//tempLayoutDir.removeRecursively();
}
}
}
qDebug() << "latte corona deleted..." << this;
}
void DockCorona::load()
@ -147,6 +174,53 @@ void DockCorona::load()
}
}
void DockCorona::unload()
{
qDebug() << "unload: removing dockViews and containments...";
qDeleteAll(m_dockViews);
m_dockViews.clear();
m_waitingDockViews.clear();
while (!containments().isEmpty()) {
//deleting a containment will remove it from the list due to QObject::destroyed connect in Corona
//this form doesn't crash, while qDeleteAll(containments()) does
delete containments().first();
}
}
bool DockCorona::reloadLayout(QString path)
{
QFile latterc(path + "/lattedockrc");
QFile appletsrc(path + "/lattedock-appletsrc");
if (latterc.exists() && appletsrc.exists()) {
QDir oldLayoutDir(m_layoutDir);
m_layoutDir = path;
unload();
qDebug() << "reloadLayout: loading new layout - " << appletsrc.fileName();
loadLayout(appletsrc.fileName());
foreach (auto containment, containments())
addDock(containment);
if (oldLayoutDir.exists() && oldLayoutDir.absolutePath().startsWith("/tmp")
&& oldLayoutDir.absolutePath() != path) {
qDebug()<< "old layout directory should be deleted...";
// oldLayoutDir.removeRecursively();
}
return true;
}
return false;
}
void DockCorona::setupWaylandIntegration()
{
using namespace KWayland::Client;
@ -937,7 +1011,7 @@ void DockCorona::recreateDock(Plasma::Containment *containment)
//! step:2 add the new dockview
connect(view, &QObject::destroyed, this, [this, containment]() {
QTimer::singleShot(250, this, [this, containment](){
QTimer::singleShot(250, this, [this, containment]() {
if (!m_dockViews.contains(containment)) {
qDebug() << "recreate - step 2: adding dock for containment:" << containment->id();
addDock(containment);

View File

@ -69,6 +69,8 @@ public:
QList<Plasma::Types::Location> freeEdges(int screen) const;
QList<Plasma::Types::Location> freeEdges(QScreen *screen) const;
bool reloadLayout(QString path);
int docksCount() const;
int docksCount(int screen) const;
int docksCount(QScreen *screen) const;
@ -121,6 +123,8 @@ private:
void cleanConfig();
void qmlRegisterTypes() const;
void setupWaylandIntegration();
void unload();
bool appletExists(uint containmentId, uint appletId) const;
bool containmentContainsTasks(Plasma::Containment *cont);
bool containmentExists(uint id) const;
@ -138,6 +142,7 @@ private:
//! with tasks" will be loaded otherwise. Currently the older one dock wins
int m_firstContainmentWithTasks{ -1};
QString m_layoutDir;
Dock::SessionType m_session{Dock::DefaultSession};
QHash<const Plasma::Containment *, DockView *> m_dockViews;

View File

@ -8,6 +8,7 @@
#include <QProcess>
#include <QMessageBox>
#include <QDesktopServices>
#include <QTemporaryDir>
#include <QtDBus/QtDBus>
@ -408,7 +409,7 @@ void GlobalSettings::importConfiguration()
, m_fileDialog.data(), &QFileDialog::deleteLater);
connect(m_fileDialog.data(), &QFileDialog::fileSelected
, this, [&](const QString & file) {
, this, [&](const QString & file) {
importLayoutInternal(file);
});
@ -493,15 +494,29 @@ void GlobalSettings::importLayoutInternal(const QString &file)
}
auto rootDir = archive.directory();
QTemporaryDir uniqueTempDir;
uniqueTempDir.setAutoRemove(false);
QDir tempDir{uniqueTempDir.path()};
qDebug() << "temp layout directory : " << tempDir.absolutePath();
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")) {
continue;
if (!fileEntry->copyTo(tempDir.absolutePath())) {
archive.close();
showMsgError();
continue;
}
} else {
qInfo() << i18nc("import/export config", "The file has a wrong format!!!");
archive.close();
showMsgError();
return;
@ -509,14 +524,30 @@ void GlobalSettings::importLayoutInternal(const QString &file)
}
}
qDebug() << "Dynaminc Importing new layout" << file;
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() % 24);
QFile::copy(latterc, latterc + "." + n + ".bak");
QFile::copy(appletsrc, appletsrc + "." + n + ".bak");
qInfo() << "Importing the new configuration...";
if (m_corona->reloadLayout(QString(tempDir.absolutePath()))) {
auto notification = new KNotification("import-done", KNotification::CloseOnTimeout);
notification->setText(i18nc("import/export config", "Configuration imported successfully"));
notification->sendEvent();
} else {
showMsgError();
}
archive.close();
//NOTE: Restart latte for import the new configuration
QProcess::startDetached(qGuiApp->applicationFilePath() + " --import \"" + file + "\"");
qGuiApp->exit();
}
void GlobalSettings::exportConfiguration()
{
if (m_fileDialog) {
@ -537,7 +568,7 @@ void GlobalSettings::exportConfiguration()
, m_fileDialog.data(), &QFileDialog::deleteLater);
connect(m_fileDialog.data(), &QFileDialog::fileSelected
, this, [&](const QString & file) {
, this, [&](const QString & file) {
auto showNotificationError = []() {
auto notification = new KNotification("export-fail", KNotification::CloseOnTimeout);
notification->setText(i18nc("import/export config", "Failed to export configuration"));
@ -575,7 +606,7 @@ void GlobalSettings::exportConfiguration()
notification->setText(i18nc("import/export config", "Configuration exported successfully"));
connect(notification, &KNotification::action1Activated
, this, [file]() {
, this, [file]() {
QDesktopServices::openUrl({QFileInfo(file).canonicalPath()});
});