~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

« back to all changes in this revision

Viewing changes to Tomboy/dbus-sharp/ObjectPath.cs

Tags: upstream-0.3.9+dfsg
ImportĀ upstreamĀ versionĀ 0.3.9+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Runtime.InteropServices;
 
3
using System.Reflection.Emit;
 
4
 
 
5
using DBus;
 
6
 
 
7
namespace DBus.DBusType
 
8
{
 
9
  /// <summary>
 
10
  /// An object path.
 
11
  /// </summary>
 
12
  public class ObjectPath : IDBusType
 
13
  {
 
14
    public const char Code = 'o';
 
15
    private string path = null;
 
16
    private object val = null;
 
17
    private Service service = null;
 
18
    
 
19
    private ObjectPath()
 
20
    {
 
21
    }
 
22
    
 
23
    public ObjectPath(object val, Service service) 
 
24
    {
 
25
      this.val = val;
 
26
      this.service = service;
 
27
    }
 
28
    
 
29
    public ObjectPath(IntPtr iter, Service service)
 
30
    {
 
31
      IntPtr raw;
 
32
 
 
33
      dbus_message_iter_get_basic (iter, out raw);
 
34
 
 
35
      this.path = Marshal.PtrToStringAnsi (raw);
 
36
      this.service = service;
 
37
    }
 
38
 
 
39
    private string Path
 
40
    {
 
41
      get {
 
42
        if (this.path == null && this.val != null) {
 
43
          Handler handler = this.service.GetHandler(this.val);
 
44
          this.path = handler.Path;
 
45
        }
 
46
 
 
47
        return this.path;
 
48
      }
 
49
    }
 
50
 
 
51
    public void Append(IntPtr iter) 
 
52
    {
 
53
      IntPtr marshalVal = Marshal.StringToHGlobalAnsi (Path);
 
54
 
 
55
      bool success = dbus_message_iter_append_basic (iter, (int) Code, ref marshalVal);
 
56
      Marshal.FreeHGlobal (marshalVal);
 
57
 
 
58
      if (!success)
 
59
        throw new ApplicationException("Failed to append OBJECT_PATH argument:" + val);
 
60
    }
 
61
 
 
62
    public static bool Suits(System.Type type) 
 
63
    {
 
64
      object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), false);
 
65
      if (attributes.Length == 1) {
 
66
        return true;
 
67
      } else {
 
68
        return false;
 
69
      }
 
70
    }
 
71
 
 
72
    public static void EmitMarshalIn(ILGenerator generator, Type type)
 
73
    {
 
74
      if (type.IsByRef) {
 
75
        generator.Emit(OpCodes.Ldind_Ref);
 
76
      }
 
77
    }
 
78
 
 
79
    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
 
80
    {
 
81
      generator.Emit(OpCodes.Castclass, type);
 
82
      if (!isReturn) {
 
83
        generator.Emit(OpCodes.Stind_Ref);
 
84
      }
 
85
    }
 
86
 
 
87
    public object Get() 
 
88
    {
 
89
      throw new ArgumentException("Cannot call Get on an ObjectPath without specifying type.");
 
90
    }
 
91
 
 
92
    public object Get(System.Type type)
 
93
    {
 
94
      try {
 
95
        return this.service.GetObject(type, Path);
 
96
      } catch(Exception ex) {
 
97
        throw new ArgumentException("Cannot cast object pointed to by Object Path to type '" + type.ToString() + "': " + ex);
 
98
      }
 
99
    }
 
100
 
 
101
    [DllImport("dbus-1")]
 
102
    private extern static void dbus_message_iter_get_basic (IntPtr iter, out IntPtr path);
 
103
 
 
104
    [DllImport("dbus-1")]
 
105
    private extern static bool dbus_message_iter_append_basic (IntPtr iter, int type, ref IntPtr path);
 
106
  }
 
107
}