Code Sync up from tizen_2.4
[platform/upstream/libmtp.git] / examples / sendtr.c
1 /**
2  * \file sendtr.c
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.
7  *
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>
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.
29  */
30
31 #include <stdlib.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <libgen.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #ifdef HAVE_LANGINFO_H
39 #include <langinfo.h>
40 #endif
41
42 #include "config.h"
43 #include "common.h"
44 #include "util.h"
45 #include "connect.h"
46 #include "libmtp.h"
47 #include "pathutils.h"
48
49 extern LIBMTP_folder_t *folders;
50 extern LIBMTP_file_t *files;
51 extern LIBMTP_mtpdevice_t *device;
52
53 void sendtrack_usage (void)
54 {
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");
60 }
61
62 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
63 {
64   char *cp, *bp;
65
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     }
76
77     cp = strrchr(buffer, '\n');
78     if ( cp != NULL ) *cp = '\0';
79
80     bp = buffer;
81     while ( bp != cp ) {
82       if ( *bp != ' ' && *bp != '\t' ) return bp;
83       bp++;
84     }
85
86     if (! required) return bp;
87   }
88 }
89
90 static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
91 {
92   LIBMTP_album_t *album;
93   LIBMTP_album_t *album_orig;
94   LIBMTP_album_t *found_album = NULL;
95   int ret;
96
97   /* Look for the album */
98   album = LIBMTP_Get_Album_List(device);
99   album_orig = album;
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 */
110       found_album = album;
111       album = album->next;
112       found_album->next = NULL;
113     } else {
114       album = album->next;
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       }
131     }
132   }
133
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);
139     if (!tracks) {
140       printf("failed malloc in add_track_to_album()\n");
141       return 1;
142     }
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     }
148     tracks[found_album->no_tracks-1] = trackmeta->item_id;
149     found_album->tracks = tracks;
150     ret = LIBMTP_Update_Album(device, found_album);
151   } else {
152     uint32_t *trackid;
153
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 */
162   }
163
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
174   if (ret != 0) {
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);
179   } else {
180     printf("success!\n");
181   }
182   return ret;
183 }
184
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)
186 {
187   char *filename, *parent;
188   char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
189   char num[80];
190   uint64_t filesize;
191   uint32_t parent_id = 0;
192   struct stat sb;
193   LIBMTP_track_t *trackmeta;
194   LIBMTP_album_t *albuminfo;
195   int ret;
196
197   printf("Sending track %s to %s\n", from_path, to_path);
198
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");
204     return 1;
205   }
206
207   if (stat(from_path, &sb) == -1) {
208     fprintf(stderr, "%s: ", from_path);
209     perror("stat");
210     return 1;
211   }
212
213   if (!S_ISREG(sb.st_mode))
214     return 0;
215
216   filesize = sb.st_size;
217
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);
223     return 1;
224   }
225
226   if ((ptitle == NULL) && (quiet == 0)) {
227     if ( (ptitle = prompt("Title", title, 80, 0)) != NULL )
228       if (!strlen(ptitle)) ptitle = NULL;
229   }
230
231   if ((palbum == NULL) && (quiet == 0)) {
232     if ( (palbum = prompt("Album", album, 80, 0)) != NULL )
233       if (!strlen(palbum)) palbum = NULL;
234   }
235
236   if ((palbumartist == NULL) && (quiet == 0)) {
237     if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL )
238       if (!strlen(palbumartist)) palbumartist = NULL;
239   }
240
241   if ((partist == NULL) && (quiet == 0)) {
242     if ( (partist = prompt("Artist", artist, 80, 0)) != NULL )
243       if (!strlen(partist)) partist = NULL;
244   }
245
246   if ((pcomposer == NULL) && (quiet == 0)) {
247     if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL )
248       if (!strlen(pcomposer)) pcomposer = NULL;
249   }
250
251   if ((pgenre == NULL) && (quiet == 0)) {
252     if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL )
253       if (!strlen(pgenre)) pgenre = NULL;
254   }
255
256   if ((tracknum == 0) && (quiet == 0)) {
257     char *pnum;
258     if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
259       tracknum = 0;
260     else
261       tracknum = strtoul(pnum, 0, 10);
262   }
263
264   if ((year == 0) && (quiet == 0)) {
265     char *pnum;
266     if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
267       year = 0;
268     else
269       year = strtoul(pnum, 0, 10);
270   }
271
272   if ((length == 0) && (quiet == 0)) {
273     char *pnum;
274     if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
275       length = 0;
276     else
277       length = strtoul(pnum, 0, 10);
278   }
279
280   printf("Sending track:\n");
281   printf("Codec:     %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
282   if (ptitle) {
283     printf("Title:     %s\n", ptitle);
284     trackmeta->title = strdup(ptitle);
285   }
286
287   albuminfo = LIBMTP_new_album_t();
288
289   if (palbum) {
290     printf("Album:     %s\n", palbum);
291     trackmeta->album = strdup(palbum);
292     albuminfo->name = strdup(palbum);
293   }
294   if (palbumartist) {
295     printf("Album artist:    %s\n", palbumartist);
296     albuminfo->artist = strdup(palbumartist);
297   }
298   if (partist) {
299     printf("Artist:    %s\n", partist);
300     trackmeta->artist = strdup(partist);
301     if (palbumartist == NULL)
302       albuminfo->artist = strdup(partist);
303   }
304   if (pcomposer) {
305     printf("Writer or Composer:    %s\n", pcomposer);
306     trackmeta->composer = strdup(pcomposer);
307     albuminfo->composer = strdup(pcomposer);
308   }
309   if (pgenre) {
310     printf("Genre:     %s\n", pgenre);
311     trackmeta->genre = strdup(pgenre);
312     albuminfo->genre = strdup(pgenre);
313   }
314   if (year > 0) {
315     char tmp[80];
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);
320   }
321   if (tracknum > 0) {
322     printf("Track no:  %d\n", tracknum);
323     trackmeta->tracknumber = tracknum;
324   }
325   if (length > 0) {
326     printf("Length:    %d\n", length);
327     // Multiply by 1000 since this is in milliseconds
328     trackmeta->duration = length * 1000;
329   }
330   // We should always have this
331   if (filename != NULL) {
332     trackmeta->filename = strdup(filename);
333   }
334   trackmeta->filesize = filesize;
335   trackmeta->parent_id = parent_id;
336   {
337     int rc;
338     char *desc = NULL;
339     LIBMTP_devicestorage_t *pds = NULL;
340
341     if (0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED))) {
342       perror("LIBMTP_Get_Storage()");
343       exit(-1);
344     }
345     for (pds = device->storage; pds != NULL; pds = pds->next) {
346       if (pds->id == storageid) {
347         desc = strdup(pds->StorageDescription);
348         break;
349       }
350     }
351     if (NULL != desc) {
352       printf("Storage ID: %s (%u)\n", desc, storageid);
353       free(desc);
354     } else
355       printf("Storage ID: %u\n", storageid);
356     trackmeta->storage_id = storageid;
357   }
358
359   printf("Sending track...\n");
360   ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL);
361   printf("\n");
362   if (ret != 0) {
363     printf("Error sending track.\n");
364     LIBMTP_Dump_Errorstack(device);
365     LIBMTP_Clear_Errorstack(device);
366     ret = 1;
367   } else {
368     printf("New track ID: %d\n", trackmeta->item_id);
369   }
370
371   /* Add here add to album call */
372   if (palbum)
373     ret = add_track_to_album(albuminfo, trackmeta);
374
375   LIBMTP_destroy_album_t(albuminfo);
376   LIBMTP_destroy_track_t(trackmeta);
377
378   return ret;
379 }
380
381 int sendtrack_command (int argc, char **argv) {
382   int opt, ret;
383   extern int optind;
384   extern char *optarg;
385   char *partist = NULL;
386   char *palbumartist = NULL;
387   char *pcomposer = NULL;
388   char *ptitle = NULL;
389   char *pgenre = NULL;
390   char *pcodec = NULL;
391   char *palbum = NULL;
392   uint16_t tracknum = 0;
393   uint16_t length = 0;
394   uint16_t year = 0;
395   uint16_t quiet = 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 ) {
398     switch (opt) {
399     case 't':
400       ptitle = strdup(optarg);
401       break;
402     case 'a':
403       partist = strdup(optarg);
404       break;
405     case 'A':
406       palbumartist = strdup(optarg);
407       break;
408     case 'w':
409       pcomposer = strdup(optarg);
410       break;
411     case 'l':
412       palbum = strdup(optarg);
413       break;
414     case 'c':
415       pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA
416       break;
417     case 'g':
418       pgenre = strdup(optarg);
419       break;
420     case 'n':
421       tracknum = atoi(optarg);
422       break;
423     case 's':
424       storageid = (uint32_t) strtoul(optarg, NULL, 0);
425       break;
426     case 'd':
427       length = atoi(optarg);
428       break;
429     case 'y':
430       year = atoi(optarg);
431       break;
432     case 'q':
433       quiet = 1;
434       break;
435     default:
436       sendtrack_usage();
437     }
438   }
439   argc -= optind;
440   argv += optind;
441
442   if ( argc != 2 ) {
443     printf("You need to pass a filename and destination.\n");
444     sendtrack_usage();
445     return 0;
446   }
447
448   checklang();
449
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);
452   free (ptitle);
453   free (partist);
454   free (palbumartist);
455   free (pcomposer);
456   free (palbum);
457   free (pcodec);
458   free (pgenre);
459   return ret;
460 }