/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk
1 by Gustav Hartvigsson
* Inital release
1
/*
2
   This file is part of LoggerOuter.
3
4
 LoggerOuter is free software: you can redistribute it and/or modify it under the
5
 terms of the GNU Lesser General Public License as published by the Free Software
6
 Foundation, either version 3 of the License, or (at your option) any later
7
 version.
8
9
 LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
10
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12
 License for more details.
13
14
 You should have received a copy of the GNU Lessel General Public License
15
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
public struct LO.Settings {
19
20
  const string CONFIG_INI_NAMESPACE_MAIN = "Main";
21
22
  string? config_path;
23
  GLib.KeyFile? key_file;
24
25
  private static GLib.Once<LO.Settings?> instance;
26
27
  public bool? dark_theme;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
28
  
29
  //TODO: Timer countdown setting.
30
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
31
  public string to_string () {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
32
    var builder = new StringBuilder ();
33
    builder.append_printf ("key_file: %px\n", key_file);
34
    builder.append_printf ("config_path: %s", config_path);
35
    builder.append_printf ("dark_theme: %s", dark_theme.to_string ());
36
    return builder.str;
37
  }
1 by Gustav Hartvigsson
* Inital release
38
39
  public static unowned LO.Settings? get_instance () {
40
    return instance.once (() => {
41
      return LO.Settings ();
42
    });
43
  }
44
45
  private Settings () {}
46
47
  public void load_settings (string config_path) {
48
    key_file = new GLib.KeyFile ();
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
49
    this.config_path = config_path;
1 by Gustav Hartvigsson
* Inital release
50
    parse_key_file ();
51
  }
52
53
  public void load_setting_from_xdg () {
54
    key_file = new GLib.KeyFile ();
55
    this.config_path = LO_XDG_CONFIG_PATH;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
56
    if (GLib.File.new_for_path (this.config_path).query_exists ()) {
57
      parse_key_file ();
58
    } else {
59
      stderr.printf ("Colud not find file: %s", this.config_path);
60
      this.dark_theme = false;
61
    }
1 by Gustav Hartvigsson
* Inital release
62
  }
63
64
  private void parse_key_file ()
65
  requires (key_file != null) {
66
    try {
67
      key_file.load_from_file (this.config_path, GLib.KeyFileFlags.NONE);
68
    } catch (GLib.FileError e) {
69
      string message = @"ERROR: $(e.message)\n";
70
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
71
      GLib.Process.exit (41);
1 by Gustav Hartvigsson
* Inital release
72
    } catch (GLib.KeyFileError e) {
73
      string message = @"ERROR: $(e.message)\n";
74
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
75
      GLib.Process.exit (42);
1 by Gustav Hartvigsson
* Inital release
76
    }
77
78
    try {
79
      if( key_file.has_key (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme") ) {
80
        this.dark_theme = key_file.get_boolean (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme");
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
81
      } else {
82
        this.dark_theme = true;
1 by Gustav Hartvigsson
* Inital release
83
      }
84
    } catch (GLib.KeyFileError e) {
85
      string message = @"ERROR: $(e.message)\n";
86
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
87
      GLib.Process.exit (43);
1 by Gustav Hartvigsson
* Inital release
88
    }
89
  }
90
}