~ubuntu-branches/ubuntu/gutsy/tomboy/gutsy-updates

« back to all changes in this revision

Viewing changes to Tomboy/dbus-sharp/dbus-sharp/src/MessageWriter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-11-22 19:22:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20061122192217-odnb17d1ib22okof
Tags: 0.5.1-0ubuntu1
* New upstream release:
  + New Managed D-Bus/DBusSharp (Sebastian Dröge)
  + Additional search interface improvements
  + More secure wrapper script to launch Tomboy.exe
  + Fix panel and TrayIcon resizing
  + Fix 1x1 pixel TrayIcon
  + Removed old tintin image
* debian/control:
  + Update build dependencies
* debian/patches/01-dbus0.9.patch,
  debian/patches/50_tintin.patch,
  debian/patches/51_tomboy-dllmap.patch,
  debian/patches/52_external-dbus-sharp.patch,
  debian/patches/53_tomboy-tray-icon.patch:
  + Dropped, merged upstream
* debian/rules,
  debian/tomboy.desktop:
  + Use upstream's desktop file again after it was fixed now
* debian/rules:
  + DBus service file path workaround removed, it's fixed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006 Alp Toker <alp@atoker.com>
 
2
// This software is made available under the MIT License
 
3
// See COPYING for details
 
4
 
 
5
//defined by default, since this is not a controversial extension
 
6
#define PROTO_TYPE_SINGLE
 
7
 
 
8
using System;
 
9
using System.Text;
 
10
using System.Diagnostics;
 
11
using System.Collections.Generic;
 
12
using System.IO;
 
13
 
 
14
namespace NDesk.DBus
 
