File: misc_playlist.c

package info (click to toggle)
gtkpod 0.99.14-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,716 kB
  • ctags: 3,266
  • sloc: ansic: 43,120; sh: 4,837; xml: 1,316; lex: 637; perl: 264; makefile: 236; awk: 73; python: 35
file content (1261 lines) | stat: -rw-r--r-- 36,829 bytes parent folder | download
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
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
/*
|  Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
|  Part of the gtkpod project.
| 
|  URL: http://www.gtkpod.org/
|  URL: http://gtkpod.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id: misc_playlist.c 1970 2008-05-11 06:07:12Z jcsjcs $
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <string.h>
#include "charset.h"
#include "itdb.h"
#include "info.h"
#include "sha1.h"
#include "misc.h"
#include "misc_track.h"
#include "prefs.h"


#define DEBUG_MISC 0


/*------------------------------------------------------------------*\
 *                                                                  *
 *             Add new playlist asking user for name                *
 *                                                                  *
\*------------------------------------------------------------------*/

/* Add a new playlist at position @position. The name for the new
 * playlist is queried from the user. A default (@dflt) name can be
 * provided.
 * Return value: the new playlist or NULL if the dialog was
 * cancelled. */
Playlist *add_new_pl_user_name (iTunesDB *itdb,
				gchar *dflt, gint32 position)
{
    ExtraiTunesDBData *eitdb;
    Playlist *result = NULL;
    gchar *name;

    g_return_val_if_fail (itdb, NULL);

    eitdb = itdb->userdata;
    g_return_val_if_fail (eitdb, NULL);

    if (!eitdb->itdb_imported)
    {
	gtkpod_warning_simple(_("Please load the iPod before adding playlists."));
	return NULL;
    }

    name = get_user_string (
	_("New Playlist"),
	_("Please enter a name for the new playlist"),
	dflt? dflt:_("New Playlist"),
	NULL, NULL, GTK_STOCK_ADD);
    if (name)
    {
	result = gp_playlist_add_new (itdb, name, FALSE, position);
	gtkpod_tracks_statusbar_update ();
    }
    return result;
}


/* Add a new playlist or smart playlist at position @position. The
 * name for the new playlist is queried from the user. A default
 * (@dflt) name can be provided.
 * Return value: none. In the case of smart playlists, the playlist
 * will not be created immediately. */
void add_new_pl_or_spl_user_name (iTunesDB *itdb,
				  gchar *dflt, gint32 position)
{
    ExtraiTunesDBData *eitdb;
    gboolean is_spl = FALSE;
    gchar *name;

    g_return_if_fail (itdb);

    eitdb = itdb->userdata;
    g_return_if_fail (eitdb);

    if (!eitdb->itdb_imported)
    {
	gtkpod_warning_simple(_("Please load the iPod before adding playlists."));
	return;
    }

    name = get_user_string (
	_("New Playlist"),
	_("Please enter a name for the new playlist"),
	dflt? dflt:_("New Playlist"),
	_("Smart Playlist"), &is_spl, GTK_STOCK_ADD);

    if (name)
    {
	if (!is_spl)
	{   /* add standard playlist */
	    gp_playlist_add_new (itdb, name, FALSE, position);
	    gtkpod_tracks_statusbar_update ();
	}
	else
	{   /* add smart playlist */
	    spl_edit_new (itdb, name, position);
	}
    }
}

/* callback */
G_MODULE_EXPORT void
on_smart_playlist_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
    iTunesDB *itdb = gp_get_selected_itdb();

    if (itdb)
    {
	spl_edit_new (itdb, NULL, -1);
    }
    else
    {
	message_sb_no_itdb_selected ();
    }
}


/*------------------------------------------------------------------*\
 *                                                                  *
 *                     Special Playlist Stuff                       *
 *                                                                  *
\*------------------------------------------------------------------*/

/* used for special playlist creation */
typedef gboolean (*PL_InsertFunc)(Track *track, gpointer userdata);

/* generate_category_playlists: Create a playlist for each category
   @cat (T_ARTIST, T_ALBUM, T_GENRE, T_COMPOSER) */
