summaryrefslogtreecommitdiff
path: root/tools/inspect/utils.vala
blob: ca545538d91e4380d2579979982ff8887296b777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Philip Withnall <philip.withnall@collabora.co.uk>
 */

using Folks;
using Gee;
using GLib;

private class Folks.Inspect.Utils
{
  /* The current indentation level, in spaces */
  private static uint indentation = 0;
  private static string indentation_string = "";

  public static void init ()
    {
      Utils.indentation_string = "";

      /* Register some general transformation functions */
      Value.register_transform_func (typeof (Object), typeof (string),
          Utils.transform_object_to_string);
      Value.register_transform_func (typeof (Folks.PersonaStore),
          typeof (string), Utils.transform_persona_store_to_string);
      Value.register_transform_func (typeof (string[]), typeof (string),
          Utils.transform_string_array_to_string);
    }

  private static void transform_object_to_string (Value src,
      out Value dest)
    {
      /* FIXME: works around bgo#638363 */
      Value dest_tmp = Value (typeof (string));
      dest_tmp.take_string ("%p".printf (src.get_object ()));
      dest = dest_tmp;
    }

  private static void transform_persona_store_to_string (Value src,
      out Value dest)
    {
      /* FIXME: works around bgo#638363 */
      Value dest_tmp = Value (typeof (string));
      Folks.PersonaStore store = (Folks.PersonaStore) src.get_object ();
      dest_tmp.take_string ("%p: %s, %s (%s)".printf (store, store.type_id,
          store.id, store.display_name));
      dest = dest_tmp;
    }

  private static void transform_string_array_to_string (Value src,
      out Value dest)
    {
      /* FIXME: works around bgo#638363 */
      Value dest_tmp = Value (typeof (string));
      unowned string[] array = (string[]) src.get_boxed ();
      string output = "{ ";
      bool first = true;
      foreach (string element in array)
        {
          if (first == false)
            output += ", ";
          output += "'%s'".printf (element);
          first = false;
        }
      output += " }";
      dest_tmp.take_string (output);
      dest = dest_tmp;
    }

  public static void indent ()
    {
      /* We indent in increments of two spaces */
      Utils.indentation += 2;
      Utils.indentation_string = string.nfill (Utils.indentation, ' ');
    }

  public static void unindent ()
    {
      Utils.indentation -= 2;
      Utils.indentation_string = string.nfill (Utils.indentation, ' ');
    }

  [PrintfFormat ()]
  public static void print_line (string format, ...)
    {
      /* FIXME: store the va_list temporarily to work around bgo#638308 */
      var valist = va_list ();
      string output = format.vprintf (valist);
      stdout.printf ("%s%s\n", Utils.indentation_string, output);
    }

  public static void print_individual (Individual individual,
      bool show_personas)
    {
      Utils.print_line ("Individual '%s' with %u personas:",
          individual.id, individual.personas.length ());

      /* List the Individual's properties */
      unowned ParamSpec[] properties =
          individual.get_class ().list_properties ();

      Utils.indent ();
      foreach (unowned ParamSpec pspec in properties)
        {
          Value prop_value;
          string output_string;

          /* Ignore the personas property if we're printing the personas out */
          if (show_personas == true && pspec.get_name () == "personas")
            continue;

          prop_value = Value (pspec.value_type);
          individual.get_property (pspec.get_name (), ref prop_value);

          output_string = Utils.property_to_string (individual.get_type (),
              pspec.get_name (), prop_value);

          Utils.print_line ("%-20s  %s", pspec.get_nick (), output_string);
        }

      if (show_personas == true)
        {
          Utils.print_line ("");
          Utils.print_line ("Personas:");

          Utils.indent ();
          foreach (Persona persona in individual.personas)
            Utils.print_persona (persona);
          Utils.unindent ();
        }
      Utils.unindent ();
    }

