Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 1 | /** |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 2 | * \file connect.c |
| 3 | * Main programs implementing several utilities in one. |
| 4 | * |
| 5 | * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com> |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008-2010 Linus Walleij <triad@df.lth.se> |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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 | */ |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 24 | #include <libgen.h> |
| 25 | #include <getopt.h> |
Linus Walleij | 59765e8 | 2008-08-15 07:17:12 +0000 | [diff] [blame] | 26 | #include <string.h> |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 27 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 28 | #include "common.h" |
Linus Walleij | 59765e8 | 2008-08-15 07:17:12 +0000 | [diff] [blame] | 29 | #include "util.h" |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 30 | #include "pathutils.h" |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 31 | #include "connect.h" |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 32 | |
| 33 | LIBMTP_folder_t *folders; |
| 34 | LIBMTP_file_t *files; |
| 35 | LIBMTP_mtpdevice_t *device; |
| 36 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 37 | static void |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 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 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 50 | static void |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 51 | usage(void) |
| 52 | { |
cjdebenh | dfce886 | 2006-11-03 02:08:45 +0000 | [diff] [blame] | 53 | printf("Usage: connect <command1> <command2>\n"); |
| 54 | printf("Commands: --delete [filename]\n"); |
Linus Walleij | 0b062e8 | 2009-01-17 14:59:46 +0000 | [diff] [blame] | 55 | printf(" --sendfile [source] [destination]\n"); |
| 56 | printf(" --sendtrack [source] [destination]\n"); |
| 57 | printf(" --getfile [source] [destination]\n"); |
cjdebenh | dfce886 | 2006-11-03 02:08:45 +0000 | [diff] [blame] | 58 | printf(" --newfolder [foldername]\n"); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 61 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 62 | int main (int argc, char **argv) |
| 63 | { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 64 | int ret = 0; |
| 65 | |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 66 | checklang(); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 67 | |
| 68 | LIBMTP_Init(); |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 69 | |
| 70 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 71 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 72 | device = LIBMTP_Get_First_Device(); |
| 73 | if (device == NULL) { |
| 74 | printf("No devices.\n"); |
| 75 | return 0; |
| 76 | } |
Richard Low | dc0b6c7 | 2006-11-13 09:22:23 +0000 | [diff] [blame] | 77 | files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 78 | folders = LIBMTP_Get_Folder_List (device); |
| 79 | |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 80 | if ((strncmp(basename(argv[0]),"mtp-delfile",11) == 0) || (strncmp(basename(argv[0]),"delfile",7) == 0)) { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 81 | ret = delfile_command(argc,argv); |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 82 | } else if ((strncmp(basename(argv[0]),"mtp-getfile",13) == 0) || (strncmp(basename(argv[0]),"getfile",9) == 0)) { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 83 | ret = getfile_command(argc,argv); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 84 | } else if ((strncmp(basename(argv[0]),"mtp-newfolder",13) == 0) || (strncmp(basename(argv[0]),"newfolder",9) == 0)) { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 85 | ret = newfolder_command(argc,argv); |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 86 | } else if ((strncmp(basename(argv[0]),"mtp-sendfile",11) == 0) || (strncmp(basename(argv[0]),"sendfile",7) == 0)) { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 87 | ret = sendfile_command(argc, argv); |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 88 | } else if ((strncmp(basename(argv[0]),"mtp-sendtr",10) == 0) || (strncmp(basename(argv[0]),"sendtr",6) == 0)) { |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 89 | ret = sendtrack_command(argc, argv); |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 90 | } else { |
Linus Walleij | 2242b02 | 2009-01-02 01:44:00 +0000 | [diff] [blame] | 91 | if ( argc < 2 ) { |
| 92 | usage (); |
| 93 | return 1; |
| 94 | } |
| 95 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 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 | }; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 106 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 107 | int c = getopt_long (argc, argv, "d:f:g:n:t:", long_options, &option_index); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 108 | if (c == -1) |
| 109 | break; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 110 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 111 | char *arg1, *arg2; |
| 112 | switch (c) { |
| 113 | case 'd': |
| 114 | printf("Delete %s\n",optarg); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 115 | ret = delfile_function(optarg); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 116 | break; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 117 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 118 | case 'f': |
| 119 | printf("Send file %s\n",optarg); |
| 120 | split_arg(optarg,&arg1,&arg2); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 121 | ret = sendfile_function(arg1,arg2); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 122 | break; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 123 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 124 | case 'g': |
| 125 | printf("Get file %s\n",optarg); |
| 126 | split_arg(optarg,&arg1,&arg2); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 127 | ret = getfile_function(arg1,arg2); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 128 | break; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 129 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 130 | case 'n': |
| 131 | printf("New folder %s\n",optarg); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 132 | ret = newfolder_function(optarg); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 133 | break; |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 134 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 135 | case 't': |
| 136 | printf("Send track %s\n",optarg); |
| 137 | split_arg(optarg,&arg1,&arg2); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 138 | ret = sendtrack_function(arg1,arg2,NULL,NULL,NULL,NULL,NULL,NULL,0,0,0,0,0); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | } |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 142 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 143 | if (optind < argc) { |
| 144 | printf("Unknown options: "); |
| 145 | while (optind < argc) |
| 146 | printf("%s ", argv[optind++]); |
| 147 | printf("\n"); |
| 148 | } |
| 149 | } |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 150 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 151 | LIBMTP_Release_Device(device); |
| 152 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 153 | return ret; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 154 | } |