/loggerouter/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerouter/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
   This file is part of LoggerOuter.

 LoggerOuter is free software: you can redistribute it and/or modify it under the
 terms of the GNU Lesser General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.

 LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lessel General Public License
 along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
 */

public struct LO.ActionEntry {
  string group; /*< The "[Group]" heading*/
  string? text; /*< the "Text=foo bar" key*/
  string exec; /*< the "Exec=baz buz" key*/
  string? icon; /*< icon name or path. "Icon="*/
  bool icon_is_path; /*< wether or not "icon" is a path or an icon name. */

  public string to_string () {
    string outstr = @"[$(group)]\nText=";
    if (text != null) {
      outstr += text;
    }
    outstr += @"\nExec=$(exec)\n";
    if (icon != null) {
      outstr += @"Icon=$(icon)\n";
    } else {
      outstr += "Icon=\n";
    }
    outstr += @"# Icon is path: $(icon_is_path)\n";
    return outstr;
  }
}

public struct LO.Action {
  Gee.ArrayList<LO.ActionEntry?> entries;
  KeyFile? actions_key_file;
  string? actions_file;


  // Standard way of loadin settings file
  public Action.from_xdg () {
    actions_key_file = new KeyFile ();
    // TODO: Fail gracefully if file does not exist.
    try {
      actions_key_file.load_from_file (LO_XDG_ACTIONS_PATH, GLib.KeyFileFlags.NONE);
    } catch (GLib.FileError e) {
      string message = "ERROR: Could not load configuration file.\n" +
                       "Have you created one? See \"--help\" for more information.\n" +
                       @"Error: $(e.message)\n";
      GLib.stderr.printf (message);
      LO.present_dialog (message);
      GLib.Process.exit (33);
    } catch (GLib.KeyFileError e) {
      string message = "ERROR: Could not load configuration file.\n" +
                       " ini file error.\n" +
                       @"Error: $(e.message)\n";
      GLib.stderr.printf (message);
      LO.present_dialog (message);
      GLib.Process.exit (34);
    }

    parse_keyfile ();
  }

  // For use with --config_file
  public Action.from_file (string actions_path) {
    //absolute path:
    actions_key_file = new KeyFile ();

    this.actions_file = LO.resolve_path (actions_path);

    try {
      actions_key_file.load_from_file (actions_file, GLib.KeyFileFlags.NONE);
    } catch (GLib.FileError e) {
      string message = @"ERROR: Could not open file \"$(actions_file)\":\n" +
        @"$(e.message)\n";
      GLib.stderr.printf (message);
      LO.present_dialog (message);
      GLib.Process.exit (37);
    } catch (GLib.KeyFileError e) {
      string message =
        @"ERROR: Something went wrong whilst loading the key-file \"$(actions_file)\":\n" +
        @"$(e.message)\n";
      GLib.stderr.printf (message);
      LO.present_dialog (message);
      GLib.Process.exit (38);
    }

    parse_keyfile ();
  }

  // Parse the KeyFile. 
  void parse_keyfile ()
  requires (this.actions_key_file != null) {
     /* The keyfile stucture is like this:
      * Each action has it's own group, the groups name is the name of the action
      * in the UI.
      * the key "exec" is used what is run when the button in pressed.
      *
      * Example:
      *
      * [Logout]
      * Text=Logout session
      * Exec=swaymsg exit
      */
    string[] group_list = actions_key_file.get_groups ();

    if (group_list.length < 1) { // No groups
      string message = @"ERROR: The keyfile $(this.actions_file) is empty.\n" +
                        "Please add actions to the keyfile (ini-file).\n";
      GLib.stderr.printf (message);
      LO.present_dialog (message);
      GLib.Process.exit (35);
    }

    entries = new Gee.ArrayList<LO.ActionEntry?> ();

    foreach (string _group in group_list) {
      string? _text = null;
      string? _icon = null;
      try {
        if (actions_key_file.has_key (_group, "Text")) {
          _text = actions_key_file.get_string (_group, "Text");
        }
        string? _exec = actions_key_file.get_string (_group, "Exec");
        if (actions_key_file.has_key (_group, "Icon"))  {
          _icon = actions_key_file.get_string (_group, "Icon");
        }
        bool _icon_is_path = false;
        if (_icon != null) {
          if (str_starts_with (_icon, "file://")) {
            _icon_is_path = true;
          }
        }
        var entry = LO.ActionEntry () {
          group = _group,
          text = _text,
          exec = _exec,
          icon = _icon,
          icon_is_path = _icon_is_path
        };
        entries.add (entry);
      } catch (GLib.KeyFileError e) {
        string message = "ERROR: An error has occurred whilst parsing the key-file:\n" +
          @"Error: $(e.message)\n";
        LO.present_dialog (message);
        stderr.printf (message);
        GLib.Process.exit (36);
      }
    }
  }

  public Gee.ArrayList<LO.ActionEntry?>? get_entries () {
    if (this.entries == null) {
      GLib.stderr.printf ("ERROR: enteties list does not seem to be initialised!\n");
      return null;
    }
    return this.entries;
  }

}