File: tree.c

package info (click to toggle)
gmerlin 0.4.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,268 kB
  • ctags: 10,598
  • sloc: ansic: 112,902; sh: 11,111; makefile: 1,425; xml: 72; sed: 16
file content (1605 lines) | stat: -rw-r--r-- 39,546 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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2010 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.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, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* For stat/opendir */

#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

/* Gmerlin includes */
#include <config.h>
#include <gmerlin/translation.h>

#include <gmerlin/tree.h>
#include <treeprivate.h>
#include <gmerlin/utils.h>
#include <gmerlin/log.h>

#define LOG_DOMAIN "mediatree"

#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#endif

static int nth_in_list(bg_album_t * children, bg_album_t * child)
  {
  int ret;
  bg_album_t * tmp_album;

  ret = 0;
  tmp_album = children;

  while(tmp_album)
    {
    if(tmp_album == child)
      return ret;
    ret++;
    tmp_album = tmp_album->next;
    } 
  return -1;
  }

int * bg_media_tree_get_path(bg_media_tree_t * tree, bg_album_t * album)
  {
  bg_album_t * parent;
  bg_album_t * child;
  int depth;
  int * ret;
  int i;
  
  /* Get the depth of the album */

  depth = 1;
  parent = album->parent;

  while(parent)
    {
    depth++;
    parent = parent->parent;
    }

  /* Fill indices */
  
  ret = malloc((depth+1)* sizeof(int));
  
  ret[depth] = -1;

  child = album;
  parent = child->parent;
  
  for(i = depth-1; i >= 1; i--)
    {
    ret[i] = nth_in_list(parent->children, child);
    parent = parent->parent;
    child = child->parent;
    }
  ret[0] = nth_in_list(tree->children, child);
  return ret;
  }

static bg_album_t * append_album(bg_album_t * list, bg_album_t * new_album)
  {
  bg_album_t * a;
  if(!list)
    return new_album;
  a = list;
  
  while(a->next)
    a = a->next;
  a->next = new_album;
  return list;
  }

static bg_album_t *
remove_from_list(bg_album_t * list, bg_album_t * album)
  {
  bg_album_t * sibling_before;

  if(album == list)
    return album->next;
  else
    {
    sibling_before = list;
    while(sibling_before->next != album)
      sibling_before = sibling_before->next;
    sibling_before->next = album->next;
    return list;
    }
  }

/* Insert new album into list */

static bg_album_t * insert_album_after(bg_album_t * list,
                                       bg_album_t * new_album,
                                       bg_album_t * sibling)
  {
  if(!list)
    {
    new_album->next = (bg_album_t*)0;
    return new_album;
    }
  if(!sibling)
    {
    new_album->next = list;
    return new_album;
    }
  
  new_album->next = sibling->next;
  sibling->next = new_album;
  return list;
  }

static bg_album_t * insert_album_before(bg_album_t * list,
                                        bg_album_t * new_album,
                                        bg_album_t * sibling)
  {
  bg_album_t * before;
  
  if(!list)
    {
    new_album->next = (bg_album_t*)0;
    return new_album;
    }
  
  before = list;

  if(list == sibling)
    {
    new_album->next = list;
    return new_album;
    }
  
  while(before->next != sibling)
    before = before->next;

  return insert_album_after(list, new_album, before);
  
  }

/* Functions used both by tree and album */

static int get_num_albums(bg_album_t * list)
  {
  int ret = 0;

  if(list)
    {
    ret++;

    while(list->next)
      {
      list = list->next;
      ret++;
      }
    }
  return ret;
  }

static bg_album_t * get_album(bg_album_t * list, int index)
  {
  int i;
  for(i = 0; i < index; i++)
    list = list->next;
  return list;
  }

/* For album */

int bg_album_get_num_children(bg_album_t * a)
  {
  return get_num_albums(a->children);
  }

bg_album_t * bg_album_get_child(bg_album_t * a, int i)
  {
  return get_album(a->children, i);
  }

/* For tree */

int bg_media_tree_get_num_albums(bg_media_tree_t * t)
  {
  return get_num_albums(t->children);
  }

bg_album_t * bg_media_tree_get_album(bg_media_tree_t * t, int i)
  {
  return get_album(t->children, i);
  }

bg_plugin_registry_t *
bg_media_tree_get_plugin_registry(bg_media_tree_t * t)
  {
  return t->com.plugin_reg;
  }

static void check_special(bg_media_tree_t * tree, bg_album_t * children)
  {
  bg_album_t * a;

  a = children;
  
  while(a)
    {
    if(a->type == BG_ALBUM_TYPE_INCOMING)
      {
      tree->incoming = a;
      bg_album_rename(a, TR("Incoming"));
      }
    else if(a->type == BG_ALBUM_TYPE_FAVOURITES)
      {
      tree->com.favourites = a;
      bg_album_rename(a, TR("Favourites"));
      }
    a = a->next;

    /* Check children */
    if(a && a->children)
      check_special(tree, a->children);
    }
  }

