[cheese] Camera related actions are now disabled until the pipeline is playing



commit 889948e6f31c017e046701c8d388130aecbdd7da
Author: Yuvaraj Pandian T <yuvipanda gmail com>
Date:   Sat Aug 7 17:42:40 2010 +0530

    Camera related actions are now disabled until the pipeline is playing

 libcheese/cheese-camera.c   |   21 +++++
 libcheese/cheese-camera.h   |    1 +
 src/cheese-window.vala      |  182 ++++++++++++++++++++++++++-----------------
 src/vapi/cheese-common.vapi |    1 +
 4 files changed, 134 insertions(+), 71 deletions(-)
---
diff --git a/libcheese/cheese-camera.c b/libcheese/cheese-camera.c
index 095da2b..9aa51e4 100644
--- a/libcheese/cheese-camera.c
+++ b/libcheese/cheese-camera.c
@@ -106,6 +106,7 @@ enum
   PHOTO_SAVED,
   PHOTO_TAKEN,
   VIDEO_SAVED,
+  STATE_CHANGED,
   LAST_SIGNAL
 };
 
@@ -210,6 +211,19 @@ cheese_camera_bus_message_cb (GstBus *bus, GstMessage *message, CheeseCamera *ca
       cheese_camera_change_sink (camera, priv->video_display_bin,
                                  priv->photo_save_bin, priv->video_save_bin);
       priv->is_recording = FALSE;
+    }  
+  }
+  else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED)
+  {
+    if (strcmp(GST_MESSAGE_SRC_NAME (message), "pipeline") == 0)
+    {
+      GstState old, new;
+      gst_message_parse_state_changed (message, &old, &new, NULL);
+      if (new == GST_STATE_PLAYING)
+      {
+        g_signal_emit (camera, camera_signals[STATE_CHANGED], 0, new);
+      }
+      g_debug ("%d -> %d", old, new);
     }
   }
 }
@@ -1050,6 +1064,13 @@ cheese_camera_class_init (CheeseCameraClass *klass)
                                               g_cclosure_marshal_VOID__VOID,
                                               G_TYPE_NONE, 0);
 
+  camera_signals[STATE_CHANGED] = g_signal_new ("state-changed", G_OBJECT_CLASS_TYPE (klass),
+                                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                                              G_STRUCT_OFFSET (CheeseCameraClass, state_changed),
+                                              NULL, NULL,
+                                              g_cclosure_marshal_VOID__INT,
+                                                G_TYPE_NONE, 1, G_TYPE_INT);
+
 
   g_object_class_install_property (object_class, PROP_VIDEO_TEXTURE,
                                    g_param_spec_pointer ("video-texture",
diff --git a/libcheese/cheese-camera.h b/libcheese/cheese-camera.h
index f6a61f9..195ff86 100644
--- a/libcheese/cheese-camera.h
+++ b/libcheese/cheese-camera.h
@@ -51,6 +51,7 @@ typedef struct
   void (*photo_saved)(CheeseCamera *camera);
   void (*photo_taken)(CheeseCamera *camera, GdkPixbuf *pixbuf);
   void (*video_saved)(CheeseCamera *camera);
+  void (*state_changed)(CheeseCamera *camera, GstState new_state);
 } CheeseCameraClass;
 
 enum CheeseCameraError
diff --git a/src/cheese-window.vala b/src/cheese-window.vala
index 99821d4..d56cb23 100644
--- a/src/cheese-window.vala
+++ b/src/cheese-window.vala
@@ -88,6 +88,7 @@ public class Cheese.MainWindow : Gtk.Window
   private bool is_recording;       /* Video Recording Flag */
   private bool is_bursting;
   private bool is_effects_selector_active;
+  private bool is_camera_actions_sensitive;
 
   private Gtk.Button[] buttons;
 
@@ -157,25 +158,25 @@ public class Cheese.MainWindow : Gtk.Window
     if (filename == null)
       return;                     /* Nothing selected. */
 
-	try
-	{
-		uri = GLib.Filename.to_uri (filename);
-		screen = this.get_screen ();
-		Gtk.show_uri (screen, uri, Gtk.get_current_event_time ());
-	}
-	catch (Error err)
-	{
-		MessageDialog error_dialog = new MessageDialog (this,
-                                                        Gtk.DialogFlags.MODAL |
-                                                        Gtk.DialogFlags.DESTROY_WITH_PARENT,
-                                                        Gtk.MessageType.ERROR,
-                                                        Gtk.ButtonsType.OK,
-                                                        "Could not open %s",
-                                                        filename);
-
-        error_dialog.run ();
-        error_dialog.destroy ();
-	}
+    try
+    {
+      uri    = GLib.Filename.to_uri (filename);
+      screen = this.get_screen ();
+      Gtk.show_uri (screen, uri, Gtk.get_current_event_time ());
+    }
+    catch (Error err)
+    {
+      MessageDialog error_dialog = new MessageDialog (this,
+                                                      Gtk.DialogFlags.MODAL |
+                                                      Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                                      Gtk.MessageType.ERROR,
+                                                      Gtk.ButtonsType.OK,
+                                                      "Could not open %s",
+                                                      filename);
+
+      error_dialog.run ();
+      error_dialog.destroy ();
+    }
   }
 
   [CCode (instance_pos = -1)]
@@ -217,23 +218,23 @@ public class Cheese.MainWindow : Gtk.Window
       return;                    /* Nothing selected. */
 
     File file_to_trash = File.new_for_path (filename);
