2020-07-30 17:59:34 +08:00
|
|
|
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
|
|
|
|
|
* -*- coding: utf-8 -*-
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2020 KylinSoft Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* 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 3 of the License, or
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
2020-03-09 15:04:02 +08:00
|
|
|
#include "plugin-manager.h"
|
2020-08-11 21:06:19 +08:00
|
|
|
#include <QStandardPaths>
|
2020-03-04 17:19:11 +08:00
|
|
|
#include "global.h"
|
2020-03-09 15:04:02 +08:00
|
|
|
#include "clib-syslog.h"
|
2020-03-12 00:39:22 -07:00
|
|
|
#include "plugin-info.h"
|
2020-03-04 17:19:11 +08:00
|
|
|
|
2020-03-03 17:30:22 +08:00
|
|
|
#include <glib.h>
|
|
|
|
|
#include <stdio.h>
|
2020-03-04 17:19:11 +08:00
|
|
|
#include <fcntl.h>
|
2020-03-03 17:30:22 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2020-03-04 17:19:11 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <gio/gio.h>
|
2020-03-03 17:30:22 +08:00
|
|
|
#include <sys/stat.h>
|
2020-03-04 17:19:11 +08:00
|
|
|
#include <sys/types.h>
|
2020-03-03 17:30:22 +08:00
|
|
|
|
2020-03-04 17:19:11 +08:00
|
|
|
#include <QDebug>
|
2020-03-11 23:13:24 -07:00
|
|
|
#include <QDBusError>
|
2020-03-12 00:39:22 -07:00
|
|
|
#include <QDBusConnectionInterface>
|
2020-03-03 11:21:34 +08:00
|
|
|
|
2020-03-15 19:51:57 -07:00
|
|
|
QList<PluginInfo*>* PluginManager::mPlugin = nullptr;
|
|
|
|
|
PluginManager* PluginManager::mPluginManager = nullptr;
|
2020-03-04 17:19:11 +08:00
|
|
|
|
2020-03-11 23:13:24 -07:00
|
|
|
static bool is_schema (QString& schema);
|
|
|
|
|
static bool register_manager(PluginManager& pm);
|
2020-08-31 16:18:25 +08:00
|
|
|
bool sortPluginByPriority(PluginInfo* a,PluginInfo* b);
|
2020-03-04 09:41:40 +08:00
|
|
|
|
2020-03-09 15:04:02 +08:00
|
|
|
PluginManager::PluginManager()
|
2020-03-03 11:21:34 +08:00
|
|
|
{
|
2020-03-15 19:51:57 -07:00
|
|
|
if (nullptr == mPlugin) mPlugin = new QList<PluginInfo*>();
|
2020-03-03 11:21:34 +08:00
|
|
|
}
|
2020-03-03 17:30:22 +08:00
|
|
|
|
2020-03-09 15:04:02 +08:00
|
|
|
PluginManager::~PluginManager()
|
2020-03-03 17:30:22 +08:00
|
|
|
{
|
2020-03-14 22:14:16 -07:00
|
|
|
managerStop();
|
2020-03-15 19:51:57 -07:00
|
|
|
delete mPlugin;
|
|
|
|
|
mPlugin = nullptr;
|
2020-03-03 17:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:04:02 +08:00
|
|
|
PluginManager* PluginManager::getInstance()
|
2020-03-03 17:30:22 +08:00
|
|
|
{
|
2020-03-09 15:04:02 +08:00
|
|
|
if (nullptr == mPluginManager) {
|
2020-03-04 17:19:11 +08:00
|
|
|
CT_SYSLOG(LOG_DEBUG, "ukui settings manager will be created!")
|
2020-03-09 15:04:02 +08:00
|
|
|
mPluginManager = new PluginManager;
|
2020-03-11 23:13:24 -07:00
|
|
|
if (!register_manager(*mPluginManager)) {
|
2020-03-16 00:44:34 -07:00
|
|
|
CT_SYSLOG(LOG_ERR, "register manager failed!");
|
2020-03-11 23:13:24 -07:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-03-03 17:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:04:02 +08:00
|
|
|
return mPluginManager;
|
2020-03-03 17:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:19:45 +08:00
|
|
|
bool PluginManager::managerStart()
|
2020-03-03 17:30:22 +08:00
|
|
|
{
|
2020-03-14 22:14:16 -07:00
|
|
|
GDir* dir = NULL;
|
|
|
|
|
QString schema;
|
|
|
|
|
GError* error = NULL;
|
|
|
|
|
const char* name = NULL;
|
|
|
|
|
|
2020-08-11 21:06:19 +08:00
|
|
|
qDebug("Starting settings manager");
|
|
|
|
|
|
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
|
|
|
|
|
QString libpath = qApp->libraryPaths().at(0);
|
|
|
|
|
QString path = libpath.mid(0,libpath.lastIndexOf('/')-4)+"/ukui-settings-daemon";
|
2020-03-04 17:19:11 +08:00
|
|
|
|
2020-03-14 22:14:16 -07:00
|
|
|
dir = g_dir_open ((char*)path.toUtf8().data(), 0, &error);
|
|
|
|
|
if (NULL == dir) {
|
|
|
|
|
CT_SYSLOG(LOG_ERR, "%s", error->message);
|
|
|
|
|
g_error_free(error);
|
|
|
|
|
error = nullptr;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ((name = g_dir_read_name(dir))) {
|
|
|
|
|
char* filename = NULL;
|
|
|
|
|
if (!g_str_has_suffix(name, PLUGIN_EXT)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
filename = g_build_filename((char*)path.toUtf8().data(), name, NULL);
|
|
|
|
|
if (g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
|
|
|
|
|
QString ftmp(filename);
|
|
|
|
|
PluginInfo* info = new PluginInfo(ftmp);
|
|
|
|
|
if (info == NULL) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mPlugin->contains(info)) {
|
|
|
|
|
CT_SYSLOG(LOG_DEBUG, "The list has contain this plugin, '%s'", ftmp.toUtf8().data());
|
|
|
|
|
if (info != NULL) delete info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check plugin's schema
|
|
|
|
|
schema = QString("%1.plugins.%2").arg(DEFAULT_SETTINGS_PREFIX).arg(info->getPluginLocation().toUtf8().data());
|
|
|
|
|
if (is_schema (schema)) {
|
|
|
|
|
CT_SYSLOG(LOG_DEBUG, "right schema '%s'", schema.toUtf8().data());
|
|
|
|
|
info->setPluginSchema(schema);
|
|
|
|
|
mPlugin->insert(0, info);
|
|
|
|
|
} else {
|
|
|
|
|
CT_SYSLOG(LOG_ERR, "Ignoring unknown schema '%s'", schema.toUtf8().data());
|
|
|
|
|
if (info != NULL) delete info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_free(filename);
|
|
|
|
|
}
|
|
|
|
|
g_dir_close(dir);
|
|
|
|
|
|
2020-08-31 16:18:25 +08:00
|
|
|
//sort plugin
|
|
|
|
|
qSort(mPlugin->begin(),mPlugin->end(),sortPluginByPriority);
|
2020-03-14 22:14:16 -07:00
|
|
|
|
|
|
|
|
CT_SYSLOG(LOG_DEBUG, "Now Activity plugins ...");
|
|
|
|
|
for (int i = 0; i < mPlugin->size(); ++i) {
|
|
|
|
|
PluginInfo* info = mPlugin->at(i);
|
|
|
|
|
CT_SYSLOG(LOG_DEBUG, "start activity plugin: %s ...", info->getPluginName().toUtf8().data());
|
|
|
|
|
info->pluginActivate();
|
|
|
|
|
}
|
2020-03-17 23:12:52 -07:00
|
|
|
CT_SYSLOG(LOG_DEBUG, "All plugins has been activited!");
|
2020-03-03 17:30:22 +08:00
|
|
|
|
2020-03-09 15:19:45 +08:00
|
|
|
return true;
|
2020-03-03 17:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:04:02 +08:00
|
|
|
void PluginManager::managerStop()
|
2020-03-04 09:41:40 +08:00
|
|
|
{
|
2020-03-04 17:19:11 +08:00
|
|
|
CT_SYSLOG(LOG_DEBUG, "Stopping settings manager");
|
2020-03-14 22:14:16 -07:00
|
|
|
while (!mPlugin->isEmpty()) {
|
|
|
|
|
PluginInfo* plugin = mPlugin->takeFirst();
|
|
|
|
|
plugin->pluginDeactivate();
|
|
|
|
|
delete plugin;
|
|
|
|
|
}
|
2020-03-12 00:39:22 -07:00
|
|
|
|
2020-03-15 19:51:57 -07:00
|
|
|
// exit main event loop
|
|
|
|
|
QCoreApplication::exit();
|
2020-03-04 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 15:19:45 +08:00
|
|
|
bool PluginManager::managerAwake()
|
2020-03-04 09:41:40 +08:00
|
|
|
{
|
2020-03-04 18:03:50 +08:00
|
|
|
CT_SYSLOG(LOG_DEBUG, "Awake called")
|
2020-03-09 23:38:34 -07:00
|
|
|
return managerStart();
|
2020-03-04 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-04 17:56:39 +08:00
|
|
|
static bool is_item_in_schema (const char* const* items, QString& item)
|
2020-03-04 17:19:11 +08:00
|
|
|
{
|
|
|
|
|
while (*items) {
|
2020-03-04 17:56:39 +08:00
|
|
|
if (g_strcmp0 (*items++, item.toLatin1().data()) == 0) return true;
|
2020-03-04 17:19:11 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 17:56:39 +08:00
|
|
|
bool is_schema (QString& schema)
|
2020-03-04 17:19:11 +08:00
|
|
|
{
|
|
|
|
|
return is_item_in_schema (g_settings_list_schemas(), schema);
|
2020-03-03 17:30:22 +08:00
|
|
|
}
|
2020-03-11 23:13:24 -07:00
|
|
|
|
|
|
|
|
static bool register_manager(PluginManager& pm)
|
|
|
|
|
{
|
2020-03-12 00:39:22 -07:00
|
|
|
QString ukuiDaemonBusName = UKUI_SETTINGS_DAEMON_DBUS_NAME;
|
|
|
|
|
|
|
|
|
|
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(ukuiDaemonBusName)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 23:13:24 -07:00
|
|
|
QDBusConnection bus = QDBusConnection::sessionBus();
|
|
|
|
|
if (!bus.registerService(UKUI_SETTINGS_DAEMON_DBUS_NAME)) {
|
|
|
|
|
CT_SYSLOG(LOG_ERR, "error getting system bus: '%s'", bus.lastError().message().toUtf8().data());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 00:39:22 -07:00
|
|
|
if (!bus.registerObject(UKUI_SETTINGS_DAEMON_DBUS_PATH, (QObject*)&pm, QDBusConnection::ExportAllSlots)) {
|
2020-03-11 23:13:24 -07:00
|
|
|
CT_SYSLOG(LOG_ERR, "regist settings manager error: '%s'", bus.lastError().message().toUtf8().data());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CT_SYSLOG(LOG_DEBUG, "regist settings manager successful!");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-31 16:18:25 +08:00
|
|
|
|
|
|
|
|
bool sortPluginByPriority(PluginInfo* a,PluginInfo* b)
|
|
|
|
|
{
|
|
|
|
|
return a->getPluginPriority() < b->getPluginPriority();
|
|
|
|
|
}
|