~ubuntu-branches/ubuntu/wily/gnomad2/wily

« back to all changes in this revision

Viewing changes to src/jukebox.c

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2006-04-08 15:58:16 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060408155816-25y8gx2wa4chfr69
Tags: 2.8.3-1
* New upstream release.
* Update the Debian policy to version 3.6.2.2. No changes necessary.
* Update the watch file.
* Update the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* jukebox.c
2
 
   Functions related to the communication with jukebox hardware
3
 
   using the libnjb library.
4
 
   Copyright (C) 2001-2003 Linus Walleij
5
 
 
6
 
This file is part of the GNOMAD package.
7
 
 
8
 
GNOMAD is free software; you can redistribute it and/or modify
9
 
it under the terms of the GNU General Public License as published by
10
 
the Free Software Foundation; either version 2, or (at your option)
11
 
any later version.
12
 
 
13
 
You should have received a copy of the GNU General Public License
14
 
along with GNOMAD; see the file COPYING.  If not, write to
15
 
the Free Software Foundation, 59 Temple Place - Suite 330,
16
 
Boston, MA 02111-1307, USA. 
17
 
 
18
 
*/
 
1
/**
 
2
 * \file jukebox.c
 
3
 *  Functions related to the communication with jukebox hardware
 
4
 *  using the libnjb library.
 
5
 *  Copyright (C) 2001-2003 Linus Walleij
 
6
 *
 
7
 * This file is part of the GNOMAD package.
 
8
 *
 
9
 * GNOMAD is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2, or (at your option)
 
12
 * any later version.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with GNOMAD; see the file COPYING.  If not, write to
 
16
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA. 
 
18
 *
 
19
 */
19
20
 
20
21
#include <time.h>
21
22
#include "common.h"
32
33
extern GtkWidget *main_window;
33
34
 
34
35
/* Local variables */
35
 
static njb_t njbs[NJB_MAX_DEVICES];
36
 
static njb_t *njb = NULL;
37
 
static gboolean njb_connected = FALSE;
 
36
static gboolean use_mtp = FALSE;
 
37
static njb_t pde_devices[NJB_MAX_DEVICES];
 
38
static njb_t *pdedevice = NULL;
 
39
static int PDE_device_count = 0;
 
40
static int MTP_device_count = 0;
 
41
static gboolean device_connected = FALSE;
 
42
#ifdef HAVE_LIBMTP
 
43
static LIBMTP_mtpdevice_t *mtpdevice;
 
44
#endif
38
45
/* Jukebox properties variables */
39
46
static gchar *jukebox_ownerstring = NULL;
40
47
static gchar *jukebox_timestring = NULL;
110
117
 
111
118
static void refresh_id(void)
112
119
{
113
 
  if (njb != NULL) {
114
 
    NJB_Ping(njb);
 
120
  if (!use_mtp && pdedevice != NULL) {
 
121
    NJB_Ping(pdedevice);
115
122
  }
116
123
}
117
124
 
118
125
/* Returns the unique jukebox ID */
119
126
static gchar *jukebox_get_idstring(void)
120
127
{
121
 
  if (njb != NULL) {
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]);
 
128
  if (use_mtp) {
 
129
#ifdef HAVE_LIBMTP
 
130
    return LIBMTP_Get_Serialnumber(mtpdevice);
 
131
#else
 
132
    return NULL;
 
133
#endif
 
134
  } else {
 
135
    if (pdedevice != NULL) {
 
136
      u_int8_t sdmiid[16];
 
137
      gchar idstring[32];
 
138
      
 
139
      if (NJB_Get_SDMI_ID(pdedevice, (u_int8_t *) &sdmiid) == 0) {
 
140
        return g_strdup_printf("%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
 
141
                               sdmiid[0], sdmiid[1], sdmiid[2], sdmiid[3], sdmiid[4], 
 
142
                               sdmiid[5], sdmiid[6], sdmiid[7], sdmiid[8], sdmiid[9], 
 
143
                               sdmiid[10], sdmiid[11], sdmiid[12], sdmiid[13], sdmiid[14], 
 
144
                               sdmiid[15]);
 
145
      } else {
 
146
        return NULL;
 
147
      }
131
148
    } else {
132
149
      return NULL;
133
150
    }
134
 
  } else {
135
 
    return NULL;
136
151
  }
137
152
}
138
153
 
139
154
/* Returns a string representing the firmware revision */
140
155
static gchar *jukebox_get_firmware(void)
141
156
{
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
 
      }
 
157
  if (use_mtp) {
 
158
#ifdef HAVE_LIBMTP
 
159
    return LIBMTP_Get_Deviceversion(mtpdevice);
 
160
#else
 
161
    return NULL;
 
162
#endif
 
163
  } else {
 
164
    if (pdedevice != NULL) {
 
165
      if (jukebox_firmware == NULL) {
 
166
        u_int8_t major, minor, release;
 
167
        
 
168
        if (NJB_Get_Firmware_Revision(pdedevice, &major, &minor, &release) == -1) {
 
169
          return NULL;
 
170
        }
 
171
        if (pdedevice->device_type == NJB_DEVICE_NJB1) {
 
172
          jukebox_firmware = g_strdup_printf("%u.%u", major, minor);
 
173
        } else {
 
174
          jukebox_firmware = g_strdup_printf("%u.%u.%u", major, minor, release);
 
175
        }
 
176
      }
 
177
      return jukebox_firmware;
 
178
    } else {
 
179
      return NULL;
154
180
    }
155
 
    return jukebox_firmware;
156
 
  } else {
157
 
    return NULL;
158
181
  }
159
182
}
160
183
 
161
184
/* Returns a string representing the hardware revision */
162
185
static gchar *jukebox_get_hardware(void)
163
186
{
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;
 
187
  if (use_mtp) {
 
188
#ifdef HAVE_LIBMTP
 
189
    return LIBMTP_Get_Deviceversion(mtpdevice);
 
190
#else
 
191
    return NULL;
 
192
#endif
 
193
  } else {
 
194
    if (pdedevice != NULL) {
 
195
      if (jukebox_hardware == NULL) {
 
196
        u_int8_t major, minor, release;
 
197
        
 
198
        if (NJB_Get_Hardware_Revision(pdedevice, &major, &minor, &release) == -1) {
 
199
          return NULL;
 
200
        }
 
201
        jukebox_hardware = g_strdup_printf("%u.%u.%u", major, minor, release);
170
202
      }
171
 
      jukebox_hardware = g_strdup_printf("%u.%u.%u", major, minor, release);
 
203
      return jukebox_hardware;
 
204
    } else {
 
205
      return NULL;
172
206
    }
173
 
    return jukebox_hardware;
174
 
  } else {
175
 
    return NULL;
176
207
  }
177
208
}
178
209
 
