/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.Timer : Gtk.Window {
19
20
  Gtk.Box layout;
21
  Gtk.Box button_box;
22
  Gtk.Label timer_label;
23
  Gtk.Label text_label;
24
  Gtk.Button ok_button;
25
  Gtk.Button close_button;
26
  
27
  private int _time;
28
  public int time {
29
    construct set {
30
      this._time = value;
31
    }
32
    get {
33
      return this._time;
34
    }
35
  }
36
37
  private string _text;
38
  public string text {
39
    construct set {
40
      this._text = value;
41
    }
42
    get {
43
      return _text;
44
    }
45
  }
46
47
  public delegate void ReturnCallback (ExitCode exit_code);
48
  ReturnCallback callback;
49
50
  construct {
51
52
    this.width_request = 100;
53
    this.height_request = 100;
54
    this.resizable = false;
55
56
    layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 5) {
57
      hexpand = true,
58
      vexpand = true,
59
      homogeneous = false,
60
    };
61
    button_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
62
      hexpand = true,
63
      vexpand = false,
64
      homogeneous = true,
65
    };
66
67
    timer_label = new Gtk.Label ("Time Remanining: 0") {
68
      vexpand = true,
69
    };
70
71
    text_label = new Gtk.Label (this.text) {
72
      vexpand = true,
73
    };
74
75
    layout.append (text_label);
76
    layout.append (timer_label);
77
78
    close_button = new Gtk.Button.with_label ("Cancel");
79
    close_button.clicked.connect (() => {
80
      _time = -1;
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
81
      return_to_caller (ExitCode.CANCEL);
1 by Gustav Hartvigsson
* Inital release
82
    });
83
    button_box.append (close_button);
84
85
    ok_button = new Gtk.Button.with_label ("OK");
86
    ok_button.clicked.connect (() => {
87
      return_to_caller (ExitCode.OK);
88
    });
89
    button_box.append (ok_button);
90
91
    layout.append (button_box);
92
    
93
    this.child = layout;
94
    this.modal = true;
95
  }
96
97
  public Timer (string text, int time, owned ReturnCallback callback) {
98
    this.text = text;
99
    this.time = time;
100
    this.callback = (owned) callback;
101
102
    this.text_label.set_text (this.text);
103
    this.start_timer ();
104
  }
105
106
  public enum ExitCode {
107
    INVALID,
108
    OK,
109
    CANCEL;
110
111
    public string to_string () {
112
      switch (this) {
113
        case (INVALID):
114
          return "INVALID";
115
        case (OK):
116
          return "OK";
117
        case (CANCEL):
118
          return "CANCEL";
119
        default:
120
          assert_not_reached ();
121
      }
122
    }
123
  }
124
125
  private bool label_update () {
126
    if (_time <= -1) {
5 by Gustav Hartvigsson
[timer.vala] Fixed the timeout being triggered even is user has pressed concle.
127
      return false;
128
    }
129
    if (_time == 0) {
1 by Gustav Hartvigsson
* Inital release
130
      return_to_caller (ExitCode.OK);
131
      return false;
132
    }
133
    
134
    this._time--;
135
136
    string text = @"Time Remanining: $(this._time)";
137
    this.timer_label.set_text (text);
138
139
140
    return true;
141
  }
142
143
  private void start_timer () {
144
    label_update ();
145
    GLib.Timeout.add_seconds (1, label_update);
146
  }
147
148
  private void return_to_caller (ExitCode e) {
149
      assert (this.callback != null);
150
      this.callback (e);
151
      this.destroy ();
152
  }
153
154
#if TIMER_DEBUG
155
156
  static int main (string[] args) {
157
    Gtk.init ();
158
    Gtk.Application app = new Gtk.Application ("com.foobar.test", GLib.ApplicationFlags.FLAGS_NONE);
159
    app.activate.connect (() => {
160
      LO.Timer t = new LO.Timer ("Shutting down", 30, (e) => {
161
        print (@"Got: $(e)\n");
162
        if (e == LO.Timer.ExitCode.OK) {
163
          return 0;
164
        } else {
165
          return 1;
166
        }
167
      });
168
      t.present ();
169
      t.show ();
170
      app.add_window (t);
171
    });
172
173
    return app.run ();
174
  }
175
176
#endif
177
178
}
179