static int get_user_pass(void * data, const char * resource,
                         char ** user, char ** pass)
  {
  bg_album_common_t * com = (bg_album_common_t *)data;

  if(!com->username && !com->password)
    {
    if(!com->userpass_callback)
      return 0;
    if(!com->userpass_callback(resource, user, pass, &com->save_auth,
                               com->userpass_callback_data))
      return 0;
    /* Store user and password in the common area so we can use it later on */
    com->username = bg_strdup(com->username, *user);
    com->password = bg_strdup(com->password, *pass);
    }
  else
    {
    *user = bg_strdup(*user, com->username);
    *pass = bg_strdup(*pass, com->password);
    }
  return 1;
  }


static bg_album_t * find_by_plugin(bg_album_t * albums,
                                   const char * plugin)
  {
  while(albums)
    {
    if(albums->plugin_info && !strcmp(albums->plugin_info->name, plugin))
      return albums;
    albums = albums->next;
    }
  return (bg_album_t *)0;
  }

static bg_album_t * find_by_device(bg_album_t * albums,
                                   const char * device)
  {
  while(albums)
    {
    if(albums->device && !strcmp(albums->device, device))
      return albums;
    albums = albums->next;
    }
  return (bg_album_t *)0;
  
  }

static void add_device_plugins(bg_media_tree_t * ret,
                               bg_plugin_registry_t * plugin_reg,
                               int flag, bg_album_type_t type)
  {
  bg_album_t * plugin_album;
  bg_album_t * device_album;
  int num_plugins, i, j;
  const bg_plugin_info_t * info;
  num_plugins =
    bg_plugin_registry_get_num_plugins(ret->com.plugin_reg,
                                       BG_PLUGIN_INPUT, flag);
  
  for(i = 0; i < num_plugins; i++)
    {
    info = bg_plugin_find_by_index(ret->com.plugin_reg, i,
                                   BG_PLUGIN_INPUT, flag);
    
    /* Add album for the plugin */

    if(!(plugin_album = find_by_plugin(ret->children, info->name)))
      {
      plugin_album = bg_album_create(&(ret->com), BG_ALBUM_TYPE_PLUGIN, NULL);

      bg_bindtextdomain(info->gettext_domain,
                     info->gettext_directory);
      
      plugin_album->plugin_info = info;
      
      ret->children = append_album(ret->children, plugin_album);
      }
    
    plugin_album->name  = bg_strdup(plugin_album->name,
                                    TRD(info->long_name, info->gettext_domain));

    
    /* Now, get the number of devices */
    
    if(info->devices && info->devices->device)
      {
      j = 0;
      
      while(info->devices[j].device)
        {
        if(!(device_album =
             find_by_device(plugin_album->children,
                            info->devices[j].device)))
          {
          device_album = bg_album_create(&(ret->com), type, plugin_album);
          device_album->device = bg_strdup(device_album->device,
                                           info->devices[j].device);
          if(info->devices[j].name)
            {
            device_album->name = bg_strdup(device_album->name,
                                           info->devices[j].name);
            }
          else
            {
            device_album->name = bg_strdup(device_album->name,
                                           info->devices[j].device);
            }
          device_album->plugin_info = info;
          plugin_album->children = append_album(plugin_album->children,
                                              device_album);
          }
        j++;
        } /* Device loop */

      /* TODO: Remove albums, whose devices vanished */
      
      }
    } /* Plugin loop */
  
  }

bg_media_tree_t *
bg_media_tree_create(const char * filename,
                     bg_plugin_registry_t * plugin_reg)
  {
  bg_media_tree_t * ret;
  const char * pos1;
  
  
  ret = calloc(1, sizeof(*ret));

  ret->cfg_section = bg_cfg_section_create((char*)0);
  
  ret->com.plugin_reg = plugin_reg;
  ret->com.set_current_callback = bg_media_tree_set_current;
  ret->com.set_current_callback_data = ret;

  ret->com.input_callbacks.user_pass = get_user_pass;
  ret->com.input_callbacks.data      = &(ret->com);

#ifdef HAVE_INOTIFY
  ret->com.inotify_fd = inotify_init();
#endif
  
  ret->filename = bg_strdup(ret->filename, filename);
  pos1 = strrchr(ret->filename, '/');
  
  ret->com.directory = bg_strndup(ret->com.directory, ret->filename, pos1);

  return ret;
  }

void bg_media_tree_init(bg_media_tree_t * ret)
  {
  /* Load the entire tree */
  
  bg_media_tree_load(ret);

  /* Create special albums if necessary */

  check_special(ret, ret->children);

  if(!ret->incoming)
    {
    ret->incoming = bg_album_create(&(ret->com), BG_ALBUM_TYPE_INCOMING, NULL);
    ret->incoming->name  = bg_strdup(ret->incoming->name, TR("Incoming"));
    ret->incoming->xml_file  = bg_strdup(ret->incoming->xml_file,
                                         "incoming.xml");
    ret->children = append_album(ret->children, ret->incoming);
    }
  if(!ret->com.favourites)
    {
    ret->com.favourites = bg_album_create(&(ret->com), BG_ALBUM_TYPE_FAVOURITES, NULL);
    ret->com.favourites->name  = bg_strdup(ret->com.favourites->name, TR("Favourites"));
    ret->com.favourites->xml_file  = bg_strdup(ret->com.favourites->xml_file, "favourites.xml");
    ret->children = append_album(ret->children, ret->com.favourites);
    }
  
  /* Check for removable devices */
  add_device_plugins(ret, ret->com.plugin_reg, BG_PLUGIN_TUNER,     BG_ALBUM_TYPE_TUNER);
  add_device_plugins(ret, ret->com.plugin_reg, BG_PLUGIN_REMOVABLE, BG_ALBUM_TYPE_REMOVABLE);
  }

