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