~ubuntu-branches/ubuntu/hardy/gnomad2/hardy

« back to all changes in this revision

Viewing changes to src/jukebox.c

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2005-08-19 16:09:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050819160928-l2glu227nh0algdc
Tags: 2.8.0-2
Add a versioned dependency for libnjb-dev (>> 2.2). Closes: #324036.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "filesystem.h"
26
26
#include "filenaming.h"
27
27
#include "util.h"
 
28
#if !GTK_CHECK_VERSION(2,4,0)
 
29
#include <libgnomeui/libgnomeui.h>
 
30
#endif
28
31
 
29
 
/* External global used by libnjb */
30
 
extern int njb_error;
 
32
extern GtkWidget *main_window;
31
33
 
32
34
/* Local variables */
33
35
static njb_t njbs[NJB_MAX_DEVICES];
34
36
static njb_t *njb = NULL;
35
 
static njbid_t *njbid = NULL;
36
37
static gboolean njb_connected = FALSE;
37
38
/* Jukebox properties variables */
38
39
static gchar *jukebox_ownerstring = NULL;
39
40
static gchar *jukebox_timestring = NULL;
40
41
static gchar *jukebox_firmware = NULL;
 
42
static gchar *jukebox_hardware = NULL;
41
43
static u_int64_t jukebox_totalbytes = 0;
42
44
static u_int64_t jukebox_freebytes = 0;
43
45
static u_int64_t jukebox_usedbytes = 0;
71
73
 *                     +-> track -> track -> ... -> NULL
72
74
 */
73
75
static GSList *playlistlist = NULL;
 
76
/* 
 
77
 * This is a file list with folder information, filtered
 
78
 * to present a folder view
 
79
 */
 
80
static GSList *datafilelist = NULL;
74
81
 
75
82
 
76
83
/***********************************************************************************/
77
84
/* Public and private functions                                                    */
78
85
/***********************************************************************************/
79
86
 
 
87
static void display_njberror(njb_t *njb, gchar *template) 
 
88
{
 
89
  gboolean found_njb_error = FALSE;
 
90
  const gchar *sp;
 
91
  GString *error_string = g_string_new(template);
 
92
 
 
93
  if (njb != NULL) {  
 
94
    NJB_Error_Reset_Geterror(njb);
 
95
    while ((sp = NJB_Error_Geterror(njb)) != NULL) {
 
96
      found_njb_error = TRUE;
 
97
      g_string_append_printf(error_string, "%s\n", sp);
 
98
    }
 
99
  } else {
 
100
    found_njb_error = TRUE;
 
101
    g_string_append_printf(error_string, _("Could not open the device on the USB bus"));
 
102
  }
 
103
  
 
104
  if (!found_njb_error)
 
105
    g_string_append_printf(error_string, _("(unknown cause)"));
 
106
 
 
107
  create_error_dialog(error_string->str);
 
108
  g_string_free(error_string, TRUE);
 
109
}
 
110
 
80
111
static void refresh_id(void)
81
112
{
82
113
  if (njb != NULL) {
83
 
    if (njbid != NULL) {
84
 
      g_free(njbid);
85
 
      njbid = NULL;
86
 
    }
87
 
    njbid = NJB_Ping(njb);
 
114
    NJB_Ping(njb);
88
115
  }
89
116
}
90
117
 
91
118
/* Returns the unique jukebox ID */
92
 
gchar *jukebox_get_idstring(void)
 
119
static gchar *jukebox_get_idstring(void)
93
120
{
94
121
  if (njb != NULL) {
95
 
    return (gchar *) njb->idstring;
 
122
    u_int8_t sdmiid[16];
 
123
    gchar idstring[32];
 
124
 
 
125
    if (NJB_Get_SDMI_ID(njb, (u_int8_t *) &sdmiid) == 0) {
 
126
      return g_strdup_printf("%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
 
127
                             sdmiid[0], sdmiid[1], sdmiid[2], sdmiid[3], sdmiid[4], 
 
128
                             sdmiid[5], sdmiid[6], sdmiid[7], sdmiid[8], sdmiid[9], 
 
129
                             sdmiid[10], sdmiid[11], sdmiid[12], sdmiid[13], sdmiid[14], 
 
130
                             sdmiid[15]);
 
131
    } else {
 
132
      return NULL;
 
133
    }
96
134
  } else {
97
135
    return NULL;
98
136
  }
99
137
}
100
138
 
101
139
/* Returns a string representing the firmware revision */
102
 
gchar *jukebox_get_firmware(void)
 
140
static gchar *jukebox_get_firmware(void)
103
141
{
104
 
  if (jukebox_firmware != NULL) {
105
 
    g_free(jukebox_firmware);
106
 
    jukebox_firmware = NULL;
107
 
  }
108
 
  refresh_id();
109
 
  if (njbid != NULL) {
110
 
    /* if (njb->device_type == NJB_DEVICE_NJB1) */
111
 
    jukebox_firmware = 
112
 
      g_strdup_printf("%u.%u", njbid->fwMajor, njbid->fwMinor);
113
 
    /* else g_strdup_printf("%u.%u.%u", njbid->fwMajor, njbid->fwMinor, njbid->fwRel); */
 
142
  if (njb != NULL) {
 
143
    if (jukebox_firmware == NULL) {
 
144
      u_int8_t major, minor, release;
 
145
 
 
146
      if (NJB_Get_Firmware_Revision(njb, &major, &minor, &release) == -1) {
 
147
        return NULL;
 
148
      }
 
149
      if (njb->device_type == NJB_DEVICE_NJB1) {
 
150
        jukebox_firmware = g_strdup_printf("%u.%u", major, minor);
 
151
      } else {
 
152
        jukebox_firmware = g_strdup_printf("%u.%u.%u", major, minor, release);
 
153
      }
 
154
    }
114
155
    return jukebox_firmware;
115
156
  } else {
116
157
    return NULL;
117
158
  }
118
159
}
119
160
 
 
161
/* Returns a string representing the hardware revision */
 
162
static gchar *jukebox_get_hardware(void)
 
163
{
 
164
  if (njb != NULL) {
 
165
    if (jukebox_hardware == NULL) {
 
166
      u_int8_t major, minor, release;
 
167
 
 
168
      if (NJB_Get_Hardware_Revision(njb, &major, &minor, &release) == -1) {
 
169
        return NULL;
 
170
      }
 
171
      jukebox_hardware = g_strdup_printf("%u.%u.%u", major, minor, release);
 
172
    }
 
173
    return jukebox_hardware;
 
174
  } else {
 
175
    return NULL;
 
176
  }
 
177
}
 
178
 
120
179
/* Returns the product name */
121
 
gchar *jukebox_get_prodname(void)
122
 
{
123
 
  refresh_id();
124
 
  if (njbid != NULL) {
125
 
    return (gchar *) njbid->productName;
126
 
  } else {
127
 
    return NULL;
128
 
  }
129
 
}
130
 
 
131
 
/* Returns data about if the power cord is connected */
132
 
gboolean jukebox_get_power(void)
133
 
{
134
 
  refresh_id();
135
 
  if (njbid != NULL) {
136
 
    return (njbid->power > 0) ? TRUE : FALSE;
137
 
  } else {
138
 
    return FALSE;
139
 
  }
 
180
static gchar *jukebox_get_prodname(void)
 
181
{
 
182
  if (njb != NULL) {
 
183
    return (gchar *) NJB_Get_Device_Name(njb, 1);
 
184
  } else {
 
185
    return NULL;
 
186
  }
 
187
}
 
188
 
 
189
/* Returns data about if the power cord is connected */
 
190
static gboolean jukebox_get_power(void)
 
191
{
 
192
  if (njb != NULL) {
 
193
    int powerstat = NJB_Get_Auxpower(njb);
 
194
    if (powerstat == 1) {
 
195
      return TRUE;
 
196
    } else {
 
197
      return FALSE;
 
198
    }
 
199
  } else {
 
200
    return FALSE;
 
201
  }
 
202
}
 
203
 
 
204
/* Returns data about if the power cord is connected */
 
205
static gboolean jukebox_get_charging(void)
 
206
{
 
207
  if (njb != NULL) {
 
208
    int chargestat = NJB_Get_Battery_Charging(njb);
 
209
    if (chargestat == 1) {
 
210
      return TRUE;
 
211
    } else {
 
212
      return FALSE;
 
213
    }
 
214
  } else {
 
215
    return FALSE;
 
216
  }
 
217
}
 
218
 
 
219
/* Returns the battery level in percent */
 
220
static guint jukebox_get_battery_level(void)
 
221
{
 
222
  if (njb != NULL) {
 
223
    return NJB_Get_Battery_Level(njb);
 
224
  } else {
 
225
    return 0;
 
226
  }
 
227
}
 
228
 
 
229
static void add_to_dialog(GtkWidget *dialog, GtkWidget *thing)
 
230
{
 
231
#if GTK_CHECK_VERSION(2,4,0)
 
232
  gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), thing, TRUE, TRUE, 0);
 
233
#else
 
234
  gtk_box_pack_start (GTK_BOX(GNOME_DIALOG(dialog)->vbox), thing, TRUE, TRUE, 0);
 
235
#endif
 
236
}
 
237
 
 
238
/* This creates a device information dialog and returns it. */
 
239
GtkWidget *jukebox_get_deviceinfo_dialog(void)
 
240
{
 
241
  GtkWidget *label, *button, *dialog, *hbox;
 
242
  gchar tmp[50];
 
243
#ifdef G_HAVE_GINT64
 
244
  guint64 total, free, used;
 
245
  guint songs, playlists, datafiles;
 
246
#endif
 
247
 
 
248
  if (njb == NULL) {
 
249
    return NULL;
 
250
  }
 
251
  
 
252
  // Make sure our information is up-to-date
 
253
  refresh_id();
 
254
 
 
255
#if GTK_CHECK_VERSION(2,4,0)
 
256
  dialog = gtk_message_dialog_new (GTK_WINDOW(main_window),
 
257
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
258
                                   GTK_MESSAGE_INFO,
 
259
                                   GTK_BUTTONS_CLOSE,
 
260
                                   _("Jukebox information"));
 
261
  // dialog = gtk_dialog_new();
 
262
  gtk_window_set_title (GTK_WINDOW (dialog), (_("Jukebox information")));
 
263
  gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 5);
 
264
  gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dialog)->action_area), TRUE);
 
