/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 class LO.Application : Gtk.Application {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
19
1 by Gustav Hartvigsson
* Inital release
20
  LO.Actions actions;
7 by Stream
Added support for setting timeout in config.
21
  public Gtk.Window? window;
1 by Gustav Hartvigsson
* Inital release
22
  Gtk.ListBox? list_box;
23
24
  public bool can_quit = true;
25
26
  public void try_quit () {
27
    if (can_quit) {
28
      app.quit ();
29
    }
30
  }
31
32
33
  construct {
34
    GLib.OptionEntry[] options = {
35
      {"version", 'v', OptionFlags.NONE, OptionArg.NONE, null,
36
      "Display version number.", null},
37
38
      {"config", 0, OptionFlags.NONE, OptionArg.FILENAME, null,
39
      "Path to configuration file." , "<path/to/config.ini>"},
40
41
      {"actions", 0, OptionFlags.NONE, OptionArg.FILENAME, null,
42
      "Path to actions file." , "<path/to/actions.ini>"},
43
44
      {"help-actions", 0, OptionFlags.NONE, OptionArg.NONE, null,
45
      "Print extra information about action configuration files.", null},
46
47
      {"help-config", 0, OptionFlags.NONE, OptionArg.NONE, null,
48
      "Print information on how to configure app specific things.", null},
49
50
      {null, 0, 0, 0, null, null}, 
51
    };
52
    application_id = APP_NAME;
53
    add_main_option_entries (options);
54
55
  }
56
57
  public override int handle_local_options (VariantDict options) {
58
    if (options.contains ("version")) {
59
      print (get_app_version ());
60
      return 0;
61
    }
62
    if (options.contains ("help-actions")) {
63
      print_actions_help ();
64
      return 0;
65
    }
66
67
    if (options.contains ("help-config")) {
68
      print_config_help ();
69
      return 0;
70
    }
71
72
    if (options.contains ("actions")) {
73
      opts.actions_path = options.lookup_value ("actions", null).get_bytestring ();
74
    }
75
76
    if (options.contains ("config")) {
77
      opts.config_path = options.lookup_value ("config", null).get_bytestring ();
78
    }
79
80
    return -1; // Let the base class handle the rest.
81
  }
82
83
  public Application () {
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
84
85
  }
86
1 by Gustav Hartvigsson
* Inital release
87
  public override void activate () {
88
    //var settings = Settings.get_instance ();
7 by Stream
Added support for setting timeout in config.
89
    //var _opts = Options.get_instance ();
90
1 by Gustav Hartvigsson
* Inital release
91
    window = new Gtk.ApplicationWindow (this) {
92
      title = "LoggerOuter",
93
      width_request = 500,
94
      height_request = 500,
95
      resizable = false,
96
    };
97
98
    var _action = new GLib.SimpleAction ("quit", null);
99
100
    _action.activate.connect (() => {this.quit ();} );
101
    this.add_action (_action);
102
    this.set_accels_for_action ("app.quit", {"Escape","q",null});
103
104
    if (opts.config_path != null) {
105
      settings.load_settings (opts.config_path);
106
    } else {
107
      settings.load_setting_from_xdg ();
108
    }
109
110
    if (opts.actions_path != null) {
7 by Stream
Added support for setting timeout in config.
111
      actions = LO.Actions.from_file (opts.actions_path);
112
    } else {
113
      actions = LO.Actions.from_xdg ();
114
    }
115
116
117
    list_box  = new Gtk.ListBox () {
1 by Gustav Hartvigsson
* Inital release
118
      margin_top = 50,
119
      margin_bottom = 50,
120
      margin_start = 100,
121
      margin_end = 100,
122
      show_separators = true,
123
      activate_on_single_click = true,
124
    };
125
    list_box.row_activated.connect ((row) => {
126
      row.child.activate ();
127
    });
128
129
130
    add_buttons (actions.get_entries ());
131
132
    window.set_child (list_box);
133
    window.present ();
134
    window.set_focus (list_box.get_selected_row  ());
135
136
    bool dark_theme = settings.dark_theme;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
137
    Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = dark_theme;
138
  }
1 by Gustav Hartvigsson
* Inital release
139
140
141
  void add_buttons (Gee.ArrayList<LO.ActionEntry?> list) {
142
    foreach (ActionEntry entry in list) {
143
      var btn = new LO.ActionButton (ref entry);
7 by Stream
Added support for setting timeout in config.
144
      this.list_box.append (btn);
1 by Gustav Hartvigsson
* Inital release
145
    }
146
  }
147
148
  void print_actions_help () {
149
    string message = """
150
Help about how to configure actions for %s.
151
152
The standard locaton is "$XDG_USER_HOME/loggerouter/actions.ini", but
153
you can specify your own location using the --actions= option.
154
155
Each "group" in the ini file will be displayed like a button in the application,
156
and will take the name of the group. Thus it is of great importance that the group
157
is unique, as groups of the same name will be merged.
158
159
Each group must have an "Exec=" key.
160
161
There is an optional "Text=" key that can be set.
162
163
An optional "Icon=" key can be set. Normaly this uses the GTK icon handling mechanism,
164
but you can prefix with "file://" and the app will try to use that image intead.
165
166
An example of how this may work is as follows:
167
[logout]
168
Text=Logout session.
169
Exec=swaymsg exit
170
Icon=system-log-out-symbolic
171
""";
172
    GLib.stdout.printf (message, opts.exec_name);
173
  }
174
175
  void print_config_help () {
176
    string message = """
177
Help for how to configure %s.
178
179
The standard location is "$XDG_USER_HOME/loggerouter/config.ini", but
180
you can specify your own location using the --config= option.
181
182
The ini file has only one section: [Main]
183
184
Valid keys are:
185
DarkTheme=      # Do you want to use a dark theme?
186
""";
187
    GLib.stdout.printf (message, opts.exec_name);
188
  }
189
190
191
}
192