/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.App : Gtk.Application {
19
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
85
  public override void activate () {
86
87
    window = new Gtk.ApplicationWindow (this) {
88
      title = "LoggerOuter",
89
      width_request = 500,
90
      height_request = 500,
91
      resizable = false,
92
    };
93
94
    var _action = new GLib.SimpleAction ("quit", null);
95
96
    _action.activate.connect (() => {this.quit ();} );
97
    this.add_action (_action);
98
    this.set_accels_for_action ("app.quit", {"Escape","q",null});
99
100
    if (opts.actions_path != null) {
101
      actions = LO.Action.from_file (opts.actions_path);
102
    } else {
103
      actions = LO.Action.from_xdg ();
104
    }
105
106
    if (opts.config_path != null) {
107
      settings.load_settings (opts.config_path);
108
    } else {
109
      settings.load_setting_from_xdg ();
110
    }
111
112
    list_box  = new Gtk.ListBox () {
113
      margin_top = 50,
114
      margin_bottom = 50,
115
      margin_start = 100,
116
      margin_end = 100,
117
      show_separators = true,
118
      activate_on_single_click = true,
119
    };
120
    list_box.row_activated.connect ((row) => {
121
      row.child.activate ();
122
    });
123
124
    if (opts.config_path != null) {
125
      settings.load_settings (opts.config_path);
126
    } else {
127
      settings.load_setting_from_xdg ();
128
    }
129
130
    Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = settings.dark_theme;
131
132
    add_buttons (actions.get_entries ());
133
134
    window.set_child (list_box);
135
    window.present ();
136
    window.set_focus (list_box.get_selected_row  ());
137
138
  }
139
140
141
  void add_buttons (Gee.ArrayList<LO.ActionEntry?> list) {
142
    foreach (ActionEntry entry in list) {
143
      var btn = new LO.ActionButton (entry);
144
      this.list_box.append (btn);
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