Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file getfile.c
      3  * Example program to retrieve a file off the device.
      4  *
      5  * Copyright (C) 2005-2007 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 "pathutils.h"
     25 #include <stdlib.h>
     26 #include <limits.h>
     27 
     28 void getfile_function(char *,char *);
     29 void getfile_command(int, char **);
     30 void getfile_usage(void);
     31 
     32 extern LIBMTP_folder_t *folders;
     33 extern LIBMTP_file_t *files;
     34 extern LIBMTP_mtpdevice_t *device;
     35 
     36 void getfile_usage (void)
     37 {
     38   fprintf(stderr, "getfile <fileid/trackid> <filename>\n");
     39 }
     40 
     41 void
     42 getfile_function(char * from_path,char * to_path)
     43 {
     44   int id = parse_path (from_path,files,folders);
     45   if (id > 0) {
     46     printf("Getting %s to %s\n",from_path,to_path);
     47     if (LIBMTP_Get_File_To_File(device, id, to_path, progress, NULL) != 0 ) {
     48       printf("\nError getting file from MTP device.\n");
     49       LIBMTP_Dump_Errorstack(device);
     50       LIBMTP_Clear_Errorstack(device);
     51     }
     52   }
     53 }
     54 
     55 
     56 void getfile_command(int argc, char **argv)
     57 {
     58   uint32_t id;
     59   char *endptr;
     60   char *file;
     61 
     62   // We need file ID and filename
     63   if ( argc != 3 ) {
     64     getfile_usage();
     65     return;
     66   }
     67 
     68   // Sanity check song ID
     69   id = strtoul(argv[1], &endptr, 10);
     70   if ( *endptr != 0 ) {
     71     fprintf(stderr, "illegal value %s\n", argv[1]);
     72     return;
     73   } else if ( ! id ) {
     74     fprintf(stderr, "bad file/track id %u\n", id);
     75     return;
     76  }
     77 
     78   // Filename, e.g. "foo.mp3"
     79   file = argv[2];
     80   printf("Getting file/track %d to local file %s\n", id, file);
     81 
     82   // This function will also work just as well for tracks.
     83   if (LIBMTP_Get_File_To_File(device, id, file, progress, NULL) != 0 ) {
     84     printf("\nError getting file from MTP device.\n");
     85   }
     86   // Terminate progress bar.
     87   printf("\n");
     88 
     89   return;
     90 }
     91 
     92