1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-25 19:21:41 +03:00

make indicators infrastructure smarter

--indicators are now tracked only based on their
installation path, that means that are updated
more precise when changes are applied and
at the same time views are recreated only when
the show the specific indicator that was changed
This commit is contained in:
Michail Vourlakos 2019-12-31 15:49:53 +02:00
parent c44d5fa5b6
commit 91b345e615
5 changed files with 141 additions and 61 deletions

View File

@ -29,6 +29,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QProcess> #include <QProcess>
#include <QTemporaryDir> #include <QTemporaryDir>
#include <QTimer>
// KDE // KDE
#include <KDirWatch> #include <KDirWatch>
@ -49,23 +50,32 @@ Factory::Factory(QObject *parent)
{ {
m_parentWidget = new QWidget(); m_parentWidget = new QWidget();
m_watchedPaths = Latte::Layouts::Importer::standardPaths(); m_mainPaths = Latte::Layouts::Importer::standardPaths();
for(int i=0; i<m_watchedPaths.count(); ++i) { for(int i=0; i<m_mainPaths.count(); ++i) {
m_watchedPaths[i] = m_watchedPaths[i] + "/latte/indicators"; m_mainPaths[i] = m_mainPaths[i] + "/latte/indicators";
discoverNewIndicators(m_mainPaths[i]);
} }
reload();
//! track paths for changes //! track paths for changes
for(const auto &dir : m_watchedPaths) { for(const auto &dir : m_mainPaths) {
KDirWatch::self()->addDir(dir); KDirWatch::self()->addDir(dir);
} }
connect(KDirWatch::self(), &KDirWatch::dirty, this, [ & ](const QString & path) { connect(KDirWatch::self(), &KDirWatch::dirty, this, [ & ](const QString & path) {
if (m_watchedPaths.contains(path)) { if (m_indicatorsPaths.contains(path)) {
reload(); //! indicator updated
emit pluginsUpdated(); reload(path);
} else if (m_mainPaths.contains(path)){
//! consider indicator addition
discoverNewIndicators(path);
}
});
connect(KDirWatch::self(), &KDirWatch::deleted, this, [ & ](const QString & path) {
if (m_indicatorsPaths.contains(path)) {
//! indicator removed
removeIndicatorRecords(path);
} }
}); });
@ -111,62 +121,109 @@ KPluginMetaData Factory::metadata(QString pluginId)
return KPluginMetaData(); return KPluginMetaData();
} }
void Factory::reload() void Factory::reload(const QString &indicatorPath)
{ {
m_plugins.clear(); QString pluginChangedId;
m_pluginUiPaths.clear();
m_customPluginIds.clear();
m_customPluginNames.clear();
m_customLocalPluginIds.clear();
for(const auto &path : m_watchedPaths) { if (!indicatorPath.isEmpty() && indicatorPath != "." && indicatorPath != "..") {
QDir standard(path); QString metadataFile = indicatorPath + "/metadata.desktop";
if (standard.exists()) { if(QFileInfo(metadataFile).exists()) {
QStringList pluginDirs = standard.entryList(QStringList(),QDir::AllDirs | QDir::NoSymLinks); KPluginMetaData metadata = KPluginMetaData::fromDesktopFile(metadataFile);
for (const auto &pluginDir : pluginDirs) { if (metadataAreValid(metadata)) {
if (pluginDir != "." && pluginDir != "..") { pluginChangedId = metadata.pluginId();
QString metadataFile = standard.absolutePath() + "/" + pluginDir + "/metadata.desktop"; QString uiFile = indicatorPath + "/package/" + metadata.value("X-Latte-MainScript");
if(QFileInfo(metadataFile).exists()) { if (!m_plugins.contains(metadata.pluginId())) {
KPluginMetaData metadata = KPluginMetaData::fromDesktopFile(metadataFile); m_plugins[metadata.pluginId()] = metadata;
}
if (metadataAreValid(metadata)) { if (QFileInfo(uiFile).exists()) {
QString uiFile = standard.absolutePath() + "/" + pluginDir + "/package/" + metadata.value("X-Latte-MainScript"); m_pluginUiPaths[metadata.pluginId()] = QFileInfo(uiFile).absolutePath();
}
if (QFileInfo(uiFile).exists() && !m_plugins.contains(metadata.pluginId())) { if ((metadata.pluginId() != "org.kde.latte.default")
m_plugins[metadata.pluginId()] = metadata; && (metadata.pluginId() != "org.kde.latte.plasma")) {
if ((metadata.pluginId() != "org.kde.latte.default") if (!m_customPluginIds.contains(metadata.pluginId())) {
&& (metadata.pluginId() != "org.kde.latte.plasma")) { m_customPluginIds << metadata.pluginId();
m_customPluginIds << metadata.pluginId(); }
m_customPluginNames << metadata.name();
}
if (standard.absolutePath().startsWith(QDir::homePath())) { if (!m_customPluginNames.contains(metadata.name())) {
m_customLocalPluginIds << metadata.pluginId(); m_customPluginNames << metadata.name();
} }
}
m_pluginUiPaths[metadata.pluginId()] = QFileInfo(uiFile).absolutePath(); if (indicatorPath.startsWith(QDir::homePath())) {
m_customLocalPluginIds << metadata.pluginId();
}
}
QString pluginPath = metadata.fileName().remove("metadata.desktop"); qDebug() << " Indicator Package Loaded ::: " << metadata.name() << " [" << metadata.pluginId() << "]" << " - [" << indicatorPath <<"]";
qDebug() << " Indicator Package Loaded ::: " << metadata.name() << " [" << metadata.pluginId() << "]" << " - [" <<pluginPath<<"]";
/*qDebug() << " Indicator value ::: " << metadata.pluginId(); /*qDebug() << " Indicator value ::: " << metadata.pluginId();
qDebug() << " Indicator value ::: " << metadata.fileName(); qDebug() << " Indicator value ::: " << metadata.fileName();
qDebug() << " Indicator value ::: " << metadata.value("X-Latte-MainScript"); qDebug() << " Indicator value ::: " << metadata.value("X-Latte-MainScript");
qDebug() << " Indicator value ::: " << metadata.value("X-Latte-ConfigUi"); qDebug() << " Indicator value ::: " << metadata.value("X-Latte-ConfigUi");
qDebug() << " Indicator value ::: " << metadata.value("X-Latte-ConfigXml");*/ qDebug() << " Indicator value ::: " << metadata.value("X-Latte-ConfigXml");*/
}
}
}
}
}
} }
} }
emit customPluginsChanged(); if (!pluginChangedId.isEmpty()) {
emit indicatorChanged(pluginChangedId);
}
}
void Factory::discoverNewIndicators(const QString &main)
{
if (!m_mainPaths.contains(main)) {
return;
}
QDirIterator indicatorsDirs(main, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::NoIteratorFlags);
while(indicatorsDirs.hasNext()){
indicatorsDirs.next();
QString iPath = indicatorsDirs.filePath();
if (!m_indicatorsPaths.contains(iPath)) {
m_indicatorsPaths << iPath;
KDirWatch::self()->addDir(iPath);
reload(iPath);
}
}
}
void Factory::removeIndicatorRecords(const QString &path)
{
if (m_indicatorsPaths.contains(path)) {
QString pluginId = path.section('/',-1);
m_plugins.remove(pluginId);
m_pluginUiPaths.remove(pluginId);
int pos = m_customPluginIds.indexOf(pluginId);
m_customPluginIds.removeAt(pos);
m_customPluginNames.removeAt(pos);
m_customLocalPluginIds.removeAll(pluginId);
m_indicatorsPaths.removeAll(path);
qDebug() << " indicator removed 1 :: " << pluginId;
KDirWatch::self()->removeDir(path);
//! delay informing the removal in case it is just an update
QTimer::singleShot(1000, [this, pluginId]() {
emit indicatorRemoved(pluginId);
});
}
}
bool Factory::isCustomType(const QString &id) const
{
return ((id != "org.kde.latte.default") && (id != "org.kde.latte.plasma"));
} }
bool Factory::metadataAreValid(KPluginMetaData &metadata) bool Factory::metadataAreValid(KPluginMetaData &metadata)