void bg_media_tree_destroy(bg_media_tree_t * t)
  {
  bg_album_t * next_album;
  bg_media_tree_save(t);
  
  if(t->purge_directory)
    bg_media_tree_purge_directory(t);
  
  bg_cfg_section_destroy(t->cfg_section);
  
  while(t->children)
    {
    next_album = t->children->next;
        
    bg_album_destroy(t->children);
    t->children = next_album;
    }
  if(t->com.directory)
    free(t->com.directory);
  if(t->com.metadata_format)
    free(t->com.metadata_format);
  if(t->com.blacklist)
    free(t->com.blacklist);
  if(t->com.load_handle)
    bg_plugin_unref(t->com.load_handle);
  
  if(t->filename)
    free(t->filename);

#ifdef HAVE_INOTIFY
  close(t->com.inotify_fd);
#endif
  
  free(t);
  }

bg_album_t * bg_media_tree_append_album(bg_media_tree_t * tree,
                                        bg_album_t * parent)
  {
  bg_album_t * album_before;
  bg_album_t * album_after;
  
  bg_album_t * new_album =
    bg_album_create(&(tree->com), BG_ALBUM_TYPE_REGULAR, parent);
    
  if(parent)
    {
    bg_album_append_child(parent, new_album);
    }

  /* Top level album, take care of the system albums */

  else
    {
    if(tree->children)
      {
      album_before = tree->children;

      /* Tree has only device albums */
            
      if(album_before->type == BG_ALBUM_TYPE_PLUGIN)
        {
        new_album->next = tree->children; 
        tree->children = new_album;
        }
      else
        {
        while(album_before->next && (album_before->next->type != BG_ALBUM_TYPE_PLUGIN))
          album_before = album_before->next;
        album_after = album_before->next;
        album_before->next = new_album;
        new_album->next = album_after;
        }
      }
    else
      tree->children = new_album;
    }
  return new_album;
  }

void bg_media_tree_remove_album(bg_media_tree_t * tree,
                                bg_album_t * album)
  {
  char * tmp_path = (char *)0;

  if(album->parent)
    bg_album_remove_from_parent(album);
  else
    tree->children = remove_from_list(tree->children, album);
  
  /* Check if album file if present */
  
  if(album->xml_file)
    tmp_path = bg_sprintf("%s/%s", tree->com.directory, album->xml_file);
  
  /* Schredder everything */

  bg_album_destroy(album);

  if(tmp_path)
    {
    remove(tmp_path);
    free(tmp_path);     
    }
  }

/* Check if we can move an album */

static int check_move_common(bg_media_tree_t * t,
                             bg_album_t * album,
                      bg_album_t * sibling)
  {
  bg_album_t * parent;

  if(album == sibling)
    return 0;

  parent = sibling->parent;
  while(parent)
    {
    if(album == parent)
      return 0;
    parent = parent->parent;
    }
  return 1;
  }

int bg_media_tree_check_move_album_before(bg_media_tree_t * t,
                                          bg_album_t * album,
                                          bg_album_t * after)
  {
  if(!check_move_common(t, album, after))
    {
    return 0;
    }

  /* Never drop after removable albums */

  switch(after->type)
    {
    case BG_ALBUM_TYPE_REMOVABLE:
    case BG_ALBUM_TYPE_PLUGIN:
    case BG_ALBUM_TYPE_TUNER:
      return 0;
    case BG_ALBUM_TYPE_INCOMING:
    case BG_ALBUM_TYPE_FAVOURITES:
    case BG_ALBUM_TYPE_REGULAR:
      return 1;
    }
  return 1;
  }

int bg_media_tree_check_move_album_after(bg_media_tree_t * t,
                                         bg_album_t * album,
                                         bg_album_t * before)
  {
  if(!check_move_common(t, album, before))
    {
    return 0;
    }

  /* Never drop after removable albums */

  switch(before->type)
    {
    case BG_ALBUM_TYPE_REMOVABLE:
    case BG_ALBUM_TYPE_PLUGIN:
    case BG_ALBUM_TYPE_TUNER:
      return 0;
    case BG_ALBUM_TYPE_INCOMING:
    case BG_ALBUM_TYPE_FAVOURITES:
    case BG_ALBUM_TYPE_REGULAR:
      return 1;
    }
  return 1;
  }

/* Move an album inside the tree */

void bg_media_tree_move_album_before(bg_media_tree_t * t,
                                     bg_album_t * album,
                                     bg_album_t * after)
  {
  if(!bg_media_tree_check_move_album_before(t, album, after))
    return;

  /* Remove the album from the parent's children list */

  if(album->parent)
    {
    album->parent->children =
      remove_from_list(album->parent->children, album);
    }
  else
    {
    t->children = remove_from_list(t->children, album);
    }

  /* Insert at new location */

  if(after->parent)
    {
    after->parent->children =
      insert_album_before(after->parent->children, album, after);
    album->parent = after->parent;
    }
  else
    {
    t->children = insert_album_before(t->children, album, after);
    album->parent = (bg_album_t*)0;
    }
  }

