File: sharing-manager.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,412 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (224 lines) | stat: -rw-r--r-- 7,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2021 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/>.
 */

#include "sharing-manager.h"
#include "clib-syslog.h"


#define SHATINGS_SCHEMA     "org.ukui.SettingsDaemon.plugins.sharing"
#define KEY_SERVICE_NAME    "service-name"

#define USD_DBUS_NAME           "org.ukui.SettingsDaemon"
#define USD_DBUS_PATH           "/org/ukui/SettingsDaemon"
#define USD_DBUS_BASE_INTERFACE "org.ukui.SettingsDaemon"

#define RYGEL_BUS_NAME        "org.ukui.Rygel1"
#define RYGEL_OBJECT_PATH     "/org/ukui/Rygel1"
#define RYGEL_INTERFACE_NAME  "org.ukui.Rygel1"

#define SYSTEM_DBUS_NAME    "org.freedesktop.systemd1"
#define SYSTEM_DBUS_PATH    "/org/freedesktop/systemd1"
#define SYSTEM_DBUS_INTER   "org.freedesktop.systemd1.Manager"

#define USD_SHARING_DBUS_NAME USD_DBUS_NAME ".Sharing"
#define USD_SHARING_DBUS_PATH USD_DBUS_PATH "/Sharing"

SharingManager* SharingManager::mSharingManager = nullptr;

SharingManager::SharingManager()
{
    mDbus = new sharingDbus(this);
    new SharingAdaptor(mDbus);
    QDBusConnection sessionBus = QDBusConnection::sessionBus();
    if(sessionBus.registerService(USD_SHARING_DBUS_NAME)){
        sessionBus.registerObject(USD_SHARING_DBUS_PATH,
                                  mDbus,
                                  QDBusConnection::ExportAllContents);
    }
    connect(mDbus, &sharingDbus::serviceChange,
            this, &SharingManager::sharingManagerServiceChange);
}

void SharingManager::startKrd()
{
    QString confFile = QDir::homePath()+"/.config/kylin-remote-desktop/krd.ini";
    QDBusInterface krdIfc("com.kylin.RemoteDesktop",
                          "/com/kylin/RemoteDesktop",
                          "com.kylin.RemoteDesktop",
                          QDBusConnection::sessionBus());

    if (!krdIfc.isValid() || !QFile::exists(confFile)) return;

    QSettings krdCfg(confFile, QSettings::IniFormat);

    int isOpen       = krdCfg.value("mIsOpen").toInt();
    int isNeedPwd    = krdCfg.value("mNeedPwd").toInt();

    QString passwd   = krdCfg.value("passwd").toString();
    QString protocol = krdCfg.value("protocol").toString();
    QString output   = krdCfg.value("output", "eDP-1").toString();

    if (!isOpen) return;

    if (protocol.isEmpty() || !protocol.compare("vnc")) {
        krdIfc.call("Start", output);
    } else {
        krdIfc.call("StartRDP", output);
    }

    krdIfc.setProperty("AuthMethod", isNeedPwd);

    if (isNeedPwd) {
        krdIfc.call("SetPassword", passwd);
    }
}

SharingManager::~SharingManager()
{
    if(mSharingManager) {
        delete mSharingManager;
        mSharingManager = nullptr;
    }
}

SharingManager  *SharingManager::SharingManagerNew()
{
    if(nullptr == mSharingManager)
        mSharingManager = new SharingManager();
    return mSharingManager;
}

bool SharingManager::sharingManagerStartService(QString service)
{
    bool serviceState;
    USD_LOG(LOG_DEBUG, "About to start %s", service.toLatin1().data());
    serviceState = sharingManagerHandleService("StartUnit", service);
    return serviceState;
}

bool SharingManager::sharingManagerStopService(QString service)
{
    bool serviceState;
    USD_LOG(LOG_DEBUG, "About to stop %s", service.toLatin1().data());
    serviceState = sharingManagerHandleService("StopUnit", service);
    return serviceState;
}

bool SharingManager::sharingManagerHandleService(QString method, QString service )
{
    QString service_file = QString("%1.service").arg(service);
    QDBusMessage message =
    QDBusMessage::createMethodCall(SYSTEM_DBUS_NAME,
                                   SYSTEM_DBUS_PATH,
                                   SYSTEM_DBUS_INTER,
                                   method);
    message <<service_file<<"replace";
    QDBusMessage response = QDBusConnection::sessionBus().call(message);
    if (response.type() != QDBusMessage::ReplyMessage){
        USD_LOG(LOG_DEBUG, "servives dbus called failed");
        return false;
    }
    return true;
}

//update gsettings "service-name" key contents
bool update_ignore_paths(QList<QString>** ignore_paths,QString serviceName,bool state)
{
    bool found=(*ignore_paths)->contains(serviceName.toLatin1().data());
    if(state && found==false){
        (*ignore_paths)->push_front(serviceName.toLatin1().data());
        return true;
    }
    if(!state && found==true){
        (*ignore_paths)->removeOne(serviceName.toLatin1().data());
        return true;
    }
    return false;
}

void SharingManager::updateSaveService(bool state, QString serviceName)
{
    QStringList service_list;
    QStringList serviceStr;
    QList<QString>* service;
    bool updated;
    QList<QString>::iterator l;

    service =new QList<QString>();
    //get contents from "ignore-paths" key
    if (!settings->get(KEY_SERVICE_NAME).toStringList().isEmpty())
        service_list.append(settings->get(KEY_SERVICE_NAME).toStringList());

    for(auto str: service_list){
        if(!str.isEmpty())
            service->push_back(str);
    }

    updated = update_ignore_paths(&service, serviceName, state);

    if(updated){
        for(l = service->begin(); l != service->end(); ++l){
            serviceStr.append(*l);
        }
        //set latest contents to gsettings "ignore-paths" key
        settings->set(KEY_SERVICE_NAME, QVariant::fromValue(serviceStr));
    }
    //free QList Memory
    if(service){
        service->clear();
    }
}

void SharingManager::sharingManagerServiceChange(QString state, QString service)
{
    bool serviceState;
    if(state.compare("enable") == 0){
        serviceState = sharingManagerStartService(service);
        if (serviceState)
            updateSaveService(true, service);
    }
    else if (state.compare("disable") == 0) {
        serviceState = sharingManagerStopService(service);
        if (serviceState)
            updateSaveService(false, service);
    }
}

bool SharingManager::start()
{
    USD_LOG(LOG_DEBUG,"Starting sharing manager!");

    startKrd();

    settings = new QGSettings(SHATINGS_SCHEMA);
    QStringList serviceList = settings->get(KEY_SERVICE_NAME).toStringList();
    for (QString serviceName : serviceList) {
        sharingManagerStartService(serviceName);
    }
    return true;
}

void SharingManager::stop()
{
    USD_LOG(LOG_DEBUG,"Stopping sharing manager!");
    if(settings) {
        delete settings;
        settings = nullptr;
    }
}