1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/***************************************************************************
* Copyright (C) 2006 by Peter Penz *
* peter.penz@gmx.at *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "undomanager.h"
#include <klocale.h>
#include <kio/netaccess.h>
#include <tqtimer.h>
#include <assert.h>
#include "dolphin.h"
#include "dolphinstatusbar.h"
#include "progressindicator.h"
DolphinCommand::DolphinCommand() :
m_type(Copy),
m_macroIndex(-1)
{
// Implementation note: DolphinCommands are stored in a TQValueList, whereas
// TQValueList requires a default constructor of the added class.
// Instead of expressing this implementation detail to the interface by adding a
// Type::Undefined just Type::Copy is used to assure that all members have
// a defined state.
}
DolphinCommand::DolphinCommand(Type type,
const KURL::List& source,
const KURL& dest) :
m_type(type),
m_macroIndex(-1),
m_source(source),
m_dest(dest)
{
}
DolphinCommand::~DolphinCommand()
{
}
DolphinCommand& DolphinCommand::operator = (const DolphinCommand& command)
{
m_type = command.m_type;
m_source = command.m_source;
m_dest = command.m_dest;
return *this;
}
UndoManager& UndoManager::instance()
{
static UndoManager* instance = 0;
if (instance == 0) {
instance = new UndoManager();
}
return *instance;
}
void UndoManager::addCommand(const DolphinCommand& command)
{
++m_historyIndex;
if (m_recordMacro) {
DolphinCommand macroCommand = command;
macroCommand.m_macroIndex = m_macroCounter;
m_history.insert(m_history.at(m_historyIndex), macroCommand);
}
else {
m_history.insert(m_history.at(m_historyIndex), command);
}
emit undoAvailable(true);
emit undoTextChanged(i18n("Undo: %1").tqarg(commandText(command)));
// prevent an endless growing of the Undo history
if (m_historyIndex > 10000) {
m_history.erase(m_history.begin());
--m_historyIndex;
}
}
void UndoManager::beginMacro()
{
assert(!m_recordMacro);
m_recordMacro = true;
++m_macroCounter;
}
void UndoManager::endMacro()
{
assert(m_recordMacro);
m_recordMacro = false;
}
void UndoManager::undo()
{
if (m_recordMacro) {
endMacro();
}
if (m_historyIndex < 0) {
return;
}
int progressCount = 0;
int macroCount = 1;
calcStepsCount(macroCount, progressCount);
m_progressIndicator = new ProgressIndicator(i18n("Executing undo operation..."),
i18n("Executed undo operation."),
progressCount);
for (int i = 0; i < macroCount; ++i) {
const DolphinCommand command = m_history[m_historyIndex];
--m_historyIndex;
if (m_historyIndex < 0) {
emit undoAvailable(false);
emit undoTextChanged(i18n("Undo"));
}
else {
emit undoTextChanged(i18n("Undo: %1").tqarg(commandText(m_history[m_historyIndex])));
}
if (m_historyIndex < static_cast<int>(m_history.count()) - 1) {
emit redoAvailable(true);
emit redoTextChanged(i18n("Redo: %1").tqarg(commandText(command)));
}
else {
emit redoAvailable(false);
emit redoTextChanged(i18n("Redo"));
}
KURL::List sourceURLs = command.source();
KURL::List::Iterator it = sourceURLs.begin();
const KURL::List::Iterator end = sourceURLs.end();
const TQString destURL(command.destination().prettyURL(+1));
KIO::Job* job = 0;
switch (command.type()) {
case DolphinCommand::Link:
case DolphinCommand::Copy: {
KURL::List list;
while (it != end) {
const KURL deleteURL(destURL + (*it).filename());
list.append(deleteURL);
++it;
}
job = KIO::del(list, false, false);
break;
}
case DolphinCommand::Move: {
KURL::List list;
const KURL newDestURL((*it).directory());
while (it != end) {
const KURL newSourceURL(destURL + (*it).filename());
list.append(newSourceURL);
++it;
}
job = KIO::move(list, newDestURL, false);
break;
}
case DolphinCommand::Rename: {
assert(sourceURLs.count() == 1);
KIO::NetAccess::move(command.destination(), (*it));
break;
}
case DolphinCommand::Trash: {
while (it != end) {
// TODO: use KIO::special for accessing the trash protocol. See
// also Dolphin::slotJobResult() for further details.
const TQString originalFileName((*it).filename().section('-', 1));
KURL newDestURL(destURL + originalFileName);
KIO::NetAccess::move(*it, newDestURL);
++it;
m_progressIndicator->execOperation();
}
break;
}
case DolphinCommand::CreateFolder:
case DolphinCommand::CreateFile: {
KIO::NetAccess::del(command.destination(), &Dolphin::mainWin());
break;
}
}
if (job != 0) {
// Execute the jobs in a synchronous manner and forward the progress
// information to the Dolphin statusbar.
connect(job, TQT_SIGNAL(percent(KIO::Job*, unsigned long)),
this, TQT_SLOT(slotPercent(KIO::Job*, unsigned long)));
KIO::NetAccess::synchronousRun(job, &Dolphin::mainWin());
}
m_progressIndicator->execOperation();
}
delete m_progressIndicator;
m_progressIndicator = 0;
}
void UndoManager::redo()
{
if (m_recordMacro) {
endMacro();
}
const int maxHistoryIndex = m_history.count() - 1;
if (m_historyIndex >= maxHistoryIndex) {
return;
}
++m_historyIndex;
int progressCount = 0;
int macroCount = 1;
calcStepsCount(macroCount, progressCount);
m_progressIndicator = new ProgressIndicator(i18n("Executing redo operation..."),
i18n("Executed redo operation."),
progressCount);
for (int i = 0; i < macroCount; ++i) {
const DolphinCommand command = m_history[m_historyIndex];
if (m_historyIndex >= maxHistoryIndex) {
emit redoAvailable(false);
emit redoTextChanged(i18n("Redo"));
}
else {
emit redoTextChanged(i18n("Redo: %1").tqarg(commandText(m_history[m_historyIndex + 1])));
}
emit undoAvailable(true);
emit undoTextChanged(i18n("Undo: %1").tqarg(commandText(command)));
Dolphin& dolphin = Dolphin::mainWin();
KURL::List sourceURLs = command.source();
KURL::List::Iterator it = sourceURLs.begin();
const KURL::List::Iterator end = sourceURLs.end();
KIO::Job* job = 0;
switch (command.type()) {
case DolphinCommand::Link: {
job = KIO::link(sourceURLs, command.destination(), false);
break;
}
case DolphinCommand::Copy: {
job = KIO::copy(sourceURLs, command.destination(), false);
break;
}
case DolphinCommand::Rename:
case DolphinCommand::Move: {
job = KIO::move(sourceURLs, command.destination(), false);
break;
}
case DolphinCommand::Trash: {
const TQString destURL(command.destination().prettyURL());
while (it != end) {
// TODO: use KIO::special for accessing the trash protocol. See
// also Dolphin::slotJobResult() for further details.
const TQString originalFileName((*it).filename().section('-', 1));
KURL originalSourceURL(destURL + "/" + originalFileName);
KIO::Job* moveToTrashJob = KIO::trash(originalSourceURL);
KIO::NetAccess::synchronousRun(moveToTrashJob, &dolphin);
++it;
m_progressIndicator->execOperation();
}
break;
}
case DolphinCommand::CreateFolder: {
KIO::NetAccess::mkdir(command.destination(), &dolphin);
break;
}
case DolphinCommand::CreateFile: {
m_progressIndicator->execOperation();
KURL::List::Iterator it = sourceURLs.begin();
assert(sourceURLs.count() == 1);
KIO::CopyJob* copyJob = KIO::copyAs(*it, command.destination(), false);
copyJob->setDefaultPermissions(true);
job = copyJob;
break;
}
}
if (job != 0) {
// Execute the jobs in a synchronous manner and forward the progress
// information to the Dolphin statusbar.
connect(job, TQT_SIGNAL(percent(KIO::Job*, unsigned long)),
this, TQT_SLOT(slotPercent(KIO::Job*, unsigned long)));
KIO::NetAccess::synchronousRun(job, &dolphin);
}
++m_historyIndex;
m_progressIndicator->execOperation();
}
--m_historyIndex;
delete m_progressIndicator;
m_progressIndicator = 0;
}
UndoManager::UndoManager() :
m_recordMacro(false),
m_historyIndex(-1),
m_macroCounter(0),
m_progressIndicator(0)
{
}
UndoManager::~UndoManager()
{
delete m_progressIndicator;
m_progressIndicator = 0;
}
TQString UndoManager::commandText(const DolphinCommand& command) const
{
TQString text;
switch (command.type()) {
case DolphinCommand::Copy: text = i18n("Copy"); break;
case DolphinCommand::Move: text = i18n("Move"); break;
case DolphinCommand::Link: text = i18n("Link"); break;
case DolphinCommand::Rename: text = i18n("Rename"); break;
case DolphinCommand::Trash: text = i18n("Move to Trash"); break;
case DolphinCommand::CreateFolder: text = i18n("Create New Folder"); break;
case DolphinCommand::CreateFile: text = i18n("Create New File"); break;
default: break;
}
return text;
}
void UndoManager::slotPercent(KIO::Job* /* job */, unsigned long /* percent */)
{
// It is not allowed to update the progress indicator in the context
// of this slot, hence do an asynchronous triggering.
TQTimer::singleShot(0, this, TQT_SLOT(updateProgress()));
}
void UndoManager::updateProgress()
{
m_progressIndicator->execOperation();
}
void UndoManager::calcStepsCount(int& macroCount, int& progressCount)
{
progressCount = 0;
macroCount = 0;
const int macroIndex = m_history[m_historyIndex].m_macroIndex;
if (macroIndex < 0) {
// default use case: no macro has been recorded
macroCount = 1;
progressCount = m_history[m_historyIndex].source().count();
return;
}
// iterate backward for undo...
int i = m_historyIndex;
while ((i >= 0) && (m_history[i].m_macroIndex == macroIndex)) {
++macroCount;
progressCount += m_history[i].source().count();
--i;
}
// iterate forward for redo...
const int max = m_history.count() - 1;
i = m_historyIndex + 1;
while ((i <= max) && (m_history[i].m_macroIndex == macroIndex)) {
++macroCount;
progressCount += m_history[i].source().count();
++i;
}
}
#include "undomanager.moc"
|