sendtr.c

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

Generated on Thu Oct 4 18:37:44 2007 for libmtp by  doxygen 1.5.2