newplaylist.c revision 20c3b6712c74c05c12b5e5f7123a2d7e1876c40e
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  char *rest;
43
44  fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
45
46  while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
47    switch (opt) {
48    case 'h':
49      usage();
50    case 'i':
51      idcount++;
52      if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
53        printf("realloc failed\n");
54        return 1;
55      }
56      ids = tmp;
57      ids[(idcount-1)] = strtoul(optarg, &rest, 0);
58      break;
59    case 'n':
60      playlistname = strdup(optarg);
61      break;
62    default:
63      usage();
64    }
65  }
66  argc -= optind;
67  argv += optind;
68
69  if ( playlistname == NULL) {
70    printf("You need to supply a playlist name.\n");
71    usage();
72  }
73
74  if (idcount == 0) {
75    printf("You need to supply one or more track IDs\n");
76    usage();
77  }
78
79
80  LIBMTP_Init();
81  device = LIBMTP_Get_First_Device();
82  if (device == NULL) {
83    printf("No devices.\n");
84    return 0;
85  }
86
87  LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
88  playlist->name = playlistname;
89  playlist->no_tracks = idcount;
90  playlist->tracks = ids;
91  int ret = LIBMTP_Create_New_Playlist(device,playlist,0);
92  if (ret != 0) {
93    printf("Couldn't create playlist object\n");
94    LIBMTP_Dump_Errorstack(device);
95    LIBMTP_Clear_Errorstack(device);
96  }
97  else {
98    printf("Created new playlist: %u\n", playlist->playlist_id);
99  }
100
101  LIBMTP_Release_Device(device);
102  printf("OK.\n");
103  return 0;
104}
105
106