blob: 9f47ae560139ab854b13b839fb7b99ac6594369e [file] [log] [blame]
Linus Walleij3d78c4c2007-02-15 12:18:12 +00001/**
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 */
rreardon508705f2006-11-19 21:27:22 +000022#include "common.h"
23#include "string.h"
Linus Walleij2eaaff02009-01-15 21:30:36 +000024#include <stdlib.h>
25#include <limits.h>
rreardon508705f2006-11-19 21:27:22 +000026#include <sys/stat.h>
27#include <fcntl.h>
28#include <errno.h>
29
30static void usage(void) {
nicklas79aac47292009-09-28 18:16:45 +000031 printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname> -s <storage_id> -p <parent_id>\n");
rreardon508705f2006-11-19 21:27:22 +000032 exit(0);
33}
34
35int 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;
Linus Walleij20c3b672007-11-09 19:02:19 +000044 char *rest;
nicklas79aac47292009-09-28 18:16:45 +000045 uint32_t storageid = 0;
46 uint32_t parentid = 0;
rreardon508705f2006-11-19 21:27:22 +000047
tedbullockcd9f4992007-03-29 06:00:40 +000048 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
49
nicklas79aac47292009-09-28 18:16:45 +000050 while ( (opt = getopt(argc, argv, "hn:i:s:p:")) != -1 ) {
rreardon508705f2006-11-19 21:27:22 +000051 switch (opt) {
52 case 'h':
53 usage();
54 case 'i':
55 idcount++;
56 if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
57 printf("realloc failed\n");
58 return 1;
59 }
60 ids = tmp;
Linus Walleij20c3b672007-11-09 19:02:19 +000061 ids[(idcount-1)] = strtoul(optarg, &rest, 0);
rreardon508705f2006-11-19 21:27:22 +000062 break;
63 case 'n':
64 playlistname = strdup(optarg);
65 break;
nicklas79aac47292009-09-28 18:16:45 +000066 case 's':
67 storageid = (uint32_t) strtoul(optarg, NULL, 0);
68 break;
69 case 'p':
70 parentid = (uint32_t) strtoul(optarg, NULL, 0);
71 break;
rreardon508705f2006-11-19 21:27:22 +000072 default:
73 usage();
74 }
75 }
76 argc -= optind;
77 argv += optind;
78
79 if ( playlistname == NULL) {
80 printf("You need to supply a playlist name.\n");
81 usage();
82 }
83
84 if (idcount == 0) {
85 printf("You need to supply one or more track IDs\n");
86 usage();
87 }
88
89
90 LIBMTP_Init();
91 device = LIBMTP_Get_First_Device();
92 if (device == NULL) {
93 printf("No devices.\n");
94 return 0;
95 }
96
97 LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
98 playlist->name = playlistname;
99 playlist->no_tracks = idcount;
100 playlist->tracks = ids;
nicklas79aac47292009-09-28 18:16:45 +0000101 playlist->parent_id = parentid;
102 playlist->storage_id = storageid;
Linus Walleijea68f1f2008-06-22 21:54:44 +0000103 int ret = LIBMTP_Create_New_Playlist(device,playlist);
rreardon508705f2006-11-19 21:27:22 +0000104 if (ret != 0) {
Linus Walleij070e9b42007-01-22 08:49:28 +0000105 printf("Couldn't create playlist object\n");
106 LIBMTP_Dump_Errorstack(device);
107 LIBMTP_Clear_Errorstack(device);
rreardon508705f2006-11-19 21:27:22 +0000108 }
109 else {
Linus Walleij070e9b42007-01-22 08:49:28 +0000110 printf("Created new playlist: %u\n", playlist->playlist_id);
rreardon508705f2006-11-19 21:27:22 +0000111 }
112
113 LIBMTP_Release_Device(device);
114 printf("OK.\n");
115 return 0;
116}
117