/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/action.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
   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 struct LO.ActionEntry {
 
19
  string group; /*< The "[Group]" heading*/
 
20
  string? text; /*< the "Text=foo bar" key*/
 
21
  string exec; /*< the "Exec=baz buz" key*/
 
22
  string? icon; /*< icon name or path. "Icon="*/
 
23
  bool icon_is_path; /*< wether or not "icon" is a path or an icon name. */
 
24
 
 
25
  public string to_string () {
 
26
    string outstr = @"[$(group)]\nText=";
 
27
    if (text != null) {
 
28
      outstr += text;
 
29
    }
 
30
    outstr += @"\nExec=$(exec)\n";
 
31
    if (icon != null) {
 
32
      outstr += @"Icon=$(icon)\n";
 
33
    } else {
 
34
      outstr += "Icon=\n";
 
35
    }
 
36
    outstr += @"# Icon is path: $(icon_is_path)\n";
 
37
    return outstr;
 
38
  }
 
39
}
 
40
 
 
41
public struct LO.Action {
 
42
  Gee.ArrayList<LO.ActionEntry?> entries;
 
43
  KeyFile? actions_key_file;
 
44
  string? actions_file;
 
45
 
 
46
 
 
47
  // Standard way of loadin settings file
 
48
  public Action.from_xdg () {
 
49
    actions_key_file = new KeyFile ();
 
50
 
 
51
    try {
 
52
      actions_key_file.load_from_file (LO_XDG_ACTIONS_PATH, GLib.KeyFileFlags.NONE);
 
53
    } catch (GLib.FileError e) {
 
54
      string message = "ERROR: Could not load configuration file.\n" +
 
55
                       "Have you created one? See \"--help\" for more information.\n" +
 
56
                       @"Error: $(e.message)\n";
 
57
      GLib.stderr.printf (message);
 
58
      LO.prensent_dialog (message);
 
59
      GLib.Process.exit (33);
 
60
    } catch (GLib.KeyFileError e) {
 
61
      string message = "ERROR: Could not load configuration file.\n" +
 
62
                       " ini file error.\n" +
 
63
                       @"Error: $(e.message)\n";
 
64
      GLib.stderr.printf (message);
 
65
      LO.prensent_dialog (message);
 
66
      GLib.Process.exit (34);
 
67
    }
 
68
 
 
69
    parse_keyfile ();
 
70
  }
 
71
 
 
72
  // For use with --config_file
 
73
  public Action.from_file (string actions_path) {
 
74
    //absolute path:
 
75
    actions_key_file = new KeyFile ();
 
76
 
 
77
    this.actions_file = LO.resolve_path (actions_path);
 
78
 
 
79
    try {
 
80
      actions_key_file.load_from_file (actions_file, GLib.KeyFileFlags.NONE);
 
81
    } catch (GLib.FileError e) {
 
82
      string message = @"ERROR: Could not open file \"$(actions_file)\":\n" +
 
83
        @"$(e.message)\n";
 
84
      GLib.stderr.printf (message);
 
85
      prensent_dialog (message);
 
86
      GLib.Process.exit (37);
 
87
    } catch (GLib.KeyFileError e) {
 
88
      string message =
 
89
        @"ERROR: Something went wrong whilst loading the key-file \"$(actions_file)\":\n" +
 
90
        @"$(e.message)\n";
 
91
      GLib.stderr.printf (message);
 
92
      prensent_dialog (message);
 
93
      GLib.Process.exit (38);
 
94
    }
 
95
 
 
96
    parse_keyfile ();
 
97
  }
 
98
 
 
99
  // Parse the KeyFile. 
 
100
  void parse_keyfile ()
 
101
  requires (this.actions_key_file != null) {
 
102
     /* The keyfile stucture is like this:
 
103
      * Each action has it's own group, the groups name is the name of the action
 
104
      * in the UI.
 
105
      * the key "exec" is used what is run when the button in pressed.
 
106
      *
 
107
      * Example:
 
108
      *
 
109
      * [Logout]
 
110
      * Text=Logout session
 
111
      * Exec=swaymsg exit
 
112
      */
 
113
    string[] group_list = actions_key_file.get_groups ();
 
114
 
 
115
    if (group_list.length < 1) { // No groups
 
116
      string message = @"ERROR: The keyfile $(this.actions_file) is empty.\n" +
 
117
                        "Please add actions to the keyfile (ini-file).\n";
 
118
      GLib.stderr.printf (message);
 
119
      LO.prensent_dialog (message);
 
120
      GLib.Process.exit (35);
 
121
    }
 
122
 
 
123
    entries = new Gee.ArrayList<LO.ActionEntry?> ();
 
124
 
 
125
    foreach (string _group in group_list) {
 
126
      string? _text = null;
 
127
      string? _icon = null;
 
128
      try {
 
129
        if (actions_key_file.has_key (_group, "Text")) {
 
130
          _text = actions_key_file.get_string (_group, "Text");
 
131
        }
 
132
        string? _exec = actions_key_file.get_string (_group, "Exec");
 
133
        if (actions_key_file.has_key (_group, "Icon"))  {
 
134
          _icon = actions_key_file.get_string (_group, "Icon");
 
135
        }
 
136
        bool _icon_is_path = false;
 
137
        if (_icon != null) {
 
138
          if (str_starts_with (_icon, "file://")) {
 
139
            _icon_is_path = true;
 
140
          }
 
141
        }
 
142
        var entry = LO.ActionEntry () {
 
143
          group = _group,
 
144
          text = _text,
 
145
          exec = _exec,
 
146
          icon = _icon,
 
147
          icon_is_path = _icon_is_path
 
148
        };
 
149
        entries.add (entry);
 
150
      } catch (GLib.KeyFileError e) {
 
151
        string message = "ERROR: An error has occurred whilst parsing the key-file:\n" +
 
152
                         @"Error: $(e.message)\n";
 
153
        prensent_dialog (message);
 
154
        stderr.printf (message);
 
155
        GLib.Process.exit (36);
 
156
      }
 
157
    }
 
158
  }
 
159
 
 
160
  public Gee.ArrayList<LO.ActionEntry?>? get_entries () {
 
161
    if (this.entries == null) {
 
162
      GLib.stderr.printf ("ERROR: enteties list does not seem to be initialised!\n");
 
163
      return null;
 
164
    }
 
165
    return this.entries;
 
166
  }
 
167
 
 
168
}