Blame examples/newplaylist.c

Packit Bot a284d4
/** 
Packit Bot a284d4
 * \file newplaylist.c
Packit Bot a284d4
 * Example program to create a playlist on a device.
Packit Bot a284d4
 *
Packit Bot a284d4
 * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com>
Packit Bot a284d4
 *
Packit Bot a284d4
 * This library is free software; you can redistribute it and/or
Packit Bot a284d4
 * modify it under the terms of the GNU Lesser General Public
Packit Bot a284d4
 * License as published by the Free Software Foundation; either
Packit Bot a284d4
 * version 2 of the License, or (at your option) any later version.
Packit Bot a284d4
 *
Packit Bot a284d4
 * This library is distributed in the hope that it will be useful,
Packit Bot a284d4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot a284d4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot a284d4
 * Lesser General Public License for more details.
Packit Bot a284d4
 *
Packit Bot a284d4
 * You should have received a copy of the GNU Lesser General Public
Packit Bot a284d4
 * License along with this library; if not, write to the
Packit Bot a284d4
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Bot a284d4
 * Boston, MA 02111-1307, USA.
Packit Bot a284d4
 */
Packit Bot a284d4
#include "common.h"
Packit Bot a284d4
#include "string.h"
Packit Bot a284d4
#include <stdlib.h>
Packit Bot a284d4
#include <limits.h>
Packit Bot a284d4
#include <sys/stat.h>
Packit Bot a284d4
#include <fcntl.h>
Packit Bot a284d4
#include <errno.h>
Packit Bot a284d4
Packit Bot a284d4
static void usage(void) {
Packit Bot a284d4
  printf("Usage: newplaylist -i <fileid/trackid> -n <playlistname> -s <storage_id> -p <parent_id>\n");
Packit Bot a284d4
  exit(0);
Packit Bot a284d4
}
Packit Bot a284d4
Packit Bot a284d4
int main (int argc, char **argv) {
Packit Bot a284d4
  int opt;
Packit Bot a284d4
  extern int optind;
Packit Bot a284d4
  extern char *optarg;
Packit Bot a284d4
  LIBMTP_mtpdevice_t *device = NULL;
Packit Bot a284d4
  int idcount = 0;
Packit Bot a284d4
  uint32_t *ids = NULL;
Packit Bot a284d4
  uint32_t *tmp = NULL;
Packit Bot a284d4
  char *playlistname = NULL;
Packit Bot a284d4
  char *rest;
Packit Bot a284d4
  uint32_t storageid = 0;
Packit Bot a284d4
  uint32_t parentid = 0;
Packit Bot a284d4
 
Packit Bot a284d4
  fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
Packit Bot a284d4
Packit Bot a284d4
  while ( (opt = getopt(argc, argv, "hn:i:s:p:")) != -1 ) {
Packit Bot a284d4
    switch (opt) {
Packit Bot a284d4
    case 'h':
Packit Bot a284d4
      usage();
Packit Bot a284d4
    case 'i':
Packit Bot a284d4
      idcount++;
Packit Bot a284d4
      if ((tmp = realloc(ids, sizeof(uint32_t) * (idcount))) == NULL) {
Packit Bot a284d4
        printf("realloc failed\n");
Packit Bot a284d4
        return 1;
Packit Bot a284d4
      }
Packit Bot a284d4
      ids = tmp;
Packit Bot a284d4
      ids[(idcount-1)] = strtoul(optarg, &rest, 0);
Packit Bot a284d4
      break;
Packit Bot a284d4
    case 'n':
Packit Bot a284d4
      playlistname = strdup(optarg);
Packit Bot a284d4
      break;
Packit Bot a284d4
    case 's':
Packit Bot a284d4
      storageid = (uint32_t) strtoul(optarg, NULL, 0);
Packit Bot a284d4
	  break;
Packit Bot a284d4
    case 'p':
Packit Bot a284d4
      parentid = (uint32_t) strtoul(optarg, NULL, 0);
Packit Bot a284d4
	  break;
Packit Bot a284d4
    default:
Packit Bot a284d4
      usage();
Packit Bot a284d4
    }
Packit Bot a284d4
  }
Packit Bot a284d4
  argc -= optind;
Packit Bot a284d4
  argv += optind;
Packit Bot a284d4
Packit Bot a284d4
  if ( playlistname == NULL) {
Packit Bot a284d4
    printf("You need to supply a playlist name.\n");
Packit Bot a284d4
    usage();
Packit Bot a284d4
  }
Packit Bot a284d4
Packit Bot a284d4
  if (idcount == 0) {
Packit Bot a284d4
    printf("You need to supply one or more track IDs\n");
Packit Bot a284d4
    usage();
Packit Bot a284d4
  }
Packit Bot a284d4
Packit Bot a284d4
    
Packit Bot a284d4
  LIBMTP_Init();
Packit Bot a284d4
  device = LIBMTP_Get_First_Device();
Packit Bot a284d4
  if (device == NULL) {
Packit Bot a284d4
    printf("No devices.\n");
Packit Bot a284d4
    return 0;
Packit Bot a284d4
  }
Packit Bot a284d4
Packit Bot a284d4
  LIBMTP_playlist_t *playlist = LIBMTP_new_playlist_t();
Packit Bot a284d4
  playlist->name = playlistname;
Packit Bot a284d4
  playlist->no_tracks = idcount;
Packit Bot a284d4
  playlist->tracks = ids;
Packit Bot a284d4
  playlist->parent_id = parentid;
Packit Bot a284d4
  playlist->storage_id = storageid;
Packit Bot a284d4
  int ret = LIBMTP_Create_New_Playlist(device,playlist);
Packit Bot a284d4
  if (ret != 0) {
Packit Bot a284d4
    printf("Couldn't create playlist object\n");
Packit Bot a284d4
    LIBMTP_Dump_Errorstack(device);
Packit Bot a284d4
    LIBMTP_Clear_Errorstack(device);
Packit Bot a284d4
  }
Packit Bot a284d4
  else {
Packit Bot a284d4
    printf("Created new playlist: %u\n", playlist->playlist_id);
Packit Bot a284d4
  }
Packit Bot a284d4
Packit Bot a284d4
  LIBMTP_Release_Device(device);
Packit Bot a284d4
  printf("OK.\n");
Packit Bot a284d4
  return 0;
Packit Bot a284d4
}
Packit Bot a284d4