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 }
00140 
00141 
00142 NewCommand::NewCommand( KABC::AddressBook *addressBook, const KABC::Addressee::List &addressees )
00143   : Command( addressBook ), mAddresseeList( addressees )
00144 {
00145 }
00146 
00147 QString NewCommand::name() const
00148 {
00149   return i18n( "New Contact", "New %n Contacts", mAddresseeList.count() );
00150 }
00151 
00152 void NewCommand::unexecute()
00153 {
00154   KABC::Addressee::List::ConstIterator it;
00155   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00156 
00157   // lock resources
00158   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00159     lock()->lock( (*it).resource() );
00160 
00161   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00162     addressBook()->removeAddressee( *it );
00163     lock()->unlock( (*it).resource() );
00164   }
00165 }
00166 
00167 void NewCommand::execute()
00168 {
00169   KABC::Addressee::List::Iterator it;
00170   const KABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
00171 
00172   // lock resources
00173   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00174     lock()->lock( (*it).resource() );
00175 
00176   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00177     addressBook()->insertAddressee( *it );
00178     lock()->unlock( (*it).resource() );
00179   }
00180 }
00181 
00182 
00183 EditCommand::EditCommand( KABC::AddressBook *addressBook,
00184                           const KABC::Addressee &oldAddressee,
00185                           const KABC::Addressee &newAddressee )
00186   : Command( addressBook ),
00187     mOldAddressee( oldAddressee ), mNewAddressee( newAddressee )
00188 {
00189 }
00190 
00191 QString EditCommand::name() const
00192 {
00193   return i18n( "Edit Contact" );
00194 }
00195 
00196 void EditCommand::unexecute()
00197 {
00198   lock()->lock( mOldAddressee.resource() );
00199   addressBook()->insertAddressee( mOldAddressee );
00200   lock()->unlock( mOldAddressee.resource() );
00201 }
00202 
00203 void EditCommand::execute()
00204 {
00205   lock()->lock( mNewAddressee.resource() );
00206   addressBook()->insertAddressee( mNewAddressee );
00207   lock()->unlock( mNewAddressee.resource() );
00208 }
00209 
00210 
00211 CutCommand::CutCommand( KABC::AddressBook *addressBook, const QStringList &uidList )
00212   : Command( addressBook ), mUIDList( uidList )
00213 {
00214 }
00215 
00216 QString CutCommand::name() const
00217 {
00218   return i18n( "Cut Contact", "Cut %n Contacts", mUIDList.count() );
00219 }
00220 
00221 void CutCommand::unexecute()
00222 {
00223   KABC::Addressee::List::ConstIterator it;
00224   const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00225 
00226   // lock resources
00227   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00228     lock()->lock( (*it).resource() );
00229 
00230   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00231     addressBook()->insertAddressee( *it );
00232     lock()->unlock( (*it).resource() );
00233   }
00234 
00235   mAddresseeList.clear();
00236 
00237   QClipboard *cb = QApplication::clipboard();
00238   kapp->processEvents();
00239   cb->setText( mOldText );
00240 }
00241 
00242 void CutCommand::execute()
00243 {
00244   KABC::Addressee addr;
00245 
00246   QStringList::ConstIterator it;
00247   const QStringList::ConstIterator endIt( mUIDList.end() );
00248   for ( it = mUIDList.begin(); it != endIt; ++it ) {
00249     addr = addressBook()->findByUid( *it );
00250     mAddresseeList.append( addr );
00251     lock()->lock( addr.resource() );
00252   }
00253 
00254   KABC::Addressee::List::ConstIterator addrIt;
00255   const KABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
00256   for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
00257     addressBook()->removeAddressee( *addrIt );
00258     lock()->unlock( addr.resource() );
00259   }
00260 
00261   // Convert to clipboard
00262   mClipText = AddresseeUtil::addresseesToClipboard( mAddresseeList );
00263 
00264   QClipboard *cb = QApplication::clipboard();
00265   mOldText = cb->text();
00266   kapp->processEvents();
00267   cb->setText( mClipText );
00268 }
00269 
00270 CopyToCommand::CopyToCommand( KABC::AddressBook *addressBook, const QStringList &uidList,
00271                                               KABC::Resource *resource )
00272     : Command( addressBook ), mUIDList( uidList ), mResource( resource )
00273 {
00274 }
00275 
00276 QString CopyToCommand::name() const
00277 {
00278     return i18n( "Copy Contact To", "Copy %n Contacts To", mUIDList.count() );
00279 }
00280 
00281 void CopyToCommand::unexecute()
00282 {
00283     KABC::Addressee::List::ConstIterator it;
00284     const KABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00285     //For copy : just remove it from the "copied to" resource.
00286     // lock resources
00287     for ( it = mAddresseeList.begin(); it != endIt; ++it )
00288         lock()->lock( (*it).resource() );
00289 
00290     for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00291         addressBook()->removeAddressee( *it );
00292         lock()->unlock( (*it).resource() );
00293     }
00294 }
00295 
00296 void CopyToCommand::execute()
00297 {
00298   KABLock::self( addressBook() )->lock( mResource );
00299   QStringList::Iterator it( mUIDList.begin() );
00300   const QStringList::Iterator endIt( mUIDList.end() );
00301   while ( it != endIt ) {
00302     KABC::Addressee addr = addressBook()->findByUid( *it++ );
00303     if ( !addr.isEmpty() ) {
00304       KABC::Addressee newAddr( addr );
00305       // We need to set a new uid, otherwise the insert below is
00306       // ignored. This is bad for syncing, but unavoidable, afaiks
00307       newAddr.setUid( KApplication::randomString( 10 ) );
00308       newAddr.setResource( mResource );
00309       addressBook()->insertAddressee( newAddr );
00310       mAddresseeList.append( newAddr );
00311     }
00312   }
00313   KABLock::self( addressBook() )->unlock( mResource );
00314 
00315 }
00316 
00317 MoveToCommand::MoveToCommand( KAB::Core *core, const QStringList &uidList,
00318                                               KABC::Resource *resource )
00319     : Command( core->addressBook() ), mUIDList( uidList ), mResource( resource ), mCore( core )
00320 {
00321 }
00322 
00323 QString MoveToCommand::name() const
00324 {
00325     return i18n( "Move Contact To", "Move %n Contacts To", mUIDList.count() );
00326 }
00327 
00328 void MoveToCommand::unexecute()
00329 {
00330   //For move : remove it from the "copied to" resource and insert it back to "copied from" resource.
00331     KABC::Resource *resource = mCore->requestResource( mCore->widget() );
00332     if ( !resource )
00333       return;
00334     moveContactTo( resource );
00335 }
00336 
00337 void MoveToCommand::execute()
00338 {
00339     moveContactTo( mResource );
00340 }
00341 
00342 void MoveToCommand::moveContactTo( KABC::Resource *resource )
00343 {
00344     KABLock::self( addressBook() )->lock( resource );
00345     QStringList::Iterator it( mUIDList.begin() );
00346     const QStringList::Iterator endIt( mUIDList.end() );
00347     while ( it != endIt ) {
00348         KABC::Addressee addr = addressBook()->findByUid( *it++ );
00349         if ( !addr.isEmpty() ) {
00350             KABC::Addressee newAddr( addr );
00351       // We need to set a new uid, otherwise the insert below is
00352       // ignored. This is bad for syncing, but unavoidable, afaiks
00353             QString uid = KApplication::randomString( 10 );
00354             newAddr.setUid( uid );
00355             newAddr.setResource( resource );
00356             addressBook()->insertAddressee( newAddr );
00357             mAddresseeList.append( newAddr );
00358             mUIDList.append( uid );
00359             const bool inserted = addressBook()->find( newAddr ) != addressBook()->end();
00360             if ( inserted ) {
00361                 KABLock::self( addressBook() )->lock( addr.resource() );
00362                 addressBook()->removeAddressee( addr );
00363                 KABLock::self( addressBook() )->unlock( addr.resource() );
00364             }
00365         }
00366     }
00367     KABLock::self( addressBook() )->unlock( resource );
00368 
00369 }
KDE Home | KDE Accessibility Home | Description of Access Keys