00001
00029 #define _LARGEFILE_SOURCE
00030 #define _LARGEFILE64_SOURCE
00031
00032 #include "common.h"
00033 #include <string.h>
00034 #include <libgen.h>
00035 #include <sys/stat.h>
00036 #include <sys/types.h>
00037 #include <fcntl.h>
00038 #ifdef HAVE_LANGINFO_H
00039 #include <langinfo.h>
00040 #endif
00041 #include "libmtp.h"
00042 #include "pathutils.h"
00043
00044 extern LIBMTP_folder_t *folders;
00045 extern LIBMTP_file_t *files;
00046 extern LIBMTP_mtpdevice_t *device;
00047
00048 int sendtrack_function (char *, char *, char *, char *, char *, char *, char *, char *, uint16_t, uint16_t, uint16_t);
00049 void sendtrack_command (int, char **);
00050 void sendtrack_usage (void);
00051
00052 void sendtrack_usage (void)
00053 {
00054 fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n");
00055 fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n");
00056 fprintf(stderr, " -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n");
00057 fprintf(stderr, " -d <duration in seconds> <local path> <remote path>\n");
00058 fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
00059 }
00060
00061 static void checklang(void)
00062 {
00063 char *langsuff = NULL;
00064 char *lang = getenv("LANG");
00065
00066 #ifdef HAVE_LANGINFO_H
00067 langsuff = nl_langinfo(CODESET);
00068 #else
00069
00070
00071
00072
00073 if (lang != NULL) {
00074 if (strlen(lang) > 5) {
00075 langsuff = &lang[strlen(lang)-5];
00076 }
00077 }
00078 #endif
00079 if (strcmp(langsuff, "UTF-8")) {
00080 printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang);
00081 printf("If you want to have support for diacritics and Unicode characters,\n");
00082 printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n");
00083 }
00084 }
00085
00086 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
00087 {
00088 char *cp, *bp;
00089
00090 while (1) {
00091 fprintf(stdout, "%s> ", prompt);
00092 if ( fgets(buffer, bufsz, stdin) == NULL ) {
00093 if (ferror(stdin)) {
00094 perror("fgets");
00095 } else {
00096 fprintf(stderr, "EOF on stdin\n");
00097 }
00098 return NULL;
00099 }
00100
00101 cp = strrchr(buffer, '\n');
00102 if ( cp != NULL ) *cp = '\0';
00103
00104 bp = buffer;
00105 while ( bp != cp ) {
00106 if ( *bp != ' ' && *bp != '\t' ) return bp;
00107 bp++;
00108 }
00109
00110 if (! required) return bp;
00111 }
00112 }
00113
00114 static int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
00115 {
00116 LIBMTP_album_t *album;
00117 LIBMTP_album_t *found_album = NULL;
00118 int ret;
00119
00120
00121 album = LIBMTP_Get_Album_List(device);
00122 while(album != NULL) {
00123 if ((album->name != NULL &&
00124 album->artist != NULL &&
00125 !strcmp(album->name, albuminfo->name) &&
00126 !strcmp(album->artist, albuminfo->artist)) ||
00127 (album->name != NULL &&
00128 album->composer != NULL &&
00129 !strcmp(album->name, albuminfo->name) &&
00130 !strcmp(album->composer, albuminfo->composer))) {
00131
00132 found_album = album;
00133 album = album->next;
00134 found_album->next = NULL;
00135 } else {
00136 LIBMTP_album_t *tmp;
00137
00138 tmp = album;
00139 album = album->next;
00140 LIBMTP_destroy_album_t(tmp);
00141 }
00142 }
00143
00144 if (found_album != NULL) {
00145 uint32_t *tracks;
00146
00147 tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t));
00148 printf("Album \"%s\" found: updating...\n", found_album->name);
00149 if (!tracks) {
00150 printf("failed malloc in add_track_to_album()\n");
00151 return 1;
00152 }
00153 found_album->no_tracks++;
00154 if (found_album->tracks != NULL) {
00155 memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t));
00156 free(found_album->tracks);
00157 }
00158 tracks[found_album->no_tracks-1] = trackmeta->item_id;
00159 found_album->tracks = tracks;
00160 ret = LIBMTP_Update_Album(device, found_album);
00161 LIBMTP_destroy_album_t(found_album);
00162 } else {
00163 uint32_t *trackid;
00164
00165 trackid = (uint32_t *)malloc(sizeof(uint32_t));
00166 *trackid = trackmeta->item_id;
00167 albuminfo->tracks = trackid;
00168 albuminfo->no_tracks = 1;
00169 albuminfo->storage_id = trackmeta->storage_id;
00170 printf("Album doesn't exist: creating...\n");
00171 ret = LIBMTP_Create_New_Album(device, albuminfo);
00172
00173 }
00174
00175 if (ret != 0) {
00176 printf("Error creating or updating album.\n");
00177 LIBMTP_Dump_Errorstack(device);
00178 LIBMTP_Clear_Errorstack(device);
00179 } else {
00180 printf("success!\n");
00181 }
00182 return ret;
00183 }
00184
00185 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)
00186 {
00187 char *filename, *parent;
00188 char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
00189 char num[80];
00190 uint64_t filesize;
00191 uint32_t parent_id = 0;
00192 #ifdef __USE_LARGEFILE64
00193 struct stat64 sb;
00194 #else
00195 struct stat sb;
00196 #endif
00197 LIBMTP_track_t *trackmeta;
00198 LIBMTP_album_t *albuminfo;
00199 int ret;
00200
00201 printf("Sending track %s to %s\n",from_path,to_path);
00202
00203 trackmeta = LIBMTP_new_track_t();
00204 albuminfo = LIBMTP_new_album_t();
00205
00206 parent = dirname(to_path);
00207 filename = basename(to_path);
00208 parent_id = parse_path (parent,files,folders);
00209 if (parent_id == -1) {
00210 printf("Parent folder could not be found, skipping\n");
00211 return 1;
00212 }
00213
00214 #ifdef __USE_LARGEFILE64
00215 if ( stat64(from_path, &sb) == -1 ) {
00216 #else
00217 if ( stat(from_path, &sb) == -1 ) {
00218 #endif
00219 fprintf(stderr, "%s: ", from_path);
00220 perror("stat");
00221 return 1;
00222 } else if (S_ISREG (sb.st_mode)) {
00223 #ifdef __USE_LARGEFILE64
00224 filesize = sb.st_size;
00225 #else
00226 filesize = (uint64_t) sb.st_size;
00227 #endif
00228 trackmeta->filetype = find_filetype (from_path);
00229 if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) {
00230 printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
00231 return 1;
00232 }
00233
00234 if (ptitle == NULL) {
00235 ptitle = prompt("Title", title, 80, 0);
00236 }
00237 if (!strlen(ptitle))
00238 ptitle = NULL;
00239
00240 if (palbum == NULL) {
00241 palbum = prompt("Album", album, 80, 0);
00242 }
00243 if (!strlen(palbum))
00244 palbum = NULL;
00245
00246 if (palbumartist == NULL) {
00247 palbumartist = prompt("Album artist", albumartist, 80, 0);
00248 }
00249 if (partist == NULL) {
00250 partist = prompt("Artist", artist, 80, 0);
00251 }
00252 if (!strlen(partist))
00253 partist = NULL;
00254
00255 if (pcomposer == NULL) {
00256 pcomposer = prompt("Writer or Composer", composer, 80, 0);
00257 }
00258 if (!strlen(pcomposer))
00259 pcomposer = NULL;
00260
00261 if (pgenre == NULL) {
00262 pgenre = prompt("Genre", genre, 80, 0);
00263 }
00264 if (!strlen(pgenre))
00265 pgenre = NULL;
00266
00267 if (tracknum == 0) {
00268 char *pnum;
00269 if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
00270 tracknum = 0;
00271 if ( strlen(pnum) ) {
00272 tracknum = strtoul(pnum, 0, 10);
00273 } else {
00274 tracknum = 0;
00275 }
00276 }
00277
00278 if (year == 0) {
00279 char *pnum;
00280 if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
00281 year = 0;
00282 if ( strlen(pnum) ) {
00283 year = strtoul(pnum, 0, 10);
00284 } else {
00285 year = 0;
00286 }
00287 }
00288
00289 if (length == 0) {
00290 char *pnum;
00291 if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
00292 length = 0;
00293 if ( strlen(pnum) ) {
00294 length = strtoul(pnum, 0, 10);
00295 } else {
00296 length = 0;
00297 }
00298 }
00299
00300 printf("Sending track:\n");
00301 printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
00302 if (ptitle) {
00303 printf("Title: %s\n", ptitle);
00304 trackmeta->title = strdup(ptitle);
00305 }
00306 if (palbum) {
00307 printf("Album: %s\n", palbum);
00308 trackmeta->album = strdup(palbum);
00309 albuminfo->name = strdup(palbum);
00310 }
00311 if (palbumartist) {
00312 printf("Album artist: %s\n", palbumartist);
00313 albuminfo->artist = strdup(palbumartist);
00314 }
00315 if (partist) {
00316 printf("Artist: %s\n", partist);
00317 trackmeta->artist = strdup(partist);
00318 if (palbumartist == NULL)
00319 albuminfo->artist = strdup(partist);
00320 }
00321
00322 if (pcomposer) {
00323 printf("Writer or Composer: %s\n", pcomposer);
00324 trackmeta->composer = strdup(pcomposer);
00325 albuminfo->composer = strdup(pcomposer);
00326 }
00327 if (pgenre) {
00328 printf("Genre: %s\n", pgenre);
00329 trackmeta->genre = strdup(pgenre);
00330 albuminfo->genre = strdup(pgenre);
00331 }
00332 if (year > 0) {
00333 char tmp[80];
00334 printf("Year: %d\n", year);
00335 snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year);
00336 tmp[sizeof(tmp)-1] = '\0';
00337 trackmeta->date = strdup(tmp);
00338 }
00339 if (tracknum > 0) {
00340 printf("Track no: %d\n", tracknum);
00341 trackmeta->tracknumber = tracknum;
00342 }
00343 if (length > 0) {
00344 printf("Length: %d\n", length);
00345
00346 trackmeta->duration = length * 1000;
00347 }
00348
00349 if (filename != NULL) {
00350 trackmeta->filename = strdup(filename);
00351 }
00352 trackmeta->filesize = filesize;
00353 trackmeta->parent_id = parent_id;
00354
00355 trackmeta->storage_id = 0;
00356
00357 printf("Sending track...\n");
00358 ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL);
00359 printf("\n");
00360 if (ret != 0) {
00361 printf("Error sending track.\n");
00362 LIBMTP_Dump_Errorstack(device);
00363 LIBMTP_Clear_Errorstack(device);
00364 } else {
00365 printf("New track ID: %d\n", trackmeta->item_id);
00366 }
00367
00368
00369 ret = add_track_to_album(albuminfo, trackmeta);
00370
00371 LIBMTP_destroy_album_t(albuminfo);
00372 LIBMTP_destroy_track_t(trackmeta);
00373
00374 return 0;
00375 }
00376 return 0;
00377 }
00378
00379 void sendtrack_command (int argc, char **argv) {
00380 int opt;
00381 extern int optind;
00382 extern char *optarg;
00383 char *partist = NULL;
00384 char *palbumartist = NULL;
00385 char *pcomposer = NULL;
00386 char *ptitle = NULL;
00387 char *pgenre = NULL;
00388 char *pcodec = NULL;
00389 char *palbum = NULL;
00390 uint16_t tracknum = 0;
00391 uint16_t length = 0;
00392 uint16_t year = 0;
00393 uint16_t quiet = 0;
00394 char *lang;
00395 while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:")) != -1 ) {
00396 switch (opt) {
00397 case 't':
00398 ptitle = strdup(optarg);
00399 break;
00400 case 'a':
00401 partist = strdup(optarg);
00402 break;
00403 case 'A':
00404 palbumartist = strdup(optarg);
00405 break;
00406 case 'w':
00407 pcomposer = strdup(optarg);
00408 break;
00409 case 'l':
00410 palbum = strdup(optarg);
00411 break;
00412 case 'c':
00413 pcodec = strdup(optarg);
00414 break;
00415 case 'g':
00416 pgenre = strdup(optarg);
00417 break;
00418 case 'n':
00419 tracknum = atoi(optarg);
00420 break;
00421 case 'd':
00422 length = atoi(optarg);
00423 break;
00424 case 'y':
00425 year = atoi(optarg);
00426 break;
00427 case 'q':
00428 quiet = 1;
00429 break;
00430 default:
00431 sendtrack_usage();
00432 }
00433 }
00434 argc -= optind;
00435 argv += optind;
00436
00437 if ( argc != 2 ) {
00438 printf("You need to pass a filename and destination.\n");
00439 sendtrack_usage();
00440 }
00441
00442 checklang();
00443
00444 printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year);
00445 sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year);
00446 }