File indexing completed on 2023-05-30 11:40:28
0001 /* 0002 Auto away-presence setter-class 0003 Copyright (C) 2011 Martin Klapetek <martin.klapetek@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 "autoaway.h" 0021 0022 #include <KConfig> 0023 #include <KConfigGroup> 0024 #include <KSharedConfig> 0025 0026 #include <KIdleTime> 0027 0028 AutoAway::AutoAway(QObject *parent) 0029 : TelepathyKDEDModulePlugin(parent), 0030 m_awayTimeoutId(-1), 0031 m_extAwayTimeoutId(-1), 0032 m_awayPresence(Tp::Presence::away()), 0033 m_extAwayPresence(Tp::Presence::xa()) 0034 { 0035 reloadConfig(); 0036 } 0037 0038 AutoAway::~AutoAway() 0039 { 0040 } 0041 0042 QString AutoAway::pluginName() const 0043 { 0044 return QString::fromLatin1("auto-away"); 0045 } 0046 0047 void AutoAway::timeoutReached(int id) 0048 { 0049 if (id == m_awayTimeoutId) { 0050 setPlugin(m_awayPresence); 0051 } else if (id == m_extAwayTimeoutId) { 0052 setPlugin(m_extAwayPresence); 0053 } else { 0054 return; 0055 } 0056 0057 KIdleTime::instance()->catchNextResumeEvent(); 0058 } 0059 0060 void AutoAway::backFromIdle() 0061 { 0062 setPlugin(Enabled); 0063 } 0064 0065 void AutoAway::reloadConfig() 0066 { 0067 KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathyrc")); 0068 config.data()->reparseConfiguration(); 0069 KConfigGroup kdedConfig = config->group("KDED"); 0070 0071 bool autoAwayEnabled = kdedConfig.readEntry("autoAwayEnabled", true); 0072 bool autoXAEnabled = kdedConfig.readEntry("autoXAEnabled", true); 0073 0074 //WARNING: can't use removeAllTimeouts because this runs inside KDED, it would remove other KDED timeouts as well 0075 KIdleTime::instance()->removeIdleTimeout(m_awayTimeoutId); 0076 m_awayTimeoutId = -1; 0077 KIdleTime::instance()->removeIdleTimeout(m_extAwayTimeoutId); 0078 m_extAwayTimeoutId = -1; 0079 0080 if (autoAwayEnabled) { 0081 connect(KIdleTime::instance(), static_cast<void (KIdleTime::*)(int)>(&KIdleTime::timeoutReached), this, &AutoAway::timeoutReached); 0082 connect(KIdleTime::instance(), &KIdleTime::resumingFromIdle, this, &AutoAway::backFromIdle); 0083 0084 int awayTime = kdedConfig.readEntry("awayAfter", 5); 0085 QString awayMessage = kdedConfig.readEntry(QLatin1String("awayMessage"), QString()); 0086 awayMessage.replace(QRegularExpression(QLatin1String("%te\\b")), QLatin1String("%te+") + QString::number(awayTime)); 0087 m_awayPresence.setStatusMessage(awayMessage); 0088 m_awayTimeoutId = KIdleTime::instance()->addIdleTimeout(awayTime * 60 * 1000); 0089 } else { 0090 disconnect(KIdleTime::instance()); 0091 } 0092 0093 if (autoAwayEnabled && autoXAEnabled) { 0094 int xaTime = kdedConfig.readEntry("xaAfter", 15); 0095 QString xaMessage = kdedConfig.readEntry(QLatin1String("xaMessage"), QString()); 0096 xaMessage.replace(QRegularExpression(QLatin1String("%te\\b")), QLatin1String("%te+") + QString::number(xaTime)); 0097 m_extAwayPresence.setStatusMessage(xaMessage); 0098 m_extAwayTimeoutId = KIdleTime::instance()->addIdleTimeout(xaTime * 60 * 1000); 0099 } 0100 0101 setPlugin(State(autoAwayEnabled)); 0102 }