[Gtk-sharp-list] New Sample: sample/GnomeVfsTest.cs

Tamara Tamara <foxxygirltamara@gmail.com>
Mon, 6 Dec 2004 03:14:49 -0800


------=_Part_3386_18609795.1102331689401
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I was busy not studying for finals and looking at sample/TestVfs.cs by
Jeroen Zwartepoorte and noticed that it was badly broken (note the
patch I posted). So I decided to gut it and rework it.  I have tried
to follow the mcs coding style but it was not what I wrote it in
originally (and it was my first attempt to meet the style) so it may
not follow the style perfectly.

I replaced Jeroen's name with my own because the differences were so
great it's not really the same piece of software anymore. Putting both
names on the file would sound like there was collaboration. I didn't
collaborate with anyone, I just threw this together in a couple hours
while working on a half-dozen other things at once. I'm not sure what
the right thing to do here would be.

Anyway, so here's my program. It's loosely based on sample/TestVfs.cs
but I gave myself full credit for it. Feel free to change the authors
line if you wish (although I'd like to keep my name in it :-).

-- Tamara

------=_Part_3386_18609795.1102331689401
Content-Type: text/x-csharp; name="GnomeVfsTest.cs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="GnomeVfsTest.cs"

//
// TestVfs.cs: Test for Gnome.Vfs bindings.
//
// Author:
//        Tamara Roberson (foxxygirltamara@gmail.com)
//
// (C) 2004 Tamara Roberson
//

using System;
using System.Text;
using System.IO;

class FileDialog : Gtk.FileChooserDialog {

        public FileDialog (string title, Gtk.FileChooserAction action)=20
        : base (title, null, action, "gnome-vfs")
        {
                this.LocalOnly =3D false;

                this.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
                this.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok);

                this.DefaultResponse =3D Gtk.ResponseType.Ok;
        }

        public Gnome.Vfs.Uri GetUri ()
        {
                int resp =3D this.Run ();
               =20
                this.Hide ();
                       =20
                if (resp !=3D (int) Gtk.ResponseType.Ok)
                        throw new Exception ("No File Selected.");

                return new Gnome.Vfs.Uri (this.Uri);
        }


        public static Gnome.Vfs.Uri OpenFile (string title)
        {
                FileDialog fd =3D new FileDialog (title, Gtk.FileChooserAct=
ion.Open);
                return fd.GetUri ();
        }

        public static Gnome.Vfs.Uri SaveFile(string title)
        {
                FileDialog fd =3D new FileDialog (title, Gtk.FileChooserAct=
ion.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 =3D FileDialog.OpenFile ("Show File Info"=
);
                Console.WriteLine ("Selected uri\t=3D {0}", uri.ToString ()=
);
               =20
                // MimeType
                string mimeType =3D Gnome.Vfs.MimeType.GetMimeTypeForUri (u=
ri.ToString ());
                Console.WriteLine ("Mimetype\t=3D {0}", mimeType);
       =20
                // IsLocal
                Gnome.Vfs.FileInfoOptions options =3D Gnome.Vfs.FileInfoOpt=
ions.Default;
                Gnome.Vfs.FileInfo info =3D new Gnome.Vfs.FileInfo (uri.ToS=
tring (), options);
               =20
                Console.WriteLine ("IsLocal\t\t=3D {0}", info.IsLocal);
        }
       =20
        private void WriteFile ()
        {
                // Ask for a file
                Gnome.Vfs.Uri uri =3D FileDialog.SaveFile ("Write to File")=
;
       =20
                // Create Stream
                Gnome.Vfs.VfsStream vs =3D new Gnome.Vfs.VfsStream (uri.ToS=
tring (), FileMode.CreateNew);
       =20
                // Write to the file
                UTF8Encoding utf8 =3D new UTF8Encoding ();
                byte [] buf =3D utf8.GetBytes ("Testing 1 2 3, asdjfaskjdhf=
kajshdf");
                vs.Write (buf, 0, buf.Length);
       =20
                // Close the file
                vs.Close();
        }
       =20
        private void ReadFile()
        {
                // Ask for a file
                Gnome.Vfs.Uri uri =3D FileDialog.OpenFile ("Read File");
       =20
                // Create Stream
                Gnome.Vfs.VfsStream vs =3D new Gnome.Vfs.VfsStream (uri.ToS=
tring (), FileMode.Open);
       =20
                // Read File byte by byte
                while (true) {
                        int c =3D vs.ReadByte ();
       =20
                        if (c < 0) {
                                Console.WriteLine ("EOF");
                                break;
                        }
       =20
                        Console.Write ((char) c);
                }
       =20
                // Close File
                vs.Close ();
        }
       =20
        private void OpenAsync ()
        {
                // Ask for a file
                Gnome.Vfs.Uri uri =3D FileDialog.OpenFile ("Open File Async=
hronously");
       =20
                // Open the file Asynchronously
                Gnome.Vfs.AsyncCallback openCallback =3D new Gnome.Vfs.Asyn=
cCallback (OnUriOpen);
                Gnome.Vfs.Async.Open (uri.ToString (), Gnome.Vfs.OpenMode.R=
ead, 0, openCallback);
        }
       =20
        private void CreateAsync ()
        {
                // Ask for a file
                Gnome.Vfs.Uri uri =3D FileDialog.SaveFile ("Create File Asy=
nchronously");
               =20
                // Create a file Asynchronously
                Gnome.Vfs.FilePermissions perms =3D
                        Gnome.Vfs.FilePermissions.UserRead  |
                        Gnome.Vfs.FilePermissions.UserWrite |
                        Gnome.Vfs.FilePermissions.GroupRead |
                        Gnome.Vfs.FilePermissions.OtherRead;
                               =20
                Gnome.Vfs.AsyncCallback createCallback =3D new Gnome.Vfs.As=
yncCallback (OnUriCreate);
                Gnome.Vfs.Async.Create (uri, Gnome.Vfs.OpenMode.Write, fals=
e, perms, 0, createCallback);
        }
       =20
        static void OnUriOpen (Gnome.Vfs.Handle handle, Gnome.Vfs.Result re=
sult)
        {
                Console.WriteLine ("Async.Open result\t=3D {0} ({1})", Gnom=
e.Vfs.Vfs.ResultToString (result), result);
       =20
                Gnome.Vfs.AsyncReadCallback readCallback =3D new Gnome.Vfs.=
AsyncReadCallback (OnUriRead);

                byte [] buffer =3D new byte [128];
                Gnome.Vfs.Async.Read (handle, out buffer [0], 128, readCall=
back);
        }
               =20
        public static void OnUriRead (Gnome.Vfs.Handle handle, Gnome.Vfs.Re=
sult result,=20
                                      byte [] buffer, ulong bytesRequested,=
 ulong bytesRead)
        {
                Console.WriteLine ("Async.Read result\t=3D {0} ({1})", Gnom=
e.Vfs.Vfs.ResultToString(result), result);
       =20
                if (result !=3D Gnome.Vfs.Result.Ok)
                        return;

                Console.WriteLine ("bytesRequested\t\t=3D {0}", bytesReques=
ted);
                Console.WriteLine ("bytesRead\t\t=3D {0}", bytesRead);

                Console.Write("bytes\t\t\t=3D ");
                for (int i =3D 0; i < (int) bytesRead; i++)
                        Console.Write ((char) buffer [i]);
                Console.WriteLine ();

                Gnome.Vfs.AsyncReadCallback readCallback =3D new Gnome.Vfs.=
AsyncReadCallback (OnUriRead);

                byte [] buf =3D new byte [128];
                Gnome.Vfs.Async.Read (handle, out buf [0], 128, readCallbac=
k);
        }
               =20
        public void OnUriCreate (Gnome.Vfs.Handle handle, Gnome.Vfs.Result =
result)
        {
                Console.WriteLine ("Async.Create result\t=3D {0} ({1})", Gn=
ome.Vfs.Vfs.ResultToString (result), result);
                       =20
                if (result !=3D Gnome.Vfs.Result.Ok)
                        return;
                UTF8Encoding utf8 =3D new UTF8Encoding ();
                byte [] buffer =3D utf8.GetBytes ("Testing 1 2 3 asdlfjalsj=
dfksjdf \nGustavo Gir=C3=A1ldez\n");
                Gnome.Vfs.AsyncWriteCallback writeCallback =3D new Gnome.Vf=
s.AsyncWriteCallback (OnUriWrite);
                Gnome.Vfs.Async.Write (handle, out buffer [0], (uint) buffe=
r.Length, writeCallback);
        }
               =20
        public static void OnUriWrite (Gnome.Vfs.Handle handle, Gnome.Vfs.R=
esult result,=20
                                       byte [] buffer, ulong bytesRequested=
, ulong bytesWritten)
        {
                Console.WriteLine ("Async.Write result\t=3D {0} ({1})", Gno=
me.Vfs.Vfs.ResultToString (result), result);
        }
       =20
       =20
        static void Main (string [] args)
        {
                // Initialize Gtk
                Gtk.Application.Init ();

                new TestVfs ();

                // Run!                       =20
                Gtk.Application.Run ();
        }       =20
}

------=_Part_3386_18609795.1102331689401--