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 TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include <tqapplication.h>
00026 #include <tqclipboard.h>
00027 
00028 #include <tdelocale.h>
00029 #include <tdeapplication.h>
00030 
00031 #include <tdeabc/resource.h>
00032 
00033 #include "addresseeutil.h"
00034 #include "addresseeconfig.h"
00035 #include "core.h"
00036 #include "kablock.h"
00037 
00038 #include "undocmds.h"
00039 
00040 bool Command::resourceExist( TDEABC::Resource *resource )
00041 {
00042   TQPtrList<TDEABC::Resource>  lst = addressBook()->resources();
00043   for ( Resource *res = lst.first(); res; res = lst.next() ) {
00044     if ( res == resource )
00045       return true;
00046   }
00047   return false;
00048 }
00049 
00050 DeleteCommand::DeleteCommand( TDEABC::AddressBook *addressBook,
00051                               const TQStringList &uidList)
00052   : Command( addressBook ), mUIDList( uidList )
00053 {
00054 }
00055 
00056 TQString DeleteCommand::name() const
00057 {
00058   return i18n( "Delete Contact", "Delete %n Contacts", mUIDList.count() );
00059 }
00060 
00061 void DeleteCommand::unexecute()
00062 {
00063   // Put it back in the document
00064   TDEABC::Addressee::List::ConstIterator it;
00065   const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00066 
00067   // lock resources
00068   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00069     lock()->lock( (*it).resource() );
00070 
00071   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00072     if ( resourceExist( ( *it ).resource() ) )
00073       addressBook()->insertAddressee( *it );
00074     lock()->unlock( (*it).resource() );
00075   }
00076 
00077   mAddresseeList.clear();
00078 }
00079 
00080 void DeleteCommand::execute()
00081 {
00082   TDEABC::Addressee addr;
00083 
00084   TQStringList::ConstIterator it;
00085   const TQStringList::ConstIterator endIt( mUIDList.end() );
00086   for ( it = mUIDList.begin(); it != endIt; ++it ) {
00087     addr = addressBook()->findByUid( *it );
00088     lock()->lock( addr.resource() );
00089     mAddresseeList.append( addr );
00090     AddresseeConfig cfg( addr );
00091     cfg.remove();
00092   }
00093 
00094   TDEABC::Addressee::List::ConstIterator addrIt;
00095   const TDEABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
00096   for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
00097     if ( resourceExist( ( *addrIt ).resource() ) )
00098       addressBook()->removeAddressee( *addrIt );
00099     lock()->unlock( (*addrIt).resource() );
00100   }
00101 }
00102 
00103 
00104 PasteCommand::PasteCommand( KAB::Core *core, const TDEABC::Addressee::List &addressees )
00105   : Command( core->addressBook() ), mAddresseeList( addressees ), mCore( core )
00106 {
00107 }
00108 
00109 TQString PasteCommand::name() const
00110 {
00111   return i18n( "Paste Contact", "Paste %n Contacts", mAddresseeList.count() );
00112 }
00113 
00114 void PasteCommand::unexecute()
00115 {
00116   TDEABC::Addressee::List::ConstIterator it;
00117   const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00118 
00119   // lock resources
00120   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00121     lock()->lock( (*it).resource() );
00122 
00123   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00124     if ( resourceExist( ( *it ).resource() ) )
00125       addressBook()->removeAddressee( *it );
00126     lock()->unlock( (*it).resource() );
00127   }
00128 }
00129 
00130 void PasteCommand::execute()
00131 {
00132   TQStringList uids;
00133 
00134   TDEABC::Addressee::List::ConstIterator constIt;
00135   const TDEABC::Addressee::List::ConstIterator constEndIt( mAddresseeList.end() );
00136 
00137   // lock resources
00138   for ( constIt = mAddresseeList.begin(); constIt != constEndIt; ++constIt )
00139     lock()->lock( (*constIt).resource() );
00140 
00141   TDEABC::Addressee::List::Iterator it;
00142   const TDEABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
00143   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00144     if ( resourceExist( ( *it ).resource() ) ) {
00145 
00150       (*it).setUid( TDEApplication::randomString( 10 ) );
00151       uids.append( (*it).uid() );
00152       addressBook()->insertAddressee( *it );
00153     }
00154     lock()->unlock( (*it).resource() );
00155   }
00156 
00157 }
00158 
00159 
00160 NewCommand::NewCommand( TDEABC::AddressBook *addressBook, const TDEABC::Addressee::List &addressees )
00161   : Command( addressBook ), mAddresseeList( addressees )
00162 {
00163 }
00164 
00165 TQString NewCommand::name() const
00166 {
00167   return i18n( "New Contact", "New %n Contacts", mAddresseeList.count() );
00168 }
00169 
00170 void NewCommand::unexecute()
00171 {
00172   TDEABC::Addressee::List::ConstIterator it;
00173   const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00174 
00175   // lock resources
00176   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00177     lock()->lock( (*it).resource() );
00178 
00179   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00180     if ( resourceExist( ( *it ).resource() ) )
00181       addressBook()->removeAddressee( *it );
00182     lock()->unlock( (*it).resource() );
00183   }
00184 }
00185 
00186 void NewCommand::execute()
00187 {
00188   TDEABC::Addressee::List::Iterator it;
00189   const TDEABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
00190 
00191   // lock resources
00192   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00193     lock()->lock( (*it).resource() );
00194 
00195   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00196     if ( resourceExist( ( *it ).resource() ) )
00197       addressBook()->insertAddressee( *it );
00198     lock()->unlock( (*it).resource() );
00199   }
00200 }
00201 
00202 
00203 EditCommand::EditCommand( TDEABC::AddressBook *addressBook,
00204                           const TDEABC::Addressee &oldAddressee,
00205                           const TDEABC::Addressee &newAddressee )
00206   : Command( addressBook ),
00207     mOldAddressee( oldAddressee ), mNewAddressee( newAddressee )
00208 {
00209 }
00210 
00211 TQString EditCommand::name() const
00212 {
00213   return i18n( "Edit Contact" );
00214 }
00215 
00216 void EditCommand::unexecute()
00217 {
00218   if ( resourceExist( mOldAddressee.resource() ) )
00219   {
00220     lock()->lock( mOldAddressee.resource() );
00221     addressBook()->insertAddressee( mOldAddressee );
00222     lock()->unlock( mOldAddressee.resource() );
00223   }
00224 }
00225 
00226 void EditCommand::execute()
00227 {
00228   if ( resourceExist( mNewAddressee.resource() ) )
00229   {
00230     lock()->lock( mNewAddressee.resource() );
00231     addressBook()->insertAddressee( mNewAddressee );
00232     lock()->unlock( mNewAddressee.resource() );
00233   }
00234 }
00235 
00236 
00237 CutCommand::CutCommand( TDEABC::AddressBook *addressBook, const TQStringList &uidList )
00238   : Command( addressBook ), mUIDList( uidList )
00239 {
00240 }
00241 
00242 TQString CutCommand::name() const
00243 {
00244   return i18n( "Cut Contact", "Cut %n Contacts", mUIDList.count() );
00245 }
00246 
00247 void CutCommand::unexecute()
00248 {
00249   TDEABC::Addressee::List::ConstIterator it;
00250   const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00251 
00252   // lock resources
00253   for ( it = mAddresseeList.begin(); it != endIt; ++it )
00254     lock()->lock( (*it).resource() );
00255 
00256   for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00257     if ( resourceExist( ( *it ).resource() ) )
00258       addressBook()->insertAddressee( *it );
00259     lock()->unlock( (*it).resource() );
00260   }
00261 
00262   mAddresseeList.clear();
00263 
00264   TQClipboard *cb = TQApplication::clipboard();
00265   kapp->processEvents();
00266   cb->setText( mOldText );
00267 }
00268 
00269 void CutCommand::execute()
00270 {
00271   TDEABC::Addressee addr;
00272 
00273   TQStringList::ConstIterator it;
00274   const TQStringList::ConstIterator endIt( mUIDList.end() );
00275   for ( it = mUIDList.begin(); it != endIt; ++it ) {
00276     addr = addressBook()->findByUid( *it );
00277     mAddresseeList.append( addr );
00278     lock()->lock( addr.resource() );
00279   }
00280 
00281   TDEABC::Addressee::List::ConstIterator addrIt;
00282   const TDEABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
00283   for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
00284     if ( resourceExist( ( *addrIt ).resource() ) )
00285       addressBook()->removeAddressee( *addrIt );
00286     lock()->unlock( addr.resource() );
00287   }
00288 
00289   // Convert to clipboard
00290   mClipText = AddresseeUtil::addresseesToClipboard( mAddresseeList );
00291 
00292   TQClipboard *cb = TQApplication::clipboard();
00293   mOldText = cb->text();
00294   kapp->processEvents();
00295 #if defined(KABC_VCARD_ENCODING_FIX)
00296   cb->setText( TQString::fromUtf8( mClipText.data() ) );
00297 #else
00298   cb->setText( mClipText );
00299 #endif
00300 }
00301 
00302 CopyToCommand::CopyToCommand( TDEABC::AddressBook *addressBook, const TQStringList &uidList,
00303                                               TDEABC::Resource *resource )
00304     : Command( addressBook ), mUIDList( uidList ), mResource( resource )
00305 {
00306 }
00307 
00308 TQString CopyToCommand::name() const
00309 {
00310     return i18n( "Copy Contact To", "Copy %n Contacts To", mUIDList.count() );
00311 }
00312 
00313 void CopyToCommand::unexecute()
00314 {
00315     TDEABC::Addressee::List::ConstIterator it;
00316     const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
00317     //For copy : just remove it from the "copied to" resource.
00318     // lock resources
00319     for ( it = mAddresseeList.begin(); it != endIt; ++it )
00320         lock()->lock( (*it).resource() );
00321 
00322     for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
00323       if ( resourceExist( ( *it ).resource() ) )
00324         addressBook()->removeAddressee( *it );
00325       lock()->unlock( (*it).resource() );
00326     }
00327 }
00328 
00329 void CopyToCommand::execute()
00330 {
00331   KABLock::self( addressBook() )->lock( mResource );
00332   TQStringList::Iterator it( mUIDList.begin() );
00333   const TQStringList::Iterator endIt( mUIDList.end() );
00334   while ( it != endIt ) {
00335     TDEABC::Addressee addr = addressBook()->findByUid( *it++ );
00336     if ( !addr.isEmpty() ) {
00337       TDEABC::Addressee newAddr( addr );
00338       // We need to set a new uid, otherwise the insert below is
00339       // ignored. This is bad for syncing, but unavoidable, afaiks
00340       newAddr.setUid( TDEApplication::randomString( 10 ) );
00341       newAddr.setResource( mResource );
00342       if ( resourceExist( newAddr.resource() ) )
00343         addressBook()->insertAddressee( newAddr );
00344       mAddresseeList.append( newAddr );
00345     }
00346   }
00347   KABLock::self( addressBook() )->unlock( mResource );
00348 
00349 }
00350 
00351 MoveToCommand::MoveToCommand( KAB::Core *core, const TQStringList &uidList,
00352                                               TDEABC::Resource *resource )
00353     : Command( core->addressBook() ), mUIDList( uidList ), mResource( resource ), mCore( core )
00354 {
00355 }
00356 
00357 TQString MoveToCommand::name() const
00358 {
00359     return i18n( "Move Contact To", "Move %n Contacts To", mUIDList.count() );
00360 }
00361 
00362 void MoveToCommand::unexecute()
00363 {
00364   //For move : remove it from the "copied to" resource and insert it back to "copied from" resource.
00365     TDEABC::Resource *resource = mCore->requestResource( mCore->widget() );
00366     if ( !resource )
00367       return;
00368     moveContactTo( resource );
00369 }
00370 
00371 void MoveToCommand::execute()
00372 {
00373     moveContactTo( mResource );
00374 }
00375 
00376 void MoveToCommand::moveContactTo( TDEABC::Resource *resource )
00377 {
00378     KABLock::self( addressBook() )->lock( resource );
00379     TQStringList::Iterator it( mUIDList.begin() );
00380     const TQStringList::Iterator endIt( mUIDList.end() );
00381     while ( it != endIt ) {
00382         TDEABC::Addressee addr = addressBook()->findByUid( *it++ );
00383         if ( !addr.isEmpty() ) {
00384             TDEABC::Addressee newAddr( addr );
00385       // We need to set a new uid, otherwise the insert below is
00386       // ignored. This is bad for syncing, but unavoidable, afaiks
00387             TQString uid = TDEApplication::randomString( 10 );
00388             newAddr.setUid( uid );
00389             newAddr.setResource( resource );
00390             if ( resourceExist( newAddr.resource() ) )
00391               addressBook()->insertAddressee( newAddr );
00392             mAddresseeList.append( newAddr );
00393             mUIDList.append( uid );
00394             const bool inserted = addressBook()->find( newAddr ) != addressBook()->end();
00395             if ( inserted ) {
00396               if ( resourceExist( addr.resource() ) ) {
00397                 KABLock::self( addressBook() )->lock( addr.resource() );
00398                 addressBook()->removeAddressee( addr );
00399                 KABLock::self( addressBook() )->unlock( addr.resource() );
00400               }
00401             }
00402         }
00403     }
00404     KABLock::self( addressBook() )->unlock( resource );
00405 
00406 }