~ubuntu-branches/ubuntu/wily/grilo/wily-proposed

« back to all changes in this revision

Viewing changes to tools/grilo-launch/grl-launch.c

  • Committer: Package Import Robot
  • Author(s): Alberto Garcia
  • Date: 2014-08-26 20:41:54 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20140826204154-56guk7gff3bwkooj
Tags: 0.2.11-1
* New upstream release.
* debian/libgrilo-0.2-1.symbols: update.
* Install the Grilo test UI and Grilo launch binaries:
  - debian/control: add build dependency on libgtk-3-dev and update
    description.
  - debian/libgrilo-0.2-bin.install: install the binaries.
* debian/rules:
  - Enable parallel builds.
  - Don't override dh_install, it's no longer necessary.
* debian/patches/manpages.patch:
  - Add manpages for all commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Igalia S.L.
 
3
 *
 
4
 * Contact: Iago Toral Quiroga <itoral@igalia.com>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public License
 
8
 * as published by the Free Software Foundation; version 2.1 of
 
9
 * the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
19
 * 02110-1301 USA
 
20
 *
 
21
 */
 
22
 
 
23
#include <grilo.h>
 
24
#include <glib.h>
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#define GRL_LOG_DOMAIN_DEFAULT grl_launch_log_domain
 
29
GRL_LOG_DOMAIN_STATIC(grl_launch_log_domain);
 
30
 
 
31
static GMainLoop *mainloop = NULL;
 
32
static GOptionContext *context = NULL;
 
33
static GrlMediaSerializeType serialize_type;
 
34
static GrlRegistry *registry = NULL;
 
35
static gboolean full;
 
36
static gboolean serialize;
 
37
static gboolean titles;
 
38
static gboolean version;
 
39
static gchar **operation_list = NULL;
 
40
static gchar *conffile = NULL;
 
41
static gchar *flags_parameter;
 
42
static gchar *keys_parameter;
 
43
static gint count = G_MAXINT;
 
44
static gint delay = 1;
 
45
static gint skip = 0;
 
46
 
 
47
static GOptionEntry entries[] = {
 
48
  { "config", 'C', 0,
 
49
    G_OPTION_ARG_STRING, &conffile,
 
50
    "Configuration file to send to sources",
 
51
    NULL },
 
52
  { "count", 'c', 0,
 
53
    G_OPTION_ARG_INT, &count,
 
54
    "Number of elements to return",
 
55
    NULL },
 
56
  { "delay", 'd', 0,
 
57
    G_OPTION_ARG_INT, &delay,
 
58
    "Wait some seconds before performing the operation (default 1 second)",
 
59
    NULL },
 
60
  { "flags", 'f', 0,
 
61
    G_OPTION_ARG_STRING, &flags_parameter,
 
62
    "List of comma-separated flags to use",
 
63
    "full|fast_only|idle_relay" },
 
64
  { "full", 'F', 0,
 
65
    G_OPTION_ARG_NONE, &full,
 
66
    "Full serialize",
 
67
    NULL },
 
68
  { "keys", 'k', 0,
 
69
    G_OPTION_ARG_STRING, &keys_parameter,
 
70
    "List of comma-separated keys to retrieve",
 
71
    NULL },
 
72
  { "serialize", 'S', 0,
 
73
    G_OPTION_ARG_NONE, &serialize,
 
74
    "Serialize",
 
75
    NULL },
 
76
  { "skip", 's', 0,
 
77
    G_OPTION_ARG_INT, &skip,
 
78
    "Number of elements to skip",
 
79
    NULL },
 
80
  { "titles", 'T', 0,
 
81
    G_OPTION_ARG_NONE, &titles,
 
82
    "Print titles",
 
83
    NULL },
 
84
  { "version", 'V', 0,
 
85
    G_OPTION_ARG_NONE, &version,
 
86
    "Print version",
 
87
    NULL },
 
88
  { G_OPTION_REMAINING, '\0', 0,
 
89
    G_OPTION_ARG_STRING_ARRAY, &operation_list,
 
90
    "<operation> <options>",
 
91
    NULL },
 
92
  { NULL }
 
93
};
 
94
 
 
95
static gboolean
 
96
quit (gboolean print_help)
 
