newplaylist.c revision 508705f40e77e580e42da11b74368df89a2dbbaf
1#include "common.h"
2#include "string.h"
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <errno.h>
6
7static void usage(void) {
8  printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname>\n");
9  exit(0);
10}
11
12int main (int argc, char **argv) {
13  int opt;
14  extern int optind;
15  extern char *optarg;
16  LIBMTP_mtpdevice_t *device = NULL;
17  int idcount = 0;
18  uint32_t *ids = NULL;
19  uint32_t *tmp = NULL;
20  char *playlistname = NULL;
21
22  while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
23    switch (opt) {
24    case 'h':
25      usage();
26    case 'i':
27      idcount++;
28      if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
29        printf("realloc failed\n");
30        return 1;
31      }
32      ids = tmp;
33      ids[(idcount-1)] = atoi(strdup(optarg));
34      break;
35    case 'n':
36      playlistname = strdup(optarg);
37      break;
38    default:
39      usage();
40    }
41  }
42  argc -= optind;
43  argv += optind;
44
45  if ( playlistname == NULL) {
46    printf("You need to supply a playlist name.\n");
47    usage();
48  }
49
50  if (idcount == 0) {
51    printf("You need to supply one or more track IDs\n");
52    usage();
53  }
54
55
56  LIBMTP_Init();
57  device = LIBMTP_Get_First_Device();
58  if (device == NULL) {
59    printf("No devices.\n");
60    return 0;
61  }
62
63  LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
64  playlist->name = playlistname;
65  playlist->no_tracks = idcount;
66  playlist->tracks = ids;
67  int ret = LIBMTP_Create_New_Playlist(device,playlist,0);
68  if (ret != 0) {
69    printf("Couldn't create album object\n");
70  }
71  else {
72  	printf("Created new playlist: %u\n", playlist->playlist_id);
73  }
74
75  LIBMTP_Release_Device(device);
76  printf("OK.\n");
77  return 0;
78}
79
80