[goobox] use GNotification instead of libnotify



commit c21b75519ad87cc3968de757ff723853be727588
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Fri Dec 28 15:32:18 2018 +0100

    use GNotification instead of libnotify
    
    requires glib 2.40

 meson.build                             |  16 +--
 src/goo-application-actions-callbacks.c |  46 ++++++++
 src/goo-application-actions-callbacks.h |   3 +
 src/goo-application-actions-entries.h   |   5 +-
 src/goo-application.c                   |  12 +++
 src/goo-player-info.c                   |  12 +++
 src/goo-player-info.h                   |   7 +-
 src/goo-window.c                        |  35 +++---
 src/main.c                              | 186 ++++----------------------------
 src/main.h                              |  15 +--
 src/meson.build                         |  57 +++++-----
 11 files changed, 157 insertions(+), 237 deletions(-)
---
diff --git a/meson.build b/meson.build
index dda9a9e..ff784d7 100644
--- a/meson.build
+++ b/meson.build
@@ -1,15 +1,14 @@
-project('goobox', 'c', 
+project('goobox', 'c',
   license : 'GPL2+',
   version : '3.5.0',
   meson_version : '>=0.43'
 )
 
-glib_version = '>=2.38'
+glib_version = '>=2.40'
 gtk_version = '>=3.13.2'
 gstreamer_version = '>=1.0.0'
 musicbrainz_version = '>=5.0.0'
 libcoverart_version = '>=1.0.0'
-libnotify_version = '>=0.4.3'
 
 gnome = import('gnome')
 i18n = import('i18n')
@@ -28,13 +27,6 @@ else
   use_libcoverart = libcoverart_dep.found()
 endif
 
-if get_option('disable-libnotify')
-  use_libnotify = false  
-else
-  libnotify_dep = dependency('libnotify', version : libnotify_version, required : false)
-  use_libnotify = libnotify_dep.found()
-endif
-
 # config.h
 
 config_data = configuration_data()
@@ -50,9 +42,6 @@ config_data.set_quoted('PACKAGE_VERSION', meson.project_version())
 if use_libcoverart
   config_data.set('HAVE_LIBCOVERART', 1)
 endif
-if use_libnotify
-  config_data.set('ENABLE_NOTIFICATION', 1)
-endif
 config_file = configure_file(output : 'config.h', configuration : config_data)
 config_inc = include_directories('.')
 
@@ -71,7 +60,6 @@ summary = [
   '           project: @0@ @1@'.format(meson.project_name(), meson.project_version()),
   '            prefix: @0@'.format(prefix),
   '       libcoverart: @0@'.format(use_libcoverart),
-  '         libnotify: @0@'.format(use_libnotify),  
   ''
 ]
 message('\n'.join(summary))
diff --git a/src/goo-application-actions-callbacks.c b/src/goo-application-actions-callbacks.c
index fc9cd3d..512f573 100644
--- a/src/goo-application-actions-callbacks.c
+++ b/src/goo-application-actions-callbacks.c
@@ -27,6 +27,7 @@
 #include "goo-application-actions-callbacks.h"
 #include "goo-window.h"
 #include "preferences.h"
+#include "main.h"
 
 
 void
@@ -162,3 +163,48 @@ goo_application_activate_shuffle (GSimpleAction *action,
 
        g_object_unref (settings);
 }
+
+
+void
+goo_application_activate_pause (GSimpleAction *action,
+                               GVariant      *parameter,
+                               gpointer       user_data)
+{
+       const char *device_id;
+       GtkWidget  *window;
+
+       device_id = g_variant_get_string (parameter, NULL);
+       window = main_get_window_from_device (device_id);
+       if (window != NULL)
+               goo_window_pause (GOO_WINDOW (window));
+}
+
+
+void
+goo_application_activate_play (GSimpleAction *action,
+                              GVariant      *parameter,
+                              gpointer       user_data)
+{
+       const char *device_id;
+       GtkWidget  *window;
+
+       device_id = g_variant_get_string (parameter, NULL);
+       window = main_get_window_from_device (device_id);
+       if (window != NULL)
+               goo_window_play (GOO_WINDOW (window));
+}
+
+
+void
+goo_application_activate_play_next (GSimpleAction *action,
+                                   GVariant      *parameter,
+                                   gpointer       user_data)
+{
+       const char *device_id;
+       GtkWidget  *window;
+
+       device_id = g_variant_get_string (parameter, NULL);
+       window = main_get_window_from_device (device_id);
+       if (window != NULL)
+               goo_window_next (GOO_WINDOW (window));
+}
diff --git a/src/goo-application-actions-callbacks.h b/src/goo-application-actions-callbacks.h
index c0ac4ea..17a5a2f 100644
--- a/src/goo-application-actions-callbacks.h
+++ b/src/goo-application-actions-callbacks.h
@@ -34,5 +34,8 @@ DEF_ACTION_CALLBACK (goo_application_activate_preferences)
 DEF_ACTION_CALLBACK (goo_application_activate_quit)
 DEF_ACTION_CALLBACK (goo_application_activate_repeat)
 DEF_ACTION_CALLBACK (goo_application_activate_shuffle)
+DEF_ACTION_CALLBACK (goo_application_activate_pause)
+DEF_ACTION_CALLBACK (goo_application_activate_play)
+DEF_ACTION_CALLBACK (goo_application_activate_play_next)
 
 #endif /* GOO_APPLICATION_ACTIONS_CALLBACKS_H */
diff --git a/src/goo-application-actions-entries.h b/src/goo-application-actions-entries.h
index 94d9566..803f7ca 100644
--- a/src/goo-application-actions-entries.h
+++ b/src/goo-application-actions-entries.h
@@ -35,7 +35,10 @@ static const GActionEntry goo_application_actions[] = {
        { PREF_PLAYLIST_SHUFFLE, _g_toggle_action_activated, NULL, "true", goo_application_activate_shuffle },
        { "help",  goo_application_activate_help },
        { "about", goo_application_activate_about },
-       { "quit",  goo_application_activate_quit }
+       { "quit",  goo_application_activate_quit },
+       { "pause",  goo_application_activate_pause, "s", NULL, NULL },
+       { "play",  goo_application_activate_play, "s", NULL, NULL },
+       { "play-next",  goo_application_activate_play_next, "s", NULL, NULL },
 };
 
 
diff --git a/src/goo-application.c b/src/goo-application.c
index 5e342f5..725a65c 100644
--- a/src/goo-application.c
+++ b/src/goo-application.c
@@ -395,6 +395,17 @@ goo_application_startup (GApplication *application)
 }
 
 