97
{
 
98
  gchar *help;
 
99
 
 
100
  if (print_help) {
 
101
    help = g_option_context_get_help (context, TRUE, NULL);
 
102
    g_print ("%s", help);
 
103
    g_free (help);
 
104
  }
 
105
 
 
106
  g_main_loop_quit (mainloop);
 
107
  return FALSE;
 
108
}
 
109
 
 
110
static void
 
111
print_version (void)
 
112
{
 
113
  g_print ("grl-launch-" GRL_MAJORMINOR " version " VERSION "\n");
 
114
  g_print ("Grilo " VERSION "\n");
 
115
  g_print ("https://wiki.gnome.org/Projects/Grilo\n");
 
116
}
 
117
 
 
118
static void
 
119
print_key (GrlMedia *media,
 
120
           GrlKeyID key)
 
121
{
 
122
  GByteArray *binary_blob;
 
123
  const GValue *value;
 
124
  gboolean has_comma;
 
125
  gchar *str_value;
 
126
 
 
127
  value = grl_data_get (GRL_DATA (media), key);
 
128
 
 
129
  if (!value) {
 
130
    return;
 
131
  }
 
132
 
 
133
  if (G_VALUE_HOLDS_STRING (value)) {
 
134
    str_value = (gchar *) g_value_get_string (value);
 
135
    has_comma = g_strstr_len (str_value, -1, ",") != NULL;
 
136
    if (has_comma) {
 
137
      g_print ("\"%s\"", str_value);
 
138
    } else {
 
139
      g_print ("%s", str_value);
 
140
    }
 
141
  } else if (G_VALUE_HOLDS_INT (value)) {
 
142
    g_print ("%d", g_value_get_int (value));
 
143
  } else if (G_VALUE_HOLDS_FLOAT (value)) {
 
144
    g_print ("%f", g_value_get_float (value));
 
145
  } else if (G_VALUE_HOLDS_BOOLEAN (value)) {
 
146
    g_print ("%s", g_value_get_boolean (value)? "true": "false");
 
147
  } else if (G_VALUE_TYPE (value) == G_TYPE_BYTE_ARRAY) {
 
148
    binary_blob = g_value_get_boxed (value);
 
149
    str_value = g_base64_encode (binary_blob->data, binary_blob->len);
 
150
    g_print ("%s", str_value);
 
151
    g_free (str_value);
 
152
  } else if (G_VALUE_TYPE (value) == G_TYPE_DATE_TIME) {
 
153
    str_value = g_date_time_format (g_value_get_boxed (value), "%FT%T");
 
154
    g_print ("%s", str_value);
 
155
    g_free (str_value);
 
156
  }
 
157
}
 
158
 
 
159
static void
 
160
print_titles (GList *keys)
 
161
{
 
162
  gboolean print_newline = FALSE;
 
163
 
 
164
  if (!titles) {
 
165
    return;
 
166
  }
 
167
 
 
168
  if (serialize || keys) {
 
169
    print_newline = TRUE;
 
170
  }
 
171
 
 
172
  if (serialize) {
 
173
    g_print ("media");
 
174
    if (keys) {
 
175
      g_print (",");
 
176
    }
 
177
  }
 
178
 
 
179
  while (keys) {
 
180
    g_print ("%s", grl_metadata_key_get_name (GRLPOINTER_TO_KEYID (keys->data)));
 
181
    keys = g_list_next (keys);
 
182
    if (keys) {
 
183
      g_print (",");
 
184
    }
 
185
  }
 
186
 
 
187
  if (print_newline) {
 
188
    g_print ("\n");
 
189
  }
 
190
}
 
191
 
 
192
static void
 
193
print_result_cb (GrlSource *source,
 
194
                 guint operation_id,
 
195
                 GrlMedia *media,
 
196
                 guint remaining,
 
197
                 gpointer user_data,
 
198
                 const GError *error)
 