void bg_media_tree_move_album_after(bg_media_tree_t * t,
                                    bg_album_t * album,
                                    bg_album_t * before)
  {
  if(!bg_media_tree_check_move_album_after(t, album, before))
    return;

  /* Remove the album from the parent's children list */

  if(album->parent)
    {
    album->parent->children =
      remove_from_list(album->parent->children, album);
    }
  else
    {
    t->children = remove_from_list(t->children, album);
    }

  /* Insert at new location */

  if(before->parent)
    {
    before->parent->children =
      insert_album_after(before->parent->children, album, before);
    album->parent = before->parent;
    }
  else
    {
    t->children = insert_album_after(t->children, album, before);
    album->parent = (bg_album_t*)0;
    }

  
  }

int bg_media_tree_check_move_album(bg_media_tree_t * t,
                                   bg_album_t * album,
                                   bg_album_t * parent)
  {
  bg_album_t * test_album;
  
  /* Never move album into it's subalbum */

  test_album = parent;

  while(test_album)
    {
    if(album == test_album)
      return 0;
    test_album = test_album->parent;
    }

  /* Never drop into removable albums */

  switch(parent->type)
    {
    case BG_ALBUM_TYPE_REMOVABLE:
    case BG_ALBUM_TYPE_PLUGIN:
    case BG_ALBUM_TYPE_TUNER:
      return 0;
    case BG_ALBUM_TYPE_REGULAR:
    case BG_ALBUM_TYPE_INCOMING:
    case BG_ALBUM_TYPE_FAVOURITES:
      return 1;
    }
  
  return 1;
  }

void bg_media_tree_move_album(bg_media_tree_t * t,
                              bg_album_t * album,
                              bg_album_t * parent)
  {
  if(!bg_media_tree_check_move_album(t, album, parent))
    return;

  /* Remove the album from the parent's children list */

  if(album->parent)
    {
    album->parent->children =
      remove_from_list(album->parent->children, album);
    }
  else
    {
    t->children = remove_from_list(t->children, album);
    }

  if(!parent)
    {
    t->children = insert_album_before(t->children, album, (bg_album_t*)0);
    album->parent = (bg_album_t*)0;
    }
  else
    {
    parent->children = insert_album_before(parent->children,
                                           album, (bg_album_t*)0);
    album->parent = parent;
    }
  }


/* Set and get entries for playback */

void bg_media_tree_set_current(void * data,
                               bg_album_t * album,
                               const bg_album_entry_t * entry)
  {
  bg_album_t * last_current_album;
  
  bg_media_tree_t * t = (bg_media_tree_t*)data;

  last_current_album = t->com.current_album;

  if((last_current_album != album) &&
     t->com.shuffle_list &&
     (t->last_shuffle_mode == BG_SHUFFLE_MODE_CURRENT))
    {
    bg_shuffle_list_destroy(t->com.shuffle_list);
    t->com.shuffle_list = NULL;
    }
  
  t->com.current_album = album;

  if(t->com.current_album)
    {
    t->com.current_entry = t->com.current_album->entries;
    
    while(t->com.current_entry != entry)
      t->com.current_entry = t->com.current_entry->next;
    }
  else
    {
    t->com.current_entry = (bg_album_entry_t*)0;
    }

  if(last_current_album &&
     (last_current_album != album))
    bg_album_current_changed(last_current_album);
  
  if(album)
    bg_album_current_changed(album);
  
  if(t->change_callback)
    t->change_callback(t, t->change_callback_data);
  }

/* Shuffle list stuff */

static bg_shuffle_list_t * get_shuffle_tracks(bg_album_t * album,
                                              bg_shuffle_list_t * list,
                                              bg_shuffle_list_t ** last_entry,
                                              int all_open)
  {
  bg_album_t       * tmp_album;
  bg_album_entry_t * tmp_entry;
  
  if(bg_album_is_open(album))
    {
    tmp_entry = album->entries;
    while(tmp_entry)
      {
      if(list)
        {
        (*last_entry)->next = calloc(1, sizeof(*((*last_entry)->next)));
        *last_entry = (*last_entry)->next;
        }
      else
        {
        list = calloc(1, sizeof(*list));
        *last_entry = list;
        }
      (*last_entry)->album = album;
      (*last_entry)->entry = tmp_entry;

      tmp_entry = tmp_entry->next;
      }
    }

  /* Do the same for the children */

  if(all_open)
    {
    tmp_album = album->children;
    while(tmp_album)
      {
      list = get_shuffle_tracks(tmp_album, list, last_entry, 1);
      tmp_album = tmp_album->next;
      }
    }

  return list;
  }

