=== modified file 'src/obexpush.c' --- old/src/obexpush.c 2009-03-25 23:56:27 +0000 +++ new/src/obexpush.c 2009-03-30 16:41:07 +0000 @@ -67,52 +67,79 @@ } static void +launch_file_viewer (const gchar *file_uri) +{ + GdkAppLaunchContext *ctx = NULL; + GTimeVal val; + + g_get_current_time (&val); + +#if GTK_CHECK_VERSION(2,14,0) + ctx = gdk_app_launch_context_new (); + gdk_app_launch_context_set_screen (ctx, gdk_screen_get_default ()); + gdk_app_launch_context_set_timestamp (ctx, val.tv_sec); +#endif + + if (!g_app_info_launch_default_for_uri (file_uri, + G_APP_LAUNCH_CONTEXT (ctx), + NULL)) { + g_warning ("Failed to launch the file viewer\n"); + } + + if (ctx) + g_object_unref (ctx); +} + +static void +open_download_folder (const gchar *file_uri) +{ + GdkAppLaunchContext *ctx = NULL; + GTimeVal val; + GFile *file; + GFile *parent; + gchar *parent_uri; + + g_get_current_time (&val); + +#if GTK_CHECK_VERSION(2,14,0) + ctx = gdk_app_launch_context_new (); + gdk_app_launch_context_set_screen (ctx, gdk_screen_get_default ()); + gdk_app_launch_context_set_timestamp (ctx, val.tv_sec); +#endif + + file = g_file_new_for_uri (file_uri); + parent = g_file_get_parent (file); + parent_uri = g_file_get_uri (parent); + + g_object_unref (file); + g_object_unref (parent); + + if (!g_app_info_launch_default_for_uri (parent_uri, + G_APP_LAUNCH_CONTEXT (ctx), + NULL)) { + g_warning ("Failed to launch the file manager\n"); + } + + g_free (parent_uri); + if (ctx) + g_object_unref (ctx); +} + +static void notification_launch_action_on_file_cb (NotifyNotification *notification, const char *action, const char *file_uri) { - GdkScreen *screen; - GAppLaunchContext *ctx; - GTimeVal val; - g_assert (action != NULL); - g_get_current_time (&val); - -#if GTK_CHECK_VERSION(2,14,0) - ctx = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ()); - screen = gdk_screen_get_default (); - gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (ctx), screen); - gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (ctx), val.tv_sec); -#else - ctx = NULL; - screen = NULL; -#endif - /* We launch the file viewer for the file */ if (g_str_equal (action, "display") != FALSE) { - if (g_app_info_launch_default_for_uri (file_uri, ctx, NULL) == FALSE) { - g_warning ("Failed to launch the file viewer\n"); - } + launch_file_viewer (file_uri); } /* we open the Downloads folder */ if (g_str_equal (action, "reveal") != FALSE) { - GFile *file; - GFile *parent; - gchar *parent_uri; - - file = g_file_new_for_uri (file_uri); - parent = g_file_get_parent (file); - parent_uri = g_file_get_uri (parent); - g_object_unref (file); - g_object_unref (parent); - - if (!g_app_info_launch_default_for_uri (parent_uri, ctx, NULL)) { - g_warning ("Failed to launch the file manager\n"); - } - - g_free (parent_uri); + open_download_folder (file_uri); } notify_notification_close (notification, NULL); @@ -120,11 +147,39 @@ * will call the close callback */ } +static gboolean +supports_actions () +{ + static gboolean supports_actions = FALSE; + static gboolean have_checked = FALSE; + + if (!have_checked) { + GList *caps = NULL; + GList *c; + + have_checked = TRUE; + + caps = notify_get_server_caps (); + if (caps != NULL) { + for (c = caps; c != NULL; c = c->next) { + if (strcmp ((char*)c->data, "actions") == 0) { + supports_actions = TRUE; + break; + } + } + + g_list_foreach (caps, (GFunc) g_free, NULL); + g_list_free (caps); + } + } + + return supports_actions; +} + static void show_notification (const char *filename) { - char *file_uri, *notification_text, *display, *mime_type; - NotifyNotification *notification; + char *file_uri, *display, *mime_type; GAppInfo *app; file_uri = g_filename_to_uri (filename, NULL, NULL); @@ -134,37 +189,91 @@ } display = g_filename_display_basename (filename); - /* Translators: %s is the name of the filename received */ - notification_text = g_strdup_printf(_("You received \"%s\" via Bluetooth"), display); - g_free (display); - notification = notify_notification_new_with_status_icon (_("You received a file"), - notification_text, - "dialog-information", - GTK_STATUS_ICON (statusicon)); - - notify_notification_set_timeout (notification, NOTIFY_EXPIRES_DEFAULT); - mime_type = g_content_type_guess (filename, NULL, 0, NULL); app = g_app_info_get_default_for_type (mime_type, FALSE); - if (app != NULL) { - /* FIXME duplicate the uri, otherwise libnotify crashes */ - char *uri; + + if (supports_actions ()) { + NotifyNotification *notification; + gchar *notification_text; + + /* Translators: %s is the name of the filename received */ + notification_text = g_strdup_printf(_("You received \"%s\" via Bluetooth"), display); + notification = notify_notification_new_with_status_icon (_("You received a file"), + notification_text, + "dialog-information", + GTK_STATUS_ICON (statusicon)); + + notify_notification_set_timeout (notification, NOTIFY_EXPIRES_DEFAULT); + + if (app != NULL) { + /* FIXME duplicate the uri, otherwise libnotify crashes */ + char *uri; + uri = g_strdup (file_uri); + notify_notification_add_action (notification, "display", _("Open File"), + (NotifyActionCallback) notification_launch_action_on_file_cb, + (gpointer) uri, (GFreeFunc) g_free); + } + notify_notification_add_action (notification, "reveal", _("Reveal File"), + (NotifyActionCallback) notification_launch_action_on_file_cb, + (gpointer) file_uri, (GFreeFunc) g_free); + + g_signal_connect (G_OBJECT (notification), "closed", G_CALLBACK (on_close_notification), notification); + + if (!notify_notification_show (notification, NULL)) { + g_warning ("failed to send notification\n"); + } + g_free (notification_text); + } else { + GtkWidget *dialog; + GtkWidget *image; + gint response; + gchar *basename; + + basename = g_path_get_basename (filename); + + dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_OTHER, GTK_BUTTONS_NONE, + _("You have received the file \"%s\" over Bluetooth."), basename, + NULL); + gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE); + gtk_window_set_icon_name (GTK_WINDOW (dialog), "gnome-obex-server"); + if (app != NULL) { + image = gtk_image_new_from_gicon (g_app_info_get_icon (app), GTK_ICON_SIZE_DIALOG); + gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image); + } + gtk_window_set_title (GTK_WINDOW (dialog), _("File Received")); + gtk_dialog_add_buttons (GTK_DIALOG (dialog), + _("Open"), GTK_RESPONSE_YES, + _("Reveal"), GTK_RESPONSE_APPLY, + _("OK"), GTK_RESPONSE_OK, + NULL); + gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); + + gtk_widget_show_all (dialog); + response = gtk_dialog_run (GTK_DIALOG (dialog)); + switch (response) { + case GTK_RESPONSE_YES: + /* "Open" */ + launch_file_viewer (file_uri); + break; + + case GTK_RESPONSE_APPLY: + /* "Reveal" */ + open_download_folder (file_uri); + break; + + case GTK_RESPONSE_OK: + /* "OK" doesn't really do anything, we just dismiss the dialog. */ + break; + } + + gtk_widget_hide (dialog); + hide_statusicon (); + g_free (basename); + } + + g_free (display); + if (app != NULL) g_object_unref (app); - uri = g_strdup (file_uri); - notify_notification_add_action (notification, "display", _("Open File"), - (NotifyActionCallback) notification_launch_action_on_file_cb, - (gpointer) uri, (GFreeFunc) g_free); - } - notify_notification_add_action (notification, "reveal", _("Reveal File"), - (NotifyActionCallback) notification_launch_action_on_file_cb, - (gpointer) file_uri, (GFreeFunc) g_free); - - g_signal_connect (G_OBJECT (notification), "closed", G_CALLBACK (on_close_notification), notification); - - if (!notify_notification_show (notification, NULL)) { - g_warning ("failed to send notification\n"); - } - g_free (notification_text); } static void