Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 1 | /** |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 2 | * \file sendtr.c |
| 3 | * Example program to send a music track to a device. |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 4 | * This program is derived from the exact equivalent in libnjb. |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 5 | * based on Enrique Jorreto Ledesma's work on the original program by |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 6 | * Shaun Jackman and Linus Walleij. |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 7 | * |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2003-2010 Linus Walleij <triad@df.lth.se> |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 9 | * Copyright (C) 2003-2005 Shaun Jackman |
| 10 | * Copyright (C) 2003-2005 Enrique Jorrete Ledesma |
| 11 | * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com> |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 12 | * Copyright (C) 2008 Nicolas Pennequin <nicolas.pennequin@free.fr> |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 13 | * Copyright (C) 2008 Joseph Nahmias <joe@nahmias.net> |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 14 | * |
| 15 | * This library is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU Lesser General Public |
| 17 | * License as published by the Free Software Foundation; either |
| 18 | * version 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This library is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * Lesser General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU Lesser General Public |
| 26 | * License along with this library; if not, write to the |
| 27 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 28 | * Boston, MA 02111-1307, USA. |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 29 | */ |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 30 | |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <limits.h> |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 33 | #include <string.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 34 | #include <libgen.h> |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <fcntl.h> |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 38 | #ifdef HAVE_LANGINFO_H |
| 39 | #include <langinfo.h> |
| 40 | #endif |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 41 | |
| 42 | #include "config.h" |
| 43 | #include "common.h" |
| 44 | #include "util.h" |
| 45 | #include "connect.h" |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 46 | #include "libmtp.h" |
| 47 | #include "pathutils.h" |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 48 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 49 | extern LIBMTP_folder_t *folders; |
| 50 | extern LIBMTP_file_t *files; |
| 51 | extern LIBMTP_mtpdevice_t *device; |
| 52 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 53 | void sendtrack_usage (void) |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 54 | { |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 55 | fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n"); |
| 56 | fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n"); |
| 57 | fprintf(stderr, " -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n"); |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 58 | fprintf(stderr, " -d <duration in seconds> -s <storage_id> <local path> <remote path>\n"); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 59 | fprintf(stderr, "(-q means the program will not ask for missing information.)\n"); |
| 60 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 61 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 62 | static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required) |
| 63 | { |
| 64 | char *cp, *bp; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 65 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 66 | while (1) { |
| 67 | fprintf(stdout, "%s> ", prompt); |
| 68 | if ( fgets(buffer, bufsz, stdin) == NULL ) { |
| 69 | if (ferror(stdin)) { |
| 70 | perror("fgets"); |
| 71 | } else { |
| 72 | fprintf(stderr, "EOF on stdin\n"); |
| 73 | } |
| 74 | return NULL; |
| 75 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 76 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 77 | cp = strrchr(buffer, '\n'); |
| 78 | if ( cp != NULL ) *cp = '\0'; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 79 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 80 | bp = buffer; |
| 81 | while ( bp != cp ) { |
| 82 | if ( *bp != ' ' && *bp != '\t' ) return bp; |
| 83 | bp++; |
| 84 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 85 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 86 | if (! required) return bp; |
| 87 | } |
| 88 | } |
| 89 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 90 | static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta) |
| 91 | { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 92 | LIBMTP_album_t *album; |
Venkateswara Rao Mandela | 0c6c8fc | 2012-05-16 13:08:50 +0200 | [diff] [blame] | 93 | LIBMTP_album_t *album_orig; |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 94 | LIBMTP_album_t *found_album = NULL; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 95 | int ret; |
| 96 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 97 | /* Look for the album */ |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 98 | album = LIBMTP_Get_Album_List(device); |
Venkateswara Rao Mandela | 0c6c8fc | 2012-05-16 13:08:50 +0200 | [diff] [blame] | 99 | album_orig = album; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 100 | while(album != NULL) { |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 101 | if ((album->name != NULL && |
Linus Walleij | bbd9142 | 2008-02-12 12:00:51 +0000 | [diff] [blame] | 102 | album->artist != NULL && |
| 103 | !strcmp(album->name, albuminfo->name) && |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 104 | !strcmp(album->artist, albuminfo->artist)) || |
| 105 | (album->name != NULL && |
| 106 | album->composer != NULL && |
| 107 | !strcmp(album->name, albuminfo->name) && |
| 108 | !strcmp(album->composer, albuminfo->composer))) { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 109 | /* Disconnect this album for later use */ |
| 110 | found_album = album; |
| 111 | album = album->next; |
| 112 | found_album->next = NULL; |
| 113 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 114 | album = album->next; |
Venkateswara Rao Mandela | 0c6c8fc | 2012-05-16 13:08:50 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | if (found_album == NULL) { |
| 119 | printf("Could not find Album. Retrying with only Album name\n"); |
| 120 | album = album_orig; |
| 121 | while(album != NULL) { |
| 122 | if ((album->name != NULL) && |
| 123 | !strcmp(album->name, albuminfo->name) ){ |
| 124 | /* Disconnect this album for later use */ |
| 125 | found_album = album; |
| 126 | album = album->next; |
| 127 | found_album->next = NULL; |
| 128 | } else { |
| 129 | album = album->next; |
| 130 | } |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 131 | } |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 132 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 133 | |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 134 | if (found_album != NULL) { |
| 135 | uint32_t *tracks; |
| 136 | |
| 137 | tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t)); |
| 138 | printf("Album \"%s\" found: updating...\n", found_album->name); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 139 | if (!tracks) { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 140 | printf("failed malloc in add_track_to_album()\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 141 | return 1; |
| 142 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 143 | found_album->no_tracks++; |
| 144 | if (found_album->tracks != NULL) { |
| 145 | memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t)); |
| 146 | free(found_album->tracks); |
| 147 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 148 | tracks[found_album->no_tracks-1] = trackmeta->item_id; |
Linus Walleij | bbd9142 | 2008-02-12 12:00:51 +0000 | [diff] [blame] | 149 | found_album->tracks = tracks; |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 150 | ret = LIBMTP_Update_Album(device, found_album); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 151 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 152 | uint32_t *trackid; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 153 | |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 154 | trackid = (uint32_t *)malloc(sizeof(uint32_t)); |
| 155 | *trackid = trackmeta->item_id; |
| 156 | albuminfo->tracks = trackid; |
| 157 | albuminfo->no_tracks = 1; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 158 | albuminfo->storage_id = trackmeta->storage_id; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 159 | printf("Album doesn't exist: creating...\n"); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 160 | ret = LIBMTP_Create_New_Album(device, albuminfo); |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 161 | /* albuminfo will be destroyed later by caller */ |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 162 | } |
Linus Walleij | c3a6eeb | 2010-01-30 07:32:41 +0000 | [diff] [blame] | 163 | |
Venkateswara Rao Mandela | 0c6c8fc | 2012-05-16 13:08:50 +0200 | [diff] [blame] | 164 | /* Delete the earlier retrieved Album list */ |
| 165 | album=album_orig; |
| 166 | while(album!=NULL){ |
| 167 | LIBMTP_album_t *tmp; |
| 168 | |
| 169 | tmp = album; |
| 170 | album = album->next; |
| 171 | LIBMTP_destroy_album_t(tmp); |
| 172 | } |
| 173 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 174 | if (ret != 0) { |
| 175 | printf("Error creating or updating album.\n"); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 176 | printf("(This could be due to that your device does not support albums.)\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 177 | LIBMTP_Dump_Errorstack(device); |
| 178 | LIBMTP_Clear_Errorstack(device); |
| 179 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 180 | printf("success!\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 181 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 182 | return ret; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 185 | int sendtrack_function(char * from_path, char * to_path, char *partist, char *palbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t tracknum, uint16_t length, uint16_t year, uint32_t storageid, uint16_t quiet) |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 186 | { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 187 | char *filename, *parent; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 188 | char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80]; |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 189 | char *to_path_copy = NULL; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 190 | char num[80]; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 191 | uint64_t filesize; |
Linus Walleij | 3eb115c | 2006-06-05 10:28:24 +0000 | [diff] [blame] | 192 | uint32_t parent_id = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 193 | struct stat sb; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 194 | LIBMTP_track_t *trackmeta; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 195 | LIBMTP_album_t *albuminfo; |
| 196 | int ret; |
| 197 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 198 | printf("Sending track %s to %s\n", from_path, to_path); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 199 | |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 200 | to_path_copy = strdup(to_path); |
| 201 | parent = dirname(to_path_copy); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 202 | parent_id = parse_path (parent,files,folders); |
| 203 | if (parent_id == -1) { |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 204 | free (to_path_copy); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 205 | printf("Parent folder could not be found, skipping\n"); |
| 206 | return 1; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 207 | } |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 208 | strcpy (to_path_copy,to_path); |
| 209 | filename = basename(to_path_copy); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 210 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 211 | if (stat(from_path, &sb) == -1) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 212 | fprintf(stderr, "%s: ", from_path); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 213 | perror("stat"); |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 214 | free (to_path_copy); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 215 | return 1; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 216 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 217 | |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 218 | if (!S_ISREG(sb.st_mode)) { |
| 219 | free (to_path_copy); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 220 | return 0; |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 221 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 222 | |
| 223 | filesize = sb.st_size; |
Marcus Meissner | 9959f5b | 2015-04-11 13:33:48 +0200 | [diff] [blame] | 224 | |
| 225 | trackmeta = LIBMTP_new_track_t(); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 226 | trackmeta->filetype = find_filetype (from_path); |
| 227 | if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) { |
| 228 | printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
Marcus Meissner | 9959f5b | 2015-04-11 13:33:48 +0200 | [diff] [blame] | 229 | LIBMTP_destroy_track_t(trackmeta); |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 230 | free (to_path_copy); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | if ((ptitle == NULL) && (quiet == 0)) { |
| 235 | if ( (ptitle = prompt("Title", title, 80, 0)) != NULL ) |
| 236 | if (!strlen(ptitle)) ptitle = NULL; |
| 237 | } |
| 238 | |
| 239 | if ((palbum == NULL) && (quiet == 0)) { |
| 240 | if ( (palbum = prompt("Album", album, 80, 0)) != NULL ) |
| 241 | if (!strlen(palbum)) palbum = NULL; |
| 242 | } |
| 243 | |
| 244 | if ((palbumartist == NULL) && (quiet == 0)) { |
| 245 | if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL ) |
| 246 | if (!strlen(palbumartist)) palbumartist = NULL; |
| 247 | } |
| 248 | |
| 249 | if ((partist == NULL) && (quiet == 0)) { |
| 250 | if ( (partist = prompt("Artist", artist, 80, 0)) != NULL ) |
| 251 | if (!strlen(partist)) partist = NULL; |
| 252 | } |
| 253 | |
| 254 | if ((pcomposer == NULL) && (quiet == 0)) { |
| 255 | if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL ) |
| 256 | if (!strlen(pcomposer)) pcomposer = NULL; |
| 257 | } |
| 258 | |
| 259 | if ((pgenre == NULL) && (quiet == 0)) { |
| 260 | if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL ) |
| 261 | if (!strlen(pgenre)) pgenre = NULL; |
| 262 | } |
| 263 | |
| 264 | if ((tracknum == 0) && (quiet == 0)) { |
| 265 | char *pnum; |
| 266 | if ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) |
| 267 | tracknum = 0; |
| 268 | else |
| 269 | tracknum = strtoul(pnum, 0, 10); |
| 270 | } |
| 271 | |
| 272 | if ((year == 0) && (quiet == 0)) { |
| 273 | char *pnum; |
| 274 | if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) |
| 275 | year = 0; |
| 276 | else |
| 277 | year = strtoul(pnum, 0, 10); |
| 278 | } |
| 279 | |
| 280 | if ((length == 0) && (quiet == 0)) { |
| 281 | char *pnum; |
| 282 | if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) |
| 283 | length = 0; |
| 284 | else |
| 285 | length = strtoul(pnum, 0, 10); |
| 286 | } |
| 287 | |
| 288 | printf("Sending track:\n"); |
| 289 | printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
| 290 | if (ptitle) { |
| 291 | printf("Title: %s\n", ptitle); |
| 292 | trackmeta->title = strdup(ptitle); |
| 293 | } |
Marcus Meissner | 9959f5b | 2015-04-11 13:33:48 +0200 | [diff] [blame] | 294 | |
| 295 | albuminfo = LIBMTP_new_album_t(); |
| 296 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 297 | if (palbum) { |
| 298 | printf("Album: %s\n", palbum); |
| 299 | trackmeta->album = strdup(palbum); |
| 300 | albuminfo->name = strdup(palbum); |
| 301 | } |
| 302 | if (palbumartist) { |
| 303 | printf("Album artist: %s\n", palbumartist); |
| 304 | albuminfo->artist = strdup(palbumartist); |
| 305 | } |
| 306 | if (partist) { |
| 307 | printf("Artist: %s\n", partist); |
| 308 | trackmeta->artist = strdup(partist); |
| 309 | if (palbumartist == NULL) |
| 310 | albuminfo->artist = strdup(partist); |
| 311 | } |
| 312 | if (pcomposer) { |
| 313 | printf("Writer or Composer: %s\n", pcomposer); |
| 314 | trackmeta->composer = strdup(pcomposer); |
| 315 | albuminfo->composer = strdup(pcomposer); |
| 316 | } |
| 317 | if (pgenre) { |
| 318 | printf("Genre: %s\n", pgenre); |
| 319 | trackmeta->genre = strdup(pgenre); |
| 320 | albuminfo->genre = strdup(pgenre); |
| 321 | } |
| 322 | if (year > 0) { |
| 323 | char tmp[80]; |
| 324 | printf("Year: %d\n", year); |
| 325 | snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year); |
| 326 | tmp[sizeof(tmp)-1] = '\0'; |
| 327 | trackmeta->date = strdup(tmp); |
| 328 | } |
| 329 | if (tracknum > 0) { |
| 330 | printf("Track no: %d\n", tracknum); |
| 331 | trackmeta->tracknumber = tracknum; |
| 332 | } |
| 333 | if (length > 0) { |
| 334 | printf("Length: %d\n", length); |
| 335 | // Multiply by 1000 since this is in milliseconds |
| 336 | trackmeta->duration = length * 1000; |
| 337 | } |
| 338 | // We should always have this |
| 339 | if (filename != NULL) { |
| 340 | trackmeta->filename = strdup(filename); |
| 341 | } |
| 342 | trackmeta->filesize = filesize; |
| 343 | trackmeta->parent_id = parent_id; |
| 344 | { |
| 345 | int rc; |
| 346 | char *desc = NULL; |
| 347 | LIBMTP_devicestorage_t *pds = NULL; |
| 348 | |
| 349 | if (0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED))) { |
| 350 | perror("LIBMTP_Get_Storage()"); |
| 351 | exit(-1); |
| 352 | } |
| 353 | for (pds = device->storage; pds != NULL; pds = pds->next) { |
| 354 | if (pds->id == storageid) { |
| 355 | desc = strdup(pds->StorageDescription); |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | if (NULL != desc) { |
| 360 | printf("Storage ID: %s (%u)\n", desc, storageid); |
| 361 | free(desc); |
| 362 | } else |
| 363 | printf("Storage ID: %u\n", storageid); |
| 364 | trackmeta->storage_id = storageid; |
| 365 | } |
| 366 | |
| 367 | printf("Sending track...\n"); |
| 368 | ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL); |
| 369 | printf("\n"); |
| 370 | if (ret != 0) { |
| 371 | printf("Error sending track.\n"); |
| 372 | LIBMTP_Dump_Errorstack(device); |
| 373 | LIBMTP_Clear_Errorstack(device); |
| 374 | ret = 1; |
| 375 | } else { |
| 376 | printf("New track ID: %d\n", trackmeta->item_id); |
| 377 | } |
| 378 | |
| 379 | /* Add here add to album call */ |
| 380 | if (palbum) |
| 381 | ret = add_track_to_album(albuminfo, trackmeta); |
| 382 | |
| 383 | LIBMTP_destroy_album_t(albuminfo); |
| 384 | LIBMTP_destroy_track_t(trackmeta); |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 385 | free (to_path_copy); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 386 | |
| 387 | return ret; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 388 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 389 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 390 | int sendtrack_command (int argc, char **argv) { |
Marcus Meissner | aca5c26 | 2015-04-11 13:26:49 +0200 | [diff] [blame] | 391 | int opt, ret; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 392 | extern int optind; |
| 393 | extern char *optarg; |
| 394 | char *partist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 395 | char *palbumartist = NULL; |
| 396 | char *pcomposer = NULL; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 397 | char *ptitle = NULL; |
| 398 | char *pgenre = NULL; |
| 399 | char *pcodec = NULL; |
| 400 | char *palbum = NULL; |
| 401 | uint16_t tracknum = 0; |
| 402 | uint16_t length = 0; |
| 403 | uint16_t year = 0; |
| 404 | uint16_t quiet = 0; |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 405 | uint32_t storageid = 0; |
| 406 | while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 407 | switch (opt) { |
| 408 | case 't': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 409 | free (ptitle); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 410 | ptitle = strdup(optarg); |
| 411 | break; |
| 412 | case 'a': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 413 | free (partist); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 414 | partist = strdup(optarg); |
| 415 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 416 | case 'A': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 417 | free (palbumartist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 418 | palbumartist = strdup(optarg); |
| 419 | break; |
| 420 | case 'w': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 421 | free (pcomposer); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 422 | pcomposer = strdup(optarg); |
| 423 | break; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 424 | case 'l': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 425 | free (palbum); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 426 | palbum = strdup(optarg); |
| 427 | break; |
| 428 | case 'c': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 429 | free (pcodec); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 430 | pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA |
| 431 | break; |
| 432 | case 'g': |
Marcus Meissner | ff7e545 | 2015-10-04 13:07:18 +0200 | [diff] [blame] | 433 | free (pgenre); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 434 | pgenre = strdup(optarg); |
| 435 | break; |
| 436 | case 'n': |
| 437 | tracknum = atoi(optarg); |
| 438 | break; |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 439 | case 's': |
| 440 | storageid = (uint32_t) strtoul(optarg, NULL, 0); |
| 441 | break; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 442 | case 'd': |
| 443 | length = atoi(optarg); |
| 444 | break; |
| 445 | case 'y': |
| 446 | year = atoi(optarg); |
| 447 | break; |
| 448 | case 'q': |
| 449 | quiet = 1; |
| 450 | break; |
| 451 | default: |
| 452 | sendtrack_usage(); |
| 453 | } |
| 454 | } |
| 455 | argc -= optind; |
| 456 | argv += optind; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 457 | |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 458 | if ( argc != 2 ) { |
| 459 | printf("You need to pass a filename and destination.\n"); |
| 460 | sendtrack_usage(); |
Marcus Meissner | 61fe60f | 2015-10-04 12:39:40 +0200 | [diff] [blame] | 461 | ret = 0; |
| 462 | } else { |
| 463 | checklang(); |
| 464 | printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d,%u,%d\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year, storageid, quiet); |
| 465 | ret = sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid, quiet); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 466 | } |
Marcus Meissner | aca5c26 | 2015-04-11 13:26:49 +0200 | [diff] [blame] | 467 | free (ptitle); |
| 468 | free (partist); |
| 469 | free (palbumartist); |
| 470 | free (pcomposer); |
| 471 | free (palbum); |
| 472 | free (pcodec); |
| 473 | free (pgenre); |
| 474 | return ret; |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 475 | } |