199
{
 
200
  GList *k;
 
201
  GList *keys = (GList *) user_data;
 
202
  gboolean print_newline = FALSE;
 
203
  gchar *media_serial;
 
204
  static guint total_results = 0;
 
205
 
 
206
  if (error) {
 
207
    g_print ("Error: %s\n", error->message);
 
208
  }
 
209
 
 
210
  if (media) {
 
211
    if (serialize || keys) {
 
212
      print_newline = TRUE;
 
213
    }
 
214
 
 
215
    total_results++;
 
216
    if (serialize) {
 
217
      media_serial = grl_media_serialize_extended (media, serialize_type);
 
218
      g_print ("%s", media_serial);
 
219
      g_free (media_serial);
 
220
      if (keys) {
 
221
        g_print (",");
 
222
      }
 
223
    }
 
224
    k = keys;
 
225
    while (k) {
 
226
      print_key (media, GRLPOINTER_TO_KEYID (k->data));
 
227
      k = g_list_next (k);
 
228
      if (k) {
 
229
        g_print (",");
 
230
      }
 
231
    }
 
232
 
 
233
    g_object_unref (media);
 
234
 
 
235
    if (print_newline) {
 
236
      g_print ("\n");
 
237
    }
 
238
  }
 
239
 
 
240
  if (remaining == 0) {
 
241
    switch (total_results) {
 
242
    case 0:
 
243
      g_print ("No results\n");
 
244
      break;
 
245
    case 1:
 
246
      g_print ("1 result\n");
 
247
      break;
 
248
    default:
 
249
      g_print ("%u results\n", total_results);
 
250
    }
 
251
    g_list_free (keys);
 
252
    quit (FALSE);
 
253
  }
 
254
}
 
255
 
 
256
static void
 
257
print_single_result_cb (GrlSource *source,
 
258
                        guint operation_id,
 
259
                        GrlMedia *media,
 
260
                        gpointer user_data,
 
261
                        const GError *error)
 
262
{
 
263
  print_result_cb (source, operation_id, media, 0, user_data, error);
 
264
}
 
265
 
 
266
static GList *
 
267
get_keys (void)
 
268
{
 
269
  GList *keys = NULL;
 
270
  GrlKeyID key;
 
271
  GrlRegistry *registry;
 
272
  gchar **keys_array;
 
273
  gint i;
 
274
 
 
275
  if (!keys_parameter) {
 
276
    return NULL;
 
277
  }
 
278
 
 
279
  registry = grl_registry_get_default ();
 
280
  keys_array = g_strsplit (keys_parameter, ",", -1);
 
281
 
 
282
  for (i = 0; keys_array[i]; i++) {
 
283
    if (g_strcmp0 (keys_array[i], "*") == 0) {
 
284
      g_list_free (keys);
 
285
      g_strfreev (keys_array);
 
286
      return grl_registry_get_metadata_keys (registry);
 
287
    }
 
288
 
 
289
    key = grl_registry_lookup_metadata_key (registry, keys_array[i]);
 
290
    if (key == GRL_METADATA_KEY_INVALID) {
 
291
      g_print ("Unknown %s key\n", keys_array[i]);
 
292
    } else {
 
293
      keys = g_list_append (keys, GRLKEYID_TO_POINTER (key));
 
294
    }
 
295
  }
 
296
 
 
297
  g_strfreev (keys_array);
 
298
 
 
299
  return keys;
 
300
}
 
301
 
 
302
static GrlResolutionFlags
 
303
get_flags (void)
 
304
{
 
305
  GrlResolutionFlags flags = GRL_RESOLVE_NORMAL;
 
306
  gchar **flags_array;
 
307
  gint i;
 
308
 
 
309
  if (!flags_parameter) {
 
310
    return flags;
 
311
  }
 
312
 
 
313
  flags_array = g_strsplit (flags_parameter, ",", -1);
 
314
 
 
315
  for (i = 0; flags_array[i]; i++) {
 
316
    if (g_strcmp0 (flags_array[i], "full") == 0) {
 
317
      flags |= GRL_RESOLVE_FULL;
 
318
    } else if (g_strcmp0 (flags_array[i], "idle_relay") == 0) {
 
319
      flags |= GRL_RESOLVE_IDLE_RELAY;
 
320
    } else if (g_strcmp0 (flags_array[i], "fast_only") == 0) {
 
321
      flags |= GRL_RESOLVE_FAST_ONLY;
 
322
    } else {
 
323
      g_print ("Unknown %s flag\n", flags_array[i]);
 
324
    }
 
325
  }
 
326
 
 
327
  g_strfreev (flags_array);
 
328
 
 
329
  return flags;
 
330
}
 
