[Goodies-commits] r6887 - in xfce4-screenshooter/trunk: . lib

Jerome Guelfucci jeromeg at xfce.org
Thu Mar 12 14:46:35 CET 2009


Author: jeromeg
Date: 2009-03-12 13:46:34 +0000 (Thu, 12 Mar 2009)
New Revision: 6887

Modified:
   xfce4-screenshooter/trunk/ChangeLog
   xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c
   xfce4-screenshooter/trunk/lib/screenshooter-utils.c
   xfce4-screenshooter/trunk/lib/screenshooter-utils.h
Log:
Port the saving of screenshots to GIO. Some things still need to be
polished. Some things might be broken, like opening a screenshot with
an application.

* lib/screenshooter-utils.{c,h}:
  - (screenshooter_get_home_uri) simplify the function.
  - (screenshooter_is_remote_uri) new function to detect remote uris.

* lib/screenshooter-dialogs.c:
  - (cb_progress_upload) new function to set the progress bar of the
    transfer dialog.
  - (cb_finished_upload) new function to report errors that could have
    occurred during the transfer and to close the transfer dialog when
    we are done.
  - (cb_transfer_dialog_response) new function to allow a transfer to
    be cancelled.
  - (save_screenshot_to_local_path) new function to save a screenshot
    to a local path.
  - (save_screenshot_to_remote_location) new function to save a screenshot
    to a remote place. It saves the screenshot to the temporary directory
    and copies it to the remote location. A dialog shows the progress of
    the transfer and offers to cancel it.
  - (save_screenshot_to) new function to choose between one of the two
    above save functions.
  - (screenshooter_save_screenshot) rework the function to use the
    functions listed above.


Modified: xfce4-screenshooter/trunk/ChangeLog
===================================================================
--- xfce4-screenshooter/trunk/ChangeLog	2009-03-11 19:38:52 UTC (rev 6886)
+++ xfce4-screenshooter/trunk/ChangeLog	2009-03-12 13:46:34 UTC (rev 6887)
@@ -1,3 +1,32 @@
+2009-03-12 jeromeg
+
+  Port the saving of screenshots to GIO. Some things still need to be
+  polished. Some things might be broken, like opening a screenshot with
+  an application.
+
+  * lib/screenshooter-utils.{c,h}:
+    - (screenshooter_get_home_uri) simplify the function.
+    - (screenshooter_is_remote_uri) new function to detect remote uris.
+
+  * lib/screenshooter-dialogs.c:
+    - (cb_progress_upload) new function to set the progress bar of the
+      transfer dialog.
+    - (cb_finished_upload) new function to report errors that could have
+      occurred during the transfer and to close the transfer dialog when
+      we are done.
+    - (cb_transfer_dialog_response) new function to allow a transfer to
+      be cancelled.
+    - (save_screenshot_to_local_path) new function to save a screenshot
+      to a local path.
+    - (save_screenshot_to_remote_location) new function to save a screenshot
+      to a remote place. It saves the screenshot to the temporary directory
+      and copies it to the remote location. A dialog shows the progress of
+      the transfer and offers to cancel it.
+    - (save_screenshot_to) new function to choose between one of the two
+      above save functions.
+    - (screenshooter_save_screenshot) rework the function to use the
+      functions listed above.
+
 2009-03-09 jeromeg
 
   Start porting the application to GIO. From now on,

Modified: xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c	2009-03-11 19:38:52 UTC (rev 6886)
+++ xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c	2009-03-12 13:46:34 UTC (rev 6887)
@@ -445,11 +445,156 @@
 
       sd->app = g_strdup ("none");
     }
-}                              
+}
 
 
 
