diff --git a/shell/containmentconfigview.cpp b/shell/containmentconfigview.cpp index 4c9d146e..269ae2b3 100644 --- a/shell/containmentconfigview.cpp +++ b/shell/containmentconfigview.cpp @@ -1,246 +1,246 @@ /* * Copyright 2013 Marco Martin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "currentcontainmentactionsmodel.h" #include "containmentconfigview.h" #include "plasmaquick/configmodel.h" #include "shellcorona.h" #include "config-workspace.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //////////////////////////////ContainmentConfigView ContainmentConfigView::ContainmentConfigView(Plasma::Containment *cont, QWindow *parent) : ConfigView(cont, parent), m_containment(cont), m_wallpaperConfigModel(0), m_containmentActionConfigModel(0), m_containmentPluginsConfigModel(0), m_currentContainmentActionsModel(0), m_currentWallpaperConfig(0), m_ownWallpaperConfig(0) { qmlRegisterType(); rootContext()->setContextProperty(QStringLiteral("configDialog"), this); setCurrentWallpaper(cont->containment()->wallpaper()); KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Wallpaper")); pkg.setPath(m_containment->wallpaper()); KConfigGroup cfg = m_containment->config(); cfg = KConfigGroup(&cfg, "Wallpaper"); syncWallpaperObjects(); } ContainmentConfigView::~ContainmentConfigView() { } void ContainmentConfigView::init() { setSource(QUrl::fromLocalFile(m_containment->corona()->kPackage().filePath("containmentconfigurationui"))); } PlasmaQuick::ConfigModel *ContainmentConfigView::containmentActionConfigModel() { if (!m_containmentActionConfigModel) { m_containmentActionConfigModel = new PlasmaQuick::ConfigModel(this); KPluginInfo::List actions = Plasma::PluginLoader::self()->listContainmentActionsInfo(QString()); KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Generic")); foreach (const KPluginInfo &info, actions) { pkg.setDefaultPackageRoot(QStandardPaths::locate(QStandardPaths::GenericDataLocation, PLASMA_RELATIVE_DATA_INSTALL_DIR "/containmentactions", QStandardPaths::LocateDirectory)); m_containmentActionConfigModel->appendCategory(info.icon(), info.name(), pkg.filePath("ui", QStringLiteral("config.qml")), info.pluginName()); } } return m_containmentActionConfigModel; } QAbstractItemModel *ContainmentConfigView::currentContainmentActionsModel() { if (!m_currentContainmentActionsModel) { m_currentContainmentActionsModel = new CurrentContainmentActionsModel(m_containment, this); } return m_currentContainmentActionsModel; } QString ContainmentConfigView::containmentPlugin() const { return m_containment->pluginMetaData().pluginId(); } void ContainmentConfigView::setContainmentPlugin(const QString &plugin) { if (plugin.isEmpty() || containmentPlugin() == plugin) { return; } m_containment = static_cast(m_containment->corona())->setContainmentTypeForScreen(m_containment->screen(), plugin); emit containmentPluginChanged(); } PlasmaQuick::ConfigModel *ContainmentConfigView::wallpaperConfigModel() { if (!m_wallpaperConfigModel) { m_wallpaperConfigModel = new PlasmaQuick::ConfigModel(this); QStringList dirs(QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, PLASMA_RELATIVE_DATA_INSTALL_DIR "/wallpapers", QStandardPaths::LocateDirectory)); KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("KPackage/Generic")); QStringList platform = KDeclarative::KDeclarative::runtimePlatform(); if (!platform.isEmpty()) { QMutableStringListIterator it(platform); while (it.hasNext()) { it.next(); it.setValue("platformcontents/" + it.value()); } platform.append(QStringLiteral("contents")); pkg.setContentsPrefixPaths(platform); } pkg.addFileDefinition("mainscript", QStringLiteral("ui/main.qml"), i18n("Main Script File")); foreach (const QString &dirPath, dirs) { QDir dir(dirPath); pkg.setDefaultPackageRoot(dirPath); QStringList packages; foreach (const QString &sdir, dir.entryList(QDir::AllDirs | QDir::Readable)) { - QString metadata = dirPath + '/' + sdir + "/metadata.desktop"; - if (QFile::exists(metadata)) { + const QString metadata = dirPath + '/' + sdir; + if (QFile::exists(metadata + "/metadata.json") || QFile::exists(metadata + "/metadata.desktop")) { packages << sdir; } } foreach (const QString &package, packages) { pkg.setPath(package); if (!pkg.isValid()) { continue; } m_wallpaperConfigModel->appendCategory(pkg.metadata().iconName(), pkg.metadata().name(), pkg.filePath("ui", QStringLiteral("config.qml")), package); } } } return m_wallpaperConfigModel; } PlasmaQuick::ConfigModel *ContainmentConfigView::containmentPluginsConfigModel() { if (!m_containmentPluginsConfigModel) { m_containmentPluginsConfigModel = new PlasmaQuick::ConfigModel(this); KPluginInfo::List actions = Plasma::PluginLoader::self()->listContainmentsOfType(QStringLiteral("Desktop")); foreach (const KPluginInfo &info, actions) { m_containmentPluginsConfigModel->appendCategory(info.icon(), info.name(), QString(), info.pluginName()); } } return m_containmentPluginsConfigModel; } KDeclarative::ConfigPropertyMap *ContainmentConfigView::wallpaperConfiguration() const { return m_currentWallpaperConfig; } QString ContainmentConfigView::currentWallpaper() const { return m_currentWallpaper; } void ContainmentConfigView::setCurrentWallpaper(const QString &wallpaper) { if (m_currentWallpaper == wallpaper) { return; } delete m_ownWallpaperConfig; m_ownWallpaperConfig = 0; if (m_containment->wallpaper() == wallpaper) { syncWallpaperObjects(); } else { //we have to construct an independent ConfigPropertyMap when we want to configure wallpapers that are not the current one KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Generic")); pkg.setDefaultPackageRoot(PLASMA_RELATIVE_DATA_INSTALL_DIR "/wallpapers"); pkg.setPath(wallpaper); QFile file(pkg.filePath("config", QStringLiteral("main.xml"))); KConfigGroup cfg = m_containment->config(); cfg = KConfigGroup(&cfg, "Wallpaper"); m_currentWallpaperConfig = m_ownWallpaperConfig = new KDeclarative::ConfigPropertyMap(new KConfigLoader(cfg, &file), this); } m_currentWallpaper = wallpaper; emit currentWallpaperChanged(); emit wallpaperConfigurationChanged(); } void ContainmentConfigView::applyWallpaper() { m_containment->setWallpaper(m_currentWallpaper); syncWallpaperObjects(); if (m_currentWallpaperConfig && m_ownWallpaperConfig) { for (const auto &key : m_ownWallpaperConfig->keys()) { m_currentWallpaperConfig->insert(key, m_ownWallpaperConfig->value(key)); } } delete m_ownWallpaperConfig; m_ownWallpaperConfig = 0; emit wallpaperConfigurationChanged(); } void ContainmentConfigView::syncWallpaperObjects() { QObject *wallpaperGraphicsObject = m_containment->property("wallpaperGraphicsObject").value(); if (!wallpaperGraphicsObject) { return; } rootContext()->setContextProperty(QStringLiteral("wallpaper"), wallpaperGraphicsObject); //FIXME: why m_wallpaperGraphicsObject->property("configuration").value() doesn't work? m_currentWallpaperConfig = static_cast(wallpaperGraphicsObject->property("configuration").value()); } #include "private/moc_containmentconfigview.cpp" diff --git a/shell/scripting/scriptengine_v1.cpp b/shell/scripting/scriptengine_v1.cpp index 23cb8399..5420ad6c 100644 --- a/shell/scripting/scriptengine_v1.cpp +++ b/shell/scripting/scriptengine_v1.cpp @@ -1,935 +1,943 @@ /* * Copyright 2009 Aaron Seigo * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "scriptengine_v1.h" #include #include #include #include #include #include #include #include #include #include #include #include // KIO //#include // no camelcase include #include #include #include #include #include #include #include "appinterface.h" #include "containment.h" #include "configgroup.h" #include "i18n.h" #include "panel.h" #include "widget.h" #include "../shellcorona.h" #include "../standaloneappcorona.h" #include "../screenpool.h" namespace { template inline void awaitFuture(const QFuture &future) { while (!future.isFinished()) { QCoreApplication::processEvents(); } } class ScriptArray_forEach_Helper { public: ScriptArray_forEach_Helper(const QScriptValue &array) : array(array) { } // operator + is commonly used for these things // to avoid having the lambda inside the parenthesis template void operator+ (Function function) const { if (!array.isArray()) return; int length = array.property("length").toInteger(); for (int i = 0; i < length; ++i) { function(array.property(i)); } } private: const QScriptValue &array; }; #define SCRIPT_ARRAY_FOREACH(Variable, Array) \ ScriptArray_forEach_Helper(Array) + [&] (const QScriptValue &Variable) class ScriptObject_forEach_Helper { public: ScriptObject_forEach_Helper(const QScriptValue &object) : object(object) { } // operator + is commonly used for these things // to avoid having the lambda inside the parenthesis template void operator+ (Function function) const { QScriptValueIterator it(object); while (it.hasNext()) { it.next(); function(it.name(), it.value()); } } private: const QScriptValue &object; }; #define SCRIPT_OBJECT_FOREACH(Key, Value, Array) \ ScriptObject_forEach_Helper(Array) + [&] (const QString &Key, const QScriptValue &Value) // Case insensitive comparison of two strings template inline bool matches(const QString &object, const StringType &string) { return object.compare(string, Qt::CaseInsensitive) == 0; } } namespace WorkspaceScripting { QScriptValue ScriptEngine::V1::desktopById(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() == 0) { return context->throwError(i18n("activityById requires an id")); } const uint id = context->argument(0).toInt32(); ScriptEngine *env = envFor(engine); foreach (Plasma::Containment *c, env->m_corona->containments()) { if (c->id() == id && !isPanel(c)) { return env->wrap(c); } } return engine->undefinedValue(); } QScriptValue ScriptEngine::V1::desktopsForActivity(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() == 0) { return context->throwError(i18n("desktopsForActivity requires an id")); } QScriptValue containments = engine->newArray(); int count = 0; const QString id = context->argument(0).toString(); ScriptEngine *env = envFor(engine); const auto result = env->desktopsForActivity(id); for (Containment* c: result) { containments.setProperty(count, env->wrap(c)); ++count; } containments.setProperty(QStringLiteral("length"), count); return containments; } QScriptValue ScriptEngine::V1::desktopForScreen(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() == 0) { return context->throwError(i18n("activityForScreen requires a screen id")); } const uint screen = context->argument(0).toInt32(); ScriptEngine *env = envFor(engine); return env->wrap(env->m_corona->containmentForScreen(screen)); } QScriptValue ScriptEngine::V1::createActivity(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() < 0) { return context->throwError(i18n("createActivity required the activity name")); } const QString name = context->argument(0).toString(); QString plugin = context->argument(1).toString(); ScriptEngine *env = envFor(engine); KActivities::Controller controller; // This is not the nicest way to do this, but createActivity // is a synchronous API :/ QFuture futureId = controller.addActivity(name); awaitFuture(futureId); QString id = futureId.result(); qDebug() << "Setting default Containment plugin:" << plugin; ShellCorona *sc = qobject_cast(env->m_corona); StandaloneAppCorona *ac = qobject_cast(env->m_corona); if (sc) { if (plugin.isEmpty() || plugin == QLatin1String("undefined")) { plugin = sc->defaultContainmentPlugin(); } sc->insertActivity(id, plugin); } else if (ac) { if (plugin.isEmpty() || plugin == QLatin1String("undefined")) { KConfigGroup shellCfg = KConfigGroup(KSharedConfig::openConfig(env->m_corona->package().filePath("defaults")), "Desktop"); plugin = shellCfg.readEntry("Containment", "org.kde.desktopcontainment"); } ac->insertActivity(id, plugin); } return QScriptValue(id); } QScriptValue ScriptEngine::V1::setCurrentActivity(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() < 0) { return context->throwError(i18n("setCurrentActivity required the activity id")); } const QString id = context->argument(0).toString(); KActivities::Controller controller; QFuture task = controller.setCurrentActivity(id); awaitFuture(task); return QScriptValue(task.result()); } QScriptValue ScriptEngine::V1::setActivityName(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() < 2) { return context->throwError(i18n("setActivityName required the activity id and name")); } const QString id = context->argument(0).toString(); const QString name = context->argument(1).toString(); KActivities::Controller controller; QFuture task = controller.setActivityName(id, name); awaitFuture(task); return QScriptValue(); } QScriptValue ScriptEngine::V1::activityName(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() < 1) { return context->throwError(i18n("setActivityName required the activity id and name")); } const QString id = context->argument(0).toString(); KActivities::Info info(id); return QScriptValue(info.name()); } QScriptValue ScriptEngine::V1::currentActivity(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) Q_UNUSED(context) KActivities::Consumer consumer; return consumer.currentActivity(); } QScriptValue ScriptEngine::V1::activities(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(context) return qScriptValueFromSequence(engine, envFor(engine)->availableActivities()); } // Utility function to process configs and config groups template void loadSerializedConfigs(Object *object, const QScriptValue &configs) { SCRIPT_OBJECT_FOREACH(escapedGroup, config, configs) { // If the config group is set, pass it on to the containment QStringList groups = escapedGroup.split('/', QString::SkipEmptyParts); for (QString &group: groups) { group = QUrl::fromPercentEncoding(group.toUtf8()); } qDebug() << "Config group" << groups; object->setCurrentConfigGroup(groups); // Read other properties and set the configuration SCRIPT_OBJECT_FOREACH(key, value, config) { object->writeConfig(key, value.toVariant()); }; }; } QScriptValue ScriptEngine::V1::loadSerializedLayout(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() < 1) { return context->throwError(i18n("loadSerializedLayout requires the JSON object to deserialize from")); } ScriptEngine *env = envFor(engine); const auto data = context->argument(0); if (data.property("serializationFormatVersion").toInteger() != 1) { return context->throwError(i18n("loadSerializedLayout: invalid version of the serialized object")); } const auto desktops = env->desktopsForActivity(KActivities::Consumer().currentActivity()); Q_ASSERT_X(desktops.size() != 0, "V1::loadSerializedLayout", "We need desktops"); // qDebug() << "DESKTOP DESERIALIZATION: Loading desktops..."; int count = 0; SCRIPT_ARRAY_FOREACH(desktopData, data.property("desktops")) { // If the template has more desktops than we do, ignore them if (count >= desktops.size()) return; auto desktop = desktops[count]; // qDebug() << "DESKTOP DESERIALIZATION: var cont = desktopsArray[...]; " << count << " -> " << desktop; // Setting the wallpaper plugin because it is special desktop->setWallpaperPlugin(desktopData.property("wallpaperPlugin").toString()); // qDebug() << "DESKTOP DESERIALIZATION: cont->setWallpaperPlugin(...) " << desktop->wallpaperPlugin(); // Now, lets go through the configs loadSerializedConfigs(desktop, desktopData.property("config")); // After the config, we want to load the applets SCRIPT_ARRAY_FOREACH(appletData, desktopData.property("applets")) { // qDebug() << "DESKTOP DESERIALIZATION: Applet: " << appletData.toString(); // TODO: It would be nicer to be able to call addWidget directly auto desktopObject = env->wrap(desktop); auto addAppletFunction = desktopObject.property("addWidget"); QScriptValueList args { appletData.property("plugin"), appletData.property("geometry.x").toInteger() * ScriptEngine::gridUnit(), appletData.property("geometry.y").toInteger() * ScriptEngine::gridUnit(), appletData.property("geometry.width").toInteger() * ScriptEngine::gridUnit(), appletData.property("geometry.height").toInteger() * ScriptEngine::gridUnit() }; auto appletObject = addAppletFunction.call(desktopObject, args); if (auto applet = qobject_cast(appletObject.toQObject())) { // Now, lets go through the configs for the applet loadSerializedConfigs(applet, appletData.property("config")); } }; count++; }; // qDebug() << "PANEL DESERIALIZATION: Loading panels..."; SCRIPT_ARRAY_FOREACH(panelData, data.property("panels")) { const auto panel = qobject_cast(env->createContainment( QStringLiteral("Panel"), QStringLiteral("org.kde.panel"))); Q_ASSERT(panel); // Basic panel setup panel->setLocation(panelData.property("location").toString()); panel->setHeight(panelData.property("height").toNumber() * ScriptEngine::gridUnit()); panel->setMaximumLength(panelData.property("maximumLength").toNumber() * ScriptEngine::gridUnit()); panel->setMinimumLength(panelData.property("minimumLength").toNumber() * ScriptEngine::gridUnit()); panel->setOffset(panelData.property("offset").toNumber() * ScriptEngine::gridUnit()); panel->setAlignment(panelData.property("alignment").toString()); // Loading the config for the panel loadSerializedConfigs(panel, panelData.property("config")); // Now dealing with the applets SCRIPT_ARRAY_FOREACH(appletData, panelData.property("applets")) { // qDebug() << "PANEL DESERIALIZATION: Applet: " << appletData.toString(); // TODO: It would be nicer to be able to call addWidget directly auto panelObject = env->wrap(panel); auto addAppletFunction = panelObject.property("addWidget"); QScriptValueList args { appletData.property("plugin") }; auto appletObject = addAppletFunction.call(panelObject, args); // qDebug() << "PANEL DESERIALIZATION: addWidget" // << appletData.property("plugin").toString() // ; if (auto applet = qobject_cast(appletObject.toQObject())) { // Now, lets go through the configs for the applet loadSerializedConfigs(applet, appletData.property("config")); } }; }; return QScriptValue(); } QScriptValue ScriptEngine::V1::newPanel(QScriptContext *context, QScriptEngine *engine) { QString plugin(QStringLiteral("org.kde.panel")); if (context->argumentCount() > 0) { plugin = context->argument(0).toString(); } return createContainment(QStringLiteral("Panel"), plugin, context, engine); } QScriptValue ScriptEngine::V1::panelById(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() == 0) { return context->throwError(i18n("panelById requires an id")); } const uint id = context->argument(0).toInt32(); ScriptEngine *env = envFor(engine); foreach (Plasma::Containment *c, env->m_corona->containments()) { if (c->id() == id && isPanel(c)) { return env->wrap(c); } } return engine->undefinedValue(); } QScriptValue ScriptEngine::V1::panels(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(context) QScriptValue panels = engine->newArray(); ScriptEngine *env = envFor(engine); int count = 0; foreach (Plasma::Containment *c, env->m_corona->containments()) { if (isPanel(c)) { panels.setProperty(count, env->wrap(c)); ++count; } } panels.setProperty(QStringLiteral("length"), count); return panels; } QScriptValue ScriptEngine::V1::fileExists(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { return false; } const QString path = context->argument(0).toString(); if (path.isEmpty()) { return false; } QFile f(KShell::tildeExpand(path)); return f.exists(); } QScriptValue ScriptEngine::V1::loadTemplate(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { // qDebug() << "no arguments"; return false; } const QString layout = context->argument(0).toString(); if (layout.isEmpty() || layout.contains(QStringLiteral("'"))) { // qDebug() << "layout is empty"; return false; } auto filter = [&layout](const KPluginMetaData &md) -> bool { return md.pluginId() == layout && md.value(QStringLiteral("X-Plasma-ContainmentCategories")).contains(QStringLiteral("panel")); }; QList offers = KPackage::PackageLoader::self()->findPackages(QStringLiteral("Plasma/LayoutTemplate"), QString(), filter); if (offers.isEmpty()) { // qDebug() << "offers fail" << constraint; return false; } KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LayoutTemplate")); KPluginMetaData pluginData(offers.first()); QString path; { ScriptEngine *env = envFor(engine); ShellCorona *sc = qobject_cast(env->m_corona); if (sc) { const QString overridePackagePath = sc->lookAndFeelPackage().path() + QStringLiteral("contents/layouts/") + pluginData.pluginId(); + path = overridePackagePath + QStringLiteral("/metadata.json"); + if (QFile::exists(path)) { + package.setPath(overridePackagePath); + } + path = overridePackagePath + QStringLiteral("/metadata.desktop"); if (QFile::exists(path)) { package.setPath(overridePackagePath); } } } if (!package.isValid()) { - path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, package.defaultPackageRoot() + pluginData.pluginId() + "/metadata.desktop"); + path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, package.defaultPackageRoot() + pluginData.pluginId() + "/metadata.json"); + if (path.isEmpty()) { + path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, package.defaultPackageRoot() + pluginData.pluginId() + "/metadata.desktop"); + } if (path.isEmpty()) { // qDebug() << "script path is empty"; return false; } package.setPath(pluginData.pluginId()); } const QString scriptFile = package.filePath("mainscript"); if (scriptFile.isEmpty()) { // qDebug() << "scriptfile is empty"; return false; } QFile file(scriptFile); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << i18n("Unable to load script file: %1", path); return false; } QString script = file.readAll(); if (script.isEmpty()) { // qDebug() << "script is empty"; return false; } ScriptEngine *env = envFor(engine); env->globalObject().setProperty(QStringLiteral("templateName"), env->newVariant(pluginData.name()), QScriptValue::ReadOnly | QScriptValue::Undeletable); env->globalObject().setProperty(QStringLiteral("templateComment"), env->newVariant(pluginData.description()), QScriptValue::ReadOnly | QScriptValue::Undeletable); QScriptValue rv = env->newObject(); QScriptContext *ctx = env->pushContext(); ctx->setThisObject(rv); env->evaluateScript(script, path); env->popContext(); return rv; } QScriptValue ScriptEngine::V1::applicationExists(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { return false; } const QString application = context->argument(0).toString(); if (application.isEmpty()) { return false; } // first, check for it in $PATH if (!QStandardPaths::findExecutable(application).isEmpty()) { return true; } if (KService::serviceByStorageId(application)) { return true; } if (application.contains(QStringLiteral("'"))) { // apostrophes just screw up the trader lookups below, so check for it return false; } // next, consult ksycoca for an app by that name if (!KServiceTypeTrader::self()->query(QStringLiteral("Application"), QStringLiteral("Name =~ '%1'").arg(application)).isEmpty()) { return true; } // next, consult ksycoca for an app by that generic name if (!KServiceTypeTrader::self()->query(QStringLiteral("Application"), QStringLiteral("GenericName =~ '%1'").arg(application)).isEmpty()) { return true; } return false; } QScriptValue ScriptEngine::V1::defaultApplication(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { return false; } const QString application = context->argument(0).toString(); if (application.isEmpty()) { return false; } const bool storageId = context->argumentCount() < 2 ? false : context->argument(1).toBool(); // FIXME: there are some pretty horrible hacks below, in the sense that they // assume a very // specific implementation system. there is much room for improvement here. // see // kdebase-runtime/kcontrol/componentchooser/ for all the gory details ;) if (matches(application, QLatin1String("mailer"))) { // KEMailSettings settings; // in KToolInvocation, the default is kmail; but let's be friendlier :) // QString command = settings.getSetting(KEMailSettings::ClientProgram); QString command; if (command.isEmpty()) { if (KService::Ptr kontact = KService::serviceByStorageId(QStringLiteral("kontact"))) { return storageId ? kontact->storageId() : onlyExec(kontact->exec()); } else if (KService::Ptr kmail = KService::serviceByStorageId(QStringLiteral("kmail"))) { return storageId ? kmail->storageId() : onlyExec(kmail->exec()); } } if (!command.isEmpty()) { if (false) { KConfigGroup confGroup(KSharedConfig::openConfig(), "General"); const QString preferredTerminal = confGroup.readPathEntry( "TerminalApplication", QStringLiteral("konsole")); command = preferredTerminal + QLatin1String(" -e ") + command; } return command; } } else if (matches(application, QLatin1String("browser"))) { KConfigGroup config(KSharedConfig::openConfig(), "General"); QString browserApp = config.readPathEntry("BrowserApplication", QString()); if (browserApp.isEmpty()) { const KService::Ptr htmlApp = KMimeTypeTrader::self()->preferredService(QStringLiteral("text/html")); if (htmlApp) { browserApp = storageId ? htmlApp->storageId() : htmlApp->exec(); } } else if (browserApp.startsWith('!')) { browserApp = browserApp.mid(1); } return onlyExec(browserApp); } else if (matches(application, QLatin1String("terminal"))) { KConfigGroup confGroup(KSharedConfig::openConfig(), "General"); return onlyExec(confGroup.readPathEntry("TerminalApplication", QStringLiteral("konsole"))); } else if (matches(application, QLatin1String("filemanager"))) { KService::Ptr service = KMimeTypeTrader::self()->preferredService( QStringLiteral("inode/directory")); if (service) { return storageId ? service->storageId() : onlyExec(service->exec()); } } else if (matches(application, QLatin1String("windowmanager"))) { KConfig cfg(QStringLiteral("ksmserverrc"), KConfig::NoGlobals); KConfigGroup confGroup(&cfg, "General"); return onlyExec( confGroup.readEntry("windowManager", QStringLiteral("kwin"))); } else if (KService::Ptr service = KMimeTypeTrader::self()->preferredService(application)) { return storageId ? service->storageId() : onlyExec(service->exec()); } else { // try the files in share/apps/kcm_componentchooser/ const QStringList services = QStandardPaths::locateAll( QStandardPaths::GenericDataLocation, QStringLiteral("kcm_componentchooser/")); qDebug() << "ok, trying in" << services; foreach (const QString &service, services) { if (!service.endsWith(QLatin1String(".desktop"))) { continue; } KConfig config(service, KConfig::SimpleConfig); KConfigGroup cg = config.group(QByteArray()); const QString type = cg.readEntry("valueName", QString()); // qDebug() << " checking" << service << type << application; if (matches(type, application)) { KConfig store( cg.readPathEntry("storeInFile", QStringLiteral("null"))); KConfigGroup storeCg(&store, cg.readEntry("valueSection", QString())); const QString exec = storeCg.readPathEntry( cg.readEntry("valueName", "kcm_componenchooser_null"), cg.readEntry("defaultImplementation", QString())); if (!exec.isEmpty()) { return exec; } break; } } } return false; } QScriptValue ScriptEngine::V1::applicationPath(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { return false; } const QString application = context->argument(0).toString(); if (application.isEmpty()) { return false; } // first, check for it in $PATH const QString path = QStandardPaths::findExecutable(application); if (!path.isEmpty()) { return path; } if (KService::Ptr service = KService::serviceByStorageId(application)) { return QStandardPaths::locate(QStandardPaths::ApplicationsLocation, service->entryPath()); } if (application.contains(QStringLiteral("'"))) { // apostrophes just screw up the trader lookups below, so check for it return QString(); } // next, consult ksycoca for an app by that name KService::List offers = KServiceTypeTrader::self()->query( QStringLiteral("Application"), QStringLiteral("Name =~ '%1'").arg(application)); if (offers.isEmpty()) { // next, consult ksycoca for an app by that generic name offers = KServiceTypeTrader::self()->query( QStringLiteral("Application"), QStringLiteral("GenericName =~ '%1'").arg(application)); } if (!offers.isEmpty()) { KService::Ptr offer = offers.first(); return QStandardPaths::locate(QStandardPaths::ApplicationsLocation, offer->entryPath()); } return QString(); } QScriptValue ScriptEngine::V1::userDataPath(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) if (context->argumentCount() == 0) { return QDir::homePath(); } const QString type = context->argument(0).toString(); if (type.isEmpty()) { return QDir::homePath(); } QStandardPaths::StandardLocation location = QStandardPaths::GenericDataLocation; if (matches(type, QLatin1String("desktop"))) { location = QStandardPaths::DesktopLocation; } else if (matches(type, QLatin1String("documents"))) { location = QStandardPaths::DocumentsLocation; } else if (matches(type, QLatin1String("music"))) { location = QStandardPaths::MusicLocation; } else if (matches(type, QLatin1String("video"))) { location = QStandardPaths::MoviesLocation; } else if (matches(type, QLatin1String("downloads"))) { location = QStandardPaths::DownloadLocation; } else if (matches(type, QLatin1String("pictures"))) { location = QStandardPaths::PicturesLocation; } else if (matches(type, QLatin1String("config"))) { location = QStandardPaths::GenericConfigLocation; } if (context->argumentCount() > 1) { QString loc = QStandardPaths::writableLocation(location); loc.append(QDir::separator()); loc.append(context->argument(1).toString()); return loc; } const QStringList &locations = QStandardPaths::standardLocations(location); return locations.count() ? locations.first() : QString(); } QScriptValue ScriptEngine::V1::knownWallpaperPlugins(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(engine) QString formFactor; if (context->argumentCount() > 0) { formFactor = context->argument(0).toString(); } QString constraint; if (!formFactor.isEmpty()) { constraint.append("[X-Plasma-FormFactors] ~~ '") .append(formFactor) .append("'"); } const QList wallpapers = KPackage::PackageLoader::self()->listPackages( QStringLiteral("Plasma/Wallpaper"), QString()); QScriptValue rv = engine->newArray(wallpapers.size()); for (auto wp : wallpapers) { rv.setProperty(wp.name(), engine->newArray(0)); } return rv; } QScriptValue ScriptEngine::V1::configFile(QScriptContext *context, QScriptEngine *engine) { ConfigGroup *file = 0; if (context->argumentCount() > 0) { if (context->argument(0).isString()) { file = new ConfigGroup; const QString &fileName = context->argument(0).toString(); const ScriptEngine *env = envFor(engine); const Plasma::Corona *corona = env->corona(); if (fileName == corona->config()->name()) { file->setConfig(corona->config()); } else { file->setFile(fileName); } if (context->argumentCount() > 1) { file->setGroup(context->argument(1).toString()); } } else if (ConfigGroup *parent = qobject_cast( context->argument(0).toQObject())) { file = new ConfigGroup(parent); if (context->argumentCount() > 1) { file->setGroup(context->argument(1).toString()); } } } else { file = new ConfigGroup; } QScriptValue v = engine->newQObject(file, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeSuperClassProperties | QScriptEngine::ExcludeSuperClassMethods); return v; } QScriptValue ScriptEngine::V1::desktops(QScriptContext *context, QScriptEngine *engine) { Q_UNUSED(context) QScriptValue containments = engine->newArray(); ScriptEngine *env = envFor(engine); int count = 0; foreach (Plasma::Containment *c, env->corona()->containments()) { // make really sure we get actual desktops, so check for a non empty // activty id if (!isPanel(c) && !c->activity().isEmpty()) { containments.setProperty(count, env->wrap(c)); ++count; } } containments.setProperty(QStringLiteral("length"), count); return containments; } QScriptValue ScriptEngine::V1::gridUnit() { return ScriptEngine::gridUnit(); } QScriptValue ScriptEngine::V1::createContainment(const QString &type, const QString &defaultPlugin, QScriptContext *context, QScriptEngine *engine) { const QString plugin = context->argumentCount() > 0 ? context->argument(0).toString() : defaultPlugin; ScriptEngine *env = envFor(engine); auto result = env->createContainment(type, plugin); if (!result) { return context->throwError(i18n("Could not find a plugin for %1 named %2.", type, plugin)); } return env->wrap(result); } } // namespace WorkspaceScripting diff --git a/wallpapers/image/backgroundlistmodel.cpp b/wallpapers/image/backgroundlistmodel.cpp index b30d86da..6473179c 100644 --- a/wallpapers/image/backgroundlistmodel.cpp +++ b/wallpapers/image/backgroundlistmodel.cpp @@ -1,589 +1,589 @@ /*************************************************************************** * Copyright 2007 Paolo Capriotti * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef BACKGROUNDLISTMODEL_CPP #define BACKGROUNDLISTMODEL_CPP #include "backgroundlistmodel.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "image.h" QStringList BackgroundFinder::m_suffixes; ImageSizeFinder::ImageSizeFinder(const QString &path, QObject *parent) : QObject(parent), m_path(path) { } void ImageSizeFinder::run() { QImage image(m_path); Q_EMIT sizeFound(m_path, image.size()); } BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent) : QAbstractListModel(parent), m_wallpaper(wallpaper) { connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground); //TODO: on Qt 4.4 use the ui scale factor QFontMetrics fm(QGuiApplication::font()); m_screenshotSize = fm.width('M') * 15; m_imageCache = new KImageCache(QStringLiteral("plasma_wallpaper_preview"), 10485760); } BackgroundListModel::~BackgroundListModel() { delete m_imageCache; } QHash BackgroundListModel::BackgroundListModel::roleNames() const { return { { Qt::DisplayRole, "display" }, { Qt::DecorationRole, "decoration" }, { AuthorRole, "author" }, { ScreenshotRole, "screenshot" }, { ResolutionRole, "resolution" }, { PathRole, "path" }, { PackageNameRole, "packageName" }, { RemovableRole, "removable" }, { PendingDeletionRole, "pendingDeletion" }, }; } void BackgroundListModel::removeBackground(const QString &path) { QModelIndex index; while ((index = indexOf(path)).isValid()) { beginRemoveRows(QModelIndex(), index.row(), index.row()); m_packages.removeAt(index.row()); endRemoveRows(); emit countChanged(); } } void BackgroundListModel::reload() { reload(QStringList()); } void BackgroundListModel::reload(const QStringList &selected) { if (!m_packages.isEmpty()) { beginRemoveRows(QModelIndex(), 0, m_packages.count() - 1); m_packages.clear(); endRemoveRows(); emit countChanged(); } if (!m_wallpaper) { return; } if (!selected.isEmpty()) { qDebug() << "selected" << selected; processPaths(selected); } const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("wallpapers/"), QStandardPaths::LocateDirectory); qDebug() << " WP : -------" << dirs; BackgroundFinder *finder = new BackgroundFinder(m_wallpaper.data(), dirs); connect(finder, &BackgroundFinder::backgroundsFound, this, &BackgroundListModel::backgroundsFound); m_findToken = finder->token(); finder->start(); m_removableWallpapers = QSet::fromList(selected); } void BackgroundListModel::backgroundsFound(const QStringList &paths, const QString &token) { if (token == m_findToken) { processPaths(paths); } } void BackgroundListModel::processPaths(const QStringList &paths) { if (!m_wallpaper) { return; } QList newPackages; Q_FOREACH (QString file, paths) { // check if the path is a symlink and if it is, // work with the target rather than the symlink QFileInfo info(file); if (info.isSymLink()) { file = info.symLinkTarget(); } // now check if the path contains "contents" part // which could indicate that the file is part of some other // package (could have been symlinked) and we should work // with the package (which can already be present) rather // than just one file from it int contentsIndex = file.indexOf(QStringLiteral("contents")); // FIXME: additionally check for metadata.desktop being present // which would confirm a package but might be slowing things if (contentsIndex != -1) { file.truncate(contentsIndex); } // so now we have a path to a package, check if we're not // processing the same path twice (this is different from // the "!contains(file)" call lower down, that one checks paths // already in the model and does not include the paths // that are being checked in here); we want to check for duplicates // if and only if we actually changed the path (so the conditions from above // are reused here as that means we did change the path) if ((info.isSymLink() || contentsIndex != -1) && paths.contains(file)) { continue; } if (!contains(file) && QFile::exists(file)) { KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); package.setPath(file); if (package.isValid()) { m_wallpaper->findPreferedImageInPackage(package); newPackages << package; } } } // add new files to dirwatch Q_FOREACH (const KPackage::Package &b, newPackages) { if (!m_dirwatch.contains(b.path())) { m_dirwatch.addFile(b.path()); } } if (!newPackages.isEmpty()) { const int start = rowCount(); beginInsertRows(QModelIndex(), start, start + newPackages.size() - 1); m_packages.append(newPackages); endInsertRows(); emit countChanged(); } //qDebug() << t.elapsed(); } void BackgroundListModel::addBackground(const QString& path) { if (!m_wallpaper || !contains(path)) { if (!m_dirwatch.contains(path)) { m_dirwatch.addFile(path); } beginInsertRows(QModelIndex(), 0, 0); KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); m_removableWallpapers.insert(path); package.setPath(path); m_wallpaper->findPreferedImageInPackage(package); qDebug() << "WP Bckground added " << path << package.isValid(); m_packages.prepend(package); endInsertRows(); emit countChanged(); } } QModelIndex BackgroundListModel::indexOf(const QString &path) const { for (int i = 0; i < m_packages.size(); i++) { // packages will end with a '/', but the path passed in may not QString package = m_packages[i].path(); if (package.at(package.length() - 1) == QChar::fromLatin1('/')) { package.truncate(package.length() - 1); } if (path.startsWith(package)) { // FIXME: ugly hack to make a difference between local files in the same dir // package->path does not contain the actual file name qDebug() << "WP prefix" << m_packages[i].contentsPrefixPaths() << m_packages[i].filePath("preferred") << package << path; QStringList ps = m_packages[i].contentsPrefixPaths(); bool prefixempty = ps.count() == 0; if (!prefixempty) { prefixempty = ps[0].isEmpty(); } //For local files (user wallpapers) path == m_packages[i].filePath("preferred") //E.X. path = "/home/kde/next.png" //m_packages[i].filePath("preferred") = "/home/kde/next.png" // //But for the system wallpapers this is not the case. path != m_packages[i].filePath("preferred") //E.X. path = /usr/share/wallpapers/Next/" //m_packages[i].filePath("preferred") = "/usr/share/wallpapers/Next/contents/images/1920x1080.png" if ((path == m_packages[i].filePath("preferred")) || m_packages[i].filePath("preferred").contains(path)) { qDebug() << "WP TRUE" << (!m_packages[i].contentsPrefixPaths().isEmpty()) << (path == m_packages[i].filePath("preferred")); return index(i, 0); } } } return QModelIndex(); } bool BackgroundListModel::contains(const QString &path) const { //qDebug() << "WP contains: " << path << indexOf(path).isValid(); return indexOf(path).isValid(); } int BackgroundListModel::rowCount(const QModelIndex &) const { return m_packages.size(); } QSize BackgroundListModel::bestSize(const KPackage::Package &package) const { if (m_sizeCache.contains(package.path())) { return m_sizeCache.value(package.path()); } const QString image = package.filePath("preferred"); if (image.isEmpty()) { return QSize(); } ImageSizeFinder *finder = new ImageSizeFinder(image); connect(finder, &ImageSizeFinder::sizeFound, this, &BackgroundListModel::sizeFound); QThreadPool::globalInstance()->start(finder); QSize size(-1, -1); const_cast(this)->m_sizeCache.insert(package.path(), size); return size; } void BackgroundListModel::sizeFound(const QString &path, const QSize &s) { if (!m_wallpaper) { return; } QModelIndex index = indexOf(path); if (index.isValid()) { KPackage::Package package = m_packages.at(index.row()); m_sizeCache.insert(package.path(), s); emit dataChanged(index, index); } } QVariant BackgroundListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (index.row() >= m_packages.size()) { return QVariant(); } KPackage::Package b = package(index.row()); if (!b.isValid()) { return QVariant(); } switch (role) { case Qt::DisplayRole: { QString title = b.metadata().isValid() ? b.metadata().name() : QString(); if (title.isEmpty()) { return QFileInfo(b.filePath("preferred")).completeBaseName(); } return title; } break; case ScreenshotRole: { QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, m_screenshotSize)); if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { return preview; } // qDebug() << "WP preferred: " << b.filePath("preferred"); // qDebug() << "WP screenshot: " << b.filePath("screenshot"); QUrl file = QUrl::fromLocalFile(b.filePath("preferred")); if (!m_previewJobs.contains(file) && file.isValid()) { KFileItemList list; list.append(KFileItem(file, QString(), 0)); QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); KIO::PreviewJob* job = KIO::filePreview(list, QSize(m_screenshotSize*1.6, m_screenshotSize), &availablePlugins); job->setIgnoreMaximumSize(true); connect(job, &KIO::PreviewJob::gotPreview, this, &BackgroundListModel::showPreview); connect(job, &KIO::PreviewJob::failed, this, &BackgroundListModel::previewFailed); const_cast(this)->m_previewJobs.insert(file, QPersistentModelIndex(index)); } return QVariant(); } break; case AuthorRole: if (b.metadata().isValid() && !b.metadata().authors().isEmpty()) { return b.metadata().authors().first().name(); } else { return QString(); } break; case ResolutionRole:{ QSize size = bestSize(b); if (size.isValid()) { return QString::fromLatin1("%1x%2").arg(size.width()).arg(size.height()); } return QString(); } break; case PathRole: return QUrl::fromLocalFile(b.filePath("preferred")); break; case PackageNameRole: return !b.metadata().isValid() || b.metadata().pluginId().isEmpty() ? b.filePath("preferred") : b.metadata().pluginId(); break; case RemovableRole: { QString localWallpapers = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/wallpapers/"; QString path = b.filePath("preferred"); return path.startsWith(localWallpapers) || m_removableWallpapers.contains(path); } break; case PendingDeletionRole: { QUrl wallpaperUrl = QUrl::fromLocalFile(b.filePath("preferred")); return m_pendingDeletion.contains(wallpaperUrl.toLocalFile()) ? m_pendingDeletion[wallpaperUrl.toLocalFile()] : false; } break; default: return QVariant(); break; } Q_UNREACHABLE(); } bool BackgroundListModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) { return false; } if (role == PendingDeletionRole) { KPackage::Package b = package(index.row()); if (!b.isValid()) { return false; } const QUrl wallpaperUrl = QUrl::fromLocalFile(b.filePath("preferred")); m_pendingDeletion[wallpaperUrl.toLocalFile()] = value.toBool(); emit dataChanged(index, index); return true; } return false; } void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &preview) { if (!m_wallpaper) { return; } QPersistentModelIndex index = m_previewJobs.value(item.url()); m_previewJobs.remove(item.url()); if (!index.isValid()) { return; } KPackage::Package b = package(index.row()); if (!b.isValid()) { return; } m_imageCache->insertPixmap(b.filePath("preferred"), preview); //qDebug() << "WP preview size:" << preview.size(); emit dataChanged(index, index); } void BackgroundListModel::previewFailed(const KFileItem &item) { m_previewJobs.remove(item.url()); } KPackage::Package BackgroundListModel::package(int index) const { return m_packages.at(index); } void BackgroundListModel::setPendingDeletion(int rowIndex, bool pendingDeletion) { setData(index(rowIndex, 0), pendingDeletion, PendingDeletionRole); } const QStringList BackgroundListModel::wallpapersAwaitingDeletion() { QStringList candidates; for (const KPackage::Package &b : m_packages) { const QUrl wallpaperUrl = QUrl::fromLocalFile(b.filePath("preferred")); if (m_pendingDeletion.contains(wallpaperUrl.toLocalFile()) && m_pendingDeletion[wallpaperUrl.toLocalFile()]) { candidates << wallpaperUrl.toLocalFile(); } } return candidates; } BackgroundFinder::BackgroundFinder(Image *wallpaper, const QStringList &paths) : QThread(wallpaper), m_paths(paths), m_token(QUuid().toString()) { } BackgroundFinder::~BackgroundFinder() { wait(); } QString BackgroundFinder::token() const { return m_token; } const QStringList &BackgroundFinder::suffixes() { if (m_suffixes.isEmpty()) { QSet suffixes; QMimeDatabase db; Q_FOREACH (const QByteArray &mimeType, QImageReader::supportedMimeTypes()) { QMimeType mime(db.mimeTypeForName(mimeType)); Q_FOREACH (const QString &pattern, mime.globPatterns()) { suffixes.insert(pattern); } } m_suffixes = suffixes.toList(); } return m_suffixes; } bool BackgroundFinder::isAcceptableSuffix(const QString &suffix) { // Despite its name, suffixes() returns a list of glob patterns. // Therefore the file suffix check needs to include the "*." prefix. const QStringList &globPatterns = suffixes(); return globPatterns.contains("*."+suffix.toLower()); } void BackgroundFinder::run() { QTime t; t.start(); QStringList papersFound; QDir dir; dir.setFilter(QDir::AllDirs | QDir::Files | QDir::Hidden | QDir::Readable); dir.setNameFilters(suffixes()); KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); int i; for (i = 0; i < m_paths.count(); ++i) { const QString path = m_paths.at(i); dir.setPath(path); const QFileInfoList files = dir.entryInfoList(); Q_FOREACH (const QFileInfo &wp, files) { if (wp.isDir()) { //qDebug() << "scanning directory" << wp.fileName(); const QString name = wp.fileName(); if (name == QString::fromLatin1(".") || name == QString::fromLatin1("..")) { // do nothing continue; } const QString filePath = wp.filePath(); - if (QFile::exists(filePath + QString::fromLatin1("/metadata.desktop"))) { + if (QFile::exists(filePath + QString::fromLatin1("/metadata.desktop")) || QFile::exists(filePath + QString::fromLatin1("/metadata.json"))) { package.setPath(filePath); if (package.isValid()) { if (!package.filePath("images").isEmpty()) { papersFound << package.path(); } //qDebug() << "adding package" << wp.filePath(); continue; } } // add this to the directories we should be looking at m_paths.append(filePath); } else { //qDebug() << "adding image file" << wp.filePath(); papersFound << wp.filePath(); } } } //qDebug() << "WP background found!" << papersFound.size() << "in" << i << "dirs, taking" << t.elapsed() << "ms"; Q_EMIT backgroundsFound(papersFound, m_token); deleteLater(); } #endif // BACKGROUNDLISTMODEL_CPP diff --git a/wallpapers/image/image.cpp b/wallpapers/image/image.cpp index 072ef652..49e29741 100644 --- a/wallpapers/image/image.cpp +++ b/wallpapers/image/image.cpp @@ -1,858 +1,860 @@ /*************************************************************************** * Copyright 2007 Paolo Capriotti * * Copyright 2007 Aaron Seigo * * Copyright 2008 Petri Damsten * * Copyright 2008 Alexis Ménard * * Copyright 2014 Sebastian Kügler * * Copyright 2015 Kai Uwe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "image.h" #include #include // FLT_MAX #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "backgroundlistmodel.h" #include Image::Image(QObject *parent) : QObject(parent), m_ready(false), m_delay(10), m_dirWatch(new KDirWatch(this)), m_mode(SingleImage), m_currentSlide(-1), m_model(0), m_dialog(0), m_width(0), m_height(0) { m_wallpaperPackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); connect(&m_timer, &QTimer::timeout, this, &Image::nextSlide); connect(m_dirWatch, &KDirWatch::created, this, &Image::pathCreated); connect(m_dirWatch, &KDirWatch::dirty, this, &Image::pathDirty); connect(m_dirWatch, &KDirWatch::deleted, this, &Image::pathDeleted); m_dirWatch->startScan(); connect(this, &Image::sizeChanged, this, &Image::setTargetSize); useSingleImageDefaults(); setSingleImage(); } Image::~Image() { delete m_dialog; } void Image::classBegin() { } void Image::componentComplete() { // don't bother loading single image until all properties have settled // otherwise we would load a too small image (initial view size) just // to load the proper one afterwards etc etc m_ready = true; if (m_mode == SingleImage) { setSingleImage(); } } QString Image::photosPath() const { return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); } QUrl Image::wallpaperPath() const { return QUrl::fromLocalFile(m_wallpaperPath); } void Image::addUrl(const QString &url) { addUrl(QUrl(url), true); } void Image::addUrls(const QStringList &urls) { bool first = true; for (const QString &url: urls) { // set the first drop as the current paper, just add the rest to the roll addUrl(QUrl(url), first); first = false; } } Image::RenderingMode Image::renderingMode() const { return m_mode; } void Image::setRenderingMode(RenderingMode mode) { if (mode == m_mode) { return; } m_mode = mode; if (m_mode == SlideShow) { if (m_slidePaths.isEmpty()) { m_slidePaths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("share/wallpapers"), QStandardPaths::LocateDirectory); } QTimer::singleShot(200, this, &Image::startSlideshow); updateDirWatch(m_slidePaths); updateDirWatch(m_slidePaths); } else { // we need to reset the prefered image setSingleImage(); } } float distance(const QSize& size, const QSize& desired) { // compute difference of areas float desiredAspectRatio = ( desired.height() > 0 ) ? desired.width() / (float)desired.height() : 0; float candidateAspectRatio = ( size.height() > 0 ) ? size.width() / (float)size.height() : FLT_MAX; float delta = size.width() - desired.width(); delta = (delta >= 0.0 ? delta : -delta*2 ); // Penalize for scaling up return qAbs(candidateAspectRatio - desiredAspectRatio)*25000 + delta; } QSize resSize(const QString &str) { int index = str.indexOf('x'); if (index != -1) { return QSize(str.leftRef(index).toInt(), str.midRef(index + 1).toInt()); } return QSize(); } QString Image::findPreferedImage(const QStringList &images) { if (images.empty()) { return QString(); } //float targetAspectRatio = (m_targetSize.height() > 0 ) ? m_targetSize.width() / (float)m_targetSize.height() : 0; //qDebug() << "wanted" << m_targetSize << "options" << images << "aspect ratio" << targetAspectRatio; float best = FLT_MAX; QString bestImage; foreach (const QString &entry, images) { QSize candidate = resSize(QFileInfo(entry).baseName()); if (candidate == QSize()) { continue; } //float candidateAspectRatio = (candidate.height() > 0 ) ? candidate.width() / (float)candidate.height() : FLT_MAX; float dist = distance(candidate, m_targetSize); //qDebug() << "candidate" << candidate << "distance" << dist << "aspect ratio" << candidateAspectRatio; if (bestImage.isEmpty() || dist < best) { bestImage = entry; best = dist; //qDebug() << "best" << bestImage; } } //qDebug() << "best image" << bestImage; return bestImage; } void Image::findPreferedImageInPackage(KPackage::Package &package) { if (!package.isValid() || !package.filePath("preferred").isEmpty()) { return; } QString preferred = findPreferedImage( package.entryList("images") ); package.removeDefinition("preferred"); package.addFileDefinition("preferred", "images/" + preferred, i18n("Recommended wallpaper file")); } QSize Image::targetSize() const { return m_targetSize; } void Image::setTargetSize(const QSize &size) { m_targetSize = size; if (m_mode == SingleImage) { setSingleImage(); } } int Image::height() const { return m_height; } void Image::setHeight(int h) { if (m_height != h) { m_height = h; emit sizeChanged(QSize(m_width, m_height)); } } int Image::width() const { return m_width; } void Image::setWidth(int w) { if (m_width != w) { m_width = w; emit sizeChanged(QSize(m_width, m_height)); } } KPackage::Package *Image::package() { return &m_wallpaperPackage; } void Image::useSingleImageDefaults() { Plasma::Theme theme; m_wallpaper = theme.wallpaperPath(); int index = m_wallpaper.indexOf(QString::fromLatin1("/contents/images/")); if (index > -1) { // We have file from package -> get path to package m_wallpaper = m_wallpaper.left(index); } } QAbstractItemModel* Image::wallpaperModel() { if (!m_model) { KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig(QStringLiteral("plasmarc")), QStringLiteral("Wallpapers")); m_usersWallpapers = cfg.readEntry("usersWallpapers", m_usersWallpapers); m_model = new BackgroundListModel(this, this); m_model->reload(m_usersWallpapers); } return m_model; } int Image::slideTimer() const { return m_delay; } void Image::setSlideTimer(int time) { if (time == m_delay) { return; } m_delay = time; if (m_mode == SlideShow) { updateDirWatch(m_slidePaths); startSlideshow(); } emit slideTimerChanged(); } QStringList Image::usersWallpapers() const { return m_usersWallpapers; } void Image::setUsersWallpapers(const QStringList &usersWallpapers) { if (usersWallpapers == m_usersWallpapers) { return; } m_usersWallpapers = usersWallpapers; emit usersWallpapersChanged(); } QStringList Image::slidePaths() const { return m_slidePaths; } void Image::setSlidePaths(const QStringList &slidePaths) { if (slidePaths == m_slidePaths) { return; } m_slidePaths = slidePaths; m_slidePaths.removeAll(QString()); if (m_slidePaths.isEmpty()) { m_slidePaths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("share/wallpapers"), QStandardPaths::LocateDirectory); } if (m_mode == SlideShow) { updateDirWatch(m_slidePaths); startSlideshow(); } emit slidePathsChanged(); } void Image::showAddSlidePathsDialog() { QFileDialog *dialog = new QFileDialog(0, i18n("Directory with the wallpaper to show slides from"), QLatin1String("")); dialog->setAttribute(Qt::WA_DeleteOnClose, true ); dialog->setOptions(QFileDialog::ShowDirsOnly); dialog->setAcceptMode(QFileDialog::AcceptOpen); connect(dialog, &QDialog::accepted, this, &Image::addDirFromSelectionDialog); dialog->show(); } void Image::addSlidePath(const QString &path) { if (!path.isEmpty() && !m_slidePaths.contains(path)) { m_slidePaths.append(path); if (m_mode == SlideShow) { updateDirWatch(m_slidePaths); } emit slidePathsChanged(); startSlideshow(); } } void Image::removeSlidePath(const QString &path) { if (m_slidePaths.contains(path)) { m_slidePaths.removeAll(path); if (m_mode == SlideShow) { updateDirWatch(m_slidePaths); } emit slidePathsChanged(); startSlideshow(); } } void Image::pathDirty(const QString& path) { updateDirWatch(QStringList(path)); } void Image::updateDirWatch(const QStringList &newDirs) { Q_FOREACH(const QString &oldDir, m_dirs) { if(!newDirs.contains(oldDir)) { m_dirWatch->removeDir(oldDir); } } Q_FOREACH(const QString &newDir, newDirs) { if(!m_dirWatch->contains(newDir)) { m_dirWatch->addDir(newDir, KDirWatch::WatchSubDirs | KDirWatch::WatchFiles); } } m_dirs = newDirs; } void Image::addDirFromSelectionDialog() { QFileDialog *dialog = qobject_cast(sender()); if (dialog) { addSlidePath(dialog->directoryUrl().toLocalFile()); } } void Image::syncWallpaperPackage() { m_wallpaperPackage.setPath(m_wallpaper); findPreferedImageInPackage(m_wallpaperPackage); m_wallpaperPath = m_wallpaperPackage.filePath("preferred"); } void Image::setSingleImage() { if (!m_ready) { return; } // supposedly QSize::isEmpty() is true if "either width or height are >= 0" if (!m_targetSize.width() || !m_targetSize.height()) { return; } const QString oldPath = m_wallpaperPath; if (m_wallpaper.isEmpty()) { useSingleImageDefaults(); } QString img; if (QDir::isAbsolutePath(m_wallpaper)) { syncWallpaperPackage(); if (QFile::exists(m_wallpaperPath)) { img = m_wallpaperPath; } } else { //if it's not an absolute path, check if it's just a wallpaper name - const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("wallpapers/") + QString(m_wallpaper + QString::fromLatin1("/metadata.desktop"))); + QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("wallpapers/") + QString(m_wallpaper + QString::fromLatin1("/metadata.json"))); + if (path.isEmpty()) + path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("wallpapers/") + QString(m_wallpaper + QString::fromLatin1("/metadata.desktop"))); if (!path.isEmpty()) { QDir dir(path); dir.cdUp(); syncWallpaperPackage(); img = m_wallpaperPath; } } if (img.isEmpty()) { // ok, so the package we have failed to work out; let's try the default useSingleImageDefaults(); syncWallpaperPackage(); } if (m_wallpaperPath != oldPath) { Q_EMIT wallpaperPathChanged(); } } void Image::addUrls(const QList &urls) { bool first = true; Q_FOREACH (const QUrl &url, urls) { // set the first drop as the current paper, just add the rest to the roll addUrl(url, first); first = false; } } void Image::addUrl(const QUrl &url, bool setAsCurrent) { QString path; if (url.isLocalFile()) { path = url.toLocalFile(); } else if (url.scheme().isEmpty()) { if (QDir::isAbsolutePath(url.path())) { path = url.path(); } else { path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("wallpapers/") + url.path(), QStandardPaths::LocateDirectory); } if (path.isEmpty()) { return; } } else { QString wallpaperPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("wallpapers/") + url.path(); if (!wallpaperPath.isEmpty()) { KIO::FileCopyJob *job = KIO::file_copy(url, QUrl(wallpaperPath), -1, KIO::HideProgressInfo); if (setAsCurrent) { connect(job, &KJob::result, this, &Image::setWallpaperRetrieved); } else { connect(job, &KJob::result, this, &Image::addWallpaperRetrieved); } } return; } if (setAsCurrent) { setWallpaper(path); } else { if (m_mode != SingleImage) { // it's a slide show, add it to the slide show m_slideshowBackgrounds.append(path); m_unseenSlideshowBackgrounds.append(path); } // always add it to the user papers, though addUsersWallpaper(path); } } void Image::setWallpaperRetrieved(KJob *job) { KIO::FileCopyJob *copyJob = qobject_cast(job); if (copyJob && !copyJob->error()) { setWallpaper(copyJob->destUrl().toLocalFile()); } } void Image::addWallpaperRetrieved(KJob *job) { KIO::FileCopyJob *copyJob = qobject_cast(job); if (copyJob && !copyJob->error()) { addUrl(copyJob->destUrl(), false); } } void Image::setWallpaper(const QString &path) { if (m_mode == SingleImage) { m_wallpaper = path; setSingleImage(); } else { m_slideshowBackgrounds.append(path); m_unseenSlideshowBackgrounds.clear(); m_currentSlide = m_slideshowBackgrounds.size() - 2; nextSlide(); } //addUsersWallpaper(path); } void Image::startSlideshow() { if(m_findToken.isEmpty()) { // populate background list m_timer.stop(); m_slideshowBackgrounds.clear(); m_unseenSlideshowBackgrounds.clear(); BackgroundFinder *finder = new BackgroundFinder(this, m_dirs); m_findToken = finder->token(); connect(finder, &BackgroundFinder::backgroundsFound, this, &Image::backgroundsFound); finder->start(); //TODO: what would be cool: paint on the wallpaper itself a busy widget and perhaps some text //about loading wallpaper slideshow while the thread runs } else { m_scanDirty = true; } } void Image::backgroundsFound(const QStringList &paths, const QString &token) { if (token != m_findToken) { return; } m_findToken.clear(); if(m_scanDirty) { m_scanDirty = false; startSlideshow(); return; } m_slideshowBackgrounds = paths; m_unseenSlideshowBackgrounds.clear(); // start slideshow if (m_slideshowBackgrounds.isEmpty()) { // no image has been found, which is quite weird... try again later (this is useful for events which // are not detected by KDirWatch, like a NFS directory being mounted) QTimer::singleShot(1000, this, &Image::startSlideshow); } else { m_currentSlide = -1; nextSlide(); m_timer.start(m_delay * 1000); } } void Image::getNewWallpaper() { if (!m_newStuffDialog) { m_newStuffDialog = new KNS3::DownloadDialog( QString::fromLatin1("wallpaper.knsrc") ); KNS3::DownloadDialog *strong = m_newStuffDialog.data(); strong->setTitle(i18n("Download Wallpapers")); connect(m_newStuffDialog.data(), &QDialog::accepted, this, &Image::newStuffFinished); } m_newStuffDialog.data()->show(); } void Image::newStuffFinished() { if (m_model && (!m_newStuffDialog || m_newStuffDialog.data()->changedEntries().size() > 0)) { m_model->reload(m_usersWallpapers); } } void Image::showFileDialog() { if (!m_dialog) { QUrl baseUrl; if(m_wallpaper.indexOf(QDir::homePath()) > -1){ baseUrl = QUrl(m_wallpaper); } /* m_dialog = new KFileDialog(baseUrl, QString::fromLatin1("*.png *.jpeg *.jpg *.xcf *.svg *.svgz *.bmp"), 0); m_dialog->setOperationMode(KFileDialog::Opening); m_dialog->setInlinePreviewShown(true); m_dialog->setModal(false); connect(m_dialog, SIGNAL(okClicked()), this, SLOT(wallpaperBrowseCompleted())); connect(m_dialog, SIGNAL(destroyed(QObject*)), this, SLOT(fileDialogFinished())); */ QString path; const QStringList &locations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); if (!locations.isEmpty()) { path = locations.at(0); } else { // HomeLocation is guaranteed not to be empty. path = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).at(0); } QMimeDatabase db; QStringList imageGlobPatterns; foreach(const QByteArray &mimeType, QImageReader::supportedMimeTypes()) { QMimeType mime(db.mimeTypeForName(mimeType)); imageGlobPatterns << mime.globPatterns(); } m_dialog = new QFileDialog(0, i18n("Open Image"), path, i18n("Image Files") + " ("+imageGlobPatterns.join(' ') + ')'); //i18n people, this isn't a "word puzzle". there is a specific string format for QFileDialog::setNameFilters m_dialog->setFileMode(QFileDialog::ExistingFile); connect(m_dialog, &QDialog::accepted, this, &Image::wallpaperBrowseCompleted); } m_dialog->show(); m_dialog->raise(); m_dialog->activateWindow(); } void Image::fileDialogFinished() { m_dialog = 0; } void Image::wallpaperBrowseCompleted() { Q_ASSERT(m_model); if (m_dialog && m_dialog->selectedFiles().count() > 0) { addUsersWallpaper(m_dialog->selectedFiles().first()); emit customWallpaperPicked(); } } void Image::addUsersWallpaper(const QString &file) { QString f = file; f.replace(QLatin1String("file:/"), QLatin1String("")); const QFileInfo info(f); // FIXME //the full file path, so it isn't broken when dealing with symlinks const QString wallpaper = info.canonicalFilePath(); if (wallpaper.isEmpty()) { return; } if (m_model) { if (m_model->contains(wallpaper)) { return; } // add background to the model m_model->addBackground(wallpaper); } // save it KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig(QStringLiteral("plasmarc")), QStringLiteral("Wallpapers")); m_usersWallpapers = cfg.readEntry("usersWallpapers", m_usersWallpapers); if (!m_usersWallpapers.contains(wallpaper)) { m_usersWallpapers.prepend(wallpaper); cfg.writeEntry("usersWallpapers", m_usersWallpapers); cfg.sync(); emit usersWallpapersChanged(); } } void Image::nextSlide() { if (m_slideshowBackgrounds.isEmpty()) { return; } QString previousPath; if (m_currentSlide > -1 && m_currentSlide < m_unseenSlideshowBackgrounds.size()) { previousPath = m_unseenSlideshowBackgrounds.takeAt(m_currentSlide); } if (m_unseenSlideshowBackgrounds.isEmpty()) { m_unseenSlideshowBackgrounds = m_slideshowBackgrounds; // We're filling the queue again, make sure we can't pick up again // the last one picked from the previous set if (!previousPath.isEmpty()) { m_unseenSlideshowBackgrounds.removeAll(previousPath); // prevent empty list if (m_unseenSlideshowBackgrounds.isEmpty()) { m_unseenSlideshowBackgrounds = m_slideshowBackgrounds; } } } m_currentSlide = KRandom::random() % m_unseenSlideshowBackgrounds.size(); const QString currentPath = m_unseenSlideshowBackgrounds.at(m_currentSlide); m_wallpaperPackage.setPath(currentPath); findPreferedImageInPackage(m_wallpaperPackage); m_timer.stop(); m_timer.start(m_delay * 1000); QString current = m_wallpaperPackage.filePath("preferred"); if (current.isEmpty()) { m_wallpaperPath = currentPath; } else { m_wallpaperPath = current; } Q_EMIT wallpaperPathChanged(); } void Image::openSlide() { if (!m_wallpaperPackage.isValid()) { return; } // open in image viewer QUrl filepath(m_wallpaperPackage.filePath("preferred")); qDebug() << "opening file " << filepath.path(); new KRun(filepath, NULL); } void Image::pathCreated(const QString &path) { if(!m_slideshowBackgrounds.contains(path)) { QFileInfo fileInfo(path); if(fileInfo.isFile() && BackgroundFinder::isAcceptableSuffix(fileInfo.suffix())) { m_slideshowBackgrounds.append(path); m_unseenSlideshowBackgrounds.append(path); if(m_slideshowBackgrounds.count() == 1) { nextSlide(); } } } } void Image::pathDeleted(const QString &path) { if(m_slideshowBackgrounds.removeAll(path)) { m_unseenSlideshowBackgrounds.removeAll(path); if(path == m_img) { nextSlide(); } } } //FIXME: we have to save the configuration also when the dialog cancel button is clicked. void Image::removeWallpaper(QString name) { QString localWallpapers = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/wallpapers/"; QUrl nameUrl(name); //Package plugin name if (!name.contains('/')) { KPackage::Package p = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); KJob *j = p.uninstall(name, localWallpapers); connect(j, &KJob::finished, [=] () { m_model->reload(m_usersWallpapers); }); //absolute path in the home } else if (nameUrl.path().startsWith(localWallpapers)) { QFile f(nameUrl.path()); if (f.exists()) { f.remove(); } m_model->reload(m_usersWallpapers); } else { // save it KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig(QStringLiteral("plasmarc")), QStringLiteral("Wallpapers")); m_usersWallpapers = cfg.readEntry("usersWallpapers", m_usersWallpapers); int wallpaperIndex = -1; //passed as a path or as a file:// url? if (nameUrl.isValid()) { wallpaperIndex = m_usersWallpapers.indexOf(nameUrl.path()); } else { wallpaperIndex = m_usersWallpapers.indexOf(name); } if (wallpaperIndex >= 0){ m_usersWallpapers.removeAt(wallpaperIndex); m_model->reload(m_usersWallpapers); cfg.writeEntry("usersWallpapers", m_usersWallpapers); cfg.sync(); emit usersWallpapersChanged(); Q_EMIT settingsChanged(true); } } } void Image::commitDeletion() { //This is invokable from qml, so at any moment //we can't be sure the model exists if (!m_model) { return; } for (const QString &wallpaperCandidate : m_model->wallpapersAwaitingDeletion()) { removeWallpaper(wallpaperCandidate); } }