00001 #include "common.h"
00002 #include "libmtp.h"
00003 #include <string.h>
00004 #include <sys/stat.h>
00005 #include <sys/types.h>
00006 #include <fcntl.h>
00007
00008 #ifdef HAVE_LIBGEN_H
00009 #include <libgen.h>
00010 #endif
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef HAVE_LIBGEN_H
00022 static char *basename(char *in) {
00023 char *p;
00024 if (in == NULL)
00025 return NULL;
00026 p = in + strlen(in) - 1;
00027 while (*p != '\\' && *p != '/' && *p != ':')
00028 { p--; }
00029 return ++p;
00030 }
00031 #endif
00032
00033 static int progress (uint64_t const sent, uint64_t const total, void const * const data)
00034 {
00035 int percent = (sent*100)/total;
00036 #ifdef __WIN32__
00037 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
00038 #else
00039 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
00040 #endif
00041 fflush(stdout);
00042 return 0;
00043 }
00044
00045 static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
00046 {
00047 char *cp, *bp;
00048
00049 while (1) {
00050 fprintf(stdout, "%s> ", prompt);
00051 if ( fgets(buffer, bufsz, stdin) == NULL ) {
00052 if (ferror(stdin)) {
00053 perror("fgets");
00054 } else {
00055 fprintf(stderr, "EOF on stdin\n");
00056 }
00057 return NULL;
00058 }
00059
00060 cp = strrchr(buffer, '\n');
00061 if ( cp != NULL ) *cp = '\0';
00062
00063 bp = buffer;
00064 while ( bp != cp ) {
00065 if ( *bp != ' ' && *bp != '\t' ) return bp;
00066 bp++;
00067 }
00068
00069 if (! required) return bp;
00070 }
00071 }
00072
00073 static void usage(void)
00074 {
00075 fprintf(stderr, "usage: sendfile [ -D debuglvl ] [ -q ] -t type <path>\n");
00076 fprintf(stderr, " -f \"Folder Name\"\n");
00077
00078 exit(1);
00079 }
00080
00081 static uint32_t find_folder_list(char *name, LIBMTP_folder_t *folderlist, int level)
00082 {
00083 uint32_t i;
00084
00085 if(folderlist==NULL) {
00086 return 0;
00087 }
00088
00089 if(!strcasecmp(name, folderlist->name))
00090 return folderlist->folder_id;
00091
00092 if ((i = (find_folder_list(name, folderlist->child, level+1))))
00093 return i;
00094 if ((i = (find_folder_list(name, folderlist->sibling, level))))
00095 return i;
00096
00097 return 0;
00098 }
00099
00100
00101 int main(int argc, char **argv)
00102 {
00103 int opt;
00104 extern int optind;
00105 extern char *optarg;
00106 char *path, *filename;
00107 char *ptype = NULL, type[80];
00108 char *pfolder = NULL;
00109 uint64_t filesize;
00110 uint16_t quiet = 0;
00111 struct stat sb;
00112 char *lang;
00113 LIBMTP_mtpdevice_t *device;
00114 LIBMTP_folder_t *folders = NULL;
00115 LIBMTP_file_t *genfile;
00116 int ret;
00117 uint32_t parent_id = 0;
00118
00119 LIBMTP_Init();
00120
00121 while ( (opt = getopt(argc, argv, "qht:f:")) != -1 ) {
00122 switch (opt) {
00123 case 'h':
00124 usage();
00125 exit(0);
00126 case 'f':
00127 pfolder = strdup(optarg);
00128 break;
00129 case 't':
00130 ptype = strdup(optarg);
00131 break;
00132 case 'q':
00133 quiet = 1;
00134 break;
00135 default:
00136 usage();
00137 }
00138 }
00139 argc -= optind;
00140 argv += optind;
00141
00142 if ( argc != 1 ) {
00143 printf("You need to pass a filename.\n");
00144 usage();
00145 }
00146
00147
00148 if (!quiet) {
00149 if (ptype == NULL) {
00150 if ( (ptype = prompt("Type", type, 80, 1)) == NULL ) {
00151 printf("A file type.\n");
00152 usage();
00153 }
00154 }
00155 }
00156
00157
00158
00159
00160
00161 lang = getenv("LANG");
00162 if (lang != NULL) {
00163 if (strlen(lang) > 5) {
00164 char *langsuff = &lang[strlen(lang)-5];
00165 if (strcmp(langsuff, "UTF-8")) {
00166 printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang);
00167 printf("If you want to have support for diacritics and Unicode characters,\n");
00168 printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n");
00169 }
00170 }
00171 }
00172
00173 path = argv[0];
00174
00175 filename = basename(path);
00176 if (filename == NULL) {
00177 printf("Error: filename could not be based.\n");
00178 exit(1);
00179 }
00180 if ( stat(path, &sb) == -1 ) {
00181 fprintf(stderr, "%s: ", path);
00182 perror("stat");
00183 exit(1);
00184 }
00185 filesize = (uint64_t) sb.st_size;
00186
00187 genfile = LIBMTP_new_file_t();
00188 genfile->filesize = filesize;
00189 genfile->filename = strdup(filename);
00190
00191
00192 if (!strcasecmp(ptype,"wav")) {
00193 genfile->filetype = LIBMTP_FILETYPE_WAV;
00194 } else if (!strcasecmp(ptype,"mp3")) {
00195 genfile->filetype = LIBMTP_FILETYPE_MP3;
00196 } else if (!strcasecmp(ptype,"wma")) {
00197 genfile->filetype = LIBMTP_FILETYPE_WMA;
00198 } else if (!strcasecmp(ptype,"ogg")) {
00199 genfile->filetype = LIBMTP_FILETYPE_OGG;
00200 } else if (!strcasecmp(ptype,"mp4")) {
00201 genfile->filetype = LIBMTP_FILETYPE_MP4;
00202 } else if (!strcasecmp(ptype,"wmv")) {
00203 genfile->filetype = LIBMTP_FILETYPE_WMV;
00204 } else if (!strcasecmp(ptype,"avi")) {
00205 genfile->filetype = LIBMTP_FILETYPE_AVI;
00206 } else if (!strcasecmp(ptype,"mpeg") || !strcasecmp(ptype,"mpg")) {
00207 genfile->filetype = LIBMTP_FILETYPE_MPEG;
00208 } else if (!strcasecmp(ptype,"asf")) {
00209 genfile->filetype = LIBMTP_FILETYPE_ASF;
00210 } else if (!strcasecmp(ptype,"qt") || !strcasecmp(ptype,"mov")) {
00211 genfile->filetype = LIBMTP_FILETYPE_QT;
00212 } else if (!strcasecmp(ptype,"wma")) {
00213 genfile->filetype = LIBMTP_FILETYPE_WMA;
00214 } else if (!strcasecmp(ptype,"jpg") || !strcasecmp(ptype,"jpeg")) {
00215 genfile->filetype = LIBMTP_FILETYPE_JPEG;
00216 } else if (!strcasecmp(ptype,"jfif")) {
00217 genfile->filetype = LIBMTP_FILETYPE_JFIF;
00218 } else if (!strcasecmp(ptype,"tif") || !strcasecmp(ptype,"tiff")) {
00219 genfile->filetype = LIBMTP_FILETYPE_TIFF;
00220 } else if (!strcasecmp(ptype,"bmp")) {
00221 genfile->filetype = LIBMTP_FILETYPE_BMP;
00222 } else if (!strcasecmp(ptype,"gif")) {
00223 genfile->filetype = LIBMTP_FILETYPE_GIF;
00224 } else if (!strcasecmp(ptype,"pic") || !strcasecmp(ptype,"pict")) {
00225 genfile->filetype = LIBMTP_FILETYPE_PICT;
00226 } else if (!strcasecmp(ptype,"png")) {
00227 genfile->filetype = LIBMTP_FILETYPE_PNG;
00228 } else if (!strcasecmp(ptype,"wmf")) {
00229 genfile->filetype = LIBMTP_FILETYPE_WINDOWSIMAGEFORMAT;
00230 } else if (!strcasecmp(ptype,"ics")) {
00231 genfile->filetype = LIBMTP_FILETYPE_VCALENDAR2;
00232 } else if (!strcasecmp(ptype,"exe") || !strcasecmp(ptype,"com") ||
00233 !strcasecmp(ptype,"bat") || !strcasecmp(ptype,"dll") ||
00234 !strcasecmp(ptype,"sys")) {
00235 genfile->filetype = LIBMTP_FILETYPE_WINEXEC;
00236 } else {
00237 printf("Sorry, file type \"%s\" is not yet supported\n", ptype);
00238 printf("Tagging as unknown file type.\n");
00239 genfile->filetype = LIBMTP_FILETYPE_UNKNOWN;
00240 }
00241
00242 printf("Sending file:\n");
00243
00244 device = LIBMTP_Get_First_Device();
00245 if (device == NULL) {
00246 printf("No MTP device found.\n");
00247 LIBMTP_destroy_file_t(genfile);
00248 exit(1);
00249 }
00250
00251 if (path == NULL) {
00252 printf("LIBMTP_Send_File_From_File(): Bad arguments, path was NULL\n");
00253 return -1;
00254 }
00255
00256 if (pfolder) {
00257 folders = LIBMTP_Get_Folder_List(device);
00258 if(folders == NULL) {
00259 printf("No folders found\n");
00260 exit(1);
00261 } else {
00262 parent_id = find_folder_list(pfolder, folders, 0);
00263 if (!parent_id) {
00264 printf("Parent folder could not be found, exiting\n");
00265 exit(1);
00266 } else
00267 printf("parent_id = %d\n", parent_id);
00268 }
00269 }
00270
00271 LIBMTP_destroy_folder_t(folders);
00272
00273 printf("Sending file...\n");
00274 ret = LIBMTP_Send_File_From_File(device, path, genfile, progress, NULL, parent_id);
00275
00276 printf("\n");
00277
00278 LIBMTP_Release_Device(device);
00279
00280 LIBMTP_destroy_file_t(genfile);
00281
00282 printf("OK.\n");
00283
00284 return 0;
00285 }
00286