+static void
+goo_application_activate (GApplication *application)
+{
+       GtkWidget *window;
+
+       window = _gtk_application_get_current_window (GTK_APPLICATION (application));
+       if (window != NULL)
+               gtk_window_present (GTK_WINDOW (window));
+}
+
+
 static void
 goo_application_class_init (GooApplicationClass *klass)
 {
@@ -408,6 +419,7 @@ goo_application_class_init (GooApplicationClass *klass)
        application_class->command_line = goo_application_command_line;
        application_class->local_command_line = goo_application_local_command_line;
        application_class->startup = goo_application_startup;
+       application_class->activate = goo_application_activate;
 }
 
 
diff --git a/src/goo-player-info.c b/src/goo-player-info.c
index 2a701bb..89557a5 100644
--- a/src/goo-player-info.c
+++ b/src/goo-player-info.c
@@ -61,6 +61,7 @@ struct _GooPlayerInfoPrivate {
        double       fraction;
        guint        update_progress_timeout;
        GdkPixbuf   *original_cover;
+       char        *cover_file;
 };
 
 
@@ -327,6 +328,7 @@ goo_player_info_finalize (GObject *object)
        info = GOO_PLAYER_INFO (object);
        g_free (info->priv->total_time);
        if (info->priv->update_id != 0) {
+               g_free (info->priv->cover_file);
                g_object_unref (info->priv->original_cover);
                g_source_remove (info->priv->update_id);
                info->priv->update_id = 0;
@@ -513,6 +515,8 @@ goo_player_info_set_cover (GooPlayerInfo *info,
                return;
 
        g_clear_object (&info->priv->original_cover);
+       if (info->priv->cover_file != NULL)
+               g_free (info->priv->cover_file);
 
        if (strcmp (cover, "no-disc") == 0) {
                gtk_notebook_set_current_page (GTK_NOTEBOOK (info->priv->notebook), 0);
@@ -533,6 +537,7 @@ goo_player_info_set_cover (GooPlayerInfo *info,
                                              GTK_ICON_SIZE_DIALOG);
        }
        else {
+               info->priv->cover_file = g_strdup (cover);
                info->priv->original_cover = gdk_pixbuf_new_from_file (cover, NULL);
                if (info->priv->original_cover != NULL) {
                        GdkPixbuf *image;
@@ -653,3 +658,10 @@ goo_player_info_get_cover (GooPlayerInfo *info)
 {
        return info->priv->original_cover;
 }
+
+
+const char *
+goo_player_info_get_cover_file (GooPlayerInfo *info)
+{
+       return info->priv->cover_file;
+}
diff --git a/src/goo-player-info.h b/src/goo-player-info.h
index 3d98d63..08fbd87 100644
--- a/src/goo-player-info.h
+++ b/src/goo-player-info.h
@@ -51,8 +51,9 @@ struct _GooPlayerInfoClass
         void (*cover_clicked) (GooPlayerInfo *info);
 };
 
-GType       goo_player_info_get_type      (void);
-GtkWidget * goo_player_info_new           (GooWindow     *window);
-GdkPixbuf * goo_player_info_get_cover     (GooPlayerInfo *info);
+GType          goo_player_info_get_type        (void);
+GtkWidget *    goo_player_info_new             (GooWindow      *window);
+GdkPixbuf *    goo_player_info_get_cover       (GooPlayerInfo  *info);
+const char *   goo_player_info_get_cover_file  (GooPlayerInfo  *info);
 
 #endif /* GOO_PLAYER_INFO_H */
diff --git a/src/goo-window.c b/src/goo-window.c
index c5a6f66..696a0fa 100644
--- a/src/goo-window.c
+++ b/src/goo-window.c
@@ -1136,40 +1136,37 @@ get_action_name (GooPlayerAction action)
 static gboolean
 notify_current_state_cb (gpointer user_data)
 {
-       GooWindow *window = user_data;
+       GooWindow      *window = user_data;
+       GString        *info = g_string_new ("");
+       GooPlayerState  state;
 
        if (window->priv->notify_event != 0) {
                g_source_remove (window->priv->notify_event);
                window->priv->notify_event = 0;
        }
 
-#ifdef ENABLE_NOTIFICATION
-
-       GString        *info = g_string_new ("");
-       GooPlayerState  state;
-
        state = goo_player_get_state (window->priv->player);
 
        if ((state == GOO_PLAYER_STATE_ERROR) || (state == GOO_PLAYER_STATE_NO_DISC)) {
-               system_notify (window, _("No disc"), "");
+               system_notify (window, "no-disc", _("No disc"), NULL);
                return FALSE;
        }
 
        if (state == GOO_PLAYER_STATE_DATA_DISC) {
-               system_notify (window, _("Data disc"), "");
+               system_notify (window, "data-disc", _("Data disc"), NULL);
                return FALSE;
        }
 
        if (window->priv->album == NULL) {
-               system_notify (window, "", "");
+               system_notify (window, "no-album", "", NULL);
                return FALSE;
        }
 
        if (window->priv->current_track == NULL) {
                if ((window->priv->album != NULL) && (window->priv->album->title != NULL))
-                       system_notify (window, window->priv->album->title , "");
+                       system_notify (window, "new-album", window->priv->album->title , NULL);
                else
-                       system_notify (window, _("Audio CD"), "");
+                       system_notify (window, "audio-cd", _("Audio CD"), NULL);
                return FALSE;
        }
 
@@ -1183,22 +1180,20 @@ notify_current_state_cb (gpointer user_data)
        if (window->priv->album->title != NULL) {
                char *e_album = g_markup_escape_text (window->priv->album->title, -1);
 
-               g_string_append (info, "\n");
+               g_string_append (info, " - ");
+               g_string_append_printf (info, "%s", e_album);
 
-               g_string_append_printf (info, "<i>%s</i>", e_album);
                g_free (e_album);
        }
 
-       g_string_append (info, " ");
-
-       system_notify (window,
-                      window->priv->current_track->title,
-                      info->str);
+       if (goo_player_get_state (goo_window_get_player (window)) == GOO_PLAYER_STATE_PLAYING)
+               system_notify (window,
+                              "new-track",
+                              window->priv->current_track->title,
+                              (strlen (info->str) > 0) ? info->str : NULL);
 
        g_string_free (info, TRUE);
 
-#endif /* ENABLE_NOTIFICATION */
-
        return FALSE;
 }
 
diff --git a/src/main.c b/src/main.c
index 3e6a0e7..f4625f9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,15 +34,6 @@
 #include "main.h"
 #include "preferences.h"
 #include "typedefs.h"
-#ifdef ENABLE_NOTIFICATION
-#include <libnotify/notify.h>
-#ifndef NOTIFY_CHECK_VERSION
-#define NOTIFY_CHECK_VERSION(x,y,z) 0
-#endif
-static NotifyNotification *notification = NULL;
-static gboolean            notification_supports_persistence = FALSE;
-static gboolean            notification_supports_actions = FALSE;
-#endif /* ENABLE_NOTIFICATION */
 
 
 GtkApplication *Main_Application = NULL;
@@ -64,12 +55,6 @@ main (int argc, char *argv[])
        /* run the main application */
 
        Main_Application = goo_application_new ();
-
-#ifdef ENABLE_NOTIFICATION
-       if (! notify_init (g_get_application_name ()))
-                g_warning ("Cannot initialize notification system.");
-#endif /* ENABLE_NOTIFICATION */
-
        status = g_application_run (G_APPLICATION (Main_Application), argc, argv);
 
        g_object_unref (Main_Application);
@@ -136,166 +121,41 @@ main_get_drive_for_device (const char *device)
 }
 
 
-#ifdef ENABLE_NOTIFICATION
-
-
-static gboolean
-play_next (gpointer user_data)
-{
-       GooWindow *window = user_data;
-
-       goo_window_next (window);
-
-       return FALSE;
-}
-
-
-static void
-notify_action_next_cb (NotifyNotification *notification,
-                       char               *action,
-                       gpointer            user_data)
-{
-       GooWindow *window = user_data;
-
-       if (! notification_supports_persistence)
-               notify_notification_close (notification, NULL);
-
-       g_idle_add (play_next, window);
-}
-
-
-static gboolean
-toggle_play (gpointer user_data)
-{
-       GooWindow *window = user_data;
-
-       goo_window_toggle_play (window);
-
-       return FALSE;
-}
-
-
-static void
-notify_action_toggle_play_cb (NotifyNotification *notification,
-                             char               *action,
-                             gpointer            user_data)
-{
-       GooWindow *window = user_data;
-
-       if (! notification_supports_persistence)
-               notify_notification_close (notification, NULL);
-
-       g_idle_add (toggle_play, window);
-}
-
-
-#endif /* ENABLE_NOTIFICATION */
-
-
-gboolean
-notification_has_persistence (void)
-{
-#ifdef ENABLE_NOTIFICATION
-
-       gboolean  supports_persistence = FALSE;
-       GList    *caps;
-
-       caps = notify_get_server_caps ();
-       if (caps != NULL) {
-               supports_persistence = g_list_find_custom (caps, "persistence", (GCompareFunc) strcmp) != 
NULL;
-
-               g_list_foreach (caps, (GFunc)g_free, NULL);
-               g_list_free (caps);
-       }
-
-       return supports_persistence;
-
-#else
-
-       return FALSE;
-
-#endif /* ENABLE_NOTIFICATION */
-}
-
-
 void
 system_notify (GooWindow       *window,
+              const char      *id,
               const char      *summary,
               const char      *body)
 {
-#ifdef ENABLE_NOTIFICATION
-
-       GdkPixbuf *cover;
-
-       if (! notify_is_initted ())
-               return;
+       GNotification *notification;
+       GFile         *cover_file;
+       GIcon         *cover_icon;
+       const char    *device_id;
 
-       if (notification == NULL) {
-               GList *caps;
+       notification = g_notification_new (summary);
+       if (body != NULL)
+               g_notification_set_body (G_NOTIFICATION (notification), body);
 
-               notification_supports_actions = FALSE;
-               notification_supports_persistence = FALSE;
+       /* cover */
 
-               caps = notify_get_server_caps ();
-               if (caps != NULL) {
-                       notification_supports_actions = g_list_find_custom (caps, "actions", (GCompareFunc) 
strcmp) != NULL;
-                       notification_supports_persistence = g_list_find_custom (caps, "persistence", 
(GCompareFunc) strcmp) != NULL;
+       cover_file = g_file_new_for_path (goo_player_info_get_cover_file (GOO_PLAYER_INFO 
(goo_window_get_player_info (window))));
+       cover_icon = g_file_icon_new (cover_file);
+       g_notification_set_icon (G_NOTIFICATION (notification), cover_icon);
 
-                       g_list_foreach (caps, (GFunc)g_free, NULL);
-                       g_list_free (caps);
-               }
+       /* actions */
 
-#if NOTIFY_CHECK_VERSION (0, 7, 0)
-               notification = notify_notification_new (summary, body, "goobox");
-#else
-               notification = notify_notification_new_with_status_icon (summary, body, "goobox", 
status_icon);
-#endif
-               notify_notification_set_hint_string (notification, "desktop-entry", "goobox");
-               notify_notification_set_urgency (notification, NOTIFY_URGENCY_LOW);
-       }
+       device_id = goo_player_get_device (goo_window_get_player (window));
+       if (goo_player_get_state (goo_window_get_player (window)) == GOO_PLAYER_STATE_PLAYING)
+               g_notification_add_button_with_target (G_NOTIFICATION (notification), _("Pause"), 
"app.pause", "s", device_id);
        else
-               notify_notification_update (notification, summary, body, "goobox");
-
-       cover = goo_player_info_get_cover (GOO_PLAYER_INFO (goo_window_get_player_info (window)));
-       notify_notification_set_image_from_pixbuf (notification, cover);
-
-       if (notification_supports_actions) {
-
-               notify_notification_clear_actions (notification);
-
-               if (goo_player_get_state (goo_window_get_player (window)) == GOO_PLAYER_STATE_PLAYING)
-                       notify_notification_add_action (notification,
-                                                       GOO_ICON_NAME_PAUSE,
-                                                       _("Pause"),
-                                                       notify_action_toggle_play_cb,
-                                                       window,
-                                                       NULL);
-               else
-                       notify_notification_add_action (notification,
-                                                       GOO_ICON_NAME_PLAY,
-                                                       _("Play"),
-                                                       notify_action_toggle_play_cb,
-                                                       window,
-                                                       NULL);
-
-               notify_notification_add_action (notification,
-                                               GOO_ICON_NAME_NEXT,
-                                               _("Next"),
-                                               notify_action_next_cb,
-                                               window,
-                                               NULL);
-
-               notify_notification_set_hint (notification,
-                                             "action-icons",
-                                             g_variant_new_boolean (TRUE));
-       }
+               g_notification_add_button_with_target (G_NOTIFICATION (notification), _("Play"), "app.play", 
"s", device_id);
+       g_notification_add_button_with_target (G_NOTIFICATION (notification), _("Next"), "app.play-next", 
"s", device_id);
 
-       if (notification_supports_persistence)
-               notify_notification_set_hint (notification,
-                                             "resident" /* "transient" */,
-                                             g_variant_new_boolean (TRUE));
+       /* send */
 
-       notify_notification_show (notification, NULL);
+       g_application_send_notification (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))), 
id, notification);
 
