-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdmop.cpp
More file actions
692 lines (575 loc) · 18.8 KB
/
Copy pathdmop.cpp
File metadata and controls
692 lines (575 loc) · 18.8 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, chch-kiwi@users.sourceforge.net
.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
/*
Project: Maxview
Author: Simon Glass
Copyright: 2001-2009 Bluewater Systems Ltd, www.bluewatersys.com
File: dmop.cpp
Started: 26/6/09
This file contains operations relating to Desktopmodel.
These operations are typically invoked by the undo system in desktopundo
*/
#include <QApplication>
#include <QClipboard>
#include <QDesktopServices>
#include <QFile>
#include <QMimeData>
#include <QProcess>
#include <QUrl>
#include "desktopmodel.h"
#include "file.h"
#include "op.h"
#include "utils.h"
err_info *Desktopmodel::opUnstackFromStack (QModelIndex &src, QStringList &newnames,
int srcpagenum, QList<pagepos_info> &pagepos)
{
QString newname;
err_info *e = NULL;
File *f = getFile (src);
File *fnew;
QList<File *> flist;
int upto = 0, row;
// remove every page from the stack except the first
foreach (newname, newnames)
{
pagepos_info &pp = pagepos [upto++];
// unstack the right number of pages from the source into a fresh stack
CALLB (f->unstackItems (srcpagenum, pp.pagecount, true, newname, fnew, row));
fnew->setPos (pp.pos);
fnew->setPagenum (pp.pagenum);
flist << fnew;
}
buildItem (src);
insertRows (flist, src.parent ());
return e;
}
err_info *Desktopmodel::opUnstackStacks (QModelIndexList &list, QModelIndex parent,
QList<QStringList> &_newnames)
{
QModelIndexList newlist;
QModelIndex ind;
err_info *err = NULL;
// work through each stack in the list
// note that some items might only have one page, in which case we do nothing
foreach (ind, list)
{
File *f = getFile (ind);
File *fnew;
int pagenum = 1, row;
// printf ("unstack %s, row %d\n", f->filename ().latin1 (), ind.row ());
QList<File *> flist;
QStringList stack_names;
CALL(f->load());
// remove every page from the stack except the first
while (f->pagecount () > 1)
{
// for each new page, create a fresh stack
err = f->unstackItems (0, 1, true, "", fnew, row, pagenum++);
if (err)
break;
stack_names << fnew->filename ();
flist << fnew;
}
// we leave one page on the original stack
// printf ("unstack done, row %d, flist size = %d\n", ind.row (), flist.size ());
if (flist.size ()) // any pages unstacked?
{
buildItem (ind);
insertRows (flist, parent);
}
_newnames << stack_names;
}
return err;
}
err_info *Desktopmodel::opStackStacks (QModelIndex &index, QModelIndexList &list, int &pagenum)
{
QList<QModelIndexList> pages_list;
QModelIndexList dest;
dest << index;
pages_list << list;
return opRestackStacks (dest, index.parent (), pages_list, pagenum);
}
err_info *Desktopmodel::opRestackStacks (QModelIndexList &list, QModelIndex parent,
QList<QModelIndexList> &pages_list, int &in_destpage)
{
QModelIndexList delete_list;
QModelIndex ind;
QString fname;
err_info *e = NULL;
int upto = 0;
QModelIndex pind;
int out_destpage = in_destpage; // destination page for first stack
// work through each stack in the list
foreach (ind, list)
{
File *f = getFile (ind);
// File *fnew;
// int pagenum = 1;
int destpage = in_destpage == -1 ? f->pagenum () : in_destpage;
if (out_destpage == -1)
out_destpage = destpage;
QList<File *> flist;
// add each page back onto the stack
int old_pagenum = f->pagenum ();
foreach (pind, pages_list [upto])
{
if (pind != QModelIndex ()) // not empty
{
File *pdel = getFile (pind);
// get page to stack to, and work out where the next lot will go
f->setPagenum (destpage);
destpage += pdel->pagecount ();
// stack this page, and remember to remove from model later
CALLB (f->stackItem (pdel));
delete_list << pind;
}
}
f->setPagenum (old_pagenum);
buildItem (ind);
upto++;
}
// record destination page
in_destpage = out_destpage;
// we lose the original error
err_info *e2 = opDeleteStacks (delete_list, parent);
if (!e)
e = e2;
return e;
}
err_info *Desktopmodel::opDuplicateStacks (QModelIndexList &list, QModelIndex parent,
QStringList &namelist, File::e_type type, int odd_even)
{
QModelIndexList newlist;
QModelIndex ind;
QList<File *> flist;
err_info *err = NULL;
File *fnew, *f;
int count;
// duplicate each stack, creating a list of new files
QString opname = type == File::Type_other ? tr ("Copy files") :
QString (tr ("Convert to %1")).arg (File::typeName (type));
count = type == File::Type_other ? list.size () : listPagecount (list);
Operation op (opname, count, 0);
foreach (ind, list)
{
f = getFile (ind);
CALL(f->load());
err = f->duplicateAny (type, odd_even, op, fnew);
if (err)
break;
flist << fnew;
namelist << fnew->filename ();
}
// insert the files
insertRows (flist, parent);
return err;
}
err_info *Desktopmodel::opDeleteStack (QModelIndex &index)
{
QModelIndexList list; // list of indexes to delete
list << index;
return opDeleteStacks (list, index.parent ());
}
err_info *Desktopmodel::opDeleteStacks (QModelIndexList &list, QModelIndex parent)
{
err_info *e = NULL;
QModelIndex ind;
QModelIndexList del_list; // list of indexes to delete
// Desk *desk = getDesk (parent);
UNUSED (parent);
// _model_invalid = true;
foreach (ind, list) if (getFile (ind))
{
CALLB (getFile (ind)->remove ());
del_list << ind;
}
// sort in reverse order so the row numbers don't change on us
sortForDelete (del_list);
foreach (ind, del_list)
removeRow (ind.row (), ind.parent ());
// delete desk->takeAt (ind.row ());
// _model_invalid = false;
return e;
}
err_info *Desktopmodel::opTrashStacks (QModelIndexList &list, QModelIndex parent,
QStringList &trashlist, QString &dest, bool copy)
{
err_info *e = NULL;
QModelIndex ind;
Desk *desk = getDesk (parent);
QModelIndexList del_list; // list of indexes to delete
// savePersistentIndexes ();
// emit layoutAboutToBeChanged();
if (dest.isEmpty ())
dest = desk->trashdir ();
if (dest.isEmpty ())
return err_make (ERRFN, ERR_no_trash_directory_is_defined);
// we don't know how many rows will be removed, so let's say all of them for now
// beginRemoveRows (parent, 0, rowCount (parent));
// _model_invalid = true;
foreach (ind, list)
{
File *f = getFile (ind);
QString trashname;
// skip missing items
if (!ind.isValid ())
continue;
// printf (" row %d: %p, delete %s\n", ind.row (), f,
// f->filename ().toLatin1 ().constData());
e = f->move (dest, trashname, copy);
if (e)
break;
del_list << ind;
// removeRow (ind.row (), ind.parent ());
trashlist << trashname;
}
if (!copy)
{
sortForDelete (del_list);
foreach (ind, del_list)
{
Q_ASSERT (ind.isValid ());
removeRows (ind.row (), 1, parent);
}
}
/* if we have the destination desk loaded, alert it about the new files */
addFilesToDesk (dest, trashlist);
/** this seems to be a bad idea as it causes a crash in the proxy.We are not
actually sorting the model, so perhaps we shouldn't emit it anyway. Not
emitting it doesn't seem to cause any problem */
// emit layoutChanged();
return e;
}
err_info *Desktopmodel::opUntrashStacks (QStringList &trashlist, QModelIndex parent,
QStringList &filenames, QList<pagepos_info> *pagepos, QString &src, bool copy)
{
Desk *desk = getDesk (parent);
QModelIndexList list;
QString trashname;
QModelIndex ind;
File *fnew;
err_info *e = NULL;
QList<File *> flist;
int i;
/* first, move all the trashed files back into the current directory,
creating a list of the new File * records thus created. If
an error occurs, stop where we got up to (with the one that caused
an error not added). For a copy, we just remove from the trash */
for (i = 0; i < trashlist.size (); i++)
{
trashname = trashlist [i];
QString fname = filenames [i];
// printf ("undelete %s to %s\n", trashname.toLatin1 ().constData(),
// fname.toLatin1 ().constData());
if (src.isEmpty ())
e = copy
? desk->deleteFromTrash (trashname)
: desk->moveFromTrash (trashname, fname, fnew);
else
e = copy
? desk->deleteFromDir (src, trashname)
: desk->moveFromDir (src, trashname, fname, fnew);
if (e)
break;
// if we have position/page number information, restore it
if (!copy && pagepos)
{
fnew->setPos ((*pagepos) [i].pos);
fnew->setPagenum ((*pagepos) [i].pagenum);
}
filenames [i] = fname; // update the filename
flist << fnew;
}
if (!copy)
insertRows (flist, parent);
/* if we have the source desk loaded, alert it about the removed files */
removeFilesFromDesk (src, trashlist);
return e;
}
void Desktopmodel::opMoveStacks (QModelIndexList &list, QModelIndex parent,
QList<QPoint> &newpos)
{
Desk *desk = getDesk (parent);
QModelIndex ind;
int i = 0;
foreach (ind, list) if (ind.isValid ())
{
getFile (ind)->setPos (newpos [i++]);
emit dataChanged (ind, ind);
}
desk->dirty ();
}
err_info *Desktopmodel::opUnstackPage (QModelIndex &ind, int &pagenum,
bool remove, QString &newname)
{
File *fnew, *f = getFile (ind);
err_info *e = NULL;
int row;
if (pagenum == -1)
pagenum = f->pagenum ();
if ((!remove || f->pagecount () > 1)
&& (e = f->unstackItems (pagenum, 1, remove, "", fnew, row, 1), !e))
{
newname = fnew->filename ();
// if removing, update the view of the source stack
if (remove)
buildItem (ind);
QModelIndexList list;
newItem (row, ind.parent (), list);
}
return e;
}
err_info *Desktopmodel::opUpdateAnnot (QModelIndex &ind, QHash<int, QString> &updates)
{
_modelconv->assertIsSource (ind.model (), &ind, 0);
File *f = getFile (ind);
if (f)
{
f->putAnnot (updates);
f->flush ();
}
return NULL;
}
err_info *Desktopmodel::opDeletePages (QModelIndex &ind, QBitArray &pages,
QByteArray &del_info, int &count)
{
File *f = getFile (ind);
err_info *e = NULL;
if (f)
{
e = f->removePages (pages, del_info, count);
buildItem (ind);
getDesk (ind.parent ())->dirty ();
}
return e;
}
err_info *Desktopmodel::opUndeletePages (QModelIndex &ind, QBitArray &pages,
QByteArray &del_info, int count)
{
File *f = getFile (ind);
err_info *e = NULL;
if (f)
{
e = f->restorePages (pages, del_info, count);
buildItem (ind);
}
return e;
}
void Desktopmodel::opChangeDir (QString &dirPath, QString &rootPath)
{
QModelIndex ind = showDir(dirPath, rootPath, nullptr);
emit dirChanged (dirPath, ind);
}
err_info *Desktopmodel::opRenameStack (const QModelIndex &index, QString &newname)
{
File *f = getFile (index);
err_info *e = NULL;
if (f)
{
e = f->rename (newname, true);
buildItem (index);
getDesk (index.parent ())->dirty ();
}
return e;
}
err_info *Desktopmodel::opRenamePage (const QModelIndex &index, int pagenum, QString &newname)
{
_modelconv->assertIsSource (0, &index, 0);
File *f = getFile (index);
err_info *e = NULL;
if (f)
{
if (pagenum >= 0 && pagenum < f->pagecount ()) // which it should be!
{
e = f->renamePage (pagenum, newname);
f->setPagenum (pagenum);
}
buildItem (index);
}
return e;
}
err_info *Desktopmodel::opTransformPage (const QModelIndex &index,
int pagenum, File::e_transform op)
{
_modelconv->assertIsSource (0, &index, 0);
File *f = getFile (index);
err_info *e = NULL;
if (f)
{
/* the file may not have been loaded yet, in which case it reports
no pages and the transform would be silently skipped */
f->load ();
if (!f->pagecount ())
/* an empty stack has nothing to transform; this also happens
for stacks whose pages are not accessible, such as those in
a remote repository */
e = err_make (ERRFN, ERR_stack_has_no_pages);
else if (pagenum == -1)
{
// transform every page of the stack
for (int page = 0; !e && page < f->pagecount (); page++)
e = f->transformPage (page, op);
/* the page view refreshes through pageContentChanged(), so
mark the item update as minor to avoid it also doing a full
rebuild, which loses the page selection */
_minor_change = true;
buildItem (index);
_minor_change = false;
if (!e)
emit pageContentChanged (index, -1);
}
else if (pagenum >= 0 && pagenum < f->pagecount ())
{
e = f->transformPage (pagenum, op);
f->setPagenum (pagenum);
_minor_change = true;
buildItem (index);
_minor_change = false;
if (!e)
emit pageContentChanged (index, pagenum);
}
else
e = f->not_impl ();
}
return e;
}
err_info *Desktopmodel::copyFilesToClipboard (QString &fname,
QStringList &fnamelist, bool &can_delete)
{
can_delete = true;
// if we have more than one file, use a zip
if (fnamelist.size () == 1)
{
fname = fnamelist [0];
can_delete = false; // we need to keep the file on the clipboard
}
else
CALL (util_buildZip (fname, fnamelist));
QUrl url = QUrl::fromLocalFile (fname);
// Put the file on the clipboard as a text/uri-list so Gmail compose
// (and any other webmail that accepts file paste) treats Ctrl+V as a
// file attach. mailto: URLs cannot carry attachments per RFC 6068,
// so this is the only path that lands a real attachment in Gmail
// without a per-user OAuth dance.
QMimeData *mime = new QMimeData ();
mime->setUrls (QList<QUrl> () << url);
/* A bare text/uri-list is enough for a browser, but file managers
need a copy/cut marker to paste the file. Provide both the GNOME
and KDE flavours so Files, Nautilus, Dolphin and the like treat
Ctrl+V as a file copy. */
mime->setData ("x-special/gnome-copied-files",
QByteArray ("copy\n") + url.toEncoded ());
mime->setData ("application/x-kde-cutselection", QByteArray ("0"));
QApplication::clipboard ()->setMimeData (mime);
return NULL;
}
err_info *Desktopmodel::emailFiles (QString &fname, QStringList &fnamelist, bool &can_delete)
{
CALL (copyFilesToClipboard (fname, fnamelist, can_delete));
// Open Gmail's compose URL with a pre-filled subject and body. The
// attachment cannot ride on the URL, but the user pastes it with
// Ctrl+V once the compose window is up.
QUrl compose ("https://mail.google.com/mail/?view=cm&fs=1"
"&su=Files&body=see+attachment+(paste+with+Ctrl%2BV)");
if (url_capture)
*url_capture = compose;
else
QDesktopServices::openUrl (compose);
return NULL;
}
err_info *Desktopmodel::packageFiles (QModelIndexList &slist,
File::e_type type, Operation &op, QString &fname,
QStringList &fname_list, QStringList &tmp_list)
{
int upto = 0;
// name of first file will be used for the zip name
fname = getFile (slist [0])->filename ();
QString dir = QString ("%1/").arg (P_tmpdir);
// put the path on front of each file
foreach (QModelIndex ind, slist)
{
File *f = getFile (ind);
File *fnew;
// if just copying, then get the filename
if (type == File::Type_other)
fname_list << f->pathname ();
// else convert this file into a temporary one of the right type
else
{
QString uniq;
QString ext = File::typeExt (type);
uniq = util_findNextFilename (f->leaf (), dir, ext);
CALL (f->duplicateToDesk (0, type, uniq, 3, op, fnew));
fname_list << uniq;
tmp_list << uniq;
}
op.setProgress (upto++);
}
return NULL;
}
err_info *Desktopmodel::opEmailFiles (QModelIndex parent, QModelIndexList &slist,
File::e_type type)
{
QString fname;
QStringList fname_list, tmp_list;
UNUSED (parent);
Operation op ("Packaging", slist.size (), 0);
err_info *e = packageFiles (slist, type, op, fname, fname_list, tmp_list);
bool can_delete = true;
if (!e)
e = emailFiles (fname, fname_list, can_delete);
// delete the files if we are allowed
if (can_delete) {
foreach (QString fname, tmp_list)
{
QFile f (fname);
f.remove ();
}
}
return e;
}
err_info *Desktopmodel::opCopyFiles (QModelIndex parent, QModelIndexList &slist,
File::e_type type)
{
QString fname;
QStringList fname_list, tmp_list;
UNUSED (parent);
Operation op ("Packaging", slist.size (), 0);
err_info *e = packageFiles (slist, type, op, fname, fname_list, tmp_list);
bool can_delete = true;
if (!e)
e = copyFilesToClipboard (fname, fname_list, can_delete);
// delete the converted files if they are not the one on the clipboard
if (can_delete) {
foreach (QString fname, tmp_list)
{
QFile f (fname);
f.remove ();
}
}
return e;
}
void Desktopmodel::opUpdateRepositoryList (QString &dirpath, bool add_not_delete)
{
// Tell our controller (Desktopwidget) what to do
emit updateRepositoryList (dirpath, add_not_delete);
}