Ejemplo n.º 1
0
static void
add_track_to_album_and_update (RBMtpThread *thread, RBMtpThreadTask *task)
{
	LIBMTP_album_t *album;
	gboolean new_album = FALSE;

	album = add_track_to_album (thread, task->album, task->track_id, task->storage_id, &new_album);
	write_album_to_device (thread, album, new_album);
}
static LIBMTP_track_t *
transfer_track (RBMtpSource *source,
		LIBMTP_mtpdevice_t *device,
		RhythmDBEntry *entry,
		const char *filename,
		guint64 filesize,
		const char *mimetype)
{
	LIBMTP_track_t *trackmeta = LIBMTP_new_track_t ();
	GDate d;
	int ret;

	trackmeta->title = rhythmdb_entry_dup_string (entry, RHYTHMDB_PROP_TITLE);
	trackmeta->album = rhythmdb_entry_dup_string (entry, RHYTHMDB_PROP_ALBUM);
	trackmeta->artist = rhythmdb_entry_dup_string (entry, RHYTHMDB_PROP_ARTIST);
	trackmeta->genre = rhythmdb_entry_dup_string (entry, RHYTHMDB_PROP_GENRE);
	trackmeta->filename = g_path_get_basename (filename);

	if (rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_DATE) > 0) { /* Entries without a date returns 0, g_date_set_julian don't accept that */
		g_date_set_julian (&d, rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_DATE));
		trackmeta->date	= gdate_to_char (&d);
	}
	trackmeta->tracknumber = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_TRACK_NUMBER);
	trackmeta->duration = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_DURATION) * 1000;
	trackmeta->rating = rhythmdb_entry_get_double (entry, RHYTHMDB_PROP_RATING) * 20;
	trackmeta->usecount = rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_PLAY_COUNT);
	trackmeta->filesize = filesize;
	if (mimetype == NULL) {
		mimetype = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_MIMETYPE);
	}
	trackmeta->filetype = mimetype_to_filetype (source, mimetype);
	rb_debug ("using libmtp filetype %d (%s) for source media type %s",
		  trackmeta->filetype,
		  LIBMTP_Get_Filetype_Description (trackmeta->filetype),
		  mimetype);

#ifdef HAVE_LIBMTP_030
	ret = LIBMTP_Send_Track_From_File (device, filename, trackmeta, NULL, NULL);
#else
	ret = LIBMTP_Send_Track_From_File (device, filename, trackmeta, NULL, NULL, 0);
#endif
	rb_debug ("LIBMTP_Send_Track_From_File (%s) returned %d", filename, ret);
	if (ret != 0) {
		report_libmtp_errors (device, TRUE);
		LIBMTP_destroy_track_t (trackmeta);
		return NULL;
	}

	if (strcmp (trackmeta->album, _("Unknown")) != 0) {
		add_track_to_album (source, trackmeta->album, trackmeta);
	}

	return trackmeta;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
