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
/*
 * Desktop Agnostic Library: Configuration Schema.
 *
 * Copyright (C) 2008, 2009 Mark Lee <libdesktop-agnostic@lazymalevolence.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; 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Author : Mark Lee <libdesktop-agnostic@lazymalevolence.com>
 */

using DesktopAgnostic.VFS;

namespace DesktopAgnostic.Config
{
  /**
   * Configuration schema error types.
   */
  public errordomain SchemaError
  {
    PARSE,
    INVALID_METADATA_OPTION,
    INVALID_METADATA_TYPE,
    INVALID_TYPE,
    INVALID_LIST_TYPE,
    TYPE_NAME_EXISTS,
    TYPE_GTYPE_EXISTS
  }
  /**
   * A representation of a configuration schema, comprised of one or more
   * configuration options.
   */
  public class Schema : Object
  {
    // static code
    private static HashTable<Type,SchemaType> type_registry =
      new HashTable<Type,SchemaType> ((HashFunc)gtype_hash,
                                      (EqualFunc)gtype_equal);
    private static HashTable<string,SchemaType> name_registry =
      new HashTable<string,SchemaType> (str_hash, str_equal);
    private static HashTable<string,Value?> common_metadata_keys =
      new HashTable<string,Value?> (str_hash, str_equal);
    static construct
    {
      // Loads the type modules via the module search paths + cwd.
      List<string> type_modules;
      string[] paths;
      SList<string> search_paths;

      type_modules = new List<string> ();
      // pre-load the ModuleLoader static constructor
      ModuleLoader.get_default ();
      // pre-load the default config backend module.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        // Do nothing.
      }

      // search for the config type modules
      paths = ModuleLoader.get_search_paths ();
      search_paths = new SList<string> ();
      foreach (unowned string path in paths)
      {
        if (path != null)
        {
          search_paths.append (path);
        }
      }
      search_paths.append (Environment.get_current_dir ());