331
 
 
332
static void
 
333
get_source_and_media (const gchar *str,
 
334
                      GrlSource **source,
 
335
                      GrlMedia **media)
 
336
{
 
337
  GrlRegistry *registry = grl_registry_get_default();
 
338
 
 
339
  /* Check if str is a source */
 
340
  *source = grl_registry_lookup_source (registry, str);
 
341
  if (*source) {
 
342
    *media = NULL;
 
343
  } else {
 
344
    /* Check then if this is a media */
 
345
    *media = grl_media_unserialize (str);
 
346
    if (*media) {
 
347
      *source = grl_registry_lookup_source (registry, grl_media_get_source (*media));
 
348
    }
 
349
  }
 
350
}
 
351
 
 
352
static void
 
353
content_changed_cb (GrlSource *source,
 
354
                    GPtrArray *changed_medias,
 
355
                    GrlSourceChangeType change_type,
 
356
                    gboolean location_unknown,
 
357
                    gpointer data)
 
358
{
 
359
  GrlMedia *media;
 
360
  gint i;
 
361
 
 
362
  for (i = 0; i < changed_medias->len; i++) {
 
363
    media = (GrlMedia *) g_ptr_array_index (changed_medias, i);
 
364
    switch (change_type) {
 
365
    case GRL_CONTENT_CHANGED:
 
366
      g_print ("changed,");
 
367
      break;
 
368
    case GRL_CONTENT_ADDED:
 
369
      g_print ("added,");
 
370
      break;
 
371
    case GRL_CONTENT_REMOVED:
 
372
      g_print ("removed,");
 
373
      break;
 
374
    }
 
375
 
 
376
    if (location_unknown) {
 
377
      g_print ("true");
 
378
    } else {
 
379
      g_print ("false");
 
380
    }
 
381
 
 
382
    if (serialize || data) {
 
383
      g_print(",");
 
384
      print_result_cb (source, 0, g_object_ref (media), -1, data, NULL);
 
385
    } else {
 
386
      g_print("\n");
 
387
    }
 
388
  }
 
389
}
 
390
 
 
391
static gboolean
 
392
run_search (gchar **search_params)
 
393
{
 
394
  GList *keys;
 
395
  GrlOperationOptions *options;
 
396
  GrlRegistry *registry;
 
397
  GrlSource *source;
 
398
  gchar *term;
 
399
 
 
400
  if (g_strv_length (search_params) != 2) {
 
401
    return quit (TRUE);
 
402
  }
 
403
 
 
404
  /* Empty string means "search all" */
 
405
  if (search_params[0][0] == '\0') {
 
406
    term = NULL;
 
407
  } else {
 
408
    term = search_params[0];
 
409
  }
 
410
 
 
411
  registry = grl_registry_get_default ();
 
412
  source = grl_registry_lookup_source (registry, search_params[1]);
 
413
 
 
414
  if (!source) {
 
415
    g_print ("%s is not a valid source\n", search_params[1]);
 
416
    return quit (FALSE);
 
417
  }
 
418
 
 
419
  if (!(grl_source_supported_operations (source) & GRL_OP_SEARCH)) {
 
420
    g_print ("%s do not support search\n", search_params[1]);
 
421
    return quit (FALSE);
 
422
  }
 
423
 
 
424
  keys = get_keys ();
 
425
 
 
426
  options = grl_operation_options_new (NULL);
 
427
  grl_operation_options_set_flags (options, get_flags ());
 
428
  grl_operation_options_set_count (options, count);
 
429
  grl_operation_options_set_skip (options, skip);
 
430
 
 
431
  print_titles (keys);
 
432
 
 
433
  grl_source_search (source, term, keys, options, print_result_cb, keys);
 
434
 
 
435
  g_object_unref (options);
 
436
 
 
437
  return FALSE;
 
438
}
 
439
 
 
440
static gboolean
 
441
run_browse (gchar **browse_params)
 