-                      
+void cb_progress_upload (goffset current_num_bytes,
+                         goffset total_num_bytes,
+                         gpointer user_data)
+{
+  gdouble fraction = (double) current_num_bytes / (double) total_num_bytes;
+
+  TRACE ("Progress callback!");
+
+  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (user_data), fraction);
+}
+
+
+
+static void
+cb_finished_upload (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  GError *error = NULL;
+  gboolean success;
+
+  g_return_if_fail (G_LIKELY (G_IS_FILE (source_object)));
+
+  success = g_file_copy_finish (G_FILE (source_object), res, &error);
+
+  if (!success)
+    {
+      xfce_err (error->message);
+
+      g_error_free (error);
+    }
+
+  gtk_widget_destroy (GTK_WIDGET (user_data));
+}
+
+
+
+static void
+cb_transfer_dialog_response (GtkWidget *dialog, int response, GCancellable *cancellable)
+{
+  if (G_LIKELY (response == GTK_RESPONSE_CANCEL))
+    {
+      g_cancellable_cancel (cancellable);
+
+      gtk_widget_destroy (dialog);
+    }
+}
+ 
+
+
+static void
+save_screenshot_to_local_path (GdkPixbuf *screenshot, GFile *save_file)
+{
+  GError *error = NULL;
+  gchar *save_path = g_file_get_path (save_file);
+
+  if (!gdk_pixbuf_save (screenshot, save_path, "png", &error, NULL))
+    {
+      xfce_err ("%s", error->message);
+      
+      g_error_free (error);
+    }
+
+  g_free (save_path);
+}
+
+static void
+save_screenshot_to_remote_location (GdkPixbuf *screenshot, GFile *save_file)
+{
+  gchar *save_basename = g_file_get_basename (save_file);
+  gchar *save_path;
+  GFile *save_file_temp;
+
+  GCancellable *cancellable = g_cancellable_new ();
+  
+  GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Transfering the screenshot..."),
+                                                   NULL,
+                                                   GTK_DIALOG_NO_SEPARATOR,
+                                                   GTK_STOCK_CANCEL,
+                                                   GTK_RESPONSE_CANCEL,
+                                                   NULL);
+
+  GtkWidget *progress_bar = gtk_progress_bar_new ();
+
+  save_path = g_build_filename (g_get_tmp_dir (), save_basename, NULL);
+
+  save_file_temp = g_file_new_for_path (save_path);
+
+  save_screenshot_to_local_path (screenshot, save_file_temp);
+
+  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
+  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+  gtk_window_set_deletable (GTK_WINDOW (dialog), FALSE);
+  
+  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), 20);
+  gtk_window_set_icon_name (GTK_WINDOW (dialog), "document-save");
+
+  gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox),
+                      progress_bar,
+                      FALSE,
+                      FALSE,
+                      0);
+
+  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), 0);
+
+  gtk_widget_show (progress_bar);
+
+  gtk_widget_show (dialog);
+
+  g_signal_connect (dialog,
+                    "response",
+                    G_CALLBACK (cb_transfer_dialog_response),
+                    cancellable);
+
+  g_file_copy_async (save_file_temp,
+                     save_file,
+                     G_FILE_COPY_OVERWRITE|G_FILE_COPY_BACKUP,
+                     G_PRIORITY_DEFAULT,
+                     cancellable,
+                     (GFileProgressCallback)cb_progress_upload, progress_bar,
+                     (GAsyncReadyCallback)cb_finished_upload, dialog);
+                     
+  g_object_unref (save_file_temp);
+  g_free (save_basename);
+  g_free (save_path);
+}
+
+static void
+save_screenshot_to (GdkPixbuf *screenshot, gchar *save_uri)
+{
+  GFile *save_file = g_file_new_for_uri (save_uri); 
+    
+  /* If the URI is a local one, we save directly */
+
+  if (!screenshooter_is_remote_uri (save_uri))
+    {
+      save_screenshot_to_local_path (screenshot, save_file);
+    }
+  else
+    {
+      save_screenshot_to_remote_location (screenshot, save_file);
+    }
+  
+  g_object_unref (save_file);
+}
+
+
+
 /* Public */
 
 
