/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk

« back to all changes in this revision

Viewing changes to src/application.vala

  • Committer: Gustav Hartvigsson
  • Date: 2025-06-03 20:51:10 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20250603205110-zfozhyqh8werenbv
* Inital release

Show diffs side-by-side

added added

removed removed

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