Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file newplaylist.c
      3  * Example program to create a playlist on a device.
      4  *
      5  * Copyright (C) 2006 Robert Reardon <rreardon (at) 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 
     30 static void usage(void) {
     31   printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname> -s <storage_id> -p <parent_id>\n");
     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;
     44   char *rest;
     45   uint32_t storageid = 0;
     46   uint32_t parentid = 0;
     47 
     48   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     49 
     50   while ( (opt = getopt(argc, argv, "hn:i:s:p:")) != -1 ) {
     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;
     61       ids[(idcount-1)] = strtoul(optarg, &rest, 0);
     62       break;
     63     case 'n':
     64       playlistname = strdup(optarg);
     65       break;
     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;
     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;
    101   playlist->parent_id = parentid;
    102   playlist->storage_id = storageid;
    103   int ret = LIBMTP_Create_New_Playlist(device,playlist);
    104   if (ret != 0) {
    105     printf("Couldn't create playlist object\n");
    106     LIBMTP_Dump_Errorstack(device);
    107     LIBMTP_Clear_Errorstack(device);
    108   }
    109   else {
    110     printf("Created new playlist: %u\n", playlist->playlist_id);
    111   }
    112 
    113   LIBMTP_Release_Device(device);
    114   printf("OK.\n");
    115   return 0;
    116 }
    117 
    118