Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 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 | */ |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 22 | #include "common.h" |
| 23 | #include "string.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <limits.h> |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | static void usage(void) { |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 31 | printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname> -s <storage_id> -p <parent_id>\n"); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 32 | exit(0); |
| 33 | } |
| 34 | |
| 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; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 44 | char *rest; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 45 | uint32_t storageid = 0; |
| 46 | uint32_t parentid = 0; |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 47 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 48 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 49 | |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 50 | while ( (opt = getopt(argc, argv, "hn:i:s:p:")) != -1 ) { |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 51 | 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 Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame] | 61 | ids[(idcount-1)] = strtoul(optarg, &rest, 0); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 62 | break; |
| 63 | case 'n': |
| 64 | playlistname = strdup(optarg); |
| 65 | break; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 66 | 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; |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 72 | 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; |
nicklas79 | aac4729 | 2009-09-28 18:16:45 +0000 | [diff] [blame] | 101 | playlist->parent_id = parentid; |
| 102 | playlist->storage_id = storageid; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 103 | int ret = LIBMTP_Create_New_Playlist(device,playlist); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 104 | if (ret != 0) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 105 | printf("Couldn't create playlist object\n"); |
| 106 | LIBMTP_Dump_Errorstack(device); |
| 107 | LIBMTP_Clear_Errorstack(device); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 108 | } |
| 109 | else { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 110 | printf("Created new playlist: %u\n", playlist->playlist_id); |
rreardon | 508705f | 2006-11-19 21:27:22 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | LIBMTP_Release_Device(device); |
| 114 | printf("OK.\n"); |
| 115 | return 0; |
| 116 | } |
| 117 | |