442
{
 
443
  GList *keys;
 
444
  GrlMedia *media;
 
445
  GrlOperationOptions *options;
 
446
  GrlSource *source;
 
447
 
 
448
  if (g_strv_length (browse_params) != 1) {
 
449
    return quit (TRUE);
 
450
  }
 
451
 
 
452
  get_source_and_media (browse_params[0], &source, &media);
 
453
  if (media && !GRL_IS_MEDIA_BOX (media)) {
 
454
    g_print ("%s is not a media box\n", browse_params[0]);
 
455
    return quit (FALSE);
 
456
  }
 
457
 
 
458
  if (!source) {
 
459
    g_print ("%s is not a valid source\n", browse_params[0]);
 
460
    return quit (FALSE);
 
461
  }
 
462
 
 
463
  if (!(grl_source_supported_operations (source)  & GRL_OP_BROWSE)) {
 
464
    g_print ("%s do not support browse\n", grl_source_get_id (source));
 
465
    return quit (FALSE);
 
466
  }
 
467
 
 
468
  keys = get_keys();
 
469
 
 
470
  options = grl_operation_options_new (NULL);
 
471
  grl_operation_options_set_flags (options, get_flags ());
 
472
  grl_operation_options_set_count (options, count);
 
473
  grl_operation_options_set_skip (options, skip);
 
474
 
 
475
  print_titles (keys);
 
476
 
 
477
  grl_source_browse (source, media, keys, options, print_result_cb, keys);
 
478
 
 
479
  g_object_unref (options);
 
480
 
 
481
  return FALSE;
 
482
}
 
483
 
 
484
static gboolean
 
485
run_resolve (gchar **resolve_params)
 
486
{
 
487
  GList *print_keys;
 
488
  GList *search_keys;
 
489
  GrlMedia *media;
 
490
  GrlOperationOptions *options;
 
491
  GrlRegistry *registry;
 
492
  GrlSource *source;
 
493
 
 
494
  if (g_strv_length (resolve_params) > 2) {
 
495
    return quit (TRUE);
 
496
  }
 
497
 
 
498
  get_source_and_media (resolve_params[0], &source, &media);
 
499
 
 
500
  if (resolve_params[1]) {
 
501
    /* Ask the other source */
 
502
    registry = grl_registry_get_default ();
 
503
    source = grl_registry_lookup_source (registry, resolve_params[1]);
 
504
  }
 
505
 
 
506
  if (!source) {
 
507
    g_print ("%s is not a valid source\n", resolve_params[1]? resolve_params[1]: resolve_params[0]);
 
508
    return quit (FALSE);
 
509
  }
 
510
 
 
511
 
 
512
  if (!(grl_source_supported_operations (source)  & GRL_OP_RESOLVE)) {
 
513
    g_print ("%s do not support resolve\n", grl_source_get_id (source));
 
514
    return quit (FALSE);
 
515
  }
 
516
 
 
517
  print_keys = get_keys();
 
518
 
 
519
  if (print_keys) {
 
520
    search_keys = print_keys;
 
521
  } else {
 
522
    /* Resolve requires some key to resolve; let's use "id" */
 
523
    search_keys = g_list_append (NULL, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_ID));
 
524
  }
 
525
 
 
526
  options = grl_operation_options_new (NULL);
 
527
  grl_operation_options_set_flags (options, get_flags ());
 
528
 
 
529
  print_titles (print_keys);
 
530
 
 
531
  grl_source_resolve (source, media, search_keys, options, print_single_result_cb, print_keys);
 
532
 
 
533
  g_object_unref (options);
 
534
 
 
535
  return FALSE;
 
536
}
 
537
 
 
538
static gboolean
 
539
run_may_resolve (gchar **may_resolve_params)
 
