-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathnemo-file-operations.c
More file actions
7452 lines (6203 loc) · 185 KB
/
nemo-file-operations.c
File metadata and controls
7452 lines (6203 loc) · 185 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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; indent-tabs-mode: f; c-basic-offset: 4; tab-width: 4 -*- */
/* nemo-file-operations.c - Nemo file operations.
Copyright (C) 1999, 2000 Free Software Foundation
Copyright (C) 2000, 2001 Eazel, Inc.
Copyright (C) 2007 Red Hat, Inc.
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 - Suite 500,
Boston, MA 02110-1335, USA.
Authors: Alexander Larsson <[email protected]>
Ettore Perazzoli <[email protected]>
Pavel Cisler <[email protected]>
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for sync_file_range() */
#endif
#include <config.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <locale.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include "nemo-file-operations.h"
#include "nemo-file-changes-queue.h"
#include "nemo-lib-self-check-functions.h"
#include "nemo-progress-info.h"
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-vfs-extensions.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <glib.h>
#include <libxapp/xapp-favorites.h>
#include "nemo-file-changes-queue.h"
#include "nemo-file-private.h"
#include "nemo-desktop-icon-file.h"
#include "nemo-desktop-link-monitor.h"
#include "nemo-global-preferences.h"
#include "nemo-link.h"
#include "nemo-desktop-utils.h"
#include "nemo-trash-monitor.h"
#include "nemo-file-utilities.h"
#include "nemo-file-conflict-dialog.h"
#include "nemo-file-undo-operations.h"
#include "nemo-file-undo-manager.h"
#include "nemo-job-queue.h"
/* TODO: TESTING!!! */
typedef enum {
OP_KIND_COPY,
OP_KIND_MOVE,
OP_KIND_DELETE,
OP_KIND_TRASH,
OP_KIND_EMPTY_TRASH,
OP_KIND_DUPE,
OP_KIND_PERMISSIONS,
OP_KIND_LINK,
OP_KIND_CREATE,
OP_KIND_TRUST
} OpKind;
typedef struct {
GIOSchedulerJob *io_job;
GtkWindow *parent_window;
int monitor_num;
int inhibit_cookie;
NemoProgressInfo *progress;
GCancellable *cancellable;
GHashTable *skip_files;
GHashTable *skip_readdir_error;
NemoFileUndoInfo *undo_info;
gboolean skip_all_error;
gboolean skip_all_conflict;
gboolean merge_all;
gboolean auto_rename_all;
gboolean replace_all;
gboolean delete_all;
} CommonJob;
typedef struct {
CommonJob common;
gboolean is_move;
GList *files;
GFile *destination;
GFile *desktop_location;
GFile *fake_display_source;
GdkPoint *icon_positions;
int n_icon_positions;
GHashTable *debuting_files;
gchar *target_name;
NemoCopyCallback done_callback;
gpointer done_callback_data;
} CopyMoveJob;
typedef struct {
CommonJob common;
GList *files;
gboolean try_trash;
gboolean user_cancel;
NemoDeleteCallback done_callback;
gpointer done_callback_data;
} DeleteJob;
typedef struct {
CommonJob common;
GFile *dest_dir;
char *filename;
gboolean make_dir;
GFile *src;
char *src_data;
int length;
GdkPoint position;
gboolean has_position;
GFile *created_file;
NemoCreateCallback done_callback;
gpointer done_callback_data;
} CreateJob;
typedef struct {
CommonJob common;
GList *trash_dirs;
gboolean should_confirm;
NemoOpCallback done_callback;
gpointer done_callback_data;
} EmptyTrashJob;
typedef struct {
CommonJob common;
GFile *file;
gboolean interactive;
NemoOpCallback done_callback;
gpointer done_callback_data;
} MarkTrustedJob;
typedef struct {
CommonJob common;
GFile *file;
NemoOpCallback done_callback;
gpointer done_callback_data;
guint32 file_permissions;
guint32 file_mask;
guint32 dir_permissions;
guint32 dir_mask;
} SetPermissionsJob;
typedef struct {
int num_files;
goffset num_bytes;
int num_files_since_progress;
OpKind op;
} SourceInfo;
typedef struct {
int num_files;
goffset num_bytes;
OpKind op;
guint64 last_report_time;
int last_reported_files_left;
} TransferInfo;
#define SECONDS_NEEDED_FOR_RELIABLE_TRANSFER_RATE 8
#define US_PER_MS 1000
#define PROGRESS_UPDATE_THRESHOLD 250
#define MAXIMUM_DISPLAYED_FILE_NAME_LENGTH 50
#define IS_IO_ERROR(__error, KIND) (((__error)->domain == G_IO_ERROR && (__error)->code == G_IO_ERROR_ ## KIND))
#define SKIP _("_Skip")
#define SKIP_ALL _("S_kip All")
#define RETRY _("_Retry")
#define DELETE_ALL _("Delete _All")
#define REPLACE _("_Replace")
#define REPLACE_ALL _("Replace _All")
#define MERGE _("_Merge")
#define MERGE_ALL _("Merge _All")
#define COPY_FORCE _("Copy _Anyway")
;
static void add_job_to_job_queue (GIOSchedulerJobFunc job_func,
gpointer user_data,
GCancellable *cancellable,
NemoProgressInfo *info,
OpKind kind);
static void
mark_desktop_file_trusted (CommonJob *common,
GCancellable *cancellable,
GFile *file,
gboolean interactive);
static gboolean
is_all_button_text (const char *button_text)
{
g_assert (button_text != NULL);
return !strcmp (button_text, SKIP_ALL) ||
!strcmp (button_text, REPLACE_ALL) ||
!strcmp (button_text, DELETE_ALL) ||
!strcmp (button_text, MERGE_ALL);
}
static void scan_sources (GList *files,
SourceInfo *source_info,
CommonJob *job,
OpKind kind);
static gboolean empty_trash_job (GIOSchedulerJob *io_job,
GCancellable *cancellable,
gpointer user_data);
static char * query_fs_type (GFile *file,
GCancellable *cancellable);
/* keep in time with format_time()
*
* This counts and outputs the number of “time units”
* formatted and displayed by format_time().
* For instance, if format_time outputs “3 hours, 4 minutes”
* it yields 7.
*/
static int
seconds_count_format_time_units (int seconds)
{
int minutes;
int hours;
if (seconds < 0) {
/* Just to make sure... */
seconds = 0;
}
if (seconds < 60) {
/* seconds */
return seconds;
}
if (seconds < 60*60) {
/* minutes */
minutes = seconds / 60;
return minutes;
}
hours = seconds / (60*60);
if (seconds < 60*60*4) {
/* minutes + hours */
minutes = (seconds - hours * 60 * 60) / 60;
return minutes + hours;
}
return hours;
}
static char *
format_time (int seconds)
{
int minutes;
int hours;
char *res;
if (seconds < 0) {
/* Just to make sure... */
seconds = 0;
}
if (seconds < 60) {
return g_strdup_printf (ngettext ("%'d second","%'d seconds", (int) seconds), (int) seconds);
}
if (seconds < 60*60) {
minutes = seconds / 60;
return g_strdup_printf (ngettext ("%'d minute", "%'d minutes", minutes), minutes);
}
hours = seconds / (60*60);
if (seconds < 60*60*4) {
char *h, *m;
minutes = (seconds - hours * 60 * 60) / 60;
h = g_strdup_printf (ngettext ("%'d hour", "%'d hours", hours), hours);
m = g_strdup_printf (ngettext ("%'d minute", "%'d minutes", minutes), minutes);
res = g_strconcat (h, ", ", m, NULL);
g_free (h);
g_free (m);
return res;
}
return g_strdup_printf (ngettext ("approximately %'d hour",
"approximately %'d hours",
hours), hours);
}
static char *
shorten_utf8_string (const char *base, int reduce_by_num_bytes)
{
int len;
char *ret;
const char *p;
len = strlen (base);
len -= reduce_by_num_bytes;
if (len <= 0) {
return NULL;
}
ret = g_new (char, len + 1);
p = base;
while (len) {
char *next;
next = g_utf8_next_char (p);
if (next - p > len || *next == '\0') {
break;
}
len -= next - p;
p = next;
}
if (p - base == 0) {
g_free (ret);
return NULL;
} else {
memcpy (ret, base, p - base);
ret[p - base] = '\0';
return ret;
}
}
/* Note that we have these two separate functions with separate format
* strings for ease of localization.
*/
static char *
get_link_name (const char *name, int count, int max_length)
{
const char *format;
char *result;
int unshortened_length;
gboolean use_count;
g_assert (name != NULL);
if (count < 0) {
g_warning ("bad count in get_link_name");
count = 0;
}
if (count <= 2) {
/* Handle special cases for low numbers.
* Perhaps for some locales we will need to add more.
*/
switch (count) {
default:
g_assert_not_reached ();
/* fall through */
case 0:
/* duplicate original file name */
format = "%s";
break;
case 1:
/* appended to new link file */
format = _("Link to %s");
break;
case 2:
/* appended to new link file */
format = _("Another link to %s");
break;
}
use_count = FALSE;
} else {
/* Handle special cases for the first few numbers of each ten.
* For locales where getting this exactly right is difficult,
* these can just be made all the same as the general case below.
*/
switch (count % 10) {
case 1:
/* Localizers: Feel free to leave out the "st" suffix codespell:ignore
* if there's no way to do that nicely for a
* particular language.
*/
format = _("%'dst link to %s");
break;
case 2:
/* appended to new link file */
format = _("%'dnd link to %s");
break;
case 3:
/* appended to new link file */
format = _("%'drd link to %s");
break;
default:
/* appended to new link file */
format = _("%'dth link to %s");
break;
}
use_count = TRUE;
}
if (use_count)
result = g_strdup_printf (format, count, name);
else
result = g_strdup_printf (format, name);
if (max_length > 0 && (unshortened_length = strlen (result)) > max_length) {
char *new_name;
new_name = shorten_utf8_string (name, unshortened_length - max_length);
if (new_name) {
g_free (result);
if (use_count)
result = g_strdup_printf (format, count, new_name);
else
result = g_strdup_printf (format, new_name);
g_assert ((int)strlen (result) <= max_length);
g_free (new_name);
}
}
return result;
}
/* Localizers:
* Feel free to leave out the st, nd, rd and th suffix or codespell:ignore
* make some or all of them match.
*/
/* localizers: tag used to detect the first copy of a file */
static const char untranslated_copy_duplicate_tag[] = N_(" (copy)");
/* localizers: tag used to detect the second copy of a file */
static const char untranslated_another_copy_duplicate_tag[] = N_(" (another copy)");
/* localizers: tag used to detect the x11th copy of a file */
static const char untranslated_x11th_copy_duplicate_tag[] = N_("th copy)");
/* localizers: tag used to detect the x12th copy of a file */
static const char untranslated_x12th_copy_duplicate_tag[] = N_("th copy)");
/* localizers: tag used to detect the x13th copy of a file */
static const char untranslated_x13th_copy_duplicate_tag[] = N_("th copy)");
/* localizers: tag used to detect the x1st copy of a file */
static const char untranslated_st_copy_duplicate_tag[] = N_("st copy)");
/* localizers: tag used to detect the x2nd copy of a file */
static const char untranslated_nd_copy_duplicate_tag[] = N_("nd copy)");
/* localizers: tag used to detect the x3rd copy of a file */
static const char untranslated_rd_copy_duplicate_tag[] = N_("rd copy)");
/* localizers: tag used to detect the xxth copy of a file */
static const char untranslated_th_copy_duplicate_tag[] = N_("th copy)");
#define COPY_DUPLICATE_TAG _(untranslated_copy_duplicate_tag)
#define ANOTHER_COPY_DUPLICATE_TAG _(untranslated_another_copy_duplicate_tag)
#define X11TH_COPY_DUPLICATE_TAG _(untranslated_x11th_copy_duplicate_tag)
#define X12TH_COPY_DUPLICATE_TAG _(untranslated_x12th_copy_duplicate_tag)
#define X13TH_COPY_DUPLICATE_TAG _(untranslated_x13th_copy_duplicate_tag)
#define ST_COPY_DUPLICATE_TAG _(untranslated_st_copy_duplicate_tag)
#define ND_COPY_DUPLICATE_TAG _(untranslated_nd_copy_duplicate_tag)
#define RD_COPY_DUPLICATE_TAG _(untranslated_rd_copy_duplicate_tag)
#define TH_COPY_DUPLICATE_TAG _(untranslated_th_copy_duplicate_tag)
/* localizers: appended to first file copy */
static const char untranslated_first_copy_duplicate_format[] = N_("%s (copy)%s");
/* localizers: appended to second file copy */
static const char untranslated_second_copy_duplicate_format[] = N_("%s (another copy)%s");
/* localizers: appended to x11th file copy */
static const char untranslated_x11th_copy_duplicate_format[] = N_("%s (%'dth copy)%s");
/* localizers: appended to x12th file copy */
static const char untranslated_x12th_copy_duplicate_format[] = N_("%s (%'dth copy)%s");
/* localizers: appended to x13th file copy */
static const char untranslated_x13th_copy_duplicate_format[] = N_("%s (%'dth copy)%s");
/* localizers: if in your language there's no difference between 1st, 2nd, 3rd and nth
* plurals, you can leave the st, nd, rd suffixes out and just make all the translated
* strings look like "%s (copy %'d)%s".
*/
/* localizers: appended to x1st file copy */
static const char untranslated_st_copy_duplicate_format[] = N_("%s (%'dst copy)%s");
/* localizers: appended to x2nd file copy */
static const char untranslated_nd_copy_duplicate_format[] = N_("%s (%'dnd copy)%s");
/* localizers: appended to x3rd file copy */
static const char untranslated_rd_copy_duplicate_format[] = N_("%s (%'drd copy)%s");
/* localizers: appended to xxth file copy */
static const char untranslated_th_copy_duplicate_format[] = N_("%s (%'dth copy)%s");
#define FIRST_COPY_DUPLICATE_FORMAT _(untranslated_first_copy_duplicate_format)
#define SECOND_COPY_DUPLICATE_FORMAT _(untranslated_second_copy_duplicate_format)
#define X11TH_COPY_DUPLICATE_FORMAT _(untranslated_x11th_copy_duplicate_format)
#define X12TH_COPY_DUPLICATE_FORMAT _(untranslated_x12th_copy_duplicate_format)
#define X13TH_COPY_DUPLICATE_FORMAT _(untranslated_x13th_copy_duplicate_format)
#define ST_COPY_DUPLICATE_FORMAT _(untranslated_st_copy_duplicate_format)
#define ND_COPY_DUPLICATE_FORMAT _(untranslated_nd_copy_duplicate_format)
#define RD_COPY_DUPLICATE_FORMAT _(untranslated_rd_copy_duplicate_format)
#define TH_COPY_DUPLICATE_FORMAT _(untranslated_th_copy_duplicate_format)
static char *
extract_string_until (const char *original, const char *until_substring)
{
char *result;
g_assert ((int) strlen (original) >= until_substring - original);
g_assert (until_substring - original >= 0);
result = g_malloc (until_substring - original + 1);
strncpy (result, original, until_substring - original);
result[until_substring - original] = '\0';
return result;
}
/* Dismantle a file name, separating the base name, the file suffix and removing any
* (xxxcopy), etc. string. Figure out the count that corresponds to the given
* (xxxcopy) substring.
*/
static void
parse_previous_duplicate_name (const char *name,
char **name_base,
const char **suffix,
int *count)
{
const char *tag;
g_assert (name[0] != '\0');
*suffix = eel_filename_get_extension_offset (name + 1);
if (*suffix == NULL || (*suffix)[1] == '\0') {
/* no suffix */
*suffix = "";
}
tag = strstr (name, COPY_DUPLICATE_TAG);
if (tag != NULL) {
if (tag > *suffix) {
/* handle case "foo. (copy)" */
*suffix = "";
}
*name_base = extract_string_until (name, tag);
*count = 1;
return;
}
tag = strstr (name, ANOTHER_COPY_DUPLICATE_TAG);
if (tag != NULL) {
if (tag > *suffix) {
/* handle case "foo. (another copy)" */
*suffix = "";
}
*name_base = extract_string_until (name, tag);
*count = 2;
return;
}
/* Check to see if we got one of st, nd, rd, th. */
tag = strstr (name, X11TH_COPY_DUPLICATE_TAG);
if (tag == NULL) {
tag = strstr (name, X12TH_COPY_DUPLICATE_TAG);
}
if (tag == NULL) {
tag = strstr (name, X13TH_COPY_DUPLICATE_TAG);
}
if (tag == NULL) {
tag = strstr (name, ST_COPY_DUPLICATE_TAG);
}
if (tag == NULL) {
tag = strstr (name, ND_COPY_DUPLICATE_TAG);
}
if (tag == NULL) {
tag = strstr (name, RD_COPY_DUPLICATE_TAG);
}
if (tag == NULL) {
tag = strstr (name, TH_COPY_DUPLICATE_TAG);
}
/* If we got one of st, nd, rd, th, fish out the duplicate number. */
if (tag != NULL) {
/* localizers: opening parentheses to match the "th copy)" string */
tag = strstr (name, _(" ("));
if (tag != NULL) {
if (tag > *suffix) {
/* handle case "foo. (22nd copy)" */
*suffix = "";
}
*name_base = extract_string_until (name, tag);
/* localizers: opening parentheses of the "th copy)" string */
if (sscanf (tag, _(" (%'d"), count) == 1) {
if (*count < 1 || *count > 1000000) {
/* keep the count within a reasonable range */
*count = 0;
}
return;
}
*count = 0;
return;
}
}
*count = 0;
if (**suffix != '\0') {
*name_base = extract_string_until (name, *suffix);
} else {
*name_base = g_strdup (name);
}
}
static char *
make_next_duplicate_name (const char *base, const char *suffix, int count, int max_length)
{
const char *format;
char *result;
int unshortened_length;
gboolean use_count;
if (count < 1) {
g_warning ("bad count %d in get_duplicate_name", count);
count = 1;
}
if (count <= 2) {
/* Handle special cases for low numbers.
* Perhaps for some locales we will need to add more.
*/
switch (count) {
default:
g_assert_not_reached ();
/* fall through */
case 1:
format = FIRST_COPY_DUPLICATE_FORMAT;
break;
case 2:
format = SECOND_COPY_DUPLICATE_FORMAT;
break;
}
use_count = FALSE;
} else {
/* Handle special cases for the first few numbers of each ten.
* For locales where getting this exactly right is difficult,
* these can just be made all the same as the general case below.
*/
/* Handle special cases for x11th - x20th.
*/
switch (count % 100) {
case 11:
format = X11TH_COPY_DUPLICATE_FORMAT;
break;
case 12:
format = X12TH_COPY_DUPLICATE_FORMAT;
break;
case 13:
format = X13TH_COPY_DUPLICATE_FORMAT;
break;
default:
format = NULL;
break;
}
if (format == NULL) {
switch (count % 10) {
case 1:
format = ST_COPY_DUPLICATE_FORMAT;
break;
case 2:
format = ND_COPY_DUPLICATE_FORMAT;
break;
case 3:
format = RD_COPY_DUPLICATE_FORMAT;
break;
default:
/* The general case. */
format = TH_COPY_DUPLICATE_FORMAT;
break;
}
}
use_count = TRUE;
}
if (use_count)
result = g_strdup_printf (format, base, count, suffix);
else
result = g_strdup_printf (format, base, suffix);
if (max_length > 0 && (unshortened_length = strlen (result)) > max_length) {
char *new_base;
new_base = shorten_utf8_string (base, unshortened_length - max_length);
if (new_base) {
g_free (result);
if (use_count)
result = g_strdup_printf (format, new_base, count, suffix);
else
result = g_strdup_printf (format, new_base, suffix);
g_assert ((int)strlen (result) <= max_length);
g_free (new_base);
}
}
return result;
}
static char *
get_duplicate_name (const char *name, int count_increment, int max_length)
{
char *result;
char *name_base;
const char *suffix;
int count;
parse_previous_duplicate_name (name, &name_base, &suffix, &count);
result = make_next_duplicate_name (name_base, suffix, count + count_increment, max_length);
g_free (name_base);
return result;
}
static gboolean
has_invalid_xml_char (char *str)
{
gunichar c;
while (*str != 0) {
c = g_utf8_get_char (str);
/* characters XML permits */
if (!(c == 0x9 ||
c == 0xA ||
c == 0xD ||
(c >= 0x20 && c <= 0xD7FF) ||
(c >= 0xE000 && c <= 0xFFFD) ||
(c >= 0x10000 && c <= 0x10FFFF))) {
return TRUE;
}
str = g_utf8_next_char (str);
}
return FALSE;
}
static char *
custom_full_name_to_string (char *format, va_list va)
{
GFile *file;
file = va_arg (va, GFile *);
return g_file_get_parse_name (file);
}
static void
custom_full_name_skip (va_list *va)
{
(void) va_arg (*va, GFile *);
}
static char *
custom_basename_to_string (char *format, va_list va)
{
GFile *file;
GFileInfo *info;
char *name, *basename, *tmp;
file = va_arg (va, GFile *);
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
0,
g_cancellable_get_current (),
NULL);
name = NULL;
if (info) {
name = g_strdup (g_file_info_get_display_name (info));
g_object_unref (info);
}
if (name == NULL) {
basename = g_file_get_basename (file);
if (basename != NULL) {
if (g_utf8_validate (basename, -1, NULL)) {
name = basename;
} else {
name = g_uri_escape_string (basename, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
g_free (basename);
}
}
}
if (name == NULL) {
name = g_file_get_parse_name (file);
}
/* Some chars can't be put in the markup we use for the dialogs... */
if (has_invalid_xml_char (name)) {
tmp = name;
name = g_uri_escape_string (name, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
g_free (tmp);
}
/* Finally, if the string is too long, truncate it. */
if (name != NULL) {
tmp = name;
name = eel_str_middle_truncate (tmp, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
g_free (tmp);
}
return name;
}
static void
custom_basename_skip (va_list *va)
{
(void) va_arg (*va, GFile *);
}
static char *
custom_size_to_string (char *format, va_list va)
{
goffset size;
size = va_arg (va, goffset);
int prefix;
prefix = nemo_global_preferences_get_size_prefix_preference ();
return g_format_size_full (size, prefix);
}
static void
custom_size_skip (va_list *va)
{
(void) va_arg (*va, goffset);
}
static char *
custom_time_to_string (char *format, va_list va)
{
int secs;
secs = va_arg (va, int);
return format_time (secs);
}
static void
custom_time_skip (va_list *va)
{
(void) va_arg (*va, int);
}
static char *
custom_mount_to_string (char *format, va_list va)
{
GMount *mount;
mount = va_arg (va, GMount *);
return g_mount_get_name (mount);
}
static void
custom_mount_skip (va_list *va)
{
(void) va_arg (*va, GMount *);
}
static EelPrintfHandler handlers[] = {
{ 'F', custom_full_name_to_string, custom_full_name_skip },
{ 'B', custom_basename_to_string, custom_basename_skip },
{ 'S', custom_size_to_string, custom_size_skip },
{ 'T', custom_time_to_string, custom_time_skip },
{ 'V', custom_mount_to_string, custom_mount_skip },
{ 0 }
};
static char *
f (const char *format, ...) {
va_list va;
char *res;
va_start (va, format);
res = eel_strdup_vprintf_with_custom (handlers, format, va);
va_end (va);
return res;
}
static void
get_best_name (GFile *file, gchar **name)
{
gchar *out;
if (g_file_is_native (file)) {
gchar *path = g_file_get_path (file);
if (g_str_has_prefix (path, g_get_home_dir ())) {
GString *str = g_string_new (path);
str = g_string_erase (str, 0, strlen (g_get_home_dir ()));
str = g_string_prepend (str, "~");
out = g_string_free (str, FALSE);
} else {
out = g_strdup (path);
}
g_free (path);
} else {
out = g_file_get_basename (file);
if (out == NULL) {
out = g_file_get_parse_name (file);
}
}
*name = out;
}
static void
get_parent_name (GFile *file, gchar **name)
{
GFile *parent = g_file_get_parent (file);
if (!parent)
return;
gchar *get = NULL;
get_best_name (parent, &get);
g_object_unref (parent);
*name = get;
}
/* adapted from gio/glocalfile.c */
static gboolean
_g_local_file_delete (GFile *file,
GCancellable *cancellable,
GError **error)
{
gchar *path;