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