-#endif /* ENABLE_NOTIFICATION */
+       g_object_unref (cover_icon);
+       g_object_unref (cover_file);
+       g_object_unref (notification);
 }
diff --git a/src/main.h b/src/main.h
index 76baa7c..0a7c219 100644
--- a/src/main.h
+++ b/src/main.h
@@ -30,12 +30,13 @@ extern GtkApplication *Main_Application;
 extern int            arg_auto_play;
 extern int            arg_toggle_visibility;
 
-GtkWidget *     main_get_window_from_device  (const char      *device);
-BraseroDrive *  main_get_most_likely_drive   (void);
-BraseroDrive *  main_get_drive_for_device    (const char      *device);
-gboolean        notification_has_persistence (void);
-void            system_notify                (GooWindow       *window,
-                                             const char      *title,
-                                             const char      *msg);
+GtkWidget *    main_get_window_from_device     (const char     *device);
+BraseroDrive * main_get_most_likely_drive      (void);
+BraseroDrive * main_get_drive_for_device       (const char     *device);
+gboolean       notification_has_persistence    (void);
+void           system_notify                   (GooWindow      *window,
+                                               const char      *id,
+                                               const char      *title,
+                                               const char      *msg);
 
 #endif /* MAIN_H */
diff --git a/src/meson.build b/src/meson.build
index 30a82d5..e5d999f 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -16,17 +16,17 @@ libdiscid_dep = dependency('libdiscid')
 c_args = [ '-fPIC', '-DPIC', '-Wl,--as-needed' ]
 if get_option('buildtype').contains('debug')
   c_args += [ '-DDEBUG' ]
