This repository was archived by the owner on Nov 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscan.c
More file actions
4079 lines (3539 loc) · 148 KB
/
scan.c
File metadata and controls
4079 lines (3539 loc) · 148 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
/* scan.c - 2000/06/16 */
/*
* EasyTAG - Tag editor for MP3 and Ogg Vorbis files
* Copyright (C) 2000-2003 Jerome Couderc <easytag@gmail.com>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <gtk/gtk.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>
#include <config.h>
#include <glib/gi18n-lib.h>
#include "gtk2_compat.h"
#include "scan.h"
#include "easytag.h"
#include "prefs.h"
#include "setting.h"
#include "id3_tag.h"
#include "bar.h"
#include "browser.h"
#include "log.h"
#include "misc.h"
#include "et_core.h"
#include "crc32.h"
#include "charset.h"
#define step(a,b) (b-a)+1
/****************
* Declarations *
****************/
GtkWidget *DummyEntry = NULL; /* Used to simulate a gtkentry widget for mask code '%i' */
GtkWidget *ScanTagMaskCombo = NULL;
GtkWidget *RenameFileMaskCombo = NULL;
GtkWidget *ScannerOptionCombo = NULL;
GtkWidget *RenameFilePrefixPathButton = NULL;
GtkWidget *ScanTagFrame;
GtkWidget *RenameFileFrame;
GtkWidget *ProcessFieldsFrame;
GtkWidget *FillTagPreviewLabel = NULL;
GtkWidget *RenameFilePreviewLabel = NULL;
GtkListStore *RenameFileListModel;
GtkListStore *ScanTagListModel;
GtkWidget *ProcessFileNameField;
GtkWidget *ProcessTitleField;
GtkWidget *ProcessArtistField;
GtkWidget *ProcessAlbumArtistField;
GtkWidget *ProcessAlbumField;
GtkWidget *ProcessGenreField;
GtkWidget *ProcessCommentField;
GtkWidget *ProcessComposerField;
GtkWidget *ProcessOrigArtistField;
GtkWidget *ProcessCopyrightField;
GtkWidget *ProcessURLField;
GtkWidget *ProcessEncodedByField;
GtkWidget *ProcessFieldsConvertIntoSpace = NULL;
GtkWidget *ProcessFieldsConvertSpace = NULL;
GtkWidget *ProcessFieldsConvert = NULL;
GtkWidget *ProcessFieldsConvertLabelTo;
GtkWidget *ProcessFieldsConvertTo = NULL;
GtkWidget *ProcessFieldsConvertFrom = NULL;
GtkWidget *ProcessFieldsAllUppercase = NULL;
GtkWidget *ProcessFieldsAllDowncase = NULL;
GtkWidget *ProcessFieldsFirstLetterUppercase = NULL;
GtkWidget *ProcessFieldsFirstLettersUppercase = NULL;
GtkWidget *ProcessFieldsDetectRomanNumerals = NULL;
GtkWidget *ProcessFieldsRemoveSpace = NULL;
GtkWidget *ProcessFieldsInsertSpace = NULL;
GtkWidget *ProcessFieldsOnlyOneSpace = NULL;
GtkWidget *LegendFrame = NULL;
GtkWidget *LegendButton = NULL;
GtkWidget *MaskEditorButton = NULL;
GtkWidget *MaskEditorFrame = NULL;
GtkWidget *MaskEditorVBox;
GtkWidget *MaskEditorHBox;
GtkWidget *MaskEditorScrollWindow;
GtkWidget *MaskEditorList;
GtkWidget *MaskEditorEntry;
GtkWidget *MaskEditorNewButton;
GtkWidget *MaskEditorCopyButton;
GtkWidget *MaskEditorAddButton;
GtkWidget *MaskEditorRemoveButton;
GtkWidget *MaskEditorUpButton;
GtkWidget *MaskEditorDownButton;
GtkWidget *MaskEditorSaveButton;
/* Some predefined masks -- IMPORTANT: Null-terminate me! */
gchar *Scan_Masks [] =
{
"%a - %b"G_DIR_SEPARATOR_S"%n - %t",
"%a_-_%b"G_DIR_SEPARATOR_S"%n_-_%t",
"%a - %b (%y)"G_DIR_SEPARATOR_S"%n - %a - %t",
"%a_-_%b_(%y)"G_DIR_SEPARATOR_S"%n_-_%a_-_%t",
"%a - %b (%y) - %g"G_DIR_SEPARATOR_S"%n - %a - %t",
"%a_-_%b_(%y)_-_%g"G_DIR_SEPARATOR_S"%n_-_%a_-_%t",
"%a - %b"G_DIR_SEPARATOR_S"%n. %t",
"%a_-_%b"G_DIR_SEPARATOR_S"%n._%t",
"%a-%b"G_DIR_SEPARATOR_S"%n-%t",
"%b"G_DIR_SEPARATOR_S"%n. %a - %t",
"%b"G_DIR_SEPARATOR_S"%n._%a_-_%t",
"%b"G_DIR_SEPARATOR_S"%n - %a - %t",
"%b"G_DIR_SEPARATOR_S"%n_-_%a_-_%t",
"%b"G_DIR_SEPARATOR_S"%n-%a-%t",
"%a-%b"G_DIR_SEPARATOR_S"%n-%t",
"%a"G_DIR_SEPARATOR_S"%b"G_DIR_SEPARATOR_S"%n. %t",
"%g"G_DIR_SEPARATOR_S"%a"G_DIR_SEPARATOR_S"%b"G_DIR_SEPARATOR_S"%t",
"%a_-_%b-%n-%t-%y",
"%a - %b"G_DIR_SEPARATOR_S"%n. %t(%c)",
"%t",
"Track%n",
"Track%i %n",
NULL
};
gchar *Rename_File_Masks [] =
{
"%n - %a - %t",
"%n_-_%a_-_%t",
"%n. %a - %t",
"%n._%a_-_%t",
"%a - %b"G_DIR_SEPARATOR_S"%n - %t",
"%a_-_%b"G_DIR_SEPARATOR_S"%n_-_%t",
"%a - %b (%y) - %g"G_DIR_SEPARATOR_S"%n - %t",
"%a_-_%b_(%y)_-_%g"G_DIR_SEPARATOR_S"%n_-_%t",
"%n - %t",
"%n_-_%t",
"%n. %t",
"%n._%t",
"%n - %a - %b - %t",
"%n_-_%a_-_%b_-_%t",
"%a - %b - %t",
"%a_-_%b_-_%t",
"%a - %b - %n - %t",
"%a_-_%b_-_%n_-_%t",
"%a - %t",
"%a_-_%t",
"Track %n",
NULL
};
/**gchar *Rename_Directory_Masks [] =
{
"%a - %b",
"%a_-_%b",
"%a - %b (%y) - %g",
"%a_-_%b_(%y)_-_%g",
"VA - %b (%y)",
"VA_-_%b_(%y)",
NULL
};**/
gchar *Scanner_Option_Menu_Items [] =
{
N_("Fill Tag"),
N_("Rename File and Directory"),
N_("Process Fields")
};
typedef enum
{
UNKNOWN = 0, /* Default value when initialized */
LEADING_SEPARATOR, /* characters before the first code */
TRAILING_SEPARATOR, /* characters after the last code */
SEPARATOR, /* item is a separator between two codes */
DIRECTORY_SEPARATOR, /* item is a separator between two codes with character '/' (G_DIR_SEPARATOR) */
FIELD, /* item contains text (not empty) of entry */
EMPTY_FIELD /* item when entry contains no text */
} Mask_Item_Type;
/*
* Used into Rename File Scanner
*/
typedef struct _File_Mask_Item File_Mask_Item;
struct _File_Mask_Item
{
Mask_Item_Type type;
gchar *string;
};
/*
* Used into Scan Tag Scanner
*/
typedef struct _Scan_Mask_Item Scan_Mask_Item;
struct _Scan_Mask_Item
{
gchar code; // The code of the mask without % (ex: %a => a)
gchar *string; // The string found by the scanner for the code defined the line above
};
/**************
* Prototypes *
**************/
void ScannerWindow_Quit (void);
gboolean ScannerWindow_Key_Press (GtkWidget *window, GdkEvent *event);
void Scan_Toggle_Legend_Button (void);
void Scan_Toggle_Mask_Editor_Button (void);
gchar *Scan_Replace_String (gchar *string, gchar *last, gchar *new);
void Scan_Option_Button (void);
gboolean Scan_Check_Scan_Tag_Mask (GtkWidget *widget_to_show_hide, GtkEntry *widget_source);
gboolean Scan_Check_Rename_File_Mask (GtkWidget *widget_to_show_hide, GtkEntry *widget_source);
gboolean Scan_Check_Editor_Mask (GtkWidget *widget_to_show_hide, GtkEntry *widget_source);
gchar *Scan_Generate_New_Filename_From_Mask (ET_File *ETFile, gchar *mask, gboolean no_dir_check_or_conversion);
GList *Scan_Generate_New_Tag_From_Mask (ET_File *ETFile, gchar *mask);
void Scan_Rename_File_Generate_Preview (void);
void Scan_Rename_File_Prefix_Path (void);
void Scan_Fill_Tag_Generate_Preview (void);
void Scan_Free_File_Rename_List (GList *list);
void Scan_Free_File_Fill_Tag_List (GList *list);
void Scan_Rename_Directory_Generate_Preview (void);
gchar **Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, gchar code);
void Scan_Process_Fields_Functions (gchar **string);
gint Scan_Word_Is_Roman_Numeral (gchar *text);
void Process_Fields_Check_Button_Toggled (GtkWidget *object, GList *list);
void Process_Fields_Convert_Check_Button_Toggled (GtkWidget *object);
void Process_Fields_First_Letters_Check_Button_Toggled (GtkWidget *object);
void Select_Fields_Invert_Selection (void);
void Select_Fields_Select_Unselect_All (void);
void Select_Fields_Set_Sensitive (void);
void Mask_Editor_List_Row_Selected (GtkTreeSelection* selection, gpointer data);
void Mask_Editor_List_Set_Row_Visible (GtkTreeModel *treeModel, GtkTreeIter *rowIter);
void Mask_Editor_List_New (void);
void Mask_Editor_List_Duplicate (void);
void Mask_Editor_List_Add (void);
void Mask_Editor_List_Remove (void);
void Mask_Editor_List_Move_Up (void);
void Mask_Editor_List_Move_Down (void);
void Mask_Editor_List_Save_Button (void);
void Mask_Editor_Entry_Changed (void);
gboolean Mask_Editor_List_Key_Press (GtkWidget *widget, GdkEvent *event);
void Mask_Editor_Clean_Up_Masks_List (void);
void Scanner_Option_Menu_Activate_Item (GtkWidget *widget, gpointer data);
int roman2int (const char *str);
const char * int2roman (int num);
char * int2roman_r (int num, char * str, size_t len);
/*************
* *
* Functions *
* *
*************/
void Init_ScannerWindow (void)
{
ScannerWindow = NULL;
ScannerOptionCombo= NULL;
SWScanButton = NULL;
}
/*
* Uses the filename and path to fill tag information
* Note: mask and source are read from the right to the left
*/
void Scan_Tag_With_Mask (ET_File *ETFile)
{
GList *fill_tag_list = NULL;
gchar **dest = NULL;
gchar *mask; // The 'mask' in the entry
gchar *filename_utf8;
File_Tag *FileTag;
if (!ScannerWindow || !ScanTagMaskCombo || !ETFile) return;
mask = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(ScanTagMaskCombo)))));
if (!mask) return;
// Create a new File_Tag item
FileTag = ET_File_Tag_Item_New();
ET_Copy_File_Tag_Item(ETFile,FileTag);
// Process this mask with file
fill_tag_list = Scan_Generate_New_Tag_From_Mask(ETFile,mask);
while (fill_tag_list)
{
Scan_Mask_Item *mask_item = fill_tag_list->data;
// Get the target entry for this code
dest = Scan_Return_File_Tag_Field_From_Mask_Code(FileTag,mask_item->code);
// We display the text affected to the code
if ( dest && ( OVERWRITE_TAG_FIELD || *dest==NULL || strlen(*dest)==0 ) )
ET_Set_Field_File_Tag_Item(dest,mask_item->string);
if (!fill_tag_list->next) break;
fill_tag_list = fill_tag_list->next;
}
Scan_Free_File_Fill_Tag_List(fill_tag_list);
// Set the default text to comment
if (SET_DEFAULT_COMMENT && (OVERWRITE_TAG_FIELD || FileTag->comment==NULL || strlen(FileTag->comment)==0 ) )
ET_Set_Field_File_Tag_Item((void *)&FileTag->comment,DEFAULT_COMMENT);
// Set CRC-32 value as default comment (for files with ID3 tag only ;-)
if (SET_CRC32_COMMENT && (OVERWRITE_TAG_FIELD || FileTag->comment==NULL || strlen(FileTag->comment)==0 ) )
{
gulong crc32_value = 0;
gchar *buffer;
ET_File_Description *ETFileDescription;
ETFileDescription = ETFile->ETFileDescription;
switch (ETFileDescription->TagType)
{
case ID3_TAG:
crc32_file_with_ID3_tag( ((File_Name *)((GList *)ETFile->FileNameNew)->data)->value, &crc32_value);
if (crc32_value > 0)
{
buffer = g_strdup_printf("%.8lx",crc32_value);
ET_Set_Field_File_Tag_Item((void *)&FileTag->comment,buffer);
g_free(buffer);
}
break;
default:
break;
}
}
// Save changes of the 'File_Tag' item
ET_Manage_Changes_Of_File_Data(ETFile,NULL,FileTag);
g_free(mask);
Statusbar_Message(_("Tag successfully scanned…"),TRUE);
filename_utf8 = g_path_get_basename( ((File_Name *)ETFile->FileNameNew->data)->value_utf8 );
Log_Print(LOG_OK,_("Tag successfully scanned…(%s)"),filename_utf8);
g_free(filename_utf8);
}
GList *Scan_Generate_New_Tag_From_Mask (ET_File *ETFile, gchar *mask)
{
GList *fill_tag_list = NULL;
gchar *filename_utf8;
gchar *tmp;
gchar *buf;
gchar *separator;
gchar *string;
gint len, i, loop=0;
gchar **mask_splitted;
gchar **file_splitted;
guint mask_splitted_number;
guint file_splitted_number;
guint mask_splitted_index;
guint file_splitted_index;
Scan_Mask_Item *mask_item;
if (!ETFile || !mask) return NULL;
filename_utf8 = g_strdup(((File_Name *)((GList *)ETFile->FileNameNew)->data)->value_utf8);
if (!filename_utf8) return NULL;
// Remove extension of file (if found)
tmp = strrchr(filename_utf8,'.');
for (i=0; i<=(gint)ET_FILE_DESCRIPTION_SIZE; i++)
{
if ( strcasecmp(tmp,ETFileDescription[i].Extension)==0 )
{
*tmp = 0; //strrchr(source,'.') = 0;
break;
}
}
if (i==ET_FILE_DESCRIPTION_SIZE)
{
gchar *tmp1 = g_path_get_basename(filename_utf8);
Log_Print(LOG_ERROR,_("Tag scanner: strange… the extension '%s' was not found in filename '%s'"),tmp,tmp1);
g_free(tmp1);
}
// Replace characters into mask and filename before parsing
if (FTS_CONVERT_UNDERSCORE_AND_P20_INTO_SPACE)
{
Scan_Convert_Underscore_Into_Space(mask);
Scan_Convert_Underscore_Into_Space(filename_utf8);
Scan_Convert_P20_Into_Space(mask);
Scan_Convert_P20_Into_Space(filename_utf8);
}
if (FTS_CONVERT_SPACE_INTO_UNDERSCORE)
{
Scan_Convert_Space_Into_Undescore(mask);
Scan_Convert_Space_Into_Undescore(filename_utf8);
}
// Split the Scanner mask
mask_splitted = g_strsplit(mask,G_DIR_SEPARATOR_S,0);
// Get number of arguments into 'mask_splitted'
for (mask_splitted_number=0;mask_splitted[mask_splitted_number];mask_splitted_number++);
// Split the File Path
file_splitted = g_strsplit(filename_utf8,G_DIR_SEPARATOR_S,0);
// Get number of arguments into 'file_splitted'
for (file_splitted_number=0;file_splitted[file_splitted_number];file_splitted_number++);
// Set the starting position for each tab
if (mask_splitted_number <= file_splitted_number)
{
mask_splitted_index = 0;
file_splitted_index = file_splitted_number - mask_splitted_number;
}else
{
mask_splitted_index = mask_splitted_number - file_splitted_number;
file_splitted_index = 0;
}
loop = 0;
while ( mask_splitted[mask_splitted_index]!= NULL && file_splitted[file_splitted_index]!=NULL )
{
gchar *mask_seq = mask_splitted[mask_splitted_index];
gchar *file_seq = file_splitted[file_splitted_index];
gchar *file_seq_utf8 = filename_to_display(file_seq);
//g_print(">%d> seq '%s' '%s'\n",loop,mask_seq,file_seq);
while ( mask_seq && strlen(mask_seq)>0 )
{
/*
* Determine (first) code and destination
*/
if ( (tmp=strchr(mask_seq,'%')) == NULL || strlen(tmp) < 2 )
{
break;
}
/*
* Allocate a new iten for the fill_tag_list
*/
mask_item = g_malloc0(sizeof(Scan_Mask_Item));
// Get the code (used to determine the corresponding target entry)
mask_item->code = tmp[1];
/*
* Delete text before the code
*/
if ( (len = strlen(mask_seq) - strlen(tmp)) > 0 )
{
// Get this text in 'mask_seq'
buf = g_strndup(mask_seq,len);
// We remove it in 'mask_seq'
mask_seq = mask_seq + len;
// Find the same text at the begining of 'file_seq' ?
if ( (strstr(file_seq,buf)) == file_seq )
{
file_seq = file_seq + len; // We remove it
}else
{
Log_Print(LOG_ERROR,_("Scan Error: can't find separator '%s' within '%s'"),buf,file_seq_utf8);
}
g_free(buf);
}
// Remove the current code into 'mask_seq'
mask_seq = mask_seq + 2;
/*
* Determine separator between two code or trailing text (after code)
*/
if ( mask_seq && strlen(mask_seq)>0 )
{
if ( (tmp=strchr(mask_seq,'%')) == NULL || strlen(tmp) < 2 )
{
// No more code found
len = strlen(mask_seq);
}else
{
len = strlen(mask_seq) - strlen(tmp);
}
separator = g_strndup(mask_seq,len);
// Remove the current separator in 'mask_seq'
mask_seq = mask_seq + len;
// Try to find the separator in 'file_seq'
if ( (tmp=strstr(file_seq,separator)) == NULL )
{
Log_Print(LOG_ERROR,_("Scan Error: can't find separator '%s' within '%s'"),separator,file_seq_utf8);
separator[0] = 0; // Needed to avoid error when calculting 'len' below
}
// Get the string affected to the code (or the corresponding entry field)
len = strlen(file_seq) - (tmp!=NULL?strlen(tmp):0);
string = g_strndup(file_seq,len);
// Remove the current separator in 'file_seq'
file_seq = file_seq + strlen(string) + strlen(separator);
g_free(separator);
// We get the text affected to the code
mask_item->string = string;
}else
{
// We display the remaining text, affected to the code (no more data in 'mask_seq')
mask_item->string = g_strdup(file_seq);
}
// Add the filled mask_iten to the list
fill_tag_list = g_list_append(fill_tag_list,mask_item);
}
g_free(file_seq_utf8);
// Next sequences
mask_splitted_index++;
file_splitted_index++;
loop++;
}
g_free(filename_utf8);
g_strfreev(mask_splitted);
g_strfreev(file_splitted);
// The 'fill_tag_list' must be freed after use
return fill_tag_list;
}
void Scan_Fill_Tag_Generate_Preview (void)
{
gchar *mask = NULL;
gchar *preview_text = NULL;
GList *fill_tag_list = NULL;
if (!ETCore->ETFileDisplayedList
|| !ScannerWindow || !RenameFileMaskCombo || !FillTagPreviewLabel
|| gtk_combo_box_get_active(GTK_COMBO_BOX(ScannerOptionCombo)) != SCANNER_FILL_TAG)
return;
mask = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(ScanTagMaskCombo)))));
if (!mask)
return;
preview_text = g_strdup("");
fill_tag_list = Scan_Generate_New_Tag_From_Mask(ETCore->ETFileDisplayed,mask);
while (fill_tag_list)
{
Scan_Mask_Item *mask_item = fill_tag_list->data;
gchar *tmp_code = g_strdup_printf("%c",mask_item->code);
gchar *tmp_string = g_markup_printf_escaped("%s",mask_item->string); // To avoid problem with strings containing characters like '&'
gchar *tmp_preview_text = preview_text;
preview_text = g_strconcat(tmp_preview_text,"<b>","%",tmp_code," = ",
"</b>","<i>",tmp_string,"</i>",NULL);
g_free(tmp_code);
g_free(tmp_string);
g_free(tmp_preview_text);
if (!fill_tag_list->next) break;
fill_tag_list = fill_tag_list->next;
tmp_preview_text = preview_text;
preview_text = g_strconcat(tmp_preview_text," || ",NULL);
g_free(tmp_preview_text);
}
Scan_Free_File_Fill_Tag_List(fill_tag_list);
if (GTK_IS_LABEL(FillTagPreviewLabel))
{
if (preview_text)
{
//gtk_label_set_text(GTK_LABEL(FillTagPreviewLabel),preview_text);
gtk_label_set_markup(GTK_LABEL(FillTagPreviewLabel),preview_text);
} else
{
gtk_label_set_text(GTK_LABEL(FillTagPreviewLabel),"");
}
// Force the window to be redrawed
gtk_widget_queue_resize(ScannerWindow);
}
g_free(mask);
g_free(preview_text);
}
void Scan_Free_File_Fill_Tag_List (GList *list)
{
// Free the list
list = g_list_first(list);
while (list)
{
if (list->data)
{
g_free(((Scan_Mask_Item *)list->data)->string);
g_free( (Scan_Mask_Item *)list->data );
}
if (!list->next) break;
list = list->next;
}
g_list_free(list);
list = NULL;
}
/**************************
* Scanner To Rename File *
**************************/
/*
* Uses tag information (displayed into tag entries) to rename file
* Note: mask and source are read from the right to the left.
* Note1: a mask code may be used severals times...
*/
void Scan_Rename_File_With_Mask (ET_File *ETFile)
{
gchar *filename_generated_utf8 = NULL;
gchar *filename_generated = NULL;
gchar *filename_new_utf8 = NULL;
gchar *mask = NULL;
File_Name *FileName;
if (!ScannerWindow || !RenameFileMaskCombo || !ETFile) return;
mask = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(RenameFileMaskCombo)))));
if (!mask) return;
// Note : if the first character is '/', we have a path with the filename,
// else we have only the filename. The both are in UTF-8.
filename_generated_utf8 = Scan_Generate_New_Filename_From_Mask(ETFile,mask,FALSE);
g_free(mask);
if (!filename_generated_utf8)
return;
if (g_utf8_strlen(filename_generated_utf8,-1)<1)
{
g_free(filename_generated_utf8);
return;
}
// Convert filename to file-system encoding
filename_generated = filename_from_display(filename_generated_utf8);
if (!filename_generated)
{
GtkWidget *msgdialog;
msgdialog = gtk_message_dialog_new(GTK_WINDOW(ScannerWindow),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Could not convert filename '%s' into system filename encoding"),
filename_generated_utf8);
gtk_window_set_title(GTK_WINDOW(msgdialog),_("Filename translation"));
gtk_dialog_run(GTK_DIALOG(msgdialog));
gtk_widget_destroy(msgdialog);
g_free(filename_generated_utf8);
return;
}
/* Build the filename with the full path or relative to old path */
filename_new_utf8 = ET_File_Name_Generate(ETFile,filename_generated_utf8);
g_free(filename_generated);
g_free(filename_generated_utf8);
/* Set the new filename */
// Create a new 'File_Name' item
FileName = ET_File_Name_Item_New();
// Save changes of the 'File_Name' item
ET_Set_Filename_File_Name_Item(FileName,filename_new_utf8,NULL);
ET_Manage_Changes_Of_File_Data(ETFile,FileName,NULL);
g_free(filename_new_utf8);
Statusbar_Message(_("New file name successfully scanned…"),TRUE);
filename_new_utf8 = g_path_get_basename(((File_Name *)ETFile->FileNameNew->data)->value_utf8);
Log_Print(LOG_OK,_("New file name successfully scanned…(%s)"),filename_new_utf8);
g_free(filename_new_utf8);
return;
}
/*
* Build the new filename using tag + mask
* Used also to rename the directory (from the browser)
* @param ETFile : the etfile to process
* @param mask : the pattern to parse
* @param no_dir_check_or_conversion : if FALSE, disable checking of a directory
* in the mask, and don't convert "illegal" characters. This is used in the
* function "Write_Playlist" for the content of the playlist.
* Returns filename in UTF-8
*/
gchar *Scan_Generate_New_Filename_From_Mask (ET_File *ETFile, gchar *mask, gboolean no_dir_check_or_conversion)
{
gchar *tmp;
gchar **source = NULL;
gchar *path_utf8_cur = NULL;
gchar *filename_new_utf8 = NULL;
gchar *filename_tmp = NULL;
GList *rename_file_list = NULL;
File_Mask_Item *mask_item;
File_Mask_Item *mask_item_prev;
File_Mask_Item *mask_item_next;
gint counter = 0;
if (!ETFile || !mask) return NULL;
/*
* Check for a directory in the mask
*/
if (!no_dir_check_or_conversion)
{
if (g_path_is_absolute(mask))
{
// Absolute directory
}else if (strrchr(mask,G_DIR_SEPARATOR)!=NULL) // This is '/' on UNIX machines and '\' under Windows
{
// Relative path => set beginning of the path
path_utf8_cur = g_path_get_dirname( ((File_Name *)ETFile->FileNameCur->data)->value_utf8 );
}
}
/*
* Parse the codes to generate a list (1rst item = 1rst code)
*/
while ( mask!=NULL && (tmp=strrchr(mask,'%'))!=NULL && strlen(tmp)>1 )
{
// Mask contains some characters after the code ('%b__')
if (strlen(tmp)>2)
{
mask_item = g_malloc0(sizeof(File_Mask_Item));
if (counter)
{
if (strchr(tmp+2,G_DIR_SEPARATOR))
mask_item->type = DIRECTORY_SEPARATOR;
else
mask_item->type = SEPARATOR;
} else
{
mask_item->type = TRAILING_SEPARATOR;
}
mask_item->string = g_strdup(tmp+2);
rename_file_list = g_list_prepend(rename_file_list,mask_item);
}
// Now, parses the code to get the corresponding string (from tag)
source = Scan_Return_File_Tag_Field_From_Mask_Code((File_Tag *)ETFile->FileTag->data,tmp[1]);
mask_item = g_malloc0(sizeof(File_Mask_Item));
if (source && *source && strlen(*source)>0)
{
mask_item->type = FIELD;
mask_item->string = g_strdup(*source);
// Replace invalid characters for this field
// Note : shouldn't be done always as for the content of a playlist, we don't need to replace...
if (!no_dir_check_or_conversion)
{
ET_File_Name_Convert_Character(mask_item->string);
}
// Replace characters (rules) (!! don't convert in directory path_utf8_cur)
if (RFS_CONVERT_UNDERSCORE_AND_P20_INTO_SPACE)
{
Scan_Convert_Underscore_Into_Space(mask_item->string);
Scan_Convert_P20_Into_Space(mask_item->string);
}
if (RFS_CONVERT_SPACE_INTO_UNDERSCORE)
{
Scan_Convert_Space_Into_Undescore(mask_item->string);
}
if (RFS_REMOVE_SPACES)
{
Scan_Remove_Spaces(mask_item->string);
}
}else
{
mask_item->type = EMPTY_FIELD;
mask_item->string = NULL;
}
rename_file_list = g_list_prepend(rename_file_list,mask_item);
*tmp = '\0'; // Cut parsed data of mask
counter++; // To indicate that we made at least one loop to identifiate 'separator' or 'trailing_separator'
}
// It may have some characters before the last remaining code ('__%a')
if (mask!=NULL && strlen(mask)>0)
{
mask_item = g_malloc0(sizeof(File_Mask_Item));
mask_item->type = LEADING_SEPARATOR;
mask_item->string = g_strdup(mask);
rename_file_list = g_list_prepend(rename_file_list,mask_item);
}
if (!rename_file_list) return NULL;
/*
* For Debugging : display the "rename_file_list" list
*/
/***{
GList *list = g_list_first(rename_file_list);
gint i = 0;
g_print("## rename_file_list - start\n");
while (list)
{
File_Mask_Item *mask_item = (File_Mask_Item *)list->data;
Mask_Item_Type type = mask_item->type;
gchar *string = mask_item->string;
//g_print("item %d : \n",i++);
//g_print(" - type : '%s'\n",type==UNKNOWN?"UNKNOWN":type==LEADING_SEPARATOR?"LEADING_SEPARATOR":type==TRAILING_SEPARATOR?"TRAILING_SEPARATOR":type==SEPARATOR?"SEPARATOR":type==DIRECTORY_SEPARATOR?"DIRECTORY_SEPARATOR":type==FIELD?"FIELD":type==EMPTY_FIELD?"EMPTY_FIELD":"???");
//g_print(" - string : '%s'\n",string);
g_print("%d -> %s (%s) | ",i++,type==UNKNOWN?"UNKNOWN":type==LEADING_SEPARATOR?"LEADING_SEPARATOR":type==TRAILING_SEPARATOR?"TRAILING_SEPARATOR":type==SEPARATOR?"SEPARATOR":type==DIRECTORY_SEPARATOR?"DIRECTORY_SEPARATOR":type==FIELD?"FIELD":type==EMPTY_FIELD?"EMPTY_FIELD":"???",string);
list = list->next;
}
g_print("\n## rename_file_list - end\n\n");
}***/
/*
* Build the new filename with items placed into the list
* (read the list from the end to the beginning)
*/
rename_file_list = g_list_last(rename_file_list);
filename_new_utf8 = g_strdup("");
while (rename_file_list)
{
File_Mask_Item *mask_item = rename_file_list->data;
if ( mask_item->type==TRAILING_SEPARATOR ) // Trailing characters of mask
{
// Doesn't write it if previous field is empty
if (rename_file_list->prev && ((File_Mask_Item *)rename_file_list->prev->data)->type!=EMPTY_FIELD)
{
filename_tmp = filename_new_utf8;
filename_new_utf8 = g_strconcat(mask_item->string,filename_new_utf8,NULL);
g_free(filename_tmp);
}
}else
if ( mask_item->type==EMPTY_FIELD )
// We don't concatenate the field value (empty) and the previous
// separator (except leading separator) to the filename.
// If the empty field is the 'first', we don't concatenate it, and the
// next separator too.
{
if (rename_file_list->prev)
{
// The empty field isn't the first.
// If previous string is a separator, we don't use it, except if the next
// string is a FIELD (not empty)
mask_item_prev = rename_file_list->prev->data;
if ( mask_item_prev->type==SEPARATOR )
{
if ( !(rename_file_list->next && (mask_item_next=rename_file_list->next->data)
&& mask_item_next->type==FIELD) )
{
rename_file_list = rename_file_list->prev;
}
}
}else
if (rename_file_list->next && (mask_item_next=rename_file_list->next->data)
&& mask_item_next->type==SEPARATOR)
// We are at the 'beginning' of the mask (so empty field is the first)
// and next field is a separator. As the separator may have been already added, we remove it
{
if ( filename_new_utf8 && mask_item_next->string && (strncmp(filename_new_utf8,mask_item_next->string,strlen(mask_item_next->string))==0) ) // To avoid crash if filename_new_utf8 is 'empty'
{
filename_tmp = filename_new_utf8;
filename_new_utf8 = g_strdup(filename_new_utf8+strlen(mask_item_next->string));
g_free(filename_tmp);
}
}
}else // SEPARATOR, FIELD, LEADING_SEPARATOR, DIRECTORY_SEPARATOR
{
filename_tmp = filename_new_utf8;
filename_new_utf8 = g_strconcat(mask_item->string,filename_new_utf8,NULL);
g_free(filename_tmp);
}
if (!rename_file_list->prev) break;
rename_file_list = rename_file_list->prev;
}
// Free the list
Scan_Free_File_Rename_List(rename_file_list);
// Add current path if relative path entered
if (path_utf8_cur)
{
filename_tmp = filename_new_utf8; // in UTF-8!
filename_new_utf8 = g_strconcat(path_utf8_cur,G_DIR_SEPARATOR_S,filename_new_utf8,NULL);
g_free(filename_tmp);
g_free(path_utf8_cur);
}
return filename_new_utf8; // in UTF-8!
}
void Scan_Rename_File_Generate_Preview (void)
{
gchar *preview_text = NULL;
gchar *mask = NULL;
if (!ETCore->ETFileDisplayed
|| !ScannerWindow || !RenameFileMaskCombo || !RenameFilePreviewLabel)
return;
if (gtk_combo_box_get_active(GTK_COMBO_BOX(ScannerOptionCombo)) != SCANNER_RENAME_FILE)
return;
mask = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(RenameFileMaskCombo)))));
if (!mask)
return;
preview_text = Scan_Generate_New_Filename_From_Mask(ETCore->ETFileDisplayed,mask,FALSE);
if (GTK_IS_LABEL(RenameFilePreviewLabel))
{
if (preview_text)
{
//gtk_label_set_text(GTK_LABEL(RenameFilePreviewLabel),preview_text);
gchar *tmp_string = g_markup_printf_escaped("%s",preview_text); // To avoid problem with strings containing characters like '&'
gchar *str = g_strdup_printf("<i>%s</i>",tmp_string);
gtk_label_set_markup(GTK_LABEL(RenameFilePreviewLabel),str);
g_free(tmp_string);
g_free(str);
} else
{
gtk_label_set_text(GTK_LABEL(RenameFilePreviewLabel),"");
}
// Force the window to be redrawed
gtk_widget_queue_resize(ScannerWindow);
}
g_free(mask);
g_free(preview_text);
}
void Scan_Free_File_Rename_List (GList *list)
{
// Free the list
list = g_list_last(list);
while (list)
{
if (list->data)
{
g_free(((File_Mask_Item *)list->data)->string);
g_free( (File_Mask_Item *)list->data );
}
if (!list->prev) break;
list = list->prev;
}
g_list_free(list);
list = NULL;
}
/*
* Adds the current path of the file to the mask on the "Rename File Scanner" entry
*/
void Scan_Rename_File_Prefix_Path (void)
{
gint pos;
gchar *path_tmp;
const gchar *combo_text = NULL;
gchar *combo_tmp;
ET_File *ETFile = ETCore->ETFileDisplayed;
gchar *filename_utf8_cur = ((File_Name *)ETFile->FileNameCur->data)->value_utf8;
gchar *path_utf8_cur;