• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file files.c
3  * Example program that lists all files on a device.
4  *
5  * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
6  * Copyright (C) 2007 Ted Bullock <tbullock@canada.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 <stdlib.h>
25 
dump_fileinfo(LIBMTP_file_t * file)26 static void dump_fileinfo(LIBMTP_file_t *file)
27 {
28   printf("File ID: %u\n", file->item_id);
29   if (file->filename != NULL)
30     printf("   Filename: %s\n", file->filename);
31 
32   // This is sort of special...
33   if (file->filesize == (uint32_t) -1) {
34     printf("   None. (abstract file, size = -1)\n");
35   } else {
36 #ifdef __WIN32__
37     printf("   File size %llu (0x%016I64X) bytes\n", file->filesize, file->filesize);
38 #else
39     printf("   File size %llu (0x%016llX) bytes\n",
40 	   (long long unsigned int) file->filesize,
41 	   (long long unsigned int) file->filesize);
42 #endif
43   }
44   printf("   Parent ID: %u\n", file->parent_id);
45   printf("   Storage ID: 0x%08X\n", file->storage_id);
46   printf("   Filetype: %s\n", LIBMTP_Get_Filetype_Description(file->filetype));
47 }
48 
main(int argc,char ** argv)49 int main (int argc, char **argv)
50 {
51   LIBMTP_mtpdevice_t *device_list, *iter;
52   LIBMTP_file_t *files;
53 
54   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
55 
56   LIBMTP_Init();
57 
58   switch(LIBMTP_Get_Connected_Devices(&device_list))
59   {
60   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
61     fprintf(stdout, "mtp-files: No Devices have been found\n");
62     return 0;
63   case LIBMTP_ERROR_CONNECTING:
64     fprintf(stderr, "mtp-files: There has been an error connecting. Exit\n");
65     return 1;
66   case LIBMTP_ERROR_MEMORY_ALLOCATION:
67     fprintf(stderr, "mtp-files: Memory Allocation Error. Exit\n");
68     return 1;
69 
70   /* Unknown general errors - This should never execute */
71   case LIBMTP_ERROR_GENERAL:
72   default:
73     fprintf(stderr, "mtp-files: Unknown error, please report "
74                     "this to the libmtp developers\n");
75   return 1;
76 
77   /* Successfully connected at least one device, so continue */
78   case LIBMTP_ERROR_NONE:
79     fprintf(stdout, "mtp-files: Successfully connected\n");
80     fflush(stdout);
81   }
82 
83   /* iterate through connected MTP devices */
84   for(iter = device_list; iter != NULL; iter = iter->next)
85   {
86 
87     char *friendlyname;
88 
89     /* Echo the friendly name so we know which device we are working with */
90     friendlyname = LIBMTP_Get_Friendlyname(iter);
91     if (friendlyname == NULL) {
92       printf("Listing File Information on Device with name: (NULL)\n");
93     } else {
94       printf("Listing File Information on Device with name: %s\n", friendlyname);
95       free(friendlyname);
96     }
97 
98 	  /* Get track listing. */
99 	  files = LIBMTP_Get_Filelisting_With_Callback(iter, NULL, NULL);
100 	  if (files == NULL) {
101 	    printf("No files.\n");
102 	    LIBMTP_Dump_Errorstack(iter);
103 	    LIBMTP_Clear_Errorstack(iter);
104 	  } else {
105 	    LIBMTP_file_t *file, *tmp;
106 	    file = files;
107 	    while (file != NULL) {
108 	      dump_fileinfo(file);
109 	      tmp = file;
110 	      file = file->next;
111 	      LIBMTP_destroy_file_t(tmp);
112       }
113 	  }
114   }
115 
116   LIBMTP_Release_Device_List(device_list);
117   printf("OK.\n");
118   exit (0);
119 }
120 
121