]> www.fi.muni.cz Git - evince.git/commitdiff
Clean up the media player keys stuff.
authorCarlos Garcia Campos <carlosgc@gnome.org>
Thu, 4 Sep 2008 11:32:12 +0000 (11:32 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Thu, 4 Sep 2008 11:32:12 +0000 (11:32 +0000)
2008-09-04  Carlos Garcia Campos  <carlosgc@gnome.org>

* shell/ev-application.c: (ev_application_get_media_keys):
* shell/ev-media-player-keys.[ch]:
(ev_media_player_keys_class_init), (on_media_player_key_pressed),
(ev_media_player_keys_grab_keys),
(ev_media_player_keys_release_keys), (ev_media_player_keys_init),
(ev_media_player_keys_focused), (ev_media_player_keys_finalize):
* shell/ev-window.[ch]: (ev_window_dispose),
(view_actions_focus_in_cb), (ev_window_media_player_key_pressed),
(ev_window_init):

Clean up the media player keys stuff.

svn path=/trunk/; revision=3148

ChangeLog
shell/ev-application.c
shell/ev-media-player-keys.c
shell/ev-media-player-keys.h
shell/ev-window.c
shell/ev-window.h

index 5cf9f0efca941771f94f8ac6b7dc09b9d4f9f178..1b7b5265aab8ba3f9e9a2b2597afc690a36d7e2a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2008-09-04  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * shell/ev-application.c: (ev_application_get_media_keys):
+       * shell/ev-media-player-keys.[ch]:
+       (ev_media_player_keys_class_init), (on_media_player_key_pressed),
+       (ev_media_player_keys_grab_keys),
+       (ev_media_player_keys_release_keys), (ev_media_player_keys_init),
+       (ev_media_player_keys_focused), (ev_media_player_keys_finalize):
+       * shell/ev-window.[ch]: (ev_window_dispose),
+       (view_actions_focus_in_cb), (ev_window_media_player_key_pressed),
+       (ev_window_init):
+
+       Clean up the media player keys stuff.
+       
 2008-08-31  Carlos Garcia Campos  <carlosgc@gnome.org>
 
        * NEWS:
index fd43c881eb65bfc984065dc3d70f253143b27270..e852561f3009de2b4c202d9d3e1812a0367c7f9c 100644 (file)
@@ -722,6 +722,13 @@ ev_application_shutdown (EvApplication *application)
                application->print_settings = NULL;
        }
 #endif /* GTK 2.11.0 */
+
+#ifdef ENABLE_DBUS
+       if (application->keys) {
+               g_object_unref (application->keys);
+               application->keys = NULL;
+       }
+#endif /* ENABLE_DBUS */
        
        g_free (application->last_chooser_uri);
        g_object_unref (application);
@@ -825,13 +832,11 @@ ev_application_get_windows (EvApplication *application)
  *
  * Returns: A #EvMediaPlayerKeys.
  */
-GObject
-*ev_application_get_media_keys (EvApplication *application)
+GObject *
+ev_application_get_media_keys (EvApplication *application)
 {
 #ifdef ENABLE_DBUS
-       if (!application->keys)
-               return NULL;
-       return g_object_ref (G_OBJECT (application->keys));
+       return G_OBJECT (application->keys);
 #else
        return NULL;
 #endif /* ENABLE_DBUS */
index c244b2e9fbe8a3a6d1adf53c3afac66baf97a8d4..04d858f3217215875bb82138151d13c269027fa9 100644 (file)
@@ -22,9 +22,6 @@
 #include "config.h"
 
 #include <glib.h>
-#include <glib-object.h>
-#include <glib/gi18n-lib.h>
-#include <gmodule.h>
 #include <dbus/dbus-glib.h>
 #include <string.h>
 
 
 #include "ev-marshal.h"
 
+enum {
+       KEY_PRESSED,
+       LAST_SIGNAL
+};
+
 struct _EvMediaPlayerKeys
 {
        GObject        parent;
-       DBusGProxy    *media_player_keys_proxy;
-       EvWindow      *window;
+       
+       DBusGProxy    *proxy;
 };
 
 struct _EvMediaPlayerKeysClass
 {
        GObjectClass parent_class;
+
+       /* Signals */
+       void (* key_pressed) (EvMediaPlayerKeys *keys,
+                             const gchar       *key);
 };
 
+static guint signals[LAST_SIGNAL] = { 0 };
+
 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
 
-static void ev_media_player_keys_init          (EvMediaPlayerKeys *keys);
-static void ev_media_player_keys_finalize      (GObject *object);
+static void ev_media_player_keys_finalize (GObject *object);
 
 static void
 ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+       signals[KEY_PRESSED] =
+               g_signal_new ("key_pressed",
+                             EV_TYPE_MEDIA_PLAYER_KEYS,
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (EvMediaPlayerKeysClass, key_pressed),
+                             NULL, NULL,
+                             g_cclosure_marshal_VOID__STRING,
+                             G_TYPE_NONE,
+                             1, G_TYPE_STRING);
+       
        object_class->finalize = ev_media_player_keys_finalize;
 }
 
 static void
