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