  public static void print_persona (Persona persona)
    {
      Utils.print_line ("Persona '%s':", persona.uid);

      /* List the Persona's properties */
      unowned ParamSpec[] properties =
          persona.get_class ().list_properties ();

      Utils.indent ();
      foreach (unowned ParamSpec pspec in properties)
        {
          Value prop_value;
          string output_string;

          prop_value = Value (pspec.value_type);
          persona.get_property (pspec.get_name (), ref prop_value);

          output_string = Utils.property_to_string (persona.get_type (),
              pspec.get_name (), prop_value);

          Utils.print_line ("%-20s  %s", pspec.get_nick (), output_string);
        }
      Utils.unindent ();
    }

  public static void print_persona_store (PersonaStore store,
      bool show_personas)
    {
      Utils.print_line ("Persona store '%s' with %u personas:",
          store.id, store.personas.size ());

      /* List the store's properties */
      unowned ParamSpec[] properties =
          store.get_class ().list_properties ();

      Utils.indent ();
      foreach (unowned ParamSpec pspec in properties)
        {
          Value prop_value;
          string output_string;

          /* Ignore the personas property if we're printing the personas out */
          if (show_personas == true && pspec.get_name () == "personas")
            continue;

          prop_value = Value (pspec.value_type);
          store.get_property (pspec.get_name (), ref prop_value);

          output_string = Utils.property_to_string (store.get_type (),
              pspec.get_name (), prop_value);

          Utils.print_line ("%-20s  %s", pspec.get_nick (), output_string);
        }

      if (show_personas == true)
        {
          Utils.print_line ("");
          Utils.print_line ("Personas:");

          Utils.indent ();
          store.personas.foreach ((k, v) =>
            {
              Utils.print_persona ((Persona) v);
            });
          Utils.unindent ();
        }
      Utils.unindent ();
    }

