File indexing completed on 2024-05-19 05:21:53
0001 /* 0002 * Copyright (C) 2003 by Tomas Pospisek <tpo@sourcepole.ch> 0003 * Copyright (C) 2019 Alexander Potashev <aspotashev@gmail.com> 0004 * 0005 * This program is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program 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 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License along 0016 * with this program; if not, write to the 0017 * Free Software Foundation, Inc. 0018 * 51 Franklin Street, Fifth Floor 0019 * Boston, MA 02110-1301 USA. 0020 * 0021 */ 0022 0023 #include "desktoptracker.h" 0024 0025 #include <QTimer> 0026 0027 #include <KX11Extras> 0028 #include <KWindowSystem> 0029 0030 #include "ktimetracker.h" 0031 #include "ktimetrackerutility.h" 0032 #include "ktt_debug.h" 0033 0034 DesktopTracker::DesktopTracker() 0035 : m_desktopCount(numDesktops) 0036 , m_timer(new QTimer(this)) 0037 { 0038 if(KWindowSystem::isPlatformX11()) 0039 { 0040 // currentDesktop will return 0 if no window manager is started 0041 m_previousDesktop = std::max(KX11Extras::currentDesktop() - 1, 0); 0042 m_desktop = std::max(KX11Extras::currentDesktop() - 1, 0); 0043 numDesktops = KX11Extras::numberOfDesktops(); 0044 // Setup desktop change handling 0045 connect(KX11Extras::self(), &KX11Extras::currentDesktopChanged, this, &DesktopTracker::handleDesktopChange); 0046 } 0047 0048 m_timer->setSingleShot(true); 0049 connect(m_timer, &QTimer::timeout, this, &DesktopTracker::changeTimers); 0050 } 0051 0052 void DesktopTracker::handleDesktopChange(int desktop) 0053 { 0054 m_desktop = desktop; 0055 0056 // If user changes back and forth between desktops rapidly and frequently, 0057 // the data file can get huge fast if logging is turned on. Then saving 0058 // gets slower, etc. There's no benefit in saving a lot of start/stop 0059 // events that are very small. Wait a bit to make sure the user is settled. 0060 m_timer->start(KTimeTrackerSettings::minActiveTime() * 1000); 0061 } 0062 0063 void DesktopTracker::changeTimers() 0064 { 0065 --m_desktop; // desktopTracker starts with 0 for desktop 1 0066 // notify start all tasks setup for running on desktop 0067 0068 // stop trackers for m_previousDesktop 0069 for (Task *task : m_desktopTracker[m_previousDesktop]) { 0070 Q_EMIT leftActiveDesktop(task); 0071 } 0072 0073 // start trackers for desktop 0074 for (Task *task : m_desktopTracker[m_desktop]) { 0075 Q_EMIT reachedActiveDesktop(task); 0076 } 0077 0078 m_previousDesktop = m_desktop; 0079 } 0080 0081 QString DesktopTracker::startTracking() 0082 { 0083 if(KWindowSystem::isPlatformX11()) 0084 { 0085 int currentDesktop = KX11Extras::currentDesktop() - 1; 0086 if (currentDesktop < 0) { 0087 currentDesktop = 0; 0088 } 0089 0090 if (currentDesktop >= maxDesktops) { 0091 return QStringLiteral("desktop number too high, desktop tracking will not work"); 0092 } 0093 0094 for (Task *task : m_desktopTracker[currentDesktop]) { 0095 Q_EMIT reachedActiveDesktop(task); 0096 } 0097 } 0098 0099 return QString(); 0100 } 0101 0102 void DesktopTracker::registerForDesktops(Task *task, DesktopList desktopList) 0103 { 0104 qCDebug(KTT_LOG) << "Entering function"; 0105 // if no desktop is marked, disable auto tracking for this task 0106 if (desktopList.empty()) { 0107 for (int i = 0; i < maxDesktops; ++i) { 0108 TaskVector *v = &(m_desktopTracker[i]); 0109 TaskVector::iterator tit = std::find(v->begin(), v->end(), task); 0110 if (tit != v->end()) { 0111 m_desktopTracker[i].erase(tit); 0112 } 0113 if(KWindowSystem::isPlatformX11()) 0114 { 0115 // if the task was previously tracking this desktop then 0116 // emit a signal that is not tracking it any more 0117 if (i == KX11Extras::currentDesktop() - 1) { 0118 Q_EMIT leftActiveDesktop(task); 0119 } 0120 } 0121 } 0122 qCDebug(KTT_LOG) << "Leaving function, desktopList.size=0"; 0123 return; 0124 } 0125 0126 // If desktop contains entries then configure desktopTracker 0127 // If a desktop was disabled, it will not be stopped automatically. 0128 // If enabled: Start it now. 0129 if (!desktopList.empty()) { 0130 for (int i = 0; i < maxDesktops; ++i) { 0131 TaskVector &v = m_desktopTracker[i]; 0132 TaskVector::iterator tit = std::find(v.begin(), v.end(), task); 0133 // Is desktop i in the desktop list? 0134 if (std::find(desktopList.begin(), desktopList.end(), i) != desktopList.end()) { 0135 if (tit == v.end()) { 0136 // not yet in start vector 0137 v.push_back(task); // track in desk i 0138 } 0139 } else { 0140 // delete it 0141 if (tit != v.end()) { 0142 // not in start vector any more 0143 v.erase(tit); // so we delete it from desktopTracker 0144 if(KWindowSystem::isPlatformX11()) 0145 { 0146 // if the task was previously tracking this desktop then 0147 // emit a signal that is not tracking it any more 0148 if (i == KX11Extras::currentDesktop() - 1) { 0149 Q_EMIT leftActiveDesktop(task); 0150 } 0151 } 0152 } 0153 } 0154 } 0155 startTracking(); 0156 } 0157 qCDebug(KTT_LOG) << "Leaving function"; 0158 } 0159 0160 #include "moc_desktoptracker.cpp"