File indexing completed on 2023-05-30 11:40:29

0001 /*
0002     Set away - option when screen saver activated-class
0003     Copyright (C) 2013  Lucas Betschart <lucasbetschart@gmail.com>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Lesser General Public
0007     License as published by the Free Software Foundation; either
0008     version 2.1 of the License, or (at your option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013     Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public
0016     License along with this library; if not, write to the Free Software
0017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018 */
0019 
0020 #include "screensaveraway.h"
0021 
0022 #include <KConfig>
0023 #include <KConfigGroup>
0024 #include <KSharedConfig>
0025 
0026 #include <QDBusConnectionInterface>
0027 #include <QDBusInterface>
0028 
0029 ScreenSaverAway::ScreenSaverAway(QObject *parent)
0030     : TelepathyKDEDModulePlugin(parent)
0031 {
0032     m_screenSaverInterface = new QDBusInterface(QLatin1String("org.freedesktop.ScreenSaver"),
0033                                 QLatin1String("/ScreenSaver"),
0034                                 QString(),
0035                                 QDBusConnection::sessionBus(), this);
0036 
0037     reloadConfig();
0038 }
0039 
0040 ScreenSaverAway::~ScreenSaverAway()
0041 {
0042 }
0043 
0044 QString ScreenSaverAway::pluginName() const
0045 {
0046     return QString::fromLatin1("screen-saver-away");
0047 }
0048 
0049 void ScreenSaverAway::onActiveChanged(bool newState)
0050 {
0051     if (newState) {
0052         QString awayMessage = m_screenSaverAwayMessage;
0053         QDBusReply<int> idleTime = m_screenSaverInterface->asyncCall(QLatin1String("GetSessionIdleTime"));
0054         awayMessage.replace(QRegularExpression(QLatin1String("%te\\b")), QLatin1String("%te+") + QString::number(std::round(idleTime.value() / 1000 / 60)));
0055         setPlugin(Tp::Presence::away(awayMessage));
0056     } else {
0057         setPlugin(Enabled);
0058     }
0059 }
0060 
0061 void ScreenSaverAway::reloadConfig()
0062 {
0063     KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
0064     config.data()->reparseConfiguration();
0065     KConfigGroup kdedConfig = config->group("KDED");
0066 
0067     bool screenSaverAwayEnabled = kdedConfig.readEntry("screenSaverAwayEnabled", true);
0068     m_screenSaverAwayMessage = kdedConfig.readEntry(QLatin1String("screenSaverAwayMessage"), QString());
0069 
0070     if (screenSaverAwayEnabled) {
0071         //watch for screen locked
0072         connect(m_screenSaverInterface, SIGNAL(ActiveChanged(bool)), this, SLOT(onActiveChanged(bool)));
0073     } else {
0074         m_screenSaverInterface->disconnect();
0075     }
0076 
0077     setPlugin(State(screenSaverAwayEnabled));
0078 }