/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
7 by Stream
Added support for setting timeout in config.
20
  public const int64 DEFAULT_TIMEOUT = 15;
21
22
  private const string CONFIG_INI_NAMESPACE_MAIN = "Main";
1 by Gustav Hartvigsson
* Inital release
23
24
  string? config_path;
25
  GLib.KeyFile? key_file;
26
27
28
  public bool? dark_theme;
7 by Stream
Added support for setting timeout in config.
29
  public int64 timeout_time;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
30
  
31
  //TODO: Timer countdown setting.
32
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
33
  public string to_string () {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
34
    var builder = new StringBuilder ();
7 by Stream
Added support for setting timeout in config.
35
    builder.append_printf ("key_file: (%px)\n", key_file);
36
    builder.append_printf ("\tconfig_path: %s\n", config_path);
37
    builder.append_printf ("\tdark_theme: %s\n", dark_theme.to_string ());
38
    builder.append_printf ("\ttimeout_time: %s\n",  timeout_time.to_string ());
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
39
    return builder.str;
40
  }
1 by Gustav Hartvigsson
* Inital release
41
7 by Stream
Added support for setting timeout in config.
42
  //private static GLib.Once<LO.Settings?> instance;
43
  //public static unowned LO.Settings? get_instance () {
44
  //  unowned var ret_val = instance.once (() => {
45
  //    return LO.Settings ();
46
  //  });
47
 
48
  //  return ret_val;
49
  //}
50
51
  public Settings () {
52
    this.timeout_time = -1;
53
    this.dark_theme = null;
1 by Gustav Hartvigsson
* Inital release
54
  }
55
56
  public void load_settings (string config_path) {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
57
    this.config_path = config_path;
7 by Stream
Added support for setting timeout in config.
58
    key_file = new GLib.KeyFile ();
1 by Gustav Hartvigsson
* Inital release
59
    parse_key_file ();
60
  }
61
62
  public void load_setting_from_xdg () {
63
    key_file = new GLib.KeyFile ();
64
    this.config_path = LO_XDG_CONFIG_PATH;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
65
    if (GLib.File.new_for_path (this.config_path).query_exists ()) {
66
      parse_key_file ();
67
    } else {
7 by Stream
Added support for setting timeout in config.
68
      stderr.printf ("Could not find file: %s", this.config_path);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
69
      this.dark_theme = false;
70
    }
1 by Gustav Hartvigsson
* Inital release
71
  }
72
7 by Stream
Added support for setting timeout in config.
73
1 by Gustav Hartvigsson
* Inital release
74
  private void parse_key_file ()
75
  requires (key_file != null) {
76
    try {
77
      key_file.load_from_file (this.config_path, GLib.KeyFileFlags.NONE);
78
    } catch (GLib.FileError e) {
79
      string message = @"ERROR: $(e.message)\n";
80
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
81
      GLib.Process.exit (41);
1 by Gustav Hartvigsson
* Inital release
82
    } catch (GLib.KeyFileError e) {
83
      string message = @"ERROR: $(e.message)\n";
84
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
85
      GLib.Process.exit (42);
1 by Gustav Hartvigsson
* Inital release
86
    }
87
88
    try {
89
      if( key_file.has_key (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme") ) {
90
        this.dark_theme = key_file.get_boolean (CONFIG_INI_NAMESPACE_MAIN, "DarkTheme");
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
91
      } else {
92
        this.dark_theme = true;
1 by Gustav Hartvigsson
* Inital release
93
      }
94
    } catch (GLib.KeyFileError e) {
95
      string message = @"ERROR: $(e.message)\n";
96
      stderr.printf (message);
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
97
      GLib.Process.exit (43);
1 by Gustav Hartvigsson
* Inital release
98
    }
7 by Stream
Added support for setting timeout in config.
99
100
    try {
101
        if (key_file.has_key (CONFIG_INI_NAMESPACE_MAIN, "Timeout")) {
102
            this.timeout_time = key_file.get_int64 (CONFIG_INI_NAMESPACE_MAIN,
103
                                                     "Timeout");
104
        } else {
105
            this.timeout_time = DEFAULT_TIMEOUT;
106
        }
107
    } catch (GLib.KeyFileError e) {
108
      string message = @"ERROR: $(e.message)\n";
109
      stderr.printf (message);
110
      GLib.Process.exit (44);
111
    }
112
    assert (this.timeout_time != -1);
1 by Gustav Hartvigsson
* Inital release
113
  }
114
}