static void create_shuffle_list(bg_media_tree_t * tree, bg_shuffle_mode_t shuffle_mode)
  {
  int rand_max;
  int64_t index;
  int i;
  bg_album_t * tmp;
  bg_shuffle_list_t * list, *tmp_entry;
  bg_shuffle_list_t ** array_1;
  bg_shuffle_list_t ** array_2;
  int num;
    
  /* 1. Create the list */
  
  list       = (bg_shuffle_list_t*)0;
  tmp_entry  = (bg_shuffle_list_t*)0;

  if(shuffle_mode == BG_SHUFFLE_MODE_ALL)
    {
    tmp = tree->children;
    
    while(tmp)
      {
      list = get_shuffle_tracks(tmp, list, &tmp_entry, 1);
      tmp = tmp->next;
      }
    }
  else if(tree->com.current_album)
    list = get_shuffle_tracks(tree->com.current_album, list, &tmp_entry, 0);
  else
    return;
    
  /* 2. Count the entries */

  num = 0;
  tmp_entry = list;

  while(tmp_entry)
    {
    num++;
    tmp_entry = tmp_entry->next;
    }

  /* 3. Create first array */

  array_1 = malloc(num * sizeof(*array_1));
  array_2 = malloc(num * sizeof(*array_2));

  tmp_entry = list;
  
  for(i = 0; i < num; i++)
    {
    array_1[i] = tmp_entry;
    tmp_entry = tmp_entry->next;
    }

  /* 4. Shuffle */

  for(i = 0; i < num - 1; i++)
    {
    rand_max = num - i - 1;
    index = ((int64_t)rand() * (int64_t)(rand_max-1)) / RAND_MAX;
    array_2[i] = array_1[index];
    if(index < rand_max)
      array_1[index] = array_1[num - i - 1];
    }
  array_2[num - 1] = array_1[0];
  
  /* 5. Set the next pointers for the list */

  for(i = 0; i < num - 1; i++)
    {
    array_2[i]->next   = array_2[i+1];
    array_2[i+1]->prev = array_2[i];
    }
  array_2[0]->prev = NULL;
  array_2[num-1]->next = NULL;
    
  /* 6. Cleanup */
  
  tree->com.shuffle_list = array_2[0];
  tree->last_shuffle_mode = shuffle_mode;
  tree->shuffle_current = tree->com.shuffle_list;
  
  free(array_1);
  free(array_2);
  
  }

static void check_shuffle_list(bg_media_tree_t * tree, bg_shuffle_mode_t shuffle_mode)
  {
  if(tree->com.shuffle_list && (tree->last_shuffle_mode != shuffle_mode))
    {
    bg_shuffle_list_destroy(tree->com.shuffle_list);
    tree->com.shuffle_list = NULL;
    }
  if(!tree->com.shuffle_list)
    {
    create_shuffle_list(tree, shuffle_mode);
    }
  }

/* Set the next and previous track */

int bg_media_tree_next(bg_media_tree_t * tree, int wrap,
                       bg_shuffle_mode_t shuffle_mode)
  {
  if(shuffle_mode == BG_SHUFFLE_MODE_OFF)
    {
    if(tree->com.current_album)
      return bg_album_next(tree->com.current_album, wrap);
    else
      return 0;
    }
  else
    {
    check_shuffle_list(tree, shuffle_mode);

    if(!tree->com.shuffle_list)
      return 0;
    
    if(!tree->shuffle_current->next)
      {
      if(wrap)
        tree->shuffle_current = tree->com.shuffle_list;
      else
        return 0;
      }
    else
      tree->shuffle_current = tree->shuffle_current->next;
    bg_media_tree_set_current(tree, tree->shuffle_current->album,
                              tree->shuffle_current->entry);
    return 1;
    }
  return 0;
  }

int bg_media_tree_previous(bg_media_tree_t * tree, int wrap, bg_shuffle_mode_t shuffle_mode)
  {
  if(shuffle_mode == BG_SHUFFLE_MODE_OFF)
    {
    if(tree->com.current_album)
      return bg_album_previous(tree->com.current_album, wrap);
    else
      return 0;
    }
  else
    {
    check_shuffle_list(tree, shuffle_mode);

    if(!tree->shuffle_current->prev)
      {
      if(wrap)
        {
        while(tree->shuffle_current->next)
          tree->shuffle_current = tree->shuffle_current->next;
        }
      else
        return 0;
      }
    else
      tree->shuffle_current = tree->shuffle_current->prev;
    bg_media_tree_set_current(tree, tree->shuffle_current->album,
                              tree->shuffle_current->entry);
    return 1;
    }
  return 0;
  }

void bg_shuffle_list_destroy(bg_shuffle_list_t * l)
  {
  bg_shuffle_list_t * tmp;
  tmp = l;

  while(tmp)
    {
    tmp = l->next;
    free(l);
    l = tmp;
    }
  }

void bg_media_tree_set_change_callback(bg_media_tree_t * tree,
                                       void (*change_callback)(bg_media_tree_t*, void*),
                                       void* change_callback_data)
  {
  tree->change_callback = change_callback;
  tree->change_callback_data = change_callback_data;
  }

void bg_media_tree_set_play_callback(bg_media_tree_t * tree,
                                     void (*play_callback)(void*),
                                     void* play_callback_data)
  {
  tree->com.play_callback      = play_callback;
  tree->com.play_callback_data = play_callback_data;
  }

