kaddressbook

undocmds.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (C) 1999 Don Sanders <sanders@kde.org>
00004                   2005 Tobias Koenig <tokoe@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qapplication.h>
00026 #include <qclipboard.h>
00027 
00028 #include <klocale.h>
00029 #include <kapplication.h>
00030 
00031 #include "addresseeutil.h"
00032 #include "addresseeconfig.h"
00033 #include "core.h"
00034 #include "kablock.h"
00035 
00036 #include "undocmds.h"
00037 
00038 DeleteCommand::DeleteCommand( KABC::AddressBook *addressBook,
00039                               const QStringList &uidList)
00040   : Command( addressBook ), mUIDList( uidList )
00041 {
00042 }
00043 
00044 QString DeleteCommand::name() const
00045 {
00046   return i18n( "Delete Contact", "Delete %n Contacts", mUIDList.count() );
00047 }
00048 
00049 void DeleteCommand::unexecute()
00050 {
00051   // Put it back in the document
00052   KABC::Addressee::List::ConstIterator it;
00053   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00054 
00055   // lock resources
00056   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00057     lock()->lock( (*it).resource() );
00058 
00059   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00060     addressBook()->insertAddressee( *it );
00061     lock()->unlock( (*it).resource() );
00062   }
00063 
00064   mAddresseeList.clear();
00065 }
00066 
00067 void DeleteCommand::execute()
00068 {
00069   KABC::Addressee addr;
00070 
00071   QStringList::ConstIterator it;
00072   const QStringList::ConstIterator endIt( mUIDList.end() );
00073   for ( it = mUIDList.begin(); it != endIt; ++it ) {
00074     addr = addressBook()->findByUid( *it );
00075     lock()->lock( addr.resource() );
00076     mAddresseeList.append( addr );
00077     AddresseeConfig cfg( addr );
00078     cfg.remove();
00079   }
00080 
00081   KABC::Addressee::List::ConstIterator addrIt;
00082   const KABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
00083   for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
00084     addressBook()->removeAddressee( *addrIt );
00085     lock()->unlock( (*addrIt).resource() );
00086   }
00087 }
00088 
00089 
00090 PasteCommand::PasteCommand( KAB::Core *core, const KABC::Addressee::List &addressees )
00091   : Command( core->addressBook() ), mAddresseeList( addressees ), mCore( core )
00092 {
00093 }
00094 
00095 QString PasteCommand::name() const
00096 {
00097   return i18n( "Paste Contact", "Paste %n Contacts", mAddresseeList.count() );
00098 }
00099 
00100 void PasteCommand::unexecute()
00101 {
00102   KABC::Addressee::List::ConstIterator it;
00103   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00104 
00105   // lock resources
00106   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00107     lock()->lock( (*it).resource() );
00108 
00109   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00110     addressBook()->removeAddressee( *it );
00111     lock()->unlock( (*it).resource() );
00112   }
00113 }
00114 
00115 void PasteCommand::execute()
00116 {
00117   QStringList uids;
00118 
00119   KABC::Addressee::List::ConstIterator constIt;
00120   const KABC::Addressee::List::ConstIterator constEndIt( mAddresseeList.end() );
00121 
00122   // lock resources
00123   for ( constIt = mAddresseeList.begin(); constIt != constEndIt; ++constIt )
00124     lock()->lock( (*constIt).resource() );
00125 
00126   KABC::Addressee::List::Iterator it;
00127   const KABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
00128   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00133     (*it).setUid( KApplication::randomString( 10 ) );
00134     uids.append( (*it).uid() );
00135     addressBook()->insertAddressee( *it );
00136     lock()->unlock( (*it).resource() );
00137   }
00138 
00139   QStringList::ConstIterator uidIt;
00140   const QStringList::ConstIterator uidEndIt( uids.end() );
00141   for ( uidIt = uids.begin(); uidIt != uidEndIt; ++uidIt )
00142     mCore->editContact( *uidIt );
00143 }
00144 
00145 
00146 NewCommand::NewCommand( KABC::AddressBook *addressBook, const KABC::Addressee::List &addressees )
00147   : Command( addressBook ), mAddresseeList( addressees )
00148 {
00149 }
00150 
00151 QString NewCommand::name() const
00152 {
00153   return i18n( "New Contact", "New %n Contacts", mAddresseeList.count() );
00154 }
00155 
00156 void NewCommand::unexecute()
00157 {
00158   KABC::Addressee::List::ConstIterator it;
00159   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00160 
00161   // lock resources
00162   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00163     lock()->lock( (*it).resource() );
00164 
00165   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00166     addressBook()->removeAddressee( *it );
00167     lock()->unlock( (*it).resource() );
00168   }
00169 }
00170 
00171 void NewCommand::execute()
00172 {
00173   KABC::Addressee::List::Iterator it;
00174   const KABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
00175 
00176   // lock resources
00177   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00178     lock()->lock( (*it).resource() );
00179 
00180   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00181     addressBook()->insertAddressee( *it );
00182     lock()->unlock( (*it).resource() );
00183   }
00184 }
00185 
00186 
00187 EditCommand::EditCommand( KABC::AddressBook *addressBook,
00188                           const KABC::Addressee &oldAddressee,
00189                           const KABC::Addressee &newAddressee )
00190   : Command( addressBook ),
00191     mOldAddressee( oldAddressee ), mNewAddressee( newAddressee )
00192 {
00193 }
00194 
00195 QString EditCommand::name() const
00196 {
00197   return i18n( "Edit Contact" );
00198 }
00199 
00200 void EditCommand::unexecute()
00201 {
00202   lock()->lock( mOldAddressee.resource() );
00203   addressBook()->insertAddressee( mOldAddressee );
00204   lock()->unlock( mOldAddressee.resource() );
00205 }
00206 
00207 void EditCommand::execute()
00208 {
00209   lock()->lock( mNewAddressee.resource() );
00210   addressBook()->insertAddressee( mNewAddressee );
00211   lock()->unlock( mNewAddressee.resource() );
00212 }
00213 
00214 
00215 CutCommand::CutCommand( KABC::AddressBook *addressBook, const QStringList &uidList )
00216   : Command( addressBook ), mUIDList( uidList )
00217 {
00218 }
00219 
00220 QString CutCommand::name() const
00221 {
00222   return i18n( "Cut Contact", "Cut %n Contacts", mUIDList.count() );
00223 }
00224 
00225 void CutCommand::unexecute()
00226 {
00227   KABC::Addressee::List::ConstIterator it;
00228   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00229 
00230   // lock resources
00231   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00232     lock()->lock( (*it).resource() );
00233 
00234   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00235     addressBook()->insertAddressee( *it );
00236     lock()->unlock( (*it).resource() );
00237   }
00238 
00239   mAddresseeList.clear();
00240 
00241   QClipboard *cb = QApplication::clipboard();
00242   kapp->processEvents();
00243   cb->setText( mOldText );
00244 }
00245 
00246 void CutCommand::execute()
00247 {
00248   KABC::Addressee addr;
00249 
00250   QStringList::ConstIterator it;
00251   const QStringList::ConstIterator endIt( mUIDList.end() );
00252   for ( it = mUIDList.begin(); it != endIt; ++it ) {
00253     addr = addressBook()->findByUid( *it );
00254     mAddresseeList.append( addr );
00255     lock()->lock( addr.resource() );
00256   }
00257 
00258   KABC::Addressee::List::ConstIterator addrIt;
00259   const KABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
00260   for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
00261     addressBook()->removeAddressee( *addrIt );
00262     lock()->unlock( addr.resource() );
00263   }
00264 
00265   // Convert to clipboard
00266   mClipText = AddresseeUtil::addresseesToClipboard( mAddresseeList );
00267 
00268   QClipboard *cb = QApplication::clipboard();
00269   mOldText = cb->text();
00270   kapp->processEvents();
00271   cb->setText( mClipText );
00272 }
KDE Home | KDE Accessibility Home | Description of Access Keys