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