From 55aed282667f82b0202fcfe698323cabca12da96 Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Tue, 2 Nov 2004 19:17:05 +0000 Subject: [PATCH] 2004-11-02 Jeroen Zwartepoorte * gnomevfs/AsyncDirectoryLoadCallback.cs: * gnomevfs/AsyncDirectoryLoadCallbackNative.cs: * gnomevfs/Directory.cs: Implement asynchronous directory loading. * gnomevfs/FileInfo.cs: Clear the FileInfoNative struct in the destructor. * gnomevfs/Makefile.am: Add new callback files. * sample/gnomevfs/TestDirectory.cs: Add async test. svn path=/trunk/gtk-sharp/; revision=35571 --- ChangeLog | 10 +++++ gnomevfs/AsyncDirectoryLoadCallback.cs | 14 +++++++ gnomevfs/AsyncDirectoryLoadCallbackNative.cs | 37 ++++++++++++++++++ gnomevfs/Directory.cs | 13 +++++++ gnomevfs/FileInfo.cs | 12 +++++- gnomevfs/Makefile.am | 40 ++++++++++---------- sample/gnomevfs/TestDirectory.cs | 33 +++++++++++++++- 7 files changed, 136 insertions(+), 23 deletions(-) create mode 100644 gnomevfs/AsyncDirectoryLoadCallback.cs create mode 100644 gnomevfs/AsyncDirectoryLoadCallbackNative.cs diff --git a/ChangeLog b/ChangeLog index 78fedf06b..b1822b774 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2004-11-02 Jeroen Zwartepoorte + + * gnomevfs/AsyncDirectoryLoadCallback.cs: + * gnomevfs/AsyncDirectoryLoadCallbackNative.cs: + * gnomevfs/Directory.cs: Implement asynchronous directory loading. + * gnomevfs/FileInfo.cs: Clear the FileInfoNative struct in the + destructor. + * gnomevfs/Makefile.am: Add new callback files. + * sample/gnomevfs/TestDirectory.cs: Add async test. + 2004-11-01 Jeroen Zwartepoorte * gnomevfs/Directory.cs: New Create and Delete methods. Free the diff --git a/gnomevfs/AsyncDirectoryLoadCallback.cs b/gnomevfs/AsyncDirectoryLoadCallback.cs new file mode 100644 index 000000000..c29b83742 --- /dev/null +++ b/gnomevfs/AsyncDirectoryLoadCallback.cs @@ -0,0 +1,14 @@ +// +// VfsAsyncDirectoryLoadCallback.cs: GnomeVFSAsyncDirectoryLoadCallback delegate. +// +// Author: +// Jeroen Zwartepoorte +// +// (C) Copyright Jeroen Zwartepoorte 2004 +// + +using System; + +namespace Gnome.Vfs { + public delegate void AsyncDirectoryLoadCallback (Result result, FileInfo[] infos, uint entries_read); +} diff --git a/gnomevfs/AsyncDirectoryLoadCallbackNative.cs b/gnomevfs/AsyncDirectoryLoadCallbackNative.cs new file mode 100644 index 000000000..71589d5d3 --- /dev/null +++ b/gnomevfs/AsyncDirectoryLoadCallbackNative.cs @@ -0,0 +1,37 @@ +// +// VfsAsyncDirectoryLoadCallbackNative.cs: Utility class for accessing gnome-vfs methods. +// +// Author: +// Jeroen Zwartepoorte +// +// (C) Copyright Jeroen Zwartepoorte 2004 +// + +using System; +using System.Runtime.InteropServices; + +namespace Gnome.Vfs { + internal delegate void AsyncDirectoryLoadCallbackNative (IntPtr handle, Result result, IntPtr list, uint entries_read, IntPtr data); + + internal class AsyncDirectoryLoadCallbackWrapper : GLib.DelegateWrapper { + + public void NativeCallback (IntPtr handle, Result result, IntPtr list, uint entries_read, IntPtr data) + { + GLib.List infos = new GLib.List (list, typeof (FileInfo.FileInfoNative)); + FileInfo[] entries = new FileInfo [infos.Count]; + for (int i = 0; i < infos.Count; i++) + entries[i] = new FileInfo ((FileInfo.FileInfoNative) infos [i]); + + _managed (result, entries, entries_read); + } + + internal AsyncDirectoryLoadCallbackNative NativeDelegate; + protected AsyncDirectoryLoadCallback _managed; + + public AsyncDirectoryLoadCallbackWrapper (AsyncDirectoryLoadCallback managed, object o) : base (o) + { + NativeDelegate = new AsyncDirectoryLoadCallbackNative (NativeCallback); + _managed = managed; + } + } +} diff --git a/gnomevfs/Directory.cs b/gnomevfs/Directory.cs index 075c3b158..4ed4fa03c 100644 --- a/gnomevfs/Directory.cs +++ b/gnomevfs/Directory.cs @@ -49,6 +49,19 @@ namespace Gnome.Vfs { return entries; } + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_load_directory (out IntPtr handle, string uri, FileInfoOptions options, uint items_per_notification, int priority, AsyncDirectoryLoadCallbackNative native, IntPtr data); + + public static void GetEntries (string uri, FileInfoOptions options, + uint itemsPerNotification, int priority, + AsyncDirectoryLoadCallback callback) + { + IntPtr handle = IntPtr.Zero; + AsyncDirectoryLoadCallbackWrapper wrapper = new AsyncDirectoryLoadCallbackWrapper (callback, null); + gnome_vfs_async_load_directory (out handle, uri, options, itemsPerNotification, + priority, wrapper.NativeDelegate, IntPtr.Zero); + } + [DllImport ("gnomevfs-2")] private static extern Result gnome_vfs_make_directory (string uri, uint perm); diff --git a/gnomevfs/FileInfo.cs b/gnomevfs/FileInfo.cs index f5245b9e4..e77970d58 100644 --- a/gnomevfs/FileInfo.cs +++ b/gnomevfs/FileInfo.cs @@ -43,10 +43,10 @@ namespace Gnome.Vfs { private FileInfoNative info; [DllImport ("gnomevfs-2")] - extern static FileInfoNative gnome_vfs_file_info_new (); + private static extern FileInfoNative gnome_vfs_file_info_new (); [DllImport ("gnomevfs-2")] - extern static void gnome_vfs_file_info_copy (ref FileInfoNative dest, ref FileInfoNative src); + private static extern void gnome_vfs_file_info_copy (ref FileInfoNative dest, ref FileInfoNative src); [DllImport ("gnomevfs-2")] private static extern Result gnome_vfs_get_file_info_uri (IntPtr uri, ref FileInfoNative info, FileInfoOptions options); @@ -70,6 +70,14 @@ namespace Gnome.Vfs { Vfs.ThrowException (uri, result); } + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_file_info_clear (ref FileInfoNative info); + + ~FileInfo () + { + gnome_vfs_file_info_clear (ref info); + } + public string Name { get { if (info.name != IntPtr.Zero) diff --git a/gnomevfs/Makefile.am b/gnomevfs/Makefile.am index 0d2814988..35003094e 100644 --- a/gnomevfs/Makefile.am +++ b/gnomevfs/Makefile.am @@ -16,25 +16,27 @@ gapi_DATA = $(API) $(ADDITIONAL_API) CLEANFILES = $(ASSEMBLY) generated-stamp generated/*.cs $(API) glue/generated.c gtk-sharp.snk DISTCLEANFILES = $(ASSEMBLY).config AssemblyInfo.cs gnome-vfs-sharp.pc -sources = \ - Async.cs \ - AsyncCallback.cs \ - AsyncCallbackNative.cs \ - AsyncReadCallback.cs \ - AsyncReadCallbackNative.cs \ - AsyncWriteCallback.cs \ - AsyncWriteCallbackNative.cs \ - Directory.cs \ - FileInfo.cs \ - Handle.cs \ - MimeType.cs \ - Monitor.cs \ - Sync.cs \ - Vfs.cs \ - VfsStream.cs \ - VfsStreamAsyncResult.cs \ - Xfer.cs \ - XferProgressCallback.cs \ +sources = \ + Async.cs \ + AsyncCallback.cs \ + AsyncCallbackNative.cs \ + AsyncDirectoryLoadCallback.cs \ + AsyncDirectoryLoadCallbackNative.cs \ + AsyncReadCallback.cs \ + AsyncReadCallbackNative.cs \ + AsyncWriteCallback.cs \ + AsyncWriteCallbackNative.cs \ + Directory.cs \ + FileInfo.cs \ + Handle.cs \ + MimeType.cs \ + Monitor.cs \ + Sync.cs \ + Vfs.cs \ + VfsStream.cs \ + VfsStreamAsyncResult.cs \ + Xfer.cs \ + XferProgressCallback.cs \ XferProgressCallbackNative.cs build_sources = $(addprefix $(srcdir)/, $(sources)) AssemblyInfo.cs diff --git a/sample/gnomevfs/TestDirectory.cs b/sample/gnomevfs/TestDirectory.cs index 257376c4d..fa8889846 100644 --- a/sample/gnomevfs/TestDirectory.cs +++ b/sample/gnomevfs/TestDirectory.cs @@ -1,9 +1,12 @@ +using GLib; using Gnome.Vfs; using System; using System.Text; namespace Test.Gnome.Vfs { public class TestInfo { + private static MainLoop loop; + static void Main (string[] args) { if (args.Length != 1) { @@ -15,10 +18,36 @@ namespace Test.Gnome.Vfs { FileInfo[] entries = Gnome.Vfs.Directory.GetEntries (args[0]); - foreach (FileInfo info in entries) - Console.WriteLine (info.ToString ()); + Console.WriteLine ("Directory {0} contains {1} entries:", args[0], entries.Length); + foreach (FileInfo info in entries) { + //Console.WriteLine (info.Name); + } + + Gnome.Vfs.Directory.GetEntries (args[0], FileInfoOptions.Default, + 20, (int)Gnome.Vfs.Async.Priority.Default, + new AsyncDirectoryLoadCallback (OnDirectoryLoad)); + + loop = new MainLoop (); + loop.Run (); Gnome.Vfs.Vfs.Shutdown (); } + + private static void OnDirectoryLoad (Result result, FileInfo[] entries, uint entries_read) + { + Console.WriteLine ("DirectoryLoad: {0}", result); + if (result != Result.Ok && result != Result.ErrorEof) { + loop.Quit (); + return; + } + + Console.WriteLine ("read {0} entries", entries_read); + foreach (FileInfo info in entries) { + //Console.WriteLine (info.Name); + } + + if (result == Result.ErrorEof) + loop.Quit (); + } } }