kaddressbook

kablock.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tdeabc/addressbook.h>
25 #include <tdeabc/resource.h>
26 #include <tdelocale.h>
27 #include <tdemessagebox.h>
28 #include <kstaticdeleter.h>
29 
30 #include "kablock.h"
31 
32 class AddressBookWrapper : public TDEABC::AddressBook
33 {
34  public:
35  AddressBookWrapper( TDEABC::AddressBook* );
36 
37  TDEABC::Resource* getStandardResource()
38  {
39  return standardResource();
40  }
41 };
42 
43 KABLock *KABLock::mSelf = 0;
44 
45 static KStaticDeleter<KABLock> kabLockDeleter;
46 
47 KABLock::KABLock( TDEABC::AddressBook *ab )
48  : mAddressBook( ab )
49 {
50 }
51 
52 KABLock::~KABLock()
53 {
54 }
55 
56 KABLock *KABLock::self( TDEABC::AddressBook *ab )
57 {
58  if ( !mSelf )
59  kabLockDeleter.setObject( mSelf, new KABLock( ab ) );
60  else
61  mSelf->mAddressBook = ab;
62 
63  return mSelf;
64 }
65 
66 bool KABLock::lock( TDEABC::Resource *resource )
67 {
68  if ( mLocks.find( resource ) == mLocks.end() ) { // not locked yet
69  TDEABC::Ticket *ticket = mAddressBook->requestSaveTicket( resource );
70  if ( !ticket ) {
71  return false;
72  } else {
73  LockEntry entry;
74  entry.ticket = ticket;
75  entry.counter = 1;
76  mLocks.insert( resource, entry );
77  }
78  } else {
79  LockEntry &entry = mLocks[ resource ];
80  entry.counter++;
81  }
82 
83  return true;
84 }
85 
86 bool KABLock::unlock( TDEABC::Resource *resource )
87 {
88  AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( mAddressBook );
89  if ( resource == 0 )
90  resource = wrapper->getStandardResource();
91 
92  if ( mLocks.find( resource ) == mLocks.end() ) { // hmm, not good...
93  return false;
94  } else {
95  LockEntry &entry = mLocks[ resource ];
96  entry.counter--;
97 
98  if ( entry.counter == 0 ) {
99  mAddressBook->save( entry.ticket );
100 // # Activate in KDE 4.0
101 // mAddressBook->releaseSaveTicket( entry.ticket );
102 
103  mLocks.remove( resource );
104  }
105  }
106 
107  return true;
108 }