static void
get_track_list (RBMtpThread *thread, RBMtpThreadTask *task)
{
	RBMtpTrackListCallback cb = task->callback;
	gboolean device_forgets_albums = TRUE;
	GHashTable *update_albums = NULL;
	LIBMTP_track_t *tracks = NULL;
	LIBMTP_album_t *albums;
	LIBMTP_album_t *album;

	/* get all the albums */
	albums = LIBMTP_Get_Album_List (thread->device);
	rb_mtp_thread_report_errors (thread, FALSE);
	if (albums != NULL) {
		LIBMTP_album_t *album;

		for (album = albums; album != NULL; album = album->next) {
			if (album->name == NULL)
				continue;

			rb_debug ("album: %s, %d tracks", album->name, album->no_tracks);
			g_hash_table_insert (thread->albums, album->name, album);
			if (album->no_tracks != 0) {
				device_forgets_albums = FALSE;
			}
		}

		if (device_forgets_albums) {
			rb_debug ("stupid mtp device detected.  will rebuild all albums.");
		}
	} else {
		rb_debug ("No albums");
		device_forgets_albums = FALSE;
	}

	tracks = LIBMTP_Get_Tracklisting_With_Callback (thread->device, NULL, NULL);
	rb_mtp_thread_report_errors (thread, FALSE);
	if (tracks == NULL) {
		rb_debug ("no tracks on the device");
	} else if (device_forgets_albums) {
		LIBMTP_track_t *track;
	       
		rb_debug ("rebuilding albums");
		update_albums = g_hash_table_new (g_direct_hash, g_direct_equal);
		for (track = tracks; track != NULL; track = track->next) {
			if (track->album != NULL) {
				gboolean new_album = FALSE;
				album = add_track_to_album (thread, track->album, track->item_id, track->storage_id, &new_album);
				g_hash_table_insert (update_albums, album, GINT_TO_POINTER (new_album));
			}
		}
		rb_debug ("finished rebuilding albums");
	}

	cb (tracks, task->user_data);
	/* the callback owns the tracklist */

	if (device_forgets_albums) {
		GHashTableIter iter;
		gpointer album_ptr;
		gpointer new_album_ptr;

		rb_debug ("writing rebuilt albums back to the device");
		g_hash_table_iter_init (&iter, update_albums);
		while (g_hash_table_iter_next (&iter, &album_ptr, &new_album_ptr)) {
			album = album_ptr;
			rb_debug ("writing album \"%s\"", album->name);
			write_album_to_device (thread, album, GPOINTER_TO_INT (new_album_ptr));
		}
		g_hash_table_destroy (update_albums);

		rb_debug ("removing remaining empty albums");
		g_hash_table_iter_init (&iter, thread->albums);
		while (g_hash_table_iter_next (&iter, NULL, &album_ptr)) {
			int ret;

			album = album_ptr;
			if (album->no_tracks == 0) {
				rb_debug ("pruning empty album \"%s\"", album->name); 
				ret = LIBMTP_Delete_Object (thread->device, album->album_id);
				if (ret != 0) {
					rb_mtp_thread_report_errors (thread, FALSE);
				}
				g_hash_table_iter_remove (&iter);
			}
		}

		rb_debug ("finished updating albums on the device");
	}
}
static gboolean
load_mtp_db_idle_cb (RBMtpSource* source)
{
	RhythmDB *db = NULL;
	RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source);
	LIBMTP_track_t *tracks = NULL;
	LIBMTP_album_t *albums;
	gboolean device_forgets_albums = TRUE;

	db = get_db_for_source (source);

	g_assert (db != NULL);

	albums = LIBMTP_Get_Album_List (priv->device);
	report_libmtp_errors (priv->device, FALSE);
	if (albums != NULL) {
		LIBMTP_album_t *album;

		for (album = albums; album != NULL; album = album->next) {
			rb_debug ("album: %s, %d tracks", album->name, album->no_tracks);
			g_hash_table_insert (priv->album_map, album->name, album);
			if (album->no_tracks != 0) {
				device_forgets_albums = FALSE;
			}
		}

		if (device_forgets_albums) {
			rb_debug ("stupid mtp device detected.  will rebuild all albums.");
		}
	} else {
		rb_debug ("No albums");
		device_forgets_albums = FALSE;
	}

#ifdef HAVE_LIBMTP_030
	tracks = LIBMTP_Get_Tracklisting_With_Callback (priv->device, NULL, NULL);
#else
	tracks = LIBMTP_Get_Tracklisting (priv->device);
#endif
	report_libmtp_errors (priv->device, FALSE);
	if (tracks != NULL) {
		LIBMTP_track_t *track;
		for (track = tracks; track != NULL; track = track->next) {
			add_mtp_track_to_db (source, db, track);

			if (device_forgets_albums && track->album != NULL) {
				add_track_to_album (source, track->album, track);
			}
		}
	} else {
		rb_debug ("No tracks");
	}

	/* for stupid devices, remove any albums left with no tracks */
	if (device_forgets_albums) {
		GHashTableIter iter;
		gpointer value;
		LIBMTP_album_t *album;

		g_hash_table_iter_init (&iter, priv->album_map);
		while (g_hash_table_iter_next (&iter, NULL, &value)) {
			int ret;

			album = value;
			if (album->no_tracks == 0) {
				rb_debug ("pruning empty album \"%s\"", album->name); 
				ret = LIBMTP_Delete_Object (priv->device, album->album_id);
				if (ret != 0) {
					report_libmtp_errors (priv->device, FALSE);
				}
				g_hash_table_iter_remove (&iter);
			}
		}
	}


	g_object_unref (G_OBJECT (db));
	return FALSE;
}