~ubuntu-branches/ubuntu/precise/libmtp/precise-proposed

« back to all changes in this revision

Viewing changes to examples/sendtr.c

  • Committer: Bazaar Package Importer
  • Author(s): Savvas Radevic
  • Date: 2009-04-18 19:58:14 UTC
  • mto: (16.1.2 squeeze) (0.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090418195814-o6h16rbqdbvlrw8s
Tags: upstream-0.3.7
ImportĀ upstreamĀ versionĀ 0.3.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * based on Enrique Jorreto Ledesma's work on the original program by 
6
6
 * Shaun Jackman and Linus Walleij.
7
7
 *
8
 
 * Copyright (C) 2003-2008 Linus Walleij <triad@df.lth.se>
 
8
 * Copyright (C) 2003-2009 Linus Walleij <triad@df.lth.se>
9
9
 * Copyright (C) 2003-2005 Shaun Jackman
10
10
 * Copyright (C) 2003-2005 Enrique Jorrete Ledesma
11
11
 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
12
12
 * Copyright (C) 2008 Nicolas Pennequin <nicolas.pennequin@free.fr>
 
13
 * Copyright (C) 2008 Joseph Nahmias <joe@nahmias.net>
13
14
 *
14
15
 * This library is free software; you can redistribute it and/or
15
16
 * modify it under the terms of the GNU Lesser General Public
30
31
#define _LARGEFILE64_SOURCE
31
32
 
32
33
#include "common.h"
 
34
#include "util.h"
 
35
#include <stdlib.h>
 
36
#include <limits.h>
33
37
#include <string.h>
34
38
#include <libgen.h>
35
39
#include <sys/stat.h>
45
49
extern LIBMTP_file_t *files;
46
50
extern LIBMTP_mtpdevice_t *device;
47
51
 
48
 
int sendtrack_function (char *, char *, char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t);
 
52
int sendtrack_function (char *, char *, char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t, uint32_t);
49
53
void sendtrack_command (int, char **);
50
54
void sendtrack_usage (void);
51
55
 
54
58
  fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n");
55
59
  fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n");
56
60
  fprintf(stderr, "    -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n");
57
 
  fprintf(stderr, "       -d <duration in seconds> <local path> <remote path>\n");
 
61
  fprintf(stderr, "       -d <duration in seconds> -s <storage_id> <local path> <remote path>\n");
58
62
  fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
59
63
}
60
64
 
61
 
static void checklang(void)
62
 
{
63
 
  char *langsuff = NULL;
64
 
  char *lang = getenv("LANG");
65
 
 
66
 
#ifdef HAVE_LANGINFO_H
67
 
  langsuff = nl_langinfo(CODESET);
68
 
#else
69
 
  /*
70
 
   * Check environment variables $LANG and $LC_CTYPE
71
 
   * to see if we want to support UTF-8 unicode
72
 
   */
73
 
  if (lang != NULL) {
74
 
    if (strlen(lang) > 5) {
75
 
      langsuff = &lang[strlen(lang)-5];
76
 
    }
77
 
  }
78
 
#endif
79
 
  if (strcmp(langsuff, "UTF-8")) {
80
 
    printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang);
81
 
    printf("If you want to have support for diacritics and Unicode characters,\n");
82
 
    printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n");
83
 
  }
84
 
}
85
 
 
86
65
static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
87
66
{
88
67
  char *cp, *bp;
174
153
  
175
154
  if (ret != 0) {
176
155
    printf("Error creating or updating album.\n");
 
156
    printf("(This could be due to that your device does not support albums.)\n");
177
157
    LIBMTP_Dump_Errorstack(device);
178
158
    LIBMTP_Clear_Errorstack(device);
179
159
  } else {
182
162
  return ret;
183
163
}
184
164
 
185
 
int sendtrack_function(char * from_path, char * to_path, char *partist, char *palbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t tracknum, uint16_t length, uint16_t year)
 
165
int sendtrack_function(char * from_path, char * to_path, char *partist, char *palbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t tracknum, uint16_t length, uint16_t year, uint32_t storageid)
186
166
{
187
167
  char *filename, *parent;
188
168
  char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
203
183
  trackmeta = LIBMTP_new_track_t();
204
184
  albuminfo = LIBMTP_new_album_t();
205
185
 
206
 
  parent = dirname(to_path);
207
 
  filename = basename(to_path);
 
186
  parent = dirname(strdup(to_path));
 
187
  filename = basename(strdup(to_path));
208
188
  parent_id = parse_path (parent,files,folders);
209
189
  if (parent_id == -1) {
210
190
    printf("Parent folder could not be found, skipping\n");
351
331
    }
352
332
    trackmeta->filesize = filesize;
353
333
    trackmeta->parent_id = parent_id;
354
 
    // Use default storage.
355
 
    trackmeta->storage_id = 0;
 
334
    {
 
335
        int rc;
 
336
        char *desc = NULL;
 
337
        LIBMTP_devicestorage_t *pds = NULL;
 
338
 
 
339
        if ( 0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED)) )
 
340
        {
 
341
            perror("LIBMTP_Get_Storage()");
 
342
            exit(-1);
 
343
        }
 
344
        for (pds = device->storage; pds != NULL; pds = pds->next)
 
345
        {
 
346
            if (pds->id == storageid)
 
347
            {
 
348
                desc = strdup(pds->StorageDescription);
 
349
                break;
 
350
            }
 
351
        }
 
352
        if (NULL != desc)
 
353
        {
 
354
            printf("Storage ID: %s (%u)\n", desc, storageid);
 
355
            free(desc);
 
356
        }
 
357
        else
 
358
            printf("Storage ID: %u\n", storageid);
 
359
        trackmeta->storage_id = storageid;
 
360
    }
356
361
      
357
362
    printf("Sending track...\n");
358
363
    ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL);
365
370
      printf("New track ID: %d\n", trackmeta->item_id);
366
371
    }
367
372
 
368
 
/* Add here add to album call */
369
 
                ret = add_track_to_album(albuminfo, trackmeta);
 
373
    /* Add here add to album call */
 
374
    if (palbum)
 
375
      ret = add_track_to_album(albuminfo, trackmeta);
370
376
 
371
377
    LIBMTP_destroy_album_t(albuminfo);
372
378
    LIBMTP_destroy_track_t(trackmeta);
391
397
  uint16_t length = 0;
392
398
  uint16_t year = 0;
393
399
  uint16_t quiet = 0;
394
 
  char *lang;
395
 
  while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:")) != -1 ) {
 
400
  uint32_t storageid = 0;
 
401
  while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) {
396
402
    switch (opt) {
397
403
    case 't':
398
404
      ptitle = strdup(optarg);
418
424
    case 'n':
419
425
      tracknum = atoi(optarg);
420
426
      break;
 
427
    case 's':
 
428
      storageid = (uint32_t) strtoul(optarg, NULL, 0);
 
429
      break;
421
430
    case 'd':
422
431
      length = atoi(optarg);
423
432
      break;
437
446
  if ( argc != 2 ) {
438
447
    printf("You need to pass a filename and destination.\n");
439
448
    sendtrack_usage();
 
449
    return;
440
450
  }
441
451
 
442
452
  checklang();
443
453
  
444
 
  printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year);
445
 
  sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year);
 
454
  printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d,%u\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year, storageid);
 
455
  sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid);
446
456
}