Commit 9ae537f8 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Port konsoleprofiles applet away from Dataengines

Task: https://phabricator.kde.org/T13321
parent 5b42f138
......@@ -89,8 +89,8 @@ ecm_set_disabled_deprecation_versions(
)
add_subdirectory(dict)
add_subdirectory(profiles)
add_subdirectory(applets)
add_subdirectory(dataengines)
add_subdirectory(runners)
add_subdirectory(wallpapers)
......
......@@ -9,6 +9,7 @@ import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PlasmaComponents3
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.plasma.private.profiles 1.0 as Profiles
FocusScope {
id: konsoleProfiles
......@@ -25,21 +26,13 @@ FocusScope {
}
}
PlasmaCore.DataSource {
id: profilesSource
engine: "org.kde.konsoleprofiles"
onSourceAdded: connectSource(source)
onSourceRemoved: disconnectSource(source)
Component.onCompleted: connectedSources = sources
}
PlasmaCore.SortFilterModel {
id: profilesModel
sortRole: "prettyName"
id: sortModel
sortRole: "name"
sortOrder: "AscendingOrder"
sourceModel: PlasmaCore.DataModel {
dataSource: profilesSource
sourceModel: Profiles.ProfilesModel {
id: profilesModel
appName: "konsole"
}
}
......@@ -95,7 +88,7 @@ FocusScope {
id: view
model: profilesModel
model: sortModel
clip: true
focus: true
keyNavigationWraps: true
......@@ -110,9 +103,11 @@ FocusScope {
}
function openProfile() {
var service = profilesSource.serviceForSource(model["DataEngineSource"])
/*var service = profilesSource.serviceForSource(model["DataEngineSource"])
var operation = service.operationDescription("open")
var job = service.startOperationCall(operation)
var = service.startOperationCall(operation)*/
console.error(model.profileIdentifier)
profilesModel.openProfile(model.profileIdentifier)
}
PlasmaComponents3.Label {
......@@ -127,7 +122,7 @@ FocusScope {
}
verticalAlignment: Text.AlignVCenter
text: model.prettyName
text: model.name
elide: Text.ElideRight
}
......
add_subdirectory(konsoleprofiles)
add_definitions(-DTRANSLATION_DOMAIN="plasma_engine_konsoleprofiles")
set(konsoleprofilesengine_SRCS
konsoleprofilesengine.cpp
konsoleprofilesservice.cpp
)
kcoreaddons_add_plugin(plasma_engine_konsoleprofiles SOURCES ${konsoleprofilesengine_SRCS} INSTALL_NAMESPACE "plasma/dataengine")
target_link_libraries(plasma_engine_konsoleprofiles
KF5::Plasma
KF5::Notifications
KF5::KIOGui
KF5::Service
)
install(FILES org.kde.plasma.dataengine.konsoleprofiles.operations DESTINATION ${PLASMA_DATA_INSTALL_DIR}/services)
#! /usr/bin/env bash
$XGETTEXT `find . -name \*.cpp` -o $podir/plasma_engine_konsoleprofiles.pot
/*
* SPDX-FileCopyrightText: 2011 Shaun Reich <shaun.reich@kdemail.net>
* SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KONSOLEPROFILESENGINE_H
#define KONSOLEPROFILESENGINE_H
#include <Plasma/DataEngine>
class KDirWatch;
class KonsoleProfilesEngine : public Plasma::DataEngine
{
Q_OBJECT
public:
explicit KonsoleProfilesEngine(QObject *parent, const QVariantList &args);
~KonsoleProfilesEngine() override;
void init();
Plasma::Service *serviceForSource(const QString &source) override;
private Q_SLOTS:
void profilesChanged();
private:
void loadProfiles();
KDirWatch *m_dirWatch;
};
#endif
/*
* SPDX-FileCopyrightText: 2011 Shaun Reich <shaun.reich@kdemail.net>
* SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "konsoleprofilesservice.h"
#include <QDebug>
#include <QMap>
#include <KNotificationJobUiDelegate>
#include <KIO/CommandLauncherJob>
KonsoleProfilesService::KonsoleProfilesService(QObject *parent, const QString &profileName)
: Plasma::Service(parent)
{
setName(QStringLiteral("org.kde.plasma.dataengine.konsoleprofiles"));
setDestination(profileName);
}
Plasma::ServiceJob *KonsoleProfilesService::createJob(const QString &operation, QMap<QString, QVariant> &parameters)
{
return new ProfileJob(this, operation, parameters);
}
ProfileJob::ProfileJob(KonsoleProfilesService *service, const QString &operation, const QMap<QString, QVariant> &parameters)
: Plasma::ServiceJob(service->destination(), operation, parameters, service)
{
}
void ProfileJob::start()
{
// destination is the profile name, operation is e.g. "open"
// QMap<QString, QVariant>jobParameters = parameters();
const QString operation = operationName();
qDebug() << "SERVICE START...operation: " << operation << " dest: " << destination();
if (operation == QLatin1String("open")) {
// Q_ASSERT(!jobParameters.isEmpty());
// Would be nice if we could just return this in createJob above
auto *job = new KIO::CommandLauncherJob(QStringLiteral("konsole"), {QStringLiteral("--profile"), destination()});
job->setDesktopName(QStringLiteral("org.kde.konsole"));
auto *delegate = new KNotificationJobUiDelegate;
delegate->setAutoErrorHandlingEnabled(true);
job->setUiDelegate(delegate);
connect(job, &KIO::CommandLauncherJob::result, this, [this, job] {
setError(job->error());
setErrorText(job->errorText());
emitResult();
});
job->start();
}
}
/*
* SPDX-FileCopyrightText: 2011 Shaun Reich <shaun.reich@kdemail.net>
* SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KONSOLEPROFILESSERVICE_H
#define KONSOLEPROFILESSERVICE_H
#include <Plasma/Service>
#include <Plasma/ServiceJob>
class KonsoleProfilesService : public Plasma::Service
{
Q_OBJECT
public:
KonsoleProfilesService(QObject *parent, const QString &profileName);
protected:
Plasma::ServiceJob *createJob(const QString &operation, QMap<QString, QVariant> &parameters) override;
};
class ProfileJob : public Plasma::ServiceJob
{
Q_OBJECT
public:
ProfileJob(KonsoleProfilesService *service, const QString &operation, const QMap<QString, QVariant> &parameters);
void start() override;
};
#endif
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE kcfg SYSTEM
"http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
<kcfg>
<group name="open">
</group>
</kcfg>
{
"KPlugin": {
"Authors": [
{
"Email": "shaun.reich@kdemail.net",
"Name": "Shaun Reich",
"Name[ar]": "Shaun Reich",
"Name[az]": "Shaun Reich",
"Name[ca@valencia]": "Shaun Reich",
"Name[ca]": "Shaun Reich",
"Name[cs]": "Shaun Reich",
"Name[de]": "Shaun Reich",
"Name[en_GB]": "Shaun Reich",
"Name[es]": "Shaun Reich",
"Name[eu]": "Shaun Reich",
"Name[fi]": "Shaun Reich",
"Name[fr]": "Shaun Reich",
"Name[ia]": "Shaun Reich",
"Name[id]": "Shaun Reich",
"Name[it]": "Shaun Reich",
"Name[ja]": "Shaun Reich",
"Name[ko]": "Shaun Reich",
"Name[lt]": "Shaun Reich",
"Name[nl]": "Shaun Reich",
"Name[nn]": "Shaun Reich",
"Name[pl]": "Shaun Reich",
"Name[pt]": "Shaun Reich",
"Name[pt_BR]": "Shaun Reich",
"Name[ro]": "Shaun Reich",
"Name[ru]": "Shaun Reich",
"Name[sk]": "Shaun Reich",
"Name[sl]": "Shaun Reich",
"Name[sv]": "Shaun Reich",
"Name[tr]": "Shaun Reich",
"Name[uk]": "Shaun Reich",
"Name[vi]": "Shaun Reich",
"Name[x-test]": "xxShaun Reichxx",
"Name[zh_CN]": "Shaun Reich"
}
],
"Category": "Utiltities",
"Description": "List and launch Konsole profiles",
"Description[ar]": "تعرض و تطلق تشكيلات كونسول",
"Description[az]": "Konsol profillərini sıralamaq və başlatmaq",
"Description[ca@valencia]": "Llista i inicia els perfils de Konsole",
"Description[ca]": "Llista i llança els perfils del Konsole",
"Description[cs]": "Seznam a spouštění profilů Konsole",
"Description[de]": "Konsole-Profile anzeigen und starten",
"Description[en_GB]": "List and launch Konsole profiles",
"Description[es]": "Listar y lanzar perfiles de Konsole",
"Description[eu]": "«Konsole»ren profilak zerrendatu eta jaurtitzea",
"Description[fi]": "Luettele ja käynnistä Konsole-profiileja",
"Description[fr]": "Lister et lancer des profils de Konsole",
"Description[ia]": "Lista e lancea profilos de Konsole",
"Description[id]": "Cantumkan dan luncurkan profil Konsole",
"Description[it]": "Elenca ed esegue i profili di Konsole",
"Description[ja]": "Konsole のプロファイルを起動します",
"Description[ko]": "Konsole 프로필을 보고 실행하기",
"Description[lt]": "Išvardyti ir paleisti Konsole profilius",
"Description[nl]": "Toon en start Konsole-profielen",
"Description[nn]": "Vis og start Konsoll-profilar",
"Description[pl]": "Wypisuje i uruchamia profile Konsoli",
"Description[pt]": "Listar e invocar os perfis do Konsole",
"Description[pt_BR]": "Lista e carrega os perfis do Konsole",
"Description[ro]": "Enumeră și lansează profiluri de Konsolă",
"Description[ru]": "Запуск Konsole для разных задач",
"Description[sk]": "Zoznam a spustenie profilov Konsole",
"Description[sl]": "Zlistaj in zaženi profile Konzole",
"Description[sv]": "Lista och starta terminalprofiler",
"Description[tr]": "Konsole profillerini listele ve çalıştır",
"Description[uk]": "Перегляньте і запустіть профілі Konsole",
"Description[vi]": "Liệt kê và khởi chạy các hồ sơ Konsole",
"Description[x-test]": "xxList and launch Konsole profilesxx",
"Description[zh_CN]": "列出 Konsole 配置并启动它们",
"EnabledByDefault": true,
"Icon": "utilities-terminal",
"Id": "org.kde.konsoleprofiles",
"License": "GPL-2.0+",
"Name": "Konsole Profiles",
"Name[ar]": "تشكيلات كونسول",
"Name[az]": "Konsol profilləri",
"Name[ca@valencia]": "Perfils de Konsole",
"Name[ca]": "Perfils del Konsole",
"Name[cs]": "Profily Konsole",
"Name[de]": "Konsole-Profile",
"Name[en_GB]": "Konsole Profiles",
"Name[es]": "Perfiles de Konsole",
"Name[eu]": "«Konsole»ren profilak",
"Name[fi]": "Konsole-profiilit",
"Name[fr]": "Profils de Konsole",
"Name[ia]": "Profilos de Konsole",
"Name[id]": "Profil Konsole",
"Name[it]": "Profili di Konsole",
"Name[ja]": "Konsole プロファイル",
"Name[ko]": "Konsole 프로필",
"Name[lt]": "Konsole profiliai",
"Name[nl]": "Konsole-profielen",
"Name[nn]": "Konsoll-profilar",
"Name[pl]": "Profile Konsoli",
"Name[pt]": "Perfis do Konsole",
"Name[pt_BR]": "Perfis do Konsole",
"Name[ro]": "Profiluri Konsolă",
"Name[ru]": "Konsole: профили",
"Name[sk]": "Profily Konsole",
"Name[sl]": "Profili Konzole",
"Name[sv]": "Terminalprofiler",
"Name[tr]": "Konsol Profilleri",
"Name[uk]": "Профілі Konsole",
"Name[vi]": "Hồ sơ Konsole",
"Name[x-test]": "xxKonsole Profilesxx",
"Name[zh_CN]": "Konsole 配置",
"Version": "1.0",
"Website": "https://kde.org/plasma-desktop/"
}
}
add_library(profiles_utility_static STATIC profilesmodel.cpp)
target_link_libraries(profiles_utility_static PRIVATE Qt::Core KF5::CoreAddons KF5::ConfigCore KF5::KIOGui)
target_include_directories(profiles_utility_static
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)
add_library(profiles_qml_plugin profilesplugin.cpp)
target_link_libraries(profiles_qml_plugin
Qt::Qml
profiles_utility_static
)
install(TARGETS profiles_qml_plugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/private/profiles/)
install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/private/profiles)
/*
* SPDX-FileCopyrightText: 2011 Shaun Reich <shaun.reich@kdemail.net>
* SPDX-FileCopyrightText: 2008 Montel Laurent <montel@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "konsoleprofilesengine.h"
#include "konsoleprofilesservice.h"
#include "profilesmodel.h"
// KF
#include <KConfig>
#include <KConfigGroup>
#include <KDirWatch>
// Qt
#include <KIO/CommandLauncherJob>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
KonsoleProfilesEngine::KonsoleProfilesEngine(QObject *parent, const QVariantList &args)
: Plasma::DataEngine(parent, args)
, m_dirWatch(nullptr)
ProfilesModel::ProfilesModel(QObject *parent)
: QAbstractListModel(parent)
{
init();
}
KonsoleProfilesEngine::~KonsoleProfilesEngine()
void ProfilesModel::init()
{
}
void KonsoleProfilesEngine::init()
{
qDebug() << "KonsoleProfilesDataEngine init";
m_dirWatch = new KDirWatch(this);
const QStringList konsoleDataBaseDirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
for (const QString &konsoleDataBaseDir : konsoleDataBaseDirs) {
m_dirWatch->addDir(konsoleDataBaseDir + QLatin1String("/konsole"));
m_dirWatch->addDir(konsoleDataBaseDir + QLatin1Char('/') + m_appName);
}
connect(m_dirWatch, &KDirWatch::dirty, this, &KonsoleProfilesEngine::profilesChanged);
connect(m_dirWatch, &KDirWatch::dirty, this, &ProfilesModel::loadProfiles);
loadProfiles();
}
Plasma::Service *KonsoleProfilesEngine::serviceForSource(const QString &source)
{
// create a new service for this profile's name, so it can be operated on.
return new KonsoleProfilesService(this, source);
}
void KonsoleProfilesEngine::profilesChanged()
{
// wipe the data clean, load it again. (there's not a better way of doing this but no big deal)
removeAllSources();
loadProfiles();
}
void KonsoleProfilesEngine::loadProfiles()
void ProfilesModel::loadProfiles()
{
QStringList profilesPaths;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("konsole"), QStandardPaths::LocateDirectory);
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, m_appName, QStandardPaths::LocateDirectory);
for (const auto &dir : dirs) {
const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.profile")});
......@@ -67,10 +45,12 @@ void KonsoleProfilesEngine::loadProfiles()
}
}
beginResetModel();
m_data.clear();
for (const auto &profilePath : std::as_const(profilesPaths)) {
QFileInfo info(profilePath);
const QString profileName = info.baseName();
QString niceName = profileName;
const QString profileIdentifier = info.baseName();
QString niceName = profileIdentifier;
KConfig cfg(profilePath, KConfig::SimpleConfig);
if (cfg.hasGroup("General")) {
......@@ -80,12 +60,41 @@ void KonsoleProfilesEngine::loadProfiles()
niceName = grp.readEntry("Name");
}
qDebug() << "adding sourcename: " << profileName << " ++" << niceName;
setData(profileName, QStringLiteral("prettyName"), niceName);
m_data.append(ProfileData{niceName, profileIdentifier});
}
}
endResetModel();
}
K_PLUGIN_CLASS_WITH_JSON(KonsoleProfilesEngine, "plasma-dataengine-konsoleprofiles.json")
QHash<int, QByteArray> ProfilesModel::roleNames() const
{
return {
{NameRole, "name"},
{ProfileIdentifierRole, "profileIdentifier"},
};
}
QVariant ProfilesModel::data(const QModelIndex &index, int role) const
{
const ProfileData &data = m_data.at(index.row());
switch (role) {
case NameRole:
return data.name;
case ProfileIdentifierRole:
return data.profileIdentifier;
default:
return QVariant();
}
}
#include "konsoleprofilesengine.moc"
void ProfilesModel::openProfile(const QString profileIdentifier)
{
KIO::CommandLauncherJob *job;
if (m_appName == QLatin1String("konsole")) {
job = new KIO::CommandLauncherJob(m_appName, QStringList{QStringLiteral("--profile"), profileIdentifier});
} else {
Q_UNREACHABLE();
}
job->start();
}
/*
SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QAbstractListModel>
class KDirWatch;
struct ProfileData {
QString name;
QString profileIdentifier;
};
class ProfilesModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString appName READ appName WRITE setAppName NOTIFY appNameChanged)
enum Roles {
NameRole = Qt::DisplayRole,
ProfileIdentifierRole = Qt::UserRole,
};
public:
explicit ProfilesModel(QObject *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE void openProfile(const QString profileIdentifier);
int rowCount(const QModelIndex & /*parent*/ = QModelIndex()) const override
{
return m_data.size();
}
QString appName() const
{
return m_appName;
}
void setAppName(const QString &name)
{
if (m_appName != name) {
m_appName = name;
init();
Q_EMIT appNameChanged();
}
}
Q_SIGNALS:
void appNameChanged();
private:
void init();
void loadProfiles();
QString m_appName;
KDirWatch *m_dirWatch = nullptr;
QList<ProfileData> m_data;
};
/*
SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "profilesmodel.h"
#include <QQmlContext>
#include <QQmlEngine>
#include <QQmlExtensionPlugin>
class ProfilesPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri)
{
Q_ASSERT(uri == QByteArrayLiteral("org.kde.plasma.private.profiles"));
qmlRegisterType<ProfilesModel>(uri, 1, 0, "ProfilesModel");
}