  private static string property_to_string (Type object_type,
      string prop_name,
      Value prop_value)
    {
      string output_string;

      /* Overrides for various known properties */
      if (object_type.is_a (typeof (Individual)) && prop_name == "personas")
        {
          unowned GLib.List<Persona> personas =
              (GLib.List<Persona>) prop_value.get_pointer ();
          return "List of %u personas".printf (personas.length ());
        }
      else if (object_type.is_a (typeof (PersonaStore)) &&
          prop_name == "personas")
        {
          unowned HashTable<string, Persona> personas =
              (HashTable<string, Persona>) prop_value.get_boxed ();
          return "Set of %u personas".printf (personas.size ());
        }
      else if (prop_name == "groups")
        {
          HashTable<string, bool> groups =
              (HashTable<string, bool>) prop_value.get_boxed ();
          output_string = "{ ";
          bool first = true;

          /* FIXME: This is rather inefficient */
          groups.foreach ((k, v) =>
            {
              if ((bool) v == true)
                {
                  if (first == false)
                    output_string += ", ";
                  output_string += "'%s'".printf ((string) k);
                  first = false;
                }
            });

          output_string += " }";
          return output_string;
        }
      else if (prop_name == "avatar")
        {
          string ret = null;
          File avatar = (File) prop_value.get_object ();
          if (avatar != null)
            ret = avatar.get_uri ();
          return ret;
        }
      else if (prop_name == "im-addresses")
        {
          HashTable<string, LinkedHashSet<string>> im_addresses =
              (HashTable<string, LinkedHashSet<string>>)
              prop_value.get_boxed ();
          output_string = "{ ";
          bool first = true;

          /* FIXME: This is rather inefficient */
          im_addresses.foreach ((k, v) =>
            {
              if (first == false)
                output_string += ", ";
              output_string += "'%s' : { ".printf ((string) k);
              first = false;

              LinkedHashSet<string> addresses = (LinkedHashSet<string>) v;
              bool _first = true;
              foreach (var a in addresses)
                {
                  if (_first == false)
                    output_string += ", ";
                  output_string += "'%s'".printf ((string) a);
                  _first = false;
                }

              output_string += " }";
            });

          output_string += " }";
          return output_string;
        }
      else if (prop_name == Folks.PersonaStore.detail_key
          (PersonaDetail.WEB_SERVICE_ADDRESSES))
        {
          HashMap<string, LinkedHashSet<string>> web_service_addresses =
              (HashMap<string, LinkedHashSet<string>>)
              prop_value.get_object ();
          output_string = "{ ";
          bool first = true;

          foreach (var entry in web_service_addresses.entries)
            {
              if (first == false)
                output_string += ", ";
              output_string += "'%s' : { ".printf ((string) entry.key);
              first = false;

              LinkedHashSet<string> addresses =
                  (LinkedHashSet<string>) entry.value;
              bool _first = true;
              foreach (var a in addresses)
                {
                  if (_first == false)
                    output_string += ", ";
                  output_string += "'%s'".printf ((string) a);
                  _first = false;
                }

              output_string += " }";
            }

          output_string += " }";
          return output_string;
        }
      else if (prop_name == "email-addresses" ||
               prop_name == "urls" ||
               prop_name == "phone-numbers")
        {
          output_string = "{ ";
          bool first = true;
          unowned GLib.List<FieldDetails> prop_list =
              (GLib.List<FieldDetails>) prop_value.get_pointer ();

          foreach (var p in prop_list)
            {
              if (!first)
                {
                  output_string += ", ";
                }
              output_string +=  p.value;
              first = false;
            }
            output_string += " }";

            return output_string;
        }
      else if (prop_name == "birthday")
        {
          unowned DateTime dobj = (DateTime) prop_value.get_boxed ();
          if (dobj != null)
            return dobj.to_string ();
          else
            return "";
        }
      else if (prop_name == "postal-addresses")
        {
          output_string = "{ ";
          bool first = true;
          unowned GLib.List<PostalAddress> prop_list =
              (GLib.List<PostalAddress>) prop_value.get_pointer ();

          foreach (var p in prop_list)
            {
              if (!first)
                {
                  output_string += ". ";
                }
              output_string +=  p.to_string ();
              first = false;
            }
            output_string += " }";

            return output_string;
        }
      else if (prop_name == "structured-name")
        {
          unowned StructuredName sn = (StructuredName) prop_value.get_object ();
          string ret = null;
          if (sn != null)
            ret = sn.to_string ();
          return ret;
        }

      return Utils.transform_value_to_string (prop_value);
    }

  public static string transform_value_to_string (Value prop_value)
    {
      if (Value.type_transformable (prop_value.type (), typeof (string)))
        {
          /* Convert to a string value */
          Value string_value = Value (typeof (string));
          prop_value.transform (ref string_value);
          return string_value.get_string ();
        }
      else
        {
          /* Can't convert the property value to a string */
          return "Can't convert from type '%s' to '%s'".printf (
              prop_value.type ().name (), typeof (string).name ());
        }
    }

  /* FIXME: This can't be in the command_completion_cb() function because Vala
   * doesn't allow static local variables. Erk. */
  private static MapIterator<string, Command>? command_name_iter = null;

  /* Complete a command name, starting with @word. */
  public static string? command_name_completion_cb (string word,
      int state)
    {
      /* Initialise state. Whoever wrote the readline API should be shot. */
      if (state == 0)
        Utils.command_name_iter = main_client.commands.map_iterator ();

      while (Utils.command_name_iter.next () == true)
        {
          string command_name = Utils.command_name_iter.get_key ();
          if (command_name.has_prefix (word))
            return command_name;
        }

      /* Clean up */
      Utils.command_name_iter = null;
      return null;
    }

  /* FIXME: This can't be in the individual_id_completion_cb() function because
   * Vala doesn't allow static local variables. Erk. */
  private static HashTableIter<string, Individual>? individual_id_iter = null;