15
{
 
16
        public class MessageWriter
 
17
        {
 
18
                //TODO: use endianness instead of writing the message is in native format
 
19
                protected EndianFlag endianness;
 
20
                protected MemoryStream stream;
 
21
                protected BinaryWriter bw;
 
22
 
 
23
                //TODO: enable this ctor instead of the current one when endian support is done
 
24
                //public MessageWriter () : this (Connection.NativeEndianness)
 
25
                public MessageWriter () : this (EndianFlag.Little)
 
26
                {
 
27
                }
 
28
 
 
29
                public MessageWriter (EndianFlag endianness)
 
30
                {
 
31
                        if (endianness != EndianFlag.Little)
 
32
                                throw new NotImplementedException ("Only little-endian message writing is currently supported");
 
33
 
 
34
                        this.endianness = endianness;
 
35
                        stream = new MemoryStream ();
 
36
                        bw = new BinaryWriter (stream);
 
37
                }
 
38
 
 
39
                public byte[] ToArray ()
 
40
                {
 
41
                        //TODO: mark the writer locked or something here
 
42
                        return stream.ToArray ();
 
43
                }
 
44
 
 
45
                public void CloseWrite ()
 
46
                {
 
47
                        int needed = Protocol.PadNeeded ((int)stream.Position, 8);
 
48
                        for (int i = 0 ; i != needed ; i++)
 
49
                                stream.WriteByte (0);
 
50
                }
 
51
 
 
52
                public void Write (byte val)
 
53
                {
 
54
                        WritePad (1);
 
55
                        bw.Write (val);
 
56
                }
 
57
 
 
58
                public void Write (bool val)
 
59
                {
 
60
                        WritePad (4);
 
61
                        bw.Write ((uint) (val ? 1 : 0));
 
62
                }
 
63
 
 
64
                public void Write (short val)
 
65
                {
 
66
                        WritePad (2);
 
67
                        bw.Write (val);
 
68
                }
 
69
 
 
70
                public void Write (ushort val)
 
71
                {
 
72
                        WritePad (2);
 
73
                        bw.Write (val);
 
74
                }
 
75
 
 
76
                public void Write (int val)
 
77
                {
 
78
                        WritePad (4);
 
79
                        bw.Write (val);
 
80
                }
 
81
 
 
82
                public void Write (uint val)
 
83
                {
 
84
 
 
85
                        WritePad (4);
 
86
                        bw.Write (val);
 
87
                }
 
88
 
 
89
                public void Write (long val)
 
90
                {
 
91
                        WritePad (8);
 
92
                        bw.Write (val);
 
93
                }
 
94
 
 
95
                public void Write (ulong val)
 
96
                {
 
97
                        WritePad (8);
 
98
                        bw.Write (val);
 
99
                }
 
100
 
 
101
#if PROTO_TYPE_SINGLE
 
102
                public void Write (float val)
 
103
                {
 
104
                        WritePad (4);
 
105
                        bw.Write (val);
 
106
                }
 
107
#endif
 
108
 
 
109
                public void Write (double val)
 
110
                {
 
111
                        WritePad (8);
 
112
                        bw.Write (val);
 
113
                }
 
114
 
 
115
                public void Write (string val)
 
116
                {
 
117
                        byte[] utf8_data = Encoding.UTF8.GetBytes (val);
 
118
                        Write ((uint)utf8_data.Length);
 
119
                        bw.Write (utf8_data);
 
120
                        bw.Write ((byte)0); //NULL string terminator
 
121
                }
 
122
 
 
123
                public void Write (ObjectPath val)
 
124
                {
 
125
                        Write (val.Value);
 
126
                }
 
127
 
 
128
                public void Write (Signature val)
 
129
                {
 
130
                        WritePad (1);
 
131
                        Write ((byte)val.Length);
 
132
                        bw.Write (val.GetBuffer ());
 
133
                        bw.Write ((byte)0); //NULL signature terminator
 
134
                }
 
135
 
 
136
                public void Write (Type type, object val)
 
137
                {
 
138
                        if (type == typeof (void))
 
139
                                return;
 
140
 
 
141
                        if (type.IsArray) {
 
142
                                Write (type, (Array)val);
 
143
                        } else if (type == typeof (ObjectPath)) {
 
144
                                Write ((ObjectPath)val);
 
145
                        } else if (type == typeof (Signature)) {
 
146
                                Write ((Signature)val);
 
147
                        } else if (type.IsGenericType && (type.GetGenericTypeDefinition () == typeof (IDictionary<,>) || type.GetGenericTypeDefinition () == typeof (Dictionary<,>))) {
 
148
                                Type[] genArgs = type.GetGenericArguments ();
 
149
                                System.Collections.IDictionary idict = (System.Collections.IDictionary)val;
 
150
                                WriteFromDict (genArgs[0], genArgs[1], idict);
 
151
                        } else if (!type.IsPrimitive && type.IsValueType && !type.IsEnum) {
 
152
                                Write (type, (ValueType)val);
 
153
                                /*
 
154
                        } else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
 
155
                                //is it possible to support nullable types?
 
156
                                Type[] genArgs = type.GetGenericArguments ();
 
157
                                WriteVariant (genArgs[0], val);
 
158
                                */
 
159
                        } else {
 
160
                                Write (Signature.TypeToDType (type), val);
 
161
                        }
 
162
                }
 
163
 
 
164
                //helper method, should not be used as it boxes needlessly
 
165
                public void Write (DType dtype, object val)
 
166
                {
 
167
                        switch (dtype)
 
168
                        {
 
169
                                case DType.Byte:
 
170
                                {
 
171
                                        Write ((byte)val);
 
172
                                }
 
173
                                break;
 
174
                                case DType.Boolean:
 
175
                                {
 
176
                                        Write ((bool)val);
 
177
                                }
 
178
                                break;
 
179
                                case DType.Int16:
 
180
                                {
 
181
                                        Write ((short)val);
 
182
                                }
 
183
                                break;
 
184
                                case DType.UInt16:
 
185
                                {
 
186
                                        Write ((ushort)val);
 
187
                                }
 
188
                                break;
 
189
                                case DType.Int32:
 
190
                                {
 
191
                                        Write ((int)val);
 
192
                                }
 
193
                                break;
 
194
                                case DType.UInt32:
 
195
                                {
 
196
                                        Write ((uint)val);
 
197
                                }
 
198
                                break;
 
199
                                case DType.Int64:
 
200
                                {
 
201
                                        Write ((long)val);
 
202
                                }
 
203
                                break;
 
204
                                case DType.UInt64:
 
205
                                {
 
206
                                        Write ((ulong)val);
 
207
                                }
 
208
                                break;
 
209
#if PROTO_TYPE_SINGLE
 
210
                                case DType.Single:
 
211
                                {
 
212
                                        Write ((float)val);
 
213
                                }
 
214
                                break;
 
215
#endif
 
216
                                case DType.Double:
 
217
                                {
 
218
                                        Write ((double)val);
 
219
                                }
 
220
                                break;
 
221
                                case DType.String:
 
222
                                {
 
223
                                        Write ((string)val);
 
224
                                }
 
225
                                break;
 
226
                                case DType.ObjectPath:
 
227
                                {
 
228
                                        Write ((ObjectPath)val);
 
229
                                }
 
230
                                break;
 
231
                                case DType.Signature:
 
232
                                {
 
233
                                        Write ((Signature)val);
 
234
                                }
 
235
                                break;
 
236
                                case DType.Variant:
 
237
                                {
 
238
                                        Write ((object)val);
 
239
                                }
 
240
                                break;
 
241
                                default:
 
242
                                throw new Exception ("Unhandled D-Bus type: " + dtype);
 
243
                        }
 
244
                }
 
245
 
 
246
                //variant
 
247
                public void Write (object val)
 
248
                {
 
249
                        //TODO: maybe support sending null variants
 
250
 
 
251
                        if (val == null)
 
252
                                throw new NotSupportedException ("Cannot send null variant");
 
253
 
 
254
                        Type type = val.GetType ();
 
255
 
 
256
                        WriteVariant (type, val);
 
257
                }
 
258
 
 
259
                public void WriteVariant (Type type, object val)
 
260
                {
 
261
                        Signature sig = Signature.GetSig (type);
 
262
 
 
263
                        Write (sig);
 
264
                        Write (type, val);
 
265
                }
 
266
 
 
267
                //this requires a seekable stream for now
 
268
                public void Write (Type type, Array val)
 
269
                {
 
270
                        //if (type.IsArray)
 
271
                        type = type.GetElementType ();
 
272
 
 
273
                        Write ((uint)0);
 
274
                        long lengthPos = stream.Position - 4;
 
275
 
 
276
                        //advance to the alignment of the element
 
277
                        WritePad (Protocol.GetAlignment (Signature.TypeToDType (type)));
 
278
 
 
279
                        long startPos = stream.Position;
 
280
 
 
281
                        foreach (object elem in val)
 
282
                                Write (type, elem);
 
283
 
 
284
                        long endPos = stream.Position;
 
285
 
 
286
                        stream.Position = lengthPos;
 
287
                        Write ((uint)(endPos - startPos));
 
288
 
 
289
                        stream.Position = endPos;
 
290
                }
 
291
 
 
292
                public void WriteFromDict (Type keyType, Type valType, System.Collections.IDictionary val)
 
293
                {
 
294
                        Write ((uint)0);
 
295
                        long lengthPos = stream.Position - 4;
 
296
 
 
297
                        //advance to the alignment of the element
 
298
                        //WritePad (Protocol.GetAlignment (Signature.TypeToDType (type)));
 
299
                        WritePad (8);
 
300
 
 
301
                        long startPos = stream.Position;
 
302
 
 
303
                        foreach (System.Collections.DictionaryEntry entry in val)
 
304
                        {
 
305
                                WritePad (8);
 
306
 
 
307
                                Write (keyType, entry.Key);
 
308
                                Write (valType, entry.Value);
 
309
                        }
 
310
 
 
311
                        long endPos = stream.Position;
 
312
 
 
313
                        stream.Position = lengthPos;
 
314
                        Write ((uint)(endPos - startPos));
 
315
 
 
316
                        stream.Position = endPos;
 
317
                }
 
318
 
 
319
                public void Write (Type type, ValueType val)
 
320
                {
 
321
                        WritePad (8); //offset for structs, right?
 
322
 
 
323
                        /*
 
324
                        ConstructorInfo[] cis = type.GetConstructors ();
 
325
                        if (cis.Length != 0) {
 
326
                                System.Reflection.ParameterInfo[]  parms = ci.GetParameters ();
 
327
 
 
328
                                foreach (ParameterInfo parm in parms) {
 
329
                                }
 
330
                        }
 
331
                        */
 
332
                        if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (KeyValuePair<,>)) {
 
333
                                System.Reflection.PropertyInfo key_prop = type.GetProperty ("Key");
 
334
                                Write (key_prop.PropertyType, key_prop.GetValue (val, null));
 
335
 
 
336
                                System.Reflection.PropertyInfo val_prop = type.GetProperty ("Value");
 
337
                                Write (val_prop.PropertyType, val_prop.GetValue (val, null));
 
338
 
 
339
                                return;
 
340
                        }
 
341
 
 
342
                        System.Reflection.FieldInfo[] fis = type.GetFields ();
 
343
 
 
344
                        foreach (System.Reflection.FieldInfo fi in fis) {
 
345
                                object elem;
 
346
                                //public virtual object GetValueDirect (TypedReference obj);
 
347
                                elem = fi.GetValue (val);
 
348
                                //Write (Signature.TypeToDType (fi.FieldType), elem);
 
349
                                Write (fi.FieldType, elem);
 
350
                        }
 
351
                }
 
352
 
 
353
                //RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider ();
 
354
                public void WritePad (int alignment)
 
355
                {
 
356
                        stream.Position = Protocol.Padded ((int)stream.Position, alignment);
 
357
                }
 
358
        }
 
359
}