Use LED pattern helper to test LED patterns
[led-pattern-ed] / src / led-program-dialog.vala
1 /* This file is part of LED Pattern Editor.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * LED Pattern Editor is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * LED Pattern Editor is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 class LedProgramDialog : Gtk.Dialog {
20         LedPatternView lpv;
21         LedPatternRX51 pattern;
22
23         public LedProgramDialog (LedPatternRX51 _pattern) {
24                 pattern = _pattern;
25                 set_title ("LED pattern editor - " +
26                            (pattern.name.has_prefix ("Pattern") ?
27                             pattern.name.offset (7) : pattern.name) + " pattern");
28
29                 var content = (Gtk.VBox) get_content_area ();
30                 content.set_size_request (-1, 5*70);
31
32                 lpv = new LedPatternView (pattern.copy ());
33                 lpv.set_size_request (-1, 70);
34                 content.pack_start (lpv, false, false, 0);
35
36                 var pannable = new Hildon.PannableArea ();
37                 var vbox = new Gtk.VBox (false, 0);
38
39                 foreach (LedCommand command in lpv.pattern.engine1) {
40                         var command_widget = new LedCommandWidget (command);
41
42                         vbox.pack_start (command_widget, false, false, 0);
43                 }
44
45                 pannable.add_with_viewport (vbox);
46                 content.pack_start (pannable, true, true, 0);
47
48                 content.show_all ();
49
50                 add_button ("Test", 1);
51                 add_button ("Done", Gtk.ResponseType.ACCEPT);
52
53                 response.connect (on_response);
54         }
55
56         void on_response (int response) {
57                 if (response == 1) {
58                         Timeout.add (200, delayed_spawn);
59                         return;
60                 }
61                 if (response == Gtk.ResponseType.ACCEPT) {
62                         if (pattern.dump () != lpv.pattern.dump ()) {
63                                 pattern.replace_with (lpv.pattern);
64                         }
65                 }
66         }
67
68         bool delayed_spawn () {
69                 try {
70                         int exit_status;
71                         string error;
72                         var command = "sudo /usr/bin/led-pattern-helper test \"" +
73                                       lpv.pattern.dump () + "\"";
74                         Process.spawn_command_line_sync (command, null, out error, out exit_status);
75                         if (exit_status != 0) {
76                                 var information = "Exit status: %d\n%s".printf (exit_status, error);
77                                 Hildon.Banner.show_information (null, null, information);
78                         }
79                 } catch (SpawnError e) {
80                         Hildon.Banner.show_information (null, null, e.message);
81                 }
82
83                 return false;
84         }
85 }