179
210
/* Returns the product name */
180
211
static gchar *jukebox_get_prodname(void)
181
212
{
182
 
  if (njb != NULL) {
183
 
    return (gchar *) NJB_Get_Device_Name(njb, 1);
 
213
  if (use_mtp) {
 
214
#ifdef HAVE_LIBMTP
 
215
    return LIBMTP_Get_Modelname(mtpdevice);
 
216
#else
 
217
    return NULL;
 
218
#endif
184
219
  } else {
185
 
    return NULL;
 
220
    if (pdedevice != NULL) {
 
221
      return (gchar *) NJB_Get_Device_Name(pdedevice, 1);
 
222
    } else {
 
223
      return NULL;
 
224
    }
186
225
  }
187
226
}
188
227
 
189
228
/* Returns data about if the power cord is connected */
190
229
static gboolean jukebox_get_power(void)
191
230
{
192
 
  if (njb != NULL) {
193
 
    int powerstat = NJB_Get_Auxpower(njb);
194
 
    if (powerstat == 1) {
195
 
      return TRUE;
 
231
  if (use_mtp) {
 
232
    // Not supported yet.
 
233
    return FALSE;
 
234
  } else {
 
235
    if (pdedevice != NULL) {
 
236
      int powerstat = NJB_Get_Auxpower(pdedevice);
 
237
      if (powerstat == 1) {
 
238
        return TRUE;
 
239
      } else {
 
240
        return FALSE;
 
241
      }
196
242
    } else {
197
243
      return FALSE;
198
244
    }
199
 
  } else {
200
 
    return FALSE;
201
245
  }
202
246
}
203
247
 
204
248
/* Returns data about if the power cord is connected */
205
249
static gboolean jukebox_get_charging(void)
206
250
{
207
 
  if (njb != NULL) {
208
 
    int chargestat = NJB_Get_Battery_Charging(njb);
209
 
    if (chargestat == 1) {
210
 
      return TRUE;
 
251
  if (use_mtp) {
 
252
    // Not supported yet.
 
253
    return TRUE;
 
254
  } else {
 
255
    if (pdedevice != NULL) {
 
256
      int chargestat = NJB_Get_Battery_Charging(pdedevice);
 
257
      if (chargestat == 1) {
 
258
        return TRUE;
 
259
      } else {
 
260
        return FALSE;
 
261
      }
211
262
    } else {
212
263
      return FALSE;
213
264
    }
214
 
  } else {
215
 
    return FALSE;
216
265
  }
217
266
}
218
267
 
219
268
/* Returns the battery level in percent */
220
269
static guint jukebox_get_battery_level(void)
221
270
{
222
 
  if (njb != NULL) {
223
 
    return NJB_Get_Battery_Level(njb);
 
271
  if (use_mtp) {
 
272
#ifdef HAVE_LIBMTP
 
273
    uint8_t max, current;
 
274
    gint ret;
 
275
    
 
276
    ret = LIBMTP_Get_Batterylevel(mtpdevice, &max, &current);
 
277
    if (ret == 0) {
 
278
      // Adjust to a scale between 0 and 100, linear interpolation
 
279
      gfloat percentage = ((gfloat) current) / ((gfloat) max);
 
280
      return (guint) (percentage * (gfloat) 100);
 
281
    } else {
 
282
      return 0;
 
283
    }
 
284
#else
 
285
    return 0;
 
286
#endif
224
287
  } else {
225
 
    return 0;
 
288
    if (pdedevice != NULL) {
 
289
      return NJB_Get_Battery_Level(pdedevice);
 
290
    } else {
 
291
      return 0;
 
292
    }
226
293
  }
227
294
}
228
295
 
240
307
{
241
308
  GtkWidget *label, *button, *dialog, *hbox;
242
309
  gchar tmp[50];
243
 
#ifdef G_HAVE_GINT64
244
310
  guint64 total, free, used;
245
311
  guint songs, playlists, datafiles;
246
 
#endif
247
312
 
248
 
  if (njb == NULL) {
 
313
  if (use_mtp) {
 
314
#ifdef HAVE_LIBMTP
 
315
    if (mtpdevice == NULL) {
 
316
      return NULL;
 
317
    }
 
318
#else
249
319
    return NULL;
 
320
#endif
 
321
  } else {
 
322
    if (pdedevice == NULL) {
 
323
      return NULL;
 
324
    }
250
325
  }
251
326
  
252
327
  // Make sure our information is up-to-date
304
379
  add_to_dialog(dialog, hbox);
305
380
  gtk_widget_show (hbox);
306
381
 
307
 
  if (njb->device_type != NJB_DEVICE_NJB1) {
 
382
  if (use_mtp || (pdedevice->device_type != NJB_DEVICE_NJB1)) {
308
383
    hbox = gtk_hbox_new(FALSE, 0);
309
384
    label = gtk_label_new (_("Hardware revision:"));
310
385
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
350
425
  add_to_dialog(dialog, hbox);
351
426
  gtk_widget_show (hbox);
352
427
  
353
 
  if (njb->device_type != NJB_DEVICE_NJB1) {
 
428
  // The pdedevice struct will not be dereferenced unless 
 
429
  // PDE is used.
 
430
  if (use_mtp || (pdedevice != NULL && pdedevice->device_type != NJB_DEVICE_NJB1)) {
354
431
    hbox = gtk_hbox_new(FALSE, 0);
355
432
    label = gtk_label_new (_("Battery level:"));
356
433
    gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
387
464
  add_to_dialog(dialog, hbox);
388
465
  gtk_widget_show (hbox);
389
466
 
390
 
#ifdef G_HAVE_GINT64
391
467
  jukebox_getusage(&total, &free, &used, &songs, &playlists, &datafiles);
392
468
 
393
469
  hbox = gtk_hbox_new(FALSE, 0);
464
540
  gtk_widget_show(label);
465
541
  add_to_dialog(dialog, hbox);
466
542
  gtk_widget_show (hbox);
467
 
#endif
468
543
  return dialog;
469
544
}
470
545
 
471
546
gchar *jukebox_get_time(void)
472
547
{
473
548
  njb_time_t *time;
474
 
  
475
 
  if (njb != NULL) {
476
 
    time = NJB_Get_Time(njb);
477
 
    if (time != NULL) {
 
549
 
 
550
  if (use_mtp) {
 
551
    // Not implemented
 
552
    return g_strdup("Not implemented");
 
553
  } else {
 
554
    if (pdedevice != NULL) {
 
555
      time = NJB_Get_Time(pdedevice);
 
556
      if (time != NULL) {
 
557
        if (jukebox_timestring != NULL) {
 
558
          g_free(jukebox_timestring);
 
559
          jukebox_timestring = NULL;
 
560
        }
 
561
        jukebox_timestring = g_strdup_printf("%u-%.2u-%.2u %.2u:%.2u:%.2u", 
 
562
                                             time->year, time->month, time->day,
 
563
                                             time->hours, time->minutes, 
 
564
                                             time->seconds);
 
565
        free(time);
 
566
      }
 
567
    } else {
478
568
      if (jukebox_timestring != NULL) {
479
569
        g_free(jukebox_timestring);
480
 
        jukebox_timestring = NULL;
481
570
      }
482
 
      jukebox_timestring = g_strdup_printf("%u-%.2u-%.2u %.2u:%.2u:%.2u", 
483
 
                                           time->year, time->month, time->day,
484
 
                                           time->hours, time->minutes, 
485
 
                                           time->seconds);
486
 
      free(time);
487
 
    }
488
 
  } else {
489
 
    if (jukebox_timestring != NULL) {
490
 
      g_free(jukebox_timestring);
491
 
    }
492
 
    jukebox_timestring = NULL;
 
571
      jukebox_timestring = NULL;
 
572
    }
 
573
    return jukebox_timestring;
493
574
  }
494
 
  return jukebox_timestring;
495
575
}
496
576
 
497
577
 
550
630
    gdk_threads_leave();
551
631
}
552
632
 
 
633
 
553
634
/* This callback handle all "cancel" buttons during
554
635
 * jukebox operations */
555
636
void cancel_jukebox_operation_click ( GtkButton *button,
565
646
 * index from 0 upwards.
566
647
 */
567
648
gchar **jukebox_discover(void) {
568
 
  int jukeboxcount;
 
649
  int device_count;
569
650
  gchar **retarray;
570
651
 
571
652
  /*
581
662
  /* Also test with NJB_UC_8859 */
582
663
  NJB_Set_Unicode(NJB_UC_UTF8);
583
664
 
584
 
  if ( NJB_Discover(njbs, 0, &jukeboxcount) == -1 ) {
 
665
  if ( NJB_Discover(pde_devices, 0, &PDE_device_count) == -1 ) {
585
666
    create_error_dialog(_("Could not try to locate jukeboxes\nUSB error?"));
586
667
    return NULL;
587
668
  }
 
669
 
 
670
#ifdef HAVE_LIBMTP
 
671
  LIBMTP_Init();
 
672
  mtpdevice = LIBMTP_Get_First_Device();
 
673
  if (mtpdevice != NULL) {
 
674
    MTP_device_count = 1;
 
675
  }
 
676
#endif
 
677
 
 
678
  device_count = PDE_device_count + MTP_device_count;
588
679
  
589
 
  if ( jukeboxcount == 0 ) {
 
680
  if ( device_count == 0 ) {
590
681
    create_error_dialog(_("No jukeboxes found on USB bus"));
591
682
    return NULL;
592
683
  }
593
684
 
594
685
  // Add a nice selection dialog here njb->device_type tells the type,
595
686
  // and owner string is useful too.
596
 
  if ( jukeboxcount >= 1) {
 
687
  if ( device_count >= 1) {
597
688
    int i;
598
689
 
599
690
    // Space for jukebox strings.
600
 
    retarray = g_malloc((jukeboxcount+1) * sizeof(gchar *));
601
 
    for (i = 0; i < jukeboxcount; i++) {
602
 
      njb = &njbs[i];
603
 
      switch (njb->device_type)
604
 
        {
605
 
        /* Put in hack to display D.A.P. Jukebox in europe? */
606
 
        case NJB_DEVICE_NJB1:
607
 
          retarray[i] = g_strdup_printf("Nomad Jukebox 1 (D.A.P.)");
608
 
          break;
609
 
        case NJB_DEVICE_NJB2:
610
 
          retarray[i] = g_strdup_printf("Nomad Jukebox 2");
611
 
          break;
612
 
        case NJB_DEVICE_NJB3:
613
 
          retarray[i] = g_strdup_printf("Nomad Jukebox 3");
614
 
          break;
615
 
        case NJB_DEVICE_NJBZEN:
616
 
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen");
617
 
          break;
618
 
        case NJB_DEVICE_NJBZEN2:
619
 
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen USB 2.0");
620
 
          break;
621
 
        case NJB_DEVICE_NJBZENNX:
622
 
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen NX");
623
 
          break;
624
 
        case NJB_DEVICE_NJBZENXTRA:
625
 
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen Xtra");
626
 
          break;
627
 
        case NJB_DEVICE_DELLDJ:
628
 
          retarray[i] = g_strdup_printf("Dell Digital Jukebox");
629
 
          break;
630
 
        case NJB_DEVICE_NJBZENTOUCH:
631
 
          retarray[i] = g_strdup_printf("Nomad Jukebox Zen Touch");
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;
642
 
        default:
643
 
          retarray[i] = g_strdup_printf(_("Unknown jukebox type"));
644
 
          break;
645
 
        }
646
 
    }
647
 
    retarray[jukeboxcount] = NULL;
648
 
    njb = NULL;
 
691
    retarray = g_malloc((device_count+1) * sizeof(gchar *));
 
692
    for (i = 0; i < PDE_device_count; i++) {
 
693
      pdedevice = &pde_devices[i];
 
694
      // This function may be called prior to opening the jukebox
 
695
      // if parameterized with type 0.
 
696
      retarray[i] = g_strdup(NJB_Get_Device_Name(pdedevice, 0));
 
697
    }
 
698
    pdedevice = NULL;
 
699
 
 
700
    // Replace with for() statement once libmtp support multiple devices
 
701
    if (MTP_device_count > 0) {
 
702
      i++;
 
703
#ifdef HAVE_LIBMTP
 
704
      retarray[i] = g_strdup(LIBMTP_Get_Modelname(mtpdevice));
 
705
      g_print("Queried %s\n", retarray[i]);
 
706
#else
 
707
      // Should not happen but to be certain ...
 
708
      retarray[i] = NULL;
 
709
#endif
 
710
    }
 
711
    retarray[device_count] = NULL;
649
712
    return retarray;
650
713
  }
651
714
 
655
718
// This selects a certain jukebox for use and sets the
656
719
// global njb variable to point to thus jukebox.
657
720
gboolean jukebox_select(gint i) {
658
 
  njb = &njbs[i];
 
721
 
 
722
  if (i < PDE_device_count) {
 
723
    g_print("This is a PDE device\n");
 
724
    pdedevice = &pde_devices[i];
659
725
  
660
 
  if (NJB_Open(njb) == -1) {
661
 
    display_njberror(njb, _("Could not open jukebox:\n"));
662
 
    return FALSE;
663
 
  }
664
 
 
665
 
  /*
666
 
   * Causing problems. Removing for now. 2004-04-12
667
 
  if ((njbid = NJB_Ping(njb)) == NULL) {
668
 
    create_error_dialog(_("Could not ping jukebox"));
669
 
    NJB_Error_Dump(stderr);
670
 
    NJB_Close(njb);
671
 
    return FALSE;
672
 
  }
673
 
  */
674
 
 
675
 
  if (NJB_Capture(njb) == -1) {
676
 
    display_njberror(njb, _("Could not capture jukebox:\n"));
677
 
    NJB_Close(njb);
678
 
    return FALSE;
679
 
  }
680
 
  njb_connected = TRUE;
 
726
    if (NJB_Open(pdedevice) == -1) {
 
727
      display_njberror(pdedevice, _("Could not open jukebox:\n"));
 
728
      return FALSE;
 
729
    }
 
730
 
 
731
    /*
 
732
     * Causing problems. Removing for now. 2004-04-12
 
733
     if ((njbid = NJB_Ping(njb)) == NULL) {
 
734
     create_error_dialog(_("Could not ping jukebox"));
 
735
     NJB_Error_Dump(stderr);
 
736
     NJB_Close(njb);
 
737
     return FALSE;
 
738
     }
 
739
    */
 
740
    
 
741
    if (NJB_Capture(pdedevice) == -1) {
 
742
      display_njberror(pdedevice, _("Could not capture jukebox:\n"));
 
743
      NJB_Close(pdedevice);
 
744
      return FALSE;
 
745
    }
 
746
    
 
747
    /* Set turbo mode according to preferences */
 
748
    if (get_prefs_turbo_mode()) {
 
749
      NJB_Set_Turbo_Mode(pdedevice, NJB_TURBO_ON);
 
750
    } else {
 
751
      NJB_Set_Turbo_Mode(pdedevice, NJB_TURBO_OFF);
 
752
    }
 
753
 
 
754
    use_mtp = FALSE;
 
755
#ifdef HAVE_LIBMTP
 
756
    mtpdevice = NULL;
 
757
#endif
 
758
  }
 
759
 
 
760
  // Else try to connect to the MTP device
 
761
  else if (MTP_device_count > 0) {
 
762
    g_print("This is an MTP device\n");
 
763
    use_mtp = TRUE;
 
764
    // This is implicitly connected to in the first library call.
 
765
    // Set njb to NULL to be certain this is the only device used.
 
766
    pdedevice = NULL;
 
767
  }
 
768
 
 
769
  // Else we have no devices
 
770
  else {
 
771
    g_print("We have no devices!\n");
 
772
    pdedevice = NULL;
 
773
    use_mtp = FALSE;
 
774
#ifdef HAVE_LIBMTP
 
775
    mtpdevice = NULL;
 
776
#endif
 
777
    return FALSE;
 
778
  }
 
779
 
 
780
  device_connected = TRUE;  
681
781
  return TRUE;
682
782
}
683
783
 
684
784
// Check if a jukebox has been selected.
685
785
gboolean jukebox_selected(void)
686
786
{
687
 
  if (!njb) {
688
 
    // g_print("njb was null.\n");
 
787
  if (use_mtp) {
 
788
#ifdef HAVE_LIBMTP
 
789
    if (mtpdevice == NULL) {
 
790
      g_print("MTP device NULL.\n");
 
791
      return FALSE;
 
792
    }
 
793
#else
689
794
    return FALSE;
 
795
#endif
 
796
  } else {
 
797
    if (pdedevice == NULL) {
 
798
      g_print("PDE device NULL.\n");
 
799
      return FALSE;
 
800
    }
690
801
  }
691
 
  if (!njb_connected) {
692
 
    // g_print("njb was not connected.\n");
 
802
  
 
803
  if (!device_connected) {
 
804
    g_print("Device was not connected.\n");
693
805
    return FALSE;
694
806
  }
695
807
  return TRUE;
698
810
/* Called at end of session to release jukebox */
699
811
void jukebox_release(void)
700
812
{
701
 
  if (njb_connected) {
702
 
    NJB_Ping(njb);
703
 
    NJB_Release(njb);
704
 
    NJB_Close(njb);
705
 
    njb_connected = FALSE;
 
813
  if (device_connected) {
 
814
    if (use_mtp) {
 
815
#ifdef HAVE_LIBMTP
 
816
      if (mtpdevice != NULL) {
 
817
        LIBMTP_Release_Device(mtpdevice);
 
818
      }
 
819
#endif
 
820
    } else {
 
821
      if (pdedevice != NULL) {
 
822
        NJB_Ping(pdedevice);
 
823
        NJB_Release(pdedevice);
 
824
        NJB_Close(pdedevice);
 
825
      }
 
826
    }
 
827
    device_connected = FALSE;
706
828
  }
707
829
}
708
830
 
710
832
 * drop down is then built from */
711
833
static void build_playlist_list()
712
834
{
713
 
  njb_playlist_t *playlist;
714
 
 
715
 
  if (jukebox_playlist != NULL) {
716
 
    GList *tmplist = jukebox_playlist;
 
835
  if (use_mtp) {
 
836
    // Not implemented
 
837
    return;
 
838
  } else {
 
839
    njb_playlist_t *playlist;
717
840
    
718
 
    while(tmplist) {
719
 
      if (tmplist->data != NULL)
720
 
        g_free(tmplist->data);
721
 
      tmplist = tmplist->next;
722
 
      tmplist = tmplist->next;
723
 
    }
724
 
    g_list_free(jukebox_playlist);
725
 
    jukebox_playlist = NULL;
726
 
  }
727
 
  NJB_Reset_Get_Playlist(njb);
728
 
  while (playlist = NJB_Get_Playlist(njb)) {
729
 
    jukebox_playlists++;
730
 
    jukebox_playlist = g_list_append(jukebox_playlist, g_strdup(playlist->name));
731
 
    jukebox_playlist = g_list_append(jukebox_playlist, GUINT_TO_POINTER(playlist->plid));
732
 
    // Dangerous?
733
 
    NJB_Playlist_Destroy(playlist);
 
841
    if (jukebox_playlist != NULL) {
 
842
      GList *tmplist = jukebox_playlist;
 
843
      
 
844
      while(tmplist) {
 
845
        if (tmplist->data != NULL)
 
846
          g_free(tmplist->data);
 
847
        tmplist = tmplist->next;
 
848
        tmplist = tmplist->next;
 
849
      }
 
850
      g_list_free(jukebox_playlist);
 
851
      jukebox_playlist = NULL;
 
852
    }
 
853
    NJB_Reset_Get_Playlist(pdedevice);
 
854
    while (playlist = NJB_Get_Playlist(pdedevice)) {
 
855
      jukebox_playlists++;
 
856
      jukebox_playlist = g_list_append(jukebox_playlist, g_strdup(playlist->name));
 
857
      jukebox_playlist = g_list_append(jukebox_playlist, GUINT_TO_POINTER(playlist->plid));
 
858
      // Dangerous?
 
859
      NJB_Playlist_Destroy(playlist);
 
860
    }
734
861
  }
735
862
}
736
863
 
737
864
 
738
865
static void create_tree_structure()
739
866
{
740
 
  njb_playlist_t *playlist;
741
 
 
742
 
  /* This is for the actual playlist tree scan */
743
 
  NJB_Reset_Get_Playlist(njb);
744
 
  while (playlist = NJB_Get_Playlist(njb)) {
745
 
    njb_playlist_track_t *track;
746
 
    gnomadplaylist_entry_t *entry;
747
 
    gint i,j;
748
 
    gchar *tmp;
749
 
 
750
 
    entry = (gnomadplaylist_entry_t *) g_malloc(sizeof(gnomadplaylist_entry_t));
751
 
    entry->name = NULL;
752
 
    entry->plid = 0;
753
 
    entry->tracklist = NULL;
754
 
    entry->name = g_strdup(playlist->name);
755
 
    tmp = g_strdup_printf("%lu", playlist->plid);
756
 
    entry->plid = tmp;
757
 
    NJB_Playlist_Reset_Gettrack(playlist);
758
 
    while (track = NJB_Playlist_Gettrack(playlist)) {
759
 
      entry->tracklist = g_slist_append(entry->tracklist,
760
 
                                        GUINT_TO_POINTER(track->trackid));
761
 
    }
762
 
    NJB_Playlist_Destroy(playlist);
763
 
    playlistlist = g_slist_append(playlistlist, (gpointer) entry);
764
 
    if (cancel_jukebox_operation)
765
 
      break;
766
 
  }
767
 
  if ( NJB_Error_Pending(njb) ) {
768
 
    NJB_Error_Dump(njb,stderr);
 
867
  if (use_mtp) {
 
868
    // Not implemented yet.
 
869
    playlistlist = NULL;
 
870
    return;
 
871
  } else {
 
872
    njb_playlist_t *playlist;
 
873
    
 
874
    /* This is for the actual playlist tree scan */
 
875
    NJB_Reset_Get_Playlist(pdedevice);
 
876
    while (playlist = NJB_Get_Playlist(pdedevice)) {
 
877
      njb_playlist_track_t *track;
 
878
      gnomadplaylist_entry_t *entry;
 
879
      gint i,j;
 
880
      gchar *tmp;
 
881
 
 
882
      entry = (gnomadplaylist_entry_t *) g_malloc(sizeof(gnomadplaylist_entry_t));
 
883
      entry->name = NULL;
 
884
      entry->plid = 0;
 
885
      entry->tracklist = NULL;
 
886
      entry->name = g_strdup(playlist->name);
 
887
      tmp = g_strdup_printf("%lu", playlist->plid);
 
888
      entry->plid = tmp;
 
889
      NJB_Playlist_Reset_Gettrack(playlist);
 
890
      while (track = NJB_Playlist_Gettrack(playlist)) {
 
891
        entry->tracklist = g_slist_append(entry->tracklist,
 
892
                                          GUINT_TO_POINTER(track->trackid));
 
893
      }
 
894
      NJB_Playlist_Destroy(playlist);
 
895
      playlistlist = g_slist_append(playlistlist, (gpointer) entry);
 
896
      if (cancel_jukebox_operation)
 
897
        break;
 
898
    }
 
899
    if ( NJB_Error_Pending(pdedevice) ) {
 
900
      NJB_Error_Dump(pdedevice,stderr);
 
901
    }
769
902
  }
770
903
}
771
904
 
867
1000
static void add_to_playlist(guint playlistid, GList *metalist, 
868
1001
                            GtkTreeStore *pltreestore, gboolean threaded)
869
1002
{
870
 
  njb_playlist_track_t *pl_track;
871
 
  njb_playlist_t *playlist;
872
 
  GList *tmplist = metalist;
873
 
  gboolean found = FALSE;
874
 
 
875
 
  if (!metalist)
 
1003
  if (use_mtp) {
 
1004
    // Not implemented yet.
876
1005
    return;
877
 
  NJB_Reset_Get_Playlist(njb);
878
 
  while (playlist = NJB_Get_Playlist(njb)) {
879
 
    /* g_print("Is it %lu?\n", playlist->plid); */
880
 
    if (playlist->plid == playlistid) {
881
 
      /* g_print("YES!\n"); */
882
 
      found = TRUE;
883
 
      break;
884
 
    }
885
 
    /* g_print("NO.\n"); */
886
 
    NJB_Playlist_Destroy(playlist);
887
 
  }
888
 
  if (!found) {
889
 
    if (threaded)
890
 
      gdk_threads_enter();
891
 
    create_error_dialog(_("Could not find playlist"));
892
 
    if (threaded)
893
 
      gdk_threads_leave();
894
 
  } else if (tmplist) {
895
 
    while(tmplist) {
896
 
      metadata_t *meta;
897
 
      u_int32_t id;
898
 
 
899
 
      meta = (metadata_t *) tmplist->data;
900
 
      id = string_to_guint(meta->path);
901
 
      /* g_print("Adding track %lu to playlist %lu\n", id, playlistid); */
902
 
      pl_track = NJB_Playlist_Track_New(id);
903
 
      NJB_Playlist_Addtrack(playlist, pl_track, NJB_PL_END);
904
 
      tmplist = g_list_next(tmplist);
905
 
    }
906
 
    if (NJB_Update_Playlist(njb, playlist) == -1)
907
 
      NJB_Error_Dump(njb, stderr);
908
 
    NJB_Playlist_Destroy(playlist);
909
 
  }
910
 
  /* Rebuild the tree afterwards */
911
 
  build_playlist_tree(pltreestore, threaded);
 
1006
  } else {
 
1007
    njb_playlist_track_t *pl_track;
 
1008
    njb_playlist_t *playlist;
 
1009
    GList *tmplist = metalist;
 
1010
    gboolean found = FALSE;
 
1011
    
 
1012
    if (!metalist)
 
1013
      return;
 
1014
    NJB_Reset_Get_Playlist(pdedevice);
 
1015
    while (playlist = NJB_Get_Playlist(pdedevice)) {
 
1016
      /* g_print("Is it %lu?\n", playlist->plid); */
 
1017
      if (playlist->plid == playlistid) {
 
1018
        /* g_print("YES!\n"); */
 
1019
        found = TRUE;
 
1020
        break;
 
1021
      }
 
1022
      /* g_print("NO.\n"); */
 
1023
      NJB_Playlist_Destroy(playlist);
 
1024
    }
 
1025
    if (!found) {
 
1026
      if (threaded)
 
1027
        gdk_threads_enter();
 
1028
      create_error_dialog(_("Could not find playlist"));
 
1029
      if (threaded)
 
1030
        gdk_threads_leave();
 
1031
    } else if (tmplist) {
 
1032
      while(tmplist) {
 
1033
        metadata_t *meta;
 
1034
        u_int32_t id;
 
1035
        
 
1036
        meta = (metadata_t *) tmplist->data;
 
1037
        id = string_to_guint(meta->path);
 
1038
        /* g_print("Adding track %lu to playlist %lu\n", id, playlistid); */
 
1039
        pl_track = NJB_Playlist_Track_New(id);
 
1040
        NJB_Playlist_Addtrack(playlist, pl_track, NJB_PL_END);
 
1041
        tmplist = g_list_next(tmplist);
 
1042
      }
 
1043
      if (NJB_Update_Playlist(pdedevice, playlist) == -1)
 
1044
        NJB_Error_Dump(pdedevice, stderr);
 
1045
      NJB_Playlist_Destroy(playlist);
 
1046
    }
 
1047
    /* Rebuild the tree afterwards */
 
1048
    build_playlist_tree(pltreestore, threaded);
 
1049
  }
912
1050
}
913
1051
 
914
1052
/* Update the usage information */
917
1055
  u_int64_t totalbytes = 0;
918
1056
  u_int64_t freebytes = 0;
919
1057
  
920
 
  if (NJB_Get_Disk_Usage (njb, &totalbytes, &freebytes) == -1) {
921
 
    NJB_Error_Dump(njb, stderr);
922
 
    return;
 
1058
  if (use_mtp) {
 
1059
#ifdef HAVE_LIBMTP
 
1060
    gint ret;
 
1061
    gchar *desc;
 
1062
    gchar *label;
 
1063
 
 
1064
    ret = LIBMTP_Get_Storageinfo(mtpdevice, &totalbytes, &freebytes, &desc, &label);
 
1065
    if (ret != 0) {
 
1066
      totalbytes = 0;
 
1067
      freebytes = 0;
 
1068
    }
 
1069
    // These are not used right now.
 
1070
    if (desc != NULL) {
 
1071
      g_free(desc);
 
1072
    }
 
1073
    if (label != NULL) {
 
1074
      g_free(label);
 
1075
    }
 
1076
#endif
 
1077
  } else {
 
1078
    if (NJB_Get_Disk_Usage (pdedevice, &totalbytes, &freebytes) == -1) {
 
1079
      NJB_Error_Dump(pdedevice, stderr);
 
1080
      return;
 
1081
    }
923
1082
  }
924
1083
 
925
1084
  jukebox_totalbytes = totalbytes;
1063
1222
  }
1064
1223
}
1065
1224
 
 
1225
#ifdef G_OS_WIN32
 
1226
// Since windows cannot thread as it should, this function
 
1227
// will be called from the main look instead.
 
1228
static gboolean call_build_all(gpointer data)
 
1229
{
 
1230
  GtkTreeStore *pltreestore = (GtkTreeStore *) data;  
 
1231
 
 
1232
  jblist_from_songhash(FALSE);
 
1233
  rebuild_datafile_list("\\");
 
1234
  build_playlist_tree(pltreestore, FALSE);
 
1235
  return FALSE;
 
1236
}
 
1237
#endif
 
1238
 
1066
1239
/**
1067
1240
 * Scanning thread - reads in the Jukebox track directory, the datafile directory
1068
1241
 * and all playlists, along with some general information about the jukebox.
 
1242
 * All the GUI related stuff is conditional and will not be used on Windows
 
1243
 * since Windows don't like that more than one thread tries to write to the
 
1244
 * widgets (i.e. the Win32 widget set is simply not natively thread safe!)
1069
1245
 */
1070
1246
gpointer scan_thread(gpointer thread_args)
1071
1247
{
 
1248
  gboolean last_item = FALSE;
1072
1249
  njb_songid_t *songtag;
1073
1250
  njb_datafile_t *datatag;
 
1251
#ifdef HAVE_LIBMTP
 
1252
  LIBMTP_track_t *mtptracks;
 
1253
  LIBMTP_track_t *mtptrack;
 
1254
#endif
 
1255
#ifndef G_OS_WIN32
1074
1256
  gchar *scan_songs = _("Scanning songs...");
1075
1257
  gchar *slow_warning = _("Scanning songs:\nYou have selected extended metadata scan,\n so scanning will be very slow.");
1076
1258
  gchar *scan_playlists = _("Scanning playlists...");
1077
1259
  gchar *scan_datafiles = _("Scanning datafiles...");
 
1260
#endif
1078
1261
  scan_thread_arg_t *args = (scan_thread_arg_t *) thread_args;
1079
1262
 
1080
1263
  /* Destroy old song hashes */
1084
1267
  jukebox_playlists = 0;
1085
1268
 
1086
1269
  /* Retrieve general information about the jukebox */
 
1270
#ifndef G_OS_WIN32
1087
1271
  gdk_threads_enter();
1088
1272
  if (get_prefs_extended_metadata()) {
1089
1273
    gtk_label_set_text(GTK_LABEL(args->label), slow_warning);
1091
1275
    gtk_label_set_text(GTK_LABEL(args->label), scan_songs);
1092
1276
  }
1093
1277
  gdk_threads_leave();
 
1278
#endif
1094
1279
  /* If it has already been read, free the old string */
1095
1280
  if (jukebox_ownerstring != NULL)
1096
1281
    g_free(jukebox_ownerstring);
1097
 
  jukebox_ownerstring = g_strdup(NJB_Get_Owner_String (njb));
 
1282
  if (use_mtp) {
 
1283
#ifdef HAVE_LIBMTP
 
1284
    jukebox_ownerstring = LIBMTP_Get_Ownername(mtpdevice);
 
1285
    g_print("Owner name: %s\n", jukebox_ownerstring);
 
1286
#else
 
1287
    jukebox_ownerstring = NULL;
 
1288
#endif
 
1289
  } else {
 
1290
    jukebox_ownerstring = g_strdup(NJB_Get_Owner_String (pdedevice));
 
1291
  }
1098
1292
  flush_usage();
1099
1293
  
 
1294
  songhash = g_hash_table_new(NULL, NULL);
 
1295
  if (!use_mtp) {
 
1296
    // Activate extended metadata mode if desired
 
1297
    // FIXME: Modify metadata editor to reflect this.
 
1298
    if (get_prefs_extended_metadata()) {
 
1299
      NJB_Get_Extended_Tags(pdedevice, 1);
 
1300
    } else {
 
1301
      NJB_Get_Extended_Tags(pdedevice, 0);
 
1302
    }
 
1303
  }
 
1304
 
1100
1305
  /* Next retrieve the track listing */
1101
 
  songhash = g_hash_table_new(NULL, NULL);
1102
 
  /* Activate extended metadata mode if desired */
1103
 
  // FIXME: Add preset. Modify metadata editor to reflect this.
1104
 
  if (get_prefs_extended_metadata()) {
1105
 
    NJB_Get_Extended_Tags(njb, 1);
 
1306
  if (use_mtp) {
 
1307
#ifdef HAVE_LIBMTP
 
1308
    mtptracks = LIBMTP_Get_Tracklisting(mtpdevice);
 
1309
    mtptrack = mtptracks;
 
1310
    if (mtptrack == NULL) {
 
1311
      last_item = TRUE;
 
1312
    }
 
1313
#else
 
1314
    last_item = TRUE;
 
1315
#endif
1106
1316
  } else {
1107
 
    NJB_Get_Extended_Tags(njb, 0);
 
1317
    NJB_Reset_Get_Track_Tag(pdedevice);
 
1318
    songtag = NJB_Get_Track_Tag(pdedevice);
 
1319
    if (songtag == NULL) {
 
1320
      last_item = TRUE;
 
1321
    }
1108
1322
  }
1109
 
  NJB_Reset_Get_Track_Tag(njb);
1110
 
  /* FIXME/IMPROVEMENT: If jukebox_songs != 0 start displaying progress bar? */
1111
 
 
1112
 
  while (songtag = NJB_Get_Track_Tag(njb)) {
 
1323
  /* 
 
1324
   * FIXME/IMPROVEMENT: If jukebox_songs != 0 start displaying progress bar? 
 
1325
   * series 3 devices can actually report the number of songs. However it
 
1326
   * is so fast anyway, so who'd want it? Most time is spent updating the
 
1327
   * widget, not communicating with the device.
 
1328
   */
 
1329
  
 
1330
  while (!last_item) {
1113
1331
    gint i;
1114
1332
    metadata_t *meta;
1115
 
    njb_songid_frame_t *frame;
1116
1333
    gchar *scanstring;
1117
1334
 
1118
1335
    jukebox_songs++;
1119
1336
    /* Number of songs scanned during scan. */
 
1337
#ifndef G_OS_WIN32
1120
1338
    scanstring = g_strdup_printf(_("%u songs scanned"), jukebox_songs);
1121
1339
    gdk_threads_enter();
1122
1340
    gtk_label_set_text(GTK_LABEL(args->label), scanstring);
1123
1341
    gdk_threads_leave();
1124
1342
    g_free(scanstring);
1125
 
 
1126
 
    NJB_Songid_Reset_Getframe(songtag);
1127
 
    /* Create a structure to hold the data in the columns */
 
1343
#endif
 
1344
    // Create a structure to hold the data in the columns
1128
1345
    meta = new_metadata_t();
1129
 
    meta->path = g_strdup_printf("%lu", songtag->trid);
1130
 
 
1131
 
    /* Loop through the song tags */
1132
 
    while(frame = NJB_Songid_Getframe(songtag)){
1133
 
      // FIXME: add progress bar for scanning?
1134
 
 
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)) {
1159
 
        meta->protect = TRUE;
1160
 
      } else {
1161
 
        g_print("Unknown frame type %s\n", frame->label);
 
1346
 
 
1347
    if (use_mtp) {
 
1348
#ifdef HAVE_LIBMTP
 
1349
      LIBMTP_track_t *tmp;
 
1350
      gchar *tmpyear;
 
1351
 
 
1352
      // Add to metadata holder
 
1353
      meta->path = g_strdup_printf("%lu", mtptrack->item_id);
 
1354
      meta->artist = g_strdup(mtptrack->artist);
 
1355
      meta->title = g_strdup(mtptrack->title);
 
1356
      meta->genre = g_strdup(mtptrack->genre);
 
1357
      meta->album = g_strdup(mtptrack->album);
 
1358
      meta->length = seconds_to_mmss((guint) (mtptrack->duration/1000));
 
1359
      meta->size = (guint32) mtptrack->filesize;
 
1360
      switch (mtptrack->codec) {
 
1361
      case LIBMTP_CODEC_WAV:
 
1362
        meta->codec = g_strdup("WAV");
 
1363
        break;
 
1364
      case LIBMTP_CODEC_MP3:
 
1365
        meta->codec = g_strdup("MP3");
 
1366
        break;
 
1367
      case LIBMTP_CODEC_WMA:
 
1368
        meta->codec = g_strdup("WMA");
 
1369
        break;
 
1370
      default:
 
1371
        meta->codec = g_strdup("N/A");
 
1372
      }
 
1373
      meta->trackno = (guint) mtptrack->tracknumber;
 
1374
      // TODO: Complicated. Fix later.
 
1375
      tmpyear = g_strdup(mtptrack->date);
 
1376
      if (tmpyear != NULL && strlen(tmpyear) > 4) {
 
1377
        // Terminate after the year
 
1378
        tmpyear[4] = '\0';
 
1379
        // Convert to a figure.
 
1380
        meta->year = string_to_guint(tmpyear);
 
1381
      }
 
1382
      if (tmpyear != NULL) {
 
1383
        g_free(tmpyear);
 
1384
      }
 
1385
      meta->filename = g_strdup(mtptrack->filename);
 
1386
      // These two are not supported yet.
 
1387
      meta->folder = NULL;
 
1388
      meta->protect = FALSE;
 
1389
      
 
1390
      tmp = mtptrack;
 
1391
      mtptrack = mtptrack->next;
 
1392
      LIBMTP_destroy_track_t(tmp);
 
1393
      if (mtptrack == NULL) {
 
1394
        last_item = TRUE;
 
1395
      }
 
1396
#else
 
1397
      last_item = TRUE;
 
1398
#endif
 
1399
    } else {
 
1400
      njb_songid_frame_t *frame;
 
1401
 
 
1402
      NJB_Songid_Reset_Getframe(songtag);
 
1403
      meta->path = g_strdup_printf("%lu", songtag->trid);
 
1404
      
 
1405
      /* Loop through the song tags */
 
1406
      while(frame = NJB_Songid_Getframe(songtag)){
 
1407
        // FIXME: add progress bar for scanning?
 
1408
        
 
1409
        if (!strcmp(frame->label, FR_ARTIST)) {
 
1410
          meta->artist = g_strdup(frame->data.strval);
 
1411
        } else if (!strcmp(frame->label, FR_TITLE)) {
 
1412
          meta->title = g_strdup(frame->data.strval);
 
1413
        } else if (!strcmp(frame->label, FR_ALBUM)) {
 
1414
          meta->album = g_strdup(frame->data.strval);
 
1415
        } else if (!strcmp(frame->label, FR_GENRE)) {
 
1416
          meta->genre = g_strdup(frame->data.strval);
 
1417
        } else if (!strcmp(frame->label, FR_LENGTH)) {
 
1418
          meta->length = seconds_to_mmss((guint) frame->data.u_int16_val);
 
1419
        } else if (!strcmp(frame->label, FR_SIZE)) {
 
1420
          meta->size = frame->data.u_int32_val;
 
1421
        } else if (!strcmp(frame->label, FR_CODEC)) {
 
1422
          // Uppercaseify it. Work in all GTK+ >= 2.0?
 
1423
          meta->codec = g_ascii_strup(frame->data.strval, -1);
 
1424
        } else if (!strcmp(frame->label, FR_TRACK)) {
 
1425
          meta->trackno = (guint) frame->data.u_int16_val;
 
1426
        } else if (!strcmp(frame->label, FR_YEAR)) {
 
1427
          meta->year = (guint) frame->data.u_int16_val;
 
1428
        } else if (!strcmp(frame->label, FR_FNAME)) {
 
1429
          meta->filename = g_strdup(frame->data.strval);
 
1430
        } else if (!strcmp(frame->label, FR_FOLDER)) {
 
1431
          meta->folder = g_strdup(frame->data.strval);
 
1432
        } else if (!strcmp(frame->label, FR_PROTECTED)) {
 
1433
          meta->protect = TRUE;
 
1434
        } else {
 
1435
          g_print("Unknown frame type %s\n", frame->label);
 
1436
        }
 
1437
      }
 
1438
      NJB_Songid_Destroy(songtag);
 
1439
      songtag = NJB_Get_Track_Tag(pdedevice);
 
1440
      if (songtag == NULL) {
 
1441
        last_item = TRUE;
1162
1442
      }
1163
1443
    }
1164
 
    /* Compensate for missing tag information */
 
1444
    // Compensate for missing tag information
1165
1445
    if (!meta->artist)
1166
1446
      meta->artist = g_strdup("<Unknown>");
1167
1447
    if (!meta->title)
1172
1452
      meta->genre = g_strdup("<Unknown>");
1173
1453
    if (!meta->length)
1174
1454
      meta->length = g_strdup("0:00");
1175
 
    /* Add to hash */
 
1455
    // Add to song hash
 
1456
    if (gnomad_debug != 0) {
 
1457
      g_print("Adding song \"%s\" to songhash...\n", meta->title);
 
1458
      dump_metadata_t(meta);
 
1459
    }
1176
1460
    g_hash_table_insert(songhash,
1177
1461
                        GUINT_TO_POINTER(string_to_guint(meta->path)),
1178
1462
                        (gpointer) meta);
1179
 
    NJB_Songid_Destroy(songtag);
1180
1463
    if (cancel_jukebox_operation)
1181
1464
      break;
1182
1465
  }
 
1466
 
1183
1467
  /* Build songlist from hash */
 
1468
#ifndef G_OS_WIN32
 
1469
  if (gnomad_debug != 0) {
 
1470
    g_print("Building songlist from hash...\n");
 
1471
  }
1184
1472
  jblist_from_songhash(TRUE);
1185
 
 
1186
 
  /* Fill in the list of data files */
1187
 
  NJB_Reset_Get_Datafile_Tag(njb);
 
1473
#endif
 
1474
 
 
1475
  // TODO: when libmtp can do this, rewrite this to support datafile
 
1476
  // scanning in a nice way.
 
1477
  if (use_mtp) {
 
1478
    // Not yet implemented
 
1479
  } else {
 
1480
    /* Fill in the list of data files */
 
1481
    NJB_Reset_Get_Datafile_Tag(pdedevice);
 
1482
  }
 
1483
 
 
1484
#ifndef G_OS_WIN32
1188
1485
  gdk_threads_enter();
1189
1486
  gtk_label_set_text(GTK_LABEL(args->label), scan_datafiles);
 
1487
  gdk_threads_leave();
 
1488
#endif
1190
1489
 
1191
1490
  /* Destroy the old datafile list */
1192
1491
  destroy_datafile_list();
1193
1492
 
1194
 
  gdk_threads_leave();
1195
 
  while (datatag = NJB_Get_Datafile_Tag (njb)) {
1196
 
    metadata_t *meta;
1197
 
    u_int64_t filesize;
1198
 
    gint i;
1199
 
    gchar *scanstring;
1200
 
 
1201
 
    jukebox_datafiles++;
1202
 
    scanstring = g_strdup_printf(_("%u data files scanned"), jukebox_datafiles);
1203
 
    gdk_threads_enter();
1204
 
    gtk_label_set_text(GTK_LABEL(args->label), scanstring);
1205
 
    gdk_threads_leave();
1206
 
    g_free(scanstring);
1207
 
 
1208
 
    meta = new_metadata_t();
1209
 
    /* g_print("Scan: filename %s, size %d\n", datatag->filename, strlen(datatag->filename)); */
1210
 
    /* Convert filesize from 64 bit unsigned integer value */
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);
 
1493
  if (use_mtp) {
 
1494
    // Not yet implemented
 
1495
  } else {
 
1496
    while (datatag = NJB_Get_Datafile_Tag (pdedevice)) {
 
1497
      metadata_t *meta;
 
1498
      u_int64_t filesize;
 
1499
      gint i;
 
1500
      gchar *scanstring;
 
1501
      
 
1502
      jukebox_datafiles++;
 
1503
    
 
1504
#ifndef G_OS_WIN32
 
1505
      scanstring = g_strdup_printf(_("%u data files scanned"), jukebox_datafiles);
 
1506
      gdk_threads_enter();
 
1507
      gtk_label_set_text(GTK_LABEL(args->label), scanstring);
 
1508
      gdk_threads_leave();
 
1509
      g_free(scanstring);
 
1510
#endif
 
1511
 
 
1512
      meta = new_metadata_t();
 
1513
      /* g_print("Scan: filename %s, size %d\n", datatag->filename, strlen(datatag->filename)); */
 
1514
      /* Convert filesize from 64 bit unsigned integer value */
 
1515
      filesize = (u_int64_t) datatag->filesize;
 
1516
      /* FIXME: here we loos all the 64-bit quality, because meta->size is 32-bit ... */
 
1517
      meta->size = (guint) filesize;
 
1518
      meta->folder = g_strdup(datatag->folder);
 
1519
      if (datatag->filesize == 0) {
 
1520
        gchar **tmp = g_strsplit(datatag->folder, "\\", 0);
 
1521
        gint veclen = vectorlength(tmp);
 
1522
        if (veclen > 2) {
 
1523
          gchar *basename = tmp[veclen-2];
 
1524
          meta->filename = g_strdup(basename);
 
1525
        } else {
 
1526
          /* This should not happen */
 
1527
          meta->filename = g_strdup("Erroneous folder");
 
1528
        }
 
1529
        g_strfreev(tmp);
1221
1530
      } else {
1222
 
        /* This should not happen */
1223
 
        meta->filename = g_strdup("Erroneous folder");
 
1531
        meta->filename = g_strdup(datatag->filename);
1224
1532
      }
1225
 
      g_strfreev(tmp);
1226
 
    } else {
1227
 
      meta->filename = g_strdup(datatag->filename);
 
1533
      /* File ID */
 
1534
      meta->path = g_strdup_printf("%lu", datatag->dfid);
 
1535
      datafilelist = g_slist_append(datafilelist, (gpointer) meta);
 
1536
      // destroy_metadata_t(meta);
 
1537
      NJB_Datafile_Destroy (datatag);
 
1538
      if (cancel_jukebox_operation)
 
1539
        break;
1228
1540
    }
1229
 
    /* File ID */
1230
 
    meta->path = g_strdup_printf("%lu", datatag->dfid);
1231
 
    datafilelist = g_slist_append(datafilelist, (gpointer) meta);
1232
 
    // destroy_metadata_t(meta);
1233
 
    NJB_Datafile_Destroy (datatag);
1234
 
    if (cancel_jukebox_operation)
1235
 
      break;
1236
 
  }
1237
 
 
1238
 
  gdk_threads_enter();
1239
 
  /* Then refill and sort everything */
1240
 
  rebuild_datafile_list("\\");
1241
 
  gtk_label_set_text(GTK_LABEL(args->label), scan_playlists);
1242
 
  gdk_threads_leave();
 
1541
 
 
1542
    /* Then refill and sort everything */
 
1543
#ifndef G_OS_WIN32
 
1544
    gdk_threads_enter();
 
1545
    rebuild_datafile_list("\\");
 
1546
    gtk_label_set_text(GTK_LABEL(args->label), scan_playlists);
 
1547
    gdk_threads_leave();
 
1548
#endif
 
1549
  }
 
1550
 
 
1551
  // The refresh the playlist tree
 
1552
  if (gnomad_debug != 0) {
 
1553
    g_print("Building playlist tree...\n");
 
1554
  }
 
1555
#ifndef G_OS_WIN32
1243
1556
  build_playlist_tree(args->pltreestore, TRUE);
1244
 
  
1245
1557
  gdk_threads_enter();
1246
1558
  gtk_widget_destroy(args->dialog);
1247
1559
  gdk_threads_leave();
 
1560
#else
 
1561
  // This will call all widget rebuilding from within the main
 
1562
  // loop instead on Windows.
 
1563
  g_idle_add(call_build_all, (gpointer) args->pltreestore);
 
1564
  gtk_widget_destroy(args->dialog);
 
1565
#endif
 
1566
  
1248
1567
  jukebox_locked = FALSE;
1249
1568
}
1250
1569
 
1251
 
/*
1252
 
 * Progression callback for transfer both back and forth to the
1253
 
 * jukebox
1254
 
 */
1255
 
static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
 
1570
static set_progress_fraction(guint64 const sent, guint64 const total)
1256
1571
{
1257
1572
  gdouble fraction;
1258
 
 
 
1573
  
1259
1574
  fraction = ((gdouble) sent / (gdouble) total);
1260
1575
  gdk_threads_enter();
1261
 
  // gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), percentage);
1262
1576
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), fraction);
1263
1577
  gdk_threads_leave();
 
1578
}
 
1579
 
 
1580
/*
 
1581
 * Progression callback for transfer both back and forth to the
 
1582
 * jukebox
 
1583
 */
 
1584
static NJB_Xfer_Callback pde_progress;
 
1585
static int pde_progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
 
1586
{
 
1587
  set_progress_fraction(sent, total);
1264
1588
  /* This cancels any jukebox operation */
1265
1589
  if (cancel_jukebox_operation)
1266
1590
    return -1;
1267
1591
  return 0;
1268
1592
}
1269
1593
 
 
1594
/**
 
1595
 * Same thing but for MTP.
 
1596
 */
 
1597
#ifdef HAVE_LIBMTP
 
1598
static LIBMTP_progressfunc_t mtp_progress;
 
1599
static int mtp_progress (uint64_t const sent, uint64_t const total,
 
1600
                         void const * const data)
 
1601
{
 
1602
  set_progress_fraction(sent, total);
 
1603
  // TODO: cancellation
 
1604
}
 
1605
#endif
 
1606
 
 
1607
#ifdef G_OS_WIN32
 
1608
// Windows don't like it when other threads write to the GUI
 
1609
// so we do it here from the main loop instead.
 
1610
gboolean call_fill_in_dir(gpointer data)
 
1611
{
 
1612
  guint what = (guint) data;
 
1613
 
 
1614
  fill_in_dir(what, get_current_dir());
 
1615
  return FALSE;
 
1616
}
 
1617
#endif
1270
1618
 
1271
1619
/***********************************************************************************/
1272
1620
/* Transferring music from jukebox thread                                          */
1289
1637
      /* Get title from filelist */
1290
1638
      jbmeta = (metadata_t *) tmplist->data;
1291
1639
      filename = compose_filename(jbmeta);
 
1640
#ifndef G_OS_WIN32
1292
1641
      tmpfname = filename_fromutf8(filename);
 
1642
#else
 
1643
      // Windows locale version of filename, since libnjb does not
 
1644
      // convert filenames into widechar unicode...
 
1645
      tmpfname = g_locale_from_utf8(filename,-1,NULL,NULL,NULL);
 
1646
#endif
1293
1647
      create_dirs(tmpfname);
1294
1648
      id = string_to_guint(jbmeta->path);
1295
1649
      gdk_threads_enter();
1298
1652
      gdk_threads_leave();
1299
1653
      /* g_print("Transferring %s...\n", file); */
1300
1654
      /* Transfer track */
1301
 
      if ( NJB_Get_Track(njb, id, jbmeta->size, tmpfname, progress, NULL) == -1 ) {
1302
 
        NJB_Error_Dump(njb, stderr);
1303
 
        g_free(filename);
1304
 
        goto clean_up_and_return;
 
1655
      if (use_mtp) {
 
1656
#ifdef HAVE_LIBMTP
 
1657
        int ret;
 
1658
        ret = LIBMTP_Get_Track_To_File(mtpdevice, id, tmpfname, mtp_progress, NULL);
 
1659
        if (ret != 0) {
 
1660
          g_free(filename);
 
1661
          g_free(tmpfname);
 
1662
          goto clean_up_and_return;
 
1663
        }
 
1664
#endif
 
1665
      } else {
 
1666
        if ( NJB_Get_Track(pdedevice, id, jbmeta->size, tmpfname, pde_progress, NULL) == -1 ) {
 
1667
          NJB_Error_Dump(pdedevice, stderr);
 
1668
          g_free(filename);
 
1669
          g_free(tmpfname);
 
1670
          goto clean_up_and_return;
 
1671
        }
1305
1672
      }
1306
1673
      /* Copy all metadata, but replace jukebox ID with path */
1307
1674
      meta = clone_metadata_t(jbmeta);
1321
1688
      gdk_threads_enter();
1322
1689
      add_metadata_to_model(meta, HD_LIST);
1323
1690
      gdk_flush(); // Time consuming?
1324
 
      // FIXME: trig resort gtk_clist_sort(GTK_CLIST(args->harddiskclist));
1325
1691
      gdk_threads_leave();
1326
1692
      /* Destroy metadata */
1327
1693
      destroy_metadata_t(meta);
1331
1697
 clean_up_and_return:
1332
1698
 
1333
1699
  jukebox_locked = FALSE;
 
1700
#ifndef G_OS_WIN32
1334
1701
  gdk_threads_enter();
1335
1702
  gtk_widget_destroy(args->dialog);
1336
1703
  fill_in_dir(HD_LIST, get_current_dir());
1337
1704
  gdk_threads_leave();
 
1705
#else
 
1706
  gtk_widget_destroy(args->dialog);
 
1707
  g_idle_add(call_fill_in_dir, (gpointer) HD_LIST);
 
1708
#endif
1338
1709
  /* Free the memory used by the list */
1339
1710
  destroy_metalist(args->metalist);
1340
1711
}
1364
1735
      gdk_threads_leave();
1365
1736
      /* g_print("Transfering %s\n", jbmeta->filename); */
1366
1737
      /* Transfer file */
 
1738
#ifndef G_OS_WIN32
1367
1739
      tmpfname = filename_fromutf8(jbmeta->filename);
1368
 
      if (NJB_Get_File(njb, id, jbmeta->size, tmpfname, progress, NULL) == -1) {
 
1740
#else
 
1741
      // Windows locale version of filename, since libnjb does not
 
1742
      // convert filenames into unicode...
 
1743
      tmpfname = g_locale_from_utf8(jbmeta->filename,-1,NULL,NULL,NULL);
 
1744
#endif
 
1745
      if (NJB_Get_File(pdedevice, id, jbmeta->size, tmpfname, pde_progress, NULL) == -1) {
1369
1746
        g_free(tmpfname);
1370
 
        NJB_Error_Dump(njb, stderr);
 
1747
        NJB_Error_Dump(pdedevice, stderr);
1371
1748
        goto data_error;
1372
1749
      }
1373
1750
      g_free(tmpfname);
1383
1760
 
1384
1761
  destroy_metalist(args->metalist);
1385
1762
  jukebox_locked = FALSE;
 
1763
#ifndef G_OS_WIN32
1386
1764
  gdk_threads_enter();
1387
1765
  gtk_widget_destroy(args->dialog);
1388
1766
  fill_in_dir(HDDATA_LIST, get_current_dir());
1389
1767
  gdk_threads_leave();
 
1768
#else
 
1769
  gtk_widget_destroy(args->dialog);
 
1770
  g_idle_add(call_fill_in_dir, (gpointer) HDDATA_LIST);
 
1771
#endif
1390
1772
}
1391
1773
 
1392
1774
 
1416
1798
      metadata_t *hdmeta;
1417
1799
      metadata_t *jbmeta;
1418
1800
      gint protectint;
1419
 
      gchar *tmpfname;
 
1801
      gchar *tmpfname = NULL;
 
1802
      gchar *tmpfname8 = NULL;
1420
1803
      gchar *tmp;
1421
1804
      u_int32_t id, length;
1422
 
      njb_songid_t *songid;
1423
 
      njb_songid_frame_t *frame;
1424
 
      
 
1805
      gboolean clone_and_strip = FALSE;
 
1806
 
1425
1807
      hdmeta = (metadata_t *) templist->data;
1426
1808
      // g_print("Storing %s on jukebox...\n", hdmeta->filename);
 
1809
 
 
1810
      // If we want to remove ID3 tags then we fix that here
 
1811
      if (get_prefs_id3remove() && !strcmp(hdmeta->codec, "MP3")) {
 
1812
        clone_and_strip = TRUE;
 
1813
        tmpfname8 = g_build_filename(tmpdirname, hdmeta->filename, NULL);
 
1814
        tmpfname = filename_fromutf8(tmpfname8);
 
1815
        /* Call the filesystem to clone the file and remove the tag */
 
1816
        if (!clone_and_strip_id3(hdmeta->path, tmpfname8)) {
 
1817
          goto hd2jb_cleanup;
 
1818
        }
 
1819
      } else {
 
1820
        tmpfname = filename_fromutf8(hdmeta->path);
 
1821
      }
 
1822
      
1427
1823
      gdk_threads_enter();
1428
1824
      tmp = g_strdup_printf("%s - %s", hdmeta->artist, hdmeta->title);
1429
1825
      gtk_label_set_text(GTK_LABEL(args->label), tmp);
1432
1828
      gdk_threads_leave();
1433
1829
      length = mmss_to_seconds(hdmeta->length);
1434
1830
      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
 
 
1462
 
      /* Optionally remove ID3 tag */
1463
 
      if (get_prefs_id3remove() && !strcmp(hdmeta->codec, "MP3")) {
1464
 
        gchar *tmpfname8;
1465
 
        tmpfname8 = g_build_filename(tmpdirname, hdmeta->filename, NULL);
1466
 
        tmpfname = filename_fromutf8(tmpfname8);
1467
 
 
1468
 
        /* Call the filesystem to clone the file and remove the tag */
1469
 
        if (clone_and_strip_id3(hdmeta->path, tmpfname8)) {
1470
 
          // g_print("Transferring track.\n");
1471
 
          if (NJB_Send_Track (njb, tmpfname, songid,
1472
 
                              progress, NULL, 
1473
 
                              &id) == -1) {
1474
 
            NJB_Error_Dump(njb, stderr);
1475
 
            goto hd2jb_cleanup;
1476
 
          }
1477
 
          /* Afterwards delete the tempfile */
1478
 
          unlink(tmpfname);
1479
 
          g_free(tmpfname8);
1480
 
        }
 
1831
 
 
1832
      if (use_mtp) {
 
1833
#ifdef HAVE_LIBMTP
 
1834
        LIBMTP_track_t *trackmeta = LIBMTP_new_track_t();
 
1835
        int ret;
 
1836
        
 
1837
        // This will be filled in, dummy set to 0.
 
1838
        trackmeta->item_id = 0;
 
1839
        trackmeta->title = g_strdup(hdmeta->title);
 
1840
        trackmeta->album = g_strdup(hdmeta->album);
 
1841
        trackmeta->artist = g_strdup(hdmeta->artist);
 
1842
        trackmeta->genre = g_strdup(hdmeta->genre);
 
1843
        trackmeta->date = g_strdup_printf("%4d0101T0000.0", hdmeta->year);
 
1844
        trackmeta->filename = g_strdup(hdmeta->filename);
 
1845
        trackmeta->tracknumber = hdmeta->trackno;
 
1846
        // This need to be in milliseconds
 
1847
        trackmeta->duration = length * 1000;
 
1848
        trackmeta->filesize = get_64bit_file_size(tmpfname);
 
1849
        if (!strcmp(hdmeta->codec, "WAV")) {
 
1850
          trackmeta->codec = LIBMTP_CODEC_WAV;
 
1851
        } else if (!strcmp(hdmeta->codec, "MP3")) {
 
1852
          trackmeta->codec = LIBMTP_CODEC_MP3;
 
1853
        } else if (!strcmp(hdmeta->codec, "WMA")) {
 
1854
          trackmeta->codec = LIBMTP_CODEC_WMA;
 
1855
        } else {
 
1856
          trackmeta->codec = LIBMTP_CODEC_UNKNOWN;
 
1857
        }
 
1858
        // TODO: Discarding return value as for now
 
1859
        g_print("Transferring MTP track...\n");
 
1860
        ret = LIBMTP_Send_Track_From_File(mtpdevice, tmpfname, trackmeta,
 
1861
                                          mtp_progress, NULL);
 
1862
        if (ret != 0) {
 
1863
          g_print("Error sending file \"%s\" to MTP device!\n", tmpfname);
 
1864
          goto hd2jb_cleanup;
 
1865
        }
 
1866
        // Fetch new track ID.
 
1867
        id = trackmeta->item_id;
 
1868
        LIBMTP_destroy_track_t(trackmeta);
 
1869
#endif
1481
1870
      } else {
1482
 
        tmpfname = filename_fromutf8(hdmeta->path);
1483
 
        if (NJB_Send_Track (njb, tmpfname, songid,
1484
 
                            progress, NULL, 
 
1871
        njb_songid_t *songid;
 
1872
        njb_songid_frame_t *frame;
 
1873
 
 
1874
        songid = NJB_Songid_New();
 
1875
        frame = NJB_Songid_Frame_New_Codec(hdmeta->codec);
 
1876
        NJB_Songid_Addframe(songid, frame);
 
1877
        // libnjb will add this if not added by hand
 
1878
        // frame = NJB_Songid_Frame_New_Filesize(filesize);
 
1879
        // NJB_Songid_Addframe(songid, frame);
 
1880
        frame = NJB_Songid_Frame_New_Title(hdmeta->title);
 
1881
        NJB_Songid_Addframe(songid, frame);
 
1882
        frame = NJB_Songid_Frame_New_Album(hdmeta->album);
 
1883
        NJB_Songid_Addframe(songid, frame);
 
1884
        frame = NJB_Songid_Frame_New_Artist(hdmeta->artist);
 
1885
        NJB_Songid_Addframe(songid, frame);
 
1886
        frame = NJB_Songid_Frame_New_Genre(hdmeta->genre);
 
1887
        NJB_Songid_Addframe(songid, frame);
 
1888
        frame = NJB_Songid_Frame_New_Year(hdmeta->year);
 
1889
        NJB_Songid_Addframe(songid, frame);
 
1890
        frame = NJB_Songid_Frame_New_Tracknum(hdmeta->trackno);
 
1891
        NJB_Songid_Addframe(songid, frame);
 
1892
        frame = NJB_Songid_Frame_New_Length(length);
 
1893
        NJB_Songid_Addframe(songid, frame);
 
1894
        frame = NJB_Songid_Frame_New_Filename(hdmeta->filename);
 
1895
        NJB_Songid_Addframe(songid, frame);
 
1896
        if (hdmeta->protect) {
 
1897
          frame = NJB_Songid_Frame_New_Protected(1);
 
1898
          NJB_Songid_Addframe(songid, frame);
 
1899
        }
 
1900
        
 
1901
        // g_print("Transferring track.\n");
 
1902
        if (NJB_Send_Track (pdedevice, tmpfname, songid,
 
1903
                            pde_progress, NULL, 
1485
1904
                            &id) == -1) {
1486
 
          NJB_Error_Dump(njb, stderr);
 
1905
          NJB_Error_Dump(pdedevice, stderr);
1487
1906
          goto hd2jb_cleanup;
1488
1907
        }
 
1908
        // FIXME: do we need to destroy songid? Memleak?
 
1909
        /* g_print("%s stored on jukebox with ID: %lu\n", file, id); */
1489
1910
      }
1490
 
      /* g_print("%s stored on jukebox with ID: %lu\n", file, id); */
1491
 
      g_free(tmpfname);
 
1911
 
1492
1912
      jukebox_songs++;
1493
1913
      /* Add to disk listbox and re-sort */
1494
1914
      /* Add correct trackid in col 7, add the row */
1504
1924
                          GUINT_TO_POINTER(id),
1505
1925
                          (gpointer) jbmeta);
1506
1926
      new_metalist = g_list_append (new_metalist, jbmeta);
 
1927
      if (clone_and_strip) {
 
1928
        /* Afterwards delete the tempfile */
 
1929
        unlink(tmpfname);
 
1930
        if (tmpfname8 != NULL) {
 
1931
          g_free(tmpfname8);
 
1932
        }
 
1933
      }
 
1934
      if (tmpfname != NULL) {
 
1935
        g_free(tmpfname);
 
1936
      }
1507
1937
      templist = g_list_next(templist);
1508
1938
    }
1509
 
  // Remove temporary directory for tag-stripped files.
1510
 
  if (get_prefs_id3remove()) {
1511
 
    rmdir(tmpdirname);
1512
 
    // g_free(tmpdirname);
1513
 
  }
1514
 
  /* At last add the tracks to default playlists if
1515
 
   * any such are selected. */
 
1939
  /*
 
1940
   * At last add the tracks to default playlists if
 
1941
   * any such are selected.
 
1942
   */
1516
1943
  if (args->playlists != NULL) {
1517
1944
    add_tracks_to_playlists(args->playlists, new_metalist, args->pltreestore, TRUE);
1518
1945
  }
1521
1948
  
1522
1949
 hd2jb_cleanup:
1523
1950
 
1524
 
  destroy_metalist(args->metalist);
 
1951
  // Remove temporary directory for tag-stripped files.
 
1952
  if (get_prefs_id3remove()) {
 
1953
    rmdir(tmpdirname);
 
1954
  }
 
1955
  
 
1956
  jukebox_locked = FALSE;
1525
1957
  g_list_free(args->playlists);
 
1958
  destroy_metalist(args->metalist);
 
1959
#ifndef G_OS_WIN32
1526
1960
  gdk_threads_enter();
1527
1961
  gtk_widget_destroy(args->dialog);
1528
 
  jukebox_locked = FALSE;
1529
1962
  gdk_threads_leave();
 
1963
#else
 
1964
  gtk_widget_destroy(args->dialog);
 
1965
#endif
1530
1966
  g_free(args);
1531
1967
}
1532
1968
 
1562
1998
      gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), (gfloat) 0);
1563
1999
      gdk_threads_leave();
1564
2000
      tmpfname = filename_fromutf8(hdmeta->path);
1565
 
      if (NJB_Send_File (njb, tmpfname, hdmeta->filename, folder, progress, NULL, &id) == -1) {
 
2001
      if (NJB_Send_File (pdedevice, tmpfname, hdmeta->filename, folder, pde_progress, NULL, &id) == -1) {
1566
2002
        g_free(tmpfname);
1567
 
        NJB_Error_Dump(njb, stderr);
 
2003
        NJB_Error_Dump(pdedevice, stderr);
1568
2004
        goto hd2jb_data_cleanup;
1569
2005
      }
1570
2006
      g_free(tmpfname);
1592
2028
 
1593
2029
 hd2jb_data_cleanup:
1594
2030
 
 
2031
  jukebox_locked = FALSE;
1595
2032
  destroy_metalist(args->metalist);
 
2033
#ifndef G_OS_WIN32
1596
2034
  gdk_threads_enter();
1597
2035
  gtk_widget_destroy(args->dialog);
1598
 
  jukebox_locked = FALSE;
1599
2036
  gdk_threads_leave();
 
2037
#else
 
2038
  gtk_widget_destroy(args->dialog);
 
2039
#endif
 
2040
  g_free(args);
1600
2041
}
1601
2042
 
1602
2043
 
1620
2061
    
1621
2062
    meta = (metadata_t *) tmplist->data;
1622
2063
    id = string_to_guint(meta->path);
1623
 
    NJB_Reset_Get_Playlist(njb);
1624
 
 
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);
1631
 
      }
1632
 
      NJB_Playlist_Destroy(playlist);
1633
 
    }
1634
 
    if (NJB_Error_Pending(njb)) {
1635
 
      NJB_Error_Dump(njb, stderr);
 
2064
 
 
2065
    if (use_mtp) {
 
2066
      // Not implemented yet
 
2067
    } else {
 
2068
      NJB_Reset_Get_Playlist(pdedevice);
 
2069
 
 
2070
      while (playlist = NJB_Get_Playlist(pdedevice)) {
 
2071
        NJB_Playlist_Deltrack_TrackID(playlist, id);
 
2072
        /* If the playlist changed, update it */
 
2073
        if (playlist->_state == NJB_PL_CHTRACKS) {
 
2074
          if (NJB_Update_Playlist(pdedevice, playlist) == -1)
 
2075
            NJB_Error_Dump(pdedevice, stderr);
 
2076
        }
 
2077
        NJB_Playlist_Destroy(playlist);
 
2078
      }
 
2079
      if (NJB_Error_Pending(pdedevice)) {
 
2080
        NJB_Error_Dump(pdedevice, stderr);
 
2081
      }
1636
2082
    }
1637
2083
    tmplist = g_list_next(tmplist);
1638
2084
  }
1656
2102
    id = string_to_guint(meta->path);
1657
2103
    /* g_print("Deleting: %u from jukebox library\n", id); */
1658
2104
    if (id) {
1659
 
      if ( NJB_Delete_Datafile(njb, id) == -1 ) {
1660
 
        NJB_Error_Dump(njb, stderr);
 
2105
      if (use_mtp) {
 
2106
        // Not implemented yet
 
2107
      } else {
 
2108
        if ( NJB_Delete_Datafile(pdedevice, id) == -1 ) {
 
2109
          NJB_Error_Dump(pdedevice, stderr);
 
2110
        }
1661
2111
      }
1662
2112
      // Remove it from the datafile cache
1663
2113
      remove_from_datafile_list(meta->path);
1691
2141
    /* g_print("Deleting: %lu from jukebox library\n", id); */
1692
2142
    if (id) {
1693
2143
      metadata_t *tmpmeta;
1694
 
      if ( NJB_Delete_Track(njb, id) == -1 ) {
1695
 
        NJB_Error_Dump(njb, stderr);
 
2144
      
 
2145
      if (use_mtp) {
 
2146
#ifdef HAVE_LIBMTP
 
2147
        int ret;
 
2148
        // TODO: what do we do if the returncode is != 0?
 
2149
        ret = LIBMTP_Delete_Track(mtpdevice, id);
 
2150
#endif
 
2151
      } else {
 
2152
        if ( NJB_Delete_Track(pdedevice, id) == -1 ) {
 
2153
          NJB_Error_Dump(pdedevice, stderr);
 
2154
        }
1696
2155
      }
1697
2156
      /* Remove song from hash */
1698
2157
      tmpmeta = (metadata_t *) g_hash_table_lookup(songhash, GUINT_TO_POINTER(id));
1724
2183
  /* Set metadata on the file with ID id */
1725
2184
  u_int32_t id, length;
1726
2185
  metadata_t *tmpmeta;
1727
 
  njb_songid_t *songid;
1728
 
  njb_songid_frame_t *frame;
1729
2186
 
1730
2187
  id = string_to_guint(meta->path);
1731
2188
  length = mmss_to_seconds(meta->length);
1738
2195
    return;
1739
2196
  }
1740
2197
 
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
 
  }
 
2198
  if (use_mtp) {
 
2199
#ifdef HAVE_LIBMTP
 
2200
    LIBMTP_track_t *trackmeta = LIBMTP_new_track_t();
 
2201
    int ret;
 
2202
 
 
2203
    trackmeta->item_id = id;
 
2204
    trackmeta->title = g_strdup(meta->title);
 
2205
    trackmeta->album = g_strdup(meta->album);
 
2206
    trackmeta->artist = g_strdup(meta->artist);
 
2207
    trackmeta->genre = g_strdup(meta->genre);
 
2208
    trackmeta->date = g_strdup_printf("%4d0101T0000.0", meta->year);
 
2209
    // This cannot be set this way but send it anyway
 
2210
    trackmeta->filename = g_strdup(meta->filename);
 
2211
    trackmeta->tracknumber = meta->trackno;
 
2212
    // This need to be in milliseconds
 
2213
    trackmeta->duration = length * 1000;
 
2214
    // This cannot be set this way but send it anyway
 
2215
    trackmeta->filesize = (uint64_t) meta->size;
 
2216
    if (!strcmp(meta->codec, "WAV")) {
 
2217
      trackmeta->codec = LIBMTP_CODEC_WAV;
 
2218
    } else if (!strcmp(meta->codec, "MP3")) {
 
2219
      trackmeta->codec = LIBMTP_CODEC_MP3;
 
2220
    } else if (!strcmp(meta->codec, "WMA")) {
 
2221
      trackmeta->codec = LIBMTP_CODEC_WMA;
 
2222
    } else {
 
2223
      trackmeta->codec = LIBMTP_CODEC_UNKNOWN;
 
2224
    }
 
2225
    // TODO: Discarding return value as for now
 
2226
    ret = LIBMTP_Update_Track_Metadata(mtpdevice, trackmeta);
 
2227
    LIBMTP_destroy_track_t(trackmeta);
 
2228
#endif
 
2229
  } else {
 
2230
    njb_songid_t *songid;
 
2231
    njb_songid_frame_t *frame;
 
2232
 
 
2233
    songid = NJB_Songid_New();
 
2234
    /* On NJB1 incremental update is not possible! */
 
2235
    if (pdedevice->device_type == NJB_DEVICE_NJB1) {
 
2236
      // Sometimes it fails with old codec names.
 
2237
      gchar *tmpcodec = g_ascii_strup(meta->codec, -1);
 
2238
      
 
2239
      frame = NJB_Songid_Frame_New_Codec(tmpcodec);
 
2240
      g_free(tmpcodec);
 
2241
      NJB_Songid_Addframe(songid, frame);
 
2242
      frame = NJB_Songid_Frame_New_Filesize(meta->size);
 
2243
      NJB_Songid_Addframe(songid, frame);
 
2244
    }
 
2245
    /* Ultimately only send altered fields to NJB series 3 */
 
2246
    frame = NJB_Songid_Frame_New_Title(meta->title);
 
2247
    NJB_Songid_Addframe(songid, frame);
 
2248
    frame = NJB_Songid_Frame_New_Album(meta->album);
 
2249
    NJB_Songid_Addframe(songid, frame);
 
2250
    frame = NJB_Songid_Frame_New_Artist(meta->artist);
 
2251
    NJB_Songid_Addframe(songid, frame);
 
2252
    frame = NJB_Songid_Frame_New_Genre(meta->genre);
 
2253
    NJB_Songid_Addframe(songid, frame);
 
2254
    frame = NJB_Songid_Frame_New_Year(meta->year);
 
2255
    NJB_Songid_Addframe(songid, frame);
 
2256
    frame = NJB_Songid_Frame_New_Tracknum(meta->trackno);
 
2257
    NJB_Songid_Addframe(songid, frame);
 
2258
    frame = NJB_Songid_Frame_New_Length(length);
 
2259
    NJB_Songid_Addframe(songid, frame);
 
2260
    /*
 
2261
     * These will only be set if the jukebox has retrieved
 
2262
     * extended metadata so that the user has a chance of modifying
 
2263
     * an existing tag, not just overwrite what is already there.
 
2264
     */
 
2265
    if (get_prefs_extended_metadata()) {
 
2266
      if (meta->filename != NULL) {
 
2267
        frame = NJB_Songid_Frame_New_Filename(meta->filename);
 
2268
        NJB_Songid_Addframe(songid, frame);
 
2269
      }
 
2270
      if (meta->folder != NULL) {
 
2271
        frame = NJB_Songid_Frame_New_Folder(meta->folder);
 
2272
        NJB_Songid_Addframe(songid, frame);
 
2273
      }
 
2274
    }
 
2275
    if (meta->protect) {
 
2276
      frame = NJB_Songid_Frame_New_Protected(1);
 
2277
      NJB_Songid_Addframe(songid, frame);
 
2278
    }
 
2279
    
 
2280
    if (NJB_Replace_Track_Tag(pdedevice, id, songid) == -1) {
 
2281
      NJB_Error_Dump(pdedevice, stderr);
 
2282
    }
 
2283
  }
 
2284
 
1791
2285
  /* Replace the data in the hash table */
1792
2286
  tmpmeta = (metadata_t *) g_hash_table_lookup(songhash,
1793
2287
                                               GUINT_TO_POINTER(id));
1826
2320
    return 0;
1827
2321
  }
1828
2322
  if (NJB_Playlist_Set_Name(playlist, plname) == -1) {
1829
 
    NJB_Error_Dump(njb, stderr);
 
2323
    NJB_Error_Dump(pdedevice, stderr);
1830
2324
    return 0;
1831
2325
  }
1832
2326
  jukebox_locked = TRUE;
1833
2327
  /* g_print("Calling NJB_Update_Playlist\n"); */
1834
 
  if (NJB_Update_Playlist(njb, playlist) == -1) {
 
2328
  if (NJB_Update_Playlist(pdedevice, playlist) == -1) {
1835
2329
    gdk_threads_enter();
1836
2330
    create_error_dialog(_("Could not create playlist"));
1837
2331
    gdk_threads_leave();
1838
 
    NJB_Error_Dump(njb, stderr);
 
2332
    NJB_Error_Dump(pdedevice, stderr);
1839
2333
  }
1840
2334
  jukebox_playlists++;
1841
2335
  plid = playlist->plid;
1850
2344
void jukebox_delete_playlist(guint plid)
1851
2345
{
1852
2346
  jukebox_locked = TRUE;
1853
 
  if (NJB_Delete_Playlist(njb, plid) == -1) {
 
2347
  if (NJB_Delete_Playlist(pdedevice, plid) == -1) {
1854
2348
    gdk_threads_enter();
1855
2349
    create_error_dialog(_("Could not delete playlist"));
1856
2350
    gdk_threads_leave();
1857
 
    NJB_Error_Dump(njb, stderr);
 
2351
    NJB_Error_Dump(pdedevice, stderr);
1858
2352
  }
1859
2353
  jukebox_playlists--;
1860
2354
  build_playlist_list();
1868
2362
  gboolean found = FALSE;
1869
2363
  
1870
2364
  jukebox_locked = TRUE;
1871
 
  NJB_Reset_Get_Playlist(njb);
1872
 
  while (playlist = NJB_Get_Playlist(njb)) {
 
2365
  NJB_Reset_Get_Playlist(pdedevice);
 
2366
  while (playlist = NJB_Get_Playlist(pdedevice)) {
1873
2367
    /* g_print("Playlist %u == Playlist %u ... ",playlist->plid, plid); */
1874
2368
    if (playlist->plid == plid) {
1875
2369
      /* g_print("Yes.\n"); */
1881
2375
  }
1882
2376
  if (found) {
1883
2377
    if (NJB_Playlist_Set_Name(playlist, name) == -1) {
1884
 
      NJB_Error_Dump(njb, stderr);
 
2378
      NJB_Error_Dump(pdedevice, stderr);
1885
2379
    } else {
1886
 
      if (NJB_Update_Playlist(njb, playlist) == -1) {
 
2380
      if (NJB_Update_Playlist(pdedevice, playlist) == -1) {
1887
2381
        if (threaded)
1888
2382
          gdk_threads_enter();
1889
2383
        create_error_dialog(_("Could not rename playlist"));
1890
2384
        if (threaded)
1891
2385
          gdk_threads_leave();
1892
 
        NJB_Error_Dump(njb, stderr);
 
2386
        NJB_Error_Dump(pdedevice, stderr);
1893
2387
      }
1894
2388
    }
1895
2389
    NJB_Playlist_Destroy(playlist);
1933
2427
    
1934
2428
    jukebox_locked = TRUE;
1935
2429
    // g_print("randomizing playlist %lu\n", plist);
1936
 
    NJB_Reset_Get_Playlist(njb);
1937
 
    while (playlist = NJB_Get_Playlist(njb)) {
 
2430
    NJB_Reset_Get_Playlist(pdedevice);
 
2431
    while (playlist = NJB_Get_Playlist(pdedevice)) {
1938
2432
      // g_print("Is it %lu?\n", playlist->plid);
1939
2433
      if (playlist->plid == plist) {
1940
2434
        // g_print("YES!");
1991
2485
          
1992
2486
          // Mark it shuffled
1993
2487
          playlist->_state = NJB_PL_CHTRACKS;
1994
 
          if (NJB_Update_Playlist(njb, playlist) == -1)
1995
 
            NJB_Error_Dump(njb, stderr);
 
2488
          if (NJB_Update_Playlist(pdedevice, playlist) == -1)
 
2489
            NJB_Error_Dump(pdedevice, stderr);
1996
2490
          new_plid = playlist->plid;
1997
2491
        } else {
1998
2492
          g_print("Playlist is too small to shuffle\n");
2021
2515
 
2022
2516
    jukebox_locked = TRUE;
2023
2517
    // g_print("Removing track: %lu from playlist %lu\n", trackid, plist);
2024
 
    NJB_Reset_Get_Playlist(njb);
2025
 
    while (playlist = NJB_Get_Playlist(njb)) {
 
2518
    NJB_Reset_Get_Playlist(pdedevice);
 
2519
    while (playlist = NJB_Get_Playlist(pdedevice)) {
2026
2520
      // g_print("Is it in %lu? ...", playlist->plid);
2027
2521
      if (playlist->plid == plist) {
2028
2522
        // g_print("YES!\n");
2056
2550
      }
2057
2551
      if (playlist->_state==NJB_PL_CHTRACKS) {
2058
2552
        /* g_print("Called NJB_Update_Playlist...\n"); */
2059
 
        if (NJB_Update_Playlist(njb, playlist) == -1)
2060
 
          NJB_Error_Dump(njb, stderr);
 
2553
        if (NJB_Update_Playlist(pdedevice, playlist) == -1)
 
2554
          NJB_Error_Dump(pdedevice, stderr);
2061
2555
        new_plid = playlist->plid;
2062
2556
      }
2063
2557
      NJB_Playlist_Destroy(playlist);
2082
2576
void jukebox_set_ownerstring(gchar *owner)
2083
2577
{
2084
2578
  jukebox_locked = TRUE;
2085
 
  NJB_Set_Owner_String (njb, owner);
2086
 
  if (jukebox_ownerstring != NULL)
2087
 
    g_free(jukebox_ownerstring);
2088
 
  jukebox_ownerstring = g_strdup(owner);
2089
 
  /* The ownerstring max length is 64 bytes
2090
 
   * including the null-terminator according
2091
 
   * to the protocol specification */
2092
 
  if (strlen(jukebox_ownerstring) >= OWNER_STRING_LENGTH)
2093
 
    jukebox_ownerstring[OWNER_STRING_LENGTH-1] = '\0';
 
2579
  if (use_mtp) {
 
2580
    // Not implemented yet
 
2581
  } else {
 
2582
    NJB_Set_Owner_String (pdedevice, owner);
 
2583
    if (jukebox_ownerstring != NULL)
 
2584
      g_free(jukebox_ownerstring);
 
2585
    jukebox_ownerstring = g_strdup(owner);
 
2586
    /* The ownerstring max length is 64 bytes
 
2587
     * including the null-terminator according
 
2588
     * to the protocol specification */
 
2589
    if (strlen(jukebox_ownerstring) >= OWNER_STRING_LENGTH)
 
2590
      jukebox_ownerstring[OWNER_STRING_LENGTH-1] = '\0';
 
2591
  }
2094
2592
  jukebox_locked = FALSE;
2095
2593
}
2096
2594
 
2128
2626
void jukebox_reset_get_eax(void)
2129
2627
{
2130
2628
  jukebox_locked = TRUE;
2131
 
  NJB_Reset_Get_EAX_Type(njb);
 
2629
  NJB_Reset_Get_EAX_Type(pdedevice);
2132
2630
}
2133
2631
 
2134
2632
 
2136
2634
 * something fails. */
2137
2635
njb_eax_t *jukebox_get_eax(void)
2138
2636
{
2139
 
  njb_eax_t *eax = NJB_Get_EAX_Type(njb);
 
2637
  njb_eax_t *eax = NJB_Get_EAX_Type(pdedevice);
2140
2638
 
2141
2639
  if (eax == NULL)
2142
2640
    jukebox_locked = FALSE;
2153
2651
{
2154
2652
  if (created_play_mutex) {
2155
2653
    g_mutex_lock(play_thread_mutex);
2156
 
    NJB_Adjust_EAX(njb, effect, patch, value);
 
2654
    NJB_Adjust_EAX(pdedevice, effect, patch, value);
2157
2655
    g_mutex_unlock(play_thread_mutex);
2158
2656
  }
2159
2657
}
2167
2665
  gboolean found = FALSE;
2168
2666
 
2169
2667
  jukebox_locked = TRUE;
2170
 
  NJB_Reset_Get_Playlist(njb);
2171
 
  while (playlist = NJB_Get_Playlist(njb)) {
 
2668
  NJB_Reset_Get_Playlist(pdedevice);
 
2669
  while (playlist = NJB_Get_Playlist(pdedevice)) {
2172
2670
    if (playlist->plid == plid) {
2173
2671
      found = TRUE;
2174
2672
      break;
2204
2702
  GList *tmplist = list;
2205
2703
  
2206
2704
  if (tmplist != NULL)
2207
 
    NJB_Stop_Play(njb);
 
2705
    NJB_Stop_Play(pdedevice);
2208
2706
  while (tmplist) {
2209
2707
    metadata_t *meta;
2210
2708
    u_int32_t id;
2215
2713
      if (first) {
2216
2714
        /* g_print("Playing: %lu\n", id); */
2217
2715
        first = FALSE;
2218
 
        if (NJB_Play_Track(njb, id) == -1)
2219
 
          NJB_Error_Dump(njb, stderr);
 
2716
        if (NJB_Play_Track(pdedevice, id) == -1)
 
2717
          NJB_Error_Dump(pdedevice, stderr);
2220
2718
      } else {
2221
2719
        /* g_print("Queueing: %lu\n", id); */
2222
 
        if (NJB_Queue_Track(njb, id) == -1)
2223
 
          NJB_Error_Dump(njb, stderr);
 
2720
        if (NJB_Queue_Track(pdedevice, id) == -1)
 
2721
          NJB_Error_Dump(pdedevice, stderr);
2224
2722
      }
2225
2723
    }
2226
2724
    tmplist = tmplist->next;
2263
2761
{
2264
2762
  if (created_play_mutex) {
2265
2763
    g_mutex_lock(play_thread_mutex);
2266
 
    NJB_Seek_Track(njb, (u_int32_t) songpos);
 
2764
    NJB_Seek_Track(pdedevice, (u_int32_t) songpos);
2267
2765
    g_mutex_unlock(play_thread_mutex);
2268
2766
  } 
2269
2767
}
2383
2881
      guint seconds;
2384
2882
 
2385
2883
      g_mutex_lock(play_thread_mutex);
2386
 
      NJB_Elapsed_Time(njb, &sec, &change);
 
2884
      NJB_Elapsed_Time(pdedevice, &sec, &change);
2387
2885
      g_mutex_unlock(play_thread_mutex);
2388
2886
      hhmmss(sec, &hh, &mm, &ss);
2389
2887
      sprintf(tmp, "%02u:%02u:%02u", hh, mm, ss);
2422
2920
  }
2423
2921
 
2424
2922
  /* Free the argument list */
 
2923
 
 
2924
#ifndef G_OS_WIN32
2425
2925
  gdk_threads_enter();
2426
2926
  gtk_widget_destroy(args->dialog);
2427
2927
  gdk_threads_leave();
 
2928
#else
 
2929
  // Let the main loop destroy the dialog on Win32
 
2930
  g_idle_add(gtk_widget_destroy, (gpointer) args->dialog);
 
2931
#endif
2428
2932
  g_mutex_lock(play_thread_mutex);
2429
2933
  destroy_metalist(playlist);
2430
2934
  playlist = NULL;
2431
 
  NJB_Stop_Play(njb);
 
2935
  NJB_Stop_Play(pdedevice);
2432
2936
  g_mutex_unlock(play_thread_mutex);
2433
2937
  jukebox_locked = FALSE;
2434
2938
}
2444
2948
  struct tm *tm;
2445
2949
  
2446
2950
  // Get the time from the jukebox, then modify it.
2447
 
  jukeboxtime = NJB_Get_Time(njb);
 
2951
  jukeboxtime = NJB_Get_Time(pdedevice);
2448
2952
  g_get_current_time(&currenttimeval);
2449
2953
  currenttime = (GTime) currenttimeval.tv_sec;
2450
2954
 
2463
2967
  jukeboxtime->minutes = tm->tm_min;
2464
2968
  jukeboxtime->seconds = tm->tm_sec;
2465
2969
 
2466
 
  NJB_Set_Time(njb, jukeboxtime);
 
2970
  NJB_Set_Time(pdedevice, jukeboxtime);
2467
2971
  NJB_Destroy_Time(jukeboxtime);
2468
2972
}
2469
2973
 
2488
2992
  jbmeta->folder = g_strdup_printf("%s%s\\", folder, foldername);
2489
2993
  jbmeta->filename = g_strdup(foldername);
2490
2994
  jbmeta->size = 0;
2491
 
  if (NJB_Create_Folder (njb, jbmeta->folder, &id) == -1) {
 
2995
  if (NJB_Create_Folder (pdedevice, jbmeta->folder, &id) == -1) {
2492
2996
    // Error back off
2493
2997
    destroy_metadata_t(jbmeta);
2494
 
    NJB_Error_Dump(njb, stderr);
 
2998
    NJB_Error_Dump(pdedevice, stderr);
2495
2999
    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
3000
    return;
2497
3001
  }