View File

@ -52,6 +52,7 @@ public:
void removeIndicator(QString id); void removeIndicator(QString id);
bool pluginExists(QString id) const; bool pluginExists(QString id) const;
bool isCustomType(const QString &id) const;
QString uiPath(QString pluginName) const; QString uiPath(QString pluginName) const;
@ -63,11 +64,14 @@ public:
//! imports an indicator compressed file //! imports an indicator compressed file
static Latte::Types::ImportExportState importIndicatorFile(QString compressedFile); static Latte::Types::ImportExportState importIndicatorFile(QString compressedFile);
signals: signals:
void customPluginsChanged(); void indicatorChanged(const QString &indicatorId);
void pluginsUpdated(); void indicatorRemoved(const QString &indicatorId);
private: private:
void reload(); void reload(const QString &indicatorPath);
void removeIndicatorRecords(const QString &path);
void discoverNewIndicators(const QString &main);
private: private:
QHash<QString, KPluginMetaData> m_plugins; QHash<QString, KPluginMetaData> m_plugins;
@ -77,7 +81,9 @@ private:
QStringList m_customPluginNames; QStringList m_customPluginNames;
QStringList m_customLocalPluginIds; QStringList m_customLocalPluginIds;
QStringList m_watchedPaths; //! plugins paths
QStringList m_mainPaths;
QStringList m_indicatorsPaths;
QWidget *m_parentWidget; QWidget *m_parentWidget;
}; };