540
{
 
541
  GList *required_keys = NULL;
 
542
  GrlKeyID resolve_key;
 
543
  GrlMedia *media;
 
544
  GrlRegistry *registry;
 
545
  GrlSource *source;
 
546
  gboolean may;
 
547
 
 
548
  if (g_strv_length (may_resolve_params) > 3) {
 
549
    return quit (TRUE);
 
550
  }
 
551
 
 
552
  registry = grl_registry_get_default ();
 
553
  resolve_key = grl_registry_lookup_metadata_key (registry, may_resolve_params[0]);
 
554
  if (resolve_key == GRL_METADATA_KEY_INVALID) {
 
555
    g_print ("Unknown %s key\n", may_resolve_params[0]);
 
556
    return quit (FALSE);
 
557
  }
 
558
 
 
559
  get_source_and_media (may_resolve_params[1], &source, &media);
 
560
 
 
561
  if (may_resolve_params[2]) {
 
562
    /* Ask the other source */
 
563
    registry = grl_registry_get_default ();
 
564
    source = grl_registry_lookup_source (registry, may_resolve_params[2]);
 
565
  }
 
566
 
 
567
  if (!source) {
 
568
    g_print ("%s is not a valid source\n", may_resolve_params[2]? may_resolve_params[2]: may_resolve_params[1]);
 
569
    return quit (FALSE);
 
570
  }
 
571
 
 
572
 
 
573
  if (!(grl_source_supported_operations (source)  & GRL_OP_RESOLVE)) {
 
574
    g_print ("%s do not support resolve\n", grl_source_get_id (source));
 
575
    return quit (FALSE);
 
576
  }
 
577
 
 
578
  may = grl_source_may_resolve (source, media, resolve_key, &required_keys);
 
579
 
 
580
  if (may) {
 
581
    g_print ("%s can resolve %s key\n", grl_source_get_id (source), may_resolve_params[0]);
 
582
  } else {
 
583
    g_print ("%s cannot resolve %s key", grl_source_get_id (source), may_resolve_params[0]);
 
584
    if (required_keys) {
 
585
      g_print (". It requires ");
 
586
      while (required_keys) {
 
587
        g_print ("%s", grl_metadata_key_get_name (GRLPOINTER_TO_KEYID (required_keys->data)));
 
588
        required_keys = g_list_next (required_keys);
 
589
        if (required_keys) {
 
590
          g_print (",");
 
591
        }
 
592
      }
 
593
    }
 
594
    g_print ("\n");
 
595
  }
 
596
 
 
597
  return quit (FALSE);
 
598
}
 
599
 
 
600
 
 
601
static gboolean
 
602
run_query (gchar **query_params)
 
603
{
 
604
  GList *keys;
 
605
  GrlOperationOptions *options;
 
606
  GrlRegistry *registry;
 
607
  GrlSource *source;
 
608
 
 
609
  if (g_strv_length (query_params) != 2) {
 
610
    return quit (TRUE);
 
611
  }
 
612
 
 
613
  registry = grl_registry_get_default ();
 
614
  source = grl_registry_lookup_source (registry, query_params[1]);
 
615
 
 
616
  if (!source) {
 
617
    g_print ("%s is not a valid source\n", query_params[1]);
 
618
    return quit (FALSE);
 
619
  }
 
620
 
 
621
  if (!(grl_source_supported_operations (source) & GRL_OP_QUERY)) {
 
622
    g_print ("%s do not support query\n", query_params[1]);
 
623
    return quit (FALSE);
 
624
  }
 
625
 
 
626
  keys = get_keys ();
 
627
 
 
628
  options = grl_operation_options_new (NULL);
 
629
  grl_operation_options_set_flags (options, get_flags ());
 
630
  grl_operation_options_set_count (options, count);
 
631
  grl_operation_options_set_skip (options, skip);
 
632
 
 
633
  print_titles (keys);
 
634
 
 
635
  grl_source_query (source, query_params[0], keys, options, print_result_cb, keys);
 
636
 
 
637
  g_object_unref (options);
 
638
 
 
639
  return FALSE;
 
640
}
 
641
 
 
642
static gboolean
 
643
run_monitor (gchar **monitor_params)
 
644
{
 
645
  GError *error = NULL;
 
646
  GList *keys;
 
647
  GrlRegistry *registry;
 
648
  GrlSource *source;
 
649
 
 
650
  if (g_strv_length (monitor_params) != 1) {
 
651
    return quit (TRUE);
 
652
  }
 
653
 
 
654
  registry = grl_registry_get_default ();
 
655
  source = grl_registry_lookup_source (registry, monitor_params[0]);
 
656
 
 
657
  if (!source) {
 
658
    g_print ("%s is not a valid source\n", monitor_params[0]);
 
659
    return quit (FALSE);
 
660
  }
 
661
 
 
662
  if (!(grl_source_supported_operations (source) & GRL_OP_NOTIFY_CHANGE)) {
 
663
    g_print ("%s do not support changes monitoring\n", monitor_params[0]);
 
664
    return quit (FALSE);
 
665
  }
 
666
 
 
667
  if (!grl_source_notify_change_start (source, &error)) {
 
668
    g_print ("Cannot monitor on %s: %s\n", monitor_params[0], error->message);
 
669
    g_error_free (error);
 
670
    return quit (FALSE);
 
671
  }
 
672
 
 
673
  keys = get_keys ();
 
674
 
 
675
  if (titles) {
 
676
    g_print ("change_type,location_unknown");
 
677
    if (serialize || keys) {
 
678
      g_print(",");
 
679
      print_titles (keys);
 
680
    } else {
 
681
      g_print ("\n");
 
682
    }
 
683
  }
 
684
 
 
685
  g_signal_connect (source,
 
686
                    "content-changed",
 
687
                    G_CALLBACK (content_changed_cb),
 
688
                    keys);
 
689
 
 
690
  return FALSE;
 
691
}
 
