kaddressbook

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 Qt, and distribute the resulting executable,
00021     without including the source code for Qt 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 
00061   return mSelf;
00062 }
00063 
00064 bool KABLock::lock( KABC::Resource *resource )
00065 {
00066   if ( mLocks.find( resource ) == mLocks.end() ) { // not locked yet
00067     KABC::Ticket *ticket = mAddressBook->requestSaveTicket( resource );
00068     if ( !ticket ) {
00069       return false;
00070     } else {
00071       LockEntry entry;
00072       entry.ticket = ticket;
00073       entry.counter = 1;
00074       mLocks.insert( resource, entry );
00075     }
00076   } else {
00077     LockEntry &entry = mLocks[ resource ];
00078     entry.counter++;
00079   }
00080 
00081   return true;
00082 }
00083 
00084 bool KABLock::unlock( KABC::Resource *resource )
00085 {
00086   AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( mAddressBook );
00087   if ( resource == 0 )
00088     resource = wrapper->getStandardResource();
00089 
00090   if ( mLocks.find( resource ) == mLocks.end() ) { // hmm, not good...
00091     return false;
00092   } else {
00093     LockEntry &entry = mLocks[ resource ];
00094     entry.counter--;
00095 
00096     if ( entry.counter == 0 ) {
00097       mAddressBook->save( entry.ticket );
00098 //      # Activate in KDE 4.0
00099 //      mAddressBook->releaseSaveTicket( entry.ticket );
00100 
00101       mLocks.remove( resource );
00102     }
00103   }
00104 
00105   return true;
00106 }
KDE Home | KDE Accessibility Home | Description of Access Keys