-proxy_destroy (DBusGProxy *proxy,
-              EvMediaPlayerKeys* keys)
+on_media_player_key_pressed (DBusGProxy        *proxy,
+                            const gchar       *application,
+                            const gchar       *key,
+                            EvMediaPlayerKeys *keys)
 {
-       keys->media_player_keys_proxy = NULL;
+       if (strcmp ("Evince", application) == 0) {
+               g_signal_emit (keys, signals[KEY_PRESSED], 0, key);
+       }
 }
 
 static void
-on_media_player_key_pressed (DBusGProxy *proxy, const gchar *application, const gchar *key, EvMediaPlayerKeys *keys)
+ev_media_player_keys_grab_keys (EvMediaPlayerKeys *keys)
 {
-       if (strcmp ("Evince", application) == 0 && keys->window != NULL) {
-               /* Note how Previous/Next only go to the
-                * next/previous page despite their icon telling you
-                * they should go to the beginning/end.
-                *
-                * There's very few keyboards with FFW/RWD though,
-                * so we stick the most useful keybinding on the most
-                * often seen keys
-                */
-               if (strcmp ("Play", key) == 0) {
-                       ev_window_start_presentation (keys->window);
-               } else if (strcmp ("Previous", key) == 0) {
-                       ev_window_go_previous_page (keys->window);
-               } else if (strcmp ("Next", key) == 0) {
-                       ev_window_go_next_page (keys->window);
-               } else if (strcmp ("FastForward", key) == 0) {
-                       ev_window_go_last_page (keys->window);
-               } else if (strcmp ("Rewind", key) == 0) {
-                       ev_window_go_first_page (keys->window);
-               }
-       }
+       dbus_g_proxy_call (keys->proxy,
+                          "GrabMediaPlayerKeys", NULL,
+                          G_TYPE_STRING, "Evince",
+                          G_TYPE_UINT, 0,
+                          G_TYPE_INVALID, G_TYPE_INVALID);
+}
+
+static void
+ev_media_player_keys_release_keys (EvMediaPlayerKeys *keys)
+{
+       dbus_g_proxy_call (keys->proxy,
+                          "ReleaseMediaPlayerKeys", NULL,
+                          G_TYPE_STRING, "Evince",
+                          G_TYPE_INVALID, G_TYPE_INVALID);
 }
 
 static void
@@ -104,17 +118,17 @@ ev_media_player_keys_init (EvMediaPlayerKeys *keys)
 
        /* Try the gnome-settings-daemon version,
         * then the gnome-control-center version of things */