View File

@ -54,17 +54,19 @@ Indicator::Indicator(Latte::View *parent)
connect(m_view, &Latte::View::latteTasksArePresentChanged, this, &Indicator::latteTasksArePresentChanged); connect(m_view, &Latte::View::latteTasksArePresentChanged, this, &Indicator::latteTasksArePresentChanged);
connect(m_view, &Latte::View::customPluginsChanged, [this]() { connect(m_view, &Latte::View::indicatorPluginChanged, [this](const QString &indicatorId) {
if (m_corona && !m_corona->indicatorFactory()->pluginExists(m_type)) { if (m_corona && m_corona->indicatorFactory()->isCustomType(indicatorId)) {
emit customPluginsChanged();
}
});
connect(m_view, &Latte::View::indicatorPluginRemoved, [this](const QString &indicatorId) {
if (m_corona && m_type == indicatorId && !m_corona->indicatorFactory()->pluginExists(indicatorId)) {
setType("org.kde.latte.default"); setType("org.kde.latte.default");
} }
emit customPluginsChanged(); if (m_corona && m_corona->indicatorFactory()->isCustomType(indicatorId)) {
}); emit customPluginsChanged();
connect(this, &Indicator::pluginChanged, [this]() {
if ((m_type != "org.kde.latte.default") && m_type != "org.kde.latte.plasma") {
setCustomType(m_type);
} }
}); });
@ -266,6 +268,10 @@ void Indicator::load(QString type)
QString path = m_metadata.fileName(); QString path = m_metadata.fileName();
m_pluginPath = path.remove("metadata.desktop"); m_pluginPath = path.remove("metadata.desktop");
if (m_corona && m_corona->indicatorFactory()->isCustomType(type)) {
setCustomType(type);
}
updateScheme(); updateScheme();
updateComponent(); updateComponent();

View File

@ -253,9 +253,19 @@ void View::init()
connect(m_contextMenu, &ViewPart::ContextMenu::menuChanged, this, &View::contextMenuIsShownChanged); connect(m_contextMenu, &ViewPart::ContextMenu::menuChanged, this, &View::contextMenuIsShownChanged);
connect(m_corona->indicatorFactory(), &Latte::Indicator::Factory::pluginsUpdated, this, &View::reloadSource);
//! View sends this signal in order to avoid crashes from ViewPart::Indicator when the view is recreated //! View sends this signal in order to avoid crashes from ViewPart::Indicator when the view is recreated
connect(m_corona->indicatorFactory(), &Latte::Indicator::Factory::customPluginsChanged, this, &View::customPluginsChanged); connect(m_corona->indicatorFactory(), &Latte::Indicator::Factory::indicatorChanged, this, [&](const QString &indicatorId) {
emit indicatorPluginChanged(indicatorId);
});
connect(this, &View::indicatorPluginChanged, this, [&](const QString &indicatorId) {
if (m_indicator && m_indicator->type() == indicatorId) {
reloadSource();
}
});
connect(m_corona->indicatorFactory(), &Latte::Indicator::Factory::indicatorRemoved, this, &View::indicatorPluginRemoved);
connect(m_corona, &Latte::Corona::availableScreenRectChanged, this, &View::availableScreenRectChangedForViewParts); connect(m_corona, &Latte::Corona::availableScreenRectChanged, this, &View::availableScreenRectChangedForViewParts);
///!!!!! ///!!!!!

View File

@ -298,7 +298,8 @@ signals:
//! pass on signals to children in order to avoid crashes when View is recreated or destroyed //! pass on signals to children in order to avoid crashes when View is recreated or destroyed
void availableScreenRectChangedForViewParts(); void availableScreenRectChangedForViewParts();
void customPluginsChanged(); void indicatorPluginChanged(const QString &indicatorId);
void indicatorPluginRemoved(const QString &indicatorId);
//! are used to trigger the Corona relevant signals and in that //! are used to trigger the Corona relevant signals and in that
//! way we can disable any such signaling all together, e.g. through disconnectSensitiveSignals() //! way we can disable any such signaling all together, e.g. through disconnectSensitiveSignals()