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 | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 8 | * Copyright (C) 2003-2008 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 | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 13 | * |
| 14 | * This library is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU Lesser General Public |
| 16 | * License as published by the Free Software Foundation; either |
| 17 | * version 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This library is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * Lesser General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Lesser General Public |
| 25 | * License along with this library; if not, write to the |
| 26 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 27 | * Boston, MA 02111-1307, USA. |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 28 | */ |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 29 | #define _LARGEFILE_SOURCE |
| 30 | #define _LARGEFILE64_SOURCE |
| 31 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 32 | #include <string.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 33 | #include <libgen.h> |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
| 36 | #include <fcntl.h> |
| 37 | #include "common.h" |
| 38 | #include "libmtp.h" |
| 39 | #include "pathutils.h" |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 40 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 41 | extern LIBMTP_folder_t *folders; |
| 42 | extern LIBMTP_file_t *files; |
| 43 | extern LIBMTP_mtpdevice_t *device; |
| 44 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 45 | int sendtrack_function (char *, char *, char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t); |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 46 | void sendtrack_command (int, char **); |
| 47 | void sendtrack_usage (void); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 48 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 49 | void sendtrack_usage (void) |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 50 | { |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 51 | fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n"); |
| 52 | fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n"); |
| 53 | fprintf(stderr, " -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n"); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 54 | fprintf(stderr, " -d <duration in seconds> <local path> <remote path>\n"); |
| 55 | fprintf(stderr, "(-q means the program will not ask for missing information.)\n"); |
| 56 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 57 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 58 | static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required) |
| 59 | { |
| 60 | char *cp, *bp; |
| 61 | |
| 62 | while (1) { |
| 63 | fprintf(stdout, "%s> ", prompt); |
| 64 | if ( fgets(buffer, bufsz, stdin) == NULL ) { |
| 65 | if (ferror(stdin)) { |
| 66 | perror("fgets"); |
| 67 | } else { |
| 68 | fprintf(stderr, "EOF on stdin\n"); |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | cp = strrchr(buffer, '\n'); |
| 74 | if ( cp != NULL ) *cp = '\0'; |
| 75 | |
| 76 | bp = buffer; |
| 77 | while ( bp != cp ) { |
| 78 | if ( *bp != ' ' && *bp != '\t' ) return bp; |
| 79 | bp++; |
| 80 | } |
| 81 | |
| 82 | if (! required) return bp; |
| 83 | } |
| 84 | } |
| 85 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 86 | static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta) |
| 87 | { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 88 | LIBMTP_album_t *album; |
| 89 | LIBMTP_album_t *found_album = NULL; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 90 | int ret; |
| 91 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 92 | /* Look for the album */ |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 93 | album = LIBMTP_Get_Album_List(device); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 94 | while(album != NULL) { |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 95 | if ((album->name != NULL && |
Linus Walleij | bbd9142 | 2008-02-12 12:00:51 +0000 | [diff] [blame] | 96 | album->artist != NULL && |
| 97 | !strcmp(album->name, albuminfo->name) && |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 98 | !strcmp(album->artist, albuminfo->artist)) || |
| 99 | (album->name != NULL && |
| 100 | album->composer != NULL && |
| 101 | !strcmp(album->name, albuminfo->name) && |
| 102 | !strcmp(album->composer, albuminfo->composer))) { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 103 | /* Disconnect this album for later use */ |
| 104 | found_album = album; |
| 105 | album = album->next; |
| 106 | found_album->next = NULL; |
| 107 | } else { |
| 108 | LIBMTP_album_t *tmp; |
| 109 | |
| 110 | tmp = album; |
| 111 | album = album->next; |
| 112 | LIBMTP_destroy_album_t(tmp); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 113 | } |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 116 | if (found_album != NULL) { |
| 117 | uint32_t *tracks; |
| 118 | |
| 119 | tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t)); |
| 120 | printf("Album \"%s\" found: updating...\n", found_album->name); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 121 | if (!tracks) { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 122 | printf("failed malloc in add_track_to_album()\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 125 | found_album->no_tracks++; |
| 126 | if (found_album->tracks != NULL) { |
| 127 | memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t)); |
| 128 | free(found_album->tracks); |
| 129 | } |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 130 | tracks[found_album->no_tracks-1] = trackmeta->item_id; |
Linus Walleij | bbd9142 | 2008-02-12 12:00:51 +0000 | [diff] [blame] | 131 | found_album->tracks = tracks; |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 132 | ret = LIBMTP_Update_Album(device, found_album); |
| 133 | LIBMTP_destroy_album_t(found_album); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 134 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 135 | uint32_t *trackid; |
| 136 | |
| 137 | trackid = (uint32_t *)malloc(sizeof(uint32_t)); |
| 138 | *trackid = trackmeta->item_id; |
| 139 | albuminfo->tracks = trackid; |
| 140 | albuminfo->no_tracks = 1; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 141 | printf("Album doesn't exist: creating...\n"); |
| 142 | ret = LIBMTP_Create_New_Album(device, albuminfo, 0); |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 143 | /* albuminfo will be destroyed later by caller */ |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (ret != 0) { |
| 147 | printf("Error creating or updating album.\n"); |
| 148 | LIBMTP_Dump_Errorstack(device); |
| 149 | LIBMTP_Clear_Errorstack(device); |
| 150 | } else { |
Linus Walleij | 9564013 | 2008-02-12 11:57:37 +0000 | [diff] [blame] | 151 | printf("success!\n"); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 152 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 153 | return ret; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 156 | 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) |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 157 | { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 158 | char *filename, *parent; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 159 | 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] | 160 | char num[80]; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 161 | uint64_t filesize; |
Linus Walleij | 3eb115c | 2006-06-05 10:28:24 +0000 | [diff] [blame] | 162 | uint32_t parent_id = 0; |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 163 | #ifdef __USE_LARGEFILE64 |
| 164 | struct stat64 sb; |
| 165 | #else |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 166 | struct stat sb; |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 167 | #endif |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 168 | LIBMTP_track_t *trackmeta; |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 169 | LIBMTP_album_t *albuminfo; |
| 170 | int ret; |
| 171 | |
| 172 | printf("Sending track %s to %s\n",from_path,to_path); |
| 173 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 174 | trackmeta = LIBMTP_new_track_t(); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 175 | albuminfo = LIBMTP_new_album_t(); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 176 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 177 | parent = dirname(to_path); |
| 178 | filename = basename(to_path); |
| 179 | parent_id = parse_path (parent,files,folders); |
| 180 | if (parent_id == -1) { |
| 181 | printf("Parent folder could not be found, skipping\n"); |
| 182 | return 1; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 183 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 184 | |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 185 | #ifdef __USE_LARGEFILE64 |
| 186 | if ( stat64(from_path, &sb) == -1 ) { |
| 187 | #else |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 188 | if ( stat(from_path, &sb) == -1 ) { |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 189 | #endif |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 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 | #ifdef __USE_LARGEFILE64 |
| 195 | filesize = sb.st_size; |
| 196 | #else |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 197 | filesize = (uint64_t) sb.st_size; |
Linus Walleij | 0535838 | 2007-08-06 20:46:35 +0000 | [diff] [blame] | 198 | #endif |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 199 | trackmeta->filetype = find_filetype (from_path); |
Linus Walleij | 02ee880 | 2006-12-30 17:27:58 +0000 | [diff] [blame] | 200 | if ((trackmeta->filetype != LIBMTP_FILETYPE_MP3) |
| 201 | && (trackmeta->filetype != LIBMTP_FILETYPE_WAV) |
| 202 | && (trackmeta->filetype != LIBMTP_FILETYPE_OGG) |
| 203 | && (trackmeta->filetype != LIBMTP_FILETYPE_MP4) |
| 204 | && (trackmeta->filetype != LIBMTP_FILETYPE_AAC) |
| 205 | && (trackmeta->filetype != LIBMTP_FILETYPE_M4A) |
| 206 | && (trackmeta->filetype != LIBMTP_FILETYPE_FLAC) |
| 207 | && (trackmeta->filetype != LIBMTP_FILETYPE_WMA)) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 208 | printf("Not a valid codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
Linus Walleij | 02ee880 | 2006-12-30 17:27:58 +0000 | [diff] [blame] | 209 | printf("Supported formats: MP3, WAV, OGG, MP4, AAC, M4A, FLAC, WMA\n"); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 210 | return 1; |
| 211 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 212 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 213 | if (ptitle == NULL) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 214 | ptitle = prompt("Title", title, 80, 0); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 215 | } |
| 216 | if (!strlen(ptitle)) |
| 217 | ptitle = NULL; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 218 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 219 | if (palbum == NULL) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 220 | palbum = prompt("Album", album, 80, 0); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 221 | } |
| 222 | if (!strlen(palbum)) |
| 223 | palbum = NULL; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 224 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 225 | if (palbumartist == NULL) { |
| 226 | palbumartist = prompt("Album artist", albumartist, 80, 0); |
| 227 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 228 | if (partist == NULL) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 229 | partist = prompt("Artist", artist, 80, 0); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 230 | } |
| 231 | if (!strlen(partist)) |
| 232 | partist = NULL; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 233 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 234 | if (pcomposer == NULL) { |
| 235 | pcomposer = prompt("Writer or Composer", composer, 80, 0); |
| 236 | } |
| 237 | if (!strlen(pcomposer)) |
| 238 | pcomposer = NULL; |
| 239 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 240 | if (pgenre == NULL) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 241 | pgenre = prompt("Genre", genre, 80, 0); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 242 | } |
| 243 | if (!strlen(pgenre)) |
| 244 | pgenre = NULL; |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 245 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 246 | if (tracknum == 0) { |
| 247 | char *pnum; |
| 248 | if ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) |
| 249 | tracknum = 0; |
| 250 | if ( strlen(pnum) ) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 251 | tracknum = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 252 | } else { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 253 | tracknum = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 256 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 257 | if (year == 0) { |
| 258 | char *pnum; |
| 259 | if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 260 | year = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 261 | if ( strlen(pnum) ) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 262 | year = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 263 | } else { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 264 | year = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 267 | |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 268 | if (length == 0) { |
| 269 | char *pnum; |
| 270 | if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 271 | length = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 272 | if ( strlen(pnum) ) { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 273 | length = strtoul(pnum, 0, 10); |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 274 | } else { |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 275 | length = 0; |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 278 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 279 | printf("Sending track:\n"); |
| 280 | printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); |
| 281 | if (ptitle) { |
| 282 | printf("Title: %s\n", ptitle); |
| 283 | trackmeta->title = strdup(ptitle); |
| 284 | } |
| 285 | if (palbum) { |
| 286 | printf("Album: %s\n", palbum); |
| 287 | trackmeta->album = strdup(palbum); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 288 | albuminfo->name = strdup(palbum); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 289 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 290 | if (palbumartist) { |
| 291 | printf("Album artist: %s\n", palbumartist); |
| 292 | albuminfo->artist = strdup(palbumartist); |
| 293 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 294 | if (partist) { |
| 295 | printf("Artist: %s\n", partist); |
| 296 | trackmeta->artist = strdup(partist); |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 297 | if (palbumartist == NULL) |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 298 | albuminfo->artist = strdup(partist); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 299 | } |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 300 | |
| 301 | if (pcomposer) { |
| 302 | printf("Writer or Composer: %s\n", pcomposer); |
| 303 | trackmeta->composer = strdup(pcomposer); |
| 304 | albuminfo->composer = strdup(pcomposer); |
| 305 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 306 | if (pgenre) { |
| 307 | printf("Genre: %s\n", pgenre); |
| 308 | trackmeta->genre = strdup(pgenre); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 309 | albuminfo->genre = strdup(pgenre); |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 310 | } |
| 311 | if (year > 0) { |
| 312 | char tmp[80]; |
| 313 | printf("Year: %d\n", year); |
| 314 | snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year); |
| 315 | tmp[sizeof(tmp)-1] = '\0'; |
| 316 | trackmeta->date = strdup(tmp); |
| 317 | } |
| 318 | if (tracknum > 0) { |
| 319 | printf("Track no: %d\n", tracknum); |
| 320 | trackmeta->tracknumber = tracknum; |
| 321 | } |
| 322 | if (length > 0) { |
| 323 | printf("Length: %d\n", length); |
| 324 | // Multiply by 1000 since this is in milliseconds |
| 325 | trackmeta->duration = length * 1000; |
| 326 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 327 | // We should always have this |
| 328 | if (filename != NULL) { |
| 329 | trackmeta->filename = strdup(filename); |
| 330 | } |
| 331 | trackmeta->filesize = filesize; |
| 332 | |
| 333 | printf("Sending track...\n"); |
| 334 | ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL, parent_id); |
Linus Walleij | d2778d1 | 2007-08-06 19:24:58 +0000 | [diff] [blame] | 335 | printf("\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 336 | if (ret != 0) { |
| 337 | printf("Error sending track.\n"); |
| 338 | LIBMTP_Dump_Errorstack(device); |
| 339 | LIBMTP_Clear_Errorstack(device); |
| 340 | } else { |
| 341 | printf("New track ID: %d\n", trackmeta->item_id); |
| 342 | } |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 343 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 344 | /* Add here add to album call */ |
| 345 | ret = add_track_to_album(albuminfo, trackmeta); |
| 346 | |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 347 | LIBMTP_destroy_album_t(albuminfo); |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 348 | LIBMTP_destroy_track_t(trackmeta); |
Linus Walleij | 75fe2bf | 2008-02-12 07:34:35 +0000 | [diff] [blame] | 349 | |
cjdebenh | cb4ac9f | 2006-11-03 02:00:49 +0000 | [diff] [blame] | 350 | return 0; |
Linus Walleij | 17e39f7 | 2006-02-23 15:54:28 +0000 | [diff] [blame] | 351 | } |
Linus Walleij | bde621f | 2006-02-22 16:11:35 +0000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 354 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 355 | void sendtrack_command (int argc, char **argv) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 356 | int opt; |
| 357 | extern int optind; |
| 358 | extern char *optarg; |
| 359 | char *partist = NULL; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 360 | char *palbumartist = NULL; |
| 361 | char *pcomposer = NULL; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 362 | char *ptitle = NULL; |
| 363 | char *pgenre = NULL; |
| 364 | char *pcodec = NULL; |
| 365 | char *palbum = NULL; |
| 366 | uint16_t tracknum = 0; |
| 367 | uint16_t length = 0; |
| 368 | uint16_t year = 0; |
| 369 | uint16_t quiet = 0; |
| 370 | char *lang; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 371 | while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:")) != -1 ) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 372 | switch (opt) { |
| 373 | case 't': |
| 374 | ptitle = strdup(optarg); |
| 375 | break; |
| 376 | case 'a': |
| 377 | partist = strdup(optarg); |
| 378 | break; |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 379 | case 'A': |
| 380 | palbumartist = strdup(optarg); |
| 381 | break; |
| 382 | case 'w': |
| 383 | pcomposer = strdup(optarg); |
| 384 | break; |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 385 | case 'l': |
| 386 | palbum = strdup(optarg); |
| 387 | break; |
| 388 | case 'c': |
| 389 | pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA |
| 390 | break; |
| 391 | case 'g': |
| 392 | pgenre = strdup(optarg); |
| 393 | break; |
| 394 | case 'n': |
| 395 | tracknum = atoi(optarg); |
| 396 | break; |
| 397 | case 'd': |
| 398 | length = atoi(optarg); |
| 399 | break; |
| 400 | case 'y': |
| 401 | year = atoi(optarg); |
| 402 | break; |
| 403 | case 'q': |
| 404 | quiet = 1; |
| 405 | break; |
| 406 | default: |
| 407 | sendtrack_usage(); |
| 408 | } |
| 409 | } |
| 410 | argc -= optind; |
| 411 | argv += optind; |
| 412 | |
| 413 | if ( argc != 2 ) { |
| 414 | printf("You need to pass a filename and destination.\n"); |
| 415 | sendtrack_usage(); |
| 416 | } |
| 417 | /* |
| 418 | * Check environment variables $LANG and $LC_CTYPE |
| 419 | * to see if we want to support UTF-8 unicode |
| 420 | */ |
| 421 | lang = getenv("LANG"); |
| 422 | if (lang != NULL) { |
| 423 | if (strlen(lang) > 5) { |
| 424 | char *langsuff = &lang[strlen(lang)-5]; |
| 425 | if (strcmp(langsuff, "UTF-8")) { |
| 426 | printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang); |
| 427 | printf("If you want to have support for diacritics and Unicode characters,\n"); |
| 428 | printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n"); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 429 | } |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Linus Walleij | 31b7429 | 2008-05-02 23:29:06 +0000 | [diff] [blame^] | 433 | printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year); |
| 434 | sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 435 | } |