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