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

Jerome Guelfucci jeromeg at xfce.org
Mon Mar 9 20:29:31 CET 2009


Author: jeromeg
Date: 2009-03-09 19:29:31 +0000 (Mon, 09 Mar 2009)
New Revision: 6875

Modified:
   xfce4-screenshooter/trunk/ChangeLog
   xfce4-screenshooter/trunk/lib/screenshooter-actions.c
   xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c
   xfce4-screenshooter/trunk/lib/screenshooter-utils.c
   xfce4-screenshooter/trunk/lib/screenshooter-utils.h
   xfce4-screenshooter/trunk/src/main.c
Log:
Start porting the application to GIO. From now on,
sd->screenshot_dir is an URI to the save directory.

* src/main.c (main):
  - don't do anything before gtk is initialized.
  - use a GFile for the default save directory.
  - fallback to the home URI if the directory stored in preferences
    does not exist anymore.
  - use GIO for the command line argument.
  - use xfce_err to show an error instead of g_error.

* lib/screenshooter-actions.c: fallback to the home URI.

* lib/screenshooter-utils.c:
  - (screenshooter_read_rc_file) fallback to the home URI.
  - (screenshooter_get_home_uri) new function to get the URI of the
    home folder.

* lib/screenshooter-utils.h: add he prototype of the new function.

* lib/screenshooter-dialogs.c:
  - (screenshooter_dialog_new) use uris in the gtk file chooser.
  - (screenshooter_save_screenshot) use uris in the gtk file chooser.
  - (cb_default_folder) get the uri instead of the file name.
  - (generate_filename_for_uri) use GFiles instead of plain paths.



Modified: xfce4-screenshooter/trunk/ChangeLog
===================================================================
--- xfce4-screenshooter/trunk/ChangeLog	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/ChangeLog	2009-03-09 19:29:31 UTC (rev 6875)
@@ -1,3 +1,31 @@
+2009-03-09 jeromeg
+
+  Start porting the application to GIO. From now on,
+  sd->screenshot_dir is an URI to the save directory.
+
+  * src/main.c (main):
+    - don't do anything before gtk is initialized.
+    - use a GFile for the default save directory.
+    - fallback to the home URI if the directory stored in preferences
+      does not exist anymore.
+    - use GIO for the command line argument.
+    - use xfce_err to show an error instead of g_error.
+
+  * lib/screenshooter-actions.c: fallback to the home URI.
+
+  * lib/screenshooter-utils.c:
+    - (screenshooter_read_rc_file) fallback to the home URI.
+    - (screenshooter_get_home_uri) new function to get the URI of the
+      home folder.
+
+  * lib/screenshooter-utils.h: add he prototype of the new function.
+
+  * lib/screenshooter-dialogs.c:
+    - (screenshooter_dialog_new) use uris in the gtk file chooser.
+    - (screenshooter_save_screenshot) use uris in the gtk file chooser.
+    - (cb_default_folder) get the uri instead of the file name.
+    - (generate_filename_for_uri) use GFiles instead of plain paths.
+
 2009-03-08 jeromeg
 
   Patch from Mike Massonnet.

Modified: xfce4-screenshooter/trunk/lib/screenshooter-actions.c
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-actions.c	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/lib/screenshooter-actions.c	2009-03-09 19:29:31 UTC (rev 6875)
@@ -30,7 +30,7 @@
         {
           if (sd->screenshot_dir == NULL)
             {
-              sd->screenshot_dir = g_strdup (DEFAULT_SAVE_DIRECTORY);
+              sd->screenshot_dir = screenshooter_get_home_uri ();
             }
 
           screenshooter_save_screenshot (screenshot,

Modified: xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/lib/screenshooter-dialogs.c	2009-03-09 19:29:31 UTC (rev 6875)
@@ -203,8 +203,9 @@
                                ScreenshotData  *sd)
 {
   g_free (sd->screenshot_dir);
+  
   sd->screenshot_dir = 
-    gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
+    gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (chooser));
 }
 
    
