libmtp: sendtr.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# sendtr.c

    00001 /*
    00002  * This program is derived from the exact equivalent in libnjb.
    00003  *
    00004  * This is an improved commandline track transfer program
    00005  * based on Enrique Jorreto Ledesma's work on the original program by
    00006  * Shaun Jackman and Linus Walleij.
    00007  */
    00008
    00009 #include <string.h>
    00010 #include <libgen.h>
    00011 #include <sys/stat.h>
    00012 #include <sys/types.h>
    00013 #include <fcntl.h>
    00014 #include "common.h"
    00015 #include "libmtp.h"
    00016 #include "pathutils.h"
    00017
    00018 extern LIBMTP_folder_t *folders;
    00019 extern LIBMTP_file_t *files;
    00020 extern LIBMTP_mtpdevice_t *device;
    00021
    00022 int sendtrack_function (char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t);
    00023 void sendtrack_command (int, char **);
    00024 void sendtrack_usage (void);
    00025
    00026 void sendtrack_usage (void)
    00027 {
    00028   fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ] -t <title> -a <artist> -l <album>\n");
    00029   fprintf(stderr, "       -c <codec> -g <genre> -n <track number> -y <year> \n");
    00030   fprintf(stderr, "       -d <duration in seconds> <local path> <remote path>\n");
    00031   fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
    00032 }
    00033
    00034 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
    00035 {
    00036   char *cp, *bp;
    00037
    00038   while (1) {
    00039     fprintf(stdout, "%s> ", prompt);
    00040     if ( fgets(buffer, bufsz, stdin) == NULL ) {
    00041       if (ferror(stdin)) {
    00042         perror("fgets");
    00043       } else {
    00044         fprintf(stderr, "EOF on stdin\n");
    00045       }
    00046       return NULL;
    00047     }
    00048
    00049     cp = strrchr(buffer, '\n');
    00050     if ( cp != NULL ) *cp = '\0';
    00051
    00052     bp = buffer;
    00053     while ( bp != cp ) {
    00054       if ( *bp != ' ' && *bp != '\t' ) return bp;
    00055       bp++;
    00056     }
    00057
    00058     if (! required) return bp;
    00059   }
    00060 }
    00061
    00062 int sendtrack_function(char * from_path, char * to_path, char *partist, char *ptitle, char *pgenre, char *palbum, uint16_t tracknum, uint16_t length, uint16_t year)
    00063 {
    00064   printf("Sending track %s to %s\n",from_path,to_path);
    00065   char *filename, *parent;
    00066   char artist[80], title[80], genre[80], album[80];
    00067   char num[80];
    00068   uint64_t filesize;
    00069   uint32_t parent_id = 0;
    00070   struct stat sb;
    00071   LIBMTP_track_t *trackmeta;
    00072   trackmeta = LIBMTP_new_track_t();
    00073
    00074   parent = dirname(to_path);
    00075   filename = basename(to_path);
    00076   parent_id = parse_path (parent,files,folders);
    00077   if (parent_id == -1) {
    00078     printf("Parent folder could not be found, skipping\n");
    00079     return 1;
    00080   }
    00081
    00082   if ( stat(from_path, &sb) == -1 ) {
    00083     fprintf(stderr, "%s: ", from_path);
    00084     perror("stat");
    00085     return 1;
    00086   } else if (S_ISREG (sb.st_mode)) {
    00087     filesize = (uint64_t) sb.st_size;
    00088     trackmeta->filetype = find_filetype (from_path);
    00089     if ((trackmeta->filetype != LIBMTP_FILETYPE_MP3)
    00090         && (trackmeta->filetype != LIBMTP_FILETYPE_WAV)
    00091         && (trackmeta->filetype != LIBMTP_FILETYPE_OGG)
    00092         && (trackmeta->filetype != LIBMTP_FILETYPE_MP4)
    00093         && (trackmeta->filetype != LIBMTP_FILETYPE_AAC)
    00094         && (trackmeta->filetype != LIBMTP_FILETYPE_M4A)
    00095         && (trackmeta->filetype != LIBMTP_FILETYPE_FLAC)
    00096         && (trackmeta->filetype != LIBMTP_FILETYPE_WMA)) {
    00097       printf("Not a valid codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
    00098       printf("Supported formats: MP3, WAV, OGG, MP4, AAC, M4A, FLAC, WMA\n");
    00099       return 1;
    00100     }
    00101
    00102     int ret;
    00103
    00104     if (ptitle == NULL) {
    00105       ptitle = prompt("Title", title, 80, 0);
    00106     }
    00107     if (!strlen(ptitle))
    00108       ptitle = NULL;
    00109
    00110
    00111     if (palbum == NULL) {
    00112       palbum = prompt("Album", album, 80, 0);
    00113     }
    00114     if (!strlen(palbum))
    00115       palbum = NULL;
    00116
    00117     if (partist == NULL) {
    00118       partist = prompt("Artist", artist, 80, 0);
    00119     }
    00120     if (!strlen(partist))
    00121       partist = NULL;
    00122
    00123     if (pgenre == NULL) {
    00124       pgenre = prompt("Genre", genre, 80, 0);
    00125     }
    00126     if (!strlen(pgenre))
    00127       pgenre = NULL;
    00128
    00129     if (tracknum == 0) {
    00130       char *pnum;
    00131       if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
    00132       tracknum = 0;
    00133       if ( strlen(pnum) ) {
    00134         tracknum = strtoul(pnum, 0, 10);
    00135       } else {
    00136         tracknum = 0;
    00137       }
    00138     }
    00139
    00140     if (year == 0) {
    00141       char *pnum;
    00142       if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
    00143         year = 0;
    00144       if ( strlen(pnum) ) {
    00145         year = strtoul(pnum, 0, 10);
    00146       } else {
    00147         year = 0;
    00148       }
    00149     }
    00150
    00151     if (length == 0) {
    00152       char *pnum;
    00153       if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
    00154         length = 0;
    00155       if ( strlen(pnum) ) {
    00156         length = strtoul(pnum, 0, 10);
    00157       } else {
    00158         length = 0;
    00159       }
    00160     }
    00161
    00162
    00163     printf("Sending track:\n");
    00164     printf("Codec:     %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
    00165     if (ptitle) {
    00166       printf("Title:     %s\n", ptitle);
    00167       trackmeta->title = strdup(ptitle);
    00168     }
    00169     if (palbum) {
    00170       printf("Album:     %s\n", palbum);
    00171       trackmeta->album = strdup(palbum);
    00172     }
    00173     if (partist) {
    00174       printf("Artist:    %s\n", partist);
    00175       trackmeta->artist = strdup(partist);
    00176     }
    00177     if (pgenre) {
    00178       printf("Genre:     %s\n", pgenre);
    00179       trackmeta->genre = strdup(pgenre);
    00180     }
    00181     if (year > 0) {
    00182       char tmp[80];
    00183       printf("Year:      %d\n", year);
    00184       snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year);
    00185       tmp[sizeof(tmp)-1] = '\0';
    00186       trackmeta->date = strdup(tmp);
    00187     }
    00188     if (tracknum > 0) {
    00189       printf("Track no:  %d\n", tracknum);
    00190       trackmeta->tracknumber = tracknum;
    00191     }
    00192     if (length > 0) {
    00193       printf("Length:    %d\n", length);
    00194       // Multiply by 1000 since this is in milliseconds
    00195       trackmeta->duration = length * 1000;
    00196     }
    00197     // We should always have this
    00198     if (filename != NULL) {
    00199       trackmeta->filename = strdup(filename);
    00200     }
    00201     trackmeta->filesize = filesize;
    00202
    00203     printf("Sending track...\n");
    00204     ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL, parent_id);
    00205     printf("New track ID: %d\n", trackmeta->item_id);
    00206
    00207     LIBMTP_destroy_track_t(trackmeta);
    00208
    00209     return 0;
    00210   }
    00211   return 0;
    00212 }
    00213
    00214 void sendtrack_command (int argc, char **argv) {
    00215     int opt;
    00216     extern int optind;
    00217     extern char *optarg;
    00218     char *partist = NULL;
    00219     char *ptitle = NULL;
    00220     char *pgenre = NULL;
    00221     char *pcodec = NULL;
    00222     char *palbum = NULL;
    00223     uint16_t tracknum = 0;
    00224     uint16_t length = 0;
    00225     uint16_t year = 0;
    00226     uint16_t quiet = 0;
    00227     char *lang;
    00228       while ( (opt = getopt(argc, argv, "qD:t:a:l:c:g:n:d:y:")) != -1 ) {
    00229         switch (opt) {
    00230         case 't':
    00231           ptitle = strdup(optarg);
    00232           break;
    00233         case 'a':
    00234           partist = strdup(optarg);
    00235           break;
    00236         case 'l':
    00237           palbum = strdup(optarg);
    00238           break;
    00239         case 'c':
    00240           pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA
    00241           break;
    00242         case 'g':
    00243           pgenre = strdup(optarg);
    00244           break;
    00245         case 'n':
    00246           tracknum = atoi(optarg);
    00247           break;
    00248         case 'd':
    00249           length = atoi(optarg);
    00250           break;
    00251         case 'y':
    00252           year = atoi(optarg);
    00253           break;
    00254         case 'q':
    00255           quiet = 1;
    00256           break;
    00257         default:
    00258           sendtrack_usage();
    00259         }
    00260       }
    00261       argc -= optind;
    00262       argv += optind;
    00263
    00264       if ( argc != 2 ) {
    00265         printf("You need to pass a filename and destination.\n");
    00266         sendtrack_usage();
    00267       }
    00268       /*
    00269  *        * Check environment variables $LANG and $LC_CTYPE
    00270  *               * to see if we want to support UTF-8 unicode
    00271  *                      */
    00272       lang = getenv("LANG");
    00273       if (lang != NULL) {
    00274         if (strlen(lang) > 5) {
    00275           char *langsuff = &lang[strlen(lang)-5];
    00276           if (strcmp(langsuff, "UTF-8")) {
    00277             printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang);
    00278             printf("If you want to have support for diacritics and Unicode characters,\n");
    00279             printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n");
    00280           }
    00281         }
    00282       }
    00283
    00284       printf("%s,%s,%s,%s,%s,%s,%d%d,%d\n",argv[0],argv[1],partist,ptitle,pgenre,palbum,tracknum, length, year);
    00285       sendtrack_function(argv[0],argv[1],partist,ptitle,pgenre,palbum, tracknum, length, year);
    00286 }
    00287