-  test_args = [ 
-    '-Wall', 
-    '-Wcast-align', 
-    '-Wtype-limits', 
-    '-Wclobbered', 
-    '-Wempty-body', 
-    '-Wignored-qualifiers', 
+  test_args = [
+    '-Wall',
+    '-Wcast-align',
+    '-Wtype-limits',
+    '-Wclobbered',
+    '-Wempty-body',
+    '-Wignored-qualifiers',
     '-Wmissing-prototypes',
-    '-Wnested-externs', 
-    '-Wpointer-arith', 
-    '-Wno-sign-compare', 
+    '-Wnested-externs',
+    '-Wpointer-arith',
+    '-Wno-sign-compare',
     '-Wformat-security'
   ]
 else
@@ -49,11 +49,11 @@ source_files = files(
   'gnome-desktop-thumbnail.c',
   'gnome-thumbnail-pixbuf-utils.c',
   'goo-application.c',
-  'goo-application-actions-callbacks.c',    
+  'goo-application-actions-callbacks.c',
   'goo-error.c',
   'goo-player.c',
   'goo-player-bar.c',
-  'goo-player-info.c',      
+  'goo-player-info.c',
   'goo-window.c',
   'goo-window-actions-callbacks.c',
   'gth-user-dir.c',
@@ -68,25 +68,24 @@ marshal_files = gnome.genmarshal('goo-marshal', prefix : 'goo_marshal', sources
 
 # Build targets
 
-executable('goobox', 
-  sources : [ 
-    config_file, 
-    source_files, 
-    gresource_files, 
-    marshal_files 
-  ],     
-  dependencies : [ 
-    libm_dep, 
+executable('goobox',
+  sources : [
+    config_file,
+    source_files,
+    gresource_files,
+    marshal_files
+  ],
+  dependencies : [
+    libm_dep,
     thread_dep,
-    glib_dep, 
-    gthread_dep, 
-    gtk_dep, 
+    glib_dep,
+    gthread_dep,
+    gtk_dep,
     gstreamer_dep,
-    brasero_dep, 
-    musicbrainz_dep, 
-    libdiscid_dep, 
-    use_libcoverart ? libcoverart_dep : [], 
-    use_libnotify ? libnotify_dep : [] 
+    brasero_dep,
+    musicbrainz_dep,
+    libdiscid_dep,
+    use_libcoverart ? libcoverart_dep : []
   ],
   include_directories : config_inc,
   c_args : c_args,


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