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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2000 Eazel, Inc.
*
* This library 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 library 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 library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Andy Hertzfeld <andy@eazel.com>
*
*/
/* music view - presents the contents of the directory as an album of music */
#include <config.h>
#include "nautilus-music-view.h"
#include "mpg123_handler.h"
#include "pixmaps.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <ctype.h>
#include <libnautilus/libnautilus.h>
#include <libnautilus-extensions/nautilus-background.h>
#include <libnautilus-extensions/nautilus-directory-background.h>
#include <libnautilus-extensions/nautilus-file.h>
#include <libnautilus-extensions/nautilus-file-utilities.h>
#include <libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h>
#include <libnautilus-extensions/nautilus-glib-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <libnautilus-extensions/nautilus-metadata.h>
#include <libnautilus-extensions/nautilus-string.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtksignal.h>
#include <gnome.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnorba/gnorba.h>
#include <limits.h>
struct _NautilusMusicViewDetails {
char *uri;
NautilusContentViewFrame *view_frame;
int background_connection;
int sort_mode;
int selected_index;
int status_timeout;
int current_file_size;
int current_bitrate;
int last_play_status;
gboolean slider_dragging;
GtkVBox *album_container;
GtkWidget *album_title;
GtkWidget *song_list;
GtkWidget *album_image;
GtkWidget *control_box;
GtkWidget *play_control_box;
GtkWidget *song_label;
GtkWidget *total_track_time;
GtkWidget *playtime;
GtkWidget *playtime_bar;
GtkObject *playtime_adjustment;
GtkWidget *inactive_play_pixwidget;
GtkWidget *active_play_pixwidget;
GtkWidget *inactive_pause_pixwidget;
GtkWidget *active_pause_pixwidget;
};
/* structure for holding song info */
typedef struct {
int track_number;
int bitrate;
int track_time;
char *title;
char *artist;
char *album;
char *year;
char *comment;
char *path_uri;
} SongInfo;
enum {
TARGET_URI_LIST,
TARGET_COLOR,
TARGET_BGIMAGE,
TARGET_GNOME_URI_LIST
};
/* button commands */
enum {
PREVIOUS_BUTTON,
PLAY_BUTTON,
PAUSE_BUTTON,
STOP_BUTTON,
NEXT_BUTTON
};
/* sort modes */
enum {
SORT_BY_NUMBER,
SORT_BY_TITLE,
SORT_BY_ARTIST,
SORT_BY_BITRATE,
SORT_BY_TIME
};
static int bitrates[] = {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1};
static GtkTargetEntry music_dnd_target_table[] = {
{ "text/uri-list", 0, TARGET_URI_LIST },
{ "application/x-color", 0, TARGET_COLOR },
{ "property/bgimage", 0, TARGET_BGIMAGE },
{ "special/x-gnome-icon-list", 0, TARGET_GNOME_URI_LIST }
};
#define DEFAULT_BACKGROUND_COLOR "rgb:DDDD/DDDD/BBBB"
static void nautilus_music_view_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
int x,
int y,
GtkSelectionData *selection_data,
guint info,
guint time);
static void nautilus_music_view_initialize_class (NautilusMusicViewClass *klass);
static void nautilus_music_view_initialize (NautilusMusicView *view);
static void nautilus_music_view_destroy (GtkObject *object);
static void nautilus_music_view_update_from_uri (NautilusMusicView *music_view,
const char *uri);
static void music_view_notify_location_change_callback (NautilusContentViewFrame *view,
Nautilus_NavigationInfo *navinfo,
NautilusMusicView *music_view);
static void selection_callback (GtkCList *clist,
int row,
int column,
GdkEventButton *event,
NautilusMusicView *music_view);
static void music_view_set_selected_song_title (NautilusMusicView *music_view,
int row);
static void add_play_controls (NautilusMusicView *music_view);
static void click_column_callback(GtkCList * clist, gint column, NautilusMusicView *music_view);
static void go_to_next_track (NautilusMusicView *music_view);
static void play_current_file (NautilusMusicView *music_view, gboolean from_start);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusMusicView, nautilus_music_view, GTK_TYPE_EVENT_BOX)
static void
nautilus_music_view_initialize_class (NautilusMusicViewClass *klass)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = GTK_OBJECT_CLASS (klass);
widget_class = GTK_WIDGET_CLASS (klass);
object_class->destroy = nautilus_music_view_destroy;
widget_class->drag_data_received = nautilus_music_view_drag_data_received;
}
/* initialize ourselves by connecting to the location change signal and allocating our subviews */
static void
nautilus_music_view_initialize (NautilusMusicView *music_view)
{
GtkWidget *scrollwindow;
char *titles[] = {_("Track "), _("Title"), _("Artist"), _("Year"), _("Bitrate "), _("Time "), _("Album"), _("Comment"),};
music_view->details = g_new0 (NautilusMusicViewDetails, 1);
music_view->details->view_frame = nautilus_content_view_frame_new (GTK_WIDGET (music_view));
gtk_signal_connect (GTK_OBJECT (music_view->details->view_frame),
"notify_location_change",
GTK_SIGNAL_FUNC (music_view_notify_location_change_callback),
music_view);
music_view->details->status_timeout = -1;
music_view->details->slider_dragging = FALSE;
/* allocate a vbox to contain all of the views */
music_view->details->album_container = GTK_VBOX (gtk_vbox_new (FALSE, 8));
gtk_container_set_border_width (GTK_CONTAINER (music_view->details->album_container), 4);
gtk_container_add (GTK_CONTAINER (music_view), GTK_WIDGET (music_view->details->album_container));
gtk_widget_show (GTK_WIDGET (music_view->details->album_container));
/* allocate a widget for the album title */
music_view->details->album_title = gtk_label_new (_("Album Title"));
/* FIXME bugzilla.eazel.com 667: don't use hardwired font like this */
nautilus_gtk_widget_set_font_by_name (music_view->details->album_title,
"-*-helvetica-medium-r-normal-*-18-*-*-*-*-*-*-*"); ;
gtk_box_pack_start (GTK_BOX (music_view->details->album_container), music_view->details->album_title, FALSE, FALSE, 0);
gtk_widget_show (music_view->details->album_title);
/* allocate a list widget to hold the song list */
music_view->details->song_list = gtk_clist_new_with_titles (8, titles);
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 0, 36); /* track number */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 1, 204); /* song name */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 2, 96); /* artist */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 3, 42); /* year */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 4, 42); /* bitrate */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 5, 42); /* time */
/* we have 2 invisible columns at the end to hold data displayed as the song title */
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 6, 0);
gtk_clist_set_column_width (GTK_CLIST (music_view->details->song_list), 7, 0);
/* default the year, album and comment to hidden */
gtk_clist_set_column_visibility (GTK_CLIST (music_view->details->song_list), 3, FALSE);
gtk_clist_set_column_visibility (GTK_CLIST (music_view->details->song_list), 6, FALSE);
gtk_clist_set_column_visibility (GTK_CLIST (music_view->details->song_list), 7, FALSE);
/* make some of the columns right justified */
gtk_clist_set_column_justification(GTK_CLIST(music_view->details->song_list), 0, GTK_JUSTIFY_RIGHT);
gtk_clist_set_column_justification(GTK_CLIST(music_view->details->song_list), 3, GTK_JUSTIFY_RIGHT);
gtk_clist_set_column_justification(GTK_CLIST(music_view->details->song_list), 4, GTK_JUSTIFY_RIGHT);
gtk_clist_set_column_justification(GTK_CLIST(music_view->details->song_list), 5, GTK_JUSTIFY_RIGHT);
gtk_signal_connect (GTK_OBJECT (music_view->details->song_list),
"select_row", GTK_SIGNAL_FUNC (selection_callback), music_view);
scrollwindow = gtk_scrolled_window_new (NULL, gtk_clist_get_vadjustment (GTK_CLIST (music_view->details->song_list)));
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scrollwindow), music_view->details->song_list);
gtk_clist_set_selection_mode (GTK_CLIST (music_view->details->song_list), GTK_SELECTION_BROWSE);
gtk_box_pack_start (GTK_BOX (music_view->details->album_container), scrollwindow, TRUE, TRUE, 0);
gtk_widget_show (music_view->details->song_list);
gtk_widget_show (scrollwindow);
/* connect a signal to let us know when the column titles are clicked */
gtk_signal_connect(GTK_OBJECT(music_view->details->song_list), "click_column",
GTK_SIGNAL_FUNC(click_column_callback), music_view);
/* make an hbox to hold the optional cover and other controls */
music_view->details->control_box = gtk_hbox_new (FALSE, 4);
gtk_box_pack_start (GTK_BOX (music_view->details->album_container), music_view->details->control_box, FALSE, FALSE, 2);
gtk_widget_show (music_view->details->control_box);
/* prepare ourselves to receive dropped objects */
gtk_drag_dest_set (GTK_WIDGET (music_view),
GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
music_dnd_target_table, NAUTILUS_N_ELEMENTS (music_dnd_target_table), GDK_ACTION_COPY);
/* finally, show the view itself */
gtk_widget_show (GTK_WIDGET (music_view));
}
static void
nautilus_music_view_destroy (GtkObject *object)
{
NautilusMusicView *music_view = NAUTILUS_MUSIC_VIEW (object);
bonobo_object_unref (BONOBO_OBJECT (music_view->details->view_frame));
g_free (music_view->details->uri);
g_free (music_view->details);
NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}
/* utility to return the text describing a song */
static char *
get_song_text (NautilusMusicView *music_view, int row)
{
char *song_text, *song_name, *album_name, *year;
album_name = NULL;
year = NULL;
gtk_clist_get_text (GTK_CLIST(music_view->details->song_list), row, 1, &song_name);
gtk_clist_get_text (GTK_CLIST(music_view->details->song_list), row, 3, &year);
gtk_clist_get_text (GTK_CLIST(music_view->details->song_list), row, 6, &album_name);
if (album_name) {
if (year)
song_text = g_strdup_printf("%s\n%s (%s)", song_name, album_name, year);
else
song_text = g_strdup_printf("%s\n%s", song_name, album_name);
} else if (year) {
song_text = g_strdup_printf("%s (%s)", song_name, year);
} else {
song_text = g_strdup(song_name);
}
return song_text;
}
/* set the song title to the selected one */
static void
music_view_set_selected_song_title (NautilusMusicView *music_view, int row)
{
char *label_text;
char *temp_str;
music_view->details->selected_index = row;
label_text = get_song_text(music_view, row);
gtk_label_set(GTK_LABEL(music_view->details->song_label), label_text);
g_free(label_text);
gtk_clist_get_text (GTK_CLIST(music_view->details->song_list), row, 5, &temp_str);
gtk_label_set(GTK_LABEL(music_view->details->total_track_time), temp_str);
}
/* handle a row being selected in the list view by playing the corresponding song */
/* FIXME bugzilla.eazel.com 721: xmms shouldn't be hardwired */
static void
selection_callback(GtkCList * clist, int row, int column, GdkEventButton * event, NautilusMusicView* music_view)
{
gboolean is_playing;
char *song_name;
int play_mode;
play_mode = get_play_status();
is_playing = (play_mode == STATUS_PLAY) || (play_mode == STATUS_PAUSE);
if (is_playing && (music_view->details->selected_index == row))
return;
if (is_playing)
stop_playing_file();
song_name = gtk_clist_get_row_data (clist, row);
if (song_name == NULL) {
return;
}
music_view_set_selected_song_title(music_view, row);
if (is_playing)
play_current_file(music_view, FALSE);
}
/* handle clicks in the songlist columns */
static void click_column_callback (GtkCList * clist, gint column, NautilusMusicView *music_view)
{
if (music_view->details->sort_mode == column)
return;
music_view->details->sort_mode = column;
nautilus_music_view_update_from_uri (music_view, music_view->details->uri);
}
/* Component embedding support */
NautilusContentViewFrame *
nautilus_music_view_get_view_frame (NautilusMusicView *music_view)
{
return music_view->details->view_frame;
}
/* here are some utility routines for reading ID3 tags from mp3 files */
/* initialize a songinfo structure */
static void
initialize_song_info (SongInfo *info)
{
/* Only called after g_new0. */
info->track_number = -1;
}
/* deallocate a songinfo structure */
static void
release_song_info (SongInfo *info)
{
g_free (info->title);
g_free (info->artist);
g_free (info->album);
g_free (info->year);
g_free (info->comment);
g_free (info->path_uri);
g_free (info);
}
/* determine if the passed in filename is an mp3 file by looking at the extension */
/* FIXME: use mime-type for this? */
static gboolean
is_mp3_file(const char *song_uri)
{
return nautilus_str_has_suffix(song_uri, ".mp3")
|| nautilus_str_has_suffix(song_uri, ".MP3");
}
/* read the id3 tag of the file if present */
static gboolean
read_id_tag (const char *song_uri, SongInfo *song_info)
{
GnomeVFSHandle *mp3_file;
GnomeVFSResult result;
GnomeVFSFileSize bytes_read;
char tag_buffer[129];
char temp_str[31];
result = gnome_vfs_open(&mp3_file, song_uri, GNOME_VFS_OPEN_READ);
if (result != GNOME_VFS_OK) {
return FALSE;
}
gnome_vfs_seek (mp3_file, GNOME_VFS_SEEK_END, -128);
if (gnome_vfs_read (mp3_file, tag_buffer, 128, &bytes_read) != GNOME_VFS_OK) {
gnome_vfs_close (mp3_file);
return FALSE;
}
gnome_vfs_close (mp3_file);
if (tag_buffer[0] != 'T' || tag_buffer[1] != 'A' || tag_buffer[2] != 'G')
return FALSE;
temp_str[30] = '\0';
strncpy (temp_str, &tag_buffer[3], 30);
song_info->title = g_strdup(temp_str);
strncpy (temp_str, &tag_buffer[33], 30);
song_info->artist = g_strdup(temp_str);
strncpy (temp_str, &tag_buffer[63], 30);
song_info->album = g_strdup(temp_str);
temp_str[4] = '\0';
strncpy (temp_str, &tag_buffer[93], 4);
song_info->year = g_strdup(temp_str);
strncpy (temp_str, &tag_buffer[97], 30);
song_info->comment = g_strdup(temp_str);
if (tag_buffer[97 + 28] == 0) {
song_info->track_number = tag_buffer[97 + 29];
} else {
song_info->track_number = -1;
}
return TRUE;
}
/* this utility routine is the inner loop of fetch_bit_rate that scans the passed in buffer
for a sync field. If it finds a valid header, it returns the bit-rate index; otherwise, return -1 */
static int
scan_for_header(guchar *buffer, int buffer_length)
{
int index;
for (index = 0; index < (buffer_length - 2); index++) {
if ((buffer[index] == 255) &&
((buffer[index + 1] & 224) != 0)) {
return (buffer[index + 2] >> 4) & 15;
}
}
return -1;
}
/* fetch_bit_rate returns the bit rate of the file by scanning for a frame and extracting the
information from the frame header */
static int
fetch_bit_rate (const char *song_uri)
{
guchar buffer[1024];
GnomeVFSHandle *mp3_file;
GnomeVFSResult result;
GnomeVFSFileSize length_read;
int bit_rate_index;
/* open the file */
result = gnome_vfs_open(&mp3_file, song_uri, GNOME_VFS_OPEN_READ);
if (result != GNOME_VFS_OK) {
return -1;
}
/* read a byte at a time until we get a sync field, which consists of a byte of 255 followed
by a byte with the next 3 bits on */
bit_rate_index = -1;
while (TRUE) {
result = gnome_vfs_read(mp3_file, buffer, sizeof(buffer), &length_read);
if ((result != GNOME_VFS_OK) || (length_read < 3)) {
break;
}
bit_rate_index = scan_for_header(buffer, length_read);
if (bit_rate_index >= 0)
break;
};
gnome_vfs_close(mp3_file);
/* fetch the bitrate field, and look up the actual bitrate in the table */
if (bit_rate_index < 0)
return -1;
return bitrates[bit_rate_index];
}
/* fetch_play_time takes the pathname to a file and returns the play time in seconds */
static int
fetch_play_time (const char *song_uri, int bitrate)
{
NautilusFile *file = nautilus_file_get (song_uri + 7);
GnomeVFSFileSize file_size = nautilus_file_get_size (file);
nautilus_file_unref(file);
return file_size / (125 * bitrate);
}
/* format_play_time takes the pathname to a file and returns the play time formated as mm:ss */
static char *
format_play_time (const char *song_uri, int bitrate)
{
int seconds = fetch_play_time(song_uri, bitrate);
int minutes = seconds / 60;
int remain_seconds = seconds - (60 * minutes);
char *result = g_strdup_printf ("%d:%02d ", minutes, remain_seconds);
return result;
}
/* utility routine to pull an initial number from the beginning of the passed in name.
return -1 if there wasn't any */
static int
extract_initial_number(const char *name_str)
{
char *temp_str;
gboolean found_digit;
int accumulator;
found_digit = FALSE;
accumulator = 0;
temp_str = (char*) name_str;
while (*temp_str) {
if (isdigit(*temp_str)) {
found_digit = TRUE;
accumulator = (10 * accumulator) + *temp_str - 48;
} else
break;
temp_str += 1;
}
if (found_digit)
return accumulator;
return -1;
}
/* allocate a return a song info record, from an mp3 tag if present, or from intrinsic info */
static SongInfo *
fetch_song_info (const char *song_uri, int file_order)
{
gboolean has_info = FALSE;
SongInfo *info;
if (!is_mp3_file (song_uri)) {
return NULL;
}
info = g_new0 (SongInfo, 1);
initialize_song_info (info);
has_info = read_id_tag (song_uri, info);
/* if we couldn't get a track number, see if we can pull one from
the beginning of the file name */
if (info->track_number <= 0) {
info->track_number = extract_initial_number(g_basename(song_uri));
}
/* there was no id3 tag, so set up the info heuristically from the file name and file order */
if (!has_info) {
info->title = g_strdup (g_basename (song_uri));
}
info->bitrate = fetch_bit_rate(song_uri);
info->track_time = fetch_play_time(song_uri, info->bitrate);
return info;
}
/* sort comparison routines */
static int
sort_by_track_number (gconstpointer ap, gconstpointer bp)
{
SongInfo *a, *b;
a = (SongInfo *) ap;
b = (SongInfo *) bp;
return (int) a->track_number - b->track_number;
}
static int
sort_by_title (gconstpointer ap, gconstpointer bp)
{
SongInfo *a, *b;
a = (SongInfo *) ap;
b = (SongInfo *) bp;
return strcmp(a->title, b->title);
}
static int
sort_by_artist (gconstpointer ap, gconstpointer bp)
{
SongInfo *a, *b;
a = (SongInfo *) ap;
b = (SongInfo *) bp;
return strcmp(a->artist, b->artist);
}
static int
sort_by_time (gconstpointer ap, gconstpointer bp)
{
SongInfo *a, *b;
a = (SongInfo *) ap;
b = (SongInfo *) bp;
return a->track_time - b->track_time;
}
static int
sort_by_bitrate (gconstpointer ap, gconstpointer bp)
{
SongInfo *a, *b;
a = (SongInfo *) ap;
b = (SongInfo *) bp;
return a->bitrate - b->bitrate;
}
/* utility routine to determine most common attribute in song list. The passed in boolean selects
album or artist. Return NULL if no names or too heterogenous. This first cut just captures
the first one it can - soon, we'll use a hash table and count them up */
static char *
determine_attribute (GList *song_list, gboolean is_artist)
{
SongInfo *info;
GList *p;
for (p = song_list; p != NULL; p = p->next) {
info = (SongInfo *) p->data;
if (is_artist && info->artist != NULL) {
return g_strdup (info->artist);
} else if (!is_artist && info->album) {
return g_strdup (info->album);
}
}
return NULL;
}
/* handle the "background changed" signal */
static void
nautilus_music_view_background_changed (NautilusMusicView *music_view)
{
NautilusDirectory *directory;
NautilusBackground *background;
char *color_spec, *image;
directory = nautilus_directory_get (music_view->details->uri);
background = nautilus_get_widget_background (GTK_WIDGET (music_view));
color_spec = nautilus_background_get_color (background);
nautilus_directory_set_metadata (directory,
NAUTILUS_METADATA_KEY_DIRECTORY_BACKGROUND_COLOR,
DEFAULT_BACKGROUND_COLOR,
color_spec);
g_free (color_spec);
image = nautilus_background_get_tile_image_uri (background);
nautilus_directory_set_metadata (directory,
NAUTILUS_METADATA_KEY_DIRECTORY_BACKGROUND_IMAGE,
NULL,
image);
g_free (image);
nautilus_directory_unref(directory);
}
/* set up the background of the music view from the metadata associated with the uri */
static void
nautilus_music_view_set_up_background (NautilusMusicView *music_view, const char *uri)
{
NautilusDirectory *directory;
NautilusBackground *background;
char *background_color;
char *background_image;
directory = nautilus_directory_get (music_view->details->uri);
/* Connect the background changed signal to code that writes the color. */
background = nautilus_get_widget_background (GTK_WIDGET (music_view));
if (music_view->details->background_connection == 0) {
music_view->details->background_connection =
gtk_signal_connect_object (GTK_OBJECT (background),
"changed",
nautilus_music_view_background_changed,
GTK_OBJECT (music_view));
}
/* Set up the background color and image from the metadata. */
background_color = nautilus_directory_get_metadata (directory,
NAUTILUS_METADATA_KEY_DIRECTORY_BACKGROUND_COLOR,
DEFAULT_BACKGROUND_COLOR);
background_image = nautilus_directory_get_metadata (directory,
NAUTILUS_METADATA_KEY_DIRECTORY_BACKGROUND_IMAGE,
NULL);
nautilus_directory_unref(directory);
nautilus_background_set_color (background, background_color);
g_free (background_color);
nautilus_background_set_tile_image_uri (background, background_image);
g_free (background_image);
}
/* utility routine to sort the song list */
static GList *
sort_song_list(NautilusMusicView *music_view, GList* song_list)
{
/* sort by the specified criteria */
switch (music_view->details->sort_mode) {
case SORT_BY_NUMBER:
song_list = g_list_sort (song_list, sort_by_track_number);
break;
case SORT_BY_TITLE:
song_list = g_list_sort (song_list, sort_by_title);
break;
case SORT_BY_ARTIST:
song_list = g_list_sort (song_list, sort_by_artist);
break;
case SORT_BY_BITRATE:
song_list = g_list_sort (song_list, sort_by_bitrate);
break;
case SORT_BY_TIME:
song_list = g_list_sort (song_list, sort_by_time);
break;
default:
g_warning("unknown sort mode");
break;
}
return song_list;
}
/* update the status feedback of the play controls */
static void
update_play_controls_status (NautilusMusicView *music_view, int new_status)
{
if (new_status == STATUS_PLAY) {
gtk_widget_show(music_view->details->active_play_pixwidget);
gtk_widget_hide(music_view->details->inactive_play_pixwidget);
} else {
gtk_widget_hide(music_view->details->active_play_pixwidget);
gtk_widget_show(music_view->details->inactive_play_pixwidget);
}
if (new_status == STATUS_PAUSE) {
gtk_widget_show(music_view->details->active_pause_pixwidget);
gtk_widget_hide(music_view->details->inactive_pause_pixwidget);
} else {
gtk_widget_hide(music_view->details->active_pause_pixwidget);
gtk_widget_show(music_view->details->inactive_pause_pixwidget);
}
}
/* utility to reset the playtime to the inactive state */
static void
reset_playtime (NautilusMusicView *music_view)
{
gtk_adjustment_set_value(GTK_ADJUSTMENT(music_view->details->playtime_adjustment), 0.0);
gtk_range_set_adjustment(GTK_RANGE(music_view->details->playtime_bar), GTK_ADJUSTMENT(music_view->details->playtime_adjustment));
gtk_widget_set_sensitive(music_view->details->playtime_bar, FALSE);
gtk_label_set(GTK_LABEL(music_view->details->playtime), "--:--");
}
/* status display timer task */
static int
play_status_display (NautilusMusicView *music_view)
{
int minutes, seconds;
float percentage;
char play_time_str[256];
int frameNo, status;
gboolean is_playing;
status = get_play_status();
is_playing = (status == STATUS_PLAY) || (status == STATUS_PAUSE);
if (status == STATUS_NEXT) {
stop_playing_file();
go_to_next_track(music_view);
music_view->details->status_timeout = -1;
return FALSE;
}
if (music_view->details->last_play_status != status) {
music_view->details->last_play_status = status;
update_play_controls_status(music_view, status);
}
if (is_playing) {
if (!music_view->details->slider_dragging) {
frameNo = get_current_frame();
seconds = frameNo * 1152 / 44100;
minutes = seconds / 60;
seconds = seconds % 60;
sprintf(play_time_str, "%02d:%02d", minutes, seconds);
percentage = (gfloat) frameNo * 419 * music_view->details->current_bitrate / 128 / music_view->details->current_file_size * 100;
gtk_adjustment_set_value(GTK_ADJUSTMENT(music_view->details->playtime_adjustment), percentage);
gtk_range_set_adjustment(GTK_RANGE(music_view->details->playtime_bar), GTK_ADJUSTMENT(music_view->details->playtime_adjustment));
if (!music_view->details->slider_dragging)
gtk_label_set(GTK_LABEL(music_view->details->playtime), play_time_str);
}
} else
reset_playtime(music_view);
if (!is_playing)
music_view->details->status_timeout = -1;
return is_playing;
}
/* track incrementing routines */
static void
play_current_file(NautilusMusicView *music_view, gboolean from_start)
{
int play_mode;
char *song_filename, *temp_str;
NautilusFile *file;
play_mode = get_play_status();
gtk_clist_select_row (GTK_CLIST(music_view->details->song_list), music_view->details->selected_index, 0);
song_filename = gtk_clist_get_row_data(GTK_CLIST(music_view->details->song_list),
music_view->details->selected_index);
if (song_filename == NULL) {
return;
}
/* set up the current bitrate and file size so we can give progress feedback */
gtk_clist_get_text (GTK_CLIST(music_view->details->song_list), music_view->details->selected_index, 4, &temp_str);
music_view->details->current_bitrate = atoi(temp_str);
file = nautilus_file_get (song_filename + 7);
music_view->details->current_file_size = (int) nautilus_file_get_size (file);
nautilus_file_unref(file);
gtk_widget_set_sensitive(music_view->details->playtime_bar, TRUE);
if (music_view->details->status_timeout != -1)
gtk_timeout_remove(music_view->details->status_timeout);
music_view->details->status_timeout = gtk_timeout_add(900, (GtkFunction) play_status_display, music_view);
start_playing_file(song_filename + 7, from_start || (play_mode != STATUS_PAUSE));
}
static void
stop_if_playing(NautilusMusicView *music_view)
{
int play_mode;
play_mode = get_play_status();
if (play_mode == STATUS_PLAY || play_mode == STATUS_PAUSE)
stop_playing_file();
}
static void
go_to_next_track (NautilusMusicView *music_view)
{
stop_if_playing(music_view);
if (music_view->details->selected_index < (GTK_CLIST(music_view->details->song_list)->rows - 1)) {
music_view->details->selected_index += 1;
play_current_file(music_view, TRUE);
}
else {
update_play_controls_status(music_view, STATUS_STOP);
reset_playtime(music_view);
stop_playing_file();
}
}
static void
go_to_previous_track (NautilusMusicView *music_view) {
int frame;
stop_if_playing(music_view);
frame = get_current_frame();
/* if we're in the first 3 seconds of the song, go to the previous one, otherwise go to the beginning of this track */
if ((frame < 3*72) && (music_view->details->selected_index > 0))
music_view->details->selected_index -= 1;
play_current_file(music_view, TRUE);
}
/* callback for the play control semantics */
/* callback for buttons */
static void play_button_callback (GtkWidget * widget, NautilusMusicView *music_view)
{
play_current_file(music_view, FALSE);
}
static void stop_button_callback (GtkWidget * widget, NautilusMusicView *music_view)
{
stop_playing_file();
}
static void pause_button_callback (GtkWidget * widget, NautilusMusicView *music_view)
{
pause_playing_file();
}
static void prev_button_callback (GtkWidget * widget, NautilusMusicView *music_view)
{
go_to_previous_track(music_view);
}
static void next_button_callback (GtkWidget * widget, NautilusMusicView *music_view)
{
go_to_next_track(music_view);
}
/* here are the callbacks that handle seeking within a song by dragging the progress bar.
"Mouse down" sets the slider_dragging boolean, "motion_notify" updates the label on the left, while
"mouse up" actually moves the frame index. */
/* handle slider button press */
static void slider_press_callback(GtkWidget *bar, GdkEvent *event, NautilusMusicView *music_view)
{
music_view->details->slider_dragging = TRUE;
}
/* handle mouse motion by updating the time, but not actually seeking until the user lets go */
static void slider_moved_callback(GtkWidget *bar, GdkEvent *event, NautilusMusicView *music_view)
{
char temp_str[256];
int nframe, seconds, minutes;
GtkAdjustment *adjustment;
if (music_view->details->slider_dragging) {
adjustment = gtk_range_get_adjustment(GTK_RANGE(bar));
nframe = adjustment->value / (419.0 * music_view->details->current_bitrate / 128.0 / music_view->details->current_file_size * 100.0);
seconds = nframe * 1152 / 44100;
minutes = seconds / 60;
seconds = seconds % 60;
sprintf(temp_str, "%02d:%02d", minutes, seconds);
gtk_label_set(GTK_LABEL(music_view->details->playtime), temp_str);
}
}
/* callback for slider button release - seek to desired location */
static void slider_release_callback(GtkWidget *bar, GdkEvent *event, NautilusMusicView *music_view)
{
int play_status, nframe;
GtkAdjustment *adjustment;
play_status = get_play_status();
if (music_view->details->slider_dragging) {
adjustment = gtk_range_get_adjustment(GTK_RANGE(bar));
nframe = adjustment->value / (419.0 * music_view->details->current_bitrate / 128.0 / music_view->details->current_file_size * 100.0);
if ((play_status == STATUS_PLAY) || (play_status == STATUS_PAUSE)) {
pause_playing_file();
set_current_frame(nframe);
play_current_file(music_view, FALSE);
}
}
music_view->details->slider_dragging = FALSE;
}
/* create a button with an xpm label */
static GtkWidget *xpm_label_box (NautilusMusicView *music_view, gchar * xpm_data[])
{
GtkWidget *box;
GtkStyle *style;
GdkPixmap *pixmap;
GdkBitmap *mask;
GtkWidget *pix_widget;
box = gtk_hbox_new(FALSE, 0);
gtk_container_border_width(GTK_CONTAINER(box), 2);
style = gtk_widget_get_style(GTK_WIDGET(music_view));
pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(music_view)->window, &mask, &style->bg[GTK_STATE_NORMAL], xpm_data);
pix_widget = gtk_pixmap_new(pixmap, mask);
gtk_box_pack_start(GTK_BOX(box), pix_widget, TRUE, FALSE, 3);
gtk_widget_show(pix_widget);
return box;
}
/* creates a button with 2 internal pixwidgets, with only one visible at a time */
static GtkWidget *xpm_dual_label_box (NautilusMusicView *music_view, char * xpm_data[], gchar *alt_xpm_data[], GtkWidget **main_pixwidget, GtkWidget **alt_pixwidget )
{
GtkWidget *box;
GtkStyle *style;
GdkPixmap *pixmap;
GdkBitmap *mask;
box = gtk_hbox_new(FALSE, 0);
gtk_container_border_width(GTK_CONTAINER(box), 2);
style = gtk_widget_get_style(GTK_WIDGET(music_view));
/* create the main pixwidget */
pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(music_view)->window, &mask, &style->bg[GTK_STATE_NORMAL], xpm_data);
*main_pixwidget = gtk_pixmap_new(pixmap, mask);
gtk_box_pack_start(GTK_BOX(box), *main_pixwidget, TRUE, FALSE, 3);
gtk_widget_show(*main_pixwidget);
/* create the alternative pixwidget */
pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(music_view)->window, &mask, &style->bg[GTK_STATE_NORMAL], alt_xpm_data);
*alt_pixwidget = gtk_pixmap_new(pixmap, mask);
gtk_box_pack_start(GTK_BOX(box), *alt_pixwidget, TRUE, FALSE, 3);
gtk_widget_hide(*alt_pixwidget);
return box;
}
/* add the play controls */
static void add_play_controls (NautilusMusicView *music_view)
{
GtkWidget *table;
GtkWidget *box;
GtkWidget *vbox, *hbox2;
GtkWidget *button;
GtkTooltips *tooltips;
tooltips = gtk_tooltips_new();
table = gtk_table_new(3, 7, 0);
gtk_table_set_row_spacings(GTK_TABLE(table), 2);
gtk_table_set_col_spacings(GTK_TABLE(table), 1);
music_view->details->song_label = gtk_label_new(_("Song Title"));
gtk_widget_show(music_view->details->song_label);
vbox = gtk_vbox_new(0, 0);
gtk_box_pack_start(GTK_BOX(music_view->details->control_box), vbox, FALSE, FALSE, 6);
gtk_box_pack_start(GTK_BOX(vbox), music_view->details->song_label, FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 4);
gtk_widget_show(vbox);
music_view->details->play_control_box = vbox;
/* playtime label */
hbox2 = gtk_hbox_new(0, 0);
gtk_table_attach(GTK_TABLE(table), hbox2, 0, 6, 0, 1, 0, 0, 0, 0);
gtk_widget_show(hbox2);
music_view->details->playtime = gtk_label_new("--:--");
gtk_widget_show(music_view->details->playtime);
gtk_box_pack_start(GTK_BOX(hbox2), music_view->details->playtime, FALSE, FALSE, 0);
/* progress bar */
music_view->details->playtime_adjustment = gtk_adjustment_new(0, 0, 101, 1, 5, 1);
music_view->details->playtime_bar = gtk_hscale_new(GTK_ADJUSTMENT(music_view->details->playtime_adjustment));
gtk_signal_connect(GTK_OBJECT(music_view->details->playtime_bar), "button_press_event", GTK_SIGNAL_FUNC(slider_press_callback), music_view);
gtk_signal_connect(GTK_OBJECT(music_view->details->playtime_bar), "button_release_event", GTK_SIGNAL_FUNC(slider_release_callback), music_view);
gtk_signal_connect(GTK_OBJECT(music_view->details->playtime_bar), "motion_notify_event", GTK_SIGNAL_FUNC(slider_moved_callback), music_view);
gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), music_view->details->playtime_bar, "Drag to seek within track", NULL);
gtk_scale_set_draw_value(GTK_SCALE(music_view->details->playtime_bar), 0);
gtk_widget_show(music_view->details->playtime_bar);
gtk_widget_set_sensitive(music_view->details->playtime_bar, FALSE);
gtk_box_pack_start(GTK_BOX(hbox2), music_view->details->playtime_bar, FALSE, FALSE, 4);
/* total label */
music_view->details->total_track_time = gtk_label_new("--:--");
gtk_widget_show(music_view->details->total_track_time);
gtk_box_pack_start(GTK_BOX(hbox2), music_view->details->total_track_time, FALSE, FALSE, 0);
gtk_table_set_row_spacing(GTK_TABLE(table), 0, 5);
gtk_widget_show(music_view->details->playtime_bar);
/* buttons */
/* previous track button */
box = xpm_label_box (music_view, prev_xpm);
gtk_widget_show (box);
button = gtk_button_new ();
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), button, _("Previous"), NULL);
gtk_container_add (GTK_CONTAINER(button), box);
gtk_signal_connect (GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(prev_button_callback), music_view);
gtk_table_attach (GTK_TABLE(table), button, 0, 1, 1, 2, 0, 0, 0, 0);
gtk_button_set_relief (GTK_BUTTON(button), GTK_RELIEF_NORMAL);
gtk_widget_show (button);
/* play button */
box = xpm_dual_label_box (music_view, play_xpm, play_green_xpm, &music_view->details->inactive_play_pixwidget, &music_view->details->active_play_pixwidget);
gtk_widget_show (box);
button = gtk_button_new ();
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), button, _("Play"), NULL);
gtk_button_set_relief (GTK_BUTTON(button), GTK_RELIEF_NORMAL);
gtk_container_add (GTK_CONTAINER(button), box);
gtk_signal_connect (GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(play_button_callback), music_view);
gtk_table_attach (GTK_TABLE(table), button, 1, 2, 1, 2, 0, 0, 0, 0);
gtk_widget_show (button);
/* pause button */
box = xpm_dual_label_box (music_view, pause_xpm, pause_green_xpm, &music_view->details->inactive_pause_pixwidget, &music_view->details->active_pause_pixwidget);
gtk_widget_show (box);
button = gtk_button_new ();
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), button, _("Pause"), NULL);
gtk_button_set_relief (GTK_BUTTON(button), GTK_RELIEF_NORMAL);
gtk_container_add (GTK_CONTAINER(button), box);
gtk_signal_connect (GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(pause_button_callback), music_view);
gtk_table_attach (GTK_TABLE(table), button, 2, 3, 1, 2, 0, 0, 0, 0);
gtk_widget_show (button);
/* stop button */
box = xpm_label_box (music_view, stop_xpm);
gtk_widget_show (box);
button = gtk_button_new ();
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), button, _("Stop"), NULL);
gtk_button_set_relief (GTK_BUTTON(button), GTK_RELIEF_NORMAL);
gtk_container_add (GTK_CONTAINER(button), box);
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(stop_button_callback), music_view);
gtk_table_attach (GTK_TABLE(table), button, 3, 4, 1, 2, 0, 0, 0, 0);
gtk_widget_show(button);
/* next button */
box = xpm_label_box (music_view, next_xpm);
gtk_widget_show(box);
button = gtk_button_new();
gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), button, _("Next"), NULL);
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NORMAL);
gtk_container_add(GTK_CONTAINER(button), box);
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(next_button_callback), music_view);
gtk_table_attach (GTK_TABLE(table), button, 4, 5, 1, 2, 0, 0, 0, 0);
gtk_widget_show(button);
gtk_widget_show(table);
}
/* here's where we do most of the real work of populating the view with info from the new uri */
static void
nautilus_music_view_update_from_uri (NautilusMusicView *music_view, const char *uri)
{
GnomeVFSResult result;
GnomeVFSFileInfo *current_file_info;
GnomeVFSDirectoryList *list;
char* clist_entry[8];
GList *p;
GdkPixbuf *pixbuf;
GdkPixmap *pixmap;
GdkBitmap *mask;
GList *song_list;
SongInfo *info;
char *path_uri;
char *image_path_uri;
int file_index;
int track_index;
song_list = NULL;
image_path_uri = NULL;
file_index = 1;
track_index = 0;
/* set up the background from the metadata */
nautilus_music_view_set_up_background(music_view, uri);
/* iterate through the directory, collecting mp3 files and extracting id3 data if present */
result = gnome_vfs_directory_list_load (&list, uri,
GNOME_VFS_FILE_INFO_GETMIMETYPE,
NULL, NULL);
if (result != GNOME_VFS_OK) {
/* FIXME: need to show an alert here */
g_warning("cant open %s in music_view_update", uri);
return;
}
current_file_info = gnome_vfs_directory_list_first (list);
while (current_file_info != NULL) {
/* skip invisible files, for now */
if (current_file_info->name[0] == '.') {
current_file_info = gnome_vfs_directory_list_next(list);
continue;
}
path_uri = nautilus_make_path(uri, current_file_info->name);
/* fetch info and queue it if it's an mp3 file */
info = fetch_song_info (path_uri, file_index);
if (info) {
info->path_uri = path_uri;
file_index += 1;
song_list = g_list_append (song_list, info);
} else {
/* it's not an mp3 file, so see if it's an image */
NautilusFile *file = nautilus_file_get (path_uri + 7);
char *mime_type = nautilus_file_get_mime_type (file);
if (nautilus_str_has_prefix (mime_type, "image/")) {
/* for now, just keep the first image */
if (image_path_uri == NULL) {
image_path_uri = g_strdup (path_uri);
}
}
nautilus_file_unref (file);
g_free (path_uri);
g_free (mime_type);
}
current_file_info = gnome_vfs_directory_list_next(list);
}
gnome_vfs_directory_list_destroy(list);
song_list = sort_song_list(music_view, song_list);
/* populate the clist */
gtk_clist_clear (GTK_CLIST (music_view->details->song_list));
for (p = song_list; p != NULL; p = p->next) {
info = (SongInfo *)p->data;
clist_entry[0] = malloc(4);
if (info->track_number > 0)
sprintf(clist_entry[0], "%d ", info->track_number);
else
clist_entry[0] = '\0';
clist_entry[1] = NULL;
clist_entry[2] = NULL;
clist_entry[3] = NULL;
clist_entry[4] = NULL;
clist_entry[5] = NULL;
clist_entry[6] = NULL;
clist_entry[7] = NULL;
if (info->title)
clist_entry[1] = g_strdup(info->title);
if (info->artist)
clist_entry[2] = g_strdup(info->artist);
if (info->year)
clist_entry[3] = g_strdup(info->year);
if (info->bitrate > 0)
clist_entry[4] = g_strdup_printf("%d ", info->bitrate);
if (info->path_uri)
clist_entry[5] = format_play_time(info->path_uri, info->bitrate);
if (info->album)
clist_entry[6] = g_strdup(info->album);
if (info->comment)
clist_entry[7] = g_strdup(info->comment);
gtk_clist_append(GTK_CLIST(music_view->details->song_list), clist_entry);
gtk_clist_set_row_data(GTK_CLIST(music_view->details->song_list),
track_index, g_strdup(info->path_uri));
track_index += 1;
}
/* install the album cover */
if (image_path_uri != NULL) {
pixbuf = gdk_pixbuf_new_from_file(image_path_uri + 7);
pixbuf = nautilus_gdk_pixbuf_scale_to_fit(pixbuf, 128, 128);
gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &mask, 128);
gdk_pixbuf_unref (pixbuf);
if (music_view->details->album_image == NULL) {
music_view->details->album_image = gtk_pixmap_new(pixmap, mask);
gtk_box_pack_start(GTK_BOX(music_view->details->control_box),
music_view->details->album_image, FALSE, FALSE, 2);
}
else {
gtk_pixmap_set (GTK_PIXMAP (music_view->details->album_image), pixmap, mask);
}
gtk_widget_show (music_view->details->album_image);
g_free(image_path_uri);
} else if (music_view->details->album_image != NULL) {
gtk_widget_hide (music_view->details->album_image);
}
/* determine the album title/artist line */
if (music_view->details->album_title) {
char *artist_name, *temp_str;
char* album_name;
album_name = determine_attribute (song_list, FALSE);
if (album_name == NULL) {
album_name = g_strdup (g_basename (uri));
}
artist_name = determine_attribute (song_list, TRUE);
if (artist_name != NULL) {
temp_str = g_strdup_printf ("%s by %s", album_name, artist_name);
g_free (artist_name);
} else {
temp_str = g_strdup (album_name);
}
gtk_label_set (GTK_LABEL (music_view->details->album_title), temp_str);
g_free (temp_str);
g_free (album_name);
}
/* allocate the play controls if necessary */
if (music_view->details->play_control_box == NULL)
add_play_controls(music_view);
music_view_set_selected_song_title(music_view, 0);
/* release the song list */
for (p = song_list; p != NULL; p = p->next) {
info = (SongInfo *) p->data;
release_song_info(info);
}
g_list_free (song_list);
}
void
nautilus_music_view_load_uri (NautilusMusicView *music_view, const char *uri)
{
g_free (music_view->details->uri);
music_view->details->uri = g_strdup (uri);
nautilus_music_view_update_from_uri (music_view, uri);
}
static void
music_view_notify_location_change_callback (NautilusContentViewFrame *view,
Nautilus_NavigationInfo *navinfo,
NautilusMusicView *music_view)
{
Nautilus_ProgressRequestInfo progress;
memset(&progress, 0, sizeof(progress));
/* send required PROGRESS_UNDERWAY signal */
progress.type = Nautilus_PROGRESS_UNDERWAY;
progress.amount = 0.0;
nautilus_view_frame_request_progress_change (NAUTILUS_VIEW_FRAME (music_view->details->view_frame), &progress);
/* do the actual work here */
nautilus_music_view_load_uri (music_view, navinfo->actual_uri);
/* send the required PROGRESS_DONE signal */
progress.type = Nautilus_PROGRESS_DONE_OK;
progress.amount = 100.0;
nautilus_view_frame_request_progress_change (NAUTILUS_VIEW_FRAME (music_view->details->view_frame), &progress);
}
/* handle receiving dropped objects */
static void
nautilus_music_view_drag_data_received (GtkWidget *widget, GdkDragContext *context,
int x, int y,
GtkSelectionData *selection_data, guint info, guint time)
{
char **uris;
g_return_if_fail (NAUTILUS_IS_MUSIC_VIEW (widget));
uris = g_strsplit (selection_data->data, "\r\n", 0);
switch (info) {
case TARGET_GNOME_URI_LIST:
case TARGET_URI_LIST:
g_message ("dropped data on music_view: %s", selection_data->data);
break;
case TARGET_COLOR:
/* Let the background change based on the dropped color. */
nautilus_background_receive_dropped_color (nautilus_get_widget_background (widget),
widget, x, y, selection_data);
break;
case TARGET_BGIMAGE:
nautilus_background_set_tile_image_uri
(nautilus_get_widget_background (widget),
uris[0]);
break;
default:
g_warning ("unknown drop type");
break;
}
g_strfreev (uris);
}
|