Commit e631d858 authored by Kazord's avatar Kazord

Background & applisting modules

parent 66233e0b
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PLUGIN "AppHandler")
set(
SRC
plugin.cpp
appinfo.cpp
apphandler.cpp
)
set(CMAKE_AUTOMOC ON)
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE ARCH_TRIPLET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
install(FILES qmldir DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
#include <QString>
#include <QStringList>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDebug>
#include <QQmlListProperty>
#include "apphandler.h"
#include "appinfo.h"
#define APP_SYS_PATH "/usr/share/applications/"
#define APP_USR_PATH "/home/phablet/.local/share/applications/"
AppHandler::AppHandler() {
loadAppsInfo();
}
QList<AppInfo*> AppHandler::getApps()
{
return _appinfos;
}
QQmlListProperty<AppInfo> AppHandler::appsinfo()
{
return QQmlListProperty<AppInfo>(this, 0, &AppHandler::count_appinfo, &AppHandler::at_appinfo);
}
void AppHandler::loadAppsInfo()
{
loadAppsFromDir(APP_SYS_PATH);
loadAppsFromDir(APP_USR_PATH);
}
void AppHandler::loadAppsFromDir(const QString& path)
{
QDir dir(path);
QStringList nameFilters;
nameFilters << "*.desktop";
QStringList fileList = dir.entryList(nameFilters, QDir::Files);
foreach (const QString &fileName, fileList) {
QFile file(dir.filePath(fileName));
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream filestream(&file);
filestream.setCodec("UTF-8");
_appinfos.append(new AppInfo(filestream.readAll()));
}
qDebug() << _appinfos.size() << " desktop file read from " << path;
}
void AppHandler::append_appinfo(QQmlListProperty<AppInfo> *list, AppInfo *appinfo)
{
AppHandler *appinfoBoard = qobject_cast<AppHandler*>(list->object);
if(appinfo)
appinfoBoard->_appinfos.append(appinfo);
}
AppInfo* AppHandler::at_appinfo(QQmlListProperty<AppInfo> *list, int index) {
AppHandler *apphandler = qobject_cast<AppHandler*>(list->object);
return apphandler->_appinfos.at(index);
}
int AppHandler::count_appinfo(QQmlListProperty<AppInfo> *list) {
AppHandler *apphandler = qobject_cast<AppHandler*>(list->object);
return apphandler->_appinfos.size();
}
#ifndef APPHANDLER_H
#define APPHANDLER_H
#include <QObject>
#include <QString>
#include <QQmlListProperty>
#include "appinfo.h"
class AppHandler: public QObject {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<AppInfo> appsinfo READ appsinfo)
public:
AppHandler();
~AppHandler() = default;
QList<AppInfo*> getApps();
QQmlListProperty<AppInfo> appsinfo();
public slots:
signals:
protected:
QList<AppInfo*> _appinfos;
private:
void loadAppsFromDir(const QString& path);
void loadAppsInfo();
static void append_appinfo(QQmlListProperty<AppInfo> *list, AppInfo *appinfo);
static AppInfo* at_appinfo(QQmlListProperty<AppInfo> *list, int at);
static int count_appinfo(QQmlListProperty<AppInfo> *list);
};
#endif
#include "appinfo.h"
#include <QDebug>
AppInfo::AppInfo(const QString& infos)
{
foreach (const QString& line , infos.split("\n")) {
if(line != "") {
QStringList keyvalue = line.split("=");
_appinfo.insert(keyvalue.first(),(keyvalue.size() > 1) ? keyvalue.at(1):"");
}
}
//qDebug() << "keys:" <<_appinfo.keys() << "values" << _appinfo.values();
}
QString AppInfo::getProp(const QString& key)
{
return _appinfo.value(key);
}
#ifndef APPINFO_H
#define APPINFO_H
#include <QObject>
#include <QString>
#include <QMap>
class AppInfo: public QObject {
Q_OBJECT
public:
~AppInfo() = default;
AppInfo(const QString& infos = "");
Q_INVOKABLE QString getProp(const QString& key);
protected:
QMap<QString, QString> _appinfo;
};
#endif
#include <QtQml>
#include <QtQml/QQmlContext>
#include "plugin.h"
#include "apphandler.h"
#include "appinfo.h"
void AppHandlerPlugin::registerTypes(const char *uri) {
//@uri AppHandler
qmlRegisterSingletonType<AppHandler>(uri, 1, 0, "AppHandler", [](QQmlEngine*, QJSEngine*) -> QObject* { return new AppHandler; });
qmlRegisterType<AppInfo>(uri, 1, 0, "AppInfo");
}
#ifndef APPHANDLERPLUGIN_H
#define APPHANDLERPLUGIN_H
#include <QQmlExtensionPlugin>
class AppHandlerPlugin : public QQmlExtensionPlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri);
};
#endif
module AppHandler
plugin AppHandler
add_subdirectory(Terminalaccess)
add_subdirectory(MySettings)
add_subdirectory(AppHandler)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PLUGIN "MySettings")
set(
SRC
plugin.cpp
mysettings.cpp
accountsservice.cpp
)
set(CMAKE_AUTOMOC ON)
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
execute_process(
COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
OUTPUT_VARIABLE ARCH_TRIPLET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
install(FILES qmldir DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)
/*
* This file is part of system-settings
*
* Copyright (C) 2013 Canonical Ltd.
*
* Contact: Iain Lane <iain.lane@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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 "accountsservice.h"
#include <QDBusReply>
#include <QDebug>
#include <unistd.h>
#include <sys/types.h>
#define AS_SERVICE "org.freedesktop.Accounts"
#define AS_PATH "/org/freedesktop/Accounts"
#define AS_IFACE "org.freedesktop.Accounts"
AccountsService::AccountsService(QObject *parent)
: QObject(parent),
m_systemBusConnection(QDBusConnection::systemBus()),
m_serviceWatcher(AS_SERVICE,
m_systemBusConnection,
QDBusServiceWatcher::WatchForOwnerChange),
m_accountsserviceIface(AS_SERVICE,
AS_PATH,
AS_IFACE,
m_systemBusConnection)
{
connect (&m_serviceWatcher,
SIGNAL (serviceOwnerChanged (QString, QString, QString)),
this,
SLOT (slotNameOwnerChanged (QString, QString, QString)));
setUpInterface();
}
void AccountsService::slotChanged(QString interface,
QVariantMap changed_properties,
QStringList invalidated_properties)
{
Q_FOREACH (const QString k, changed_properties.keys())
Q_EMIT propertyChanged(interface, k);
Q_FOREACH (const QString prop, invalidated_properties)
Q_EMIT propertyChanged(interface, prop);
}
void AccountsService::slotNameOwnerChanged(QString name,
QString oldOwner,
QString newOwner)
{
Q_UNUSED (oldOwner);
Q_UNUSED (newOwner);
if (name != "org.freedesktop.Accounts")
return;
setUpInterface();
Q_EMIT (nameOwnerChanged());
}
void AccountsService::setUpInterface()
{
QDBusReply<QDBusObjectPath> qObjectPath = m_accountsserviceIface.call(
"FindUserById", qlonglong(getuid()));
if (qObjectPath.isValid()) {
m_objectPath = qObjectPath.value().path();
m_accountsserviceIface.connection().connect(
m_accountsserviceIface.service(),
m_objectPath,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
this,
SLOT(slotChanged(QString, QVariantMap, QStringList)));
m_accountsserviceIface.connection().connect(
m_accountsserviceIface.service(),
m_objectPath,
"org.freedesktop.Accounts.User",
"Changed",
this,
SIGNAL (changed ()));
}
}
QVariant AccountsService::getUserProperty(const QString &interface,
const QString &property)
{
QDBusInterface iface (
"org.freedesktop.Accounts",
m_objectPath,
"org.freedesktop.DBus.Properties",
m_systemBusConnection,
this);
if (iface.isValid()) {
QDBusReply<QDBusVariant> answer = iface.call(
"Get",
interface,
property);
if (answer.isValid()) {
return answer.value().variant();
}
}
return QVariant();
}
bool AccountsService::setUserProperty(const QString &interface,
const QString &property,
const QVariant &value)
{
QDBusInterface iface (
"org.freedesktop.Accounts",
m_objectPath,
"org.freedesktop.DBus.Properties",
m_systemBusConnection,
this);
// The value needs to be carefully wrapped
QDBusMessage msg = iface.call("Set",
interface,
property,
QVariant::fromValue(QDBusVariant(value)));
if (msg.type() == QDBusMessage::ErrorMessage) {
qWarning() << "Could not set AccountsService property" << property << "on interface" << interface << "for object" << m_objectPath << "to" << value << ":" << msg.errorMessage();
}
return msg.type() == QDBusMessage::ReplyMessage;
}
bool AccountsService::customSetUserProperty(const QString &method,
const QVariant &value)
{
QDBusInterface iface ("org.freedesktop.Accounts",
m_objectPath,
"org.freedesktop.Accounts.User",
m_systemBusConnection,
this);
QDBusMessage msg = iface.call(method, value);
if (msg.type() == QDBusMessage::ErrorMessage) {
qWarning() << "Could not call AccountsService method" << method << "for object" << m_objectPath << "with argument" << value << ":" << msg.errorMessage();
}
return msg.type() == QDBusMessage::ReplyMessage;
}
/*
* This file is part of system-settings
*
* Copyright (C) 2013 Canonical Ltd.
*
* Contact: Iain Lane <iain.lane@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
*/
#ifndef ACCOUNTSSERVICE_H
#define ACCOUNTSSERVICE_H
#include <QDBusServiceWatcher>
#include <QStringList>
#include <QtDBus/QDBusInterface>
class AccountsService : public QObject
{
Q_OBJECT
public:
explicit AccountsService (QObject *parent = 0);
QString getProperty (QString property);
QVariant getUserProperty(const QString &interface,
const QString &property);
bool setUserProperty(const QString &interface,
const QString &property,
const QVariant &value);
bool customSetUserProperty(const QString &method,
const QVariant &value);
public Q_SLOTS:
void slotChanged(QString, QVariantMap, QStringList);
void slotNameOwnerChanged(QString, QString, QString);
Q_SIGNALS:
void propertyChanged(QString interface, QString property);
void changed();
void nameOwnerChanged();
private:
QDBusConnection m_systemBusConnection;
QDBusServiceWatcher m_serviceWatcher;
QDBusInterface m_accountsserviceIface;
QString m_objectPath;
void setUpInterface();
};
#endif // ACCOUNTSSERVICE_H
#include <QVariant>
#include <QString>
#include <QUrl>
#include <QFile>
#include "mysettings.h"
MySettings::MySettings() : _acc() {
}
QString MySettings::getBackgroundFile()
{
QVariant answer = _acc.getUserProperty(
"org.freedesktop.Accounts.User",
"BackgroundFile");
QString filename = answer.toString();
if (filename.isEmpty() || !QFile::exists(filename))
return "";//TODO return something more usefull
else
return filename;
}
void MySettings::setBackgroundFile(const QString &filename)
{
QUrl backgroundFile(filename);
if (!backgroundFile.isLocalFile())
return;
_acc.customSetUserProperty("SetBackgroundFile",
backgroundFile.path());
emit(backgroundFileChanged());
}
#ifndef MYSETTINGS_H
#define MYSETTINGS_H
#include <QString>
#include "accountsservice.h"
class MySettings: public QObject {
Q_OBJECT
public:
MySettings();
~MySettings() = default;
Q_INVOKABLE QString getBackgroundFile();
public slots:
void setBackgroundFile(const QString &filename);
signals:
void backgroundFileChanged();
protected:
AccountsService _acc;
};
#endif
#include <QtQml>
#include <QtQml/QQmlContext>
#include "plugin.h"
#include "mysettings.h"
void MySettingsPlugin::registerTypes(const char *uri) {
//@uri MySettings
qmlRegisterSingletonType<MySettings>(uri, 1, 0, "MySettings", [](QQmlEngine*, QJSEngine*) -> QObject* { return new MySettings; });
}
#ifndef MYSETTINGSPLUGIN_H
#define MYSETTINGSPLUGIN_H
#include <QQmlExtensionPlugin>
class MySettingsPlugin : public QQmlExtensionPlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri);
};
#endif
module MySettings
plugin MySettings
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment