/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
31
  string to_string () {
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) {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
48
    print ("path\n");
1 by Gustav Hartvigsson
* Inital release
49
    key_file = new GLib.KeyFile ();
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
50
    this.config_path = config_path;
1 by Gustav Hartvigsson
* Inital release
51
    parse_key_file ();
52
  }
53
54
  public void load_setting_from_xdg () {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
55
    print ("xdg\n");
1 by Gustav Hartvigsson
* Inital release
56
    key_file = new GLib.KeyFile ();
57
    this.config_path = LO_XDG_CONFIG_PATH;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
58
    if (GLib.File.new_for_path (this.config_path).query_exists ()) {
59
      parse_key_file ();
60
    } else {
61
      stderr.printf ("Colud not find file: %s", this.config_path);
62
      this.dark_theme = false;
63
    }
1 by Gustav Hartvigsson
* Inital release
64
  }
65
66
  private void parse_key_file ()
67
  requires (key_file != null) {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
68
    print ("Hello!\n");
1 by Gustav Hartvigsson
* Inital release
69
    try {
70
      key_file.load_from_file (this.config_path, GLib.KeyFileFlags.NONE);
71
    } catch (GLib.FileError e) {
72
      string message = @"ERROR: $(e.message)\n";
73
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
74
      GLib.Process.exit (41);
1 by Gustav Hartvigsson
* Inital release
75
    } catch (GLib.KeyFileError e) {
76
      string message = @"ERROR: $(e.message)\n";
77
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
78
      GLib.Process.exit (42);
1 by Gustav Hartvigsson
* Inital release
79
    }
80
81
    try {
82
      if( key_file.has_key (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme") ) {
83
        this.dark_theme = key_file.get_boolean (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme");
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
84
      } else {
85
        this.dark_theme = true;
1 by Gustav Hartvigsson
* Inital release
86
      }
87
    } catch (GLib.KeyFileError e) {
88
      string message = @"ERROR: $(e.message)\n";
89
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
90
      GLib.Process.exit (43);
1 by Gustav Hartvigsson
* Inital release
91
    }
92
  }
93
}