-	try
-	{
-		file_to_trash.trash (null);
-	}
-	catch (Error err)
-	{
-		MessageDialog error_dialog = new MessageDialog (this,
-                                                        Gtk.DialogFlags.MODAL |
-                                                        Gtk.DialogFlags.DESTROY_WITH_PARENT,
-                                                        Gtk.MessageType.ERROR,
-                                                        Gtk.ButtonsType.OK,
-                                                        "Could not move %s to trash",
-                                                        filename);
-
-        error_dialog.run ();
-        error_dialog.destroy ();
-	}
+    try
+    {
+      file_to_trash.trash (null);
+    }
+    catch (Error err)
+    {
+      MessageDialog error_dialog = new MessageDialog (this,
+                                                      Gtk.DialogFlags.MODAL |
+                                                      Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                                      Gtk.MessageType.ERROR,
+                                                      Gtk.ButtonsType.OK,
+                                                      "Could not move %s to trash",
+                                                      filename);
+
+      error_dialog.run ();
+      error_dialog.destroy ();
+    }
   }
 
   [CCode (instance_pos = -1)]
@@ -853,6 +854,75 @@ public class Cheese.MainWindow : Gtk.Window
     }
   }
 
+  private Gee.HashMap<string, bool> action_sensitivities;
+  public void toggle_camera_actions_sensitivities (bool active)
+  {
+	  is_camera_actions_sensitive = active;
+    if (active)
+    {
+      foreach (string key in action_sensitivities.keys)
+      {
+        Gtk.Action action = (Gtk.Action)gtk_builder.get_object (key);
+        action.sensitive = action_sensitivities.get (key);
+      }
+    }
+    else
+    {
+      action_sensitivities = new HashMap<string, bool> (GLib.str_hash);
+      GLib.SList<weak GLib.Object> objects = gtk_builder.get_objects ();
+      foreach (GLib.Object obj in objects)
+      {
+        if (obj is Gtk.Action)
+        {
+          Gtk.Action action = (Gtk.Action)obj;
+          action_sensitivities.set (action.name, action.sensitive);
+        }
+      }
+
+      /* Keep only these actions sensitive. */
+      string active_actions[11] = { "cheese_action",
+                                    "edit_action",
+                                    "help_action",
+                                    "quit",
+                                    "help_contents",
+                                    "about",
+                                    "open",
+                                    "save_as",
+                                    "move_to_trash",
+                                    "delete",
+                                    "move_all_to_trash"};
+
+      /* Gross hack because Vala's `in` operator doesn't really work */
+      bool flag;
+      foreach (GLib.Object obj in objects)
+      {
+        flag = false;
+        if (obj is Gtk.Action)
+        {
+          Gtk.Action action = (Gtk.Action)obj;
+          foreach (string s in active_actions)
+          {
+            if (action.name == s)
+            {
+              flag = true;
+            }
+          }
+          if (!flag)
+            ((Gtk.Action)obj).sensitive = false;
+        }
+      }
+    }
+  }
+
+  private void camera_state_changed (Gst.State new_state)
+  {
+	  if (new_state == Gst.State.PLAYING)
+	  {
+		  if (!is_camera_actions_sensitive)
+			  toggle_camera_actions_sensitivities (true);
+	  }
+  }
+
   public void setup_ui ()
   {
     gtk_builder     = new Gtk.Builder ();
@@ -870,7 +940,7 @@ public class Cheese.MainWindow : Gtk.Window
       clutter_builder.load_from_file (GLib.Path.build_filename (Config.PACKAGE_DATADIR, "cheese-viewport.json"));
     }catch (Error err)
     {
-		error ("Error: %s", err.message);
+      error ("Error: %s", err.message);
     }
 
     main_vbox                         = (Gtk.VBox)gtk_builder.get_object ("mainbox_normal");
@@ -952,6 +1022,8 @@ public class Cheese.MainWindow : Gtk.Window
 
     set_mode (MediaMode.PHOTO);
     setup_effects_selector ();
+
+    toggle_camera_actions_sensitivities (false);
   }
 
   public void setup_camera ()
@@ -970,43 +1042,11 @@ public class Cheese.MainWindow : Gtk.Window
       warning ("Error: %s\n", err.message);
       error_layer.text = err.message;
       error_layer.show ();
-      GLib.SList<weak GLib.Object> objects = gtk_builder.get_objects ();
-
-      /* Keep only these actions sensitive. */
-      string active_actions[11] = { "cheese_action",
-                                    "edit_action",
-                                    "help_action",
-                                    "quit",
-                                    "help_contents",
-                                    "about",
-                                    "open",
-                                    "save_as",
-                                    "move_to_trash",
-                                    "delete",
-                                    "move_all_to_trash"};
-
-      /* Gross hack because Vala's `in` operator doesn't really work */
-      bool flag;
-      foreach (GLib.Object obj in objects)
-      {
-        flag = false;
-        if (obj is Gtk.Action)
-        {
-          Gtk.Action action = (Gtk.Action)obj;
-          foreach (string s in active_actions)
-          {
-            if (action.name == s)
-            {
-              flag = true;
-            }
-          }
-          if (!flag)
-            ((Gtk.Action)obj).sensitive = false;
-        }
-      }
+      toggle_camera_actions_sensitivities (false);
       return;
     }
 
+    camera.state_changed.connect (camera_state_changed);
     camera.play ();
   }
 }
diff --git a/src/vapi/cheese-common.vapi b/src/vapi/cheese-common.vapi
index c02621c..af9b1ec 100644
--- a/src/vapi/cheese-common.vapi
+++ b/src/vapi/cheese-common.vapi
@@ -56,6 +56,7 @@ namespace Cheese
     public virtual signal void photo_saved ();
     public virtual signal void photo_taken (Gdk.Pixbuf pixbuf);
     public virtual signal void video_saved ();
+    public virtual signal void state_changed (Gst.State new_state);
   }
   [CCode (cheader_filename = "cheese-camera-device.h")]
   public class CameraDevice : GLib.Object, GLib.Initable



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