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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
|
/***************************************************************************
* brasero-file-node.c
*
* Sat Dec 1 14:48:46 2007
* Copyright 2007 Philippe Rouquier
* <bonfire-app@wanadoo.fr>
****************************************************************************/
/*
* Brasero 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.
*
* Brasero 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 Library 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
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <gio/gio.h>
#include "burn-basics.h"
#include "brasero-file-node.h"
#include "brasero-utils.h"
BraseroFileNode *
brasero_file_node_root_new (void)
{
BraseroFileNode *root;
root = g_new0 (BraseroFileNode, 1);
root->is_root = TRUE;
root->is_imported = TRUE;
root->union3.stats = g_new0 (BraseroFileTreeStats, 1);
return root;
}
BraseroFileNode *
brasero_file_node_get_root (BraseroFileNode *node,
guint *depth_retval)
{
BraseroFileNode *parent;
guint depth = 0;
parent = node;
while (parent) {
if (parent->is_root) {
if (depth_retval)
*depth_retval = depth;
return parent;
}
depth ++;
parent = parent->parent;
}
return NULL;
}
guint
brasero_file_node_get_depth (BraseroFileNode *node)
{
guint depth = 0;
while (node) {
if (node->is_root)
return depth;
depth ++;
node = node->parent;
}
return 0;
}
BraseroFileTreeStats *
brasero_file_node_get_tree_stats (BraseroFileNode *node,
guint *depth)
{
BraseroFileTreeStats *stats;
BraseroFileNode *root;
stats = BRASERO_FILE_NODE_STATS (node);
if (stats)
return stats;
root = brasero_file_node_get_root (node, depth);
stats = BRASERO_FILE_NODE_STATS (root);
return stats;
}
gint
brasero_file_node_sort_default_cb (gconstpointer obj_a, gconstpointer obj_b)
{
const BraseroFileNode *a = obj_a;
const BraseroFileNode *b = obj_b;
if (a->is_file == b->is_file)
return 0;
if (b->is_file)
return -1;
return 1;
}
gint
brasero_file_node_sort_name_cb (gconstpointer obj_a, gconstpointer obj_b)
{
gint res;
const BraseroFileNode *a = obj_a;
const BraseroFileNode *b = obj_b;
res = brasero_file_node_sort_default_cb (a, b);
if (res)
return res;
return strcmp (BRASERO_FILE_NODE_NAME (a), BRASERO_FILE_NODE_NAME (b));
}
gint
brasero_file_node_sort_size_cb (gconstpointer obj_a, gconstpointer obj_b)
{
gint res;
gint num_a, num_b;
const BraseroFileNode *a = obj_a;
const BraseroFileNode *b = obj_b;
res = brasero_file_node_sort_default_cb (a, b);
if (res)
return res;
if (a->is_file)
return BRASERO_FILE_NODE_SECTORS (a) - BRASERO_FILE_NODE_SECTORS (b);
/* directories */
num_a = brasero_file_node_get_n_children (a);
num_b = brasero_file_node_get_n_children (b);
return num_a - num_b;
}
gint
brasero_file_node_sort_mime_cb (gconstpointer obj_a, gconstpointer obj_b)
{
gint res;
const BraseroFileNode *a = obj_a;
const BraseroFileNode *b = obj_b;
res = brasero_file_node_sort_default_cb (a, b);
if (res)
return res;
return strcmp (BRASERO_FILE_NODE_NAME (a), BRASERO_FILE_NODE_NAME (b));
}
static BraseroFileNode *
brasero_file_node_insert (BraseroFileNode *head,
BraseroFileNode *node,
GCompareFunc sort_func,
guint *newpos)
{
BraseroFileNode *iter;
guint n = 0;
/* check for some special cases */
if (!head) {
node->next = NULL;
return node;
}
if (sort_func (head, node) > 0) {
/* head is after node */
node->next = head;
if (newpos)
*newpos = 0;
return node;
}
n = 1;
for (iter = head; iter->next; iter = iter->next) {
if (sort_func (iter->next, node) > 0) {
/* iter->next should be located after node */
node->next = iter->next;
iter->next = node;
if (newpos)
*newpos = n;
return head;
}
n ++;
}
/* append it */
iter->next = node;
node->next = NULL;
if (newpos)
*newpos = n;
return head;
}
gint *
brasero_file_node_need_resort (BraseroFileNode *node,
GCompareFunc sort_func)
{
BraseroFileNode *previous;
BraseroFileNode *parent;
BraseroFileNode *head;
gint *array = NULL;
guint newpos = 0;
guint oldpos;
guint size;
parent = node->parent;
head = BRASERO_FILE_NODE_CHILDREN (parent);
/* find previous node and get old position */
if (head != node) {
previous = head;
oldpos = 0;
while (previous->next != node) {
previous = previous->next;
oldpos ++;
}
oldpos ++;
}
else {
previous = NULL;
oldpos = 0;
}
/* see where we should start from head or from node->next */
if (previous && sort_func (previous, node) > 0) {
gint i;
/* move on the left */
previous->next = node->next;
head = brasero_file_node_insert (head, node, sort_func, &newpos);
parent->union2.children = head;
/* create an array to reflect the changes */
size = brasero_file_node_get_n_children (parent);
array = g_new0 (gint, size);
for (i = 0; i < size; i ++) {
if (i == newpos)
array [i] = oldpos;
else if (i > newpos && i <= oldpos)
array [i] = i - 1;
else
array [i] = i;
}
}
else if (node->next && sort_func (node, node->next) > 0) {
gint i;
/* move on the right */
if (previous)
previous->next = node->next;
else
parent->union2.children = node->next;
/* NOTE: here we're sure head hasn't changed since we checked
* that node should go after node->next (given as head for the
* insertion here) */
brasero_file_node_insert (node->next, node, sort_func, &newpos);
/* we started from oldpos so newpos needs updating */
newpos += oldpos;
/* create an array to reflect the changes */
size = brasero_file_node_get_n_children (parent);
array = g_new0 (gint, size);
for (i = 0; i < size; i ++) {
if (i == newpos)
array [i] = oldpos;
else if (i >= oldpos && i < newpos)
array [i] = i + 1;
else
array [i] = i;
}
}
return array;
}
gint *
brasero_file_node_sort_children (BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileNode *new_order = NULL;
BraseroFileNode *iter;
BraseroFileNode *next;
gint *array = NULL;
gint num_children;
guint oldpos = 1;
guint newpos;
new_order = BRASERO_FILE_NODE_CHILDREN (parent);
/* check for some special cases */
if (!new_order)
return NULL;
if (!new_order->next)
return NULL;
/* make the array */
num_children = brasero_file_node_get_n_children (parent);
array = g_new (gint, num_children);
next = new_order->next;
new_order->next = NULL;
array [0] = 0;
for (iter = next; iter; iter = next, oldpos ++) {
/* unlink iter */
next = iter->next;
iter->next = NULL;
newpos = 0;
new_order = brasero_file_node_insert (new_order,
iter,
sort_func,
&newpos);
if (newpos < oldpos)
memmove (array + newpos + 1, array + newpos, (oldpos - newpos) * sizeof (guint));
array [newpos] = oldpos;
}
/* set the new order */
parent->union2.children = new_order;
return array;
}
gint *
brasero_file_node_reverse_children (BraseroFileNode *parent)
{
BraseroFileNode *previous;
BraseroFileNode *last;
BraseroFileNode *iter;
gint firstfile = 0;
gint *array;
gint size;
gint i;
/* when reversing the list of children the only thing we must pay
* attention to is to keep directories first; so we do it in two passes
* first order the directories and then the files */
last = BRASERO_FILE_NODE_CHILDREN (parent);
/* special case */
if (!last || !last->next)
return NULL;
previous = last;
iter = last->next;
size = 1;
if (!last->is_file) {
while (!iter->is_file) {
BraseroFileNode *next;
next = iter->next;
iter->next = previous;
size ++;
if (!next) {
/* No file afterwards */
parent->union2.children = iter;
last->next = NULL;
firstfile = size;
goto end;
}
previous = iter;
iter = next;
}
/* the new head is the last processed node */
parent->union2.children = previous;
firstfile = size;
previous = iter;
iter = iter->next;
previous->next = NULL;
}
while (iter) {
BraseroFileNode *next;
next = iter->next;
iter->next = previous;
size ++;
previous = iter;
iter = next;
}
/* NOTE: iter is NULL here */
if (last->is_file) {
last->next = NULL;
parent->union2.children = previous;
}
else
last->next = previous;
end:
array = g_new (gint, size);
for (i = 0; i < firstfile; i ++)
array [i] = firstfile - i - 1;
for (i = firstfile; i < size; i ++)
array [i] = size - i + firstfile - 1;
return array;
}
BraseroFileNode *
brasero_file_node_nth_child (BraseroFileNode *parent,
guint nth)
{
BraseroFileNode *peers;
guint pos;
peers = BRASERO_FILE_NODE_CHILDREN (parent);
for (pos = 0; pos < nth && peers; pos ++)
peers = peers->next;
return peers;
}
guint
brasero_file_node_get_n_children (const BraseroFileNode *node)
{
BraseroFileNode *children;
guint num = 0;
for (children = BRASERO_FILE_NODE_CHILDREN (node); children; children = children->next)
num ++;
return num;
}
guint
brasero_file_node_get_pos_as_child (BraseroFileNode *node)
{
BraseroFileNode *parent;
BraseroFileNode *peers;
guint pos = 0;
parent = node->parent;
for (peers = BRASERO_FILE_NODE_CHILDREN (parent); peers; peers = peers->next) {
if (peers == node)
break;
pos ++;
}
return pos;
}
gboolean
brasero_file_node_is_ancestor (BraseroFileNode *parent,
BraseroFileNode *node)
{
while (node && node != parent)
node = node->parent;
if (!node)
return FALSE;
return TRUE;
}
BraseroFileNode *
brasero_file_node_check_name_existence (BraseroFileNode *parent,
const gchar *name)
{
BraseroFileNode *iter;
if (name && name [0] == '\0')
return NULL;
iter = BRASERO_FILE_NODE_CHILDREN (parent);
for (; iter; iter = iter->next) {
if (!strcmp (name, BRASERO_FILE_NODE_NAME (iter)))
return iter;
}
return NULL;
}
BraseroFileNode *
brasero_file_node_check_imported_sibling (BraseroFileNode *node)
{
BraseroFileNode *parent;
BraseroFileNode *iter;
BraseroImport *import;
parent = node->parent;
/* That could happen if a node is moved to a location where another node
* (to be removed) has the same name and is a parent of this node */
if (!parent)
return NULL;
/* See if among the imported children of the parent one of them
* has the same name as the node being removed. If so, restore
* it with all its imported children (provided that's a
* directory). */
import = BRASERO_FILE_NODE_IMPORT (parent);
if (!import)
return NULL;
iter = import->replaced;
if (!strcmp (BRASERO_FILE_NODE_NAME (iter), BRASERO_FILE_NODE_NAME (node))) {
/* A match, remove it from the list and return it */
import->replaced = iter->next;
if (!import->replaced) {
/* no more imported saved import structure */
parent->union1.name = import->name;
parent->has_import = FALSE;
g_free (import);
}
iter->next = NULL;
return iter;
}
for (; iter->next; iter = iter->next) {
if (!strcmp (BRASERO_FILE_NODE_NAME (iter->next), BRASERO_FILE_NODE_NAME (node))) {
BraseroFileNode *removed;
/* There is one match, remove it from the list */
removed = iter->next;
iter->next = removed->next;
removed->next = NULL;
return removed;
}
}
return NULL;
}
gchar *
brasero_file_node_validate_utf8_name (const gchar *name)
{
gchar *retval, *ptr;
const gchar *invalid;
if (!name)
return NULL;
if (g_utf8_validate (name, -1, &invalid))
return NULL;
retval = g_strdup (name);
ptr = retval + (invalid - name);
*ptr = '_';
ptr++;
while (!g_utf8_validate (ptr, -1, &invalid)) {
ptr = (gchar*) invalid;
*ptr = '?';
ptr ++;
}
return retval;
}
void
brasero_file_node_graft (BraseroFileNode *file_node,
BraseroURINode *uri_node)
{
BraseroGraft *graft;
if (!file_node->is_grafted) {
BraseroFileNode *parent;
graft = g_new (BraseroGraft, 1);
graft->name = file_node->union1.name;
file_node->union1.graft = graft;
file_node->is_grafted = TRUE;
/* since it wasn't grafted propagate the size change; that is
* substract the current node size from the parent nodes until
* the parent graft point. */
for (parent = file_node->parent; parent && !parent->is_root; parent = parent->parent) {
parent->union3.sectors -= BRASERO_FILE_NODE_SECTORS (file_node);
if (parent->is_grafted)
break;
}
}
else {
BraseroURINode *old_uri_node;
graft = BRASERO_FILE_NODE_GRAFT (file_node);
old_uri_node = graft->node;
if (old_uri_node == uri_node)
return;
old_uri_node->nodes = g_slist_remove (old_uri_node->nodes, file_node);
}
graft->node = uri_node;
uri_node->nodes = g_slist_prepend (uri_node->nodes, file_node);
}
void
brasero_file_node_ungraft (BraseroFileNode *node)
{
BraseroGraft *graft;
BraseroFileNode *parent;
if (!node->is_grafted)
return;
graft = node->union1.graft;
/* Remove it from the URINode list of grafts */
graft->node->nodes = g_slist_remove (graft->node->nodes, node);
/* The name must be exactly the one of the URI*/
node->is_grafted = FALSE;
node->union1.name = graft->name;
/* Removes the graft */
g_free (graft);
/* Propagate the size change up the parents to the next
* grafted parent in the tree (if any). */
for (parent = node->parent; parent && !parent->is_root; parent = parent->parent) {
parent->union3.sectors += BRASERO_FILE_NODE_SECTORS (node);
if (parent->is_grafted)
break;
}
}
void
brasero_file_node_rename (BraseroFileNode *node,
const gchar *name)
{
g_free (BRASERO_FILE_NODE_NAME (node));
if (node->is_grafted)
node->union1.graft->name = g_strdup (name);
else
node->union1.name = g_strdup (name);
}
void
brasero_file_node_add (BraseroFileNode *parent,
BraseroFileNode *node,
GCompareFunc sort_func)
{
parent->union2.children = brasero_file_node_insert (BRASERO_FILE_NODE_CHILDREN (parent),
node,
sort_func,
NULL);
node->parent = parent;
if (!node->is_imported) {
guint depth = 0;
if (!node->is_grafted) {
/* propagate the size change*/
for (; parent && !parent->is_root; parent = parent->parent) {
parent->union3.sectors += BRASERO_FILE_NODE_SECTORS (node);
if (parent->is_grafted)
break;
}
}
if (depth > 6)
node->is_deep = TRUE;
}
}
void
brasero_file_node_set_from_info (BraseroFileNode *node,
BraseroFileTreeStats *stats,
GFileInfo *info)
{
/* NOTE: the name will never be replaced here since that means
* we could replace a previously set name (that triggered the
* creation of a graft). If someone wants to set a new name,
* then rename_node is the function. */
/* update the stats since a file could have been added to the tree but
* at this point we didn't know what it was (a file or a directory).
* Only do this if it wasn't a file before. */
if (!node->is_file
&& (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)) {
/* only count files */
stats->children ++;
}
/* update :
* - the mime type
* - the size (and possibly the one of his parent)
* - the type */
node->is_file = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
node->is_fake = FALSE;
node->is_loading = FALSE;
node->is_imported = FALSE;
node->is_reloading = FALSE;
node->is_symlink = (g_file_info_get_is_symlink (info));
if (node->is_file) {
guint sectors;
gint sectors_diff;
/* register mime type string */
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE)) {
const gchar *mime;
if (BRASERO_FILE_NODE_MIME (node))
brasero_utils_unregister_string (BRASERO_FILE_NODE_MIME (node));
mime = g_file_info_get_content_type (info);
node->union2.mime = brasero_utils_register_string (mime);
}
sectors = BRASERO_SIZE_TO_SECTORS (g_file_info_get_size (info), 2048);
if (sectors > BRASERO_FILE_2G_LIMIT && BRASERO_FILE_NODE_SECTORS (node) <= BRASERO_FILE_2G_LIMIT)
stats->num_2Gio ++;
else if (sectors <= BRASERO_FILE_2G_LIMIT && BRASERO_FILE_NODE_SECTORS (node) > BRASERO_FILE_2G_LIMIT)
stats->num_2Gio --;
/* The node isn't grafted and it's a file. So we must propagate
* its size up to the parent graft node. */
/* NOTE: we used to accumulate all the directory contents till
* the end and process all of entries at once, when it was
* finished. We had to do that to calculate the whole size. */
sectors_diff = sectors - BRASERO_FILE_NODE_SECTORS (node);
for (; node; node = node->parent) {
node->union3.sectors += sectors_diff;
if (node->is_grafted)
break;
}
}
else /* since that's directory then it must be explored now */
node->is_exploring = TRUE;
}
gchar *
brasero_file_node_get_uri_name (const gchar *uri)
{
gchar *unescaped_name;
GFile *vfs_uri;
gchar *name;
/* g_path_get_basename is not comfortable with uri related
* to the root directory so check that before */
vfs_uri = g_file_new_for_uri (uri);
name = g_file_get_basename (vfs_uri);
g_object_unref (vfs_uri);
unescaped_name = g_uri_unescape_string (name, NULL);
g_free (name);
/* NOTE: a graft should be added for non utf8 name since we
* modify them; in fact we use this function only in the next
* one which creates only grafted nodes. */
name = brasero_file_node_validate_utf8_name (unescaped_name);
if (name) {
g_free (unescaped_name);
return name;
}
return unescaped_name;
}
BraseroFileNode *
brasero_file_node_new_loading (const gchar *name,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileNode *node;
node = g_new0 (BraseroFileNode, 1);
node->union1.name = g_strdup (name);
node->is_loading = TRUE;
brasero_file_node_add (parent, node, sort_func);
return node;
}
BraseroFileNode *
brasero_file_node_new_from_info (GFileInfo *info,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileNode *node;
BraseroFileTreeStats *stats;
node = g_new0 (BraseroFileNode, 1);
node->union1.name = g_strdup (g_file_info_get_name (info));
stats = brasero_file_node_get_tree_stats (parent, NULL);
brasero_file_node_set_from_info (node, stats, info);
/* This must be done after above function */
brasero_file_node_add (parent, node, sort_func);
return node;
}
BraseroFileNode *
brasero_file_node_new_imported_session_file (BraseroVolFile *file,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileNode *node;
/* Create the node information */
node = g_new0 (BraseroFileNode, 1);
node->union1.name = g_strdup (BRASERO_VOLUME_FILE_NAME (file));
node->is_file = (file->isdir == FALSE);
node->is_imported = TRUE;
if (node->is_file)
node->union3.sectors = BRASERO_SIZE_TO_SECTORS (BRASERO_VOLUME_FILE_SIZE (file), 2048);
/* Add it (we must add a graft) */
brasero_file_node_add (parent, node, sort_func);
return node;
}
BraseroFileNode *
brasero_file_node_new_empty_folder (const gchar *name,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileNode *node;
/* Create the node information */
node = g_new0 (BraseroFileNode, 1);
node->union1.name = g_strdup (name);
node->is_fake = TRUE;
/* Add it (we must add a graft) */
brasero_file_node_add (parent, node, sort_func);
return node;
}
void
brasero_file_node_unlink (BraseroFileNode *node)
{
BraseroFileNode *iter;
BraseroImport *import;
if (!node->parent)
return;
iter = BRASERO_FILE_NODE_CHILDREN (node->parent);
/* handle the size change for previous parent */
if (!node->is_grafted && !node->is_imported) {
BraseroFileNode *parent;
/* handle the size change if it wasn't grafted */
for (parent = node->parent; parent && !parent->is_root; parent = parent->parent) {
parent->union3.sectors -= BRASERO_FILE_NODE_SECTORS (node);
if (parent->is_grafted)
break;
}
}
node->is_deep = FALSE;
if (iter == node) {
node->parent->union2.children = node->next;
node->parent = NULL;
node->next = NULL;
return;
}
for (; iter->next; iter = iter->next) {
if (iter->next == node) {
iter->next = node->next;
node->parent = NULL;
node->next = NULL;
return;
}
}
if (!node->is_imported || !node->parent->has_import)
return;
/* It wasn't found among the parent children. If parent is imported and
* the node is imported as well then check if it isn't among the import
* children */
import = BRASERO_FILE_NODE_IMPORT (node->parent);
iter = import->replaced;
if (iter == node) {
import->replaced = iter->next;
node->parent = NULL;
node->next = NULL;
return;
}
for (; iter->next; iter = iter->next) {
if (iter->next == node) {
iter->next = node->next;
node->parent = NULL;
node->next = NULL;
return;
}
}
}
void
brasero_file_node_move_from (BraseroFileNode *node,
BraseroFileTreeStats *stats)
{
gboolean was_deep;
/* NOTE: for the time being no backend supports moving imported files */
if (node->is_imported)
return;
was_deep = (brasero_file_node_get_depth (node) > 6);
if (was_deep)
stats->num_deep --;
brasero_file_node_unlink (node);
}
void
brasero_file_node_move_to (BraseroFileNode *node,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroFileTreeStats *stats;
guint depth;
/* NOTE: for the time being no backend supports moving imported files */
if (node->is_imported)
return;
/* reinsert it now at the new location */
parent->union2.children = brasero_file_node_insert (BRASERO_FILE_NODE_CHILDREN (parent),
node,
sort_func,
NULL);
node->parent = parent;
if (!node->is_grafted) {
BraseroFileNode *parent;
/* propagate the size change for new parent */
for (parent = node->parent; parent && !parent->is_root; parent = parent->parent) {
parent->union3.sectors += BRASERO_FILE_NODE_SECTORS (node);
if (parent->is_grafted)
break;
}
}
/* NOTE: here stats about the tree can change if the parent has a depth
* > 6 and if previous didn't. Other stats remains unmodified. */
stats = brasero_file_node_get_tree_stats (parent, &depth);
if (!depth > 6) {
stats->num_deep ++;
node->is_deep = TRUE;
}
}
static void
brasero_file_node_destroy_with_children (BraseroFileNode *node,
BraseroFileTreeStats *stats)
{
BraseroFileNode *child;
BraseroFileNode *next;
BraseroImport *import;
BraseroGraft *graft;
/* destroy all children recursively */
for (child = BRASERO_FILE_NODE_CHILDREN (node); child; child = next) {
next = child->next;
brasero_file_node_destroy_with_children (child, stats);
}
/* update all statistics on tree if any */
if (stats) {
/* check if that's a 2 Gio file */
if (node->is_2Gio)
stats->num_2Gio --;
/* check if that's a deep directory file */
if (node->is_deep)
stats->num_deep --;
/* update file number statistics */
if (!node->is_imported && node->is_file)
stats->children --;
}
/* destruction */
import = BRASERO_FILE_NODE_IMPORT (node);
graft = BRASERO_FILE_NODE_GRAFT (node);
if (graft) {
BraseroURINode *uri_node;
uri_node = graft->node;
/* Handle removal from BraseroURINode struct */
if (uri_node)
uri_node->nodes = g_slist_remove (uri_node->nodes, node);
g_free (graft->name);
g_free (graft);
}
else if (import) {
/* if imported then destroy the saved children */
for (child = import->replaced; child; child = next) {
next = child->next;
brasero_file_node_destroy_with_children (child, stats);
}
g_free (import->name);
g_free (import);
}
else
g_free (BRASERO_FILE_NODE_NAME (node));
/* destroy the node */
if (node->is_file && !node->is_imported && BRASERO_FILE_NODE_MIME (node))
brasero_utils_unregister_string (BRASERO_FILE_NODE_MIME (node));
if (node->is_root)
g_free (BRASERO_FILE_NODE_STATS (node));
g_free (node);
}
/**
* Destroy a node and its children updating the tree stats.
* If it isn't unlinked yet, it does it.
*/
void
brasero_file_node_destroy (BraseroFileNode *node,
BraseroFileTreeStats *stats)
{
/* remove from the parent children list or more probably from the
* import list. */
if (node->parent)
brasero_file_node_unlink (node);
/* traverse the whole tree and free children updating tree stats */
brasero_file_node_destroy_with_children (node, stats);
}
/**
* Pre-remove function that unparent a node (before a possible destruction).
* If node is imported, it saves it in its parent, destroys all child nodes
* that are not imported and restore children that were imported.
* NOTE: tree stats are only updated if the node is imported.
*/
static void
brasero_file_node_save_imported_children (BraseroFileNode *node,
BraseroFileTreeStats *stats,
GCompareFunc sort_func)
{
BraseroFileNode *iter;
BraseroImport *import;
/* clean children */
for (iter = BRASERO_FILE_NODE_CHILDREN (node); iter; iter = iter->next) {
if (!iter->is_imported)
brasero_file_node_destroy_with_children (iter, stats);
if (!iter->is_file)
brasero_file_node_save_imported_children (iter, stats, sort_func);
}
/* restore all replaced children */
import = BRASERO_FILE_NODE_IMPORT (node);
if (!import)
return;
for (iter = import->replaced; iter; iter = iter->next)
brasero_file_node_insert (iter, node, sort_func, NULL);
/* remove import */
node->union1.name = import->name;
node->has_import = FALSE;
g_free (import);
}
void
brasero_file_node_save_imported (BraseroFileNode *node,
BraseroFileTreeStats *stats,
BraseroFileNode *parent,
GCompareFunc sort_func)
{
BraseroImport *import;
/* if it isn't imported return */
if (!node->is_imported)
return;
/* Remove all the children that are not imported. Also restore
* all children that were replaced so as to restore the original
* order of files. */
/* that shouldn't happen since root itself is considered imported */
if (!parent || !parent->is_imported)
return;
/* save the node in its parent import structure */
import = BRASERO_FILE_NODE_IMPORT (parent);
if (!import) {
import = g_new0 (BraseroImport, 1);
import->name = BRASERO_FILE_NODE_NAME (parent);
parent->union1.import = import;
parent->has_import = TRUE;
}
/* unlink it and add it to the list */
brasero_file_node_unlink (node);
node->next = import->replaced;
import->replaced = node;
node->parent = parent;
/* Explore children and remove not imported ones and restore.
* Update the tree stats at the same time.
* NOTE: here the tree stats are only used for the grafted children that
* are not imported in the tree. */
brasero_file_node_save_imported_children (node, stats, sort_func);
}
|