From aca1b3ce98e06ea8c21b899b4ff81571ec519e00 Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Wed, 8 Dec 2004 08:47:18 +0000 Subject: [PATCH] 2004-12-08 Jeroen Zwartepoorte * sample/Makefile.am: Readded TestVfs.cs back and make it conditional. * sample/TestVfs.cs: New sample contributed by Tamara Roberson. [Fixes #70262] svn path=/trunk/gtk-sharp/; revision=37374 --- ChangeLog | 6 + sample/Makefile.am | 14 ++- sample/TestVfs.cs | 295 ++++++++++++++++++++++++++++++--------------- 3 files changed, 213 insertions(+), 102 deletions(-) diff --git a/ChangeLog b/ChangeLog index 998d79b46..e3e714a36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-12-08 Jeroen Zwartepoorte + + * sample/Makefile.am: Readded TestVfs.cs back and make it conditional. + * sample/TestVfs.cs: New sample contributed by Tamara Roberson. + [Fixes #70262] + 2004-12-07 Mike Kestner * gtk/glue/style.c : add missing method and prototype. [Fixes #70216] diff --git a/sample/Makefile.am b/sample/Makefile.am index 8329a7ac2..20d0d2f04 100755 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -8,6 +8,14 @@ GNOME_TARGETS= GNOME_ASSEMBLY= endif +if ENABLE_GNOMEVFS +GNOMEVFS_TARGETS=testvfs.exe +GNOMEVFS_ASSEMBLY=../gnomevfs/gnome-vfs-sharp.dll +else +GNOMEVFS_TARGETS= +GNOMEVFS_ASSEMBLY= +endif + if ENABLE_GLADE GLADE_TARGETS=glade-viewer.exe glade-test.exe GLADE_ASSEMBLY=../glade/glade-sharp.dll @@ -24,9 +32,9 @@ VTE_TARGETS= VTE_ASSEMBLY= endif -TARGETS = custom-notebook.exe drawing-sample.exe custom-widget.exe custom-cellrenderer.exe gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe size.exe scribble.exe scribble-xinput.exe treeviewdemo.exe managedtreeviewdemo.exe testdnd.exe actions.exe $(GNOME_TARGETS) $(GLADE_TARGETS) $(VTE_TARGETS) +TARGETS = custom-notebook.exe drawing-sample.exe custom-widget.exe custom-cellrenderer.exe gtk-hello-world.exe button.exe calendar.exe subclass.exe menu.exe size.exe scribble.exe scribble-xinput.exe treeviewdemo.exe managedtreeviewdemo.exe testdnd.exe actions.exe $(GNOMEVFS_TARGETS) $(GNOME_TARGETS) $(GLADE_TARGETS) $(VTE_TARGETS) -assemblies=../glib/glib-sharp.dll ../pango/pango-sharp.dll ../atk/atk-sharp.dll ../gdk/gdk-sharp.dll ../gtk/gtk-sharp.dll ../gnomevfs/gnome-vfs-sharp.dll $(GNOME_ASSEMBLY) $(GLADE_ASSEMBLY) $(VTE_ASSEMBLY) +assemblies=../glib/glib-sharp.dll ../pango/pango-sharp.dll ../atk/atk-sharp.dll ../gdk/gdk-sharp.dll ../gtk/gtk-sharp.dll $(GNOMEVFS_ASSEMBLY) $(GNOME_ASSEMBLY) $(GLADE_ASSEMBLY) $(VTE_ASSEMBLY) references=$(addprefix /r:, $(assemblies)) noinst_SCRIPTS = $(TARGETS) @@ -104,7 +112,7 @@ actions.exe: $(srcdir)/Actions.cs custom-notebook.exe: $(srcdir)/CustomNotebook.cs $(assemblies) $(CSC) /debug /out:custom-notebook.exe $(references) $(srcdir)/CustomNotebook.cs -testvfs.exe: $(srcdir)/TestVfs.cs +testvfs.exe: $(srcdir)/TestVfs.cs $(assemblies) $(CSC) /debug /out:testvfs.exe $(references) $(srcdir)/TestVfs.cs EXTRA_DIST = \ diff --git a/sample/TestVfs.cs b/sample/TestVfs.cs index 4fb4bc44c..9ac4c5ddd 100644 --- a/sample/TestVfs.cs +++ b/sample/TestVfs.cs @@ -1,114 +1,211 @@ // -// TestVfs.cs: Test gnome-vfs bindings. +// TestVfs.cs: Test for Gnome.Vfs bindings. // // Author: -// Jeroen Zwartepoorte +// Tamara Roberson (foxxygirltamara@gmail.com) // -// (C) Copyright Jeroen Zwartepoorte 2004 +// (C) 2004 Tamara Roberson // using System; using System.Text; -using GLib; -using Gtk; -using Gnome.Vfs; +using System.IO; -public class TestVfs { - static void Main (string[] args) - { - Application.Init (); - - FileChooserDialog fcd = new FileChooserDialog ("Open File", - new Window ("test"), - FileChooserAction.Open, - "gnome-vfs"); - fcd.LocalOnly = false; - fcd.AddButton (Stock.Cancel, ResponseType.Cancel); - fcd.AddButton (Stock.Open, ResponseType.Ok); - fcd.DefaultResponse = ResponseType.Ok; - int resp = fcd.Run (); - fcd.Hide (); - - if (resp != (int)ResponseType.Ok) - return; +class FileDialog : Gtk.FileChooserDialog { - Console.WriteLine ("Selected uri = {0}", fcd.Uri); - string mimetype = Mime.GetMimeType (fcd.Uri); - Console.WriteLine ("Mimetype = {0}", mimetype); + public FileDialog (string title, Gtk.FileChooserAction action) + : base (title, null, action, "gnome-vfs") + { + this.LocalOnly = false; - FileInfoOptions options = FileInfoOptions.Default; - Gnome.Vfs.FileInfo info = new Gnome.Vfs.FileInfo (fcd.Uri, options); - Console.WriteLine ("isLocal = " + info.IsLocal); + this.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel); + this.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok); -#if false - Gnome.Vfs.Uri uri = new Gnome.Vfs.Uri (fcd.Uri); - Console.WriteLine (uri.ToString ()); - - VfsStream vs = new VfsStream (fcd.Uri, FileMode.OpenOrCreate); - UTF8Encoding utf8 = new UTF8Encoding (); - byte[] buf = utf8.GetBytes ("Testing 1 2 3, asdjfaskjdhfkajshdf"); - vs.Write (buf, 0, buf.Length); - /*for (int i = 0; i < 200; i++) { - int c = vs.ReadByte (); - if (c == -1) { - Console.WriteLine ("EOF"); - break; - } - Console.Write ((char)c); - }*/ - vs.Close (); - - //Handle handle = Vfs.Open (fcd.Uri, OpenMode.Read); - - //Vfs.OpenAsync (fcd.Uri, OpenMode.Read, 0, new Gnome.Vfs.AsyncCallback (open_cb)); - - /*FilePermission perms = FilePermission.UserRead | - FilePermission.UserWrite | - FilePermission.GroupRead | - FilePermission.OtherRead; - Vfs.CreateAsync ("file:///home/jeroen/test.txt", OpenMode.Write, - false, perms, 0, new Gnome.Vfs.AsyncCallback (create_cb));*/ -#endif - Application.Run (); - } - - static void open_cb (Handle handle, Result result) - { - Console.WriteLine ("OpenAsync result = {0} ({1})", Vfs.ResultToString (result), result); + this.DefaultResponse = Gtk.ResponseType.Ok; + } - byte[] buffer = new byte[128]; - Vfs.ReadAsync (handle, out buffer[0], 128, new AsyncReadCallback (read_cb)); - } - - static void read_cb (Handle handle, Result result, byte[] buffer, ulong bytes_requested, ulong bytes_read) - { - Console.WriteLine ("ReadAsync result = {0} ({1})", Vfs.ResultToString (result), result); + public Gnome.Vfs.Uri GetUri () + { + int resp = this.Run (); + + this.Hide (); + + if (resp != (int) Gtk.ResponseType.Ok) + throw new Exception ("No File Selected."); - if (result == Result.Ok) { - Console.WriteLine ("bytes_requested = {0}", bytes_requested); - Console.WriteLine ("bytes_read = {0}", bytes_read); - /*Console.WriteLine ("bytes = "); - for (int i = 0; i < (int)bytes_read; i++) - Console.Write ("" + (char)buffer[i]);*/ - - byte[] buf = new byte[128]; - Vfs.ReadAsync (handle, out buf[0], 128, new AsyncReadCallback (read_cb)); - } - } - - static void create_cb (Handle handle, Result result) - { - Console.WriteLine ("CreateAsync result = {0} ({1})", Vfs.ResultToString (result), result); - - if (result == Result.Ok) { - UTF8Encoding utf8 = new UTF8Encoding (); - byte[] buffer = utf8.GetBytes ("Testing 1 2 3 asdlfjalsjdfksjdf \nGustavo Giráldez\n"); - Vfs.WriteAsync (handle, out buffer[0], (uint)buffer.Length, new AsyncWriteCallback (write_cb)); - } - } - - static void write_cb (Handle handle, Result result, byte[] buffer, ulong bytes_requested, ulong bytes_written) - { - Console.WriteLine ("WriteAsync result = {0} ({1})", Vfs.ResultToString (result), result); - } + return new Gnome.Vfs.Uri (this.Uri); + } + + + public static Gnome.Vfs.Uri OpenFile (string title) + { + FileDialog fd = new FileDialog (title, Gtk.FileChooserAction.Open); + return fd.GetUri (); + } + + public static Gnome.Vfs.Uri SaveFile(string title) + { + FileDialog fd = new FileDialog (title, Gtk.FileChooserAction.Save); + return fd.GetUri (); + } +} + + +public class TestVfs +{ + public TestVfs () + { + ShowFileInfo (); + WriteFile (); + ReadFile (); + OpenAsync (); + CreateAsync (); + } + + + private void ShowFileInfo() + { + // Ask for a file + Gnome.Vfs.Uri uri = FileDialog.OpenFile ("Show File Info"); + Console.WriteLine ("Selected uri\t= {0}", uri.ToString ()); + + // MimeType + string mimeType = Gnome.Vfs.MimeType.GetMimeTypeForUri (uri.ToString ()); + Console.WriteLine ("Mimetype\t= {0}", mimeType); + + // IsLocal + Gnome.Vfs.FileInfoOptions options = Gnome.Vfs.FileInfoOptions.Default; + Gnome.Vfs.FileInfo info = new Gnome.Vfs.FileInfo (uri.ToString (), options); + + Console.WriteLine ("IsLocal\t\t= {0}", info.IsLocal); + } + + private void WriteFile () + { + // Ask for a file + Gnome.Vfs.Uri uri = FileDialog.SaveFile ("Write to File"); + + // Create Stream + Gnome.Vfs.VfsStream vs = new Gnome.Vfs.VfsStream (uri.ToString (), FileMode.CreateNew); + + // Write to the file + UTF8Encoding utf8 = new UTF8Encoding (); + byte [] buf = utf8.GetBytes ("Testing 1 2 3, asdjfaskjdhfkajshdf"); + vs.Write (buf, 0, buf.Length); + + // Close the file + vs.Close(); + } + + private void ReadFile() + { + // Ask for a file + Gnome.Vfs.Uri uri = FileDialog.OpenFile ("Read File"); + + // Create Stream + Gnome.Vfs.VfsStream vs = new Gnome.Vfs.VfsStream (uri.ToString (), FileMode.Open); + + // Read File byte by byte + while (true) { + int c = vs.ReadByte (); + + if (c < 0) { + Console.WriteLine ("EOF"); + break; + } + + Console.Write ((char) c); + } + + // Close File + vs.Close (); + } + + private void OpenAsync () + { + // Ask for a file + Gnome.Vfs.Uri uri = FileDialog.OpenFile ("Open File Asynchronously"); + + // Open the file Asynchronously + Gnome.Vfs.AsyncCallback openCallback = new Gnome.Vfs.AsyncCallback (OnUriOpen); + Gnome.Vfs.Async.Open (uri.ToString (), Gnome.Vfs.OpenMode.Read, 0, openCallback); + } + + private void CreateAsync () + { + // Ask for a file + Gnome.Vfs.Uri uri = FileDialog.SaveFile ("Create File Asynchronously"); + + // Create a file Asynchronously + Gnome.Vfs.FilePermissions perms = + Gnome.Vfs.FilePermissions.UserRead | + Gnome.Vfs.FilePermissions.UserWrite | + Gnome.Vfs.FilePermissions.GroupRead | + Gnome.Vfs.FilePermissions.OtherRead; + + Gnome.Vfs.AsyncCallback createCallback = new Gnome.Vfs.AsyncCallback (OnUriCreate); + Gnome.Vfs.Async.Create (uri, Gnome.Vfs.OpenMode.Write, false, perms, 0, createCallback); + } + + static void OnUriOpen (Gnome.Vfs.Handle handle, Gnome.Vfs.Result result) + { + Console.WriteLine ("Async.Open result\t= {0} ({1})", Gnome.Vfs.Vfs.ResultToString (result), result); + + Gnome.Vfs.AsyncReadCallback readCallback = new Gnome.Vfs.AsyncReadCallback (OnUriRead); + + byte [] buffer = new byte [128]; + Gnome.Vfs.Async.Read (handle, out buffer [0], 128, readCallback); + } + + public static void OnUriRead (Gnome.Vfs.Handle handle, Gnome.Vfs.Result result, + byte [] buffer, ulong bytesRequested, ulong bytesRead) + { + Console.WriteLine ("Async.Read result\t= {0} ({1})", Gnome.Vfs.Vfs.ResultToString(result), result); + + if (result != Gnome.Vfs.Result.Ok) + return; + + Console.WriteLine ("bytesRequested\t\t= {0}", bytesRequested); + Console.WriteLine ("bytesRead\t\t= {0}", bytesRead); + + Console.Write("bytes\t\t\t= "); + for (int i = 0; i < (int) bytesRead; i++) + Console.Write ((char) buffer [i]); + Console.WriteLine (); + + Gnome.Vfs.AsyncReadCallback readCallback = new Gnome.Vfs.AsyncReadCallback (OnUriRead); + + byte [] buf = new byte [128]; + Gnome.Vfs.Async.Read (handle, out buf [0], 128, readCallback); + } + + public void OnUriCreate (Gnome.Vfs.Handle handle, Gnome.Vfs.Result result) + { + Console.WriteLine ("Async.Create result\t= {0} ({1})", Gnome.Vfs.Vfs.ResultToString (result), result); + + if (result != Gnome.Vfs.Result.Ok) + return; + UTF8Encoding utf8 = new UTF8Encoding (); + byte [] buffer = utf8.GetBytes ("Testing 1 2 3 asdlfjalsjdfksjdf \nGustavo Giráldez\n"); + Gnome.Vfs.AsyncWriteCallback writeCallback = new Gnome.Vfs.AsyncWriteCallback (OnUriWrite); + Gnome.Vfs.Async.Write (handle, out buffer [0], (uint) buffer.Length, writeCallback); + } + + public static void OnUriWrite (Gnome.Vfs.Handle handle, Gnome.Vfs.Result result, + byte [] buffer, ulong bytesRequested, ulong bytesWritten) + { + Console.WriteLine ("Async.Write result\t= {0} ({1})", Gnome.Vfs.Vfs.ResultToString (result), result); + } + + + static void Main (string [] args) + { + // Initialize Gtk + Gtk.Application.Init (); + + new TestVfs (); + + // Run! + Gtk.Application.Run (); + } }