// Device.update_storage_info() {{{ static PyObject* Device_update_storage_info(Device *self, PyObject *args) { ENSURE_DEV(NULL); if (LIBMTP_Get_Storage(self->device, LIBMTP_STORAGE_SORTBY_NOTSORTED) < 0) { PyErr_SetString(MTPError, "Failed to get storage info for device."); return NULL; } Py_RETURN_NONE; }
/* this callback runs on the device handling thread, so it can call libmtp directly */ static void mtp_device_open_cb (LIBMTP_mtpdevice_t *device, RBMtpSource *source) { RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source); DeviceOpenedData *data; if (device == NULL) { /* can't delete the source on this thread, so move it to the main thread */ g_idle_add ((GSourceFunc) device_open_failed_idle, g_object_ref (source)); return; } /* set the source name to match the device, ignoring some * particular broken device names. */ data = g_new0 (DeviceOpenedData, 1); data->source = g_object_ref (source); data->name = LIBMTP_Get_Friendlyname (device); if (data->name == NULL || strcmp (data->name, "?????") == 0) { g_free (data->name); data->name = LIBMTP_Get_Modelname (device); } if (data->name == NULL) { data->name = g_strdup (_("Digital Audio Player")); } /* get some other device information that doesn't change */ priv->manufacturer = LIBMTP_Get_Manufacturername (device); priv->device_version = LIBMTP_Get_Deviceversion (device); priv->model_name = LIBMTP_Get_Modelname (device); priv->serial = LIBMTP_Get_Serialnumber (device); /* calculate the device capacity */ priv->capacity = 0; if (LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == 0) { LIBMTP_devicestorage_t *storage; for (storage = device->storage; storage != NULL; storage = storage->next) { priv->capacity += storage->MaxCapacity; } } update_free_space_cb (device, RB_MTP_SOURCE (source)); /* figure out the set of formats supported by the device */ if (LIBMTP_Get_Supported_Filetypes (device, &data->types, &data->num_types) != 0) { rb_mtp_thread_report_errors (priv->device_thread, FALSE); } g_idle_add ((GSourceFunc) device_opened_idle, data); /* now get the track list */ rb_mtp_thread_get_track_list (priv->device_thread, (RBMtpTrackListCallback) mtp_tracklist_cb, g_object_ref (source), g_object_unref); }
bool MTPDevice::enumStorages() { criticalEnter(); LIBMTP_Clear_Errorstack(m_device); if (LIBMTP_Get_Storage(m_device, LIBMTP_STORAGE_SORTBY_NOTSORTED) < 0) { std::cerr << "Could not retrieve device storage.\n"; std::cerr << "For android phones make sure the screen is unlocked.\n"; logerr("Could not retrieve device storage. Exiting.\n"); LIBMTP_Dump_Errorstack(m_device); LIBMTP_Clear_Errorstack(m_device); return false; } criticalLeave(); return true; }
static void update_free_space_cb (LIBMTP_mtpdevice_t *device, RBMtpSource *source) { RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source); LIBMTP_devicestorage_t *storage; int ret; ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { rb_mtp_thread_report_errors (priv->device_thread, FALSE); } /* probably need a lock for this.. */ priv->free_space = 0; for (storage = device->storage; storage != NULL; storage = storage->next) { priv->free_space += storage->FreeSpaceInBytes; } }
int main (int argc, char **argv) { LIBMTP_raw_device_t *rawdevices; int numrawdevices; int i; LIBMTP_Init(); printf("Attempting to connect device(s)\n"); switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: printf("mtp-folders: no devices found\n"); return 0; case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n"); return 1; case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n"); return 1; /* Unknown general errors - This should never execute */ case LIBMTP_ERROR_GENERAL: default: fprintf(stderr, "mtp-folders: Unknown error, please report " "this to the libmtp developers\n"); return 1; /* Successfully connected at least one device, so continue */ case LIBMTP_ERROR_NONE: printf("mtp-folders: Successfully connected\n"); } /* iterate through connected MTP devices */ for (i = 0; i < numrawdevices; i++) { LIBMTP_mtpdevice_t *device; LIBMTP_devicestorage_t *storage; char *friendlyname; int ret; device = LIBMTP_Open_Raw_Device(&rawdevices[i]); if (device == NULL) { fprintf(stderr, "Unable to open raw device %d\n", i); continue; } /* Echo the friendly name so we know which device we are working with */ friendlyname = LIBMTP_Get_Friendlyname(device); if (friendlyname == NULL) { printf("Friendly name: (NULL)\n"); } else { printf("Friendly name: %s\n", friendlyname); free(friendlyname); } LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); /* Get all storages for this device */ ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { perror("LIBMTP_Get_Storage()\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); continue; } /* Loop over storages, dump folder for each one */ for (storage = device->storage; storage != 0; storage = storage->next) { LIBMTP_folder_t *folders; printf("Storage: %s\n", storage->StorageDescription); folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id); if (folders == NULL) { fprintf(stdout, "No folders found\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } else { dump_folder_list(folders,0); } LIBMTP_destroy_folder_t(folders); } LIBMTP_Release_Device(device); } free(rawdevices); printf("OK.\n"); return 0; }
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, uint32_t storageid, uint16_t quiet) { char *filename, *parent; char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80]; char num[80]; uint64_t filesize; uint32_t parent_id = 0; struct stat sb; LIBMTP_track_t *trackmeta; LIBMTP_album_t *albuminfo; int ret; printf("Sending track %s to %s\n", from_path, to_path); trackmeta = LIBMTP_new_track_t(); albuminfo = LIBMTP_new_album_t(); parent = dirname(strdup(to_path)); filename = basename(strdup(to_path)); parent_id = parse_path (parent,files,folders); if (parent_id == -1) { printf("Parent folder could not be found, skipping\n"); return 1; } if (stat(from_path, &sb) == -1) { fprintf(stderr, "%s: ", from_path); perror("stat"); return 1; } if (!S_ISREG(sb.st_mode)) return 0; filesize = sb.st_size; trackmeta->filetype = find_filetype (from_path); if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) { printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); return 1; } if ((ptitle == NULL) && (quiet == 0)) { if ( (ptitle = prompt("Title", title, 80, 0)) != NULL ) if (!strlen(ptitle)) ptitle = NULL; } if ((palbum == NULL) && (quiet == 0)) { if ( (palbum = prompt("Album", album, 80, 0)) != NULL ) if (!strlen(palbum)) palbum = NULL; } if ((palbumartist == NULL) && (quiet == 0)) { if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL ) if (!strlen(palbumartist)) palbumartist = NULL; } if ((partist == NULL) && (quiet == 0)) { if ( (partist = prompt("Artist", artist, 80, 0)) != NULL ) if (!strlen(partist)) partist = NULL; } if ((pcomposer == NULL) && (quiet == 0)) { if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL ) if (!strlen(pcomposer)) pcomposer = NULL; } if ((pgenre == NULL) && (quiet == 0)) { if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL ) if (!strlen(pgenre)) pgenre = NULL; } if ((tracknum == 0) && (quiet == 0)) { char *pnum; if ( (pnum = prompt("Track number", num, 80, 0)) == NULL ) tracknum = 0; else tracknum = strtoul(pnum, 0, 10); } if ((year == 0) && (quiet == 0)) { char *pnum; if ( (pnum = prompt("Year", num, 80, 0)) == NULL ) year = 0; else year = strtoul(pnum, 0, 10); } if ((length == 0) && (quiet == 0)) { char *pnum; if ( (pnum = prompt("Length", num, 80, 0)) == NULL ) length = 0; else length = strtoul(pnum, 0, 10); } printf("Sending track:\n"); printf("Codec: %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype)); if (ptitle) { printf("Title: %s\n", ptitle); trackmeta->title = strdup(ptitle); } if (palbum) { printf("Album: %s\n", palbum); trackmeta->album = strdup(palbum); albuminfo->name = strdup(palbum); } if (palbumartist) { printf("Album artist: %s\n", palbumartist); albuminfo->artist = strdup(palbumartist); } if (partist) { printf("Artist: %s\n", partist); trackmeta->artist = strdup(partist); if (palbumartist == NULL) albuminfo->artist = strdup(partist); } if (pcomposer) { printf("Writer or Composer: %s\n", pcomposer); trackmeta->composer = strdup(pcomposer); albuminfo->composer = strdup(pcomposer); } if (pgenre) { printf("Genre: %s\n", pgenre); trackmeta->genre = strdup(pgenre); albuminfo->genre = strdup(pgenre); } if (year > 0) { char tmp[80]; printf("Year: %d\n", year); snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year); tmp[sizeof(tmp)-1] = '\0'; trackmeta->date = strdup(tmp); } if (tracknum > 0) { printf("Track no: %d\n", tracknum); trackmeta->tracknumber = tracknum; } if (length > 0) { printf("Length: %d\n", length); // Multiply by 1000 since this is in milliseconds trackmeta->duration = length * 1000; } // We should always have this if (filename != NULL) { trackmeta->filename = strdup(filename); } trackmeta->filesize = filesize; trackmeta->parent_id = parent_id; { int rc; char *desc = NULL; LIBMTP_devicestorage_t *pds = NULL; if (0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED))) { perror("LIBMTP_Get_Storage()"); exit(-1); } for (pds = device->storage; pds != NULL; pds = pds->next) { if (pds->id == storageid) { desc = strdup(pds->StorageDescription); break; } } if (NULL != desc) { printf("Storage ID: %s (%u)\n", desc, storageid); free(desc); } else printf("Storage ID: %u\n", storageid); trackmeta->storage_id = storageid; } printf("Sending track...\n"); ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL); printf("\n"); if (ret != 0) { printf("Error sending track.\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); ret = 1; } else { printf("New track ID: %d\n", trackmeta->item_id); } /* Add here add to album call */ if (palbum) ret = add_track_to_album(albuminfo, trackmeta); LIBMTP_destroy_album_t(albuminfo); LIBMTP_destroy_track_t(trackmeta); return ret; }
/* this callback runs on the device handling thread, so it can call libmtp directly */ static void mtp_device_open_cb (LIBMTP_mtpdevice_t *device, RBMtpSource *source) { RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source); gboolean has_audio = FALSE; DeviceOpenedData *data; if (device == NULL) { /* can't delete the source on this thread, so move it to the main thread */ g_idle_add ((GSourceFunc) device_open_failed_idle, g_object_ref (source)); return; } /* set the source name to match the device, ignoring some * particular broken device names. */ data = g_new0 (DeviceOpenedData, 1); data->source = g_object_ref (source); data->name = LIBMTP_Get_Friendlyname (device); if (data->name == NULL || strcmp (data->name, "?????") == 0) { g_free (data->name); data->name = LIBMTP_Get_Modelname (device); } if (data->name == NULL) { data->name = g_strdup (_("Digital Audio Player")); } /* get some other device information that doesn't change */ priv->manufacturer = LIBMTP_Get_Manufacturername (device); priv->device_version = LIBMTP_Get_Deviceversion (device); priv->model_name = LIBMTP_Get_Modelname (device); priv->serial = LIBMTP_Get_Serialnumber (device); /* calculate the device capacity */ priv->capacity = 0; if (LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED) == 0) { LIBMTP_devicestorage_t *storage; for (storage = device->storage; storage != NULL; storage = storage->next) { priv->capacity += storage->MaxCapacity; } } update_free_space_cb (device, RB_MTP_SOURCE (source)); /* figure out the set of formats supported by the device, ensuring there's at least * one audio format aside from WAV. the purpose of this is to exclude cameras and other * MTP devices that aren't interesting to us. */ if (LIBMTP_Get_Supported_Filetypes (device, &data->types, &data->num_types) != 0) { rb_mtp_thread_report_errors (priv->device_thread, FALSE); } else { int i; for (i = 0; i < data->num_types; i++) { if (data->types[i] != LIBMTP_FILETYPE_WAV && LIBMTP_FILETYPE_IS_AUDIO (data->types[i])) { has_audio = TRUE; break; } } } if (has_audio == FALSE) { rb_debug ("device doesn't support any audio formats"); g_idle_add ((GSourceFunc) device_open_ignore_idle, data); return; } g_idle_add ((GSourceFunc) device_opened_idle, data); /* now get the track list */ rb_mtp_thread_get_track_list (priv->device_thread, (RBMtpTrackListCallback) mtp_tracklist_cb, g_object_ref (source), g_object_unref); }
int main (int argc, char **argv) { LIBMTP_raw_device_t * rawdevices; int numrawdevices; LIBMTP_error_number_t err; int i; int opt; extern int optind; extern char *optarg; while ((opt = getopt(argc, argv, "d")) != -1 ) { switch (opt) { case 'd': LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); break; } } argc -= optind; argv += optind; LIBMTP_Init(); err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); switch(err) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: fprintf(stdout, " No raw devices found.\n"); return 0; case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "Detect: There has been an error connecting. Exiting\n"); return 1; case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n"); return 1; case LIBMTP_ERROR_NONE: break; case LIBMTP_ERROR_GENERAL: default: fprintf(stderr, "Unknown connection error.\n"); return 1; } /* Iterate over connected MTP devices */ fprintf(stdout, "Attempting to connect device(s)\n"); for (i = 0; i < numrawdevices; i++) { LIBMTP_mtpdevice_t *device; LIBMTP_devicestorage_t *storage; char *friendlyname; int ret; device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); if (device == NULL) { fprintf(stderr, "Unable to open raw device %d\n", i); continue; } LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); friendlyname = LIBMTP_Get_Friendlyname(device); if (friendlyname == NULL) { printf("Device: (NULL)\n"); } else { printf("Device: %s\n", friendlyname); free(friendlyname); } /* Get all storages for this device */ ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { perror("LIBMTP_Get_Storage()"); goto bailout; } /* Loop over storages */ for (storage = device->storage; storage != 0; storage = storage->next) { fprintf(stdout, "Storage: %s\n", storage->StorageDescription); recursive_file_tree(device, storage, 0, 0); } bailout: LIBMTP_Release_Device(device); } /* End For Loop */ free(rawdevices); printf("OK.\n"); return 0; }
int main(int argc, char **argv) { int mainret = 0; if (argc < 2) { fprintf(stderr, "usage: %s [-d] mountpoint\n", argv[0]); exit(1); } char *mount_point = argv[1]; argc -= 1; argv += 1; // LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); LIBMTP_Init(); LIBMTP_error_number_t mtperr; LIBMTP_raw_device_t *rawdevs; int nrawdev; mtperr = LIBMTP_Detect_Raw_Devices(&rawdevs, &nrawdev); switch (mtperr) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: fprintf(stderr, "no raw devices\n"); exit(0); case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "ERROR connecting\n"); exit(1); case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "ERROR memory allocation\n"); exit(1); case LIBMTP_ERROR_NONE: break; default: fprintf(stderr, "ERROR unknown\n"); exit(1); } printf("%d raw devices:\n", nrawdev); int i; for (i = 0; i < nrawdev; ++i) { printf(" device #%d: %s: %s (%04X:%04X) @ bus %d, dev %d\n", i, rawdevs[i].device_entry.vendor, rawdevs[i].device_entry.product, rawdevs[i].device_entry.vendor_id, rawdevs[i].device_entry.product_id, rawdevs[i].bus_location, rawdevs[i].devnum); } mtpfuse_t ctx; memset(&ctx, 0, sizeof(ctx)); printf("opening raw device #0...\n"); ctx.dev = LIBMTP_Open_Raw_Device_Uncached(&rawdevs[0]); if (!ctx.dev) { fprintf(stderr, "ERROR opening raw device #0\n"); exit(1); } LIBMTP_Dump_Errorstack(ctx.dev); LIBMTP_Clear_Errorstack(ctx.dev); char *name = LIBMTP_Get_Friendlyname(ctx.dev); printf("opened raw device %s.\n", name); int ret = LIBMTP_Get_Storage(ctx.dev, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { fprintf(stderr, "ERROR get %s's storage:\n", name); LIBMTP_Dump_Errorstack(ctx.dev); mainret = 1; goto done; } printf("device's storage:\n"); LIBMTP_devicestorage_t *storage; for (storage = ctx.dev->storage, i = 0; storage; storage = storage->next, ++i) { printf(" storage #%d: %d - %s\n", i, storage->id, storage->StorageDescription); } if (i <= 0) { fprintf(stderr, "there is no storage that is usable.\n"); mainret = 1; goto done; } mainret = fuse_main(argc, argv, &mtpfuse_ops, &ctx); done: LIBMTP_Release_Device(ctx.dev); return mainret; }
void Plugin::UploadTo (LIBMTP_mtpdevice_t *device, const QByteArray& storageId, const QString& localPath, const QString& origPath) { if (!device->storage) LIBMTP_Get_Storage (device, 0); auto storage = device->storage; while (storage) { qDebug () << "st" << storage->id; if (QByteArray::number (storage->id) == storageId) break; storage = storage->next; } if (!storage) { qWarning () << Q_FUNC_INFO << "could not find storage" << storageId; emit uploadFinished (localPath, QFile::ResourceError, tr ("Unable to find the requested storage.")); return; } const auto id = storage->id; const auto& info = OrigInfos_.take (origPath); qDebug () << "uploading" << localPath << "of type" << GetFileType (info.FileFormat_) << "to" << storage->id; auto track = LIBMTP_new_track_t (); track->storage_id = id; auto getStr = [] (const QString& str) { return strdup (str.toUtf8 ().constData ()); }; track->filename = getStr (QFileInfo (localPath).fileName ()); track->album = getStr (info.Album_); track->title = getStr (info.TrackTitle_); track->genre = getStr (info.Genres_.join ("; ")); track->artist = getStr (info.Artist_); track->tracknumber = info.TrackNumber_; track->filetype = GetFileType (info.FileFormat_); track->filesize = QFileInfo (localPath).size (); track->date = getStr (QString::number (info.AlbumYear_) + "0101T0000.0"); auto watcher = new QFutureWatcher<UploadInfo> (); connect (watcher, SIGNAL (finished ()), this, SLOT (handleUploadFinished ())); const auto future = QtConcurrent::run ([=] () -> UploadInfo { const auto cbData = new CallbackData { this, 0 }; const auto res = LIBMTP_Send_Track_From_File (device, localPath.toUtf8 ().constData (), track, TransferCallback, cbData); delete cbData; return { res, device, localPath, track, info }; }); watcher->setFuture (future); }