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