blob: 6acfb8be1f1910b94f490cd38a8e471ee67a985a [file] [log] [blame]
Linus Walleijc3a6eeb2010-01-30 07:32:41 +00001/**
Linus Walleij543badf2007-02-05 19:07:38 +00002 * \file connect.c
3 * Main programs implementing several utilities in one.
4 *
5 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
Linus Walleij843210f2010-10-17 22:40:45 +00006 * Copyright (C) 2008-2010 Linus Walleij <triad@df.lth.se>
Linus Walleij543badf2007-02-05 19:07:38 +00007 *
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 */
Linus Walleij2eaaff02009-01-15 21:30:36 +000023#include <stdlib.h>
cjdebenhcb4ac9f2006-11-03 02:00:49 +000024#include <libgen.h>
25#include <getopt.h>
Linus Walleij59765e82008-08-15 07:17:12 +000026#include <string.h>
Linus Walleij843210f2010-10-17 22:40:45 +000027
cjdebenhcb4ac9f2006-11-03 02:00:49 +000028#include "common.h"
Linus Walleij59765e82008-08-15 07:17:12 +000029#include "util.h"
cjdebenhcb4ac9f2006-11-03 02:00:49 +000030#include "pathutils.h"
Linus Walleij843210f2010-10-17 22:40:45 +000031#include "connect.h"
cjdebenhcb4ac9f2006-11-03 02:00:49 +000032
33LIBMTP_folder_t *folders;
34LIBMTP_file_t *files;
35LIBMTP_mtpdevice_t *device;
36
Linus Walleij843210f2010-10-17 22:40:45 +000037static void
cjdebenhcb4ac9f2006-11-03 02:00:49 +000038split_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
Linus Walleij843210f2010-10-17 22:40:45 +000050static void
cjdebenhcb4ac9f2006-11-03 02:00:49 +000051usage(void)
52{
cjdebenhdfce8862006-11-03 02:08:45 +000053 printf("Usage: connect <command1> <command2>\n");
54 printf("Commands: --delete [filename]\n");
Linus Walleij0b062e82009-01-17 14:59:46 +000055 printf(" --sendfile [source] [destination]\n");
56 printf(" --sendtrack [source] [destination]\n");
57 printf(" --getfile [source] [destination]\n");
cjdebenhdfce8862006-11-03 02:08:45 +000058 printf(" --newfolder [foldername]\n");
cjdebenhcb4ac9f2006-11-03 02:00:49 +000059}
60
Linus Walleij56c63952008-06-08 21:55:58 +000061
cjdebenhcb4ac9f2006-11-03 02:00:49 +000062int main (int argc, char **argv)
63{
Linus Walleij843210f2010-10-17 22:40:45 +000064 int ret = 0;
65
Linus Walleij56c63952008-06-08 21:55:58 +000066 checklang();
cjdebenhcb4ac9f2006-11-03 02:00:49 +000067
68 LIBMTP_Init();
tedbullockcd9f4992007-03-29 06:00:40 +000069
70 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
71
cjdebenhcb4ac9f2006-11-03 02:00:49 +000072 device = LIBMTP_Get_First_Device();
73 if (device == NULL) {
74 printf("No devices.\n");
75 return 0;
76 }
Richard Lowdc0b6c72006-11-13 09:22:23 +000077 files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL);
cjdebenhcb4ac9f2006-11-03 02:00:49 +000078 folders = LIBMTP_Get_Folder_List (device);
79
cjdebenh4a99f502006-11-09 22:03:05 +000080 if ((strncmp(basename(argv[0]),"mtp-delfile",11) == 0) || (strncmp(basename(argv[0]),"delfile",7) == 0)) {
Linus Walleij843210f2010-10-17 22:40:45 +000081 ret = delfile_command(argc,argv);
cjdebenh4a99f502006-11-09 22:03:05 +000082 } else if ((strncmp(basename(argv[0]),"mtp-getfile",13) == 0) || (strncmp(basename(argv[0]),"getfile",9) == 0)) {
Linus Walleij843210f2010-10-17 22:40:45 +000083 ret = getfile_command(argc,argv);
cjdebenh71f45f02006-11-08 02:19:57 +000084 } else if ((strncmp(basename(argv[0]),"mtp-newfolder",13) == 0) || (strncmp(basename(argv[0]),"newfolder",9) == 0)) {
Linus Walleij843210f2010-10-17 22:40:45 +000085 ret = newfolder_command(argc,argv);
cjdebenh4a99f502006-11-09 22:03:05 +000086 } else if ((strncmp(basename(argv[0]),"mtp-sendfile",11) == 0) || (strncmp(basename(argv[0]),"sendfile",7) == 0)) {
Linus Walleij843210f2010-10-17 22:40:45 +000087 ret = sendfile_command(argc, argv);
cjdebenh4a99f502006-11-09 22:03:05 +000088 } else if ((strncmp(basename(argv[0]),"mtp-sendtr",10) == 0) || (strncmp(basename(argv[0]),"sendtr",6) == 0)) {
Linus Walleij843210f2010-10-17 22:40:45 +000089 ret = sendtrack_command(argc, argv);
Linus Walleijc3a6eeb2010-01-30 07:32:41 +000090 } else {
Linus Walleij2242b022009-01-02 01:44:00 +000091 if ( argc < 2 ) {
92 usage ();
93 return 1;
94 }
95
cjdebenhcb4ac9f2006-11-03 02:00:49 +000096 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 };
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000106
cjdebenh71f45f02006-11-08 02:19:57 +0000107 int c = getopt_long (argc, argv, "d:f:g:n:t:", long_options, &option_index);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000108 if (c == -1)
109 break;
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000110
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000111 char *arg1, *arg2;
112 switch (c) {
113 case 'd':
114 printf("Delete %s\n",optarg);
Linus Walleij843210f2010-10-17 22:40:45 +0000115 ret = delfile_function(optarg);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000116 break;
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000117
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000118 case 'f':
119 printf("Send file %s\n",optarg);
120 split_arg(optarg,&arg1,&arg2);
Linus Walleij843210f2010-10-17 22:40:45 +0000121 ret = sendfile_function(arg1,arg2);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000122 break;
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000123
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000124 case 'g':
125 printf("Get file %s\n",optarg);
126 split_arg(optarg,&arg1,&arg2);
Linus Walleij843210f2010-10-17 22:40:45 +0000127 ret = getfile_function(arg1,arg2);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000128 break;
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000129
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000130 case 'n':
131 printf("New folder %s\n",optarg);
Linus Walleij843210f2010-10-17 22:40:45 +0000132 ret = newfolder_function(optarg);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000133 break;
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000134
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000135 case 't':
136 printf("Send track %s\n",optarg);
137 split_arg(optarg,&arg1,&arg2);
Linus Walleij843210f2010-10-17 22:40:45 +0000138 ret = sendtrack_function(arg1,arg2,NULL,NULL,NULL,NULL,NULL,NULL,0,0,0,0,0);
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000139 break;
140 }
141 }
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000142
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000143 if (optind < argc) {
144 printf("Unknown options: ");
145 while (optind < argc)
146 printf("%s ", argv[optind++]);
147 printf("\n");
148 }
149 }
Linus Walleijc3a6eeb2010-01-30 07:32:41 +0000150
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000151 LIBMTP_Release_Device(device);
152
Linus Walleij843210f2010-10-17 22:40:45 +0000153 return ret;
cjdebenhcb4ac9f2006-11-03 02:00:49 +0000154}