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 <stdlib.h>
25 #include <limits.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 
usage(void)30 static void usage(void) {
31   printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname>\n");
32   exit(0);
33 }
34 
main(int argc,char ** argv)35 int main (int argc, char **argv) {
36   int opt;
37   extern int optind;
38   extern char *optarg;
39   LIBMTP_mtpdevice_t *device = NULL;
40   int idcount = 0;
41   uint32_t *ids = NULL;
42   uint32_t *tmp = NULL;
43   char *playlistname = NULL;
44   char *rest;
45 
46   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
47 
48   while ( (opt = getopt(argc, argv, "hn:i:")) != -1 ) {
49     switch (opt) {
50     case 'h':
51       usage();
52     case 'i':
53       idcount++;
54       if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
55         printf("realloc failed\n");
56         return 1;
57       }
58       ids = tmp;
59       ids[(idcount-1)] = strtoul(optarg, &rest, 0);
60       break;
61     case 'n':
62       playlistname = strdup(optarg);
63       break;
64     default:
65       usage();
66     }
67   }
68   argc -= optind;
69   argv += optind;
70 
71   if ( playlistname == NULL) {
72     printf("You need to supply a playlist name.\n");
73     usage();
74   }
75 
76   if (idcount == 0) {
77     printf("You need to supply one or more track IDs\n");
78     usage();
79   }
80 
81 
82   LIBMTP_Init();
83   device = LIBMTP_Get_First_Device();
84   if (device == NULL) {
85     printf("No devices.\n");
86     return 0;
87   }
88 
89   LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
90   playlist->name = playlistname;
91   playlist->no_tracks = idcount;
92   playlist->tracks = ids;
93   playlist->parent_id = 0;
94   playlist->storage_id = 0;
95   int ret = LIBMTP_Create_New_Playlist(device,playlist);
96   if (ret != 0) {
97     printf("Couldn't create playlist object\n");
98     LIBMTP_Dump_Errorstack(device);
99     LIBMTP_Clear_Errorstack(device);
100   }
101   else {
102     printf("Created new playlist: %u\n", playlist->playlist_id);
103   }
104 
105   LIBMTP_Release_Device(device);
106   printf("OK.\n");
107   return 0;
108 }
109 
110