      foreach (unowned string path in search_paths)
      {
        if (!FileUtils.test (path, FileTest.IS_DIR))
        {
          continue;
        }
        string module_glob = Path.build_filename (path, "libda-cfg-type-*");
        try
        {
          Glob found_modules;
          unowned string[] modules_paths;

          found_modules = Glob.execute (module_glob);
          modules_paths = found_modules.get_paths ();
          foreach (unowned string module in modules_paths)
          {
            if (type_modules.find (module) == null)
            {
              Type type;

              unowned ModuleLoader loader = ModuleLoader.get_default ();
              type = loader.load_from_path (Path.get_basename (module),
                                            module);
              if (type != Type.INVALID)
              {
                try
                {
                  Object obj = Object.new (type);

                  register_type ((SchemaType)((owned)obj));
                  type_modules.append (module);
                }
                catch (SchemaError err)
                {
                  warning ("Schema error: %s", err.message);
                }
              }
              else
              {
                warning ("Could not load the config type module: %s", module);
              }
            }
          }
        }
        catch (GlobError err)
        {
          if (!(err is GlobError.NOMATCH))
          {
            warning ("Glob-related error: %s", err.message);
          }
        }
      }
      // initialize the common metadata keys hashtable
      Value val = Value (typeof (bool));
      val.set_boolean (true);
      common_metadata_keys.insert ("single_instance", val);
      // force the config module to load before creating the schema.
      try
      {
        Config.get_type ();
      }
      catch (GLib.Error err)
      {
        critical ("Config error: %s", err.message);
      }
    }
    // properties
    private string _filename;
    public string filename
    {
      /**
       * Sets the schema's file name and app name (assuming the file name
       * is valid).
       */
      construct
      {
        string? basename = null;

        if (!value.has_suffix (".schema-ini"))
        {
          critical ("Schema files MUST have the extension '.schema-ini'.");
          return;
        }
        if (!FileUtils.test (value, FileTest.EXISTS))
        {
          critical ("The file '%s' could not be found.", value);
          return;
        }
        this._filename = value;
        basename = Path.get_basename (value);
        this.app_name = basename.substring (0, basename.length - 11);
      }
    }
    [Description (nick = "Application name", blurb = "The name of the application associated with the schema")]
    public string app_name { get; private set; }
    private Datalist<SchemaOption> options;
    private HashTable<string,List<string>> keys;
    private List<string> valid_metadata_keys;
    private Datalist<Value?> metadata_options;
    /**
     * Creates a new Schema object.
     * @param filename the name of the schema file to parse
     */
    public Schema (string filename) throws GLib.Error
    {
      this.filename = filename;
      unowned HashTable<string,Value?> backend_metadata_keys;
      this.options = Datalist<SchemaOption> ();
      this.keys = new HashTable<string,List<string>> (str_hash, str_equal);
      // populate the common metadata keys table
      this.valid_metadata_keys = new List<string> ();
      this.metadata_options = Datalist<Value?> ();
      foreach (unowned string key in common_metadata_keys.get_keys ())
      {
        this.valid_metadata_keys.append (key);
        this.metadata_options.set_data (key, common_metadata_keys.lookup (key));
      }
      // populate the backend-specific metadata keys table
      backend_metadata_keys = Backend.get_backend_metadata_keys ();
      foreach (unowned string key in backend_metadata_keys.get_keys ())
      {
        this.valid_metadata_keys.append (key);
        this.metadata_options.set_data (key,
                                        backend_metadata_keys.lookup (key));
      }
      this.parse ();
    }
    /**
     * Parses the schema file for configuration options and metadata.
     */
    private void
    parse () throws GLib.Error
    {
      if (this._filename == null)
      {
        throw new SchemaError.PARSE ("A (valid) schema file was not given.");
      }
      else
      {
        KeyFile data = new KeyFile ();
        data.load_from_file (this._filename, KeyFileFlags.KEEP_TRANSLATIONS);
        foreach (unowned string group in data.get_groups ())
        {
          if (group.contains ("/"))
          {
            // split option group & key, add to groups/keys lists
            unowned string last_slash = group.rchr (group.length, '/');
            long offset = group.pointer_to_offset (last_slash);
            string option_group = group.substring (0, offset);
            unowned string option_key = group.offset (offset + 1);
            unowned List<string>? list = this.keys.lookup (option_group);
            if (list == null)
            {
              List<string> key_list = new List<string> ();
              key_list.append (option_key);
              this.keys.insert (option_group, (owned)key_list);
            }
            else if (!this.exists (option_group, option_key))
            {
              list.append (option_key);
            }
            else
            {
              throw new SchemaError.PARSE ("Duplicate key found in '%s': %s",
                                           option_group, option_key);
            }
            // create a new schema option and add to options list
            SchemaOption option = new SchemaOption (ref data, option_group,
                                                    option_key);
            this.options.set_data (group, option);
          }
          else if (group == DesktopAgnostic.Config.GROUP_DEFAULT)
          {
            // parse the schema metadata
            foreach (unowned string key in data.get_keys (group))
            {
              if (this.valid_metadata_keys.find_custom (key, (CompareFunc)strcmp) == null)
              {
                throw new SchemaError.INVALID_METADATA_OPTION ("The option '%s' is not a registered metadata option.", key);
              }
              else
              {
                Value cur_val, new_val;
                Type cur_val_type;

                cur_val = this.metadata_options.get_data (key);
                cur_val_type = cur_val.type ();
                new_val = Value (cur_val_type);
                if (cur_val_type == typeof (bool))
                {
                  new_val.set_boolean (data.get_boolean (group, key));
                }
                else if (cur_val_type == typeof (int))
                {
                  new_val.set_int (data.get_integer (group, key));
                }
                else if (cur_val_type == typeof (float))
                {
                  new_val.set_float ((float)data.get_double (group, key));
                }
                else if (cur_val_type == typeof (string))
                {
                  new_val.set_string (data.get_string (group, key));
                }
                else
                {
                  throw new SchemaError.INVALID_METADATA_TYPE ("The metadata option type can only be a simple type.");
                }
                this.metadata_options.set_data (key, new_val);
              }
            }
          }
          else
          {
            throw new SchemaError.PARSE ("Invalid section in schema ('%s'), there is no group name: %s",
                                         this._filename, group);
          }
        }
      }
    }
    /**
     * Retrieves the configuration groups in the schema.
     * @return a list of zero or more groups
     */
    public List<unowned string>?
    get_groups ()
    {
      return this.keys.get_keys ();
    }
    /**
     * Retrieves the configuration keys for a specified group in the schema.
     * @param group the group name to search for keys associated with it
     * @return a list of zero or more keys
     */
    public unowned List<unowned string>?
    get_keys (string group)
    {
      return this.keys.lookup (group);
    }
    /**
     * Determines if a specified group/key exists in the schema.
     * @param group the group that the key is associated with
     * @param key the configuration key to determine if it exists
     * @return whether the group/key exists
     */
    public bool
    exists (string group, string key)
    {
      unowned List<unowned string> group_keys = this.keys.lookup (group);
      return group_keys != null &&
             group_keys.find_custom (key, (CompareFunc)strcmp) != null;
    }
    /**
     * Retrieves the metadata associated with a specific group/key.
     * @param group the group that the key is associated with
     * @param key the configuration key to retrieve metadata from
     * @return an object which contains the option metadata
     */
    public unowned SchemaOption
    get_option (string group, string key)
    {
      string full_key = group + "/" + key;
      return this.options.get_data (full_key);
    }
    /**
     * Retrieves the value of the specified metadata option.
     * @throws SchemaError if the option named specified is not registered
     */
    public Value?
    get_metadata_option (string name) throws SchemaError
    {
      if (this.valid_metadata_keys.find_custom (name, (CompareFunc)strcmp) == null)
      {
        throw new SchemaError.INVALID_METADATA_OPTION ("The option '%s' is not a registered metadata option.", name);
      }
      else
      {
        return this.metadata_options.get_data (name);
      }
    }
    /**
     * Registers a configuration schema type with the class. This is usually
     * not called manually - the class loads all of the configuration schema
     * type modules that it can find when the class is first instantiated.
     */
    public static void
    register_type (SchemaType st) throws SchemaError
    {
      if (type_registry.lookup (st.schema_type) != null)
      {
        throw new SchemaError.TYPE_GTYPE_EXISTS ("The GType associated with the SchemaType is already registered.");
      }
      else if (name_registry.lookup (st.name) != null)
      {
        throw new SchemaError.TYPE_NAME_EXISTS ("The name associated with the SchemaType is already registered.");
      }
      else
      {
        type_registry.insert (st.schema_type, st);
        name_registry.insert (st.name, st);
      }
    }
    /**
     * Looks for a registered SchemaType by its GType.
     */
    public static unowned SchemaType?
    find_type (Type type)
    {
      return type_registry.lookup (type);
    }
    /**
     * Looks for a registered SchemaType by its declared name.
     */
    public static unowned SchemaType?
    find_type_by_name (string name)
    {
      return name_registry.lookup (name);
    }
  }
}

// vim: set et ts=2 sts=2 sw=2 ai :