Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file delfile.c
      3  * Example program to delete a file off the device.
      4  *
      5  * Copyright (C) 2005-2008 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2006 Chris A. Debenham <chris (at) adebenham.com>
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Lesser General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Lesser General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Lesser General Public
     19  * License along with this library; if not, write to the
     20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     21  * Boston, MA 02111-1307, USA.
     22  */
     23 #include "common.h"
     24 #include "string.h"
     25 #include "pathutils.h"
     26 #include <stdlib.h>
     27 #include <limits.h>
     28 
     29 void delfile_usage(void);
     30 void delfile_function(char *);
     31 void delfile_command(int, char **);
     32 
     33 extern LIBMTP_mtpdevice_t *device;
     34 extern LIBMTP_folder_t *folders;
     35 extern LIBMTP_file_t *files;
     36 
     37 void delfile_usage(void)
     38 {
     39   printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
     40 }
     41 
     42 void
     43 delfile_function(char * path)
     44 {
     45   uint32_t id = parse_path (path,files,folders);
     46 
     47   if (id > 0) {
     48     printf("Deleting %s which has item_id:%d\n",path,id);
     49     int ret = 1;
     50     ret = LIBMTP_Delete_Object(device, id);
     51     if (ret != 0) {
     52       LIBMTP_Dump_Errorstack(device);
     53       LIBMTP_Clear_Errorstack(device);
     54       printf("Failed to remove file\n");
     55     }
     56   }
     57 }
     58 
     59 void delfile_command(int argc, char **argv)
     60 {
     61   int FILENAME = 1;
     62   int ITEMID = 2;
     63   int field_type = 0;
     64   int i;
     65 
     66   if ( argc > 2 ) {
     67     if (strncmp(argv[1],"-f",2) == 0) {
     68       field_type = FILENAME;
     69       strcpy(argv[1],"");
     70     } else if (strncmp(argv[1],"-n",2) == 0) {
     71       field_type = ITEMID;
     72       strcpy(argv[1],"0");
     73     } else {
     74       delfile_usage();
     75       return;
     76     }
     77   } else {
     78     delfile_usage();
     79     return;
     80   }
     81 
     82   for (i=1;i<argc;i++) {
     83     uint32_t id;
     84     char *endptr;
     85     int ret = 0;
     86 
     87     if (field_type == ITEMID) {
     88       // Sanity check song ID
     89       id = strtoul(argv[i], &endptr, 10);
     90       if ( *endptr != 0 ) {
     91         fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
     92         id = 0;
     93       }
     94     } else {
     95       if (strlen(argv[i]) > 0) {
     96         id = parse_path (argv[i],files,folders);
     97       } else {
     98         id = 0;
     99       }
    100     }
    101     if (id > 0 ) {
    102       printf("Deleting %s\n",argv[i]);
    103       ret = LIBMTP_Delete_Object(device, id);
    104     }
    105     if ( ret != 0 ) {
    106       printf("Failed to delete file:%s\n",argv[i]);
    107       LIBMTP_Dump_Errorstack(device);
    108       LIBMTP_Clear_Errorstack(device);
    109       ret = 1;
    110     }
    111   }
    112 }
    113 
    114