265
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
 
266
  g_signal_connect_object(GTK_OBJECT(dialog),
 
267
                          "delete_event",
 
268
                          G_CALLBACK(gtk_widget_destroy),
 
269
                          NULL,
 
270
                          0);
 
271
  g_signal_connect_object(GTK_OBJECT(dialog), 
 
272
                          "response",
 
273
                          G_CALLBACK(gtk_widget_destroy), 
 
274
                          NULL,
 
275
                          0);
 
276
#else
 
277
  dialog = gnome_dialog_new(_("Jukebox information"),
 
278
                            GNOME_STOCK_BUTTON_OK,
 
279
                            NULL);
 
280
  gnome_dialog_button_connect_object(GNOME_DIALOG(dialog),
 
281
                                     0,
 
282
                                     GTK_SIGNAL_FUNC(gtk_widget_destroy),
 
283
                                     GTK_OBJECT(dialog));
 
284
#endif
 
285
  hbox = gtk_hbox_new(FALSE, 0);
 
286
  label = gtk_label_new (_("Unique device ID:"));
 
287
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
288
  gtk_widget_show (label);
 
289
  add_empty_hbox (hbox);
 
290
  label = gtk_label_new(jukebox_get_idstring());
 
291
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
292
  gtk_widget_show(label);
 
293
  add_to_dialog(dialog, hbox);
 
294
  gtk_widget_show (hbox);
 
295
 
 
296
  hbox = gtk_hbox_new(FALSE, 0);
 
297
  label = gtk_label_new (_("Firmware revision:"));
 
298
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
299
  gtk_widget_show (label);
 
300
  add_empty_hbox (hbox);
 
301
  label = gtk_label_new(jukebox_get_firmware());
 
302
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
303
  gtk_widget_show(label);
 
304
  add_to_dialog(dialog, hbox);
 
305
  gtk_widget_show (hbox);
 
306
 
 
307
  if (njb->device_type != NJB_DEVICE_NJB1) {
 
308
    hbox = gtk_hbox_new(FALSE, 0);
 
309
    label = gtk_label_new (_("Hardware revision:"));
 
310
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
311
    gtk_widget_show (label);
 
312
    add_empty_hbox (hbox);
 
313
    label = gtk_label_new(jukebox_get_hardware());
 
314
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
315
    gtk_widget_show(label);
 
316
    add_to_dialog(dialog, hbox);
 
317
    gtk_widget_show (hbox);
 
318
  }
 
319
 
 
320
  hbox = gtk_hbox_new(FALSE, 0);
 
321
  label = gtk_label_new (_("Product name:"));
 
322
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
323
  gtk_widget_show (label);
 
324
  add_empty_hbox (hbox);
 
325
  label = gtk_label_new(jukebox_get_prodname());
 
326
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
327
  gtk_widget_show(label);
 
328
  add_to_dialog(dialog, hbox);
 
329
  gtk_widget_show (hbox);
 
330
 
 
331
  hbox = gtk_hbox_new(FALSE, 0);
 
332
  label = gtk_label_new (_("Auxilary power (AC or USB) connected:"));
 
333
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
334
  gtk_widget_show (label);
 
335
  add_empty_hbox (hbox);
 
336
  label = gtk_label_new((jukebox_get_power()) ? _("Yes") : _("No"));
 
337
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
338
  gtk_widget_show(label);
 
339
  add_to_dialog(dialog, hbox);
 
340
  gtk_widget_show (hbox);
 
341
 
 
342
  hbox = gtk_hbox_new(FALSE, 0);
 
343
  label = gtk_label_new (_("Battery is charging:"));
 
344
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
345
  gtk_widget_show (label);
 
346
  add_empty_hbox (hbox);
 
347
  label = gtk_label_new((jukebox_get_charging()) ? _("Yes") : _("No"));
 
348
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
349
  gtk_widget_show(label);
 
350
  add_to_dialog(dialog, hbox);
 
351
  gtk_widget_show (hbox);
 
352
  
 
353
  if (njb->device_type != NJB_DEVICE_NJB1) {
 
354
    hbox = gtk_hbox_new(FALSE, 0);
 
355
    label = gtk_label_new (_("Battery level:"));
 
356
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
357
    gtk_widget_show (label);
 
358
    add_empty_hbox (hbox);
 
359
    sprintf(tmp, "%d%%", jukebox_get_battery_level());
 
360
    label = gtk_label_new(tmp);
 
361
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
362
    gtk_widget_show(label);
 
363
    add_to_dialog(dialog, hbox);
 
364
    gtk_widget_show (hbox);
 
365
  }
 
366
 
 
367
  hbox = gtk_hbox_new(FALSE, 0);
 
368
  label = gtk_label_new (_("Jukebox owner:"));
 
369
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
370
  gtk_widget_show (label);
 
371
  add_empty_hbox (hbox);
 
372
  sprintf(tmp, "%s", jukebox_get_ownerstring());
 
373
  label = gtk_label_new(tmp);
 
374
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
375
  gtk_widget_show(label);
 
376
  add_to_dialog(dialog, hbox);
 
377
  gtk_widget_show (hbox);
 
378
 
 
379
  hbox = gtk_hbox_new(FALSE, 0);
 
380
  label = gtk_label_new (_("Time on jukebox:"));
 
381
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
382
  gtk_widget_show (label);
 
383
  add_empty_hbox (hbox);
 
384
  label = gtk_label_new(jukebox_get_time());
 
385
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
386
  gtk_widget_show(label);
 
387
  add_to_dialog(dialog, hbox);
 
388
  gtk_widget_show (hbox);
 
389
 
 
390
#ifdef G_HAVE_GINT64
 
391
  jukebox_getusage(&total, &free, &used, &songs, &playlists, &datafiles);
 
392
 
 
393
  hbox = gtk_hbox_new(FALSE, 0);
 
394
  label = gtk_label_new (_("Total bytes on disk:"));
 
395
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
396
  gtk_widget_show (label);
 
397
  add_empty_hbox (hbox);
 
398
  sprintf(tmp, "%Lu (%Lu MB)", 
 
399
          total, (guint64) total/G_GINT64_CONSTANT(1048576));
 
400
  label = gtk_label_new(tmp);
 
401
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
402
  gtk_widget_show(label);
 
403
  add_to_dialog(dialog, hbox);
 
404
  gtk_widget_show (hbox);
 
405
 
 
406
  hbox = gtk_hbox_new(FALSE, 0);
 
407
  label = gtk_label_new (_("Total free bytes available:"));
 
408
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
409
  gtk_widget_show (label);
 
410
  add_empty_hbox (hbox);
 
411
  sprintf(tmp, "%Lu (%Lu MB)", 
 
412
          free, (guint64) free/G_GINT64_CONSTANT(1048576));
 
413
  label = gtk_label_new(tmp);
 
414
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
415
  gtk_widget_show(label);
 
416
  add_to_dialog(dialog, hbox);
 
417
  gtk_widget_show (hbox);
 
418
 
 
419
  hbox = gtk_hbox_new(FALSE, 0);
 
420
  label = gtk_label_new (_("Total bytes used:"));
 
421
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
422
  gtk_widget_show (label);
 
423
  add_empty_hbox (hbox);
 
424
  sprintf(tmp, "%Lu (%Lu MB)", 
 
425
          used, (guint64) used/G_GINT64_CONSTANT(1048576));
 
426
  label = gtk_label_new(tmp);
 
427
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
428
  gtk_widget_show(label);
 
429
  add_to_dialog(dialog, hbox);
 
430
  gtk_widget_show (hbox);
 
431
 
 
432
  hbox = gtk_hbox_new(FALSE, 0);
 
433
  label = gtk_label_new (_("Number of songs:"));
 
434
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
435
  gtk_widget_show (label);
 
436
  add_empty_hbox (hbox);
 
437
  sprintf(tmp, "%lu", songs);
 
438
  label = gtk_label_new(tmp);
 
439
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
440
  gtk_widget_show(label);
 
441
  add_to_dialog(dialog, hbox);
 
442
  gtk_widget_show (hbox);
 
443
 
 
444
  hbox = gtk_hbox_new(FALSE, 0);
 
445
  label = gtk_label_new (_("Number of playlists:"));
 
446
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
447
  gtk_widget_show (label);
 
448
  add_empty_hbox (hbox);
 
449
  sprintf(tmp, "%lu", playlists);
 
450
  label = gtk_label_new(tmp);
 
451
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
452
  gtk_widget_show(label);
 
453
  add_to_dialog(dialog, hbox);
 
454
  gtk_widget_show (hbox);
 
455
 
 
456
  hbox = gtk_hbox_new(FALSE, 0);
 
457
  label = gtk_label_new (_("Number of datafiles:"));
 
458
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
459
  gtk_widget_show (label);
 
460
  add_empty_hbox (hbox);
 
461
  sprintf(tmp, "%lu", datafiles);
 
462
  label = gtk_label_new(tmp);
 
463
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
464
  gtk_widget_show(label);
 
465
  add_to_dialog(dialog, hbox);
 
466
  gtk_widget_show (hbox);
 
467
#endif
 
468
  return dialog;
140
469
}
141
470
 
142
471
gchar *jukebox_get_time(void)
254
583
 
255
584
  if ( NJB_Discover(njbs, 0, &jukeboxcount) == -1 ) {
256
585
    create_error_dialog(_("Could not try to locate jukeboxes\nUSB error?"));
257
 
    njb_error_dump(stderr);
258
586
    return NULL;
259
587
  }
260
588
  
302
630
        case NJB_DEVICE_NJBZENTOUCH:
303
631
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen Touch");
304
632
          break;
 
633
        case NJB_DEVICE_NJBZENMICRO:
 
634
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen Micro");
 
635
          break;
 
636
        case NJB_DEVICE_DELLDJ2:
 
637
          retarray[i] = g_strdup_printf("Second Generation Dell DJ");
 
638
          break;
 
639
        case NJB_DEVICE_POCKETDJ:
 
640
          retarray[i] = g_strdup_printf("Dell Pocket DJ");
 
641
          break;
305
642
        default:
306
643
          retarray[i] = g_strdup_printf(_("Unknown jukebox type"));
 
644
          break;
307
645
        }
308
646
    }
309
647
    retarray[jukeboxcount] = NULL;
320
658
  njb = &njbs[i];
321
659
  