692
 
 
693
static gboolean
 
694
run_test_media_from_uri (gchar **test_params)
 
695
{
 
696
  GList *p;
 
697
  GList *sources;
 
698
  GrlRegistry *registry;
 
699
  GrlSource *source;
 
700
  gboolean can_do;
 
701
 
 
702
  if (g_strv_length (test_params) > 2) {
 
703
    return quit (TRUE);
 
704
  }
 
705
 
 
706
  if (!test_params[0]) {
 
707
    return quit (TRUE);
 
708
  }
 
709
 
 
710
  registry = grl_registry_get_default ();
 
711
  if (test_params[1]) {
 
712
    source = grl_registry_lookup_source (registry, test_params[1]);
 
713
    if (!source) {
 
714
      g_print ("%s is not a valid source\n", test_params[1]);
 
715
      return quit (FALSE);
 
716
    }
 
717
    if (!(grl_source_supported_operations (source) & GRL_OP_MEDIA_FROM_URI)) {
 
718
      g_print ("%s does not support test_media_from_uri operation\n", test_params[1]);
 
719
      return quit (FALSE);
 
720
    }
 
721
    can_do = grl_source_test_media_from_uri (source, test_params[0]);
 
722
    g_print ("%s\t%s\n", test_params[1], can_do? "yes": "no");
 
723
  } else {
 
724
    sources = grl_registry_get_sources_by_operations (registry, GRL_OP_MEDIA_FROM_URI, TRUE);
 
725
    for (p = sources; p; p = g_list_next (p)) {
 
726
      source = GRL_SOURCE (p->data);
 
727
      can_do = grl_source_test_media_from_uri (source, test_params[0]);
 
728
      if (can_do) {
 
729
        g_print ("%s\t%s\n", grl_source_get_id (source), "yes");
 
730
      }
 
731
    }
 
732
    g_list_free (sources);
 
733
  }
 
734
 
 
735
  return quit (FALSE);
 
736
}
 
737
 
 
738
static gboolean
 
739
run_media_from_uri (gchar **uri_params)
 
740
{
 
741
  GList *print_keys;
 
742
  GList *use_keys;
 
743
  GrlOperationOptions *options;
 
744
  GrlRegistry *registry;
 
745
  GrlSource *source;
 
746
 
 
747
  if (g_strv_length (uri_params) != 2) {
 
748
    return quit (TRUE);
 
749
  }
 
750
 
 
751
  registry = grl_registry_get_default ();
 
752
  source = grl_registry_lookup_source (registry, uri_params[1]);
 
753
 
 
754
  if (!source) {
 
755
    g_print ("%s is not a valid source\n", uri_params[1]);
 
756
    return quit (FALSE);
 
757
  }
 
758
 
 
759
  if (!(grl_source_supported_operations (source) & GRL_OP_MEDIA_FROM_URI)) {
 
760
    g_print ("%s does not support media_from_uri\n", uri_params[1]);
 
761
    return quit (FALSE);
 
762
  }
 
763
 
 
764
  print_keys = get_keys ();
 
765
 
 
766
  if (print_keys) {
 
767
    use_keys = print_keys;
 
768
  } else {
 
769
    /* Media_From_Uri requires some key to use; let's use "id" */
 
770
    use_keys = g_list_append (NULL, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_ID));
 
771
  }
 
772
 
 
773
  options = grl_operation_options_new (NULL);
 
774
  grl_operation_options_set_flags (options, get_flags ());
 