void bg_media_tree_set_userpass_callback(bg_media_tree_t * tree,
                                         int (*userpass_callback)(const char * resource,
                                                                  char ** user, char ** pass,
                                                                  int * save_password,
                                                                  void * data),
                                         void * userpass_callback_data)
  {
  tree->com.userpass_callback      = userpass_callback;
  tree->com.userpass_callback_data = userpass_callback_data;
  }


bg_plugin_handle_t *
bg_media_tree_get_current_track(bg_media_tree_t * t, int * index)
  {
  bg_track_info_t * track_info;
  const bg_plugin_info_t * info;
  bg_input_plugin_t * input_plugin;
  bg_plugin_handle_t * ret = (bg_plugin_handle_t *)0;
  //  char * system_location = (char*)0;
  if(!t->com.current_entry || !t->com.current_album)
    {
    bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Doubleclick on a track first");
    goto fail;
    }
  if((t->com.current_album->type == BG_ALBUM_TYPE_REMOVABLE) ||
     (t->com.current_album->type == BG_ALBUM_TYPE_TUNER))
    {
    ret = t->com.current_album->handle;
    bg_plugin_ref(ret);
    input_plugin = (bg_input_plugin_t*)(ret->plugin);
    }
  else
    {
    if(t->com.current_entry->plugin)
      info = bg_plugin_find_by_name(t->com.plugin_reg, t->com.current_entry->plugin);
    else
      info = bg_plugin_find_by_filename(t->com.plugin_reg,
                                        t->com.current_entry->location,
                                        (BG_PLUGIN_INPUT));
#if 0    
    if(!info)
      {
      bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot open %s: no plugin found");
      goto fail;
      }
#endif
    bg_log(BG_LOG_INFO, LOG_DOMAIN, "Loading %s (plugin: %s)",
           (char*)t->com.current_entry->location,
           (info ? info->name : "auto"));
    bg_album_common_prepare_callbacks(&t->com, t->com.current_entry);
    if(!bg_input_plugin_load(t->com.plugin_reg,
                             t->com.current_entry->location, info,
                             &(ret), &t->com.input_callbacks,
                             !!(t->com.current_entry->flags & BG_ALBUM_ENTRY_EDL)))
      {
      bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Loading %s failed",
             (char*)t->com.current_entry->location);
      goto fail;
      }
    input_plugin = (bg_input_plugin_t*)(ret->plugin);
    }
  track_info = input_plugin->get_track_info(ret->priv,
                                            t->com.current_entry->index);
  if(!track_info)
    {
    bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Selecting track %d for %s failed",
           t->com.current_entry->index+1,
           (char*)t->com.current_entry->location);
    goto fail;
    }
  bg_album_update_entry(t->com.current_album, t->com.current_entry, track_info, 1);

  bg_album_common_set_auth_info(&(t->com), t->com.current_entry);
  
  //  bg_album_changed(t->com.current_album);
    
  if(index)
    *index = t->com.current_entry->index;
  return ret;
  
  fail:
  bg_media_tree_mark_error(t, 1);
  return (bg_plugin_handle_t *)0;
  }

bg_album_t * bg_media_tree_get_current_album(bg_media_tree_t * t)
  {
  return t->com.current_album;
  }

const char * bg_media_tree_get_current_track_name(bg_media_tree_t * t)
  {
  if(!t->com.current_album || !t->com.current_entry)
    return (const char *)0;
  return t->com.current_entry->name;
  }

/* Parameter stuff */

static const bg_parameter_info_t parameters[] =
  {
    {
      .name =        "use_metadata",
      .long_name =   TRS("Use metadata for track names"),
      .type =        BG_PARAMETER_CHECKBUTTON,
      .val_default = { .val_i = 1 },
      .help_string = TRS("If disabled, track name will be taken from filename")
    },
    {
      .name =        "metadata_format",
      .long_name =   TRS("Format for track names"),
      .type =        BG_PARAMETER_STRING,
      .val_default = { .val_str = "%p - %t" },
      .help_string = TRS("Format specifier for tracknames from\n\
metadata\n\
%p:    Artist\n\
%a:    Album\n\
%g:    Genre\n\
%t:    Track name\n\
%<d>n: Track number with <d> digits\n\
%y:    Year\n\
%c:    Comment"),

    },
    {
      .name =        "purge_directory",
      .long_name =   TRS("Purge directory on exit"),
      .type =        BG_PARAMETER_CHECKBUTTON,
      .val_default = { .val_i = 1 },
      .help_string = TRS("Purge directory (i.e. delete\n\
unused album files) at program exit")
    },
    {
      .name =        "prefer_edl",
      .long_name =   TRS("Prefer EDL"),
      .type =        BG_PARAMETER_CHECKBUTTON,
      .val_default = { .val_i = 0 },
      .help_string = TRS("For files, which contain edit decision lists and raw streams, this option selects which one to decode. This setting is saved in the album once the file is loaded.")
    },
    {
      .name =        "blacklist",
      .long_name =   TRS("Blacklist"),
      .type =        BG_PARAMETER_STRING,
      .val_default = { .val_str = "srt txt pdf" },
      .help_string = TRS("File extensions, which are never loaded automatically"),
    },
    { /* End of parameters */ }
  };

const bg_parameter_info_t * bg_media_tree_get_parameters(bg_media_tree_t * tree)
  {
  return parameters;
  }

