add part of systembus feature

This commit is contained in:
zhangzihao 2021-01-06 08:32:17 +08:00
parent bcc226a650
commit 3e8b64c840
7 changed files with 341 additions and 1 deletions

View File

@ -1,7 +1,8 @@
TEMPLATE = subdirs TEMPLATE = subdirs
SUBDIRS += $$PWD/libchinese-segmentation \ SUBDIRS += $$PWD/libchinese-segmentation \
$$PWD/libsearch \ $$PWD/libsearch \
$$PWD/src $$PWD/src \
$$PWD/ukuisearch-systemdbus
# The following define makes your compiler emit warnings if you use # The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings # any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the # depend on your compiler). Please consult the documentation of the

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <!-- -*- XML -*- -->
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- Only root can own the service -->
<policy user="root">
<allow own="com.ukui.search.qt.systemdbus"/>
<allow send_interface="com.ukui.search.interface"/>
</policy>
<!-- Allow anyone to invoke methods on the interfaces -->
<policy context="default">
<allow send_destination="com.ukui.search.qt.systemdbus"
send_interface="com.ukui.search.interface"/>
<allow send_destination="com.ukui.search.qt.systemdbus"
send_interface="org.freedesktop.DBus.Introspectable"/>
<allow send_destination="com.ukui.search.qt.systemdbus"
send_interface="org.freedesktop.DBus.Properties"/>
</policy>
</busconfig>

View File

@ -0,0 +1,4 @@
[D-BUS Service]
Name=com.ukui.search.qt.systemdbus
Exec=/usr/bin/ukuiSearchSystemDBus
User=root

View File

@ -0,0 +1,47 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>
#include "sysdbusregister.h"
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
app.setOrganizationName("Kylin Team");
app.setApplicationName("ukui-search-service");
QDBusConnection systemBus = QDBusConnection::systemBus();
if (!systemBus.registerService("com.ukui.search.qt.systemdbus")){
qCritical() << "QDbus register service failed reason:" << systemBus.lastError();
exit(1);
}
if (!systemBus.registerObject("/", new SysdbusRegister(), QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)){
qCritical() << "QDbus register object failed reason:" << systemBus.lastError();
exit(2);
}
return app.exec();
}

View File