@@ -228,42 +229,54 @@
 static gchar *generate_filename_for_uri (char *uri)
 {
   gboolean exists = TRUE;
-  gchar *filename;
+  GFile *directory;
+  GFile *file;
   gchar *basename;
   gint i;
 
   if (uri == NULL)
     {
-  	  return NULL;
-    }      
+      TRACE ("URI was NULL");
 
+      return NULL;
+    }
+
+  TRACE ("Get the folder corresponding to the URI");
+
+  directory = g_file_new_for_uri (uri);
+
   basename = g_strdup (_("Screenshot.png"));
-  filename = g_build_filename (uri, basename, NULL);
-  
-  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+
+  file = g_file_get_child (directory, basename);
+
+  if (!g_file_query_exists (file, NULL))
     {
-      g_free (filename);
+      g_object_unref (file);
+      g_object_unref (directory);
       
       return basename;
     }
-  
+
+  g_object_unref (file);
   g_free (basename);
-  g_free (filename);
 
   for (i = 1; exists; ++i)
     {
       basename = g_strdup_printf (_("Screenshot-%d.png"), i);
-      filename = g_build_filename (uri, basename, NULL);
 
-      if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+      file = g_file_get_child (directory, basename);
+      
+      if (!g_file_query_exists (file, NULL))
         exists = FALSE;
 
       if (exists)
         g_free (basename);
-      
-      g_free (filename);
+
+      g_object_unref (file);
     }
-   
+
+  g_object_unref (directory);
+  
   return basename;
 }
 
@@ -779,8 +792,8 @@
     gtk_file_chooser_button_new (_("Default save location"), 
                                  GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
         
-  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dir_chooser), 
-                                       sd->screenshot_dir);
+  gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dir_chooser), 
+                                           sd->screenshot_dir);
   
   gtk_box_pack_start (GTK_BOX (save_box), 
                       dir_chooser, FALSE, 
@@ -966,7 +979,7 @@
   GError *error = NULL;
   
   /* Generate the filename for the default save location */
-  
+ 
   filename = generate_filename_for_uri (default_dir);
     
   if (show_save_dialog)
@@ -994,8 +1007,8 @@
       gtk_dialog_set_default_response (GTK_DIALOG (chooser), 
                                        GTK_RESPONSE_ACCEPT);
 
-      gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (chooser), 
-                                          default_dir);
+      gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (chooser), 
+                                               default_dir);
 
       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (chooser), 
                                          filename);
@@ -1058,6 +1071,8 @@
 	    
 	  }
 
+  TRACE ("Free the gchars and unref the GFiles");
+
   g_free (filename);
   
   return savename;

Modified: xfce4-screenshooter/trunk/lib/screenshooter-utils.c
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-utils.c	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/lib/screenshooter-utils.c	2009-03-09 19:29:31 UTC (rev 6875)
@@ -521,8 +521,10 @@
   gint action = SAVE;
   gint show_save_dialog = 1;
   gint show_mouse = 1;
-  gchar *screenshot_dir = g_strdup (DEFAULT_SAVE_DIRECTORY);
+  gchar *screenshot_dir = screenshooter_get_home_uri ();
   gchar *app = g_strdup ("none");
+
+  const gchar *home_uri = screenshooter_get_home_uri ();
   
   if (file != NULL)
     {
@@ -556,7 +558,7 @@
           screenshot_dir = 
             g_strdup (xfce_rc_read_entry (rc, 
                                           "screenshot_dir", 
-                                          DEFAULT_SAVE_DIRECTORY));
+                                          home_uri));
         }
 
       TRACE ("Close the rc file");
@@ -650,4 +652,20 @@
           g_free (command);
        }
     }
-}     
+}
+
+
+
+gchar
+*screenshooter_get_home_uri ()
+{
+  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);
+
+  g_object_unref (home);
+
+  return result;
+}

Modified: xfce4-screenshooter/trunk/lib/screenshooter-utils.h
===================================================================
--- xfce4-screenshooter/trunk/lib/screenshooter-utils.h	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/lib/screenshooter-utils.h	2009-03-09 19:29:31 UTC (rev 6875)
@@ -33,10 +33,8 @@
 
 #include <unistd.h>
 