void generate_category_playlists (iTunesDB *itdb, T_item cat)
{
    Playlist *master_pl;
    gchar *qualifier;
    GList *gl;

    g_return_if_fail (itdb);

    /* Initialize the "qualifier". It is used to indicate the category of
       automatically generated playlists */
    switch (cat)
    {
    case T_ARTIST:
	qualifier = _("AR:");
	break;
    case T_ALBUM:
	qualifier = _("AL:");
	break;
    case T_GENRE:
	qualifier = _("GE:");
	break;
    case T_COMPOSER:
	qualifier = _("CO:");
	break;
    case T_YEAR:
	qualifier = _("YE:");
	break;
    default:
	g_return_if_reached ();
	break;
    }

    /* FIXME: delete all playlists named '[<qualifier> .*]' and
     * remember which playlist was selected if it gets deleted */

    master_pl = itdb_playlist_mpl (itdb);
    g_return_if_fail (master_pl);

    for (gl=master_pl->members; gl; gl=gl->next)
    {
	Track *track = gl->data;
	Playlist *cat_pl = NULL;
	gchar *category = NULL;
	const gchar *track_cat;

	track_cat = track_get_item (track, cat);

	if (track_cat)
	{
	    /* some tracks have empty strings in the genre field */
	    if(track_cat[0] == '\0')
	    {
		category = g_strdup_printf ("[%s %s]",
					    qualifier, _("Unknown"));
	    }
	    else
	    {
		category = g_strdup_printf ("[%s %s]",
					    qualifier, track_cat);
	    }

	    /* look for category playlist */
	    cat_pl = itdb_playlist_by_name (itdb, category);
	    /* or, create category playlist */
	    if(!cat_pl)
	    {
		cat_pl = gp_playlist_add_new (itdb, category,
					      FALSE, -1);
	    }
	    gp_playlist_add_track (cat_pl, track, TRUE);
	    g_free (category);
	}
    }
    gtkpod_tracks_statusbar_update();
}

/* Generate a new playlist containing all the tracks currently
   displayed */
Playlist *generate_displayed_playlist (void)
{
    GList *tracks = tm_get_all_tracks ();
    Playlist *result = NULL;

    if (tracks)
    {
	result= generate_new_playlist (gp_get_selected_itdb (), tracks);
	g_list_free (tracks);
    }
    return result;
}


/* Generate a new playlist containing all the tracks currently
   selected */
Playlist *generate_selected_playlist (void)
{
    GList *tracks = tm_get_selected_tracks ();
    Playlist *result=NULL;

    if (tracks)
    {
	result= generate_new_playlist (gp_get_selected_itdb (), tracks);
	g_list_free (tracks);
    }
    return result;
}


/* Generates a playlist containing a random selection of
   prefs_get_int("misc_track_nr") tracks in random order from the currently
   displayed tracks */
Playlist *generate_random_playlist (iTunesDB *itdb)
{
    GRand *grand = g_rand_new ();
    Playlist *new_pl = NULL;
    gchar *pl_name, *pl_name1;
    GList *rtracks = NULL;
    GList *tracks = tm_get_all_tracks ();
    gint tracks_max = prefs_get_int("misc_track_nr");
    gint tracks_nr = 0;


    while (tracks && (tracks_nr < tracks_max))
    {
	/* get random number between 0 and g_list_length()-1 */
	gint rn = g_rand_int_range (grand, 0, g_list_length (tracks));
	GList *random = g_list_nth (tracks, rn);
	rtracks = g_list_append (rtracks, random->data);
	tracks = g_list_delete_link (tracks, random);
	++tracks_nr;
    }
    pl_name1 = g_strdup_printf (_("Random (%d)"), tracks_max);
    pl_name = g_strdup_printf ("[%s]", pl_name1);
    new_pl = generate_playlist_with_name (itdb, rtracks, pl_name, TRUE);
    g_free (pl_name1);
    g_free (pl_name);
    g_list_free (tracks);
    g_list_free (rtracks);
    g_rand_free (grand);
    return new_pl;
}


void randomize_current_playlist (void)
{
    Playlist *pl= pm_get_selected_playlist ();

    if (!pl)
    {
	message_sb_no_playlist_selected ();
	return;
    }

    if (prefs_get_int("tm_autostore"))
    {
	prefs_set_int("tm_autostore", FALSE);
	gtkpod_warning (_("Auto Store of track view disabled.\n\n"));
/* 	sort_window_update (); */
    }

    itdb_playlist_randomize (pl);

    st_adopt_order_in_playlist ();
    tm_adopt_order_in_sorttab ();
}


static void not_listed_make_track_list (gpointer key, gpointer track,
					gpointer tracks)
{
    *(GList **)tracks = g_list_append (*(GList **)tracks, (Track *)track);
}

/* Generate a playlist containing all tracks that are not part of any
   playlist.
   For this, playlists starting with a "[" (generated playlists) are
   being ignored. */
Playlist *generate_not_listed_playlist (iTunesDB *itdb)
{
    GHashTable *hash;
    GList *gl, *tracks=NULL;
    guint32 i;
    gchar *pl_name;
    Playlist *new_pl, *pl;

    g_return_val_if_fail (itdb, NULL);

    /* Create hash with all track/track pairs */
    pl = itdb_playlist_mpl (itdb);
    g_return_val_if_fail (pl, NULL);
    hash = g_hash_table_new (NULL, NULL);
    for (gl=pl->members; gl != NULL; gl=gl->next)
    {
	g_hash_table_insert (hash, gl->data, gl->data);
    }
    /* remove all tracks that are members of other playlists */
    i=1;
    do
    {
	pl = itdb_playlist_by_nr (itdb, i);
	++i;
	/* skip playlists starting with a '[' */
	if (pl && pl->name && (pl->name[0] != '['))
	{
	    for (gl=pl->members; gl != NULL; gl=gl->next)
	    {
		g_hash_table_remove (hash, gl->data);
	    }
	}
    } while (pl);

    g_hash_table_foreach (hash, not_listed_make_track_list, &tracks);
    g_hash_table_destroy (hash);
    hash = NULL;

    pl_name = g_strdup_printf ("[%s]", _("Not Listed"));

    new_pl = generate_playlist_with_name (itdb, tracks, pl_name, TRUE);
    g_free (pl_name);
    g_list_free (tracks);
    return new_pl;
}


