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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • uiserver
sessiondata.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  uiserver/sessiondata.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2009 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "sessiondata.h"
36 
37 #include <KDebug>
38 
39 #include <QMutex>
40 
41 #ifndef Q_MOC_RUN
42 #include <boost/bind.hpp>
43 #endif
44 
45 using namespace Kleo;
46 using namespace boost;
47 
48 static const int GARBAGE_COLLECTION_INTERVAL = 60000; // 1min
49 
50 static QMutex mutex;
51 
52 SessionData::SessionData()
53  : mementos(),
54  ref( 0 ),
55  ripe( false )
56 {
57 
58 }
59 
60 // static
61 shared_ptr<SessionDataHandler> SessionDataHandler::instance() {
62  mutex.lock();
63  static SessionDataHandler handler;
64  return shared_ptr<SessionDataHandler>( &handler, boost::bind( &QMutex::unlock, &mutex ) );
65 }
66 
67 SessionDataHandler::SessionDataHandler()
68  : QObject(),
69  data(),
70  timer()
71 {
72  timer.setInterval( GARBAGE_COLLECTION_INTERVAL );
73  timer.setSingleShot( false );
74 }
75 
76 
77 void SessionDataHandler::enterSession( unsigned int id ) {
78  kDebug() << id;
79  const shared_ptr<SessionData> sd = sessionDataInternal( id );
80  assert( sd );
81  ++sd->ref;
82  sd->ripe = false;
83 }
84 
85 void SessionDataHandler::exitSession( unsigned int id ) {
86  kDebug() << id;
87  const shared_ptr<SessionData> sd = sessionDataInternal( id );
88  assert( sd );
89  if ( --sd->ref <= 0 ) {
90  sd->ref = 0;
91  sd->ripe = false;
92  if ( !timer.isActive() )
93  QMetaObject::invokeMethod( &timer, "start", Qt::QueuedConnection );
94  }
95 }
96 
97 shared_ptr<SessionData> SessionDataHandler::sessionDataInternal( unsigned int id ) const {
98  std::map< unsigned int, shared_ptr<SessionData> >::iterator
99  it = data.lower_bound( id );
100  if ( it == data.end() || it->first != id ) {
101  const shared_ptr<SessionData> sd( new SessionData );
102  it = data.insert( it, std::make_pair( id, sd ) );
103  }
104  return it->second;
105 }
106 
107 shared_ptr<SessionData> SessionDataHandler::sessionData( unsigned int id ) const {
108  return sessionDataInternal( id );
109 }
110 
111 void SessionDataHandler::clear() {
112  data.clear();
113 }
114 
115 void SessionDataHandler::slotCollectGarbage() {
116  const QMutexLocker locker( &mutex );
117  unsigned int alive = 0;
118  std::map< unsigned int, shared_ptr<SessionData> >::iterator it = data.begin(), end = data.end();
119  while ( it != end )
120  if ( it->second->ripe ) {
121  data.erase( it++ );
122  } else if ( !it->second->ref ) {
123  it->second->ripe = true;
124  ++it;
125  } else {
126  ++alive;
127  ++it;
128  }
129  if ( alive == data.size() )
130  QMetaObject::invokeMethod( &timer, "stop", Qt::QueuedConnection );
131 }
QTimer::setInterval
void setInterval(int msec)
QMutex
Kleo::SessionDataHandler::enterSession
void enterSession(unsigned int id)
Definition: sessiondata.cpp:77
Kleo::SessionData
Definition: sessiondata.h:52
QMutex::unlock
void unlock()
Kleo::SessionDataHandler::sessionData
boost::shared_ptr< SessionData > sessionData(unsigned int) const
Definition: sessiondata.cpp:107
Kleo::SessionDataHandler
Definition: sessiondata.h:64
boost::shared_ptr
Definition: encryptemailcontroller.h:51
sessiondata.h
QObject
Kleo::SessionDataHandler::exitSession
void exitSession(unsigned int id)
Definition: sessiondata.cpp:85
mutex
static QMutex mutex
Definition: sessiondata.cpp:50
Kleo::SessionDataHandler::clear
void clear()
Definition: sessiondata.cpp:111
QMutex::lock
void lock()
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
GARBAGE_COLLECTION_INTERVAL
static const int GARBAGE_COLLECTION_INTERVAL
Definition: sessiondata.cpp:48
QMutexLocker
QTimer::isActive
bool isActive() const
QTimer::setSingleShot
void setSingleShot(bool singleShot)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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