-rw-r--r-- | kaddressbook/undocmds.cpp | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/kaddressbook/undocmds.cpp b/kaddressbook/undocmds.cpp new file mode 100644 index 0000000..db773be --- a/dev/null +++ b/kaddressbook/undocmds.cpp | |||
@@ -0,0 +1,240 @@ | |||
1 | /* | ||
2 | This file is part of KAddressBook. | ||
3 | Copyright (C) 1999 Don Sanders <sanders@kde.org> | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | |||
19 | As a special exception, permission is given to link this program | ||
20 | with any edition of Qt, and distribute the resulting executable, | ||
21 | without including the source code for Qt in the source distribution. | ||
22 | */ | ||
23 | |||
24 | #include <qtextstream.h> | ||
25 | |||
26 | #include <qapplication.h> | ||
27 | #include <qclipboard.h> | ||
28 | |||
29 | #include <klocale.h> | ||
30 | #include <kdebug.h> | ||
31 | #include <kapplication.h> | ||
32 | #include <kabc/addressbook.h> | ||
33 | |||
34 | #include "addresseeutil.h" | ||
35 | #include "addresseeconfig.h" | ||
36 | #include "kabcore.h" | ||
37 | |||
38 | #include "undocmds.h" | ||
39 | |||
40 | ///////////////////////////////// | ||
41 | // PwDelete Methods | ||
42 | |||
43 | PwDeleteCommand::PwDeleteCommand(KABC::AddressBook *doc, | ||
44 | const QStringList &uidList) | ||
45 | : Command(), mDocument(doc), mAddresseeList(), mUidList(uidList) | ||
46 | { | ||
47 | redo(); | ||
48 | } | ||
49 | |||
50 | PwDeleteCommand::~PwDeleteCommand() | ||
51 | { | ||
52 | } | ||
53 | |||
54 | QString PwDeleteCommand::name() | ||
55 | { | ||
56 | return i18n( "Delete" ); | ||
57 | } | ||
58 | |||
59 | void PwDeleteCommand::undo() | ||
60 | { | ||
61 | // Put it back in the document | ||
62 | KABC::Addressee::List::Iterator iter; | ||
63 | for (iter = mAddresseeList.begin(); iter != mAddresseeList.end(); ++iter) | ||
64 | { | ||
65 | mDocument->insertAddressee(*iter); | ||
66 | } | ||
67 | |||
68 | mAddresseeList.clear(); | ||
69 | } | ||
70 | |||
71 | void PwDeleteCommand::redo() | ||
72 | { | ||
73 | // Just remove it from the document. This is enough to make the user | ||
74 | // Think the item has been deleted | ||
75 | KABC::Addressee a; | ||
76 | QStringList::Iterator iter; | ||
77 | for (iter = mUidList.begin(); iter != mUidList.end(); ++iter) | ||
78 | { | ||
79 | a = mDocument->findByUid(*iter); | ||
80 | mDocument->removeAddressee(a); | ||
81 | mAddresseeList.append(a); | ||
82 | AddresseeConfig::instance()->remove(a.uid()); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | ///////////////////////////////// | ||
87 | // PwPaste Methods | ||
88 | |||
89 | PwPasteCommand::PwPasteCommand( KABCore *core, const KABC::Addressee::List &list ) | ||
90 | : Command(), mCore( core ), mAddresseeList( list ) | ||
91 | { | ||
92 | redo(); | ||
93 | } | ||
94 | |||
95 | QString PwPasteCommand::name() | ||
96 | { | ||
97 | return i18n( "Paste" ); | ||
98 | } | ||
99 | |||
100 | void PwPasteCommand::undo() | ||
101 | { | ||
102 | KABC::Addressee::List::Iterator it; | ||
103 | for ( it = mAddresseeList.begin(); it != mAddresseeList.end(); ++it ) | ||
104 | mCore->addressBook()->removeAddressee( *it ); | ||
105 | } | ||
106 | |||
107 | void PwPasteCommand::redo() | ||
108 | { | ||
109 | QStringList uids; | ||
110 | KABC::Addressee::List::Iterator it; | ||
111 | for ( it = mAddresseeList.begin(); it != mAddresseeList.end(); ++it ) { | ||
112 | /* we have to set a new uid for the contact, otherwise insertAddressee() | ||
113 | ignore it. | ||
114 | */ | ||
115 | (*it).setUid( KApplication::randomString( 10 ) ); | ||
116 | uids.append( (*it).uid() ); | ||
117 | mCore->addressBook()->insertAddressee( *it ); | ||
118 | } | ||
119 | |||
120 | QStringList::Iterator uidIt; | ||
121 | for ( uidIt = uids.begin(); uidIt != uids.end(); ++uidIt ) | ||
122 | mCore->editContact( *uidIt ); | ||
123 | } | ||
124 | |||
125 | ///////////////////////////////// | ||
126 | // PwNew Methods | ||
127 | |||
128 | PwNewCommand::PwNewCommand( KABC::AddressBook *doc, const KABC::Addressee &a ) | ||
129 | : Command(), mDocument( doc ), mA( a ) | ||
130 | { | ||
131 | mDocument->insertAddressee(mA); | ||
132 | } | ||
133 | |||
134 | PwNewCommand::~PwNewCommand() | ||
135 | { | ||
136 | } | ||
137 | |||
138 | QString PwNewCommand::name() | ||
139 | { | ||
140 | return i18n( "New Contact" ); | ||
141 | } | ||
142 | |||
143 | void PwNewCommand::undo() | ||
144 | { | ||
145 | mDocument->removeAddressee( mA ); | ||
146 | } | ||
147 | |||
148 | void PwNewCommand::redo() | ||
149 | { | ||
150 | mDocument->insertAddressee( mA ); | ||
151 | } | ||
152 | |||
153 | ///////////////////////////////// | ||
154 | // PwEdit Methods | ||
155 | |||
156 | PwEditCommand::PwEditCommand(KABC::AddressBook *doc, | ||
157 | const KABC::Addressee &oldA, | ||
158 | const KABC::Addressee &newA ) | ||
159 | : Command(), mDocument(doc), mOldA(oldA), mNewA(newA) | ||
160 | { | ||
161 | redo(); | ||
162 | } | ||
163 | |||
164 | PwEditCommand::~PwEditCommand() | ||
165 | { | ||
166 | } | ||
167 | |||
168 | QString PwEditCommand::name() | ||
169 | { | ||
170 | return i18n( "Entry Edit" ); | ||
171 | } | ||
172 | |||
173 | void PwEditCommand::undo() | ||
174 | { | ||
175 | mDocument->insertAddressee(mOldA); | ||
176 | } | ||
177 | |||
178 | void PwEditCommand::redo() | ||
179 | { | ||
180 | mDocument->insertAddressee(mNewA); | ||
181 | } | ||
182 | |||
183 | ///////////////////////////////// | ||
184 | // PwCut Methods | ||
185 | |||
186 | PwCutCommand::PwCutCommand(KABC::AddressBook *doc, const QStringList &uidList) | ||
187 | : Command(), mDocument(doc), mAddresseeList(), mUidList(uidList), | ||
188 | mClipText(), mOldText() | ||
189 | { | ||
190 | redo(); | ||
191 | } | ||
192 | |||
193 | QString PwCutCommand::name() | ||
194 | { | ||
195 | return i18n( "Cut" ); | ||
196 | } | ||
197 | |||
198 | void PwCutCommand::undo() | ||
199 | { | ||
200 | KABC::Addressee::List::Iterator iter; | ||
201 | for (iter = mAddresseeList.begin(); iter != mAddresseeList.end(); ++iter) | ||
202 | { | ||
203 | mDocument->insertAddressee(*iter); | ||
204 | } | ||
205 | mAddresseeList.clear(); | ||
206 | |||
207 | QClipboard *cb = QApplication::clipboard(); | ||
208 | //US | ||
209 | #ifndef KAB_EMBEDDED | ||
210 | kapp->processEvents(); | ||
211 | #else //KAB_EMBEDDED | ||
212 | qApp->processEvents(); | ||
213 | #endif //KAB_EMBEDDED | ||
214 | cb->setText( mOldText ); | ||
215 | } | ||
216 | |||
217 | void PwCutCommand::redo() | ||
218 | { | ||
219 | KABC::Addressee a; | ||
220 | QStringList::Iterator iter; | ||
221 | for (iter = mUidList.begin(); iter != mUidList.end(); ++iter) | ||
222 | { | ||
223 | a = mDocument->findByUid(*iter); | ||
224 | mDocument->removeAddressee(a); | ||
225 | mAddresseeList.append(a); | ||
226 | } | ||
227 | |||
228 | // Convert to clipboard | ||
229 | mClipText = AddresseeUtil::addresseesToClipboard(mAddresseeList); | ||
230 | |||
231 | QClipboard *cb = QApplication::clipboard(); | ||
232 | mOldText = cb->text(); | ||
233 | //US | ||
234 | #ifndef KAB_EMBEDDED | ||
235 | kapp->processEvents(); | ||
236 | #else //KAB_EMBEDDED | ||
237 | qApp->processEvents(); | ||
238 | #endif //KAB_EMBEDDED | ||
239 | cb->setText( mClipText ); | ||
240 | } | ||