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

kleopatra

  • sources
  • kde-4.12
  • 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 #include <boost/bind.hpp>
42 
43 using namespace Kleo;
44 using namespace boost;
45 
46 static const int GARBAGE_COLLECTION_INTERVAL = 60000; // 1min
47 
48 static QMutex mutex;
49 
50 SessionData::SessionData()
51  : mementos(),
52  ref( 0 ),
53  ripe( false )
54 {
55 
56 }
57 
58 // static
59 shared_ptr<SessionDataHandler> SessionDataHandler::instance() {
60  mutex.lock();
61  static SessionDataHandler handler;
62  return shared_ptr<SessionDataHandler>( &handler, boost::bind( &QMutex::unlock, &mutex ) );
63 }
64 
65 SessionDataHandler::SessionDataHandler()
66  : QObject(),
67  data(),
68  timer()
69 {
70  timer.setInterval( GARBAGE_COLLECTION_INTERVAL );
71  timer.setSingleShot( false );
72 }
73 
74 
75 void SessionDataHandler::enterSession( unsigned int id ) {
76  kDebug() << id;
77  const shared_ptr<SessionData> sd = sessionDataInternal( id );
78  assert( sd );
79  ++sd->ref;
80  sd->ripe = false;
81 }
82 
83 void SessionDataHandler::exitSession( unsigned int id ) {
84  kDebug() << id;
85  const shared_ptr<SessionData> sd = sessionDataInternal( id );
86  assert( sd );
87  if ( --sd->ref <= 0 ) {
88  sd->ref = 0;
89  sd->ripe = false;
90  if ( !timer.isActive() )
91  QMetaObject::invokeMethod( &timer, "start", Qt::QueuedConnection );
92  }
93 }
94 
95 shared_ptr<SessionData> SessionDataHandler::sessionDataInternal( unsigned int id ) const {
96  std::map< unsigned int, shared_ptr<SessionData> >::iterator
97  it = data.lower_bound( id );
98  if ( it == data.end() || it->first != id ) {
99  const shared_ptr<SessionData> sd( new SessionData );
100  it = data.insert( it, std::make_pair( id, sd ) );
101  }
102  return it->second;
103 }
104 
105 shared_ptr<SessionData> SessionDataHandler::sessionData( unsigned int id ) const {
106  return sessionDataInternal( id );
107 }
108 
109 void SessionDataHandler::clear() {
110  data.clear();
111 }
112 
113 void SessionDataHandler::slotCollectGarbage() {
114  const QMutexLocker locker( &mutex );
115  unsigned int alive = 0;
116  std::map< unsigned int, shared_ptr<SessionData> >::iterator it = data.begin(), end = data.end();
117  while ( it != end )
118  if ( it->second->ripe ) {
119  data.erase( it++ );
120  } else if ( !it->second->ref ) {
121  it->second->ripe = true;
122  ++it;
123  } else {
124  ++alive;
125  ++it;
126  }
127  if ( alive == data.size() )
128  QMetaObject::invokeMethod( &timer, "stop", Qt::QueuedConnection );
129 }
Kleo::SessionDataHandler::enterSession
void enterSession(unsigned int id)
Definition: sessiondata.cpp:75
Kleo::SessionData
Definition: sessiondata.h:50
Kleo::SessionDataHandler::sessionData
boost::shared_ptr< SessionData > sessionData(unsigned int) const
Definition: sessiondata.cpp:105
Kleo::SessionDataHandler
Definition: sessiondata.h:62
boost::shared_ptr
Definition: encryptemailcontroller.h:51
sessiondata.h
Kleo::SessionDataHandler::exitSession
void exitSession(unsigned int id)
Definition: sessiondata.cpp:83
mutex
static QMutex mutex
Definition: sessiondata.cpp:48
Kleo::SessionDataHandler::clear
void clear()
Definition: sessiondata.cpp:109
GARBAGE_COLLECTION_INTERVAL
static const int GARBAGE_COLLECTION_INTERVAL
Definition: sessiondata.cpp:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 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

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