322
660
  if (NJB_Open(njb) == -1) {
323
 
    create_error_dialog(_("Could not open jukebox"));
324
 
    njb_error_dump(stderr);
 
661
    display_njberror(njb, _("Could not open jukebox:\n"));
325
662
    return FALSE;
326
663
  }
327
664
 
329
666
   * Causing problems. Removing for now. 2004-04-12
330
667
  if ((njbid = NJB_Ping(njb)) == NULL) {
331
668
    create_error_dialog(_("Could not ping jukebox"));
332
 
    njb_error_dump(stderr);
 
669
    NJB_Error_Dump(stderr);
333
670
    NJB_Close(njb);
334
671
    return FALSE;
335
672
  }
336
673
  */
337
674
 
338
675
  if (NJB_Capture(njb) == -1) {
339
 
    create_error_dialog(_("Could not capture jukebox"));
340
 
    njb_error_dump(stderr);
 
676
    display_njberror(njb, _("Could not capture jukebox:\n"));
341
677
    NJB_Close(njb);
342
678
    return FALSE;
343
679
  }
363
699
void jukebox_release(void)
364
700
{
365
701
  if (njb_connected) {
366
 
    njbid = NJB_Ping(njb);
 
702
    NJB_Ping(njb);
367
703
    NJB_Release(njb);
368
704
    NJB_Close(njb);
369
705
    njb_connected = FALSE;
374
710
 * drop down is then built from */
375
711
static void build_playlist_list()
376
712
{
377
 
  playlist_t *playlist;
 
713
  njb_playlist_t *playlist;
378
714
 
379
715
  if (jukebox_playlist != NULL) {
380
716
    GList *tmplist = jukebox_playlist;
394
730
    jukebox_playlist = g_list_append(jukebox_playlist, g_strdup(playlist->name));
395
731
    jukebox_playlist = g_list_append(jukebox_playlist, GUINT_TO_POINTER(playlist->plid));
396
732
    // Dangerous?
397
 
    playlist_destroy(playlist);
 
733
    NJB_Playlist_Destroy(playlist);
398
734
  }
399
735
}
400
736
 
401
737
 
402
738
static void create_tree_structure()
403
739
{
404
 
  playlist_t *playlist;
 
740
  njb_playlist_t *playlist;
405
741
 
406
742
  /* This is for the actual playlist tree scan */
407
743
  NJB_Reset_Get_Playlist(njb);
408
744
  while (playlist = NJB_Get_Playlist(njb)) {
409
 
    playlist_track_t *track;
 
745
    njb_playlist_track_t *track;
410
746
    gnomadplaylist_entry_t *entry;
411
747
    gint i,j;
412
748
    gchar *tmp;
418
754
    entry->name = g_strdup(playlist->name);
419
755
    tmp = g_strdup_printf("%lu", playlist->plid);
420
756
    entry->plid = tmp;
421
 
    playlist_reset_gettrack(playlist);
422
 
    while (track = playlist_gettrack(playlist)) {
 
757
    NJB_Playlist_Reset_Gettrack(playlist);
 
758
    while (track = NJB_Playlist_Gettrack(playlist)) {
423
759
      entry->tracklist = g_slist_append(entry->tracklist,
424
760
                                        GUINT_TO_POINTER(track->trackid));
425
761
    }
426
 
    playlist_destroy(playlist);
 
762
    NJB_Playlist_Destroy(playlist);
427
763
    playlistlist = g_slist_append(playlistlist, (gpointer) entry);
428
764
    if (cancel_jukebox_operation)
429
765
      break;
430
766
  }
431
 
  if ( njb_error != EO_EOM ) njb_error_dump(stderr);
 
767
  if ( NJB_Error_Pending(njb) ) {
 
768
    NJB_Error_Dump(njb,stderr);
 
769
  }
432
770
}
433
771
 
434
772
 
529
867
static void add_to_playlist(guint playlistid, GList *metalist, 
530
868
                            GtkTreeStore *pltreestore, gboolean threaded)
531
869
{
532
 
  playlist_track_t *pl_track;
533
 
  playlist_t *playlist;
 
870
  njb_playlist_track_t *pl_track;
 
871
  njb_playlist_t *playlist;
534
872
  GList *tmplist = metalist;
535
873
  gboolean found = FALSE;
536
874
 
545
883
      break;
546
884
    }
547
885
    /* g_print("NO.\n"); */
548
 
    playlist_destroy(playlist);
 
886
    NJB_Playlist_Destroy(playlist);
549
887
  }
550
888
  if (!found) {
551
889
    if (threaded)
561
899
      meta = (metadata_t *) tmplist->data;
562
900
      id = string_to_guint(meta->path);
563
901
      /* g_print("Adding track %lu to playlist %lu\n", id, playlistid); */
564
 
      pl_track = playlist_track_new(id);
565
 
      playlist_addtrack(playlist, pl_track, NJB_PL_END);
 
902
      pl_track = NJB_Playlist_Track_New(id);
 
903
      NJB_Playlist_Addtrack(playlist, pl_track, NJB_PL_END);
566
904
      tmplist = g_list_next(tmplist);
567
905
    }
568
906
    if (NJB_Update_Playlist(njb, playlist) == -1)
569
 
      njb_error_dump(stderr);
570
 
    playlist_destroy(playlist);
 
907
      NJB_Error_Dump(njb, stderr);
 
908
    NJB_Playlist_Destroy(playlist);
571
909
  }
572
910
  /* Rebuild the tree afterwards */
573
911
  build_playlist_tree(pltreestore, threaded);
576
914
/* Update the usage information */
577
915
static void flush_usage()
578
916
{
579
 
  u_int64_t totalbytes;
580
 
  u_int64_t freebytes;
 
917
  u_int64_t totalbytes = 0;
 
918
  u_int64_t freebytes = 0;
581
919
  
582
 
  if (NJB_Get_Disk_Usage (njb, &totalbytes, &freebytes) == -1)
583
 
    njb_error_dump(stderr);
 
920
  if (NJB_Get_Disk_Usage (njb, &totalbytes, &freebytes) == -1) {
 
921
    NJB_Error_Dump(njb, stderr);
 
922
    return;
 
923
  }
584
924
 
585
925
  jukebox_totalbytes = totalbytes;
586
926
  jukebox_freebytes = freebytes;
587
927
  jukebox_usedbytes = jukebox_totalbytes - jukebox_freebytes;
588
928
}
589
929
 
590
 
/***********************************************************************************/
591
 
/* Scanning thread - reads in the Jukebox track directory, the datafile directory  */
592
 
/* and all playlists, along with some general information about the jukebox.       */
593
 
/***********************************************************************************/
594
 
 
 
930
static void destroy_datafile_list()
 
931
{
 
932
  GSList *tmplist = datafilelist;
 
933
  metadata_t *meta;
 
934
  while (tmplist != NULL) {
 
935
    meta = (metadata_t *) tmplist->data;
 
936
    destroy_metadata_t(meta);
 
937
    tmplist = g_slist_next(tmplist);
 
938
  }
 
939
  g_slist_free(datafilelist);
 
940
  datafilelist = NULL;
 
941
}
 
942
 
 
943
static void remove_from_datafile_list(gchar *path)
 
944
{
 
945
  GSList *tmplist1 = datafilelist;
 
946
  GSList *tmplist2 = NULL;
 
947
  metadata_t *meta;
 
948
 
 
949
  while (tmplist1 != NULL) {
 
950
    meta = (metadata_t *) tmplist1->data;
 
951
    if (!strcmp(path, meta->path)) {
 
952
      // Found it...
 
953
      destroy_metadata_t(meta);
 
954
    } else {
 
955
      // Keep it...
 
956
      tmplist2 = g_slist_append(tmplist2, meta);
 
957
    }
 
958
    tmplist1 = g_slist_next(tmplist1);
 
959
  }
 
960
  g_slist_free(datafilelist);
 
961
  datafilelist = tmplist2;
 
962
}
 
963
 
 
964
static GSList *filter_datafile_list(gchar *filter)
 
965
{
 
966
  GSList *tmplist = datafilelist;
 
967
  GSList *filterlist = NULL;
 
968
  metadata_t *meta;
 
969
  gchar **tmp;
 
970
  int veclen;
 
971
  gchar *foldername;
 
972
  gchar *basename;
 
973
  
 
974
  if (filter == NULL) {
 
975
    return NULL;
 
976
  }
 
977
  /* Create a ".." directory */
 
978
  meta = new_metadata_t();
 
979
  meta->filename = g_strdup("..");
 
980
  meta->size = 0;
 
981
  tmp = g_strsplit(filter, "\\", 0);
 
982
  veclen = vectorlength(tmp);
 
983
  if (veclen > 2) {
 
984
    int i;
 
985
    gchar *newdir = NULL;
 
986
 
 
987
    for (i = 0; i < veclen - 2; i++) {
 
988
      newdir = stringcat(newdir, tmp[i]);
 
989
      newdir = stringcat(newdir, "\\");
 
990
    }
 
991
    meta->folder = newdir;
 
992
  } else {
 
993
    meta->folder = g_strdup(filter);
 
994
  }
 
995
  g_strfreev(tmp);
 
996
 
 
997
  filterlist = g_slist_append(filterlist, meta);
 
998
  /* Then filter the list */
 
999
  while (tmplist != NULL) {
 
1000
    meta = (metadata_t *) tmplist->data;
 
1001
    if (meta->folder != NULL) {
 
1002
      if (!strcmp(meta->folder, filter)
 
1003
          && meta->size != 0) {
 
1004
        /* First just add all regular files belonging to this directory */
 
1005
        filterlist = g_slist_append(filterlist, meta);
 
1006
      } else if (meta->size == 0) {
 
1007
        /* Then add any subdirectories */
 
1008
        gchar **tmp1 = g_strsplit(meta->folder, "\\", 0);
 
1009
        gchar **tmp2 = g_strsplit(filter, "\\", 0);
 
1010
        int veclen1 = vectorlength(tmp1);
 
1011
        int veclen2 = vectorlength(tmp2);
 
1012
        int i;
 
1013
 
 
1014
        /* It must be exactly one more component in the folder than in the filter */
 
1015
        if (veclen1 == veclen2+1) {
 
1016
          gboolean match = TRUE;
 
1017
 
 
1018
          /* We don't match the last component of the filter, it's NULL, see. */
 
1019
          for (i = 0; i < veclen2-1; i++) {
 
1020
            if (tmp1[i] != NULL && tmp2[i] != NULL && strcmp(tmp1[i],tmp2[i])) {
 
1021
              match = FALSE;
 
1022
            }
 
1023
          }
 
1024
          if (match) {
 
1025
            /* If it's matching up to the filter length, add it */
 
1026
            filterlist = g_slist_append(filterlist, meta);
 
1027
          }
 
1028
        }
 
1029
        g_strfreev(tmp1);
 
1030
        g_strfreev(tmp2);
 
1031
      }
 
1032
    } else if (!strcmp(filter, "\\")) {
 
1033
      filterlist = g_slist_append(filterlist, meta);
 
1034
    }
 
1035
    tmplist = g_slist_next(tmplist);
 
1036
  }
 
1037
  return filterlist;
 
1038
}
 
1039
 
 
1040
void rebuild_datafile_list(gchar *filter) {
 
1041
  /*
 
1042
   * Add everything to the model by filtering out the root
 
1043
   * directory files only from the datafile list.
 
1044
   */
 
1045
  if (datafilelist != NULL) {
 
1046
    GSList *filtered;
 
1047
 
 
1048
    /* Hack to get the datafile list store built in O(n) instead of O(n^2) */
 
1049
    recreate_list_store(JBDATA_LIST);
 
1050
    filtered = filter_datafile_list(filter);
 
1051
    if (filtered != NULL) {
 
1052
      GSList *tmplist = filtered;
 
1053
      metadata_t *meta;
 
1054
      
 
1055
      while (tmplist != NULL) {
 
1056
        meta = (metadata_t *) tmplist->data;
 
1057
        add_metadata_to_model(meta, JBDATA_LIST);
 
1058
        tmplist = g_slist_next(tmplist);
 
1059
      }
 
1060
      g_slist_free(tmplist);
 
1061
    }
 
1062
    view_and_sort_list_store(JBDATA_LIST);
 
1063
  }
 
1064
}
 
1065
 
 
1066
/**
 
1067
 * Scanning thread - reads in the Jukebox track directory, the datafile directory
 
1068
 * and all playlists, along with some general information about the jukebox.
 
1069
 */
595
1070
gpointer scan_thread(gpointer thread_args)
596
1071
{
597
 
  songid_t *songtag;
598
 
  datafile_t *datatag;
 
1072
  njb_songid_t *songtag;
 
1073
  njb_datafile_t *datatag;
599
1074
  gchar *scan_songs = _("Scanning songs...");
 
1075
  gchar *slow_warning = _("Scanning songs:\nYou have selected extended metadata scan,\n so scanning will be very slow.");
600
1076
  gchar *scan_playlists = _("Scanning playlists...");
601
1077
  gchar *scan_datafiles = _("Scanning datafiles...");
602
1078
  scan_thread_arg_t *args = (scan_thread_arg_t *) thread_args;
609
1085
 
610
1086
  /* Retrieve general information about the jukebox */
611
1087
  gdk_threads_enter();
612
 
  gtk_label_set_text(GTK_LABEL(args->label), scan_songs);
 
1088
  if (get_prefs_extended_metadata()) {
 
1089
    gtk_label_set_text(GTK_LABEL(args->label), slow_warning);
 
1090
  } else {
 
1091
    gtk_label_set_text(GTK_LABEL(args->label), scan_songs);
 
1092
  }
613
1093
  gdk_threads_leave();
614
1094
  /* If it has already been read, free the old string */
615
1095
  if (jukebox_ownerstring != NULL)
630
1110
  /* FIXME/IMPROVEMENT: If jukebox_songs != 0 start displaying progress bar? */
631
1111
 
632
1112
  while (songtag = NJB_Get_Track_Tag(njb)) {
633
 
    gchar *label;
634
 
    gchar *data;
635
 
    guint numdata;
636
1113
    gint i;
637
1114
    metadata_t *meta;
638
 
    songid_frame_t *frame;
 
1115
    njb_songid_frame_t *frame;
639
1116
    gchar *scanstring;
640
1117
 
641
1118
    jukebox_songs++;
646
1123
    gdk_threads_leave();
647
1124
    g_free(scanstring);
648
1125
 
649
 
    songid_reset_getframe(songtag);
 
1126
    NJB_Songid_Reset_Getframe(songtag);
650
1127
    /* Create a structure to hold the data in the columns */
651
1128
    meta = new_metadata_t();
652
1129
    meta->path = g_strdup_printf("%lu", songtag->trid);
653
1130
 
654
1131
    /* Loop through the song tags */
655
 
    while(frame = songid_getframe(songtag)){
 
1132
    while(frame = NJB_Songid_Getframe(songtag)){
656
1133
      // FIXME: add progress bar for scanning?
657
 
      label = (gchar *) g_malloc (frame->labelsz + 1);
658
 
      memcpy(label, frame->label, frame->labelsz);
659
 
      /* Terminate string */
660
 
      label[frame->labelsz] = '\0';
661
 
      
662
 
      if (frame->type == ID_DATA_BIN) {
663
 
        /* If it is not ASCII data */
664
 
        if (!strcmp(label, FR_LENGTH)) {
665
 
          data = seconds_to_mmss(songid_frame_data32(frame));
666
 
        }
667
 
        else {
668
 
          if (frame->datasz > 4) {
669
 
            /* 
670
 
             * There is no way to handle this right now, though 
671
 
             * it could easily be added to the libnjb if needed
672
 
             */
673
 
            g_print("Error: Found 64 bit data in songid tag frame. Ignoring.\n");
674
 
          } else
675
 
            numdata = songid_frame_data32(frame);
676
 
        }
677
 
      } else if (frame->type == ID_DATA_ASCII) {
678
 
        /* Strings that are actually integers are converted */
679
 
        if (!strcmp(label, FR_YEAR) ||
680
 
            !strcmp(label, FR_TRACK)) {
681
 
          numdata = string_to_guint(frame->data);
682
 
        } else {
683
 
          /* If it is ASCII data */
684
 
          if (frame->datasz) {
685
 
            data = g_strdup(frame->data);
686
 
          } else
687
 
            data = g_strdup("<Unknown>");
688
 
        }
689
 
      }
690
1134
 
691
 
      if (!strcmp(label, FR_ARTIST))
692
 
        meta->artist = data;
693
 
      else if (!strcmp(label, FR_TITLE))
694
 
        meta->title = data;
695
 
      else if (!strcmp(label, FR_ALBUM))
696
 
        meta->album = data;
697
 
      else if (!strcmp(label, FR_GENRE))
698
 
        meta->genre = data;
699
 
      else if (!strcmp(label, FR_LENGTH))
700
 
        meta->length = data;
701
 
      else if (!strcmp(label, FR_SIZE))
702
 
        meta->size = numdata;
703
 
      else if (!strcmp(label, FR_CODEC))
704
 
        meta->codec = data;
705
 
      else if (!strcmp(label, FR_TRACK))
706
 
        meta->trackno = numdata;
707
 
      else if (!strcmp(label, FR_YEAR))
708
 
        meta->year = numdata;
709
 
      else if (!strcmp(label, FR_FNAME))
710
 
        meta->filename = data;
711
 
      else if (!strcmp(label, FR_PROTECTED)) {
 
1135
      if (!strcmp(frame->label, FR_ARTIST)) {
 
1136
        meta->artist = g_strdup(frame->data.strval);
 
1137
      } else if (!strcmp(frame->label, FR_TITLE)) {
 
1138
        meta->title = g_strdup(frame->data.strval);
 
1139
      } else if (!strcmp(frame->label, FR_ALBUM)) {
 
1140
        meta->album = g_strdup(frame->data.strval);
 
1141
      } else if (!strcmp(frame->label, FR_GENRE)) {
 
1142
        meta->genre = g_strdup(frame->data.strval);
 
1143
      } else if (!strcmp(frame->label, FR_LENGTH)) {
 
1144
        meta->length = seconds_to_mmss((guint) frame->data.u_int16_val);
 
1145
      } else if (!strcmp(frame->label, FR_SIZE)) {
 
1146
        meta->size = frame->data.u_int32_val;
 
1147
      } else if (!strcmp(frame->label, FR_CODEC)) {
 
1148
        // Uppercaseify it. Work in all GTK+ >= 2.0?
 
1149
        meta->codec = g_ascii_strup(frame->data.strval, -1);
 
1150
      } else if (!strcmp(frame->label, FR_TRACK)) {
 
1151
        meta->trackno = (guint) frame->data.u_int16_val;
 
1152
      } else if (!strcmp(frame->label, FR_YEAR)) {
 
1153
        meta->year = (guint) frame->data.u_int16_val;
 
1154
      } else if (!strcmp(frame->label, FR_FNAME)) {
 
1155
        meta->filename = g_strdup(frame->data.strval);
 
1156
      } else if (!strcmp(frame->label, FR_FOLDER)) {
 
1157
        meta->folder = g_strdup(frame->data.strval);
 
1158
      } else if (!strcmp(frame->label, FR_PROTECTED)) {
712
1159
        meta->protect = TRUE;
713
 
        g_free(data);
714
 
      }
715
 
      else {
716
 
        if (data) {
717
 
          g_print("Discarded: %s -> %s\n", label, data);
718
 
          g_free(data);
719
 
        }
720
 
      }
721
 
      g_free(label);
 
1160
      } else {
 
1161
        g_print("Unknown frame type %s\n", frame->label);
 
1162
      }
722
1163
    }
723
1164
    /* Compensate for missing tag information */
724
1165
    if (!meta->artist)
735
1176
    g_hash_table_insert(songhash,
736
1177
                        GUINT_TO_POINTER(string_to_guint(meta->path)),
737
1178
                        (gpointer) meta);
738
 
    songid_destroy (songtag);
 
1179
    NJB_Songid_Destroy(songtag);
739
1180
    if (cancel_jukebox_operation)
740
1181
      break;
741
1182
  }
747
1188
  gdk_threads_enter();
748
1189
  gtk_label_set_text(GTK_LABEL(args->label), scan_datafiles);
749
1190
 
750
 
  /* Hack to get the datafile list store built in O(n) instead of O(n^2) */
751
 
  recreate_list_store(JBDATA_LIST);
 
1191
  /* Destroy the old datafile list */
 
1192
  destroy_datafile_list();
752
1193
 
753
1194
  gdk_threads_leave();
754
1195
  while (datatag = NJB_Get_Datafile_Tag (njb)) {
765
1206
    g_free(scanstring);
766
1207
 
767
1208
    meta = new_metadata_t();
768
 
    meta->filename = g_strdup(datatag->filename);
769
1209
    /* g_print("Scan: filename %s, size %d\n", datatag->filename, strlen(datatag->filename)); */
770
1210
    /* Convert filesize from 64 bit unsigned integer value */
771
 
    filesize = (u_int64_t) datatag->msdw;
772
 
    filesize = filesize << 32;
773
 
    filesize = filesize | (u_int64_t) datatag->lsdw;
774
 
    /* data = g_strdup_printf("%Lu", filesize); */
775
 
    meta->size = (guint) filesize; /* FIXME: hows it right? Also for tracks? */
 
1211
    filesize = (u_int64_t) datatag->filesize;
 
1212
    /* FIXME: here we loos all the 64-bit quality, because meta->size is 32-bit ... */
 
1213
    meta->size = (guint) filesize;
 
1214
    meta->folder = g_strdup(datatag->folder);
 
1215
    if (datatag->filesize == 0) {
 
1216
      gchar **tmp = g_strsplit(datatag->folder, "\\", 0);
 
1217
      gint veclen = vectorlength(tmp);
 
1218
      if (veclen > 2) {
 
1219
        gchar *basename = tmp[veclen-2];
 
1220
        meta->filename = g_strdup(basename);
 
1221
      } else {
 
1222
        /* This should not happen */
 
1223
        meta->filename = g_strdup("Erroneous folder");
 
1224
      }
 
1225
      g_strfreev(tmp);
 
1226
    } else {
 
1227
      meta->filename = g_strdup(datatag->filename);
 
1228
    }
776
1229
    /* File ID */
777
1230
    meta->path = g_strdup_printf("%lu", datatag->dfid);
778
 
    gdk_threads_enter();
779
 
    add_metadata_to_model(meta, JBDATA_LIST);
780
 
    gdk_threads_leave();
781
 
    destroy_metadata_t(meta);
782
 
    datafile_destroy (datatag);
 
1231
    datafilelist = g_slist_append(datafilelist, (gpointer) meta);
 
1232
    // destroy_metadata_t(meta);
 
1233
    NJB_Datafile_Destroy (datatag);
783
1234
    if (cancel_jukebox_operation)
784
1235
      break;
785
1236
  }
786
1237
 
787
1238
  gdk_threads_enter();
788
 
  /* Then sort everything */
789
 
  view_and_sort_list_store(JBDATA_LIST);
 
1239
  /* Then refill and sort everything */
 
1240
  rebuild_datafile_list("\\");
790
1241
  gtk_label_set_text(GTK_LABEL(args->label), scan_playlists);
791
1242
  gdk_threads_leave();
792
1243
  build_playlist_tree(args->pltreestore, TRUE);
839
1290
      jbmeta = (metadata_t *) tmplist->data;
840
1291
      filename = compose_filename(jbmeta);
841
1292
      tmpfname = filename_fromutf8(filename);
 
1293
      create_dirs(tmpfname);
842
1294
      id = string_to_guint(jbmeta->path);
843
1295
      gdk_threads_enter();
844
1296
      gtk_label_set_text(GTK_LABEL(args->label), filename);
847
1299
      /* g_print("Transferring %s...\n", file); */
848
1300
      /* Transfer track */
849
1301
      if ( NJB_Get_Track(njb, id, jbmeta->size, tmpfname, progress, NULL) == -1 ) {
850
 
        njb_error_dump(stderr);
 
1302
        NJB_Error_Dump(njb, stderr);
851
1303
        g_free(filename);
852
1304
        goto clean_up_and_return;
853
1305
      }
915
1367
      tmpfname = filename_fromutf8(jbmeta->filename);
916
1368
      if (NJB_Get_File(njb, id, jbmeta->size, tmpfname, progress, NULL) == -1) {
917
1369
        g_free(tmpfname);
918
 
        njb_error_dump(stderr);
 
1370
        NJB_Error_Dump(njb, stderr);
919
1371
        goto data_error;
920
1372
      }
921
1373
      g_free(tmpfname);
963
1415
    {
964
1416
      metadata_t *hdmeta;
965
1417
      metadata_t *jbmeta;
966
 
      gchar *tmpyear;
967
1418
      gint protectint;
968
1419
      gchar *tmpfname;
969
1420
      gchar *tmp;
970
1421
      u_int32_t id, length;
 
1422
      njb_songid_t *songid;
 
1423
      njb_songid_frame_t *frame;
971
1424
      
972
1425
      hdmeta = (metadata_t *) templist->data;
973
1426
      // g_print("Storing %s on jukebox...\n", hdmeta->filename);
978
1431
      gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), (gfloat) 0);
979
1432
      gdk_threads_leave();
980
1433
      length = mmss_to_seconds(hdmeta->length);
981
 
      tmpyear = g_strdup_printf("%lu", hdmeta->year);
982
1434
      protectint = hdmeta->protect ? 1 : 0;
 
1435
      songid = NJB_Songid_New();
 
1436
      frame = NJB_Songid_Frame_New_Codec(hdmeta->codec);
 
1437
      NJB_Songid_Addframe(songid, frame);
 
1438
      // libnjb will add this if not added by hand
 
1439
      // frame = NJB_Songid_Frame_New_Filesize(filesize);
 
1440
      // NJB_Songid_Addframe(songid, frame);
 
1441
      frame = NJB_Songid_Frame_New_Title(hdmeta->title);
 
1442
      NJB_Songid_Addframe(songid, frame);
 
1443
      frame = NJB_Songid_Frame_New_Album(hdmeta->album);
 
1444
      NJB_Songid_Addframe(songid, frame);
 
1445
      frame = NJB_Songid_Frame_New_Artist(hdmeta->artist);
 
1446
      NJB_Songid_Addframe(songid, frame);
 
1447
      frame = NJB_Songid_Frame_New_Genre(hdmeta->genre);
 
1448
      NJB_Songid_Addframe(songid, frame);
 
1449
      frame = NJB_Songid_Frame_New_Year(hdmeta->year);
 
1450
      NJB_Songid_Addframe(songid, frame);
 
1451
      frame = NJB_Songid_Frame_New_Tracknum(hdmeta->trackno);
 
1452
      NJB_Songid_Addframe(songid, frame);
 
1453
      frame = NJB_Songid_Frame_New_Length(length);
 
1454
      NJB_Songid_Addframe(songid, frame);
 
1455
      frame = NJB_Songid_Frame_New_Filename(hdmeta->filename);
 
1456
      NJB_Songid_Addframe(songid, frame);
 
1457
      if (hdmeta->protect) {
 
1458
        frame = NJB_Songid_Frame_New_Protected(1);
 
1459
        NJB_Songid_Addframe(songid, frame);
 
1460
      }
 
1461
 
983
1462
      /* Optionally remove ID3 tag */
984
1463
      if (get_prefs_id3remove() && !strcmp(hdmeta->codec, "MP3")) {
985
1464
        gchar *tmpfname8;
986
 
        gchar *tmpfname;
987
 
 
988
1465
        tmpfname8 = g_build_filename(tmpdirname, hdmeta->filename, NULL);
989
1466
        tmpfname = filename_fromutf8(tmpfname8);
 
1467
 
990
1468
        /* Call the filesystem to clone the file and remove the tag */
991
1469
        if (clone_and_strip_id3(hdmeta->path, tmpfname8)) {
992
1470
          // g_print("Transferring track.\n");
993
 
          if (NJB_Send_Track (njb, 
994
 
                              tmpfname, 
995
 
                              hdmeta->codec, 
996
 
                              hdmeta->title, 
997
 
                              hdmeta->album, 
998
 
                              hdmeta->genre,
999
 
                              hdmeta->artist, 
1000
 
                              length, 
1001
 
                              hdmeta->trackno, 
1002
 
                              tmpyear, 
1003
 
                              protectint, 
1004
 
                              progress, 
1005
 
                              NULL, 
 
1471
          if (NJB_Send_Track (njb, tmpfname, songid,
 
1472
                              progress, NULL, 
1006
1473
                              &id) == -1) {
1007
 
            njb_error_dump(stderr);
 
1474
            NJB_Error_Dump(njb, stderr);
1008
1475
            goto hd2jb_cleanup;
1009
1476
          }
1010
1477
          /* Afterwards delete the tempfile */
1011
1478
          unlink(tmpfname);
1012
1479
          g_free(tmpfname8);
1013
 
          g_free(tmpfname);
1014
 
          jukebox_songs++;
1015
1480
        }
1016
1481
      } else {
1017
1482
        tmpfname = filename_fromutf8(hdmeta->path);
1018
 
        if (NJB_Send_Track (njb, 
1019
 
                            tmpfname, 
1020
 
                            hdmeta->codec, 
1021
 
                            hdmeta->title, 
1022
 
                            hdmeta->album, 
1023
 
                            hdmeta->genre,
1024
 
                            hdmeta->artist, 
1025
 
                            length, 
1026
 
                            hdmeta->trackno, 
1027
 
                            tmpyear, 
1028
 
                            protectint, 
1029
 
                            progress, 
1030
 
                            NULL, 
 
1483
        if (NJB_Send_Track (njb, tmpfname, songid,
 
1484
                            progress, NULL, 
1031
1485
                            &id) == -1) {
1032
 
          g_free(tmpfname);
1033
 
          njb_error_dump(stderr);
 
1486
          NJB_Error_Dump(njb, stderr);
1034
1487
          goto hd2jb_cleanup;
1035
1488
        }
1036
 
        /* g_print("%s stored on jukebox with ID: %lu\n", file, id); */
1037
 
        g_free(tmpfname);
1038
 
        jukebox_songs++;
1039
1489
      }
1040
 
      g_free(tmpyear);
 
1490
      /* g_print("%s stored on jukebox with ID: %lu\n", file, id); */
 
1491
      g_free(tmpfname);
 
1492
      jukebox_songs++;
1041
1493
      /* Add to disk listbox and re-sort */
1042
1494
      /* Add correct trackid in col 7, add the row */
1043
1495
      jbmeta = clone_metadata_t(hdmeta);
1057
1509
  // Remove temporary directory for tag-stripped files.
1058
1510
  if (get_prefs_id3remove()) {
1059
1511
    rmdir(tmpdirname);
1060
 
    g_free(tmpdirname);
 
1512
    // g_free(tmpdirname);
1061
1513
  }
1062
1514
  /* At last add the tracks to default playlists if
1063
1515
   * any such are selected. */
1092
1544
         !cancel_jukebox_operation)
1093
1545
    {
1094
1546
      gchar *tmpfname;
 
1547
      gchar *folder;
 
1548
      gchar rootdir[] = "\\";
1095
1549
      u_int32_t id;
1096
1550
      metadata_t *hdmeta;
1097
1551
      metadata_t *jbmeta;
1098
1552
      
1099
1553
      hdmeta = (metadata_t *) metalist->data;
1100
 
      /* g_print("Storing %s on jukebox as %s...\n", hdmeta->path, hdmeta->filename); */
 
1554
      folder = (gchar *) gtk_entry_get_text(GTK_ENTRY(data_widgets.jbentry));
 
1555
      if (folder == NULL) {
 
1556
        /* This shouldn't happen, but include it anyway. */
 
1557
        folder = rootdir;
 
1558
      }
 
1559
      /* g_print("Storing %s on jukebox as %s in folder %s...\n", hdmeta->path, hdmeta->filename, folder); */
1101
1560
      gdk_threads_enter();
1102
1561
      gtk_label_set_text(GTK_LABEL(args->label), hdmeta->filename);
1103
1562
      gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), (gfloat) 0);
1104
1563
      gdk_threads_leave();
1105
1564
      tmpfname = filename_fromutf8(hdmeta->path);
1106
 
      if (NJB_Send_File (njb, tmpfname, hdmeta->filename, progress, NULL, &id) == -1) {
 
1565
      if (NJB_Send_File (njb, tmpfname, hdmeta->filename, folder, progress, NULL, &id) == -1) {
1107
1566
        g_free(tmpfname);
1108
 
        njb_error_dump(stderr);
 
1567
        NJB_Error_Dump(njb, stderr);
1109
1568
        goto hd2jb_data_cleanup;
1110
1569
      }
1111
1570
      g_free(tmpfname);
1118
1577
      jbmeta = clone_metadata_t(hdmeta);
1119
1578
      g_free(jbmeta->path);
1120
1579
      jbmeta->path = g_strdup_printf("%lu", id);
 
1580
      if (jbmeta->folder != NULL) {
 
1581
        g_free(jbmeta->folder);
 
1582
      }
 
1583
      jbmeta->folder = g_strdup(folder);
1121
1584
      gdk_threads_enter();
1122
1585
      add_metadata_to_model(jbmeta, JBDATA_LIST);
1123
1586
      gdk_threads_leave();
1124
 
      destroy_metadata_t(jbmeta);
 
1587
      /* Keep this in the global metadata file list! */
 
1588
      datafilelist = g_slist_append(datafilelist, (gpointer) jbmeta);
1125
1589
      metalist = g_list_next(metalist);
1126
1590
    }
1127
1591
  flush_usage();
1142
1606
                                         GtkTreeStore *pltreestore)
1143
1607
{
1144
1608
  GList *tmplist;
1145
 
  playlist_t *playlist;
1146
1609
 
1147
 
  if (!metalist)
 
1610
  if (metalist == NULL) {
1148
1611
    return;
1149
 
  /* g_print("Called remove_tracks_from_playlists()\n"); */
1150
 
  NJB_Reset_Get_Playlist(njb);
1151
 
  while (playlist = NJB_Get_Playlist(njb)) {
1152
 
    playlist_track_t *track;
 
1612
  }
 
1613
  /* g_print("Called remove_tracks_from_playlists()\n"); */  
 
1614
  tmplist = metalist;
 
1615
 
 
1616
  while (tmplist) {
 
1617
    njb_playlist_t *playlist;
 
1618
    metadata_t *meta;
 
1619
    u_int32_t id;  
1153
1620
    
1154
 
    playlist_reset_gettrack(playlist);
1155
 
    while (track = playlist_gettrack(playlist)) {
1156
 
      tmplist = metalist;
1157
 
      while (tmplist) {
1158
 
        metadata_t *meta;
1159
 
        u_int32_t id;
 
1621
    meta = (metadata_t *) tmplist->data;
 
1622
    id = string_to_guint(meta->path);
 
1623
    NJB_Reset_Get_Playlist(njb);
1160
1624
 
1161
 
        meta = (metadata_t *) tmplist->data;
1162
 
        id = string_to_guint(meta->path);
1163
 
        if (id == track->trackid) {
1164
 
          /* When the track is located in a playlist, remove it */
1165
 
          /* g_print("Removing track: %lu from playlist %lu\n", 
1166
 
             track->trackid, playlist->plid); */
1167
 
          if (track->prev != NULL)
1168
 
            track->prev->next = track->next;
1169
 
          else
1170
 
            playlist->first = track->next;
1171
 
          if (track->next != NULL)
1172
 
            track->next->prev = track->prev;
1173
 
          playlist_track_destroy(track);
1174
 
          playlist->ntracks--;
1175
 
          playlist->_state= NJB_PL_CHTRACKS;
1176
 
        }
1177
 
        tmplist = g_list_next(tmplist);
 
1625
    while (playlist = NJB_Get_Playlist(njb)) {
 
1626
      NJB_Playlist_Deltrack_TrackID(playlist, id);
 
1627
      /* If the playlist changed, update it */
 
1628
      if (playlist->_state == NJB_PL_CHTRACKS) {
 
1629
        if (NJB_Update_Playlist(njb, playlist) == -1)
 
1630
          NJB_Error_Dump(njb, stderr);
1178
1631
      }
1179
 
    }
1180
 
    /* If the playlist changed, update it */
1181
 
    if (playlist->_state==NJB_PL_CHTRACKS) {
1182
 
      if (NJB_Update_Playlist(njb, playlist) == -1)
1183
 
        njb_error_dump(stderr);
1184
 
    }
1185
 
    playlist_destroy(playlist);
1186
 
  }
1187
 
  if (njb_error != EO_EOM) {
1188
 
    njb_error_dump(stderr);
 
1632
      NJB_Playlist_Destroy(playlist);
 
1633
    }
 
1634
    if (NJB_Error_Pending(njb)) {
 
1635
      NJB_Error_Dump(njb, stderr);
 
1636
    }
 
1637
    tmplist = g_list_next(tmplist);
1189
1638
  }
1190
1639
  build_playlist_tree(pltreestore, FALSE);
1191
1640
}
1208
1657
    /* g_print("Deleting: %u from jukebox library\n", id); */
1209
1658
    if (id) {
1210
1659
      if ( NJB_Delete_Datafile(njb, id) == -1 ) {
1211
 
        njb_error_dump(stderr);
 
1660
        NJB_Error_Dump(njb, stderr);
1212
1661
      }
 
1662
      // Remove it from the datafile cache
 
1663
      remove_from_datafile_list(meta->path);
 
1664
      // Then decrease the counter
1213
1665
      if (jukebox_datafiles)
1214
1666
        jukebox_datafiles--;
1215
1667
    }
1219
1671
  jukebox_locked = FALSE;
1220
1672
}
1221
1673
 
1222
 
/* Delete a list of files from the jukebox */
 
1674
/* Delete a list of tracks from the jukebox */
1223
1675
void jukebox_delete_tracks(GList *metalist,
1224
1676
                           GtkTreeStore *pltreestore)
1225
1677
{
1240
1692
    if (id) {
1241
1693
      metadata_t *tmpmeta;
1242
1694
      if ( NJB_Delete_Track(njb, id) == -1 ) {
1243
 
        njb_error_dump(stderr);
 
1695
        NJB_Error_Dump(njb, stderr);
1244
1696
      }
1245
1697
      /* Remove song from hash */
1246
1698
      tmpmeta = (metadata_t *) g_hash_table_lookup(songhash, GUINT_TO_POINTER(id));
1247
 
      // Sometimes tracks are removed that are not part of any playlist. Weird but happens.
 
1699
      /* Sometimes tracks are removed that are not part of any playlist. Weird but happens. */
1248
1700
      if (tmpmeta != NULL) {
1249
1701
        destroy_hash(NULL, tmpmeta, NULL);
1250
1702
        g_hash_table_remove(songhash,
1272
1724
  /* Set metadata on the file with ID id */
1273
1725
  u_int32_t id, length;
1274
1726
  metadata_t *tmpmeta;
1275
 
  gchar *tmpyear;
1276
 
  gint protectint;
 
1727
  njb_songid_t *songid;
 
1728
  njb_songid_frame_t *frame;
1277
1729
 
1278
1730
  id = string_to_guint(meta->path);
1279
1731
  length = mmss_to_seconds(meta->length);
1280
 
  tmpyear = g_strdup_printf("%lu", meta->year);
1281
 
  protectint = meta->protect ? 1 : 0;
1282
 
 
1283
 
  if (NJB_Replace_Track_Tag (njb,
1284
 
                             id,
1285
 
                             meta->codec,
1286
 
                             meta->title,
1287
 
                             meta->album, 
1288
 
                             meta->genre,
1289
 
                             meta->artist,
1290
 
                             length,
1291
 
                             meta->trackno,
1292
 
                             meta->size, 
1293
 
                             meta->filename,
1294
 
                             tmpyear, 
1295
 
                             protectint) == -1) {
1296
 
    njb_error_dump(stderr);
1297
 
  }
1298
 
  g_free(tmpyear);
 
1732
  if (id == 0) {
 
1733
    create_error_dialog(_("Track ID was zero! Illegal value!"));
 
1734
    return;
 
1735
  }
 
1736
  if (length < 0) {
 
1737
    create_error_dialog(_("Song length must be greater than zero!"));
 
1738
    return;
 
1739
  }
 
1740
 
 
1741
  songid = NJB_Songid_New();
 
1742
  /* On NJB1 incremental update is not possible! */
 
1743
  if (njb->device_type == NJB_DEVICE_NJB1) {
 
1744
    // Sometimes it fails with old codec names.
 
1745
    gchar *tmpcodec = g_ascii_strup(meta->codec, -1);
 
1746
 
 
1747
    frame = NJB_Songid_Frame_New_Codec(tmpcodec);
 
1748
    g_free(tmpcodec);
 
1749
    NJB_Songid_Addframe(songid, frame);
 
1750
    frame = NJB_Songid_Frame_New_Filesize(meta->size);
 
1751
    NJB_Songid_Addframe(songid, frame);
 
1752
  }
 
1753
  /* Ultimately only send altered fields to NJB series 3 */
 
1754
  frame = NJB_Songid_Frame_New_Title(meta->title);
 
1755
  NJB_Songid_Addframe(songid, frame);
 
1756
  frame = NJB_Songid_Frame_New_Album(meta->album);
 
1757
  NJB_Songid_Addframe(songid, frame);
 
1758
  frame = NJB_Songid_Frame_New_Artist(meta->artist);
 
1759
  NJB_Songid_Addframe(songid, frame);
 
1760
  frame = NJB_Songid_Frame_New_Genre(meta->genre);
 
1761
  NJB_Songid_Addframe(songid, frame);
 
1762
  frame = NJB_Songid_Frame_New_Year(meta->year);
 
1763
  NJB_Songid_Addframe(songid, frame);
 
1764
  frame = NJB_Songid_Frame_New_Tracknum(meta->trackno);
 
1765
  NJB_Songid_Addframe(songid, frame);
 
1766
  frame = NJB_Songid_Frame_New_Length(length);
 
1767
  NJB_Songid_Addframe(songid, frame);
 
1768
  /*
 
1769
   * These will only be set if the jukebox has retrieved
 
1770
   * extended metadata so that the user has a chance of modifying
 
1771
   * an existing tag, not just overwrite what is already there.
 
1772
   */
 
1773
  if (get_prefs_extended_metadata()) {
 
1774
    if (meta->filename != NULL) {
 
1775
      frame = NJB_Songid_Frame_New_Filename(meta->filename);
 
1776
      NJB_Songid_Addframe(songid, frame);
 
1777
    }
 
1778
    if (meta->folder != NULL) {
 
1779
      frame = NJB_Songid_Frame_New_Folder(meta->folder);
 
1780
      NJB_Songid_Addframe(songid, frame);
 
1781
    }
 
1782
  }
 
1783
  if (meta->protect) {
 
1784
    frame = NJB_Songid_Frame_New_Protected(1);
 
1785
    NJB_Songid_Addframe(songid, frame);
 
1786
  }
 
1787
 
 
1788
  if (NJB_Replace_Track_Tag(njb, id, songid) == -1) {
 
1789
    NJB_Error_Dump(njb, stderr);
 
1790
  }
1299
1791
  /* Replace the data in the hash table */
1300
1792
  tmpmeta = (metadata_t *) g_hash_table_lookup(songhash,
1301
1793
                                               GUINT_TO_POINTER(id));
1302
 
  // Make sure we actually find it! Sometimes it is non-existant...
 
1794
  /* Make sure we actually find it! Sometimes it is non-existant... */
1303
1795
  if (tmpmeta != NULL) {
1304
1796
    destroy_hash(NULL, tmpmeta, NULL);
1305
1797
    g_hash_table_remove(songhash,
1318
1810
  /* End metadata transaction */
1319
1811
  jukebox_locked = FALSE;
1320
1812
  /* Update the window with the new metadata */
1321
 
  // jblist_from_songhash(FALSE);
 
1813
  jblist_from_songhash(FALSE);
1322
1814
}
1323
1815
 
1324
1816
/* Creates a new playlist and returns its playlist ID UNTHREADED */
1325
1817
guint jukebox_create_playlist(gchar *plname, GtkTreeStore *pltreestore)
1326
1818
{
1327
 
  playlist_t *playlist;
 
1819
  njb_playlist_t *playlist;
1328
1820
  guint plid = 0;
1329
1821
 
1330
1822
  /* g_print("Called create playlist\n"); */
1331
1823
  /* Create the new playlist in memory */
1332
 
  playlist=playlist_new();
 
1824
  playlist = NJB_Playlist_New();
1333
1825
  if (playlist == NULL) {
1334
1826
    return 0;
1335
1827
  }
1336
 
  if (playlist_set_name(playlist, plname) == -1) {
1337
 
    njb_error_dump(stderr);
 
1828
  if (NJB_Playlist_Set_Name(playlist, plname) == -1) {
 
1829
    NJB_Error_Dump(njb, stderr);
1338
1830
    return 0;
1339
1831
  }
1340
1832
  jukebox_locked = TRUE;
1343
1835
    gdk_threads_enter();
1344
1836
    create_error_dialog(_("Could not create playlist"));
1345
1837
    gdk_threads_leave();
1346
 
    njb_error_dump(stderr);
 
1838
    NJB_Error_Dump(njb, stderr);
1347
1839
  }
1348
1840
  jukebox_playlists++;
1349
1841
  plid = playlist->plid;
1350
 
  playlist_destroy(playlist);
 
1842
  NJB_Playlist_Destroy(playlist);
1351
1843
  build_playlist_tree(pltreestore, FALSE);
1352
1844
  jukebox_locked = FALSE;
1353
1845
  /* g_print("Created playlist\n"); */
1362
1854
    gdk_threads_enter();
1363
1855
    create_error_dialog(_("Could not delete playlist"));
1364
1856
    gdk_threads_leave();
1365
 
    njb_error_dump(stderr);
 
1857
    NJB_Error_Dump(njb, stderr);
1366
1858
  }
1367
1859
  jukebox_playlists--;
1368
1860
  build_playlist_list();
1372
1864
/* Rename the playlist with ID plid UNTHREADED */
1373
1865
void jukebox_rename_playlist(guint plid, gchar *name, GtkTreeStore *pltreestore, gboolean threaded)
1374
1866
{
1375
 
  playlist_t *playlist;
 
1867
  njb_playlist_t *playlist;
1376
1868
  gboolean found = FALSE;
1377
1869
  
1378
1870
  jukebox_locked = TRUE;
1385
1877
      break;
1386
1878
    }
1387
1879
    /* g_print("No.\n"); */
1388
 
    playlist_destroy(playlist);
 
1880
    NJB_Playlist_Destroy(playlist);
1389
1881
  }
1390
1882
  if (found) {
1391
 
    if (playlist_set_name(playlist, name) == -1) {
1392
 
      njb_error_dump(stderr);
 
1883
    if (NJB_Playlist_Set_Name(playlist, name) == -1) {
 
1884
      NJB_Error_Dump(njb, stderr);
1393
1885
    } else {
1394
1886
      if (NJB_Update_Playlist(njb, playlist) == -1) {
1395
1887
        if (threaded)
1397
1889
        create_error_dialog(_("Could not rename playlist"));
1398
1890
        if (threaded)
1399
1891
          gdk_threads_leave();
1400
 
        njb_error_dump(stderr);
 
1892
        NJB_Error_Dump(njb, stderr);
1401
1893
      }
1402
1894
    }
1403
 
    playlist_destroy(playlist);
 
1895
    NJB_Playlist_Destroy(playlist);
1404
1896
    build_playlist_tree(pltreestore, FALSE);
1405
1897
  } else {
1406
1898
    if (threaded)
1430
1922
}
1431
1923
 
1432
1924
 
 
1925
/* Randomize file from playlist UNTHREADED */
 
1926
guint jukebox_randomize_playlist(guint plist, GtkTreeStore *pltreestore)
 
1927
{
 
1928
  guint new_plid = plist;
 
1929
  
 
1930
  if (plist != 0) {
 
1931
    njb_playlist_t *playlist;
 
1932
    gboolean found = FALSE;
 
1933
    
 
1934
    jukebox_locked = TRUE;
 
1935
    // g_print("randomizing playlist %lu\n", plist);
 
1936
    NJB_Reset_Get_Playlist(njb);
 
1937
    while (playlist = NJB_Get_Playlist(njb)) {
 
1938
      // g_print("Is it %lu?\n", playlist->plid);
 
1939
      if (playlist->plid == plist) {
 
1940
        // g_print("YES!");
 
1941
        found = TRUE;
 
1942
        break;
 
1943
      }
 
1944
      // g_print("NO.");
 
1945
      NJB_Playlist_Destroy(playlist);
 
1946
    }
 
1947
    if (found) {
 
1948
      GPtrArray *contents = g_ptr_array_sized_new (playlist->ntracks);
 
1949
      gpointer temp;
 
1950
      njb_playlist_track_t *current = playlist->first;
 
1951
      njb_playlist_track_t *prev = NULL;
 
1952
      GRand* randnum = g_rand_new();
 
1953
      gint i, j;
 
1954
      // g_print(" playlist size is %u\n", playlist->ntracks);
 
1955
      
 
1956
      if (playlist->ntracks > 1)
 
1957
        {
 
1958
          //g_print("Old order\n");
 
1959
          while(current != NULL)
 
1960
            {
 
1961
              //g_print("%lu\n", current->trackid);
 
1962
              g_ptr_array_add(contents, current);
 
1963
              current = current->next;
 
1964
            }
 
1965
          
 
1966
          //For each item, swap with a randommly selected later item
 
1967
          for (i = 0; i < playlist->ntracks; ++i)
 
1968
            {
 
1969
              j = g_rand_int_range(randnum, i, playlist->ntracks);
 
1970
              //g_print("Swapping %lu [%i] with %lu [%i]\n",((njb_playlist_track_t *)contents->pdata[i])->trackid,i,((njb_playlist_track_t *)contents->pdata[j])->trackid,j);
 
1971
              temp = contents->pdata[i];
 
1972
              contents->pdata[i] = contents->pdata[j];
 
1973
              contents->pdata[j] = temp;
 
1974
            }
 
1975
          
 
1976
          // Repair the chaining
 
1977
          // g_print("New order\n");
 
1978
          for (i = 0; i < playlist->ntracks; ++i)
 
1979
            {
 
1980
              current = (njb_playlist_track_t *)contents->pdata[i];
 
1981
              //g_print("%lu\n", current->trackid);
 
1982
              current->prev = prev;
 
1983
              if (prev != NULL)
 
1984
                prev->next = current;
 
1985
              current->next = NULL;
 
1986
              prev = current;
 
1987
            }
 
1988
          
 
1989
          playlist->first  = contents->pdata[0];
 
1990
          playlist->last   = prev;
 
1991
          
 
1992
          // Mark it shuffled
 
1993
          playlist->_state = NJB_PL_CHTRACKS;
 
1994
          if (NJB_Update_Playlist(njb, playlist) == -1)
 
1995
            NJB_Error_Dump(njb, stderr);
 
1996
          new_plid = playlist->plid;
 
1997
        } else {
 
1998
          g_print("Playlist is too small to shuffle\n");
 
1999
        }
 
2000
      // Clean up
 
2001
      NJB_Playlist_Destroy(playlist);
 
2002
      g_ptr_array_free(contents, FALSE);
 
2003
      g_rand_free(randnum);
 
2004
      // Then rebuild the playlist list (necessary).
 
2005
      build_playlist_tree(pltreestore, FALSE);
 
2006
    }
 
2007
  }
 
2008
  jukebox_locked = FALSE;
 
2009
  // g_print("Old playlist ID: %lu, new playlist ID: %lu\n", plist, new_plid);
 
2010
  return new_plid;
 
2011
}
 
2012
 
1433
2013
/* Delete file from playlist UNTHREADED */
1434
 
guint jukebox_delete_track_from_playlist(guint trackid, guint plist)
 
2014
guint jukebox_delete_track_from_playlist(guint trackid, guint plist, GtkTreeStore *pltreestore)
1435
2015
{
1436
2016
  guint new_plid = plist;
1437
2017
 
1438
2018
  if (trackid != 0 && plist != 0) {
1439
 
    playlist_t *playlist;
 
2019
    njb_playlist_t *playlist;
1440
2020
    gboolean found = FALSE;
 
2021
 
1441
2022
    jukebox_locked = TRUE;
1442
 
 
1443
 
    g_print("Removing track: %lu from playlist %lu\n", 
1444
 
            trackid, plist);
 
2023
    // g_print("Removing track: %lu from playlist %lu\n", trackid, plist);
1445
2024
    NJB_Reset_Get_Playlist(njb);
1446
2025
    while (playlist = NJB_Get_Playlist(njb)) {
1447
 
      g_print("Is it in %lu?\n", playlist->plid);
 
2026
      // g_print("Is it in %lu? ...", playlist->plid);
1448
2027
      if (playlist->plid == plist) {
1449
 
        g_print("YES!");
 
2028
        // g_print("YES!\n");
1450
2029
        found = TRUE;
1451
2030
        break;
1452
2031
      }
1453
 
      g_print("NO.");
1454
 
      playlist_destroy(playlist);
 
2032
      // g_print("NO.\n");
 
2033
      NJB_Playlist_Destroy(playlist);
1455
2034
    }
1456
2035
    /* If the playlist was found, remove the track */
1457
2036
    if (found) {
1458
 
      playlist_track_t *track;
 
2037
      njb_playlist_track_t *track;
1459
2038
      
1460
 
      g_print("Found playlist %lu\n", playlist->plid);
1461
 
      while (track = playlist_gettrack(playlist)) {
 
2039
      // g_print("Found playlist %lu\n", playlist->plid);
 
2040
      while (track = NJB_Playlist_Gettrack(playlist)) {
1462
2041
        if (trackid == track->trackid) {
1463
2042
          /* When the track is located in a playlist, remove it */
1464
 
          g_print("Removing track: %lu from playlist %lu\n", 
1465
 
                  track->trackid, playlist->plid);
 
2043
          // g_print("Removing track: %lu from playlist %lu\n", track->trackid, playlist->plid);
1466
2044
          
1467
2045
          if (track->prev != NULL)
1468
2046
            track->prev->next = track->next;
1470
2048
            playlist->first = track->next;
1471
2049
          if (track->next != NULL)
1472
2050
            track->next->prev = track->prev;
1473
 
          playlist_track_destroy(track);
 
2051
          NJB_Playlist_Track_Destroy(track);
1474
2052
          playlist->ntracks--;
1475
2053
          playlist->_state = NJB_PL_CHTRACKS;
1476
2054
          break;
1479
2057
      if (playlist->_state==NJB_PL_CHTRACKS) {
1480
2058
        /* g_print("Called NJB_Update_Playlist...\n"); */
1481
2059
        if (NJB_Update_Playlist(njb, playlist) == -1)
1482
 
          njb_error_dump(stderr);
 
2060
          NJB_Error_Dump(njb, stderr);
1483
2061
        new_plid = playlist->plid;
1484
2062
      }
1485
 
      playlist_destroy(playlist);
 
2063
      NJB_Playlist_Destroy(playlist);
1486
2064
    } else {
1487
2065
      create_error_dialog(_("Could not find the track in the playlist"));
1488
2066
    }
1489
2067
  }
1490
2068
  jukebox_locked = FALSE;
1491
 
  // build_playlist_tree(playlisttree, FALSE);
1492
 
  g_print("Old playlist ID: %lu, new playlist ID: %lu\n", plist, new_plid);
 
2069
  // g_print("Old playlist ID: %lu, new playlist ID: %lu\n", plist, new_plid);
 
2070
  // When replacing playlist ID does not work we must use this
 
2071
  // build_playlist_tree(pltreestore, FALSE);
1493
2072
  return new_plid;
1494
2073
}
1495
2074
 
1584
2163
GList *jukebox_get_playlist_for_play(guint plid)
1585
2164
{
1586
2165
  GList *retlist = NULL;
1587
 
  playlist_t *playlist;
 
2166
  njb_playlist_t *playlist;
1588
2167
  gboolean found = FALSE;
1589
2168
 
1590
2169
  jukebox_locked = TRUE;
1595
2174
      break;
1596
2175
    }
1597
2176
    // Dangerous?
1598
 
    playlist_destroy(playlist);
 
2177
    NJB_Playlist_Destroy(playlist);
1599
2178
  }
1600
2179
  /* Get the tracks from the playlist */
1601
2180
  if (found) {
1602
 
    playlist_track_t *track;
 
2181
    njb_playlist_track_t *track;
1603
2182
    metadata_t *meta;
1604
2183
    metadata_t *newmeta;
1605
2184
    gchar *tmp;
1606
2185
 
1607
 
    while (track = playlist_gettrack(playlist)) {
 
2186
    while (track = NJB_Playlist_Gettrack(playlist)) {
1608
2187
      meta = (metadata_t *) g_hash_table_lookup(songhash,
1609
2188
                                                GUINT_TO_POINTER(track->trackid));
1610
2189
      // Sometimes it's non-existant
1637
2216
        /* g_print("Playing: %lu\n", id); */
1638
2217
        first = FALSE;
1639
2218
        if (NJB_Play_Track(njb, id) == -1)
1640
 
          njb_error_dump(stderr);
 
2219
          NJB_Error_Dump(njb, stderr);
1641
2220
      } else {
1642
2221
        /* g_print("Queueing: %lu\n", id); */
1643
2222
        if (NJB_Queue_Track(njb, id) == -1)
1644
 
          njb_error_dump(stderr);
 
2223
          NJB_Error_Dump(njb, stderr);
1645
2224
      }
1646
2225
    }
1647
2226
    tmplist = tmplist->next;
1660
2239
  g_free(tmp);
1661
2240
}
1662
2241
 
 
2242
/**
 
2243
 * Returns metadata for the currently playing track.
 
2244
 */
 
2245
metadata_t *jukebox_get_current_playing_metadata(void)
 
2246
{
 
2247
  metadata_t *retval = NULL;
 
2248
  if (created_play_mutex) {
 
2249
    g_mutex_lock(play_thread_mutex);
 
2250
    if (playlistitr != NULL) {
 
2251
      retval = (metadata_t *) playlistitr->data;
 
2252
    }
 
2253
    g_mutex_unlock(play_thread_mutex);
 
2254
  }
 
2255
  return retval;
 
2256
}
 
2257
 
 
2258
/**
 
2259
 * Skips forward/backward in song (position given
 
2260
 * as milliseconds).
 
2261
 */
 
2262
void jukebox_skip_songposition(guint songpos)
 
2263
{
 
2264
  if (created_play_mutex) {
 
2265
    g_mutex_lock(play_thread_mutex);
 
2266
    NJB_Seek_Track(njb, (u_int32_t) songpos);
 
2267
    g_mutex_unlock(play_thread_mutex);
 
2268
  } 
 
2269
}
 
2270
 
1663
2271
void jukebox_previous(void)
1664
2272
{
1665
2273
  if (created_play_mutex) {
1678
2286
  }
1679
2287
}
1680
2288
 
1681
 
/* The parameter tells if the next song is already
 
2289
/** 
 
2290
 * The parameter tells if the next song is already
1682
2291
 * playing, so that the list does not need to be
1683
 
 * resent. */
 
2292
 * resent.
 
2293
 */
1684
2294
void jukebox_next(gboolean already_playing)
1685
2295
{
1686
2296
  if (created_play_mutex) {
1856
2466
  NJB_Set_Time(njb, jukeboxtime);
1857
2467
  NJB_Destroy_Time(jukeboxtime);
1858
2468
}
 
2469
 
 
2470
/* 
 
2471
 * This creates a new folder on the device
 
2472
 */
 
2473
void jukebox_create_folder(gchar *foldername)
 
2474
{
 
2475
  gchar rootdir[] = "\\";
 
2476
  u_int32_t id;
 
2477
  metadata_t *jbmeta;
 
2478
  gchar *folder;
 
2479
  
 
2480
  jbmeta = new_metadata_t();
 
2481
 
 
2482
  folder = (gchar *) gtk_entry_get_text(GTK_ENTRY(data_widgets.jbentry));
 
2483
  if (folder == NULL) {
 
2484
    /* This shouldn't happen, but include it anyway. */
 
2485
    folder = rootdir;
 
2486
  }
 
2487
  // Add the new folder name
 
2488
  jbmeta->folder = g_strdup_printf("%s%s\\", folder, foldername);
 
2489
  jbmeta->filename = g_strdup(foldername);
 
2490
  jbmeta->size = 0;
 
2491
  if (NJB_Create_Folder (njb, jbmeta->folder, &id) == -1) {
 
2492
    // Error back off
 
2493
    destroy_metadata_t(jbmeta);
 
2494
    NJB_Error_Dump(njb, stderr);
 
2495
    create_error_dialog(_("The folder could not be created.\nThe most typical reason is that you are using a Nomad Jukebox 1\nwhich is too old to support folders."));
 
2496
    return;
 
2497
  }
 
2498
  jbmeta->path = g_strdup_printf("%lu", id);
 
2499
  /* Keep this in the global metadata file list! */
 
2500
  datafilelist = g_slist_append(datafilelist, (gpointer) jbmeta);
 
2501
  // Redraw
 
2502
  rebuild_datafile_list(folder);
 
2503
}