libmtp: sendtr.c

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

# sendtr.c

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