1 /*
2 Auto away-presence setter-class
3 Copyright (C) 2011 Martin Klapetek <martin.klapetek@gmail.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "autoaway.h"
21
22 #include <KConfig>
23 #include <KConfigGroup>
24 #include <KSharedConfig>
25
26 #include <KIdleTime>
27
AutoAway(QObject * parent)28 AutoAway::AutoAway(QObject *parent)
29 : TelepathyKDEDModulePlugin(parent),
30 m_awayTimeoutId(-1),
31 m_extAwayTimeoutId(-1),
32 m_awayPresence(Tp::Presence::away()),
33 m_extAwayPresence(Tp::Presence::xa())
34 {
35 reloadConfig();
36 }
37
~AutoAway()38 AutoAway::~AutoAway()
39 {
40 }
41
pluginName() const42 QString AutoAway::pluginName() const
43 {
44 return QString::fromLatin1("auto-away");
45 }
46
timeoutReached(int id)47 void AutoAway::timeoutReached(int id)
48 {
49 if (id == m_awayTimeoutId) {
50 setPlugin(m_awayPresence);
51 } else if (id == m_extAwayTimeoutId) {
52 setPlugin(m_extAwayPresence);
53 } else {
54 return;
55 }
56
57 KIdleTime::instance()->catchNextResumeEvent();
58 }
59
backFromIdle()60 void AutoAway::backFromIdle()
61 {
62 setPlugin(Enabled);
63 }
64
reloadConfig()65 void AutoAway::reloadConfig()
66 {
67 KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
68 config.data()->reparseConfiguration();
69 KConfigGroup kdedConfig = config->group("KDED");
70
71 bool autoAwayEnabled = kdedConfig.readEntry("autoAwayEnabled", true);
72 bool autoXAEnabled = kdedConfig.readEntry("autoXAEnabled", true);
73
74 //WARNING: can't use removeAllTimeouts because this runs inside KDED, it would remove other KDED timeouts as well
75 KIdleTime::instance()->removeIdleTimeout(m_awayTimeoutId);
76 m_awayTimeoutId = -1;
77 KIdleTime::instance()->removeIdleTimeout(m_extAwayTimeoutId);
78 m_extAwayTimeoutId = -1;
79
80 if (autoAwayEnabled) {
81 connect(KIdleTime::instance(), static_cast<void (KIdleTime::*)(int)>(&KIdleTime::timeoutReached), this, &AutoAway::timeoutReached);
82 connect(KIdleTime::instance(), &KIdleTime::resumingFromIdle, this, &AutoAway::backFromIdle);
83
84 int awayTime = kdedConfig.readEntry("awayAfter", 5);
85 QString awayMessage = kdedConfig.readEntry(QLatin1String("awayMessage"), QString());
86 awayMessage.replace(QRegularExpression(QLatin1String("%te\\b")), QLatin1String("%te+") + QString::number(awayTime));
87 m_awayPresence.setStatusMessage(awayMessage);
88 m_awayTimeoutId = KIdleTime::instance()->addIdleTimeout(awayTime * 60 * 1000);
89 } else {
90 disconnect(KIdleTime::instance());
91 }
92
93 if (autoAwayEnabled && autoXAEnabled) {
94 int xaTime = kdedConfig.readEntry("xaAfter", 15);
95 QString xaMessage = kdedConfig.readEntry(QLatin1String("xaMessage"), QString());
96 xaMessage.replace(QRegularExpression(QLatin1String("%te\\b")), QLatin1String("%te+") + QString::number(xaTime));
97 m_extAwayPresence.setStatusMessage(xaMessage);
98 m_extAwayTimeoutId = KIdleTime::instance()->addIdleTimeout(xaTime * 60 * 1000);
99 }
100
101 setPlugin(State(autoAwayEnabled));
102 }
103