Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file folders.c
      3  * Example program that lists all folders on a device.
      4  *
      5  * Copyright (C) 2005-2007 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2007 Ted Bullock <tbullock (at) 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 
     26 static void dump_folder_list(LIBMTP_folder_t *folderlist, int level)
     27 {
     28   int i;
     29   if(folderlist==NULL) {
     30     return;
     31   }
     32 
     33   printf("%u\t", folderlist->folder_id);
     34   for(i=0;i<level;i++) printf("  ");
     35 
     36   printf("%s\n", folderlist->name);
     37 
     38   dump_folder_list(folderlist->child, level+1);
     39   dump_folder_list(folderlist->sibling, level);
     40 }
     41 
     42 int main (int argc, char **argv)
     43 {
     44   LIBMTP_mtpdevice_t *device, *iter;
     45   LIBMTP_folder_t *folders;
     46 
     47   LIBMTP_Init();
     48   fprintf(stdout, "Attempting to connect device(s)\n");
     49 
     50   switch(LIBMTP_Get_Connected_Devices(&device))
     51   {
     52   case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
     53     fprintf(stdout, "mtp-folders: No Devices have been found\n");
     54     return 0;
     55   case LIBMTP_ERROR_CONNECTING:
     56     fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n");
     57     return 1;
     58   case LIBMTP_ERROR_MEMORY_ALLOCATION:
     59     fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n");
     60     return 1;
     61 
     62   /* Unknown general errors - This should never execute */
     63   case LIBMTP_ERROR_GENERAL:
     64   default:
     65     fprintf(stderr, "mtp-folders: Unknown error, please report "
     66                     "this to the libmtp developers\n");
     67   return 1;
     68 
     69   /* Successfully connected at least one device, so continue */
     70   case LIBMTP_ERROR_NONE:
     71     fprintf(stdout, "mtp-folders: Successfully connected\n");
     72     fflush(stdout);
     73   }
     74 
     75   /* iterate through connected MTP devices */
     76   for(iter = device; iter != NULL; iter = iter->next)
     77   {
     78   	char *friendlyname;
     79     /* Echo the friendly name so we know which device we are working with */
     80     friendlyname = LIBMTP_Get_Friendlyname(iter);
     81     if (friendlyname == NULL) {
     82       printf("Friendly name: (NULL)\n");
     83     } else {
     84       printf("Friendly name: %s\n", friendlyname);
     85       free(friendlyname);
     86     }
     87 
     88     LIBMTP_Dump_Errorstack(iter);
     89     LIBMTP_Clear_Errorstack(iter);    /* Get folder listing */
     90 
     91     folders = LIBMTP_Get_Folder_List(iter);
     92 
     93     if (folders == NULL) {
     94       fprintf(stdout, "No folders found\n");
     95       LIBMTP_Dump_Errorstack(iter);
     96       LIBMTP_Clear_Errorstack(iter);
     97     } else {
     98       dump_folder_list(folders,0);
     99     }
    100 
    101     LIBMTP_destroy_folder_t(folders);
    102   }
    103 
    104 
    105   LIBMTP_Release_Device_List(device);
    106   printf("OK.\n");
    107 
    108   return 0;
    109 }
    110