-       keys->media_player_keys_proxy = dbus_g_proxy_new_for_name_owner (connection,
-                                                                      "org.gnome.SettingsDaemon",
-                                                                      "/org/gnome/SettingsDaemon/MediaKeys",
-                                                                      "org.gnome.SettingsDaemon.MediaKeys",
-                                                                      NULL);
-       if (keys->media_player_keys_proxy == NULL) {
-               keys->media_player_keys_proxy = dbus_g_proxy_new_for_name_owner (connection,
-                                                                              "org.gnome.SettingsDaemon",
-                                                                              "/org/gnome/SettingsDaemon",
-                                                                              "org.gnome.SettingsDaemon",
-                                                                              &err);
+       keys->proxy = dbus_g_proxy_new_for_name_owner (connection,
+                                                      "org.gnome.SettingsDaemon",
+                                                      "/org/gnome/SettingsDaemon/MediaKeys",
+                                                      "org.gnome.SettingsDaemon.MediaKeys",
+                                                      NULL);
+       if (keys->proxy == NULL) {
+               keys->proxy = dbus_g_proxy_new_for_name_owner (connection,
+                                                              "org.gnome.SettingsDaemon",
+                                                              "/org/gnome/SettingsDaemon",
+                                                              "org.gnome.SettingsDaemon",
+                                                              &err);
        }
 
        dbus_g_connection_unref (connection);
@@ -122,44 +136,36 @@ ev_media_player_keys_init (EvMediaPlayerKeys *keys)
                g_warning ("Failed to create dbus proxy for org.gnome.SettingsDaemon: %s",
                           err->message);
                g_error_free (err);
+               
+               if (keys->proxy) {
+                       g_object_unref (keys->proxy);
+                       keys->proxy = NULL;
+               }
+               
                return;
-       } else {
-               g_signal_connect_object (keys->media_player_keys_proxy,
-                                        "destroy",
-                                        G_CALLBACK (proxy_destroy),
-                                        keys, 0);
        }
 
