• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopeteidletimer.cpp
Go to the documentation of this file.
1 /*
2  kopeteidletimer.cpp - Kopete Idle Timer
3 
4  Copyright (c) 2002 by Hendrik vom Lehn <hvl@linux-4-ever.de>
5  Copyright (c) 2003 by Olivier Goffart <ogoffart@kde.org>
6  Copyright (c) 2008 by Roman Jarosz <kedgedev@centrum.cz>
7  Kopete (c) 2002-2008 by the Kopete developers <kopete-devel@kde.org>
8 
9  *************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2 of the License, or (at your option) any later version. *
15  * *
16  *************************************************************************
17 */
18 
19 #include "kopeteidletimer.h"
20 #include "kopeteidleplatform_p.h"
21 
22 #include <QtCore/QTimer>
23 #include <QtCore/QDateTime>
24 #include <QtGui/QCursor>
25 
26 #include <kdebug.h>
27 
28 class Kopete::IdleTimer::Private
29 {
30 public:
31  struct TimeoutReceiver {
32  bool active;
33  int msec;
34  QObject * receiver;
35  const char * memberActive;
36  const char * memberIdle;
37  };
38  QList<TimeoutReceiver> receiverList;
39  QTimer timer;
40 
41  QPoint lastMousePos;
42  QDateTime idleSince;
43 
44  Kopete::IdlePlatform *platform;
45 };
46 
47 Kopete::IdleTimer *Kopete::IdleTimer::instance = 0L;
48 
49 Kopete::IdleTimer::IdleTimer()
50 : QObject(), d( new Private )
51 {
52  d->platform = 0;
53 
54  Kopete::IdlePlatform *p = new Kopete::IdlePlatform();
55  if ( p->init() )
56  {
57  kDebug() << "Using platform idle timer";
58  d->platform = p;
59  }
60  else
61  {
62  kWarning() << "Using dummy idle timer";
63  delete p;
64 
65  d->lastMousePos = QCursor::pos();
66  d->idleSince = QDateTime::currentDateTime();
67  }
68 
69  // init the timer
70  connect(&d->timer, SIGNAL(timeout()), this, SLOT(updateIdleTime()));
71  d->timer.start(4000);
72 }
73 
74 Kopete::IdleTimer *Kopete::IdleTimer::self()
75 {
76  if ( !instance )
77  instance = new IdleTimer;
78 
79  return instance;
80 }
81 
82 Kopete::IdleTimer::~IdleTimer()
83 {
84  instance = 0L;
85 
86  delete d->platform;
87 
88  delete d;
89 }
90 
91 int Kopete::IdleTimer::idleTime()
92 {
93  int idle;
94  if ( d->platform )
95  {
96  idle = d->platform->secondsIdle();
97  }
98  else
99  {
100  QPoint curMousePos = QCursor::pos();
101  QDateTime curDateTime = QDateTime::currentDateTime();
102  if ( d->lastMousePos != curMousePos )
103  {
104  d->lastMousePos = curMousePos;
105  d->idleSince = curDateTime;
106  }
107  idle = d->idleSince.secsTo(curDateTime);
108  }
109 
110  return idle;
111 }
112 
113 void Kopete::IdleTimer::registerTimeout( int idleSeconds, QObject *receiver,
114  const char *memberActive, const char *memberIdle )
115 {
116  Private::TimeoutReceiver item = { true, idleSeconds * 1000, receiver, memberActive, memberIdle };
117  d->receiverList.append( item );
118  connect( receiver, SIGNAL(destroyed(QObject*)), this, SLOT(unregisterTimeout(QObject*)) );
119 }
120 
121 void Kopete::IdleTimer::unregisterTimeout( QObject *receiver )
122 {
123  for ( int i = 0; i < d->receiverList.size(); )
124  {
125  if ( d->receiverList.at(i).receiver == receiver )
126  {
127  d->receiverList.removeAt( i );
128  continue;
129  }
130 
131  i++;
132  }
133 }
134 
135 void Kopete::IdleTimer::updateIdleTime()
136 {
137  int idle = idleTime() * 1000;
138  bool activity = ( idle < 10 );
139 
140  for ( int i = 0; i < d->receiverList.size(); i++ )
141  {
142  Private::TimeoutReceiver item = d->receiverList.at(i);
143  if ( item.active != activity && (activity == true || idle > item.msec ) )
144  {
145  d->receiverList[i].active = activity;
146  if ( activity )
147  QTimer::singleShot( 0, item.receiver, item.memberActive );
148  else
149  QTimer::singleShot( 0, item.receiver, item.memberIdle );
150  }
151  }
152 }
153 
154 #include "kopeteidletimer.moc"
155 // vim: set et ts=4 sts=4 sw=4:
Kopete::IdleTimer::self
static IdleTimer * self()
Get the only instance of IdleTimer.
Definition: kopeteidletimer.cpp:74
QPoint
Kopete::IdleTimer::registerTimeout
void registerTimeout(int idleSeconds, QObject *receiver, const char *memberActive, const char *memberIdle)
Register new timeout notification.
Definition: kopeteidletimer.cpp:113
Kopete::IdleTimer::~IdleTimer
~IdleTimer()
Definition: kopeteidletimer.cpp:82
QTimer
Kopete::IdleTimer
IdleTimer handles global idle time and allows to register idle timeout notifications.
Definition: kopeteidletimer.h:33
QObject
QList< TimeoutReceiver >
kopeteidletimer.h
QDateTime::currentDateTime
QDateTime currentDateTime()
Kopete::IdlePlatform
Definition: kopeteidleplatform_p.h:22
Kopete::IdleTimer::idleTime
int idleTime()
Time in seconds the user has been idle.
Definition: kopeteidletimer.cpp:91
QCursor::pos
QPoint pos()
QDateTime::secsTo
int secsTo(const QDateTime &other) const
Kopete::IdlePlatform::init
bool init()
Definition: kopeteidleplatform_dummy.cpp:28
kopeteidleplatform_p.h
QDateTime
QTimer::singleShot
singleShot
Kopete::IdleTimer::unregisterTimeout
void unregisterTimeout(QObject *receiver)
removes all registered timeout notifications for this object
Definition: kopeteidletimer.cpp:121
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal