Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 1 | /** |
| 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. |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 5 | * based on Enrique Jorreto Ledesma's work on the original program by |
| 6 | * Shaun Jackman and Linus Walleij. |
Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 7 | * |
Linus Walleij | 2242b02 | 2009-01-02 01:44:00 +0000 | [diff] [blame] | 8 | * Copyright (C) 2003-2009 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 | 2f1b640 | 2009-06-15 19:49:33 +0000 | [diff] [blame] | 31 | #include "config.h" |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 32 | #include "common.h" |
Linus Walleij | 59765e8 | 2008-08-15 07:17:12 +0000 | [diff] [blame] | 33 | #include "util.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <limits.h> |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 36 | #include <string.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 37 | #include <libgen.h> |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 38 | #include <sys/stat.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <fcntl.h> |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 41 | #ifdef HAVE_LANGINFO_H |
| 42 | #include <langinfo.h> |
| 43 | #endif |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 44 | #include "libmtp.h" |
| 45 | #include "pathutils.h" |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 46 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 47 | extern LIBMTP_folder_t *folders; |
| 48 | extern LIBMTP_file_t *files; |
| 49 | extern LIBMTP_mtpdevice_t *device; |
| 50 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 51 | int sendtrack_function (char *, char *, char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t, uint32_t, uint16_t); |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 52 | void sendtrack_command (int, char **); |
| 53 | void sendtrack_usage (void); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 54 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 55 | void sendtrack_usage (void) |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 56 | { |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 57 | fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n"); |
| 58 | fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n"); |
| 59 | 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] | 60 | 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] | 61 | fprintf(stderr, "(-q means the program will not ask for missing information.)\n"); |
| 62 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 63 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 64 | static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required) |
| 65 | { |
| 66 | char *cp, *bp; |
| 67 | |
| 68 | while (1) { |
| 69 | fprintf(stdout, "%s> ", prompt); |
| 70 | if ( fgets(buffer, bufsz, stdin) == NULL ) { |
| 71 | if (ferror(stdin)) { |
| 72 | perror("fgets"); |
| 73 | } else { |
| 74 | fprintf(stderr, "EOF on stdin\n"); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | cp = strrchr(buffer, '\n'); |
| 80 | if ( cp != NULL ) *cp = '\0'; |
| 81 | |
| 82 | bp = buffer; |
| 83 | while ( bp != cp ) { |
| 84 | if ( *bp != ' ' && *bp != '\t' ) return bp; |
| 85 | bp++; |
| 86 | } |
| 87 | |
| 88 | if (! required) return bp; |
| 89 | } |
| 90 | } |
| 91 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 92 | static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta) |
| 93 | { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 94 | LIBMTP_album_t *album; |
| 95 | LIBMTP_album_t *found_album = NULL; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 96 | int ret; |
| 97 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 98 | /* Look for the album */ |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 99 | album = LIBMTP_Get_Album_List(device); |
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 { |
| 114 | LIBMTP_album_t *tmp; |
| 115 | |
| 116 | tmp = album; |
| 117 | album = album->next; |
| 118 | LIBMTP_destroy_album_t(tmp); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 119 | } |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 122 | if (found_album != NULL) { |
| 123 | uint32_t *tracks; |
| 124 | |
| 125 | tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t)); |
| 126 | printf("Album \"%s\" found: updating...\n", found_album->name); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 127 | if (!tracks) { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 128 | printf("failed malloc in add_track_to_album()\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 129 | return 1; |
| 130 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 131 | found_album->no_tracks++; |
| 132 | if (found_album->tracks != NULL) { |
| 133 | memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t)); |
| 134 | free(found_album->tracks); |
| 135 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 136 | tracks[found_album->no_tracks-1] = trackmeta->item_id; |
Linus Walleij | bbd9142 | 2008-02-12 12:00:51 +0000 | [diff] [blame] | 137 | found_album->tracks = tracks; |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 138 | ret = LIBMTP_Update_Album(device, found_album); |
| 139 | LIBMTP_destroy_album_t(found_album); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 140 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 141 | uint32_t *trackid; |
| 142 | |
| 143 | trackid = (uint32_t *)malloc(sizeof(uint32_t)); |
| 144 | *trackid = trackmeta->item_id; |
| 145 | albuminfo->tracks = trackid; |
| 146 | albuminfo->no_tracks = 1; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 147 | albuminfo->storage_id = trackmeta->storage_id; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 148 | printf("Album doesn't exist: creating...\n"); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 149 | ret = LIBMTP_Create_New_Album(device, albuminfo); |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 150 | /* albuminfo will be destroyed later by caller */ |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (ret != 0) { |
| 154 | printf("Error creating or updating album.\n"); |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 155 | 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] | 156 | LIBMTP_Dump_Errorstack(device); |
| 157 | LIBMTP_Clear_Errorstack(device); |
| 158 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 159 | printf("success!\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 160 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 161 | return ret; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 164 | 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] | 165 | { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 166 | char *filename, *parent; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 167 | char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80]; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 168 | char num[80]; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 169 | uint64_t filesize; |
Linus Walleij | 3eb115c | 2006-06-05 10:28:24 +0000 | [diff] [blame] | 170 | uint32_t parent_id = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 171 | struct stat sb; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 172 | LIBMTP_track_t *trackmeta; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 173 | LIBMTP_album_t *albuminfo; |
| 174 | int ret; |
| 175 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 176 | printf("Sending track %s to %s\n", from_path, to_path); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 177 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 178 | trackmeta = LIBMTP_new_track_t(); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 179 | albuminfo = LIBMTP_new_album_t(); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 180 | |
Linus Walleij | eb14439 | 2008-08-28 14:43:38 +0000 | [diff] [blame] | 181 | parent = dirname(strdup(to_path)); |
| 182 | filename = basename(strdup(to_path)); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 183 | parent_id = parse_path (parent,files,folders); |
| 184 | if (parent_id == -1) { |
| 185 | printf("Parent folder could not be found, skipping\n"); |
| 186 | return 1; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 187 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 188 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 189 | if ( stat(from_path, &sb) == -1 ) { |
| 190 | fprintf(stderr, "%s: ", from_path); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 191 | perror("stat"); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 192 | return 1; |
| 193 | } else if (S_ISREG (sb.st_mode)) { |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 194 | filesize = sb.st_size; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 195 | trackmeta->filetype = find_filetype (from_path); |
Linus Walleij | 4a712ef | 2008-05-04 19:21:25 +0000 | [diff] [blame] | 196 | if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) { |
Linus Walleij | 10e0ce7 | 2008-05-04 19:20:33 +0000 | [diff] [blame] | 197 | printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 198 | return 1; |
| 199 | } |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 200 | |
| 201 | if ((ptitle == NULL) && (quiet == 0)) { |
| 202 | if ( (ptitle = prompt("Title", title, 80, 0)) != NULL ) |
| 203 | if (!strlen(ptitle)) ptitle = NULL; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 204 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 205 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 206 | if ((palbum == NULL) && (quiet == 0)) { |
| 207 | if ( (palbum = prompt("Album", album, 80, 0)) != NULL ) |
| 208 | if (!strlen(palbum)) palbum = NULL; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 209 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 210 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 211 | if ((palbumartist == NULL) && (quiet == 0)) { |
| 212 | if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL ) |
| 213 | if (!strlen(palbumartist)) palbumartist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 214 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 215 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 216 | if ((partist == NULL) && (quiet == 0)) { |
| 217 | if ( (partist = prompt("Artist", artist, 80, 0)) != NULL ) |
| 218 | if (!strlen(partist)) partist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 219 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 220 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 221 | if ((pcomposer == NULL) && (quiet == 0)) { |
| 222 | if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL ) |
| 223 | if (!strlen(pcomposer)) pcomposer = NULL; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 224 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 225 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 226 | if ((pgenre == NULL) && (quiet == 0)) { |
| 227 | if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL ) |
| 228 | if (!strlen(pgenre)) pgenre = NULL; |
| 229 | } |
| 230 | |
| 231 | if ((tracknum == 0) && (quiet == 0)) { |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 232 | char *pnum; |
| 233 | if ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 234 | tracknum = 0; |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 235 | else |
| 236 | tracknum = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 237 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 238 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 239 | if ((year == 0) && (quiet == 0)) { |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 240 | char *pnum; |
| 241 | if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 242 | year = 0; |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 243 | else |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 244 | year = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 245 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 246 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 247 | if ((length == 0) && (quiet == 0)) { |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 248 | char *pnum; |
| 249 | if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 250 | length = 0; |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 251 | else |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 252 | length = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 253 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 254 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 255 | printf("Sending track:\n"); |
| 256 | printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
| 257 | if (ptitle) { |
| 258 | printf("Title: %s\n", ptitle); |
| 259 | trackmeta->title = strdup(ptitle); |
| 260 | } |
| 261 | if (palbum) { |
| 262 | printf("Album: %s\n", palbum); |
| 263 | trackmeta->album = strdup(palbum); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 264 | albuminfo->name = strdup(palbum); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 265 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 266 | if (palbumartist) { |
| 267 | printf("Album artist: %s\n", palbumartist); |
| 268 | albuminfo->artist = strdup(palbumartist); |
| 269 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 270 | if (partist) { |
| 271 | printf("Artist: %s\n", partist); |
| 272 | trackmeta->artist = strdup(partist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 273 | if (palbumartist == NULL) |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 274 | albuminfo->artist = strdup(partist); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 275 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 276 | |
| 277 | if (pcomposer) { |
| 278 | printf("Writer or Composer: %s\n", pcomposer); |
| 279 | trackmeta->composer = strdup(pcomposer); |
| 280 | albuminfo->composer = strdup(pcomposer); |
| 281 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 282 | if (pgenre) { |
| 283 | printf("Genre: %s\n", pgenre); |
| 284 | trackmeta->genre = strdup(pgenre); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 285 | albuminfo->genre = strdup(pgenre); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 286 | } |
| 287 | if (year > 0) { |
| 288 | char tmp[80]; |
| 289 | printf("Year: %d\n", year); |
| 290 | snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year); |
| 291 | tmp[sizeof(tmp)-1] = '\0'; |
| 292 | trackmeta->date = strdup(tmp); |
| 293 | } |
| 294 | if (tracknum > 0) { |
| 295 | printf("Track no: %d\n", tracknum); |
| 296 | trackmeta->tracknumber = tracknum; |
| 297 | } |
| 298 | if (length > 0) { |
| 299 | printf("Length: %d\n", length); |
| 300 | // Multiply by 1000 since this is in milliseconds |
| 301 | trackmeta->duration = length * 1000; |
| 302 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 303 | // We should always have this |
| 304 | if (filename != NULL) { |
| 305 | trackmeta->filename = strdup(filename); |
| 306 | } |
| 307 | trackmeta->filesize = filesize; |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 308 | trackmeta->parent_id = parent_id; |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 309 | { |
| 310 | int rc; |
| 311 | char *desc = NULL; |
| 312 | LIBMTP_devicestorage_t *pds = NULL; |
| 313 | |
| 314 | if ( 0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED)) ) |
| 315 | { |
| 316 | perror("LIBMTP_Get_Storage()"); |
| 317 | exit(-1); |
| 318 | } |
| 319 | for (pds = device->storage; pds != NULL; pds = pds->next) |
| 320 | { |
| 321 | if (pds->id == storageid) |
| 322 | { |
| 323 | desc = strdup(pds->StorageDescription); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | if (NULL != desc) |
| 328 | { |
| 329 | printf("Storage ID: %s (%u)\n", desc, storageid); |
| 330 | free(desc); |
| 331 | } |
| 332 | else |
| 333 | printf("Storage ID: %u\n", storageid); |
| 334 | trackmeta->storage_id = storageid; |
| 335 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 336 | |
| 337 | printf("Sending track...\n"); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 338 | ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL); |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 339 | printf("\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 340 | if (ret != 0) { |
| 341 | printf("Error sending track.\n"); |
| 342 | LIBMTP_Dump_Errorstack(device); |
| 343 | LIBMTP_Clear_Errorstack(device); |
| 344 | } else { |
| 345 | printf("New track ID: %d\n", trackmeta->item_id); |
| 346 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 347 | |
Linus Walleij | f329662 | 2008-09-04 20:53:56 +0000 | [diff] [blame] | 348 | /* Add here add to album call */ |
Linus Walleij | 2242b02 | 2009-01-02 01:44:00 +0000 | [diff] [blame] | 349 | if (palbum) |
| 350 | ret = add_track_to_album(albuminfo, trackmeta); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 351 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 352 | LIBMTP_destroy_album_t(albuminfo); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 353 | LIBMTP_destroy_track_t(trackmeta); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 354 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 355 | return 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 356 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 359 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 360 | void sendtrack_command (int argc, char **argv) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 361 | int opt; |
| 362 | extern int optind; |
| 363 | extern char *optarg; |
| 364 | char *partist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 365 | char *palbumartist = NULL; |
| 366 | char *pcomposer = NULL; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 367 | char *ptitle = NULL; |
| 368 | char *pgenre = NULL; |
| 369 | char *pcodec = NULL; |
| 370 | char *palbum = NULL; |
| 371 | uint16_t tracknum = 0; |
| 372 | uint16_t length = 0; |
| 373 | uint16_t year = 0; |
| 374 | uint16_t quiet = 0; |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 375 | uint32_t storageid = 0; |
| 376 | 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] | 377 | switch (opt) { |
| 378 | case 't': |
| 379 | ptitle = strdup(optarg); |
| 380 | break; |
| 381 | case 'a': |
| 382 | partist = strdup(optarg); |
| 383 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame] | 384 | case 'A': |
| 385 | palbumartist = strdup(optarg); |
| 386 | break; |
| 387 | case 'w': |
| 388 | pcomposer = strdup(optarg); |
| 389 | break; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 390 | case 'l': |
| 391 | palbum = strdup(optarg); |
| 392 | break; |
| 393 | case 'c': |
| 394 | pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA |
| 395 | break; |
| 396 | case 'g': |
| 397 | pgenre = strdup(optarg); |
| 398 | break; |
| 399 | case 'n': |
| 400 | tracknum = atoi(optarg); |
| 401 | break; |
Linus Walleij | 4347fda | 2008-09-19 22:28:45 +0000 | [diff] [blame] | 402 | case 's': |
| 403 | storageid = (uint32_t) strtoul(optarg, NULL, 0); |
| 404 | break; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 405 | case 'd': |
| 406 | length = atoi(optarg); |
| 407 | break; |
| 408 | case 'y': |
| 409 | year = atoi(optarg); |
| 410 | break; |
| 411 | case 'q': |
| 412 | quiet = 1; |
| 413 | break; |
| 414 | default: |
| 415 | sendtrack_usage(); |
| 416 | } |
| 417 | } |
| 418 | argc -= optind; |
| 419 | argv += optind; |
| 420 | |
| 421 | if ( argc != 2 ) { |
| 422 | printf("You need to pass a filename and destination.\n"); |
| 423 | sendtrack_usage(); |
Linus Walleij | 2242b02 | 2009-01-02 01:44:00 +0000 | [diff] [blame] | 424 | return; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 425 | } |
Linus Walleij | 56c6395 | 2008-06-08 21:55:58 +0000 | [diff] [blame] | 426 | |
| 427 | checklang(); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 428 | |
Linus Walleij | 28ad9b7 | 2009-09-21 12:10:35 +0000 | [diff] [blame] | 429 | 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); |
| 430 | sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid, quiet); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 431 | } |