-       dbus_g_proxy_call (keys->media_player_keys_proxy,
-                          "GrabMediaPlayerKeys", NULL,
-                          G_TYPE_STRING, "Evince", G_TYPE_UINT, 0, G_TYPE_INVALID,
-                          G_TYPE_INVALID);
+       g_object_add_weak_pointer (G_OBJECT (keys->proxy),
+                                  (gpointer) &(keys->proxy));
+
+       ev_media_player_keys_grab_keys (keys);
 
        dbus_g_object_register_marshaller (ev_marshal_VOID__STRING_STRING,
                                           G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
-       dbus_g_proxy_add_signal (keys->media_player_keys_proxy, "MediaPlayerKeyPressed",
+       dbus_g_proxy_add_signal (keys->proxy, "MediaPlayerKeyPressed",
                                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
 
-       dbus_g_proxy_connect_signal (keys->media_player_keys_proxy, "MediaPlayerKeyPressed",
+       dbus_g_proxy_connect_signal (keys->proxy, "MediaPlayerKeyPressed",
                                     G_CALLBACK (on_media_player_key_pressed), keys, NULL);
 }
 
 void
-ev_media_player_keys_focused (EvMediaPlayerKeys *keys, EvWindow *window)
+ev_media_player_keys_focused (EvMediaPlayerKeys *keys)
 {
-       if (keys->media_player_keys_proxy != NULL) {
-               if (keys->window != NULL) {
-                       g_object_unref (keys->window);
-                       keys->window = NULL;
-               }
-               if (window != NULL) {
-                       dbus_g_proxy_call (keys->media_player_keys_proxy,
-                                          "GrabMediaPlayerKeys", NULL,
-                                          G_TYPE_STRING, "Evince", G_TYPE_UINT, 0, G_TYPE_INVALID,
-                                          G_TYPE_INVALID);
-                       keys->window = g_object_ref (window);
-               }
-       }
+       if (!keys->proxy)
+               return;
+       
+       ev_media_player_keys_grab_keys (keys);
 }
 
 static void
@@ -167,17 +173,10 @@ ev_media_player_keys_finalize (GObject *object)
 {
        EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
 
-       if (keys->media_player_keys_proxy != NULL) {
-               dbus_g_proxy_call (keys->media_player_keys_proxy,
-                                  "ReleaseMediaPlayerKeys", NULL,
-                                  G_TYPE_STRING, "Ev", G_TYPE_INVALID, G_TYPE_INVALID);
-               g_object_unref (keys->media_player_keys_proxy);
-               keys->media_player_keys_proxy = NULL;
-       }
-
-       if (keys->window != NULL) {
-               g_object_unref (keys->window);
-               keys->window = NULL;
+       if (keys->proxy) {
+               ev_media_player_keys_release_keys (keys);
+               g_object_unref (keys->proxy);
+               keys->proxy = NULL;
        }
 
        G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
index 135aa818ddc1814ec1bee945cf766622ffa0536c..d23c1883699dd287d150263a92f268a3e0e1270a 100644 (file)
@@ -22,7 +22,7 @@
 #ifndef EV_MEDIA_PLAYER_KEYS_H
 #define EV_MEDIA_PLAYER_KEYS_H
 
-#include "ev-window.h"
+#include <glib-object.h>
 
 G_BEGIN_DECLS
 
@@ -37,12 +37,11 @@ typedef struct _EvMediaPlayerKeys EvMediaPlayerKeys;
 typedef struct _EvMediaPlayerKeysClass EvMediaPlayerKeysClass;
 
 
-GType  ev_media_player_keys_get_type           (void) G_GNUC_CONST;
+GType             ev_media_player_keys_get_type  (void) G_GNUC_CONST;
 
-EvMediaPlayerKeys * ev_media_player_keys_new   (void);
+EvMediaPlayerKeys *ev_media_player_keys_new      (void);
 
-void ev_media_player_keys_focused              (EvMediaPlayerKeys *keys,
-                                                EvWindow *window);
+void               ev_media_player_keys_focused          (EvMediaPlayerKeys *keys);
 
 G_END_DECLS
 
index 65f5d468893a7f082a447ca9ddb3f32c09bf0990..03f98cbbb93c1bf28d8bb5512a16d9fbf657e732 100644 (file)
@@ -285,6 +285,9 @@ static void     find_bar_search_changed_cb              (EggFindBar       *find_
 static void     ev_window_do_preview_print              (EvWindow         *window);
 static void     ev_window_load_file_remote              (EvWindow         *ev_window,
                                                         GFile            *source_file);
+static void     ev_window_media_player_key_pressed      (EvWindow         *window,
+                                                        const gchar      *key,
+                                                        gpointer          user_data);
 
 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
 
@@ -3256,12 +3259,6 @@ ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
        ev_view_previous_page (EV_VIEW (ev_window->priv->view));
 }
 
-void
-ev_window_go_previous_page (EvWindow *ev_window)
-{
-       ev_window_cmd_go_previous_page (NULL, ev_window);
-}
-
 static void
 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
 {
@@ -3270,12 +3267,6 @@ ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
        ev_view_next_page (EV_VIEW (ev_window->priv->view));
 }
 
-void
-ev_window_go_next_page (EvWindow *ev_window)
-{
-       ev_window_cmd_go_next_page (NULL, ev_window);
-}
-
 static void
 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
 {
@@ -3284,12 +3275,6 @@ ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
        ev_page_cache_set_current_page (ev_window->priv->page_cache, 0);
 }
 
-void
-ev_window_go_first_page (EvWindow *ev_window)
-{
-       ev_window_cmd_go_first_page (NULL, ev_window);
-}
-
 static void
 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
 {
@@ -3301,12 +3286,6 @@ ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
        ev_page_cache_set_current_page (ev_window->priv->page_cache, n_pages - 1);
 }
 
-void
-ev_window_go_last_page (EvWindow *ev_window)
-{
-       ev_window_cmd_go_last_page (NULL, ev_window);
-}
-
 static void
 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
 {
@@ -3407,12 +3386,6 @@ ev_window_cmd_start_presentation (GtkAction *action, EvWindow *window)
        ev_window_run_presentation (window);
 }
 
-void
-ev_window_start_presentation (EvWindow *ev_window)
-{
-       ev_window_run_presentation (ev_window);
-}
-
 static gboolean
 ev_window_enumerate_printer_cb (GtkPrinter *printer,
                                EvWindow   *window)
@@ -4220,16 +4193,14 @@ ev_window_dispose (GObject *object)
 {
        EvWindow *window = EV_WINDOW (object);
        EvWindowPrivate *priv = window->priv;
-#ifdef ENABLE_DBUS
-       GObject *keys;
+       GObject *mpkeys = ev_application_get_media_keys (EV_APP);
 
-       keys = ev_application_get_media_keys (EV_APP);
-       if (keys) {
-               ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys), NULL);
-               g_object_unref (keys);
+       if (mpkeys) {
+               g_signal_handlers_disconnect_by_func (mpkeys,
+                                                     ev_window_media_player_key_pressed,
+                                                     window);
        }
-#endif /* ENABLE_DBUS */
-
+       
        if (priv->setup_document_idle > 0) {
                g_source_remove (priv->setup_document_idle);
                priv->setup_document_idle = 0;
@@ -4766,10 +4737,7 @@ view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *win
        GObject *keys;
 
        keys = ev_application_get_media_keys (EV_APP);
-       if (keys) {
-               ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys), window);
-               g_object_unref (keys);
-       }
+       ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys));
 #endif /* ENABLE_DBUS */
 
        update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, FALSE);
@@ -5337,6 +5305,35 @@ ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
        gtk_widget_show (fc);
 }
 