/* Generate a playlist consisting of the tracks in @tracks
 * with @name name. If @del_old ist TRUE, delete any old playlist with
 * the same name. */
Playlist *generate_playlist_with_name (iTunesDB *itdb,GList *tracks,
				       gchar *pl_name, gboolean del_old)
{
    Playlist *new_pl=NULL;
    gint n = g_list_length (tracks);

    g_return_val_if_fail (itdb, new_pl);

    if(n>0)
    {
	gboolean select = FALSE;
	GList *l;
	if (del_old)
	{
	    /* currently selected playlist */
	    Playlist *sel_pl= pm_get_selected_playlist ();
	    if (sel_pl->itdb != itdb)
	    {   /* different itdb */
		sel_pl = NULL;
	    }
	    /* remove all playlists with named @plname */
	    gp_playlist_remove_by_name (itdb, pl_name);
	    /* check if we deleted the selected playlist */
	    if (sel_pl)
	    {
		if (g_list_find (itdb->playlists, sel_pl) == NULL)
		    select = TRUE;
	    }
	}
	new_pl = gp_playlist_add_new (itdb, pl_name, FALSE, -1);
	g_return_val_if_fail (new_pl, new_pl);
	for (l=tracks; l; l=l->next)
	{
	    Track *track = l->data;
	    g_return_val_if_fail (track, new_pl);
	    gp_playlist_add_track (new_pl, track, TRUE);
	}
	gtkpod_statusbar_message (
	    ngettext ("Created playlist '%s' with %d track.",
		      "Created playlist '%s' with %d tracks.",
		      n), pl_name, n);
	if (new_pl && select)
	{   /* need to select newly created playlist because the old
	     * selection was deleted */
	    pm_select_playlist (new_pl);
	}
    }
    else
    {   /* n==0 */
	gtkpod_statusbar_message (_("No tracks available, playlist not created"));
    }
    gtkpod_tracks_statusbar_update();
    return new_pl;
}

/* Generate a playlist named "New Playlist" consisting of the tracks
 * in @tracks. */
Playlist *generate_new_playlist (iTunesDB *itdb, GList *tracks)
{
    gchar *name = get_user_string (
	_("New Playlist"),
	_("Please enter a name for the new playlist"),
	_("New Playlist"),
	NULL, NULL, GTK_STOCK_ADD);
    if (name)
	return generate_playlist_with_name (itdb, tracks, name, FALSE);
    return NULL;
}

/* look at the add_ranked_playlist help:
 * BEWARE this function shouldn't be used by other functions */
static GList *create_ranked_glist(iTunesDB *itdb, gint tracks_nr,
				  PL_InsertFunc insertfunc,
				  GCompareFunc comparefunc,
				  gpointer userdata)
{
   GList *tracks=NULL;
   gint f=0;
   GList *gl;

   g_return_val_if_fail (itdb, tracks);

   for (gl=itdb->tracks; gl; gl=gl->next)
   {
       Track *track = gl->data;
       g_return_val_if_fail (track, tracks);
       if (track && (!insertfunc || insertfunc (track, userdata)))
       {
	   tracks = g_list_insert_sorted (tracks, track, comparefunc);
	   ++f;
	   if (tracks_nr && (f>tracks_nr))
	   {   /*cut the tail*/
	       tracks = g_list_remove(tracks,
				      g_list_nth_data(tracks, tracks_nr));
	       --f;
	   }
       }
   }
   return tracks;
}
/* Generate or update a playlist named @pl_name, containing
 * @tracks_nr tracks.
 *
 * @str is the playlist's name (no [ or ])
 * @insertfunc: determines which tracks to enter into the new playlist.
 *              If @insertfunc is NULL, all tracks are added.
 * @comparefunc: determines order of tracks
 * @tracks_nr: max. number of tracks in playlist or 0 for no limit.
 *
 * Return value: the newly created playlist
 */
static Playlist *update_ranked_playlist(iTunesDB *itdb,
					gchar *str, gint tracks_nr,
					PL_InsertFunc insertfunc,
					GCompareFunc comparefunc,
					gpointer userdata)
{
    Playlist *result = NULL;
    gchar *pl_name = g_strdup_printf ("[%s]", str);
    GList *tracks;

    g_return_val_if_fail (itdb, result);

    tracks = create_ranked_glist(itdb, tracks_nr,
				 insertfunc, comparefunc, userdata);

    if (tracks)
    /* else generate_playlist_with_name prints something*/
    {
	result = generate_playlist_with_name (itdb, tracks, pl_name, TRUE);
    }
    g_list_free (tracks);
    g_free (pl_name);
    return result;
}


