kaddressbook

undocmds.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (C) 1999 Don Sanders <sanders@kde.org>
4 2005 Tobias Koenig <tokoe@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include <tqapplication.h>
26#include <tqclipboard.h>
27
28#include <tdelocale.h>
29#include <tdeapplication.h>
30
31#include <tdeabc/resource.h>
32
33#include "addresseeutil.h"
34#include "addresseeconfig.h"
35#include "core.h"
36#include "kablock.h"
37
38#include "undocmds.h"
39
40bool Command::resourceExist( TDEABC::Resource *resource )
41{
42 TQPtrList<TDEABC::Resource> lst = addressBook()->resources();
43 for ( Resource *res = lst.first(); res; res = lst.next() ) {
44 if ( res == resource )
45 return true;
46 }
47 return false;
48}
49
50DeleteCommand::DeleteCommand( TDEABC::AddressBook *addressBook,
51 const TQStringList &uidList)
52 : Command( addressBook ), mUIDList( uidList )
53{
54}
55
56TQString DeleteCommand::name() const
57{
58 return i18n( "Delete Contact", "Delete %n Contacts", mUIDList.count() );
59}
60
61void DeleteCommand::unexecute()
62{
63 // Put it back in the document
64 TDEABC::Addressee::List::ConstIterator it;
65 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
66
67 // lock resources
68 for ( it = mAddresseeList.begin(); it != endIt; ++it )
69 lock()->lock( (*it).resource() );
70
71 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
72 if ( resourceExist( ( *it ).resource() ) )
73 addressBook()->insertAddressee( *it );
74 lock()->unlock( (*it).resource() );
75 }
76
77 mAddresseeList.clear();
78}
79
80void DeleteCommand::execute()
81{
82 TDEABC::Addressee addr;
83
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 );
91 cfg.remove();
92 }
93
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() );
100 }
101}
102
103
104PasteCommand::PasteCommand( KAB::Core *core, const TDEABC::Addressee::List &addressees )
105 : Command( core->addressBook() ), mAddresseeList( addressees ), mCore( core )
106{
107}
108
109TQString PasteCommand::name() const
110{
111 return i18n( "Paste Contact", "Paste %n Contacts", mAddresseeList.count() );
112}
113
114void PasteCommand::unexecute()
115{
116 TDEABC::Addressee::List::ConstIterator it;
117 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
118
119 // lock resources
120 for ( it = mAddresseeList.begin(); it != endIt; ++it )
121 lock()->lock( (*it).resource() );
122
123 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
124 if ( resourceExist( ( *it ).resource() ) )
125 addressBook()->removeAddressee( *it );
126 lock()->unlock( (*it).resource() );
127 }
128}
129
130void PasteCommand::execute()
131{
132 TQStringList uids;
133
134 TDEABC::Addressee::List::ConstIterator constIt;
135 const TDEABC::Addressee::List::ConstIterator constEndIt( mAddresseeList.end() );
136
137 // lock resources
138 for ( constIt = mAddresseeList.begin(); constIt != constEndIt; ++constIt )
139 lock()->lock( (*constIt).resource() );
140
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() ) ) {
145
150 (*it).setUid( TDEApplication::randomString( 10 ) );
151 uids.append( (*it).uid() );
152 addressBook()->insertAddressee( *it );
153 }
154 lock()->unlock( (*it).resource() );
155 }
156
157}
158
159
160NewCommand::NewCommand( TDEABC::AddressBook *addressBook, const TDEABC::Addressee::List &addressees )
161 : Command( addressBook ), mAddresseeList( addressees )
162{
163}
164
165TQString NewCommand::name() const
166{
167 return i18n( "New Contact", "New %n Contacts", mAddresseeList.count() );
168}
169
170void NewCommand::unexecute()
171{
172 TDEABC::Addressee::List::ConstIterator it;
173 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
174
175 // lock resources
176 for ( it = mAddresseeList.begin(); it != endIt; ++it )
177 lock()->lock( (*it).resource() );
178
179 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
180 if ( resourceExist( ( *it ).resource() ) )
181 addressBook()->removeAddressee( *it );
182 lock()->unlock( (*it).resource() );
183 }
184}
185
186void NewCommand::execute()
187{
188 TDEABC::Addressee::List::Iterator it;
189 const TDEABC::Addressee::List::Iterator endIt( mAddresseeList.end() );
190
191 // lock resources
192 for ( it = mAddresseeList.begin(); it != endIt; ++it )
193 lock()->lock( (*it).resource() );
194
195 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
196 if ( resourceExist( ( *it ).resource() ) )
197 addressBook()->insertAddressee( *it );
198 lock()->unlock( (*it).resource() );
199 }
200}
201
202
203EditCommand::EditCommand( TDEABC::AddressBook *addressBook,
204 const TDEABC::Addressee &oldAddressee,
205 const TDEABC::Addressee &newAddressee )
206 : Command( addressBook ),
207 mOldAddressee( oldAddressee ), mNewAddressee( newAddressee )
208{
209}
210
211TQString EditCommand::name() const
212{
213 return i18n( "Edit Contact" );
214}
215
216void EditCommand::unexecute()
217{
218 if ( resourceExist( mOldAddressee.resource() ) )
219 {
220 lock()->lock( mOldAddressee.resource() );
221 addressBook()->insertAddressee( mOldAddressee );
222 lock()->unlock( mOldAddressee.resource() );
223 }
224}
225
226void EditCommand::execute()
227{
228 if ( resourceExist( mNewAddressee.resource() ) )
229 {
230 lock()->lock( mNewAddressee.resource() );
231 addressBook()->insertAddressee( mNewAddressee );
232 lock()->unlock( mNewAddressee.resource() );
233 }
234}
235
236
237CutCommand::CutCommand( TDEABC::AddressBook *addressBook, const TQStringList &uidList )
238 : Command( addressBook ), mUIDList( uidList )
239{
240}
241
242TQString CutCommand::name() const
243{
244 return i18n( "Cut Contact", "Cut %n Contacts", mUIDList.count() );
245}
246
247void CutCommand::unexecute()
248{
249 TDEABC::Addressee::List::ConstIterator it;
250 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
251
252 // lock resources
253 for ( it = mAddresseeList.begin(); it != endIt; ++it )
254 lock()->lock( (*it).resource() );
255
256 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
257 if ( resourceExist( ( *it ).resource() ) )
258 addressBook()->insertAddressee( *it );
259 lock()->unlock( (*it).resource() );
260 }
261
262 mAddresseeList.clear();
263
264 TQClipboard *cb = TQApplication::clipboard();
265 tdeApp->processEvents();
266 cb->setText( mOldText );
267}
268
269void CutCommand::execute()
270{
271 TDEABC::Addressee addr;
272
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() );
279 }
280
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() );
287 }
288
289 // Convert to clipboard
290 mClipText = AddresseeUtil::addresseesToClipboard( mAddresseeList );
291
292 TQClipboard *cb = TQApplication::clipboard();
293 mOldText = cb->text();
294 tdeApp->processEvents();
295#if defined(KABC_VCARD_ENCODING_FIX)
296 cb->setText( TQString::fromUtf8( mClipText.data() ) );
297#else
298 cb->setText( mClipText );
299#endif
300}
301
302CopyToCommand::CopyToCommand( TDEABC::AddressBook *addressBook, const TQStringList &uidList,
303 TDEABC::Resource *resource )
304 : Command( addressBook ), mUIDList( uidList ), mResource( resource )
305{
306}
307
308TQString CopyToCommand::name() const
309{
310 return i18n( "Copy Contact To", "Copy %n Contacts To", mUIDList.count() );
311}
312
313void CopyToCommand::unexecute()
314{
315 TDEABC::Addressee::List::ConstIterator it;
316 const TDEABC::Addressee::List::ConstIterator endIt( mAddresseeList.end() );
317 //For copy : just remove it from the "copied to" resource.
318 // lock resources
319 for ( it = mAddresseeList.begin(); it != endIt; ++it )
320 lock()->lock( (*it).resource() );
321
322 for ( it = mAddresseeList.begin(); it != endIt; ++it ) {
323 if ( resourceExist( ( *it ).resource() ) )
324 addressBook()->removeAddressee( *it );
325 lock()->unlock( (*it).resource() );
326 }
327}
328
329void CopyToCommand::execute()
330{
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 );
338 // We need to set a new uid, otherwise the insert below is
339 // ignored. This is bad for syncing, but unavoidable, afaiks
340 newAddr.setUid( TDEApplication::randomString( 10 ) );
341 newAddr.setResource( mResource );
342 if ( resourceExist( newAddr.resource() ) )
343 addressBook()->insertAddressee( newAddr );
344 mAddresseeList.append( newAddr );
345 }
346 }
347 KABLock::self( addressBook() )->unlock( mResource );
348
349}
350
351MoveToCommand::MoveToCommand( KAB::Core *core, const TQStringList &uidList,
352 TDEABC::Resource *resource )
353 : Command( core->addressBook() ), mUIDList( uidList ), mResource( resource ), mCore( core )
354{
355}
356
357TQString MoveToCommand::name() const
358{
359 return i18n( "Move Contact To", "Move %n Contacts To", mUIDList.count() );
360}
361
362void MoveToCommand::unexecute()
363{
364 //For move : remove it from the "copied to" resource and insert it back to "copied from" resource.
365 TDEABC::Resource *resource = mCore->requestResource( mCore->widget() );
366 if ( !resource )
367 return;
368 moveContactTo( resource );
369}
370
371void MoveToCommand::execute()
372{
373 moveContactTo( mResource );
374}
375
376void MoveToCommand::moveContactTo( TDEABC::Resource *resource )
377{
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 );
385 // We need to set a new uid, otherwise the insert below is
386 // ignored. This is bad for syncing, but unavoidable, afaiks
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();
395 if ( inserted ) {
396 if ( resourceExist( addr.resource() ) ) {
397 KABLock::self( addressBook() )->lock( addr.resource() );
398 addressBook()->removeAddressee( addr );
399 KABLock::self( addressBook() )->unlock( addr.resource() );
400 }
401 }
402 }
403 }
404 KABLock::self( addressBook() )->unlock( resource );
405
406}
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 ...