void bg_media_tree_set_parameter(void * priv, const char * name,
                                 const bg_parameter_value_t * val)
  {
  bg_media_tree_t * tree;
  tree = (bg_media_tree_t*)priv;
  if(!name)
    return;

  if(!strcmp(name, "use_metadata"))
    {
    tree->com.use_metadata = val->val_i;
    }
  else if(!strcmp(name, "metadata_format"))
    {
    tree->com.metadata_format = bg_strdup(tree->com.metadata_format, val->val_str);
    }
  else if(!strcmp(name, "blacklist"))
    {
    tree->com.blacklist = bg_strdup(tree->com.blacklist, val->val_str);
    }
  else if(!strcmp(name, "purge_directory"))
    {
    tree->purge_directory = val->val_i;
    }
  else if(!strcmp(name, "prefer_edl"))
    {
    tree->com.prefer_edl = val->val_i;
    }
  }

void bg_media_tree_mark_error(bg_media_tree_t * t, int err)
  {
  int err1;
  if(t->com.current_entry)
    {
    err1 = !!(t->com.current_entry->flags & BG_ALBUM_ENTRY_ERROR);
    err = !!err;
    
    if(err == err1)
      return;
    
    if(err)
      t->com.current_entry->flags |= BG_ALBUM_ENTRY_ERROR;
    else
      t->com.current_entry->flags &= ~BG_ALBUM_ENTRY_ERROR;
    }
  if(t->com.current_album && t->com.current_entry)
    bg_album_entry_changed(t->com.current_album, t->com.current_entry);
  }



static void add_directory(bg_media_tree_t * t, bg_album_t * parent,
                          const char * directory,
                          int recursive,
                          int subdirs_to_subalbums,
                          int watch,
                          const char * plugin,
                          int depth)
  {
  char * tmp_string;
  DIR * dir;
  char filename[FILENAME_MAX];

  struct
    {
    struct dirent d;
    char b[NAME_MAX]; /* Make sure there is enough memory */
    } dent;

  struct dirent * dent_ptr;
  const char * pos1;
  struct stat stat_buf;
  char * urls[2];
  bg_album_t * a;

  if(subdirs_to_subalbums || !depth)
    a = bg_media_tree_append_album(t, parent);
  else
    a = parent;
    
  if(parent)
    bg_album_set_expanded(parent, 1);
  
  bg_album_open(a);
  bg_album_set_expanded(a, 1);


  if(subdirs_to_subalbums || !depth)
    {
    pos1 = strrchr(directory, '/');
    pos1++;
    
    tmp_string = bg_system_to_utf8(pos1, -1);
    bg_album_rename(a, tmp_string);
    free(tmp_string);
    }
  
  if(watch)
    bg_album_set_watch_dir(a, directory);
  
  /* Scan for regular files and directories */
  
  dir = opendir(directory);
  if(!dir)
    return;

  urls[0] = filename;
  urls[1] = (char*)0;
  
  while(!readdir_r(dir, &(dent.d), &dent_ptr))
    {
    if(!dent_ptr)
      break;
    
    if(dent.d.d_name[0] == '.') /* Don't import hidden files */
      continue;
    
    sprintf(filename, "%s/%s", directory, dent.d.d_name);
    
    if(stat(filename, &stat_buf))
      {
      continue;
      }
    /* Add directory as subalbum */
    if(recursive && S_ISDIR(stat_buf.st_mode))
      {
      add_directory(t, a, filename, recursive, subdirs_to_subalbums,
                    watch,
                    plugin, depth + 1);
      }
    else if(S_ISREG(stat_buf.st_mode))
      {
      if(watch)
        bg_album_insert_file_before(a, filename,
                                    plugin,
                                    (bg_album_entry_t *)0,
                                    stat_buf.st_mtime);
      else
        bg_album_insert_file_before(a, filename,
                                    plugin,
                                    (bg_album_entry_t *)0,
                                    0);
      }
    if(t->change_callback)
      t->change_callback(t, t->change_callback_data);
    }
  closedir(dir);
  bg_album_sort_entries(a);
  bg_album_sort_children(a);
  bg_album_close(a);

  if(t->change_callback)
    t->change_callback(t, t->change_callback_data);
  
  }


/* Add an entire directory */

void bg_media_tree_add_directory(bg_media_tree_t * t, bg_album_t * parent,
                                 const char * directory,
                                 int recursive,
                                 int subdirs_to_subalbums,
                                 int watch,
                                 const char * plugin)
  {
  add_directory(t, parent, directory, recursive, subdirs_to_subalbums,
                watch,
                plugin, 0);
  
  }

static int albums_have_file(bg_album_t * album, const char * filename)
  {
  bg_album_t * a;
  a = album;

  while(a)
    {
    if(a->xml_file && !strcmp(a->xml_file, filename))
      return 1;

    else if(albums_have_file(a->children, filename))
      return 1;
    a = a->next;
    }
  return 0;
  }

