25 #include <tqapplication.h>
26 #include <tqclipboard.h>
28 #include <tdelocale.h>
29 #include <tdeapplication.h>
31 #include <tdeabc/resource.h>
33 #include "addresseeutil.h"
34 #include "addresseeconfig.h"
40 bool Command::resourceExist( TDEABC::Resource *resource )
42 TQPtrList<TDEABC::Resource> lst = addressBook()->resources();
43 for ( Resource *res = lst.first(); res; res = lst.next() ) {
44 if ( res == resource )
50 DeleteCommand::DeleteCommand( TDEABC::AddressBook *addressBook,
51 const TQStringList &uidList)
52 : Command( addressBook ), mUIDList( uidList )
56 TQString DeleteCommand::name()
const
58 return i18n(
"Delete Contact",
"Delete %n Contacts", mUIDList.count() );
61 void DeleteCommand::unexecute()
64 TDEABC::Addressee::List::ConstIterator it;
65 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
68 for ( it = mAddresseeList.begin(); it != endIt; ++it )
69 lock()->lock( (*it).resource() );
71 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
72 if ( resourceExist( ( *it ).resource() ) )
73 addressBook()->insertAddressee( *it );
74 lock()->unlock( (*it).resource() );
77 mAddresseeList.clear();
80 void DeleteCommand::execute()
82 TDEABC::Addressee addr;
84 TQStringList::ConstIterator it;
85 const TQStringList::ConstIterator endIt( mUIDList.end() );
86 for ( it = mUIDList.begin(); it != endIt; ++it ) {
87 addr = addressBook()->findByUid( *it );
88 lock()->lock( addr.resource() );
89 mAddresseeList.append( addr );
90 AddresseeConfig cfg( addr );
94 TDEABC::Addressee::List::ConstIterator addrIt;
95 const TDEABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
96 for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
97 if ( resourceExist( ( *addrIt ).resource() ) )
98 addressBook()->removeAddressee( *addrIt );
99 lock()->unlock( (*addrIt).resource() );
104 PasteCommand::PasteCommand( KAB::Core *core,
const TDEABC::Addressee::List &addressees )
105 : Command( core->addressBook() ), mAddresseeList( addressees ), mCore( core )
109 TQString PasteCommand::name()
const
111 return i18n(
"Paste Contact",
"Paste %n Contacts", mAddresseeList.count() );
114 void PasteCommand::unexecute()
116 TDEABC::Addressee::List::ConstIterator it;
117 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
120 for ( it = mAddresseeList.begin(); it != endIt; ++it )
121 lock()->lock( (*it).resource() );
123 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
124 if ( resourceExist( ( *it ).resource() ) )
125 addressBook()->removeAddressee( *it );
126 lock()->unlock( (*it).resource() );
130 void PasteCommand::execute()
134 TDEABC::Addressee::List::ConstIterator constIt;
135 const TDEABC::Addressee::List::ConstIterator constEndIt( mAddresseeList.end() );
138 for ( constIt = mAddresseeList.begin(); constIt != constEndIt; ++constIt )
139 lock()->lock( (*constIt).resource() );
141 TDEABC::Addressee::List::Iterator it;
142 const TDEABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
143 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
144 if ( resourceExist( ( *it ).resource() ) ) {
150 (*it).setUid( TDEApplication::randomString( 10 ) );
151 uids.append( (*it).uid() );
152 addressBook()->insertAddressee( *it );
154 lock()->unlock( (*it).resource() );
160 NewCommand::NewCommand( TDEABC::AddressBook *addressBook,
const TDEABC::Addressee::List &addressees )
161 : Command( addressBook ), mAddresseeList( addressees )
165 TQString NewCommand::name()
const
167 return i18n(
"New Contact",
"New %n Contacts", mAddresseeList.count() );
170 void NewCommand::unexecute()
172 TDEABC::Addressee::List::ConstIterator it;
173 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
176 for ( it = mAddresseeList.begin(); it != endIt; ++it )
177 lock()->lock( (*it).resource() );
179 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
180 if ( resourceExist( ( *it ).resource() ) )
181 addressBook()->removeAddressee( *it );
182 lock()->unlock( (*it).resource() );
186 void NewCommand::execute()
188 TDEABC::Addressee::List::Iterator it;
189 const TDEABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
192 for ( it = mAddresseeList.begin(); it != endIt; ++it )
193 lock()->lock( (*it).resource() );
195 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
196 if ( resourceExist( ( *it ).resource() ) )
197 addressBook()->insertAddressee( *it );
198 lock()->unlock( (*it).resource() );
203 EditCommand::EditCommand( TDEABC::AddressBook *addressBook,
204 const TDEABC::Addressee &oldAddressee,
205 const TDEABC::Addressee &newAddressee )
206 : Command( addressBook ),
207 mOldAddressee( oldAddressee ), mNewAddressee( newAddressee )
211 TQString EditCommand::name()
const
213 return i18n(
"Edit Contact" );
216 void EditCommand::unexecute()
218 if ( resourceExist( mOldAddressee.resource() ) )
220 lock()->lock( mOldAddressee.resource() );
221 addressBook()->insertAddressee( mOldAddressee );
222 lock()->unlock( mOldAddressee.resource() );
226 void EditCommand::execute()
228 if ( resourceExist( mNewAddressee.resource() ) )
230 lock()->lock( mNewAddressee.resource() );
231 addressBook()->insertAddressee( mNewAddressee );
232 lock()->unlock( mNewAddressee.resource() );
237 CutCommand::CutCommand( TDEABC::AddressBook *addressBook,
const TQStringList &uidList )
238 : Command( addressBook ), mUIDList( uidList )
242 TQString CutCommand::name()
const
244 return i18n(
"Cut Contact",
"Cut %n Contacts", mUIDList.count() );
247 void CutCommand::unexecute()
249 TDEABC::Addressee::List::ConstIterator it;
250 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
253 for ( it = mAddresseeList.begin(); it != endIt; ++it )
254 lock()->lock( (*it).resource() );
256 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
257 if ( resourceExist( ( *it ).resource() ) )
258 addressBook()->insertAddressee( *it );
259 lock()->unlock( (*it).resource() );
262 mAddresseeList.clear();
264 TQClipboard *cb = TQApplication::clipboard();
265 kapp->processEvents();
266 cb->setText( mOldText );
269 void CutCommand::execute()
271 TDEABC::Addressee addr;
273 TQStringList::ConstIterator it;
274 const TQStringList::ConstIterator endIt( mUIDList.end() );
275 for ( it = mUIDList.begin(); it != endIt; ++it ) {
276 addr = addressBook()->findByUid( *it );
277 mAddresseeList.append( addr );
278 lock()->lock( addr.resource() );
281 TDEABC::Addressee::List::ConstIterator addrIt;
282 const TDEABC::Addressee::List::ConstIterator addrEndIt( mAddresseeList.end() );
283 for ( addrIt = mAddresseeList.begin(); addrIt != addrEndIt; ++addrIt ) {
284 if ( resourceExist( ( *addrIt ).resource() ) )
285 addressBook()->removeAddressee( *addrIt );
286 lock()->unlock( addr.resource() );
292 TQClipboard *cb = TQApplication::clipboard();
293 mOldText = cb->text();
294 kapp->processEvents();
295 #if defined(KABC_VCARD_ENCODING_FIX)
296 cb->setText( TQString::fromUtf8( mClipText.data() ) );
298 cb->setText( mClipText );
302 CopyToCommand::CopyToCommand( TDEABC::AddressBook *addressBook,
const TQStringList &uidList,
303 TDEABC::Resource *resource )
304 : Command( addressBook ), mUIDList( uidList ), mResource( resource )
308 TQString CopyToCommand::name()
const
310 return i18n(
"Copy Contact To",
"Copy %n Contacts To", mUIDList.count() );
313 void CopyToCommand::unexecute()
315 TDEABC::Addressee::List::ConstIterator it;
316 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
319 for ( it = mAddresseeList.begin(); it != endIt; ++it )
320 lock()->lock( (*it).resource() );
322 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
323 if ( resourceExist( ( *it ).resource() ) )
324 addressBook()->removeAddressee( *it );
325 lock()->unlock( (*it).resource() );
329 void CopyToCommand::execute()
331 KABLock::self( addressBook() )->lock( mResource );
332 TQStringList::Iterator it( mUIDList.begin() );
333 const TQStringList::Iterator endIt( mUIDList.end() );
334 while ( it != endIt ) {
335 TDEABC::Addressee addr = addressBook()->findByUid( *it++ );
336 if ( !addr.isEmpty() ) {
337 TDEABC::Addressee newAddr( addr );
340 newAddr.setUid( TDEApplication::randomString( 10 ) );
341 newAddr.setResource( mResource );
342 if ( resourceExist( newAddr.resource() ) )
343 addressBook()->insertAddressee( newAddr );
344 mAddresseeList.append( newAddr );
347 KABLock::self( addressBook() )->unlock( mResource );
351 MoveToCommand::MoveToCommand( KAB::Core *core,
const TQStringList &uidList,
352 TDEABC::Resource *resource )
353 : Command( core->addressBook() ), mUIDList( uidList ), mResource( resource ), mCore( core )
357 TQString MoveToCommand::name()
const
359 return i18n(
"Move Contact To",
"Move %n Contacts To", mUIDList.count() );
362 void MoveToCommand::unexecute()
365 TDEABC::Resource *resource = mCore->requestResource( mCore->widget() );
368 moveContactTo( resource );
371 void MoveToCommand::execute()
373 moveContactTo( mResource );
376 void MoveToCommand::moveContactTo( TDEABC::Resource *resource )
378 KABLock::self( addressBook() )->lock( resource );
379 TQStringList::Iterator it( mUIDList.begin() );
380 const TQStringList::Iterator endIt( mUIDList.end() );
381 while ( it != endIt ) {
382 TDEABC::Addressee addr = addressBook()->findByUid( *it++ );
383 if ( !addr.isEmpty() ) {
384 TDEABC::Addressee newAddr( addr );
387 TQString uid = TDEApplication::randomString( 10 );
388 newAddr.setUid( uid );
389 newAddr.setResource( resource );
390 if ( resourceExist( newAddr.resource() ) )
391 addressBook()->insertAddressee( newAddr );
392 mAddresseeList.append( newAddr );
393 mUIDList.append( uid );
394 const bool inserted = addressBook()->find( newAddr ) != addressBook()->end();
396 if ( resourceExist( addr.resource() ) ) {
397 KABLock::self( addressBook() )->lock( addr.resource() );
398 addressBook()->removeAddressee( addr );
399 KABLock::self( addressBook() )->unlock( addr.resource() );
404 KABLock::self( addressBook() )->unlock( resource );
static TQString addresseesToClipboard(const TDEABC::Addressee::List &addrList)
Same as above function, except that an entire list of TDEABC::Addressee objects will be converted to ...