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 24 interface IGPodBase { 25 IntPtr Native { get; } SetBorrowed(bool borrowed)26 void SetBorrowed(bool borrowed); 27 } 28 29 public abstract class GPodBase : IGPodBase, IDisposable { 30 PtrToStringUTF8(IntPtr ptr)31 protected static string PtrToStringUTF8(IntPtr ptr) 32 { 33 return GLib.Marshaller.Utf8PtrToString(ptr); 34 } 35 ReplaceStringUTF8(ref IntPtr ptr, string str)36 protected static void ReplaceStringUTF8(ref IntPtr ptr, string str) 37 { 38 GLib.Marshaller.Free(ptr); 39 ptr = GLib.Marshaller.StringToPtrGStrdup(str); 40 } 41 42 static DateTime local_epoch = new DateTime(1970, 1, 1, 0, 0, 0); 43 static int utc_offset = (int) (TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)).TotalSeconds; 44 DateTimeTotime_t(DateTime time)45 public static IntPtr DateTimeTotime_t(DateTime time) 46 { 47 // The itunes database uses a 32bit signed value, so enforce that here to avoid 48 // overflow issues. We still need to represent time with an IntPtr though as 49 // that's what libgpod publicly exposes 50 return new IntPtr(((int)time.Subtract(local_epoch).TotalSeconds) - utc_offset); 51 } 52 time_tToDateTime(IntPtr time_t)53 public static DateTime time_tToDateTime(IntPtr time_t) 54 { 55 // The itunes database uses a 32bit signed value, so enforce that here to avoid 56 // overflow issues. We still need to represent time with an IntPtr though as 57 // that's what libgpod publicly exposes 58 return local_epoch.AddSeconds(time_t.ToInt32() + utc_offset); 59 } 60 61 internal IntPtr Native { 62 get { return HandleRef.ToIntPtr(Handle); } 63 } 64 65 IntPtr IGPodBase.Native { 66 get { return Native; } 67 } 68 69 public HandleRef Handle; 70 protected bool Borrowed; 71 GPodBase(IntPtr handle)72 public GPodBase(IntPtr handle) : this(handle, true) {} GPodBase(IntPtr handle, bool borrowed)73 public GPodBase(IntPtr handle, bool borrowed) { 74 Borrowed = borrowed; 75 Handle = new HandleRef(this, handle); 76 } ~GPodBase()77 ~GPodBase() { if (!Borrowed) Destroy(); } 78 SetBorrowed(bool borrowed)79 public void SetBorrowed(bool borrowed) { Borrowed = borrowed; } Destroy()80 protected abstract void Destroy(); Dispose()81 public void Dispose() 82 { 83 if (!Borrowed) 84 Destroy(); 85 } 86 } 87 } 88