3 using System.Collections;
4 using System.Runtime.InteropServices;
7 using Mono.Unix.Native;
11 public class ServiceDialog : Dialog
13 [DllImport ("avahi-ui")]
14 private static extern IntPtr aui_service_dialog_new (string title, IntPtr parent, IntPtr dummy);
16 [DllImport ("avahi-ui")]
17 private static extern void aui_service_dialog_set_browse_service_typesv (IntPtr dialog, IntPtr[] types);
19 [DllImport ("avahi-ui")]
20 private static extern IntPtr aui_service_dialog_get_browse_service_types (IntPtr dialog);
22 [DllImport ("avahi-ui")]
23 private static extern IntPtr aui_service_dialog_get_domain (IntPtr dialog);
25 [DllImport ("avahi-ui")]
26 private static extern void aui_service_dialog_set_domain (IntPtr dialog, IntPtr domain);
28 [DllImport ("avahi-ui")]
29 private static extern IntPtr aui_service_dialog_get_service_type (IntPtr dialog);
31 [DllImport ("avahi-ui")]
32 private static extern void aui_service_dialog_set_service_type (IntPtr dialog, IntPtr type);
34 [DllImport ("avahi-ui")]
35 private static extern IntPtr aui_service_dialog_get_service_name (IntPtr dialog);
37 [DllImport ("avahi-ui")]
38 private static extern void aui_service_dialog_set_service_name (IntPtr dialog, IntPtr type);
40 [DllImport ("avahi-ui")]
41 private static extern IntPtr aui_service_dialog_get_address (IntPtr dialog);
43 [DllImport ("avahi-ui")]
44 private static extern UInt16 aui_service_dialog_get_port (IntPtr dialog);
46 [DllImport ("avahi-ui")]
47 private static extern IntPtr aui_service_dialog_get_host_name (IntPtr dialog);
49 [DllImport ("avahi-ui")]
50 private static extern IntPtr aui_service_dialog_get_txt_data (IntPtr dialog);
52 [DllImport ("avahi-ui")]
53 private static extern bool aui_service_dialog_get_resolve_service (IntPtr dialog);
55 [DllImport ("avahi-ui")]
56 private static extern void aui_service_dialog_set_resolve_service (IntPtr dialog, bool val);
58 [DllImport ("avahi-ui")]
59 private static extern bool aui_service_dialog_get_resolve_host_name (IntPtr dialog);
61 [DllImport ("avahi-ui")]
62 private static extern void aui_service_dialog_set_resolve_host_name (IntPtr dialog, bool val);
64 [DllImport ("avahi-ui")]
65 private static extern Protocol aui_service_dialog_get_address_family (IntPtr dialog);
67 [DllImport ("avahi-ui")]
68 private static extern void aui_service_dialog_set_address_family (IntPtr dialog, Protocol proto);
70 [DllImport ("avahi-common")]
71 private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address);
73 [DllImport ("avahi-common")]
74 private static extern IntPtr avahi_string_list_get_next (IntPtr list);
76 [DllImport ("avahi-common")]
77 private static extern IntPtr avahi_string_list_get_text (IntPtr list);
79 [DllImport ("avahi-common")]
80 private static extern int avahi_string_list_get_size (IntPtr list);
82 public string[] BrowseServiceTypes {
84 IntPtr arr = aui_service_dialog_get_browse_service_types (Raw);
86 ArrayList values = new ArrayList ();
88 for (int i = 0;;i++) {
89 IntPtr ptr = Marshal.ReadIntPtr (arr, i * Marshal.SizeOf (typeof (IntPtr)));
91 if (ptr == IntPtr.Zero)
94 values.Add (GLib.Marshaller.Utf8PtrToString (ptr));
97 return (string[]) values.ToArray (typeof (string));
101 types = new IntPtr[] { IntPtr.Zero };
103 types = new IntPtr[value.Length + 1];
105 for (int i = 0; i < value.Length; i++) {
106 types[i] = GLib.Marshaller.StringToPtrGStrdup (value[i]);
109 types[value.Length] = IntPtr.Zero;
112 aui_service_dialog_set_browse_service_typesv (Raw, types);
114 for (int i = 0;;i++) {
115 if (types[i] != IntPtr.Zero)
118 GLib.Marshaller.Free (types[i]);
123 public string ServiceType {
125 return GLib.Marshaller.Utf8PtrToString (aui_service_dialog_get_service_type (Raw));
127 IntPtr type = GLib.Marshaller.StringToPtrGStrdup (value);
128 aui_service_dialog_set_service_type (Raw, type);
129 GLib.Marshaller.Free (type);
133 public string ServiceName {
135 return GLib.Marshaller.Utf8PtrToString (aui_service_dialog_get_service_name (Raw));
137 IntPtr name = GLib.Marshaller.StringToPtrGStrdup (value);
138 aui_service_dialog_set_service_name (Raw, name);
139 GLib.Marshaller.Free (name);
143 public IPAddress Address {
145 return PtrToAddress (aui_service_dialog_get_address (Raw));
151 return aui_service_dialog_get_port (Raw);
155 public string HostName {
157 return GLib.Marshaller.Utf8PtrToString (aui_service_dialog_get_host_name (Raw));
161 public string Domain {
163 return GLib.Marshaller.Utf8PtrToString (aui_service_dialog_get_domain (Raw));
165 IntPtr domain = GLib.Marshaller.StringToPtrGStrdup (value);
166 aui_service_dialog_set_domain (Raw, domain);
167 GLib.Marshaller.Free (domain);
171 public byte[][] TxtData {
173 ArrayList txtlist = new ArrayList ();
174 IntPtr txt = aui_service_dialog_get_txt_data (Raw);
176 for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
177 IntPtr buf = avahi_string_list_get_text (l);
178 int len = avahi_string_list_get_size (l);
180 byte[] txtbuf = new byte[len];
181 Marshal.Copy (buf, txtbuf, 0, len);
182 txtlist.Add (txtbuf);
185 return (byte[][]) txtlist.ToArray (typeof (byte[]));
189 public bool ResolveServiceEnabled {
191 return aui_service_dialog_get_resolve_service (Raw);
193 aui_service_dialog_set_resolve_service (Raw, value);
197 public bool ResolveHostNameEnabled {
199 return aui_service_dialog_get_resolve_host_name (Raw);
201 aui_service_dialog_set_resolve_host_name (Raw, value);
205 public Protocol AddressFamily {
207 return aui_service_dialog_get_address_family (Raw);
209 aui_service_dialog_set_address_family (Raw, value);
213 public ServiceDialog (string title, Window parent, params object[] buttonData)
215 Raw = aui_service_dialog_new (title, parent == null ? IntPtr.Zero : parent.Handle,
218 for (int i = 0; i < buttonData.Length - 1; i += 2) {
219 AddButton ((string) buttonData[i], (int) buttonData[i + 1]);
223 private static IPAddress PtrToAddress (IntPtr ptr)
225 IPAddress address = null;
227 if (ptr != IntPtr.Zero) {
228 IntPtr buf = Stdlib.malloc (256);
229 IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr);
230 address = IPAddress.Parse (GLib.Marshaller.Utf8PtrToString (addrPtr));
232 Stdlib.free (addrPtr);