[ease/plugins: 6/7] Added locking and error handling.



commit b03ebcf23d4e0970f746f11a72df5944760d7cc3
Author: Nate Stedman <natesm gmail com>
Date:   Fri Jun 18 06:52:36 2010 -0400

    Added locking and error handling.

 plugins/oca-dialog.vala            |   32 ------------------------
 src/ease-plugin-import-dialog.vala |   47 ++++++++++++++++++++++++++---------
 src/ease-plugin-import-image.vala  |    2 +-
 3 files changed, 36 insertions(+), 45 deletions(-)
---
diff --git a/plugins/oca-dialog.vala b/plugins/oca-dialog.vala
index 72f5b3d..7767af2 100644
--- a/plugins/oca-dialog.vala
+++ b/plugins/oca-dialog.vala
@@ -98,38 +98,6 @@ public class OCA.Dialog : Ease.PluginImportDialog
 			}
 		}
 	}
-	
-	private enum Column
-	{
-		PIXBUF = 0,
-		TEXT = 1,
-		OCA_IMAGE = 2
-	}
-	
-	private Gdk.Pixbuf? gdk_pixbuf_from_uri (string uri) {
-
-		File file = File.new_for_uri (uri);
-		FileInputStream filestream;
-		try {
-			filestream = file.read (null);
-		} catch (Error e) {
-			filestream = null;
-			error ("Couldn't read distant file : %s", e.message);
-		}
-		assert (filestream != null);
-		Gdk.Pixbuf pix;
-		try {
-			pix = new Gdk.Pixbuf.from_stream_at_scale (filestream,
-														   200,
-														   200,
-														   true,
-														   null);
-		} catch (Error e) {
-			error ("Couldn't create pixbuf from file: %s", e.message);
-			pix = null;
-		}
-		return pix;
-	}
 }
 
 public static int main(string[] args)
diff --git a/src/ease-plugin-import-dialog.vala b/src/ease-plugin-import-dialog.vala
index 61ef2d7..60dfcfd 100644
--- a/src/ease-plugin-import-dialog.vala
+++ b/src/ease-plugin-import-dialog.vala
@@ -106,7 +106,8 @@ public abstract class Ease.PluginImportDialog : Gtk.Dialog
 			progress_align.show_all();
 			
 			// run the call
-			call.run_async(on_call_finish, this);
+			try { call.run_async(on_call_finish, this); }
+			catch (Error e) { error(e.message); }
 		});
 		
 		// progress
@@ -156,13 +157,23 @@ public abstract class Ease.PluginImportDialog : Gtk.Dialog
 		icons.text_column = Column.TEXT;
 		icons.pixbuf_column = Column.PIXBUF;
 		
-		Thread.create(threaded_get_pixbufs, false);
+		// if threads are supported, get the pixbufs in a thread
+		if (Thread.supported())
+		{
+			try { Thread.create(threaded_get_pixbufs, false); }
+			catch { threaded_get_pixbufs(); }
+		}
+		else
+		{
+			threaded_get_pixbufs();
+		}
 	}
 	
 	private void* threaded_get_pixbufs()
 	{
 		// get the next image
-		var image = images_list.poll_head();
+		PluginImportImage image;
+		lock (images_list) { image = images_list.poll_head(); }
 		
 		// get the pixbuf for this image
 		var pixbuf = gdk_pixbuf_from_uri(image.thumb_link == null ?
@@ -171,20 +182,32 @@ public abstract class Ease.PluginImportDialog : Gtk.Dialog
 		
 		// append to the model
 		var tree_itr = Gtk.TreeIter();
-		model.append(out tree_itr);
-		model.set(tree_itr, Column.PIXBUF, pixbuf,
-		                    Column.TEXT, image.title);
+		lock (model)
+		{
+			model.append(out tree_itr);
+			model.set(tree_itr, Column.PIXBUF, pixbuf,
+				                Column.TEXT, image.title);
+		}
 		
 		// set the progress bar
-		progress.set_fraction(1 - (images_list.size / list_size));
-		
+		lock (progress)
+		{
+			progress.set_fraction(1 - (images_list.size / list_size));
+		}
+			
 		// continue if there are more images
-		if (images_list.size > 0) threaded_get_pixbufs();
-		
+		lock (images_list)
+		{
+			if (images_list.size > 0) threaded_get_pixbufs();
+		}
+			
 		// otherwise, remove the progress bar and return
-		if (progress_align.get_parent() == main_vbox)
+		lock (main_vbox)
 		{
-			main_vbox.remove(progress_align);
+			if (progress_align.get_parent() == main_vbox)
+			{
+				main_vbox.remove(progress_align);
+			}
 		}
 		return null;
 	}
diff --git a/src/ease-plugin-import-image.vala b/src/ease-plugin-import-image.vala
index d1037da..27d8aa6 100644
--- a/src/ease-plugin-import-image.vala
+++ b/src/ease-plugin-import-image.vala
@@ -1,4 +1,4 @@
-public class Ease.PluginImportImage
+public class Ease.PluginImportImage : GLib.Object
 {
 	public string title;
 	public string file_link;



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