Commit 81398398 authored by Nicholas Little's avatar Nicholas Little Committed by Andrés G. Aragoneses
Browse files

Mtp: show tracks which are in subfolders from Music/ (bgo#724678)

Files can happen to be in a hierarchy of folders inside the
Music/ folder in the device. To be able to see them we need
to do add them the proper MtpTrackInfo.MediaAttriubutes by
looking for them recursively.

Based on original patch by IBBoard <dev@ibboard.co.uk> (his
patch included read-and-write support, this patch only
implements read support for now. Write support will need
more discussion. See BGO#539804 for the latter.)
parent e5f44acb
......@@ -273,7 +273,7 @@ namespace Banshee.Dap.Mtp
using (System.IO.TextWriter writer = new System.IO.StreamWriter (Banshee.IO.File.OpenWrite (empty_file, true))) {
writer.Write ("foo");
}
Track mtp_track = new Track (System.IO.Path.GetFileName (empty_file.LocalPath), 3);
Track mtp_track = new Track (System.IO.Path.GetFileName (empty_file.LocalPath), 3, mtp_device);
mtp_device.UploadTrack (empty_file.AbsolutePath, mtp_track, mtp_device.MusicFolder);
mtp_device.Remove (mtp_track);
......@@ -425,7 +425,7 @@ namespace Banshee.Dap.Mtp
public Track TrackInfoToMtpTrack (TrackInfo track, SafeUri fromUri)
{
Track f = new Track (System.IO.Path.GetFileName (fromUri.LocalPath), (ulong) Banshee.IO.File.GetSize (fromUri));
Track f = new Track (System.IO.Path.GetFileName (fromUri.LocalPath), (ulong) Banshee.IO.File.GetSize (fromUri), mtp_device);
MtpTrackInfo.ToMtpTrack (track, f);
return f;
}
......
......@@ -73,9 +73,9 @@ namespace Banshee.Dap.Mtp
MediaAttributes = TrackMediaAttributes.AudioStream;
if (device != null) {
SetAttributeIf (file.InFolder (device.PodcastFolder) || Genre == "Podcast", TrackMediaAttributes.Podcast);
SetAttributeIf (file.InFolder (device.MusicFolder), TrackMediaAttributes.Music);
SetAttributeIf (file.InFolder (device.VideoFolder), TrackMediaAttributes.VideoStream);
SetAttributeIf (file.InFolder (device.PodcastFolder, true) || Genre == "Podcast", TrackMediaAttributes.Podcast);
SetAttributeIf (file.InFolder (device.MusicFolder, true), TrackMediaAttributes.Music);
SetAttributeIf (file.InFolder (device.VideoFolder, true), TrackMediaAttributes.VideoStream);
}
// This can be implemented if there's enough people requesting it
......
......@@ -104,7 +104,35 @@ namespace Mtp
LIBMTP_destroy_folder_t (root);
return folders;
}
private Folder CreateParentFolder ()
{
return Find (device, parentId);
}
public bool HasAncestor (Folder ancestor)
{
if (ancestor == null) {
throw new ArgumentNullException ("ancestor");
}
if (device != ancestor.device) {
throw new ArgumentException ("Folders are on different devices");
}
bool hasAncestor = false;
if (parentId != 0) {
if (parentId == ancestor.FolderId) {
hasAncestor = true;
} else {
Folder parent = CreateParentFolder ();
hasAncestor = parent.HasAncestor (ancestor);
}
}
return hasAncestor;
}
public void Remove()
{
MtpDevice.DeleteObject(device.Handle, FolderId);
......@@ -154,6 +182,26 @@ namespace Mtp
return LIBMTP_Find_Folder (folderList, folderId);
}
internal static Folder Find (MtpDevice device, uint folderId)
{
if (device == null) {
throw new ArgumentNullException ("device");
}
Folder folder = null;
IntPtr root = GetFolderList (device.Handle);
try {
IntPtr ptr = Find (root, folderId);
if (ptr != IntPtr.Zero) {
FolderStruct folderStruct = (FolderStruct)Marshal.PtrToStructure (ptr, typeof (FolderStruct));
folder = new Folder (folderStruct, device);
}
} finally {
DestroyFolder (root);
}
return folder;
}
internal static IntPtr GetFolderList (MtpDeviceHandle handle)
{
return LIBMTP_Get_Folder_List (handle);
......
......@@ -139,7 +139,11 @@ namespace Mtp
set { trackStruct.composer = value; }
}
public Track (string filename, ulong filesize) : this (new TrackStruct (), null)
public Track (string filename, ulong filesize) : this (filename, filesize, null)
{
}
public Track (string filename, ulong filesize, MtpDevice device) : this (new TrackStruct (), device)
{
this.trackStruct.filename = filename;
this.trackStruct.filesize = filesize;
......@@ -154,9 +158,23 @@ namespace Mtp
public bool InFolder (Folder folder)
{
return folder != null && trackStruct.parent_id == folder.FolderId;
return InFolder (folder, false);
}
public bool InFolder (Folder folder, bool recursive)
{
if (folder == null) {
return false;
}
bool is_parent = trackStruct.parent_id == folder.FolderId;
if (is_parent || !recursive) {
return is_parent;
}
return Folder.Find (device, trackStruct.parent_id).HasAncestor (folder);
}
public void Download (string path)
{
Download (path, null);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment