newplaylist.c revision 3d78c4ce5b4b4f38929792b6e392df1416b578b7
1/**
2 * \file newplaylist.c
3 * Example program to create a playlist on a device.
4 *
5 * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22#include "common.h"
23#include "string.h"
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27
28static void usage(void) {
29  printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname>\n");
30  exit(0);
31}
32
33int main (int argc, char **argv) {
34  int opt;
35  extern int optind;
36  extern char *optarg;
37  LIBMTP_mtpdevice_t *device = NULL;
38  int idcount = 0;
39  uint32_t *ids = NULL;
40  uint32_t *tmp = NULL;
41  char *playlistname = NULL;
42
43  while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
44    switch (opt) {
45    case 'h':
46      usage();
47    case 'i':
48      idcount++;
49      if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
50        printf("realloc failed\n");
51        return 1;
52      }
53      ids = tmp;
54      ids[(idcount-1)] = atoi(strdup(optarg));
55      break;
56    case 'n':
57      playlistname = strdup(optarg);
58      break;
59    default:
60      usage();
61    }
62  }
63  argc -= optind;
64  argv += optind;
65
66  if ( playlistname == NULL) {
67    printf("You need to supply a playlist name.\n");
68    usage();
69  }
70
71  if (idcount == 0) {
72    printf("You need to supply one or more track IDs\n");
73    usage();
74  }
75
76
77  LIBMTP_Init();
78  device = LIBMTP_Get_First_Device();
79  if (device == NULL) {
80    printf("No devices.\n");
81    return 0;
82  }
83
84  LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
85  playlist->name = playlistname;
86  playlist->no_tracks = idcount;
87  playlist->tracks = ids;
88  int ret = LIBMTP_Create_New_Playlist(device,playlist,0);
89  if (ret != 0) {
90    printf("Couldn't create playlist object\n");
91    LIBMTP_Dump_Errorstack(device);
92    LIBMTP_Clear_Errorstack(device);
93  }
94  else {
95    printf("Created new playlist: %u\n", playlist->playlist_id);
96  }
97
98  LIBMTP_Release_Device(device);
99  printf("OK.\n");
100  return 0;
101}
102
103