File: rfkill-state.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,324 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (175 lines) | stat: -rw-r--r-- 5,720 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
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2023 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 "rfkill-state.h"
#include "rfkillswitch.h"
#include "clib-syslog.h"

#include <QDebug>

#define MEDIA_SCHEMA   "org.ukui.SettingsDaemon.plugins.media-keys-state"
static QString s_rfkillState = "rfkill-state";

Q_GLOBAL_STATIC(RfkillState, s_rfkill)

RfkillState::RfkillState(QObject *parent) : QObject(parent)
{

}

RfkillState::~RfkillState()
{
    if (m_rfkillSettings) {
        disconnect(m_rfkillSettings,SIGNAL(changed(QString)),  this, SLOT(doSettingsChangeAction(const QString&)));
        m_rfkillSettings->deleteLater();
        m_rfkillSettings = nullptr;
    }
}

RfkillState *RfkillState::self()
{
    return s_rfkill;
}

void RfkillState::initialization()
{
    if (QGSettings::isSchemaInstalled(QByteArray(MEDIA_SCHEMA))) {
        if (!m_rfkillSettings) {
            m_rfkillSettings = new QGSettings(MEDIA_SCHEMA);
        }
        connect(m_rfkillSettings, SIGNAL(changed(QString)),  this, SLOT(doSettingsChangeAction(const QString&)));

        //此改动用于rfkill(飞行模式)的全局配置
        {
            //读取到全局配置,不再读取gsettings值
            QVariant state = getGlobalRfkillState();
            if (state.isValid()) {
                setFlightState(state.toInt());
                if (m_rfkillSettings) {
                    m_rfkillSettings->set(s_rfkillState, state.toInt());
                }
            } else if (m_rfkillSettings->keys().contains(s_rfkillState)) {
                //考虑到升级,启动时,未读取到全局配置,从gsettings读取,同步到全局配置
                int state = m_rfkillSettings->get(s_rfkillState).toInt();
                if(state >= 0) {
                    if(-1 == getFlightState()) {
                        m_rfkillSettings->set(s_rfkillState, -1);
                    } else {
                        if (state) {
                            setFlightState(state);
                            setGlobalRfkillState(state);
                        }
                    }
                }
            }
        }
    }
    // 用户状态切换信号
    QDBusConnection::sessionBus().connect(QString(),\
                                          QStringLiteral("/GlobaSignal"),\
                                          QStringLiteral("org.ukui.SettingsDaemon.GlobalSignal"),\
                                          "Active", this, SLOT(onUserActive(bool)));
}

int RfkillState::getWlanState()
{
    return RfkillSwitch::instance()->getCurrentWlanMode();
}

void RfkillState::setWlanState(bool state)
{
    Q_UNUSED(state)
    RfkillSwitch::instance()->turnWifiOn();
}

int RfkillState::getFlightState()
{
    return RfkillSwitch::instance()->getCurrentFlightMode();
}

void RfkillState::setFlightState(bool state)
{
    RfkillSwitch::instance()->toggleFlightMode(state);
}

int RfkillState::getBluetooth()
{
    return RfkillSwitch::instance()->getCurrentBluetoothMode();
}

void RfkillState::setBluetooth(bool state)
{
    RfkillSwitch::instance()->toggleBluetoothMode(state);
}

void RfkillState::setSettingsState(bool state)
{
    if (m_rfkillSettings && m_rfkillSettings->keys().contains(s_rfkillState)) {
        m_rfkillSettings->set(s_rfkillState, state);
    }
}

void RfkillState::setGlobalRfkillState(const QVariant& state)
{
    QDBusInterface globalManager("com.settings.daemon.qt.systemdbus", \
                                 "/globalconfig", \
                                 "com.settings.daemon.interface", \
                                 QDBusConnection::systemBus());
    QList<QVariant> arguments;
    arguments << QVariant(QString::fromLatin1("rfkill")) << QVariant(QString::fromLatin1("rfkill")) << QVariant::fromValue(QDBusVariant(state));
    globalManager.asyncCallWithArgumentList("setGlobalConf", arguments);
}

QVariant RfkillState::getGlobalRfkillState()
{
    QDBusInterface globalManager("com.settings.daemon.qt.systemdbus", \
                          "/globalconfig", \
                          "com.settings.daemon.interface", \
                          QDBusConnection::systemBus());

    QDBusReply<QVariant> reply = globalManager.call(QStringLiteral("getGlobalConf"), QStringLiteral("rfkill"), QStringLiteral("rfkill"));

    if (reply.isValid()) {
        return reply.value();
    }
    USD_LOG(LOG_ERR,"globalManager dbus interface failed .");
    return QVariant(QVariant::Invalid);
}

void RfkillState::doSettingsChangeAction(const QString &key)
{
    if (key == s_rfkillState) {
        int state = m_rfkillSettings->get(s_rfkillState).toInt();
        if(state != -1) {
            setFlightState(state);
            setGlobalRfkillState(state);
        }
    }
}

void RfkillState::onUserActive(bool state)
{
    //用户登录状态改变,获取全局配置,设置飞行模式状态
    if (state) {
        QVariant state = getGlobalRfkillState();
        if (state.isValid()) {
            m_rfkillSettings->set(s_rfkillState, state);
        }
    }
}