summaryrefslogtreecommitdiff
path: root/src/grilo.c
blob: 32f7322fbeed3268d577bd75766daad53324a9f2 (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
/*
 * Copyright (C) 2010, 2011 Igalia S.L.
 *
 * Contact: Iago Toral Quiroga <itoral@igalia.com>
 *
 * 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; 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:grilo
 * @short_description: Metadata library supporting several services
 *
 * Grilo is a metadata retrieval library. Given a search or browse operation,
 * the library will retrieve a set of metadata related to the operation from a
 * set of on-line services.
 *
 * The Grilo library should be initialized with grl_init() before it can be used.
 * You should pass pointers to the main argc and argv variables so that Grilo can
 * process its own command line options.
 *
 * After using it, in order to close cleanly all the resources opened either by
 * the core library or the sources, call grl_deinit().
 */

#include "grilo.h"
#include "grl-metadata-key-priv.h"
#include "grl-operation-priv.h"
#include "grl-registry-priv.h"
#include "grl-log-priv.h"
#include "config.h"

#include <glib/gi18n-lib.h>

static gboolean grl_initialized = FALSE;
static const gchar *plugin_path = NULL;
static const gchar *plugin_list = NULL;

static const gchar *
get_default_plugin_dir (void)
{
#ifdef G_OS_WIN32
  static gchar *plugin_dir = NULL;
  gchar *run_directory;

  if (plugin_dir)
    return plugin_dir;

  run_directory = g_win32_get_package_installation_directory_of_module (NULL);
  plugin_dir = g_build_filename (run_directory,
                                 "lib", GRL_NAME,
                                 NULL);
  g_free (run_directory);
  return plugin_dir;
#else
  return GRL_PLUGINS_DIR;
#endif
}

static gboolean
pre_parse_hook_cb (GOptionContext  *context,
                   GOptionGroup    *group,
                   gpointer         data,
                   GError         **error)
{
  /* Initialize i18n */
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

  /* Initialize operations */
  grl_operation_init ();

  return TRUE;
}

static gboolean
post_parse_hook_cb (GOptionContext  *context,
                    GOptionGroup    *group,
                    gpointer         data,
                    GError         **error)
{
  GrlRegistry *registry;
  gchar **split_element;
  gchar **split_list;

  /* Initialize GModule */
  if (!g_module_supported ()) {
    GRL_ERROR ("GModule not supported in this system");
  }

  /* Setup core log domains */
  _grl_log_init_core_domains ();

  /* Register default metadata keys */
  registry = grl_registry_get_default ();
  grl_metadata_key_setup_system_keys (registry);

  /* Set default plugin directories */
  if (!plugin_path) {
    plugin_path = g_getenv (GRL_PLUGIN_PATH_VAR);
  }

  if (!plugin_path) {
    plugin_path = get_default_plugin_dir ();
  }

  split_list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
  for (split_element = split_list; *split_element; split_element++) {
    grl_registry_add_directory (registry, *split_element);
  }
  g_strfreev (split_list);

  /* Restrict plugins to load */
  if (!plugin_list) {
    plugin_list = g_getenv (GRL_PLUGIN_LIST_VAR);
  }

  if (plugin_list) {
    split_list = g_strsplit (plugin_list, ":", 0);
    grl_registry_restrict_plugins (registry, split_list);
    g_strfreev (split_list);
  }

  grl_initialized = TRUE;

  return TRUE;
}

/**
 * grl_init:
 * @argc: (inout) (allow-none): number of input arguments, length of @argv
 * @argv: (inout) (element-type utf8) (array length=argc) (allow-none) (transfer none): list of arguments
 *
 * Initializes the Grilo library
 *
 * Since: 0.1.6
 */
void
grl_init (gint *argc,
          gchar **argv[])
{
  GOptionContext *ctx;
  GOptionGroup *group;

  if (grl_initialized) {
    GRL_DEBUG ("already initialized grl");
    return;
  }

  /* Check options */
  ctx = g_option_context_new ("- Grilo initialization");
  g_option_context_set_ignore_unknown_options (ctx, TRUE);
  group = grl_init_get_option_group ();
  g_option_context_add_group (ctx, group);
  g_option_context_parse (ctx, argc, argv, NULL);
  g_option_context_free (ctx);
}

/**
 * grl_deinit:
 *
 * Deinitializes the Grilo library.
 *
 * Call this function after finalizing using Grilo, in order to free and clean
 * up all the resources created.
 *
 * Since: 0.2.8
 */
void
grl_deinit (void)
{
  GrlRegistry *registry;

  if (!grl_initialized) {
    GRL_WARNING ("Grilo has not been initialized");
    return;
  }

  registry = grl_registry_get_default ();
  grl_registry_shutdown (registry);
  grl_initialized = FALSE;
}

/**
 * grl_init_get_option_group:
 *
 * Returns a #GOptionGroup with Grilo's argument specifications.
 *
 * This function is useful if you want to integrate Grilo with other
 * libraries that use the GOption commandline parser
 * (see g_option_context_add_group() ).
 *
 * Returns: a pointer to Grilo's option group. Should be dereferenced
 * after use.
 *
 * Since: 0.1.6
 */
GOptionGroup *
grl_init_get_option_group (void)
{
  GOptionGroup *group;
  static const GOptionEntry grl_args[] = {
    { "grl-plugin-path", 0, 0, G_OPTION_ARG_STRING, &plugin_path,
#ifdef G_OS_WIN32
      N_("Semicolon-separated paths containing Grilo plugins"), NULL },
#else
      N_("Colon-separated paths containing Grilo plugins"), NULL },
#endif
    { "grl-plugin-use", 0, 0, G_OPTION_ARG_STRING, &plugin_list,
      N_("Colon-separated list of Grilo plugins to use"), NULL },
    { NULL }
  };

  group = g_option_group_new ("grl",
                              _("Grilo Options"),
                              _("Show Grilo Options"),
                              NULL,
                              NULL);
  g_option_group_add_entries (group, grl_args);
  g_option_group_set_parse_hooks (group, pre_parse_hook_cb, post_parse_hook_cb);

  return group;
}