@@ -969,22 +1114,21 @@
                                     gboolean        show_save_dialog,
                                     gchar          *default_dir)
 {
-  GdkPixbuf *thumbnail;
   gchar *filename = NULL, *savename = NULL;
   
-  GtkWidget *preview;
-  GtkWidget *chooser;
-  gint dialog_response;
-  
-  GError *error = NULL;
-  
   /* Generate the filename for the default save location */
  
   filename = generate_filename_for_uri (default_dir);
     
   if (show_save_dialog)
 	  {
-	    /* If the user wants a save dialog, we run it, and grab the 
+      GdkPixbuf *thumbnail;
+
+      GtkWidget *preview;
+      GtkWidget *chooser;
+      gint dialog_response;
+
+      /* If the user wants a save dialog, we run it, and grab the 
        * filename the user has chosen. */
 	    
       /* Create the dialog and set its default properties */
@@ -1001,8 +1145,9 @@
       gtk_window_set_icon_name (GTK_WINDOW (chooser), 
                                 "applets-screenshooter");
                                 
-      gtk_file_chooser_set_do_overwrite_confirmation (
-      GTK_FILE_CHOOSER (chooser), TRUE);
+      gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (chooser), TRUE);
+
+      gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
       
       gtk_dialog_set_default_response (GTK_DIALOG (chooser), 
                                        GTK_RESPONSE_ACCEPT);
@@ -1035,42 +1180,27 @@
       /* The user pressed the save button */
 	    if (dialog_response == GTK_RESPONSE_ACCEPT)
 	      {
-	        /* Get the save location */
-          savename = 
-	          gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
-          
-          /* Try to save the screenshot. If it fails, we show an error
-           * dialog */
-          
-          if (!gdk_pixbuf_save (screenshot, savename, 
-                                "png", &error, NULL))
-            {
-              xfce_err ("%s", error->message);
-              
-              g_error_free (error);
-            }
-	      }
+          gchar *save_uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (chooser));
+
+          gtk_widget_hide (chooser);
+                    
+          save_screenshot_to (screenshot, save_uri);
+
+          g_free (save_uri);
+        }
 	  
-	    gtk_widget_destroy (GTK_WIDGET (chooser));
+	    gtk_widget_destroy (chooser);
 	  }  
 	else
 	  {    
-	    
 	    /* Else, we just save the file in the default folder */
+      gchar *save_uri = g_build_filename (default_dir, filename, NULL);
       
-      savename = g_build_filename (default_dir, filename, NULL);
-	    
-      /* Try to save the screenshot. If it fails, we show an error
-      * dialog */
-      if (!gdk_pixbuf_save (screenshot, savename, "png", &error, NULL))
-            {
-              xfce_err ("%s", error->message);
-              
-              g_error_free (error);
-            }
-	    
-	  }
+      save_screenshot_to (screenshot, save_uri);
 
+      g_free (save_uri);
+    }
+
   TRACE ("Free the gchars and unref the GFiles");
 
   g_free (filename);

Modified: xfce4-screenshooter/trunk/lib/screenshooter-utils.c
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-utils.c	2009-03-11 19:38:52 UTC (rev 6886)
+++ xfce4-screenshooter/trunk/lib/screenshooter-utils.c	2009-03-12 13:46:34 UTC (rev 6887)
@@ -661,11 +661,22 @@
 {
   gchar *result = NULL;
   const gchar *home_path = xfce_get_homedir ();
-  GFile *home = g_file_new_for_path (home_path);
 
-  result = g_file_get_uri (home);
+  result = g_strconcat ("file://", home_path, NULL);
 
-  g_object_unref (home);
-
   return result;
 }
+
+
+
+gboolean screenshooter_is_remote_uri (const gchar *uri)
+{
+  g_return_val_if_fail(uri != NULL, FALSE);
+
+  /* if the URI doesn't start  with "file://", we take it as remote */
+
+  if (!g_str_has_prefix (uri, "file:"))
+    return TRUE;
+
+  return FALSE;
+}

Modified: xfce4-screenshooter/trunk/lib/screenshooter-utils.h
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-utils.h	2009-03-11 19:38:52 UTC (rev 6886)
+++ xfce4-screenshooter/trunk/lib/screenshooter-utils.h	2009-03-12 13:46:34 UTC (rev 6887)
@@ -92,4 +92,7 @@
 gchar
 *screenshooter_get_home_uri      ();
 
+gboolean
+screenshooter_is_remote_uri      (const gchar          *uri);
+
 #endif                               




More information about the Goodies-commits mailing list