/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
        stdout.printf (settings.to_string ());
112
      actions = LO.Actions.from_file (opts.actions_path);
113
    } else {
114
      actions = LO.Actions.from_xdg ();
115
    }
116
117
118
    list_box  = new Gtk.ListBox () {
1 by Gustav Hartvigsson
* Inital release
119
      margin_top = 50,
120
      margin_bottom = 50,
121
      margin_start = 100,
122
      margin_end = 100,
123
      show_separators = true,
124
      activate_on_single_click = true,
125
    };
126
    list_box.row_activated.connect ((row) => {
127
      row.child.activate ();
128
    });
129
130
131
    add_buttons (actions.get_entries ());
132
133
    window.set_child (list_box);
134
    window.present ();
135
    window.set_focus (list_box.get_selected_row  ());
136
137
    bool dark_theme = settings.dark_theme;
4 by Gustav Hartvigsson
Fixed crash caused from missing config file.
138
    Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = dark_theme;
139
  }
1 by Gustav Hartvigsson
* Inital release
140
141
142
  void add_buttons (Gee.ArrayList<LO.ActionEntry?> list) {
143
    foreach (ActionEntry entry in list) {
144
      stdout.printf ("%s\n\n", entry.to_string ());
7 by Stream
Added support for setting timeout in config.
145
      var btn = new LO.ActionButton (ref entry);
146
      this.list_box.append (btn);
1 by Gustav Hartvigsson
* Inital release
147
    }
148
  }
149
150
  void print_actions_help () {
151
    string message = """
152
Help about how to configure actions for %s.
153
154
The standard locaton is "$XDG_USER_HOME/loggerouter/actions.ini", but
155
you can specify your own location using the --actions= option.
156
157
Each "group" in the ini file will be displayed like a button in the application,
158
and will take the name of the group. Thus it is of great importance that the group
159
is unique, as groups of the same name will be merged.
160
161
Each group must have an "Exec=" key.
162
163
There is an optional "Text=" key that can be set.
164
165
An optional "Icon=" key can be set. Normaly this uses the GTK icon handling mechanism,
166
but you can prefix with "file://" and the app will try to use that image intead.
167
168
An example of how this may work is as follows:
169
[logout]
170
Text=Logout session.
171
Exec=swaymsg exit
172
Icon=system-log-out-symbolic
173
""";
174
    GLib.stdout.printf (message, opts.exec_name);
175
  }
176
177
  void print_config_help () {
178
    string message = """
179
Help for how to configure %s.
180
181
The standard location is "$XDG_USER_HOME/loggerouter/config.ini", but
182
you can specify your own location using the --config= option.
183
184
The ini file has only one section: [Main]
185
186
Valid keys are:
187
DarkTheme=      # Do you want to use a dark theme?
188
""";
189
    GLib.stdout.printf (message, opts.exec_name);
190
  }
191
192
193
}
194