[model-examples] Add GTK viewer to filesystem example



commit f29178259b6021336790a283b9077c61355c696e
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Mar 12 04:43:04 2010 -0500

    Add GTK viewer to filesystem example

 configure.ac                        |    2 +
 examples/filesystem/.gitignore      |    1 +
 examples/filesystem/Makefile.am     |   12 +++++--
 examples/filesystem/fs.vala         |   15 +++++++--
 examples/filesystem/fsview-gtk.vala |   60 +++++++++++++++++++++++++++++++++++
 examples/filesystem/fsview.vala     |    3 +-
 6 files changed, 85 insertions(+), 8 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 1235e39..90c5fe0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,6 +6,8 @@ AC_PROG_CC
 
 PKG_CHECK_MODULES(gio, gio-2.0)
 PKG_CHECK_MODULES(model, model)
+PKG_CHECK_MODULES(gtk, gtk+-2.0)
+PKG_CHECK_MODULES(modelgtk, model-gtk)
 
 AC_OUTPUT([
   examples/filesystem/Makefile
diff --git a/examples/filesystem/.gitignore b/examples/filesystem/.gitignore
index b37d16e..8bf4c35 100644
--- a/examples/filesystem/.gitignore
+++ b/examples/filesystem/.gitignore
@@ -1,3 +1,4 @@
 fsview
 *.stamp
 *.c
+fsview-gtk
diff --git a/examples/filesystem/Makefile.am b/examples/filesystem/Makefile.am
index 3ceb37c..666fa67 100644
--- a/examples/filesystem/Makefile.am
+++ b/examples/filesystem/Makefile.am
@@ -1,7 +1,11 @@
-noinst_PROGRAMS = fsview
+noinst_PROGRAMS = fsview fsview-gtk
+
 fsview_SOURCES = fs.vala fsview.vala view.vala
+fsview_gtk_SOURCES = fs.vala fsview-gtk.vala
 
-VALAFLAGS = --pkg=model --pkg=posix --pkg=gio-2.0
-AM_CFLAGS = $(gio_CFLAGS) $(model_CFLAGS)
+VALAFLAGS = --pkg=posix --pkg=gio-2.0 --pkg=model-gtk
+AM_CFLAGS = $(gio_CFLAGS) $(model_CFLAGS) $(modelgtk_CFLAGS) $(gtk_CFLAGS)
 CFLAGS += -Wno-error
-AM_LDFLAGS = $(gio_LIBS) $(model_LIBS)
+
+fsview_gtk_LDFLAGS = $(gio_LIBS) $(model_LIBS) $(modelgtk_LIBS)
+fsview_LDFLAGS = $(gio_LIBS) $(model_LIBS)
diff --git a/examples/filesystem/fs.vala b/examples/filesystem/fs.vala
index a66f9b3..d6817a3 100644
--- a/examples/filesystem/fs.vala
+++ b/examples/filesystem/fs.vala
@@ -1,6 +1,6 @@
 namespace FS {
   public class File : Model.SimpleDictionary {
-    protected string name;
+    internal string name;
     FileMonitor? monitor;
     GLib.File file;
     ulong watch_id;
@@ -27,6 +27,12 @@ namespace FS {
         set_integer ("size", (int) info.get_size (), true);
         set_string ("type", info.get_content_type (), true);
         set_string ("user", info.get_attribute_string ("owner::user"), true);
+
+        if (info.get_content_type () == "inode/directory") {
+          set ("contents", new Directory (file), true);
+        } else {
+          set ("contents", null, true);
+        }
       } catch (Error e) {
         /* file probably just disappeared in the meantime.  do nothing */
       }
@@ -44,6 +50,7 @@ namespace FS {
       this.monitor = monitor;
 
       set_string ("name", name, false);
+      set ("contents", null);
       set ("size", null);
       set ("type", null);
       set ("user", null);
@@ -108,7 +115,7 @@ namespace FS {
           yield file.enumerate_children_async ("standard::name", 0, 0, null);
 
         do {
-          var files = yield enumerator.next_files_async (5, 0, null);
+          var files = yield enumerator.next_files_async (2000, 0, null);
           names = new string[0];
 
           foreach (var file in files) {
@@ -116,7 +123,7 @@ namespace FS {
           }
 
           Posix.qsort ((void *) names, names.length, sizeof (void *),
-                       compare_strings);
+                       (Posix.compar_fn_t) compare_strings);
 
           merge ("i", (Model.AbstractSortedList.ConstKey[]) names);
         } while (names.length == 5);
@@ -147,6 +154,8 @@ namespace FS {
     }
 
     public Directory (GLib.File file) {
+      Object ();
+
       this.file = file;
 
       try {
diff --git a/examples/filesystem/fsview-gtk.vala b/examples/filesystem/fsview-gtk.vala
new file mode 100644
index 0000000..ae86027
--- /dev/null
+++ b/examples/filesystem/fsview-gtk.vala
@@ -0,0 +1,60 @@
+class App {
+  MainLoop main_loop;
+
+  bool window_destroyed (Gdk.Event event) {
+    main_loop.quit ();
+    return false;
+  }
+
+  void set_integer (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
+        int val;
+        model.get (iter, 3, out val, -1);
+        cell.set ("text", val.to_string ());
+  }
+
+  internal void run (File location) {
+    string[] keys = { "contents", "name", "user", "size", "type" };
+    Type[] types = { typeof (Model.List),
+                     typeof (string), typeof (string),
+                     typeof (int), typeof (string) };
+    var d = new FS.Directory (location);
+    var v = new Gtk.TreeView ();
+    var s = new Gtk.ScrolledWindow (null, null);
+    var w = new Gtk.Window (Gtk.WindowType.TOPLEVEL);
+
+    v.set_model (new ModelGtk.TreeModel (d, 5, keys, types, false));
+    v.insert_column_with_attributes (0, "Name", new Gtk.CellRendererText (),
+                                     "text", 1);
+    v.insert_column_with_attributes (1, "User", new Gtk.CellRendererText (),
+                                     "text", 2);
+    v.insert_column_with_data_func (1, "Size", new Gtk.CellRendererText (),
+                                    set_integer);
+    v.insert_column_with_attributes (1, "Type", new Gtk.CellRendererText (),
+                                     "text", 4);
+    s.add (v);
+    w.add (s);
+
+    w.destroy_event.connect (window_destroyed);
+    w.set_default_size (800, 600);
+    w.show_all ();
+
+    main_loop = new MainLoop (null, false);
+    main_loop.run ();
+  }
+}
+
+void main (string[] args) {
+  var app = new App ();
+
+  Gtk.init (ref args);
+
+  if (args.length == 2 && args[1] != "-uri") {
+    app.run (File.new_for_path (args[1]));
+  } else if (args.length == 3 && args[1] == "--") {
+    app.run (File.new_for_path (args[2]));
+  } else if (args.length == 3 && args[1] == "-uri") {
+    app.run (File.new_for_uri (args[2]));
+  } else {
+    print ("usage: %s file\n       %s -uri uri\n", args[0], args[0]);
+  }
+}
diff --git a/examples/filesystem/fsview.vala b/examples/filesystem/fsview.vala
index 669de94..56400b8 100644
--- a/examples/filesystem/fsview.vala
+++ b/examples/filesystem/fsview.vala
@@ -7,7 +7,7 @@ class App {
     return false;
   }
 
-  protected void run (File location) {
+  internal void run (File location) {
     var d = new FS.Directory (location);
     var l = new ListView (d, new string[] { "name", "size", "user", "type" });
     t = new Toplevel (l);
@@ -19,6 +19,7 @@ class App {
     main_loop.run ();
   }
 }
+
 void main (string[] args) {
   var app = new App ();
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]