File indexing completed on 2024-05-12 05:38:47
0001 /* 0002 * SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org> 0003 * SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "ExternalServiceSettings.h" 0009 0010 // KDE 0011 #include <KAuth/Action> 0012 #include <KAuth/ExecuteJob> 0013 #include <KLocalizedString> 0014 #include <Solid/Battery> 0015 #include <Solid/Device> 0016 0017 // Qt 0018 #include <QDBusConnection> 0019 #include <QDBusConnectionInterface> 0020 #include <QDBusMessage> 0021 #include <QDBusPendingCall> 0022 #include <QDBusServiceWatcher> 0023 #include <QPointer> 0024 0025 namespace 0026 { 0027 constexpr int ChargeThresholdUnsupported = -1; 0028 } 0029 0030 namespace PowerDevil 0031 { 0032 0033 ExternalServiceSettings::ExternalServiceSettings(QObject *parent) 0034 : QObject(parent) 0035 , m_chargeStartThreshold(ChargeThresholdUnsupported) 0036 , m_chargeStopThreshold(ChargeThresholdUnsupported) 0037 , m_savedChargeStartThreshold(ChargeThresholdUnsupported) 0038 , m_savedChargeStopThreshold(ChargeThresholdUnsupported) 0039 , m_chargeStopThresholdMightNeedReconnect(false) 0040 { 0041 } 0042 0043 void ExternalServiceSettings::load(QWindow *parentWindowForKAuth) 0044 { 0045 KAuth::Action action(QStringLiteral("org.kde.powerdevil.chargethresholdhelper.getthreshold")); 0046 action.setHelperId(QStringLiteral("org.kde.powerdevil.chargethresholdhelper")); 0047 action.setParentWindow(parentWindowForKAuth); 0048 KAuth::ExecuteJob *job = action.execute(); 0049 0050 QPointer thisAlive(this); 0051 QPointer jobAlive(job); 0052 job->exec(); 0053 0054 if (thisAlive && jobAlive) { 0055 if (!job->error()) { 0056 const auto data = job->data(); 0057 setSavedChargeStartThreshold(data.value(QStringLiteral("chargeStartThreshold")).toInt()); 0058 setSavedChargeStopThreshold(data.value(QStringLiteral("chargeStopThreshold")).toInt()); 0059 setChargeStopThreshold(m_savedChargeStopThreshold); 0060 setChargeStartThreshold(m_savedChargeStartThreshold); 0061 } else { 0062 qWarning() << "org.kde.powerdevil.chargethresholdhelper.getthreshold failed:" << job->errorText(); 0063 setSavedChargeStartThreshold(ChargeThresholdUnsupported); 0064 setSavedChargeStopThreshold(ChargeThresholdUnsupported); 0065 } 0066 } else { 0067 qWarning() << "org.kde.powerdevil.chargethresholdhelper.getthreshold failed: was deleted during job execution"; 0068 } 0069 } 0070 0071 void ExternalServiceSettings::save(QWindow *parentWindowForKAuth) 0072 { 0073 if ((isChargeStartThresholdSupported() && m_chargeStartThreshold != m_savedChargeStartThreshold) 0074 || (isChargeStopThresholdSupported() && m_chargeStopThreshold != m_savedChargeStopThreshold)) { 0075 int newChargeStartThreshold = isChargeStartThresholdSupported() ? m_chargeStartThreshold : ChargeThresholdUnsupported; 0076 int newChargeStopThreshold = isChargeStopThresholdSupported() ? m_chargeStopThreshold : ChargeThresholdUnsupported; 0077 0078 KAuth::Action action(QStringLiteral("org.kde.powerdevil.chargethresholdhelper.setthreshold")); 0079 action.setHelperId(QStringLiteral("org.kde.powerdevil.chargethresholdhelper")); 0080 action.setArguments({ 0081 {QStringLiteral("chargeStartThreshold"), newChargeStartThreshold}, 0082 {QStringLiteral("chargeStopThreshold"), newChargeStopThreshold}, 0083 }); 0084 action.setParentWindow(parentWindowForKAuth); 0085 KAuth::ExecuteJob *job = action.execute(); 0086 0087 QPointer thisAlive(this); 0088 QPointer jobAlive(job); 0089 job->exec(); 0090 0091 if (thisAlive && jobAlive) { 0092 if (!job->error()) { 0093 setSavedChargeStartThreshold(newChargeStartThreshold); 0094 setSavedChargeStopThreshold(newChargeStopThreshold); 0095 } else { 0096 qWarning() << "org.kde.powerdevil.chargethresholdhelper.setthreshold failed:" << job->errorText(); 0097 } 0098 setChargeStopThreshold(m_savedChargeStopThreshold); 0099 setChargeStartThreshold(m_savedChargeStartThreshold); 0100 } else { 0101 qWarning() << "org.kde.powerdevil.chargethresholdhelper.setthreshold failed: was deleted during job execution"; 0102 } 0103 } 0104 } 0105 0106 bool ExternalServiceSettings::isSaveNeeded() const 0107 { 0108 return (isChargeStartThresholdSupported() && m_chargeStartThreshold != m_savedChargeStartThreshold) 0109 || (isChargeStopThresholdSupported() && m_chargeStopThreshold != m_savedChargeStopThreshold); 0110 } 0111 0112 bool ExternalServiceSettings::isChargeStartThresholdSupported() const 0113 { 0114 return m_savedChargeStartThreshold != ChargeThresholdUnsupported; 0115 } 0116 0117 bool ExternalServiceSettings::isChargeStopThresholdSupported() const 0118 { 0119 return m_savedChargeStopThreshold != ChargeThresholdUnsupported; 0120 } 0121 0122 void ExternalServiceSettings::setSavedChargeStartThreshold(int threshold) 0123 { 0124 bool wasChargeStartThresholdSupported = isChargeStartThresholdSupported(); 0125 m_savedChargeStartThreshold = threshold; 0126 if (wasChargeStartThresholdSupported != isChargeStartThresholdSupported()) { 0127 Q_EMIT isChargeStartThresholdSupportedChanged(); 0128 } 0129 } 0130 0131 void ExternalServiceSettings::setSavedChargeStopThreshold(int threshold) 0132 { 0133 bool wasChargeStopThresholdSupported = isChargeStopThresholdSupported(); 0134 m_savedChargeStopThreshold = threshold; 0135 if (wasChargeStopThresholdSupported != isChargeStopThresholdSupported()) { 0136 Q_EMIT isChargeStopThresholdSupportedChanged(); 0137 } 0138 } 0139 0140 int ExternalServiceSettings::chargeStartThreshold() const 0141 { 0142 return m_chargeStartThreshold; 0143 } 0144 0145 int ExternalServiceSettings::chargeStopThreshold() const 0146 { 0147 return m_chargeStopThreshold; 0148 } 0149 0150 void ExternalServiceSettings::setChargeStartThreshold(int threshold) 0151 { 0152 if (threshold == m_chargeStartThreshold) { 0153 return; 0154 } 0155 m_chargeStartThreshold = threshold; 0156 Q_EMIT chargeStartThresholdChanged(); 0157 Q_EMIT settingsChanged(); 0158 } 0159 0160 void ExternalServiceSettings::setChargeStopThreshold(int threshold) 0161 { 0162 if (threshold == m_chargeStopThreshold) { 0163 return; 0164 } 0165 m_chargeStopThreshold = threshold; 0166 Q_EMIT chargeStopThresholdChanged(); 0167 0168 if (m_chargeStopThreshold > m_savedChargeStopThreshold) { 0169 // Only show message if there is actually a charging or fully charged battery 0170 const auto devices = Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString()); 0171 for (const Solid::Device &device : devices) { 0172 const Solid::Battery *b = qobject_cast<const Solid::Battery *>(device.asDeviceInterface(Solid::DeviceInterface::Battery)); 0173 if (b->chargeState() == Solid::Battery::Charging || b->chargeState() == Solid::Battery::FullyCharged) { 0174 setChargeStopThresholdMightNeedReconnect(true); 0175 break; 0176 } 0177 } 0178 } else { 0179 setChargeStopThresholdMightNeedReconnect(false); 0180 } 0181 0182 Q_EMIT settingsChanged(); 0183 } 0184 0185 bool ExternalServiceSettings::chargeStopThresholdMightNeedReconnect() const 0186 { 0187 return m_chargeStopThresholdMightNeedReconnect; 0188 } 0189 0190 void ExternalServiceSettings::setChargeStopThresholdMightNeedReconnect(bool mightNeedReconnect) 0191 { 0192 if (mightNeedReconnect == m_chargeStopThresholdMightNeedReconnect) { 0193 return; 0194 } 0195 m_chargeStopThresholdMightNeedReconnect = mightNeedReconnect; 0196 Q_EMIT chargeStopThresholdMightNeedReconnectChanged(); 0197 } 0198 0199 } // namespace PowerDevil 0200 0201 #include "moc_ExternalServiceSettings.cpp"