/* ------------------------------------------------------------ */
/* Generate a new playlist containing the most listened (playcount
 * reverse order) tracks. to enter this playlist a track must have been
 * played */

/* Sort Function: determines the order of the generated playlist */

/* NOTE: THE USE OF 'COMP' ARE NECESSARY FOR THE TIME_PLAYED COMPARES
   WHERE A SIGN OVERFLOW MAY OCCUR BECAUSE OF THE 32 BIT UNSIGNED MAC
   TIMESTAMPS. */
static gint Most_Listened_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->playcount, a->playcount);
	if (result == 0) result = COMP (b->rating, a->rating);
	if (result == 0) result = COMP (b->time_played, a->time_played);
    }
    return result;
}

/* Insert function: determines whether a track is entered into the playlist */
static gboolean Most_Listened_IF (Track *track, gpointer userdata)
{
    if (track)   return (track->playcount != 0);
    return      FALSE;
}

void most_listened_pl (iTunesDB *itdb)
{
    gint tracks_nr = prefs_get_int("misc_track_nr");
    gchar *str;

    g_return_if_fail (itdb);
    str = g_strdup_printf (_("Most Listened (%d)"), tracks_nr);
    update_ranked_playlist (itdb, str, tracks_nr,
			    Most_Listened_IF, Most_Listened_CF, (gpointer)0 );
    g_free (str);
}


/* ------------------------------------------------------------ */
/* Generate a new playlist containing all songs never listened to. */

/* Sort Function: determines the order of the generated playlist */

/* NOTE: THE USE OF 'COMP' ARE NECESSARY FOR THE TIME_PLAYED COMPARES
   WHERE A SIGN OVERFLOW MAY OCCUR BECAUSE OF THE 32 BIT UNSIGNED MAC
   TIMESTAMPS. */
static gint Never_Listened_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->rating, a->rating);
    }
    return result;
}

/* Insert function: determines whether a track is entered into the playlist */
static gboolean Never_Listened_IF (Track *track, gpointer userdata)
{
    if (track)   return (track->playcount == 0);
    return      FALSE;
}

void never_listened_pl (iTunesDB *itdb)
{
    gint tracks_nr = 0;  /* no limit */
    gchar *str;

    g_return_if_fail (itdb);
    str = g_strdup_printf (_("Never Listened"));
    update_ranked_playlist (itdb, str, tracks_nr,
			    Never_Listened_IF, Never_Listened_CF, (gpointer)0 );
    g_free (str);
}


/* ------------------------------------------------------------ */
/* Generate a new playlist containing the most rated (rate
 * reverse order) tracks. */

/* Sort Function: determines the order of the generated playlist */
static gint Most_Rated_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->rating, a->rating);
	if (result == 0) result = COMP (b->playcount, a->playcount);
	if (result == 0) result = COMP (b->time_played, a->time_played);
    }
    return result;
}

/* Insert function: determines whether a track is entered into the playlist */
static gboolean Most_Rated_IF (Track *track, gpointer userdata)
{
    if (track) return ((track->playcount != 0) || prefs_get_int("not_played_track"));
    return FALSE;
}

void most_rated_pl (iTunesDB *itdb)
{
    gint tracks_nr = prefs_get_int("misc_track_nr");
    gchar *str;

    g_return_if_fail (itdb);
    str =  g_strdup_printf (_("Best Rated (%d)"), tracks_nr);
    update_ranked_playlist (itdb, str, tracks_nr,
			    Most_Rated_IF, Most_Rated_CF, (gpointer)0 );
    g_free (str);
}


/* ------------------------------------------------------------ */
/* Generate 6 playlists,one for each rating 1..5 and unrated    */


/* Sort Function: determines the order of the generated playlist */
static gint All_Ratings_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->playcount, a->playcount);
	if (result == 0) result = COMP (b->time_played, a->time_played);
    }
    return result;
}


/* Insert function: determines whether a track is entered into the playlist */
static gboolean All_Ratings_IF (Track *track, gpointer user_data)
{
    guint playlist_nr = GPOINTER_TO_UINT(user_data);
    if (track) return (track->rating == playlist_nr*20);
    return FALSE;
}


void each_rating_pl(iTunesDB *itdb)
{
    gchar *str;
    guint playlist_nr;

    g_return_if_fail (itdb);
    str = _("Unrated tracks");
    for (playlist_nr = 0; playlist_nr < 6; playlist_nr ++ )
    {
	if (playlist_nr > 0) 
	{
	    str = g_strdup_printf (_("Rated %d"), playlist_nr);
	} 
    	update_ranked_playlist (itdb, str, 0,
				All_Ratings_IF, All_Ratings_CF,
				GUINT_TO_POINTER(playlist_nr));
    }
    g_free (str);
}


