3 * Example program to send a music track to a device.
4 * This program is derived from the exact equivalent in libnjb.
5 * based on Enrique Jorreto Ledesma's work on the original program by
6 * Shaun Jackman and Linus Walleij.
8 * Copyright (C) 2003-2010 Linus Walleij <triad@df.lth.se>
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>
12 * Copyright (C) 2008 Nicolas Pennequin <nicolas.pennequin@free.fr>
13 * Copyright (C) 2008 Joseph Nahmias <joe@nahmias.net>
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.
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.
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.
36 #include <sys/types.h>
38 #ifdef HAVE_LANGINFO_H
47 #include "pathutils.h"
49 extern LIBMTP_folder_t *folders;
50 extern LIBMTP_file_t *files;
51 extern LIBMTP_mtpdevice_t *device;
53 void sendtrack_usage (void)
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");
58 fprintf(stderr, " -d <duration in seconds> -s <storage_id> <local path> <remote path>\n");
59 fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
62 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
67 fprintf(stdout, "%s> ", prompt);
68 if ( fgets(buffer, bufsz, stdin) == NULL ) {
72 fprintf(stderr, "EOF on stdin\n");
77 cp = strrchr(buffer, '\n');
78 if ( cp != NULL ) *cp = '\0';
82 if ( *bp != ' ' && *bp != '\t' ) return bp;
86 if (! required) return bp;
90 static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
92 LIBMTP_album_t *album;
93 LIBMTP_album_t *album_orig;
94 LIBMTP_album_t *found_album = NULL;
97 /* Look for the album */
98 album = LIBMTP_Get_Album_List(device);
100 while(album != NULL) {
101 if ((album->name != NULL &&
102 album->artist != NULL &&
103 !strcmp(album->name, albuminfo->name) &&
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))) {
109 /* Disconnect this album for later use */
112 found_album->next = NULL;
118 if (found_album == NULL) {
119 printf("Could not find Album. Retrying with only Album name\n");
121 while(album != NULL) {
122 if ((album->name != NULL) &&
123 !strcmp(album->name, albuminfo->name) ){
124 /* Disconnect this album for later use */
127 found_album->next = NULL;
134 if (found_album != NULL) {
137 tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t));
138 printf("Album \"%s\" found: updating...\n", found_album->name);
140 printf("failed malloc in add_track_to_album()\n");
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);
148 tracks[found_album->no_tracks-1] = trackmeta->item_id;
149 found_album->tracks = tracks;
150 ret = LIBMTP_Update_Album(device, found_album);
154 trackid = (uint32_t *)malloc(sizeof(uint32_t));
155 *trackid = trackmeta->item_id;
156 albuminfo->tracks = trackid;
157 albuminfo->no_tracks = 1;
158 albuminfo->storage_id = trackmeta->storage_id;
159 printf("Album doesn't exist: creating...\n");
160 ret = LIBMTP_Create_New_Album(device, albuminfo);
161 /* albuminfo will be destroyed later by caller */
164 /* Delete the earlier retrieved Album list */
171 LIBMTP_destroy_album_t(tmp);
175 printf("Error creating or updating album.\n");
176 printf("(This could be due to that your device does not support albums.)\n");
177 LIBMTP_Dump_Errorstack(device);
178 LIBMTP_Clear_Errorstack(device);
180 printf("success!\n");
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)
187 char *filename, *parent;
188 char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
191 uint32_t parent_id = 0;
193 LIBMTP_track_t *trackmeta;
194 LIBMTP_album_t *albuminfo;
197 printf("Sending track %s to %s\n", from_path, to_path);
199 parent = dirname(strdup(to_path));
200 filename = basename(strdup(to_path));
201 parent_id = parse_path (parent,files,folders);
202 if (parent_id == -1) {
203 printf("Parent folder could not be found, skipping\n");
207 if (stat(from_path, &sb) == -1) {
208 fprintf(stderr, "%s: ", from_path);
213 if (!S_ISREG(sb.st_mode))
216 filesize = sb.st_size;
218 trackmeta = LIBMTP_new_track_t();
219 trackmeta->filetype = find_filetype (from_path);
220 if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) {
221 printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
222 LIBMTP_destroy_track_t(trackmeta);
226 if ((ptitle == NULL) && (quiet == 0)) {
227 if ( (ptitle = prompt("Title", title, 80, 0)) != NULL )
228 if (!strlen(ptitle)) ptitle = NULL;
231 if ((palbum == NULL) && (quiet == 0)) {
232 if ( (palbum = prompt("Album", album, 80, 0)) != NULL )
233 if (!strlen(palbum)) palbum = NULL;
236 if ((palbumartist == NULL) && (quiet == 0)) {
237 if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL )
238 if (!strlen(palbumartist)) palbumartist = NULL;
241 if ((partist == NULL) && (quiet == 0)) {
242 if ( (partist = prompt("Artist", artist, 80, 0)) != NULL )
243 if (!strlen(partist)) partist = NULL;
246 if ((pcomposer == NULL) && (quiet == 0)) {
247 if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL )
248 if (!strlen(pcomposer)) pcomposer = NULL;
251 if ((pgenre == NULL) && (quiet == 0)) {
252 if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL )
253 if (!strlen(pgenre)) pgenre = NULL;
256 if ((tracknum == 0) && (quiet == 0)) {
258 if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
261 tracknum = strtoul(pnum, 0, 10);
264 if ((year == 0) && (quiet == 0)) {
266 if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
269 year = strtoul(pnum, 0, 10);
272 if ((length == 0) && (quiet == 0)) {
274 if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
277 length = strtoul(pnum, 0, 10);
280 printf("Sending track:\n");
281 printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
283 printf("Title: %s\n", ptitle);
284 trackmeta->title = strdup(ptitle);
287 albuminfo = LIBMTP_new_album_t();
290 printf("Album: %s\n", palbum);
291 trackmeta->album = strdup(palbum);
292 albuminfo->name = strdup(palbum);
295 printf("Album artist: %s\n", palbumartist);
296 albuminfo->artist = strdup(palbumartist);
299 printf("Artist: %s\n", partist);
300 trackmeta->artist = strdup(partist);
301 if (palbumartist == NULL)
302 albuminfo->artist = strdup(partist);
305 printf("Writer or Composer: %s\n", pcomposer);
306 trackmeta->composer = strdup(pcomposer);
307 albuminfo->composer = strdup(pcomposer);
310 printf("Genre: %s\n", pgenre);
311 trackmeta->genre = strdup(pgenre);
312 albuminfo->genre = strdup(pgenre);
316 printf("Year: %d\n", year);
317 snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year);
318 tmp[sizeof(tmp)-1] = '\0';
319 trackmeta->date = strdup(tmp);
322 printf("Track no: %d\n", tracknum);
323 trackmeta->tracknumber = tracknum;
326 printf("Length: %d\n", length);
327 // Multiply by 1000 since this is in milliseconds
328 trackmeta->duration = length * 1000;
330 // We should always have this
331 if (filename != NULL) {
332 trackmeta->filename = strdup(filename);
334 trackmeta->filesize = filesize;
335 trackmeta->parent_id = parent_id;
339 LIBMTP_devicestorage_t *pds = NULL;
341 if (0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED))) {
342 perror("LIBMTP_Get_Storage()");
345 for (pds = device->storage; pds != NULL; pds = pds->next) {
346 if (pds->id == storageid) {
347 desc = strdup(pds->StorageDescription);
352 printf("Storage ID: %s (%u)\n", desc, storageid);
355 printf("Storage ID: %u\n", storageid);
356 trackmeta->storage_id = storageid;
359 printf("Sending track...\n");
360 ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL);
363 printf("Error sending track.\n");
364 LIBMTP_Dump_Errorstack(device);
365 LIBMTP_Clear_Errorstack(device);
368 printf("New track ID: %d\n", trackmeta->item_id);
371 /* Add here add to album call */
373 ret = add_track_to_album(albuminfo, trackmeta);
375 LIBMTP_destroy_album_t(albuminfo);
376 LIBMTP_destroy_track_t(trackmeta);
381 int sendtrack_command (int argc, char **argv) {
385 char *partist = NULL;
386 char *palbumartist = NULL;
387 char *pcomposer = NULL;
392 uint16_t tracknum = 0;
396 uint32_t storageid = 0;
397 while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) {
400 ptitle = strdup(optarg);
403 partist = strdup(optarg);
406 palbumartist = strdup(optarg);
409 pcomposer = strdup(optarg);
412 palbum = strdup(optarg);
415 pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA
418 pgenre = strdup(optarg);
421 tracknum = atoi(optarg);
424 storageid = (uint32_t) strtoul(optarg, NULL, 0);
427 length = atoi(optarg);
443 printf("You need to pass a filename and destination.\n");
450 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);
451 ret = sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid, quiet);