2013-06-25 14:07:48 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* KWin - the KDE window manager *
|
|
|
|
* This file is part of the KDE project. *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2013 Antonis Tsiapaliokas <kok3rs@gmail.com> *
|
|
|
|
* *
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>. *
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "model.h"
|
2013-08-07 16:52:45 +00:00
|
|
|
#include "effectconfig.h"
|
|
|
|
|
|
|
|
#include <KDE/KPluginInfo>
|
|
|
|
#include <KDE/KService>
|
|
|
|
#include <KDE/KServiceTypeTrader>
|
|
|
|
#include <KDE/KSharedConfig>
|
|
|
|
#include <KDE/KCModuleProxy>
|
2013-06-25 14:07:48 +00:00
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
2013-07-31 13:00:03 +00:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
2013-06-25 14:07:48 +00:00
|
|
|
#include <QHash>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QtQml>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2013-08-07 16:52:45 +00:00
|
|
|
namespace KWin {
|
|
|
|
namespace Compositing {
|
2013-06-25 14:07:48 +00:00
|
|
|
|
|
|
|
EffectModel::EffectModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent) {
|
|
|
|
|
|
|
|
QHash<int, QByteArray> roleNames;
|
2013-08-07 16:52:45 +00:00
|
|
|
roleNames[NameRole] = "NameRole";
|
|
|
|
roleNames[DescriptionRole] = "DescriptionRole";
|
|
|
|
roleNames[AuthorNameRole] = "AuthorNameRole";
|
|
|
|
roleNames[AuthorEmailRole] = "AuthorEmailRole";
|
|
|
|
roleNames[LicenseRole] = "LicenseRole";
|
|
|
|
roleNames[VersionRole] = "VersionRole";
|
|
|
|
roleNames[CategoryRole] = "CategoryRole";
|
|
|
|
roleNames[ServiceNameRole] = "ServiceNameRole";
|
|
|
|
roleNames[EffectStatusRole] = "EffectStatusRole";
|
2013-06-25 14:07:48 +00:00
|
|
|
setRoleNames(roleNames);
|
|
|
|
loadEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
int EffectModel::rowCount(const QModelIndex &parent) const {
|
|
|
|
return m_effectsList.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant EffectModel::data(const QModelIndex &index, int role) const {
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-08-07 16:52:45 +00:00
|
|
|
EffectData currentEffect = m_effectsList.at(index.row());
|
2013-06-25 14:07:48 +00:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2013-08-07 16:52:45 +00:00
|
|
|
case NameRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).name;
|
2013-08-07 16:52:45 +00:00
|
|
|
case DescriptionRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).description;
|
2013-08-07 16:52:45 +00:00
|
|
|
case AuthorNameRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).authorName;
|
2013-08-07 16:52:45 +00:00
|
|
|
case AuthorEmailRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).authorEmail;
|
2013-08-07 16:52:45 +00:00
|
|
|
case LicenseRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).license;
|
2013-08-07 16:52:45 +00:00
|
|
|
case VersionRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).version;
|
2013-08-07 16:52:45 +00:00
|
|
|
case CategoryRole:
|
2013-06-25 14:07:48 +00:00
|
|
|
return m_effectsList.at(index.row()).category;
|
2013-08-07 16:52:45 +00:00
|
|
|
case ServiceNameRole:
|
|
|
|
return m_effectsList.at(index.row()).serviceName;
|
|
|
|
case EffectStatusRole:
|
|
|
|
return m_effectsList.at(index.row()).effectStatus;
|
2013-06-25 14:07:48 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectModel::loadEffects() {
|
2013-08-07 16:52:45 +00:00
|
|
|
EffectData effect;
|
|
|
|
KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), "Plugins");
|
|
|
|
QDBusMessage messageLoadEffect = QDBusMessage::createMethodCall("org.kde.kwin", "/Effects", "org.kde.kwin.Effects", "loadEffect");
|
|
|
|
QDBusMessage messageUnloadEffect = QDBusMessage::createMethodCall("org.kde.kwin", "/Effects", "org.kde.kwin.Effects", "unloadEffect");
|
2013-06-25 14:07:48 +00:00
|
|
|
|
2013-08-07 16:52:45 +00:00
|
|
|
beginResetModel();
|
2013-07-31 07:39:06 +00:00
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("KWin/Effect");
|
|
|
|
for(KService::Ptr service : offers) {
|
|
|
|
KPluginInfo plugin(service);
|
2013-06-25 14:07:48 +00:00
|
|
|
effect.name = plugin.name();
|
|
|
|
effect.description = plugin.comment();
|
|
|
|
effect.authorName = plugin.author();
|
|
|
|
effect.authorEmail = plugin.email();
|
|
|
|
effect.license = plugin.license();
|
|
|
|
effect.version = plugin.version();
|
|
|
|
effect.category = plugin.category();
|
2013-08-07 16:52:45 +00:00
|
|
|
effect.serviceName = serviceName(effect.name);
|
|
|
|
effect.effectStatus = kwinConfig.readEntry(effect.serviceName + "Enabled", false);
|
|
|
|
|
|
|
|
effect.effectStatus ? messageLoadEffect << effect.serviceName : messageUnloadEffect << effect.serviceName;
|
|
|
|
|
2013-06-25 14:07:48 +00:00
|
|
|
m_effectsList << effect;
|
|
|
|
}
|
2013-08-07 16:52:45 +00:00
|
|
|
qSort(m_effectsList.begin(), m_effectsList.end(), [](const EffectData &a, const EffectData &b) {
|
|
|
|
return a.category < b.category;
|
|
|
|
});
|
2013-07-31 07:39:06 +00:00
|
|
|
|
2013-08-07 16:52:45 +00:00
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
QDBusConnection::sessionBus().registerObject("/Effects", this);
|
|
|
|
QDBusConnection::sessionBus().send(messageLoadEffect);
|
|
|
|
QDBusConnection::sessionBus().send(messageUnloadEffect);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString EffectModel::serviceName(const QString &effectName) {
|
|
|
|
//The effect name is something like "Show Fps" and
|
|
|
|
//we want something like "showfps"
|
|
|
|
return "kwin4_effect_" + effectName.toLower().replace(" ", "");
|
2013-06-25 14:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EffectView::EffectView(QWindow *parent)
|
|
|
|
: QQuickView(parent)
|
|
|
|
{
|
|
|
|
qmlRegisterType<EffectModel>("org.kde.kwin.kwincompositing", 1, 0, "EffectModel");
|
2013-08-01 16:20:37 +00:00
|
|
|
qmlRegisterType<EffectConfig>("org.kde.kwin.kwincompositing", 1, 0, "EffectConfig");
|
|
|
|
|
2013-06-25 14:07:48 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectView::init() {
|
|
|
|
EffectModel *model = new EffectModel();
|
|
|
|
QString mainFile = QStandardPaths::locate(QStandardPaths::DataLocation, "qml/main.qml", QStandardPaths::LocateFile);
|
|
|
|
setResizeMode(QQuickView::SizeRootObjectToView);
|
|
|
|
rootContext()->setContextProperty("engineObject", this);
|
|
|
|
setSource(QUrl(mainFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectView::effectStatus(const QString &effectName, bool status) {
|
|
|
|
m_effectStatus[effectName] = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectView::syncConfig() {
|
2013-07-29 15:58:45 +00:00
|
|
|
auto it = m_effectStatus.begin();
|
2013-07-31 13:00:03 +00:00
|
|
|
KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), "Plugins");
|
|
|
|
QHash<QString, bool> effectsChanged;
|
2013-06-25 14:07:48 +00:00
|
|
|
|
2013-07-29 15:58:45 +00:00
|
|
|
while (it != m_effectStatus.end()) {
|
2013-06-25 14:07:48 +00:00
|
|
|
QVariant boolToString(it.value());
|
2013-07-31 07:39:06 +00:00
|
|
|
QString effectName = it.key().toLower();
|
|
|
|
QString effectEntry = effectName.replace(" ", "");
|
2013-07-31 13:00:03 +00:00
|
|
|
kwinConfig.writeEntry("kwin4_effect_" + effectEntry + "Enabled", boolToString.toString());
|
2013-06-25 14:07:48 +00:00
|
|
|
it++;
|
2013-07-31 13:00:03 +00:00
|
|
|
effectsChanged["kwin4_effect_" + effectEntry] = boolToString.toBool();
|
2013-06-25 14:07:48 +00:00
|
|
|
}
|
2013-07-31 13:00:03 +00:00
|
|
|
kwinConfig.sync();
|
2013-06-25 14:07:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 11:00:43 +00:00
|
|
|
QString EffectView::findImage(const QString &imagePath, int size) {
|
|
|
|
const QString relativePath("icons/oxygen/" + QString::number(size) + 'x' + QString::number(size) + '/' + imagePath);
|
|
|
|
const QString fullImagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, relativePath, QStandardPaths::LocateFile);
|
|
|
|
return fullImagePath;
|
|
|
|
}
|
2013-08-07 16:52:45 +00:00
|
|
|
|
|
|
|
}//end namespace Compositing
|
|
|
|
}//end namespace KWin
|