Add PatternInhibit support
[led-pattern-ed] / src / led-pattern-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 LedPatternDialog : Gtk.Dialog {
20         public enum Response {
21                 HELP = 1,
22                 RESTORE = 2
23         }
24
25         private unowned Osso.Context osso_context;
26         private unowned List<LedPattern> list;
27         private bool close = false;
28
29         public LedPatternDialog (List<LedPattern> _list, Osso.Context osso) {
30                 list = _list;
31                 osso_context = osso;
32                 set_title (_("LED Patterns"));
33
34                 var content = (Gtk.VBox) get_content_area ();
35                 content.set_size_request (-1, 5*70);
36
37                 var pannable = new Hildon.PannableArea ();
38                 var vbox = new Gtk.VBox (false, 0);
39
40                 foreach (LedPattern pattern in list) {
41                         // Skip PatternInhibit, which the LED Pattern Editor uses
42                         // internally to make MCE keep its hands off the LEDs.
43                         if (pattern.name == "PatternInhibit")
44                                 continue;
45
46                         var button = new LedPatternButton (pattern);
47                         Hildon.gtk_widget_set_theme_size (button, Hildon.SizeType.FINGER_HEIGHT);
48                         button.set_data ("pattern", pattern);
49                         button.clicked.connect (on_pattern_clicked);
50                         vbox.pack_start (button, true, true, 0);
51                 }
52
53                 pannable.add_with_viewport (vbox);
54                 content.pack_start (pannable, true, true, 0);
55                 content.show_all ();
56
57                 var button_help = new Gtk.Button.with_label (_("Help"));
58                 Hildon.gtk_widget_set_theme_size (button_help, Hildon.SizeType.FINGER_HEIGHT);
59
60                 var label = new Gtk.Label (_("Restore original\npatterns"));
61                 label.justify = Gtk.Justification.CENTER;
62                 var button_restore = new Gtk.Button ();
63                 button_restore.add (label);
64                 Hildon.helper_set_logical_font (button_restore, "SmallSystemFont");
65                 Hildon.gtk_widget_set_theme_size (button_restore, Hildon.SizeType.FINGER_HEIGHT);
66
67                 add_action_widget (button_help, Response.HELP);
68                 add_action_widget (button_restore, Response.RESTORE);
69
70                 var action_area = (Gtk.ButtonBox) get_action_area ();
71                 action_area.set_child_secondary (button_help, true);
72                 action_area.show_all ();
73
74                 add_button (_("Save"), Gtk.ResponseType.OK);
75
76                 response.connect (on_response);
77         }
78
79         public int run () {
80                 int response = 0;
81                 while (response >= 0) {
82                         response = base.run ();
83                         if (close)
84                                 return response;
85                 }
86                 return response;
87         }
88
89         private void on_pattern_clicked (Gtk.Button button) {
90                 LedPattern pattern = button.get_data ("pattern");
91                 if (pattern is LedPatternRX51) {
92                         var dialog = new LedProgramDialog ((LedPatternRX51) pattern);
93                         dialog.set_transient_for (this);
94
95                         int response = 0;
96                         while (response >= 0)
97                                 response = dialog.run ();
98                         dialog.destroy ();
99                 }
100         }
101
102         private void on_response (int response_id) {
103                 if (response_id == Response.HELP) {
104                         var url = "http://wiki.maemo.org/LED_Pattern_Editor";
105                         var status = osso_context.rpc_run_with_defaults ("osso_browser", "open_new_window", null, 's', url, 'b', false);
106                         if (status != Osso.Status.OK)
107                                 Hildon.Banner.show_information (this, null, _("Failed to open browser."));
108                 } else if (response_id == Response.RESTORE) {
109                         var note = new Hildon.Note.confirmation (this, _("Restore original patterns? All user-created patterns will be lost."));
110                         response_id = note.run ();
111                         note.destroy ();
112                         if (response_id == Gtk.ResponseType.OK)
113                                 close = true;
114                 }
115         }
116 }