Adding first code drop
[demorecorder] / src / ProjectSettingsDialog.vala
diff --git a/src/ProjectSettingsDialog.vala b/src/ProjectSettingsDialog.vala
new file mode 100644 (file)
index 0000000..a8245c7
--- /dev/null
@@ -0,0 +1,166 @@
+/*  Demo Recorder for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+namespace IdWorks {
+
+public class ProjectSettingsDialog : Hildon.Dialog {
+
+  Hildon.Entry name_entry;
+  Hildon.Button directory_btn;
+  Hildon.Button filename_btn;
+  string root_folder;
+  bool user_set;
+  
+  public string get_project_name() {
+    return name_entry.get_text();
+  }
+  public void set_project_name(string project_name) {
+    name_entry.set_text(project_name);
+    validate();
+  }
+  
+  public string get_project_directory() {
+    return directory_btn.get_value();
+  }
+  public void set_project_directory(string project_directory) {
+    directory_btn.set_value(project_directory);
+    validate();
+  }
+  
+  public string get_project_filename() {
+    return filename_btn.get_value();
+  }
+  public void set_project_filename(string project_filename) {
+    filename_btn.set_value(project_filename);
+    validate();
+  }
+  
+  public ProjectSettingsDialog(Gtk.Widget parent, string title) {
+    this.user_set = false;
+    this.set_parent(parent);
+    this.set_title(title);
+    this.set_default_response(Gtk.ResponseType.CANCEL);
+    construct_interface("", "", "");
+  }
+  public ProjectSettingsDialog.with_values(Gtk.Widget parent, string title, string project_name, string project_directory, string project_filename, string root_folder) {
+    user_set = (0 < project_directory.length && 0 < project_filename.length);
+    this.root_folder = root_folder;
+    this.set_parent(parent);
+    this.set_title(title);
+    this.set_default_response(Gtk.ResponseType.CANCEL);
+    construct_interface(project_name, project_directory, project_filename);
+  }
+  
+  private void construct_interface(string name, string dir, string filename) {
+    this.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK);
+    
+    Gtk.VBox control_area = (Gtk.VBox)this.get_content_area();
+    
+    Gtk.HBox name_bar = new Gtk.HBox(false, 4);
+    
+    Gtk.Label name_label = new Gtk.Label("Name");
+    name_bar.pack_start(name_label, false, false, 2);
+    name_entry = new Hildon.Entry(Hildon.SizeType.HALFSCREEN_WIDTH);
+    name_entry.set_text(name);
+    name_entry.set_placeholder("Project name");
+    name_entry.changed.connect(name_entry_changed);
+    name_bar.pack_start(name_entry, true, true, 2);
+    
+    control_area.pack_start(name_bar, true, true, 4);
+    
+    directory_btn = new Hildon.Button.with_text(Hildon.SizeType.FINGER_HEIGHT | Hildon.SizeType.HALFSCREEN_WIDTH, Hildon.ButtonArrangement.VERTICAL, "Working folder", dir);
+    directory_btn.set_style(Hildon.ButtonStyle.PICKER);
+    directory_btn.set_alignment(0, 0, 1, 1);
+    directory_btn.set_value(dir);
+    directory_btn.clicked.connect(select_directory);
+    
+    control_area.pack_start(directory_btn, true, true, 4);
+    
+    filename_btn = new Hildon.Button.with_text(Hildon.SizeType.FINGER_HEIGHT | Hildon.SizeType.HALFSCREEN_WIDTH, Hildon.ButtonArrangement.VERTICAL, "Settings file", dir);
+    filename_btn.set_style(Hildon.ButtonStyle.PICKER);
+    filename_btn.set_alignment(0, 0, 1, 1);
+    filename_btn.set_value(filename);
+    filename_btn.clicked.connect(select_filename);
+    
+    control_area.pack_start(filename_btn, true, true, 4);
+    
+    validate();
+    
+    this.show_all();
+  }
+  
+  private void name_entry_changed() {
+    if (!user_set) {
+      this.directory_btn.value = root_folder + "/" + get_project_name();
+      this.filename_btn.value = this.directory_btn.value + "/project.xml";
+    }
+    validate();
+  }
+  
+  private void validate() {
+    bool good = true;
+    good &= (0 < get_project_name().length);
+    good &= (0 < get_project_directory().length);
+    good &= (0 < get_project_filename().length);
+    this.set_response_sensitive(Gtk.ResponseType.OK, good);
+  }
+  
+  private void select_directory() {
+    // get a filename
+    Hildon.FileChooserDialog file_chooser = new Hildon.FileChooserDialog(this, Gtk.FileChooserAction.SELECT_FOLDER);
+    file_chooser.set_show_upnp(false);
+    file_chooser.set_safe_folder(this.root_folder);
+    file_chooser.set_current_folder(this.root_folder);
+    if (0 < this.directory_btn.value.length) {
+      // if it exists?
+      //file_chooser.set_current_folder(this.directory_btn.value);
+      // else
+    }
+    Gtk.ResponseType ret = (Gtk.ResponseType) file_chooser.run();
+    if (Gtk.ResponseType.OK == ret) {
+      user_set = true;
+      this.directory_btn.value = file_chooser.get_filename();
+      if ("" == this.filename_btn.value) {
+        this.filename_btn.value = this.directory_btn.value + "/project.xml";
+      }
+    }
+    file_chooser.destroy ();
+    validate();
+  }
+  
+  private void select_filename() {
+    // get a filename
+    Hildon.FileChooserDialog file_chooser = new Hildon.FileChooserDialog(this, Gtk.FileChooserAction.SAVE);
+    file_chooser.set_show_upnp(false);
+    file_chooser.set_current_folder(this.root_folder);
+    file_chooser.set_current_name("project.xml");
+    if (0 < this.filename_btn.value.length) {
+      //file_chooser.set_filename(this.filename_btn.value);
+      file_chooser.set_safe_folder(root_folder);
+      /// TODO // file_chooser.set_current_folder(this.filename_btn.value); 
+    }
+    Gtk.ResponseType ret = (Gtk.ResponseType) file_chooser.run();
+    if (Gtk.ResponseType.OK == ret) {
+      user_set = true;
+      this.filename_btn.value = file_chooser.get_filename();
+    }
+    file_chooser.destroy ();
+    validate();
+  }
+}
+
+}
\ No newline at end of file