Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file emptyfolders.c
      3  * Example program that prunes empty folders.
      4  *
      5  * Copyright (C) 2006 Andy Kelk <andy (at) mopoke.co.uk>
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Lesser General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the
     19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     20  * Boston, MA 02111-1307, USA.
     21  */
     22 #include "common.h"
     23 #include <stdlib.h>
     24 
     25 static void prune_empty_folders(LIBMTP_mtpdevice_t *device, LIBMTP_file_t *files, LIBMTP_folder_t *folderlist, int do_delete)
     26 {
     27   if(folderlist==NULL)
     28     return;
     29 
     30   if(folderlist->child == NULL) { // this *might* be empty
     31     // therefore, check every file for this parent_id
     32     int found = 0;
     33     LIBMTP_file_t *file;
     34     file = files;
     35     while (file != NULL) {
     36       if(file->parent_id == folderlist->folder_id) { // folder has a child
     37         found = 1;
     38         break;
     39       }
     40       file = file->next;
     41     }
     42 
     43     if(found == 0) { // no files claim this as a parent
     44       printf("empty folder %u (%s)\n",folderlist->folder_id,folderlist->name);
     45       if(do_delete) {
     46         if (LIBMTP_Delete_Object(device,folderlist->folder_id) != 0) {
     47           printf("Couldn't delete folder %u\n",folderlist->folder_id);
     48 	  LIBMTP_Dump_Errorstack(device);
     49 	  LIBMTP_Clear_Errorstack(device);
     50         }
     51       }
     52     }
     53   }
     54 
     55   prune_empty_folders(device,files,folderlist->child,do_delete); // recurse down
     56   prune_empty_folders(device,files,folderlist->sibling,do_delete); // recurse along
     57 }
     58 
     59 int main (int argc, char **argv)
     60 {
     61   // check if we're doing a dummy run
     62   int do_delete = 0;
     63   int opt;
     64 
     65   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     66 
     67   while ( (opt = getopt(argc, argv, "d")) != -1 ) {
     68     switch (opt) {
     69     case 'd':
     70       do_delete = 1;
     71       break;
     72     default:
     73       break;
     74     }
     75   }
     76 
     77   if(do_delete == 0) {
     78     printf("This is a dummy run. No folders will be deleted.\n");
     79     printf("To delete folders, use the '-d' option.\n");
     80   }
     81 
     82   LIBMTP_mtpdevice_t *device;
     83   LIBMTP_folder_t *folders;
     84   LIBMTP_file_t *files;
     85 
     86   LIBMTP_Init();
     87   device = LIBMTP_Get_First_Device();
     88   if (device == NULL) {
     89     printf("No devices.\n");
     90     exit (0);
     91   }
     92 
     93   // Get file listing.
     94   files = LIBMTP_Get_Filelisting_With_Callback(device,NULL,NULL);
     95 
     96   // Get folder listing.
     97   folders = LIBMTP_Get_Folder_List(device);
     98 
     99   if(folders == NULL) {
    100     printf("No folders found\n");
    101   } else {
    102     prune_empty_folders(device,files,folders,do_delete);
    103   }
    104 
    105   LIBMTP_destroy_folder_t(folders);
    106   LIBMTP_destroy_file_t(files);
    107 
    108   LIBMTP_Release_Device(device);
    109   printf("OK.\n");
    110   exit (0);
    111 }
    112 
    113