Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file connect.c
      3  * Main programs implementing several utilities in one.
      4  *
      5  * Copyright (C) 2006 Chris A. Debenham <chris (at) adebenham.com>
      6  * Copyright (C) 2008-2010 Linus Walleij <triad (at) df.lth.se>
      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 <libgen.h>
     25 #include <getopt.h>
     26 #include <string.h>
     27 
     28 #include "common.h"
     29 #include "util.h"
     30 #include "pathutils.h"
     31 #include "connect.h"
     32 
     33 LIBMTP_folder_t *folders;
     34 LIBMTP_file_t *files;
     35 LIBMTP_mtpdevice_t *device;
     36 
     37 static void
     38 split_arg(char * argument, char ** part1, char ** part2)
     39 {
     40   char *sepp;
     41   *part1 = NULL;
     42   *part2 = NULL;
     43 
     44   sepp = argument + strcspn(argument, ",");
     45   sepp[0] = '\0';
     46   *part1 = argument;
     47   *part2 = sepp+1;
     48 }
     49 
     50 static void
     51 usage(void)
     52 {
     53   printf("Usage: connect <command1> <command2>\n");
     54   printf("Commands: --delete [filename]\n");
     55   printf("          --sendfile [source] [destination]\n");
     56   printf("          --sendtrack [source] [destination]\n");
     57   printf("          --getfile [source] [destination]\n");
     58   printf("          --newfolder [foldername]\n");
     59 }
     60 
     61 
     62 int main (int argc, char **argv)
     63 {
     64   int ret = 0;
     65 
     66   checklang();
     67 
     68   LIBMTP_Init();
     69 
     70   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     71 
     72   device = LIBMTP_Get_First_Device();
     73   if (device == NULL) {
     74     printf("No devices.\n");
     75     return 0;
     76   }
     77   files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL);
     78   folders = LIBMTP_Get_Folder_List (device);
     79 
     80   if ((strncmp(basename(argv[0]),"mtp-delfile",11) == 0) || (strncmp(basename(argv[0]),"delfile",7) == 0)) {
     81     ret = delfile_command(argc,argv);
     82   } else if ((strncmp(basename(argv[0]),"mtp-getfile",13) == 0) || (strncmp(basename(argv[0]),"getfile",9) == 0)) {
     83     ret = getfile_command(argc,argv);
     84   } else if ((strncmp(basename(argv[0]),"mtp-newfolder",13) == 0) || (strncmp(basename(argv[0]),"newfolder",9) == 0)) {
     85     ret = newfolder_command(argc,argv);
     86   } else if ((strncmp(basename(argv[0]),"mtp-sendfile",11) == 0) || (strncmp(basename(argv[0]),"sendfile",7) == 0)) {
     87     ret = sendfile_command(argc, argv);
     88   } else if ((strncmp(basename(argv[0]),"mtp-sendtr",10) == 0) || (strncmp(basename(argv[0]),"sendtr",6) == 0)) {
     89     ret = sendtrack_command(argc, argv);
     90   } else {
     91     if ( argc < 2 ) {
     92       usage ();
     93       return 1;
     94     }
     95 
     96     while (1) {
     97       int option_index = 0;
     98       static struct option long_options[] = {
     99         {"delete", 1, 0, 'd'},
    100         {"sendfile", 1, 0, 'f'},
    101         {"getfile", 1, 0, 'g'},
    102         {"newfolder", 1, 0, 'n'},
    103         {"sendtrack", 1, 0, 't'},
    104         {0, 0, 0, 0}
    105       };
    106 
    107       int c = getopt_long (argc, argv, "d:f:g:n:t:", long_options, &option_index);
    108       if (c == -1)
    109         break;
    110 
    111       char *arg1, *arg2;
    112       switch (c) {
    113       case 'd':
    114         printf("Delete %s\n",optarg);
    115         ret = delfile_function(optarg);
    116         break;
    117 
    118       case 'f':
    119         printf("Send file %s\n",optarg);
    120         split_arg(optarg,&arg1,&arg2);
    121         ret = sendfile_function(arg1,arg2);
    122         break;
    123 
    124       case 'g':
    125         printf("Get file %s\n",optarg);
    126         split_arg(optarg,&arg1,&arg2);
    127         ret = getfile_function(arg1,arg2);
    128         break;
    129 
    130       case 'n':
    131         printf("New folder %s\n",optarg);
    132         ret = newfolder_function(optarg);
    133         break;
    134 
    135       case 't':
    136         printf("Send track %s\n",optarg);
    137         split_arg(optarg,&arg1,&arg2);
    138         ret = sendtrack_function(arg1,arg2,NULL,NULL,NULL,NULL,NULL,NULL,0,0,0,0,0);
    139         break;
    140       }
    141     }
    142 
    143     if (optind < argc) {
    144       printf("Unknown options: ");
    145       while (optind < argc)
    146         printf("%s ", argv[optind++]);
    147       printf("\n");
    148     }
    149   }
    150 
    151   LIBMTP_Release_Device(device);
    152 
    153   return ret;
    154 }
    155