@ -0,0 +1,161 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "sysdbusregister.h"
#include <QDebug>
#include <QSharedPointer>
#include <QRegExp>
#include <stdlib.h>
SysdbusRegister::SysdbusRegister()
{
// mHibernateFile = "/etc/systemd/sleep.conf";
// mHibernateSet = new QSettings(mHibernateFile, QSettings::IniFormat, this);
// mHibernateSet->setIniCodec("UTF-8");
}
SysdbusRegister::~SysdbusRegister()
{
}
void SysdbusRegister::exitService() {
qApp->exit(0);
}
//QString SysdbusRegister::GetComputerInfo() {
// QByteArray ba;
// FILE * fp = NULL;
// char cmd[128];
// char buf[1024];
// sprintf(cmd, "dmidecode -t system");
// if ((fp = popen(cmd, "r")) != NULL){
// rewind(fp);
// while (!feof(fp)) {
// fgets(buf, sizeof (buf), fp);
// ba.append(buf);
// }
// pclose(fp);
// fp = NULL;
// }
// return QString(ba);
//}
QString SysdbusRegister::setInotifyMaxUserWatches5()
{
// QString cmd;
//// cmd = QString("echo 9999999 | sudo tee -a /proc/sys/fs/inotify/max_user_watches");
// cmd = QString("reboot");
// QProcess p;
// p.start(cmd);
// return 123;
QByteArray ba;
FILE * fp = NULL;
char cmd[128];
char buf[1024];
sprintf(cmd, "echo 9999999 | sudo tee -a /proc/sys/fs/inotify/max_user_watches");
if ((fp = popen(cmd, "r")) != NULL){
rewind(fp);
while (!feof(fp)) {
fgets(buf, sizeof (buf), fp);
ba.append(buf);
}
pclose(fp);
fp = NULL;
}
return QString(ba);
}
////获取免密登录状态
//QString SysdbusRegister::getNoPwdLoginStatus(){
// QByteArray ba;
// FILE * fp = NULL;
// char cmd[128];
// char buf[1024];
// sprintf(cmd, "cat /etc/group |grep nopasswdlogin");
// if ((fp = popen(cmd, "r")) != NULL){
// rewind(fp);
// fgets(buf, sizeof (buf), fp);
// ba.append(buf);
// pclose(fp);
// fp = NULL;
// }else{
// qDebug()<<"popen文件打开失败"<<endl;
// }
// return QString(ba);
//}
////设置免密登录状态
//void SysdbusRegister::setNoPwdLoginStatus() {
//// QString cmd;
//// if(true == status){
//// cmd = QString("gpasswd -a %1 nopasswdlogin").arg(username);
//// } else{
//// cmd = QString("gpasswd -d %1 nopasswdlogin").arg(username);
//// }
//// QProcess::execute(cmd);
// QString cmd;
// cmd = QString("sysctl -w fs.inotify.max_user_watches=\"1\"");
// QProcess::execute(cmd);
//}
//// 设置自动登录状态
//void SysdbusRegister::setAutoLoginStatus(QString username) {
// QString filename = "/etc/lightdm/lightdm.conf";
// QSharedPointer<QSettings> autoSettings = QSharedPointer<QSettings>(new QSettings(filename, QSettings::IniFormat));
// autoSettings->beginGroup("SeatDefaults");
// autoSettings->setValue("autologin-user", username);
// autoSettings->endGroup();
// autoSettings->sync();
//}
//QString SysdbusRegister::getSuspendThenHibernate() {
// mHibernateSet->beginGroup("Sleep");
// QString time = mHibernateSet->value("HibernateDelaySec").toString();
// mHibernateSet->endGroup();
// mHibernateSet->sync();
// return time;
//}
//void SysdbusRegister::setSuspendThenHibernate(QString time) {
// mHibernateSet->beginGroup("Sleep");
// mHibernateSet->setValue("HibernateDelaySec", time);
// mHibernateSet->endGroup();
// mHibernateSet->sync();
//}
//void SysdbusRegister::setPasswdAging(int days, QString username) {
// QString cmd;
// cmd = QString("chage -M %1 %2").arg(days).arg(username);
//// cmd = QString("reboot");
// QProcess::execute(cmd);
//}

View File

@ -0,0 +1,74 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef SYSDBUSREGISTER_H
#define SYSDBUSREGISTER_H
#include <QObject>
#include <QCoreApplication>
#include <QProcess>
#include <QFile>
#include <QSettings>
class SysdbusRegister : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.ukui.search.interface")
public:
explicit SysdbusRegister();
~SysdbusRegister();
//private:
// QString mHibernateFile;
// QSettings *mHibernateSet;
//signals:
// Q_SCRIPTABLE void nameChanged(QString);
// Q_SCRIPTABLE void computerinfo(QString);
public slots:
Q_SCRIPTABLE void exitService();
// Q_SCRIPTABLE QString GetComputerInfo();
Q_SCRIPTABLE QString setInotifyMaxUserWatches5();
// // 设置免密登录状态
// Q_SCRIPTABLE void setNoPwdLoginStatus();
// // 获取免密登录状态
// Q_SCRIPTABLE QString getNoPwdLoginStatus();
// // 设置自动登录状态
// Q_SCRIPTABLE void setAutoLoginStatus(QString username);
// // 获取挂起到休眠时间
// Q_SCRIPTABLE QString getSuspendThenHibernate();
// // 设置挂起到休眠时间
// Q_SCRIPTABLE void setSuspendThenHibernate(QString time);
// // 设置密码时效
// void setPasswdAging(int days, QString username);
};
#endif // SYSDBUSREGISTER_H

View File

@ -0,0 +1,31 @@
QT += core dbus
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ukuiSearchSystemDBus
TEMPLATE = app
CONFIG += console c++11 link_pkgconfig
CONFIG -= app_bundle
DESTDIR = .
INCLUDEPATH += .
inst1.files += conf/com.ukui.search.qt.systemdbus.service
inst1.path = /usr/share/dbus-1/system-services/
inst2.files += conf/com.ukui.search.qt.systemdbus.conf
inst2.path = /etc/dbus-1/system.d/
target.source += $$TARGET
target.path = /usr/bin
INSTALLS += \
target \
inst1 \
inst2 \
HEADERS += \
sysdbusregister.h
SOURCES += \
main.cpp \
sysdbusregister.cpp