1 /*
2  * Copyright (c) 2010 Nathaniel McCallum <nathaniel@natemccallum.com>
3  *
4  * The code contained in this file is free software; you can redistribute
5  * it and/or modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either version
7  * 2.1 of the License, or (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this code; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 namespace GPod {
20 	using System;
21 	using System.Collections.Generic;
22 	using System.Runtime.InteropServices;
23 	using Gdk;
24 	using native;
25 
26 	namespace native {
27 #if ALIGNMENT_X86_LINUX
28 		[StructLayout (LayoutKind.Sequential, Pack=4)]
29 #else
30 		[StructLayout (LayoutKind.Sequential)]
31 #endif
32 		internal struct Itdb_Playlist {
33 			public IntPtr itdb;
34 			public IntPtr name;
35 			public byte   type;
36 			public byte   flag1;
37 			public byte   flag2;
38 			public byte   flag3;
39 			public int    num;
40 			public IntPtr members;
41 			public int   is_spl;
42 			public IntPtr timestamp;
43 			public ulong  id;
44 			public PlaylistSortOrder sortorder;
45 			public uint   podcastflag;
46 			//Itdb_SPLPref splpref;
47 			//Itdb_SPLRules splrules;
48 			// Ignore the rest
49 
50 			[DllImport ("gpod")]
itdb_playlist_newGPod.native.Itdb_Playlist51 			internal static extern IntPtr itdb_playlist_new(string title, bool spl);
52 
53 			[DllImport ("gpod")]
itdb_playlist_freeGPod.native.Itdb_Playlist54 			internal static extern void   itdb_playlist_free(HandleRef pl);
55 
56 			[DllImport ("gpod")]
itdb_playlist_duplicateGPod.native.Itdb_Playlist57 			internal static extern IntPtr itdb_playlist_duplicate(HandleRef pl);
58 
59 			[DllImport ("gpod")]
itdb_playlist_add_trackGPod.native.Itdb_Playlist60 			internal static extern void   itdb_playlist_add_track(HandleRef pl, HandleRef track, int pos);
61 
62 			[DllImport ("gpod")]
itdb_playlist_remove_trackGPod.native.Itdb_Playlist63 			internal static extern void   itdb_playlist_remove_track(HandleRef pl, HandleRef track);
64 		}
65 	}
66 
67 	public enum PlaylistSortOrder {
68 		Manual			= 1,
69 		//Unknown		= 2,
70 		Title			= 3,
71 		Album			= 4,
72 		Artist			= 5,
73 		Bitrate			= 6,
74 		Genre			= 7,
75 		Filetype		= 8,
76 		TimeModified 		= 9,
77 		TrackNumber		= 10,
78 		Size			= 11,
79 		Time			= 12,
80 		Year			= 13,
81 		Samplerate		= 14,
82 		Comment			= 15,
83 		TimeAdded		= 16,
84 		Equalizer		= 17,
85 		Composer		= 18,
86 		//Unknown		= 19,
87 		PlayCount		= 20,
88 		TimePlayed		= 21,
89 		CDNumber		= 22,
90 		Rating			= 23,
91 		ReleaseDate		= 24,
92 		BPM			= 25,
93 		Grouping		= 26,
94 		Category		= 27,
95 		Description		= 28
96 	}
97 
98 	internal class PlaylistTrackList : GPodList<Track> {
PlaylistTrackList(HandleRef handle, IntPtr list)99 		public PlaylistTrackList(HandleRef handle, IntPtr list)	: base(handle, list) {}
DoAdd(int index, Track item)100 		protected override void DoAdd(int index, Track item)	{ Itdb_Playlist.itdb_playlist_add_track(this.handle, item.Handle, index); }
DoUnlink(int index)101 		protected override void DoUnlink(int index)		{ Itdb_Playlist.itdb_playlist_remove_track(this.handle, this[index].Handle); }
102 
103 		protected unsafe override GLib.List List {
104 			get {
105 				return new GLib.List(((Itdb_Playlist *) handle.Handle)->members, typeof(Track));
106 			}
107 		}
108 	}
109 
110 	public unsafe class Playlist : GPodBase {
111 		public ITDB ITDB {
112 			get { return new ITDB(((Itdb_Playlist *) Native)->itdb, true); }
113 		}
114 
115 		public IList<Track> Tracks {
116 			get { return new PlaylistTrackList(Handle, ((Itdb_Playlist *) Native)->members); }
117 		}
118 
119 		public string Name {
120 			get { return PtrToStringUTF8(((Itdb_Playlist *) Native)->name); }
121 			set { var x = (Itdb_Playlist *) Native; ReplaceStringUTF8(ref x->name, value); }
122 		}
123 
124 		public bool 			IsSmartPlaylist	{ get { return ((Itdb_Playlist *) Native)->is_spl != 0; }
125 								  set { ((Itdb_Playlist *) Native)->is_spl = value ? 1 : 0; } }
126 		public DateTime			TimeCreated	{ get { return Artwork.time_tToDateTime(((Itdb_Playlist *) Native)->timestamp); }
127 								  set { ((Itdb_Playlist *) Native)->timestamp = Artwork.DateTimeTotime_t(value); } }
128 		public ulong 			ID		{ get { return ((Itdb_Playlist *) Native)->id; }
129 								  set { ((Itdb_Playlist *) Native)->id = value; } }
130 		public PlaylistSortOrder	SortOrder	{ get { return ((Itdb_Playlist *) Native)->sortorder; }
131 								  set { ((Itdb_Playlist *) Native)->sortorder = value; } }
132 		public bool			IsPodcast	{ get { return ((Itdb_Playlist *) Native)->podcastflag == 1; }
133 								  set { ((Itdb_Playlist *) Native)->podcastflag = (uint) (value ? 1 : 0); } }
134 		public bool			IsMaster	{ get { return (((Itdb_Playlist *) Native)->type & 0xff) == 1; }
135 								  set { ((Itdb_Playlist *) Native)->type = (byte) (value ? 1 : 0); } }
136 
Playlist(string name)137 		public Playlist(string name) : base (Itdb_Playlist.itdb_playlist_new (name, false)) {}
Playlist(IntPtr handle, bool borrowed)138 		public Playlist(IntPtr handle, bool borrowed)	: base(handle, borrowed) {}
Playlist(IntPtr handle)139 		public Playlist(IntPtr handle)			: base(handle) {}
Playlist(Playlist other)140 		public Playlist(Playlist other)			: base(Itdb_Playlist.itdb_playlist_duplicate(other.Handle), false) {}
Destroy()141 		protected override void Destroy()		{ Itdb_Playlist.itdb_playlist_free(Handle); }
142 	}
143 }
144 
145 
146