/* ------------------------------------------------------------ */
/* Generate a new playlist containing the last listened (last time play
 * reverse order) tracks. */

/* Sort Function: determines the order of the generated playlist */
static gint Last_Listened_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->time_played, a->time_played);
	if (result == 0) result = COMP (b->rating, a->rating);
	if (result == 0) result = COMP (b->playcount, a->playcount);
    }
    return result;
}

/* Insert function: determines whether a track is entered into the playlist */
static gboolean Last_Listened_IF (Track *track, gpointer userdata)
{
    if (track)   return (track->playcount != 0);
    return      FALSE;
}

void last_listened_pl (iTunesDB *itdb)
{
    gint tracks_nr = prefs_get_int("misc_track_nr");
    gchar *str;

    g_return_if_fail (itdb);
    str = g_strdup_printf (_("Recent (%d)"), tracks_nr);
    update_ranked_playlist (itdb, str, tracks_nr,
			    Last_Listened_IF, Last_Listened_CF, (gpointer)0);
    g_free (str);
}


/* ------------------------------------------------------------ */
/* Generate a new playlist containing the tracks listened to since the
 * last time the iPod was connected to a computer (and the playcount
 * file got wiped) */

/* Sort Function: determines the order of the generated playlist */
static gint since_last_CF (gconstpointer aa, gconstpointer bb)
{
    gint result = 0;
    const Track *a = aa;
    const Track *b = bb;

    if (a && b)
    {
	result = COMP (b->recent_playcount, a->recent_playcount);
	if (result == 0) result = COMP (b->time_played, a->time_played);
	if (result == 0) result = COMP (b->playcount, a->playcount);
	if (result == 0) result = COMP (b->rating, a->rating);
    }
    return result;
}

/* Insert function: determines whether a track is entered into the playlist */
static gboolean since_last_IF (Track *track, gpointer userdata)
{
    if (track && (track->recent_playcount != 0))  return TRUE;
    else                                        return FALSE;
}

void since_last_pl (iTunesDB *itdb)
{
    g_return_if_fail (itdb);
    update_ranked_playlist (itdb, _("Last Time"), 0,
			    since_last_IF, since_last_CF, (gpointer)0);
}


/*------------------------------------------------------------------*\
 *                                                                  *
 *                Find Orphans                                      *
 *                                                                  *
\*------------------------------------------------------------------*/

/******************************************************************************
 * Attempt to do everything at once:
 *  - find dangling links in iTunesDB
 *  - find orphaned files in mounted directory
 * Will be done by creating first a hash of all known in iTunesDB filenames,
 * and then checking every file on HDD in the hash table. If it is present -
 * remove from hashtable, if not present - it is orphaned. If at the end
 * hashtable still has some elements - they're dangling...
 *
 * TODO: instead of using case-sensitive comparison function it might be better
 *  just to convert all filenames to lowercase before doing any comparisons
 * FIX:
 *  offline... when you import db offline and then switch to online mode you still
 *  have offline db loaded and if it is different from IPOD's - then a lot of crap
 *  can happen... didn't check yet
 ******************************************************************************/


/* compare @str1 and @str2 case-sensitively only */
gint str_cmp (gconstpointer str1, gconstpointer str2, gpointer data)
{
	return compare_string_case_insensitive((gchar *)str1, (gchar *)str2);
	/*return strcmp((gchar *)str1, (gchar *)str2);*/
}

static void treeKeyDestroy(gpointer key) { g_free(key); }
static void treeValueDestroy(gpointer value) { }


/* call back function for traversing what is left from the tree -
 * dangling files - files present in DB but not present physically on iPOD.
 * It adds found tracks to the dandling list so user can see what is missing
 * and then decide on what to do with them */
gboolean remove_dangling (gpointer key, gpointer value, gpointer pl_dangling)
{
/*     printf("Found dangling item pointing file %s\n", ((Track*)value)->ipod_path); */
    Track *track = (Track*)value;
    GList **l_dangling = ((GList **)pl_dangling);
    gchar *filehash = NULL;
    gint lind;
    ExtraTrackData *etr;

    g_return_val_if_fail (l_dangling, FALSE);
    g_return_val_if_fail (track, FALSE);
    etr = track->userdata;
    g_return_val_if_fail (etr, FALSE);

    /* 1 - Original file is present on PC */
    /* 0 - Doesn't exist */
    lind = 0;
    if (etr->pc_path_locale && *etr->pc_path_locale &&
	g_file_test (etr->pc_path_locale, G_FILE_TEST_EXISTS))
    {
	lind = 1;
    }
    l_dangling[lind]=g_list_append(l_dangling[lind], track);

    g_free(filehash);
    return FALSE;               /* do not stop traversal */
}

guint ntokens(gchar** tokens)
{
    guint n=0;
    while (tokens[n]) n++;
    return n;
}