void bg_media_tree_purge_directory(bg_media_tree_t * t)
  {
  DIR * dir;
  char filename[FILENAME_MAX];
  struct dirent * dent_ptr;

  struct
    {
    struct dirent d;
    char b[NAME_MAX]; /* Make sure there is enough memory */
    } dent;

  dir = opendir(t->com.directory);
  if(!dir)
    return;

  bg_log(BG_LOG_INFO, LOG_DOMAIN, "Purging %s", t->com.directory);
  
  
  while(!readdir_r(dir, &(dent.d), &dent_ptr))
    {
    if(!dent_ptr)
      break;
    
    if(!strcmp(dent.d.d_name, ".") || !strcmp(dent.d.d_name, "..") ||
       !strcmp(dent.d.d_name, "tree.xml"))
      continue;
    
    if(!albums_have_file(t->children, dent.d.d_name))
      {
      sprintf(filename, "%s/%s", t->com.directory, dent.d.d_name);
      bg_log(BG_LOG_INFO, LOG_DOMAIN, "Removing %s", filename);
      remove(filename);
      }
    }
  closedir(dir);
  }

bg_album_t * bg_media_tree_get_incoming(bg_media_tree_t *t)
  {
  return t->incoming;
  }

bg_cfg_section_t *
bg_media_tree_get_cfg_section(bg_media_tree_t * t)
  {
  return t->cfg_section;
  }

void bg_album_common_prepare_callbacks(bg_album_common_t * com, bg_album_entry_t * entry)
  {
  if(!entry)
    {
    if(com->username) { free(com->username); com->username = (char*)0; }
    if(com->password) { free(com->password); com->password = (char*)0; }
    com->save_auth = 0;
    }
  else
    {
    com->username = bg_strdup(com->username, entry->username);
    com->password = bg_strdup(com->password, entry->password);
    com->save_auth = !!(entry->flags & BG_ALBUM_ENTRY_SAVE_AUTH);
    }
  
  }

void bg_album_common_set_auth_info(bg_album_common_t * com, bg_album_entry_t * entry)
  {
  
  if(!com->username || !com->password)
    return;

  entry->username = bg_strdup(entry->username, com->username);
  entry->password = bg_strdup(entry->password, com->password);

  if(com->save_auth)
    entry->flags |= BG_ALBUM_ENTRY_SAVE_AUTH;
  else
    entry->flags &= ~BG_ALBUM_ENTRY_SAVE_AUTH;

  }

bg_album_t * bg_media_tree_get_device_album(bg_media_tree_t * t, const char * gml)
  {
  bg_album_t * ret = (bg_album_t *)0;
  const bg_plugin_info_t * plugin_info;
  char * protocol = (char *)0;
  char * path = (char *)0;
  
  if(!bg_url_split(gml,
                   &protocol,
                   (char **)0, // user,
                   (char **)0, //  password,
                   (char **)0, //  hostname,
                   (int*)0, //  port,
                   &path)) //  path)
    return (bg_album_t*)0;

  /* 1. Seek the plugin */
  plugin_info = bg_plugin_find_by_protocol(t->com.plugin_reg, protocol);

  if(!plugin_info)
    goto fail;

  ret = t->children;
  while(ret &&
        ((ret->type != BG_ALBUM_TYPE_PLUGIN) ||
         strcmp(ret->plugin_info->name, plugin_info->name)))
    ret = ret->next;

  if(!ret)
    goto fail;
    
  /* Now, look for the device */

  ret = ret->children;

  while(ret && strcmp(ret->device, path))
    ret = ret->next;
  
  fail:
  if(path) free(path);
  if(protocol) free(protocol);
  
  return ret;
  }

void bg_media_tree_copy_current_to_favourites(bg_media_tree_t * t)
  {
  int was_open;
  
  if(!t->com.current_entry)
    {
    bg_log(BG_LOG_WARNING, LOG_DOMAIN, "No current track for copying to favourites");
    return;
    }
  if(!bg_album_is_open(t->com.favourites))
    {
    bg_album_open(t->com.favourites);
    was_open = 0;
    }
  was_open = 1;
  
  bg_album_insert_entries_before(t->com.favourites,
                                 bg_album_entry_copy(t->com.current_album,
                                                     t->com.current_entry),
                                 (bg_album_entry_t*)0);
  
  if(!was_open)
    bg_album_close(t->com.favourites);
  }

#ifdef HAVE_INOTIFY

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

void bg_media_tree_check_sync(bg_media_tree_t * t)
  {
  int i, result;
  char buffer[BUF_LEN];
  bg_album_t * a;
  struct timeval timeout;

  fd_set read_fds;
  
  /* Wait for event */
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  FD_ZERO(&read_fds);
  FD_SET(t->com.inotify_fd, &read_fds);
  if(!select(t->com.inotify_fd+1,
             &read_fds, (fd_set*)0, (fd_set*)0,&timeout))
    return;

  result = read(t->com.inotify_fd, buffer, BUF_LEN);
  
  if(result < 0)
    return;
  
  i = 0;
  while ( i < result )
    {
    struct inotify_event *event =
      ( struct inotify_event * ) &buffer[i];
    
    a = t->children;
    while(a)
      {
      if(bg_album_inotify(a, (uint8_t*)(&buffer[ i ])))
        break;
      a = a->next;
      }
    i += EVENT_SIZE + event->len;
    }
  }

#else

void bg_media_tree_check_sync(bg_media_tree_t * t) { }

#endif