/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.ActionButton : Gtk.Button {
19
// This whole class is cursed. :-)
20
  Gtk.Image icon;
21
  Gtk.Box box;
22
  Gtk.Label widget_label;
23
24
  string? exec_action;
25
26
  string _button_text;
7 by Stream
Added support for setting timeout in config.
27
  
28
  int64? _timeout_time;
1 by Gustav Hartvigsson
* Inital release
29
30
  void run_action () {
31
32
    string[]? argv = null;
33
    string[] envp = Environ.get ();
34
    Pid pid;
35
    try {
36
      GLib.Shell.parse_argv (exec_action, out argv);
37
38
      GLib.Process.spawn_async (null,  argv, envp,
39
                                GLib.SpawnFlags.DO_NOT_REAP_CHILD | GLib.SpawnFlags.SEARCH_PATH_FROM_ENVP ,
40
                                null, out pid);
41
      
42
    } catch (GLib.SpawnError e) {
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
43
      LO.present_dialog ("ERROR: %s\n", e.message);
1 by Gustav Hartvigsson
* Inital release
44
    } catch (GLib.ShellError e) {
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
45
      LO.present_dialog ("ERROR: %s\n", e.message);
1 by Gustav Hartvigsson
* Inital release
46
    }
47
    app.try_quit ();
48
  }
49
50
7 by Stream
Added support for setting timeout in config.
51
  construct {
52
    this.focusable = true;
53
1 by Gustav Hartvigsson
* Inital release
54
    this.box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
55
      homogeneous = false,
56
    };
57
    
58
      this.icon = new Gtk.Image () {
59
        vexpand = false,
60
        hexpand = false,
61
        pixel_size = 28,
62
        width_request = 28,
63
        height_request = 28,
64
      };
7 by Stream
Added support for setting timeout in config.
65
      box.append (icon);
66
  }
67
68
  public ActionButton (ref ActionEntry action) {
69
70
    if (action.icon != null) {
1 by Gustav Hartvigsson
* Inital release
71
      string _icon_name;
72
      if (!action.icon_is_path) {
73
        icon.icon_name = action.icon;
74
      } else {
75
        _icon_name = strip_prelude_of_path (action.icon);
76
        this.icon.set_from_file (_icon_name);
77
      }
7 by Stream
Added support for setting timeout in config.
78
    } else {
79
        this.icon.visible = false;
80
    }
81
82
    this._timeout_time = action.timeout_time;
83
    if (this._timeout_time < 0) {
84
      this._timeout_time = LO.Settings.DEFAULT_TIMEOUT;
85
    }
86
    stdout.printf ("TIMEOUT: %" + int64.FORMAT + "\n", (int64) this._timeout_time);
87
    //stdout.printf (@"TIMEOUT: $(this._timeout_time)\n");
1 by Gustav Hartvigsson
* Inital release
88
89
    if (action.text != null) {
90
      _button_text = action.text;
91
    } else {
92
      _button_text = action.group;
93
    }
94
95
    this.exec_action = action.exec;
96
97
    this.widget_label = new Gtk.Label (_button_text) {
98
      hexpand = true,
99
      vexpand = true,
100
      halign = Gtk.Align.CENTER,
101
      valign = Gtk.Align.CENTER,
102
      margin_end = 28,
103
    };
104
    box.append (widget_label);
105
    this.set_child (this.box);
106
107
    this.clicked.connect (() => {
7 by Stream
Added support for setting timeout in config.
108
      var timer = new LO.Timer (this._button_text, this._timeout_time, (e) => {
1 by Gustav Hartvigsson
* Inital release
109
        if (e == LO.Timer.ExitCode.OK) {
110
            this.run_action ();
111
        }
112
      });
113
      timer.set_transient_for (app.get_active_window ());
7 by Stream
Added support for setting timeout in config.
114
      timer.visible = true;
1 by Gustav Hartvigsson
* Inital release
115
    });
116
117
  }
118
119
}