void process_gtk_events_blocked()
{    while (widgets_blocked && gtk_events_pending ()) gtk_main_iteration ();  }



/* Frees memory busy by the lists containing tracks stored in
   @user_data1 */
static void
check_db_danglingcancel0  (gpointer user_data1, gpointer user_data2)
{
    g_list_free((GList *)user_data1);
    gtkpod_statusbar_message (_("Removal of dangling tracks with no files on PC was canceled."));
}


/* Frees memory busy by the lists containing tracks stored in
   @user_data1 */
static void
check_db_danglingcancel1  (gpointer user_data1, gpointer user_data2)
{
    g_list_free((GList *)user_data1);
    gtkpod_statusbar_message (_("Handling of dangling tracks with files on PC was canceled."));
}

/* "dangling": tracks that are in database but not on disk */
/* To be called for ok to remove dangling Tracks with with no files
 * linked.  Frees @user_data1 and @user_data2*/
static void
check_db_danglingok0 (gpointer user_data1, gpointer user_data2)
{
    GList *tlist = ((GList *)user_data1);
    GList *l_dangling = tlist;
    iTunesDB *itdb = user_data2;

    g_return_if_fail (itdb);
    /* traverse the list and append to the str */
    for (tlist = g_list_first(tlist);
	 tlist != NULL;
	 tlist = g_list_next(tlist))
    {
	Track *track = tlist->data;
	g_return_if_fail (track);
	
        /* printf("Removing track %d\n", track->ipod_id); */
	/* remove track from database */
	gp_playlist_remove_track (NULL, track, DELETE_ACTION_DATABASE);
    }
    g_list_free(l_dangling);
    data_changed (itdb);
    gtkpod_statusbar_message (_("Dangling tracks with no files on PC were removed."));
}



/* To be called for ok to remove dangling Tracks with with no files linked.
 * Frees @user_data1 and @user_data2*/
static void
check_db_danglingok1 (gpointer user_data1, gpointer user_data2)
{
    GList *tlist = ((GList *)user_data1);
    GList *l_dangling = tlist;
    iTunesDB *itdb = user_data2;

    g_return_if_fail (itdb);

    block_widgets ();

    /* traverse the list and append to the str */
    for (tlist = g_list_first(tlist);
	 tlist != NULL;
	 tlist = g_list_next(tlist))
    {
	Track *oldtrack;
	Track *track = tlist->data;
	ExtraTrackData *etr;
	gchar *buf;

	g_return_if_fail (track);
	etr = track->userdata;
	g_return_if_fail (etr);
        /* printf("Handling track %d\n", track->ipod_id); */

	buf = get_track_info (track, TRUE);
	gtkpod_statusbar_message (_("Processing '%s'..."), buf);
	g_free (buf);
				  
	while (widgets_blocked && gtk_events_pending ())
	    gtk_main_iteration ();

	/* Indicate that file needs to be transfered */
	track->transferred=FALSE;
	/* Update SHA1 information */
	/* remove track from sha1 hash and reinsert it
	   (hash value may have changed!) */
	sha1_track_remove (track);
	/* need to remove the old value manually! */
	g_free (etr->sha1_hash);
	etr->sha1_hash = NULL;
	oldtrack = sha1_track_exists_insert (itdb, track);
	if (oldtrack) { /* track exists, remove old track
			  and register the new version */
	    sha1_track_remove (oldtrack);
	    gp_duplicate_remove (track, oldtrack);
	    sha1_track_exists_insert (itdb, track);
	}
	/* mark for conversion / transfer */
	file_convert_add_track (track);
    }
    g_list_free(l_dangling);
    data_changed (itdb);
    gtkpod_statusbar_message (_("Dangling tracks with files on PC were handled."));
    /* I don't think it's too interesting to pop up the list of
       duplicates -- but we should reset the list. */
    gp_duplicate_remove (NULL, (void *)-1);

    release_widgets ();
}



/* checks iTunesDB for presence of dangling links and checks IPODs
 * Music directory on subject of orphaned files */
