Adding first code drop
[demorecorder] / src / ProjectSettingsDialog.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 namespace IdWorks {
19
20 public class ProjectSettingsDialog : Hildon.Dialog {
21
22   Hildon.Entry name_entry;
23   Hildon.Button directory_btn;
24   Hildon.Button filename_btn;
25   string root_folder;
26   bool user_set;
27   
28   public string get_project_name() {
29     return name_entry.get_text();
30   }
31   public void set_project_name(string project_name) {
32     name_entry.set_text(project_name);
33     validate();
34   }
35   
36   public string get_project_directory() {
37     return directory_btn.get_value();
38   }
39   public void set_project_directory(string project_directory) {
40     directory_btn.set_value(project_directory);
41     validate();
42   }
43   
44   public string get_project_filename() {
45     return filename_btn.get_value();
46   }
47   public void set_project_filename(string project_filename) {
48     filename_btn.set_value(project_filename);
49     validate();
50   }
51   
52   public ProjectSettingsDialog(Gtk.Widget parent, string title) {
53     this.user_set = false;
54     this.set_parent(parent);
55     this.set_title(title);
56     this.set_default_response(Gtk.ResponseType.CANCEL);
57     construct_interface("", "", "");
58   }
59   public ProjectSettingsDialog.with_values(Gtk.Widget parent, string title, string project_name, string project_directory, string project_filename, string root_folder) {
60     user_set = (0 < project_directory.length && 0 < project_filename.length);
61     this.root_folder = root_folder;
62     this.set_parent(parent);
63     this.set_title(title);
64     this.set_default_response(Gtk.ResponseType.CANCEL);
65     construct_interface(project_name, project_directory, project_filename);
66   }
67   
68   private void construct_interface(string name, string dir, string filename) {
69     this.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK);
70     
71     Gtk.VBox control_area = (Gtk.VBox)this.get_content_area();
72     
73     Gtk.HBox name_bar = new Gtk.HBox(false, 4);
74     
75     Gtk.Label name_label = new Gtk.Label("Name");
76     name_bar.pack_start(name_label, false, false, 2);
77     name_entry = new Hildon.Entry(Hildon.SizeType.HALFSCREEN_WIDTH);
78     name_entry.set_text(name);
79     name_entry.set_placeholder("Project name");
80     name_entry.changed.connect(name_entry_changed);
81     name_bar.pack_start(name_entry, true, true, 2);
82     
83     control_area.pack_start(name_bar, true, true, 4);
84     
85     directory_btn = new Hildon.Button.with_text(Hildon.SizeType.FINGER_HEIGHT | Hildon.SizeType.HALFSCREEN_WIDTH, Hildon.ButtonArrangement.VERTICAL, "Working folder", dir);
86     directory_btn.set_style(Hildon.ButtonStyle.PICKER);
87     directory_btn.set_alignment(0, 0, 1, 1);
88     directory_btn.set_value(dir);
89     directory_btn.clicked.connect(select_directory);
90     
91     control_area.pack_start(directory_btn, true, true, 4);
92     
93     filename_btn = new Hildon.Button.with_text(Hildon.SizeType.FINGER_HEIGHT | Hildon.SizeType.HALFSCREEN_WIDTH, Hildon.ButtonArrangement.VERTICAL, "Settings file", dir);
94     filename_btn.set_style(Hildon.ButtonStyle.PICKER);
95     filename_btn.set_alignment(0, 0, 1, 1);
96     filename_btn.set_value(filename);
97     filename_btn.clicked.connect(select_filename);
98     
99     control_area.pack_start(filename_btn, true, true, 4);
100     
101     validate();
102     
103     this.show_all();
104   }
105   
106   private void name_entry_changed() {
107     if (!user_set) {
108       this.directory_btn.value = root_folder + "/" + get_project_name();
109       this.filename_btn.value = this.directory_btn.value + "/project.xml";
110     }
111     validate();
112   }
113   
114   private void validate() {
115     bool good = true;
116     good &= (0 < get_project_name().length);
117     good &= (0 < get_project_directory().length);
118     good &= (0 < get_project_filename().length);
119     this.set_response_sensitive(Gtk.ResponseType.OK, good);
120   }
121   
122   private void select_directory() {
123     // get a filename
124     Hildon.FileChooserDialog file_chooser = new Hildon.FileChooserDialog(this, Gtk.FileChooserAction.SELECT_FOLDER);
125     file_chooser.set_show_upnp(false);
126     file_chooser.set_safe_folder(this.root_folder);
127     file_chooser.set_current_folder(this.root_folder);
128     if (0 < this.directory_btn.value.length) {
129       // if it exists?
130       //file_chooser.set_current_folder(this.directory_btn.value);
131       // else
132     }
133     Gtk.ResponseType ret = (Gtk.ResponseType) file_chooser.run();
134     if (Gtk.ResponseType.OK == ret) {
135       user_set = true;
136       this.directory_btn.value = file_chooser.get_filename();
137       if ("" == this.filename_btn.value) {
138         this.filename_btn.value = this.directory_btn.value + "/project.xml";
139       }
140     }
141     file_chooser.destroy ();
142     validate();
143   }
144   
145   private void select_filename() {
146     // get a filename
147     Hildon.FileChooserDialog file_chooser = new Hildon.FileChooserDialog(this, Gtk.FileChooserAction.SAVE);
148     file_chooser.set_show_upnp(false);
149     file_chooser.set_current_folder(this.root_folder);
150     file_chooser.set_current_name("project.xml");
151     if (0 < this.filename_btn.value.length) {
152       //file_chooser.set_filename(this.filename_btn.value);
153       file_chooser.set_safe_folder(root_folder);
154       /// TODO // file_chooser.set_current_folder(this.filename_btn.value); 
155     }
156     Gtk.ResponseType ret = (Gtk.ResponseType) file_chooser.run();
157     if (Gtk.ResponseType.OK == ret) {
158       user_set = true;
159       this.filename_btn.value = file_chooser.get_filename();
160     }
161     file_chooser.destroy ();
162     validate();
163   }
164 }
165
166 }