File indexing completed on 2025-03-16 05:05:24
0001 /* 0002 SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "indicator.h" 0007 0008 // local 0009 #include <coretypes.h> 0010 #include "indicatorinfo.h" 0011 #include "../containmentinterface.h" 0012 #include "../view.h" 0013 #include "../../lattecorona.h" 0014 #include "../../indicator/factory.h" 0015 0016 // Qt 0017 #include <QFileDialog> 0018 #include <QFileInfo> 0019 #include <QLatin1String> 0020 0021 // KDE 0022 #include <KLocalizedString> 0023 #include <KPluginMetaData> 0024 #include <KDeclarative/ConfigPropertyMap> 0025 #include <KDeclarative/QmlObjectSharedEngine> 0026 0027 0028 namespace Latte { 0029 namespace ViewPart { 0030 0031 Indicator::Indicator(Latte::View *parent) 0032 : QObject(parent), 0033 m_view(parent), 0034 m_info(new IndicatorPart::Info(this)), 0035 m_resources(new IndicatorPart::Resources(this)) 0036 { 0037 m_corona = qobject_cast<Latte::Corona *>(m_view->corona()); 0038 loadConfig(); 0039 0040 connect(this, &Indicator::enabledChanged, this, &Indicator::saveConfig); 0041 connect(this, &Indicator::pluginChanged, this, &Indicator::saveConfig); 0042 0043 connect(m_view->extendedInterface(), &ContainmentInterface::hasLatteTasksChanged, this, &Indicator::latteTasksArePresentChanged); 0044 0045 connect(m_view, &Latte::View::indicatorPluginChanged, [this](const QString &indicatorId) { 0046 if (m_corona && m_corona->indicatorFactory()->isCustomType(indicatorId)) { 0047 emit customPluginsChanged(); 0048 } 0049 }); 0050 0051 connect(m_view, &Latte::View::indicatorPluginRemoved, [this](const QString &indicatorId) { 0052 if (m_corona && m_type == indicatorId && !m_corona->indicatorFactory()->pluginExists(indicatorId)) { 0053 setType("org.kde.latte.default"); 0054 } 0055 0056 if (m_corona && m_corona->indicatorFactory()->isCustomType(indicatorId)) { 0057 emit customPluginsChanged(); 0058 } 0059 }); 0060 0061 load(m_type); 0062 0063 loadPlasmaComponent(); 0064 } 0065 0066 Indicator::~Indicator() 0067 { 0068 unloadIndicators(); 0069 0070 if (m_component) { 0071 m_component->deleteLater(); 0072 } 0073 0074 if (m_configLoader) { 0075 m_configLoader->deleteLater(); 0076 } 0077 0078 if (m_configuration) { 0079 m_configuration->deleteLater(); 0080 } 0081 0082 if (m_info) { 0083 m_info->deleteLater(); 0084 } 0085 } 0086 0087 bool Indicator::enabled() const 0088 { 0089 return m_enabled; 0090 } 0091 0092 void Indicator::setEnabled(bool enabled) 0093 { 0094 if (m_enabled == enabled) { 0095 return; 0096 } 0097 0098 m_enabled = enabled; 0099 emit enabledChanged(); 0100 } 0101 0102 bool Indicator::enabledForApplets() const 0103 { 0104 return m_enabledForApplets; 0105 } 0106 0107 void Indicator::setEnabledForApplets(bool enabled) 0108 { 0109 if (m_enabledForApplets == enabled) { 0110 return; 0111 } 0112 0113 m_enabledForApplets = enabled; 0114 emit enabledForAppletsChanged(); 0115 } 0116 0117 bool Indicator::isCustomIndicator() const 0118 { 0119 return m_corona->indicatorFactory()->isCustomType(type()); 0120 } 0121 0122 bool Indicator::latteTasksArePresent() 0123 { 0124 return m_view->extendedInterface()->hasLatteTasks(); 0125 } 0126 0127 bool Indicator::pluginIsReady() 0128 { 0129 return m_pluginIsReady; 0130 } 0131 0132 void Indicator::setPluginIsReady(bool ready) 0133 { 0134 if (m_pluginIsReady == ready) { 0135 return; 0136 } 0137 0138 m_pluginIsReady = ready; 0139 emit pluginIsReadyChanged(); 0140 } 0141 0142 int Indicator::index(const QString &type) 0143 { 0144 if (type == QLatin1String("org.kde.latte.default")) { 0145 return 0; 0146 } else if (type == QLatin1String("org.kde.latte.plasma")) { 0147 return 1; 0148 } else if (type == QLatin1String("org.kde.latte.plasmatabstyle")) { 0149 return 2; 0150 } else if (customPluginIds().contains(type)){ 0151 return 3 + customPluginIds().indexOf(type); 0152 } 0153 0154 return -1; 0155 } 0156 0157 QString Indicator::type() const 0158 { 0159 return m_type; 0160 } 0161 0162 void Indicator::setType(QString type) 0163 { 0164 if (m_type == type) { 0165 return; 0166 } 0167 0168 load(type); 0169 } 0170 0171 QString Indicator::customType() const 0172 { 0173 return m_customType; 0174 } 0175 0176 void Indicator::setCustomType(QString type) 0177 { 0178 if (m_customType == type) { 0179 return; 0180 } 0181 0182 m_customType = type; 0183 emit customPluginChanged(); 0184 } 0185 0186 int Indicator::customPluginsCount() const 0187 { 0188 return m_corona->indicatorFactory()->customPluginsCount(); 0189 } 0190 0191 QString Indicator::uiPath() const 0192 { 0193 return m_corona->indicatorFactory()->uiPath(m_type); 0194 } 0195 0196 QStringList Indicator::customPluginIds() const 0197 { 0198 return m_corona->indicatorFactory()->customPluginIds(); 0199 } 0200 0201 QStringList Indicator::customPluginNames() const 0202 { 0203 return m_corona->indicatorFactory()->customPluginNames(); 0204 } 0205 0206 QStringList Indicator::customLocalPluginIds() const 0207 { 0208 return m_corona->indicatorFactory()->customLocalPluginIds(); 0209 } 0210 0211 IndicatorPart::Info *Indicator::info() const 0212 { 0213 return m_info; 0214 } 0215 0216 IndicatorPart::Resources *Indicator::resources() const 0217 { 0218 return m_resources; 0219 } 0220 0221 QQmlComponent *Indicator::component() const 0222 { 0223 return m_component; 0224 } 0225 0226 QQmlComponent *Indicator::plasmaComponent() const 0227 { 0228 return m_plasmaComponent; 0229 } 0230 0231 QObject *Indicator::configuration() const 0232 { 0233 return m_configuration; 0234 } 0235 0236 void Indicator::load(QString type) 0237 { 0238 KPluginMetaData metadata = m_corona->indicatorFactory()->metadata(type); 0239 0240 if (metadata.isValid()) { 0241 bool state{m_enabled}; 0242 //! remove all previous indicators 0243 setPluginIsReady(false); 0244 0245 m_metadata = metadata; 0246 m_type = type; 0247 m_pluginPath = QFileInfo(m_metadata.fileName()).absolutePath(); 0248 0249 if (m_corona && m_corona->indicatorFactory()->isCustomType(type)) { 0250 setCustomType(type); 0251 } 0252 0253 updateScheme(); 0254 updateComponent(); 0255 0256 emit pluginChanged(); 0257 0258 //! create all indicators with the new type 0259 setPluginIsReady(true); 0260 } else if (type!="org.kde.latte.default") { 0261 qDebug() << " Indicator metadata are not valid : " << type; 0262 setType("org.kde.latte.default"); 0263 } 0264 } 0265 0266 void Indicator::updateComponent() 0267 { 0268 auto prevComponent = m_component; 0269 0270 QString uiPath = m_metadata.value("X-Latte-MainScript"); 0271 0272 if (!uiPath.isEmpty()) { 0273 uiPath = m_pluginPath + "/package/" + uiPath; 0274 m_component = new QQmlComponent(m_view->engine(), uiPath); 0275 } 0276 0277 if (prevComponent) { 0278 prevComponent->deleteLater(); 0279 } 0280 } 0281 0282 void Indicator::loadPlasmaComponent() 0283 { 0284 auto prevComponent = m_plasmaComponent; 0285 0286 KPluginMetaData metadata = m_corona->indicatorFactory()->metadata("org.kde.latte.plasmatabstyle"); 0287 QString uiPath = metadata.value("X-Latte-MainScript"); 0288 0289 if (!uiPath.isEmpty()) { 0290 uiPath = QFileInfo(metadata.fileName()).absolutePath() + "/package/" + uiPath; 0291 m_plasmaComponent = new QQmlComponent(m_view->engine(), uiPath); 0292 } 0293 0294 if (prevComponent) { 0295 prevComponent->deleteLater(); 0296 } 0297 0298 emit plasmaComponentChanged(); 0299 } 0300 0301 void Indicator::unloadIndicators() 0302 { 0303 setPluginIsReady(false); 0304 } 0305 0306 void Indicator::updateScheme() 0307 { 0308 auto prevConfigLoader = m_configLoader; 0309 auto prevConfiguration = m_configuration; 0310 0311 QString xmlPath = m_metadata.value("X-Latte-ConfigXml"); 0312 0313 if (!xmlPath.isEmpty()) { 0314 QFile file(m_pluginPath + "/package/" + xmlPath); 0315 m_configLoader = new KConfigLoader(m_view->containment()->config().group("Indicator").group(m_metadata.pluginId()), &file); 0316 m_configuration = new KDeclarative::ConfigPropertyMap(m_configLoader, this); 0317 } else { 0318 m_configLoader = nullptr; 0319 m_configuration = nullptr; 0320 } 0321 0322 if (prevConfigLoader) { 0323 prevConfigLoader->deleteLater(); 0324 } 0325 0326 if (prevConfiguration) { 0327 prevConfiguration->deleteLater(); 0328 } 0329 0330 emit configurationChanged(); 0331 } 0332 0333 void Indicator::loadConfig() 0334 { 0335 auto config = m_view->containment()->config().group("Indicator"); 0336 m_customType = config.readEntry("customType", QString()); 0337 m_enabled = config.readEntry("enabled", true); 0338 m_type = config.readEntry("type", "org.kde.latte.default"); 0339 } 0340 0341 void Indicator::saveConfig() 0342 { 0343 auto config = m_view->containment()->config().group("Indicator"); 0344 config.writeEntry("customType", m_customType); 0345 config.writeEntry("enabled", m_enabled); 0346 config.writeEntry("type", m_type); 0347 } 0348 0349 } 0350 }