+static void
+ev_window_media_player_key_pressed (EvWindow    *window,
+                                   const gchar *key,
+                                   gpointer     user_data)
+{
+       if (!gtk_window_is_active (GTK_WINDOW (window))) 
+               return;
+       
+       /* Note how Previous/Next only go to the
+        * next/previous page despite their icon telling you
+        * they should go to the beginning/end.
+        *
+        * There's very few keyboards with FFW/RWD though,
+        * so we stick the most useful keybinding on the most
+        * often seen keys
+        */
+       if (strcmp (key, "Play") == 0) {
+               ev_window_run_presentation (window);
+       } else if (strcmp (key, "Previous") == 0) {
+               ev_window_cmd_go_previous_page (NULL, window);
+       } else if (strcmp (key, "Next") == 0) {
+               ev_window_cmd_go_next_page (NULL, window);
+       } else if (strcmp (key, "FastForward") == 0) {
+               ev_window_cmd_go_last_page (NULL, window);
+       } else if (strcmp (key, "Rewind") == 0) {
+               ev_window_cmd_go_first_page (NULL, window);
+       }
+}
+
 static void
 ev_window_init (EvWindow *ev_window)
 {
@@ -5344,6 +5341,7 @@ ev_window_init (EvWindow *ev_window)
        GtkAccelGroup *accel_group;
        GError *error = NULL;
        GtkWidget *sidebar_widget;
+       GObject *mpkeys;
 
        g_signal_connect (ev_window, "configure_event",
                          G_CALLBACK (window_configure_event_cb), NULL);
@@ -5624,6 +5622,14 @@ ev_window_init (EvWindow *ev_window)
                                                                       "/AttachmentPopup");
        ev_window->priv->attach_list = NULL;
 
+       /* Media player keys */
+       mpkeys = ev_application_get_media_keys (EV_APP);
+       if (mpkeys) {
+               g_signal_connect_swapped (mpkeys, "key_pressed",
+                                         G_CALLBACK (ev_window_media_player_key_pressed),
+                                         ev_window);
+       }
+       
        /* Give focus to the document view */
        gtk_widget_grab_focus (ev_window->priv->view);
 
index 2957c44e64bc830449f281dad01c2cbe5a87f8a8..5ed7dd77f98681f1aedd5651c0f165aef74fd22c 100644 (file)
@@ -85,13 +85,6 @@ void         ev_window_print_range     (EvWindow       *ev_window,
                                           int             first_page,
                                           int             last_page);
 
-void           ev_window_go_last_page    (EvWindow       *ev_window);
-void           ev_window_go_first_page   (EvWindow       *ev_window);
-void           ev_window_go_next_page    (EvWindow       *ev_window);
-void           ev_window_go_previous_page(EvWindow       *ev_window);
-void           ev_window_start_presentation
-                                         (EvWindow       *ev_window);
-
 G_END_DECLS
 
 #endif /* !EV_WINDOW_H */