775
 
 
776
  print_titles (print_keys);
 
777
 
 
778
  grl_source_get_media_from_uri (source, uri_params[0], use_keys, options, print_single_result_cb, print_keys);
 
779
 
 
780
  g_object_unref (options);
 
781
 
 
782
  return FALSE;
 
783
}
 
784
 
 
785
static gboolean
 
786
run (gpointer data)
 
787
{
 
788
  if (!operation_list) {
 
789
    return quit (TRUE);
 
790
  }
 
791
 
 
792
  if (g_strcmp0 (operation_list[0], "search") == 0) {
 
793
    return run_search (++operation_list);
 
794
  } else if (g_strcmp0 (operation_list[0], "browse") == 0) {
 
795
    return run_browse (++operation_list);
 
796
  } else if (g_strcmp0 (operation_list[0], "resolve") == 0) {
 
797
    return run_resolve (++operation_list);
 
798
  } else if (g_strcmp0 (operation_list[0], "may_resolve") == 0) {
 
799
    return run_may_resolve (++operation_list);
 
800
  } else if (g_strcmp0 (operation_list[0], "query") == 0) {
 
801
    return run_query (++operation_list);
 
802
  } else if (g_strcmp0 (operation_list[0], "monitor") == 0) {
 
803
    return run_monitor (++operation_list);
 
804
  } else if (g_strcmp0 (operation_list[0], "test_media_from_uri") == 0){
 
805
    return run_test_media_from_uri (++operation_list);
 
806
  } else if (g_strcmp0 (operation_list[0], "media_from_uri") == 0){
 
807
    return run_media_from_uri (++operation_list);
 
808
  }
 
809
 
 
810
  return quit (TRUE);
 
811
}
 
812
 
 
813
int
 
814
main (int argc, char *argv[])
 
815
{
 
816
  GError *error = NULL;
 
817
 
 
818
  context = g_option_context_new ("OPERATION PARAMETERS...");
 
819
  g_option_context_add_main_entries (context, entries, NULL);
 
820
  g_option_context_add_group (context, grl_init_get_option_group ());
 
821
  g_option_context_set_summary (context,
 
822
                                "\tbrowse <source>|<media box>\n"
 
823
                                "\tmay_resolve <key> <source>|<media box> [<source>]\n"
 
824
                                "\tquery <expression> <source>\n"
 
825
                                "\tresolve <source>|<media> [<source>]\n"
 
826
                                "\tsearch <term> <source>\n"
 
827
                                "\tmonitor <source>\n"
 
828
                                "\ttest_media_from_uri <uri> [<source>]\n"
 
829
                                "\tmedia_from_uri <uri> <source>");
 
830
 
 
831
  g_option_context_parse (context, &argc, &argv, &error);
 
832
 
 
833
  if (error) {
 
834
    g_printerr ("Invalid arguments, %s\n", error->message);
 
835
    g_clear_error (&error);
 
836
    return -1;
 
837
  }
 
838
 
 
839
  if (version) {
 
840
    print_version ();
 
841
    return 0;
 
842
  }
 
843
 
 
844
  serialize_type = full? GRL_MEDIA_SERIALIZE_FULL: GRL_MEDIA_SERIALIZE_BASIC;
 
845
 
 
846
  grl_init (&argc, &argv);
 
847
 
 
848
  GRL_LOG_DOMAIN_INIT (grl_launch_log_domain, "grl-launch");
 
849
 
 
850
  registry = grl_registry_get_default ();
 
851
  if (conffile) {
 
852
    grl_registry_add_config_from_file (registry, conffile, &error);
 
853
    if (error) {
 
854
      GRL_WARNING ("Unable to load configuration: %s", error->message);
 
855
      g_error_free (error);
 
856
    }
 
857
  }
 
858
 
 
859
  mainloop = g_main_loop_new (NULL, FALSE);
 
860
 
 
861
  grl_registry_load_all_plugins (registry, NULL);
 
862
 
 
863
  g_timeout_add_seconds ((guint) delay, run, NULL);
 
864
 
 
865
  g_main_loop_run (mainloop);
 
866
 
 
867
  g_option_context_free (context);
 
868
  grl_deinit ();
 
869
 
 
870
  return 0;
 
871
}