kablock.cpp
00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <kabc/addressbook.h> 00025 #include <kabc/resource.h> 00026 #include <klocale.h> 00027 #include <kmessagebox.h> 00028 #include <kstaticdeleter.h> 00029 00030 #include "kablock.h" 00031 00032 class AddressBookWrapper : public KABC::AddressBook 00033 { 00034 public: 00035 AddressBookWrapper( KABC::AddressBook* ); 00036 00037 KABC::Resource* getStandardResource() 00038 { 00039 return standardResource(); 00040 } 00041 }; 00042 00043 KABLock *KABLock::mSelf = 0; 00044 00045 static KStaticDeleter<KABLock> kabLockDeleter; 00046 00047 KABLock::KABLock( KABC::AddressBook *ab ) 00048 : mAddressBook( ab ) 00049 { 00050 } 00051 00052 KABLock::~KABLock() 00053 { 00054 } 00055 00056 KABLock *KABLock::self( KABC::AddressBook *ab ) 00057 { 00058 if ( !mSelf ) 00059 kabLockDeleter.setObject( mSelf, new KABLock( ab ) ); 00060 else 00061 mSelf->mAddressBook = ab; 00062 00063 return mSelf; 00064 } 00065 00066 bool KABLock::lock( KABC::Resource *resource ) 00067 { 00068 if ( mLocks.find( resource ) == mLocks.end() ) { // not locked yet 00069 KABC::Ticket *ticket = mAddressBook->requestSaveTicket( resource ); 00070 if ( !ticket ) { 00071 return false; 00072 } else { 00073 LockEntry entry; 00074 entry.ticket = ticket; 00075 entry.counter = 1; 00076 mLocks.insert( resource, entry ); 00077 } 00078 } else { 00079 LockEntry &entry = mLocks[ resource ]; 00080 entry.counter++; 00081 } 00082 00083 return true; 00084 } 00085 00086 bool KABLock::unlock( KABC::Resource *resource ) 00087 { 00088 AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( mAddressBook ); 00089 if ( resource == 0 ) 00090 resource = wrapper->getStandardResource(); 00091 00092 if ( mLocks.find( resource ) == mLocks.end() ) { // hmm, not good... 00093 return false; 00094 } else { 00095 LockEntry &entry = mLocks[ resource ]; 00096 entry.counter--; 00097 00098 if ( entry.counter == 0 ) { 00099 mAddressBook->save( entry.ticket ); 00100 // # Activate in KDE 4.0 00101 // mAddressBook->releaseSaveTicket( entry.ticket ); 00102 00103 mLocks.remove( resource ); 00104 } 00105 } 00106 00107 return true; 00108 }