-#define DEFAULT_SAVE_DIRECTORY xfce_get_homedir ()
 
 
-
 /* Screenshot Modes */
 enum {
   MODE_0,
@@ -89,6 +87,9 @@
 
 void
 screenshooter_open_screenshot    (gchar                *screenshot_path,
-                                  gchar                *application);                                  
+                                  gchar                *application);
 
+gchar
+*screenshooter_get_home_uri      ();
+
 #endif                               

Modified: xfce4-screenshooter/trunk/src/main.c
===================================================================
--- xfce4-screenshooter/trunk/src/main.c	2009-03-09 19:20:23 UTC (rev 6874)
+++ xfce4-screenshooter/trunk/src/main.c	2009-03-09 19:29:31 UTC (rev 6875)
@@ -160,28 +160,14 @@
 int main(int argc, char **argv)
 {
   GError *cli_error = NULL;
+  GFile *default_save_dir;
 
   ScreenshotData *sd = g_new0 (ScreenshotData, 1);
 
-  gchar *rc_file =
-    xfce_resource_lookup (XFCE_RESOURCE_CONFIG,
-                          "xfce4/xfce4-screenshooter");
+  gchar *rc_file;
 
   xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
 
-  /* Read the preferences */
-
-  screenshooter_read_rc_file (rc_file, sd);
-
-  if (rc_file != NULL)
-    g_free (rc_file);
-
-  /* Check if the directory read from the preferences is valid */
-  if (!g_file_test (sd->screenshot_dir, G_FILE_TEST_IS_DIR))
-    {
-      sd->screenshot_dir = g_strdup (DEFAULT_SAVE_DIRECTORY);
-    }
-
   /* Print a message to advise to use help when a non existing cli option is
   passed to the executable. */
   if (!gtk_init_with_args(&argc, &argv, "", entries, PACKAGE, &cli_error))
@@ -196,6 +182,28 @@
         }
     }
 
+  /* Read the preferences */
+
+  rc_file = xfce_resource_lookup (XFCE_RESOURCE_CONFIG, "xfce4/xfce4-screenshooter");
+
+  screenshooter_read_rc_file (rc_file, sd);
+
+  if (rc_file != NULL)
+    g_free (rc_file);
+
+  /* Check if the directory read from the preferences is valid */
+
+  default_save_dir = g_file_new_for_uri (sd->screenshot_dir);
+  
+  if (!g_file_query_exists (default_save_dir, NULL))
+    {
+      g_free (sd->screenshot_dir);
+
+      sd->screenshot_dir = screenshooter_get_home_uri ();
+    }
+
+  g_object_unref (default_save_dir);
+
   /* Just print the version if we are in version mode */
   if (version)
     {
@@ -256,39 +264,21 @@
       /* If the user gave a directory name, verify that it is valid */
       if (screenshot_dir != NULL)
         {
-          if (g_file_test (screenshot_dir, G_FILE_TEST_IS_DIR))
-            {
-              /* Check if the path is absolute, if not make it
-               * absolute */
-              if (g_path_is_absolute (screenshot_dir))
-                {
-                  g_free (sd->screenshot_dir);
+          default_save_dir = g_file_new_for_commandline_arg (screenshot_dir);
 
-                  sd->screenshot_dir = screenshot_dir;
-                }
-              else
-                {
-                  gchar *current_dir = g_get_current_dir ();
-
-                  g_free (sd->screenshot_dir);
-
-                  sd->screenshot_dir =
-                    g_build_filename (current_dir,
-                                      screenshot_dir,
-                                      NULL);
-
-                  g_free (current_dir);
-                  g_free (screenshot_dir);
-                }
+          if (g_file_query_exists (default_save_dir, NULL))
+            {
+              g_free (sd->screenshot_dir);
+              sd->screenshot_dir = g_file_get_uri (default_save_dir);
             }
           else
             {
-              g_warning (_("%s is not a valid directory, the default"
-                           " directory will be used."),
-                         screenshot_dir);
-
-              g_free (screenshot_dir);
+              xfce_err (_("%s is not a valid directory, the default"
+                          " directory will be used."), screenshot_dir);
             }
+
+          g_object_unref (default_save_dir);
+          g_free (screenshot_dir);
         }
 
       screenshooter_take_and_output_screenshot (sd);




More information about the Goodies-commits mailing list