/* project-factory.vala * * Copyright (C) 2008 Nicolas Joseph * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Author: * Nicolas Joseph */ using Gtk; errordomain ProjectError { MAX_OPEN } public class Valide.ProjectFactory : Gtk.TreeView { private static ProjectFactory instance = null; private Project project; private GLib.List projects; private Gtk.TreeStore tree_store; private weak Project current_; public Project current { get { return this.current_; } set { this.current_ = value; this.refresh (); } } private ProjectFactory () { this.width_request = 200; this.headers_visible = false; } construct { this.tree_store = new Gtk.TreeStore (1, typeof (string)); this.set_model (this.tree_store); Gtk.CellRendererText render = new Gtk.CellRendererText (); Gtk.TreeViewColumn column = new Gtk.TreeViewColumn.with_attributes ("Projects", render, "markup", 0, null); this.append_column (column); this.row_activated += this.file_select; } [Callback] protected void file_select (Gtk.TreeView sender, Gtk.TreePath path, Gtk.TreeViewColumn column) { if (path.get_depth () == 2) { Gtk.TreeIter iter; string filename = null; this.tree_store.get_iter (out iter, path); this.tree_store.get (iter, 0, out filename); this.current.open_file (filename); } else { ///@todo Active project } } public void destroy () { this.instance = null; base.destroy (); } public static ProjectFactory get_instance () { if (ProjectFactory.instance == null) { ProjectFactory.instance = new ProjectFactory (); } return ProjectFactory.instance; } public void create (string filename = null) throws GLib.Error { if (current == null) { if (filename == null) { ProjectDialog dialog = new ProjectDialog (); if (dialog.run () == Gtk.ResponseType.OK) { bool create = false; string path = dialog.project_dir; if (!GLib.FileUtils.test (path, GLib.FileTest.EXISTS)) { Gtk.Dialog msg = new Gtk.Dialog.with_buttons ("New project", null, Gtk.DialogFlags.MODAL, Gtk.STOCK_NO, Gtk.ResponseType.NO, Gtk.STOCK_YES, Gtk.ResponseType.YES, null); weak Gtk.Box main_box = (Gtk.Box)msg.vbox; main_box.pack_start (new Gtk.Label ("Projects directory doesn't exist. Would you create it?"), true, true, 10); msg.show_all (); if (msg.run () == Gtk.ResponseType.YES) { GLib.DirUtils.create_with_parents (path, 0755); create = true; } msg.destroy (); } else { create = true; } if (create == true) { path = dialog.project_path; if (GLib.FileUtils.test (path, GLib.FileTest.EXISTS)) { Gtk.Dialog msg = new Gtk.Dialog.with_buttons ("New project", null, Gtk.DialogFlags.MODAL, Gtk.STOCK_NO, Gtk.ResponseType.NO, Gtk.STOCK_YES, Gtk.ResponseType.YES, null); weak Gtk.Box main_box = (Gtk.Box)msg.vbox; main_box.pack_start (new Gtk.Label ("Directory already exist. Would you overwrite it?"), true, true, 10); msg.show_all (); if (msg.run () == Gtk.ResponseType.NO) { create = false; } msg.destroy (); } else { GLib.DirUtils.create (path, 0755); } } if (create == true) { GLib.stdout.printf ("ok\n"); /// @todo Copie du template // this.open_path (filename); } } dialog.destroy (); } if (filename != null) { Project p = new Project ((filename)); this.projects.append (p); this.current = p; } } else { throw new ProjectError.MAX_OPEN ("Maximum project open!"); } } public void open (string filename = null) { Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog ("Open project", null, Gtk.FileChooserAction.OPEN, Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT, null); if (dialog.run () == Gtk.ResponseType.ACCEPT) { this.create (dialog.get_filename ()); } dialog.destroy (); } public void open_path (string! path) { } public bool close_all () { return true; } protected void refresh () { this.tree_store.clear (); foreach (Project p in projects) { string title = null; Gtk.TreeIter root; this.tree_store.append (out root, null); if (p == this.current) { this.tree_store.set (root, 0, "" + p.name + "", -1); } else { this.tree_store.set (root, 0, p.name, -1); } foreach (string file in p.files) { Gtk.TreeIter iter; this.tree_store.append (out iter, root); this.tree_store.set (iter, 0, file, -1); } } } }