void check_db (iTunesDB *itdb)
{

    void glist_list_tracks (GList * tlist, GString * str)
	{
	    if (str==NULL)
	    {
		fprintf(stderr, "Report the bug please: shouldn't be NULL at %s:%d\n",__FILE__,__LINE__);
		return;
	    }
	    /* traverse the list and append to the str */
	    for (tlist = g_list_first(tlist);
		 tlist != NULL;
		 tlist = g_list_next(tlist))
	    {
		ExtraTrackData *etr;
		Track *track = tlist->data;
		g_return_if_fail (track);
		etr = track->userdata;
		g_return_if_fail (etr);
		g_string_append_printf
		    (str,"%s(%d) %s-%s -> %s\n",_("Track"),
		     track->id, track->artist,  track->title,  etr->pc_path_utf8);
	    }
	} /* end of glist_list_tracks */

    GTree *files_known = NULL;
    GDir  *dir_des = NULL;

    gchar *pathtrack=NULL;
    gchar *ipod_filename = NULL;
#   define localdebug  0      /* may be later becomes more general verbose param */
    Playlist* pl_orphaned = NULL;
    GList * l_dangling[2] = {NULL, NULL}; /* 2 kinds of dangling tracks: with approp
					   * files and without */
    /* 1 - Original file is present on PC and has the same sha1*/
    /* 0 - Doesn't exist */

    gpointer foundtrack ;
    gint h,i;
    gint norphaned = 0;
    gint ndangling = 0;
    gchar ** tokens;
    const gchar *mountpoint = itdb_get_mountpoint (itdb);
    ExtraiTunesDBData *eitdb;
    GList *gl;
    gchar *music_dir = NULL;

    g_return_if_fail (itdb);
    eitdb = itdb->userdata;
    g_return_if_fail (eitdb);

    /* If an iTunesDB exists on the iPod, the user probably is making
       a mistake and we should tell him about it */
    if (!eitdb->itdb_imported)
    {
		gchar *itunesdb_filename = itdb_get_itunesdb_path (mountpoint);

		if (itunesdb_filename)
		{
			const gchar *str = _("You did not import the existing iTunesDB. This is most likely incorrect and will result in the loss of the existing database.\n\nIf you abort the operation, you can import the existing database before calling this function again.\n");

			gint result = gtkpod_confirmation_hig (GTK_WINDOW (gtkpod_window),
												   GTK_MESSAGE_WARNING,
												   _("Existing iTunes database not imported"),
												   str,
												   _("Proceed anyway"),
												   _("Abort operation"),
												   NULL,
												   NULL);

			if (result == GTK_RESPONSE_CANCEL)
			{
				return;
			}
		}
    }

    gtkpod_statusbar_timeout (30*STATUSBAR_TIMEOUT);
    block_widgets();

    gtkpod_statusbar_message(_("Creating a tree of known files"));
    gtkpod_tracks_statusbar_update();

    /* put all files in the hash table */
    files_known = g_tree_new_full (str_cmp, NULL,
				   treeKeyDestroy, treeValueDestroy);
    for (gl=itdb->tracks; gl; gl=gl->next)
    {
	Track *track = gl->data;
        gint ntok=0;
	g_return_if_fail (track);
        /* we don't want to report non-transferred files as dangling */
	if (!track->transferred) continue; 
	tokens = g_strsplit(track->ipod_path,":",(track->ipod_path[0]==':'?4:3));
        ntok=ntokens(tokens);
	if (ntok>=3)
	{
	    pathtrack=g_strdup (tokens[ntok-1]);
	}
	else
	{
	    /* illegal ipod_path */
	    /* the track has NO ipod_path, so we want the item to
	       ultimately be deleted from DB, * however, we need to
	       add it to tree it such a way that:
               a) it will be unique
               b) it won't match to any existing file on the ipod

               so use something invented using the pointer to the
               track structure as a way to generate uniqueness
            */
           pathtrack=g_strdup_printf ("NOFILE-%p", track);
	}

        if (localdebug)
	{
            fprintf(stdout,"File %s\n", pathtrack);
	    fflush(stdout);
	}

	g_tree_insert (files_known, pathtrack, track);
	g_strfreev(tokens);
    }

    gtkpod_statusbar_message(_("Checking iPOD files against known files in DB"));
    gtkpod_tracks_statusbar_update();
    process_gtk_events_blocked();

    music_dir = itdb_get_music_dir (mountpoint);

    for(h=0; h<itdb_musicdirs_number (itdb); h++)
    {
	/* directory name */
	gchar *ipod_dir=g_strdup_printf("F%02d",h); /* just directory name */
	gchar *ipod_fulldir;
	/* full path */
	ipod_fulldir = itdb_get_path (music_dir, ipod_dir);
	if(ipod_fulldir && (dir_des=g_dir_open(ipod_fulldir,0,NULL))) {
	    while ((ipod_filename=g_strdup(g_dir_read_name(dir_des))))
		/* we have a file in the directory*/
	    {
		pathtrack=g_strdup_printf("%s%c%s", ipod_dir, ':', ipod_filename);

                if (localdebug) {
                    fprintf(stdout,"Considering %s ", pathtrack);
                    fflush(stdout);
                }

		if ( g_tree_lookup_extended (files_known, pathtrack,
					     &foundtrack, &foundtrack) )
		{ /* file is not orphaned */
		    g_tree_remove(files_known, pathtrack); /* we don't need this any more */
                    if (localdebug) fprintf(stdout," good ");
		}
		else
		{  /* Now deal with orphaned... */
		    gchar *fn_orphaned;
		    gchar *num_str = g_strdup_printf ("F%02d", h);
		    Track *dupl_track;

		    const gchar *p_dcomps[] =
			{ num_str, ipod_filename, NULL };

		    fn_orphaned = itdb_resolve_path (music_dir, p_dcomps);

		    if (!pl_orphaned)
		    {
			gchar *str = g_strdup_printf ("[%s]", _("Orphaned"));
			pl_orphaned = gp_playlist_by_name_or_add (
			    itdb, str, FALSE);
			g_free (str);
		    }

		    norphaned++;

                    if (localdebug) fprintf(stdout,"to orphaned ");
		    if ((dupl_track = sha1_file_exists (itdb, fn_orphaned,
						       TRUE)))
		    {  /* This orphan has already been added again.
			  It will be removed with the next sync */
			Track *track = gp_track_new ();
			gchar *fn_utf8 = charset_to_utf8 (fn_orphaned);
			const gchar *dir_rel = music_dir + strlen (mountpoint);
			if (*dir_rel == G_DIR_SEPARATOR) ++dir_rel;
			track->ipod_path = g_strdup_printf (
			    "%c%s%c%s%c%s",
			    G_DIR_SEPARATOR, dir_rel,
			    G_DIR_SEPARATOR, num_str,
			    G_DIR_SEPARATOR, ipod_filename);
			itdb_filename_fs2ipod (track->ipod_path);

			gp_track_validate_entries (track);
			mark_track_for_deletion (itdb, track);
			gtkpod_warning (_(
			 "The following orphaned file had already "
			 "been added to the iPod again. It will be "
			 "removed with the next sync:\n%s\n\n"),
			 fn_utf8);
			g_free (fn_utf8);
		    }
		    else
		    {
			add_track_by_filename(itdb,
					      fn_orphaned, pl_orphaned,
					      FALSE, NULL, NULL);
		    }
		    g_free (fn_orphaned);
		    g_free (num_str);
		}
                if (localdebug) fprintf(stdout," done\n");

		g_free(ipod_filename);
		g_free(pathtrack);
	    }
            g_dir_close(dir_des);
	}
        g_free(ipod_dir);
 	g_free(ipod_fulldir);
	process_gtk_events_blocked();
    }

    ndangling=g_tree_nnodes(files_known);
    gtkpod_statusbar_message (_("Found %d orphaned and %d dangling files. Processing..."),
			      norphaned, ndangling);
    
    gtkpod_tracks_statusbar_update();

    g_free(music_dir);
    music_dir = NULL;

    /* Now lets deal with dangling tracks */
    /* Traverse the tree - leftovers are dangling - put them in two lists */
    g_tree_foreach(files_known, remove_dangling, l_dangling);

    for (i=0;i<2;i++)
    {
	GString *str_dangs = g_string_sized_new(2000);
	gint ndang=0;
	gchar *buf;

	glist_list_tracks(l_dangling[i], str_dangs); /* compose String list of the tracks */
	ndang = g_list_length(l_dangling[i]);
	if (ndang)
	{
	    if (i==1)
		buf = g_strdup_printf (
		    ngettext ("The following dangling track has a file on PC.\nPress OK to have them transfered from the file on next Sync, CANCEL to leave it as is.",
			      "The following %d dangling tracks have files on PC.\nPress OK to have them transfered from the files on next Sync, CANCEL to leave them as is.",
			      ndang), ndang);
	    else
		buf = g_strdup_printf (
		    ngettext ("The following dangling track doesn't have file on PC. \nPress OK to remove it, CANCEL to leave it as is.",
			      "The following %d dangling tracks do not have files on PC. \nPress OK to remove them, CANCEL to leave them. as is",
			      ndang), ndang);

	    if (gtkpod_confirmation
		((i==1?CONF_ID_DANGLING1:CONF_ID_DANGLING0), /* we want unique window for each */
		 FALSE,         /* gboolean modal, */
		 _("Dangling Tracks"), /* title */
		 buf,           /* label */
		 str_dangs->str, /* scrolled text */
		 NULL, 0, NULL, /* option 1 */
		 NULL, 0, NULL, /* option 2 */
		 TRUE,          /* gboolean confirm_again, */
		 NULL,          /* ConfHandlerOpt confirm_again_handler,*/
		 i==1?check_db_danglingok1:check_db_danglingok0, /* ConfHandler ok_handler,*/
		 NULL,          /* don't show "Apply" button */
		 i==1?check_db_danglingcancel1:check_db_danglingcancel0, /* cancel_handler,*/
		 l_dangling[i], /* gpointer user_data1,*/
		 itdb)             /* gpointer user_data2,*/
		== GTK_RESPONSE_REJECT)
	    {   /* free memory */
		g_list_free(l_dangling[i]);
	    }
	    g_free (buf);
	    g_string_free (str_dangs, TRUE);
	}
    }

    if (pl_orphaned) data_changed (itdb);
    g_tree_destroy (files_known);
    gtkpod_statusbar_message (_("Found %d orphaned and %d dangling files. Done."),
			      norphaned, ndangling);
    gtkpod_statusbar_timeout (0);
    release_widgets ();
}