  /* Complete an individual's ID, starting with @word. */
  public static string? individual_id_completion_cb (string word,
      int state)
    {
      /* Initialise state. Whoever wrote the readline API should be shot. */
      if (state == 0)
        {
          Utils.individual_id_iter = HashTableIter<string, Individual> (
              main_client.aggregator.individuals);
        }

      string id;
      Individual individual;
      while (Utils.individual_id_iter.next (out id, out individual) == true)
        {
          if (id.has_prefix (word))
            return id;
        }

      /* Clean up */
      Utils.individual_id_iter = null;
      return null;
    }

  /* FIXME: This can't be in the individual_id_completion_cb() function because
   * Vala doesn't allow static local variables. Erk. */
  private static unowned GLib.List<Persona>? persona_uid_iter = null;

  /* Complete an individual's ID, starting with @word. */
  public static string? persona_uid_completion_cb (string word,
      int state)
    {
      /* Initialise state. Whoever wrote the readline API should be shot. */
      if (state == 0)
        {
          Utils.individual_id_iter = HashTableIter<string, Individual> (
              main_client.aggregator.individuals);
          Utils.persona_uid_iter = null;
        }

      Individual individual = null;
      while (Utils.persona_uid_iter != null ||
          Utils.individual_id_iter.next (null, out individual) == true)
        {
          if (Utils.persona_uid_iter == null)
            {
              assert (individual != null);
              Utils.persona_uid_iter = individual.personas;
            }

          while (Utils.persona_uid_iter != null)
            {
              unowned Persona persona = (Persona) Utils.persona_uid_iter.data;
              Utils.persona_uid_iter = Utils.persona_uid_iter.next;
              if (persona.uid.has_prefix (word))
                return persona.uid;
            }
        }

      /* Clean up */
      Utils.individual_id_iter = null;
      return null;
    }

  /* FIXME: This can't be in the backend_name_completion_cb() function because
   * Vala doesn't allow static local variables. Erk. */
  private static Iterator<Backend>? backend_name_iter = null;

  /* Complete an individual's ID, starting with @word. */
  public static string? backend_name_completion_cb (string word,
      int state)
    {
      /* Initialise state. Whoever wrote the readline API should be shot. */
      if (state == 0)
        {
          Utils.backend_name_iter =
              main_client.backend_store.list_backends ().iterator ();
        }

      while (Utils.backend_name_iter.next () == true)
        {
          Backend backend = Utils.backend_name_iter.get ();
          if (backend.name.has_prefix (word))
            return backend.name;
        }

      /* Clean up */
      Utils.backend_name_iter = null;
      return null;
    }

  /* FIXME: This can't be in the persona_store_id_completion_cb() function
   * because Vala doesn't allow static local variables. Erk. */
  private static HashTableIter<string, PersonaStore>? persona_store_id_iter =
      null;

  /* Complete a persona store's ID, starting with @word. */
  public static string? persona_store_id_completion_cb (string word,
      int state)
    {
      /* Initialise state. Whoever wrote the readline API should be shot. */
      if (state == 0)
        {
          Utils.backend_name_iter =
              main_client.backend_store.list_backends ().iterator ();
          Utils.persona_store_id_iter = null;
        }

      while (Utils.persona_store_id_iter != null ||
          Utils.backend_name_iter.next () == true)
        {
          if (Utils.persona_store_id_iter == null)
            {
              Backend backend = Utils.backend_name_iter.get ();
              Utils.persona_store_id_iter =
                  HashTableIter<string, PersonaStore> (backend.persona_stores);
            }

          string id;
          PersonaStore store;
          while (Utils.persona_store_id_iter.next (out id, out store) == true)
            {
              if (id.has_prefix (word))
                return id;
            }

          /* Clean up */
          Utils.persona_store_id_iter = null;
        }

      /* Clean up */
      Utils.backend_name_iter = null;
      return null;
    }
}