[maemo-commits] [maemo-commits] r12848 - in projects/haf/trunk/hildon-desktop: . background-manager libhildondesktop src

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Thu Jul 26 14:40:19 EEST 2007
Author: jobi
Date: 2007-07-26 14:40:14 +0300 (Thu, 26 Jul 2007)
New Revision: 12848

Modified:
   projects/haf/trunk/hildon-desktop/ChangeLog
   projects/haf/trunk/hildon-desktop/background-manager/background-manager.c
   projects/haf/trunk/hildon-desktop/background-manager/background-manager.h
   projects/haf/trunk/hildon-desktop/background-manager/background-manager.xml.in
   projects/haf/trunk/hildon-desktop/background-manager/hbm-background.c
   projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-background.c
   projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-panel-window-composite.c
   projects/haf/trunk/hildon-desktop/src/hd-home-background-dialog.c
   projects/haf/trunk/hildon-desktop/src/hd-home-background.c
Log:

2007-07-26  Johan Bilien  <johan.bilien at nokia.com>

	* background-manager/background-manager.{c,h,xml.in}:
	- added cache argument, to tell the background manager
	to keep a local copy of the image file
	* libhildondesktop/hildon-desktop-background.c:
	- added cache property
	* background-manager/hbm-background.c:
	- if the cache property is set, copy the image file to it
	* src/hd-home-background.c:
	- before applying, check if the file is on a remote
	filesystem or an MMC, in which case ask the background
	manager to cache the file
	- when saving, clean the background cache directory.
	Fixes: NB#61734



Modified: projects/haf/trunk/hildon-desktop/ChangeLog
===================================================================
--- projects/haf/trunk/hildon-desktop/ChangeLog	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/ChangeLog	2007-07-26 11:40:14 UTC (rev 12848)
@@ -1,3 +1,19 @@
+2007-07-26  Johan Bilien  <johan.bilien at nokia.com>
+
+	* background-manager/background-manager.{c,h,xml.in}:
+	- added cache argument, to tell the background manager
+	to keep a local copy of the image file
+	* libhildondesktop/hildon-desktop-background.c:
+	- added cache property
+	* background-manager/hbm-background.c:
+	- if the cache property is set, copy the image file to it
+	* src/hd-home-background.c:
+	- before applying, check if the file is on a remote
+	filesystem or an MMC, in which case ask the background
+	manager to cache the file
+	- when saving, clean the background cache directory.
+	Fixes: NB#61734
+
 2007-07-25  Moises Martinez  <moises.martinez at nokia.com>
 
 	* data/applications-menu.desktop.in:

Modified: projects/haf/trunk/hildon-desktop/background-manager/background-manager.c
===================================================================
--- projects/haf/trunk/hildon-desktop/background-manager/background-manager.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/background-manager/background-manager.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -95,6 +95,7 @@
 background_manager_set_background (BackgroundManager   *manager,
                                    gint                 window_xid,
                                    const gchar         *filename,
+                                   const gchar         *cache,
                                    guint16              red,
                                    guint16              green,
                                    guint16              blue,
@@ -110,7 +111,7 @@
   gint                          width, height;
   const gchar                  *display_name;
 
-  g_debug ("set_background on %s", filename);
+  g_debug ("set_background on %s, cache as %s", filename, cache);
 
   display_name = g_getenv ("DISPLAY");
   if (!display_name)
@@ -119,6 +120,7 @@
                    background_manager_error_quark (),
                    BACKGROUND_MANAGER_ERROR_DISPLAY,
                    "Could not open display");
+      g_idle_add ((GSourceFunc)g_main_loop_quit, main_loop);
       return FALSE;
     }
 
@@ -130,6 +132,7 @@
                    BACKGROUND_MANAGER_ERROR_DISPLAY,
                    "Could not open display %s",
                    display_name);
+      g_idle_add ((GSourceFunc)g_main_loop_quit, main_loop);
       return FALSE;
     }
 
@@ -153,6 +156,7 @@
 
   background = g_object_new (HBM_TYPE_BACKGROUND,
                              "filename", filename,
+                             "cache", cache,
                              "mode", mode,
                              "color", &color,
                              "width", width,
@@ -161,6 +165,7 @@
                              NULL);
 
   *picture_xid = hbm_background_render (background, &local_error);
+  g_idle_add ((GSourceFunc)g_main_loop_quit, main_loop);
 
   if (local_error)
     {
@@ -173,6 +178,5 @@
 
   gdk_display_close (display);
 
-  g_idle_add ((GSourceFunc)g_main_loop_quit, main_loop);
   return TRUE;
 }

Modified: projects/haf/trunk/hildon-desktop/background-manager/background-manager.h
===================================================================
--- projects/haf/trunk/hildon-desktop/background-manager/background-manager.h	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/background-manager/background-manager.h	2007-07-26 11:40:14 UTC (rev 12848)
@@ -97,6 +97,7 @@
                             (BackgroundManager   *manager,
                              gint                 window_xid,
                              const gchar         *filename,
+                             const gchar         *cache,
                              guint16              red,
                              guint16              green,
                              guint16              blue,

Modified: projects/haf/trunk/hildon-desktop/background-manager/background-manager.xml.in
===================================================================
--- projects/haf/trunk/hildon-desktop/background-manager/background-manager.xml.in	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/background-manager/background-manager.xml.in	2007-07-26 11:40:14 UTC (rev 12848)
@@ -5,6 +5,7 @@
     <method name="SetBackground">
       <arg type="i" name="window_id"/>
       <arg type="s" name="filename"/>
+      <arg type="s" name="cache"/>
       <arg type="i" name="red"/>
       <arg type="i" name="green"/>
       <arg type="i" name="blue"/>

Modified: projects/haf/trunk/hildon-desktop/background-manager/hbm-background.c
===================================================================
--- projects/haf/trunk/hildon-desktop/background-manager/hbm-background.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/background-manager/hbm-background.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -50,7 +50,13 @@
 #define BUFFER_SIZE                     8192
 #define HBM_GCONF_MMC_COVER_OPEN        "/system/osso/af/mmc-cover-open"
 #define HBM_ENV_MMC_MOUNTPOINT          "MMC_MOUNTPOINT"
+#define HBM_CACHE_PERMISSION (GNOME_VFS_PERM_USER_READ | \
+                              GNOME_VFS_PERM_USER_WRITE | \
+                              GNOME_VFS_PERM_GROUP_READ | \
+                              GNOME_VFS_PERM_GROUP_WRITE | \
+                              GNOME_VFS_PERM_OTHER_READ)
 
+
 static void hbm_background_set_property (GObject            *object,
                                          guint               property_id,
                                          const GValue       *value,
@@ -79,10 +85,15 @@
   GConfClient                  *gconf_client;
   gint                          width, height;
   GnomeVFSHandle               *handle;
+  GnomeVFSHandle               *cache_handle;
 
+  GnomeVFSFileSize              bytes_read;
+
   GdkPixbuf                    *pixbuf;
   gint                          pixbuf_width, pixbuf_height;
 
+  gboolean                      caching;
+
   gboolean                      oom;
   gboolean                      on_mmc;
 
@@ -360,6 +371,42 @@
   }
 }
 
+static gboolean
+hbm_background_open_cache_file (HBMBackground *background, GError **error)
+{
+  HBMBackgroundPrivate *priv = background->priv;
+  gchar                *cache = NULL;
+  GnomeVFSResult        result;
+
+  g_object_get (background,
+                "cache", &cache,
+                NULL);
+
+  if (!cache || !*cache)
+    /* No caching */
+    return FALSE;
+
+  result = gnome_vfs_create (&priv->cache_handle,
+                             cache,
+                             GNOME_VFS_OPEN_WRITE,
+                             FALSE,
+                             HBM_CACHE_PERMISSION);
+
+  if (result != GNOME_VFS_OK)
+  {
+    g_set_error (error,
+                 BACKGROUND_MANAGER_ERROR,
+                 BACKGROUND_MANAGER_ERROR_UNREADABLE,
+                 "Unable to open `%s': %s",
+                 cache,
+                 gnome_vfs_result_to_string (result));
+
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
 static void
 hbm_background_is_oom (HBMBackground *background, GError **error)
 {
@@ -416,7 +463,6 @@
 {
   HBMBackgroundPrivate *priv = background->priv;
   GnomeVFSResult        result;
-  GnomeVFSFileSize      bytes_read;
 
   hbm_background_is_oom (background, error);
   if (*error) return FALSE;
@@ -430,7 +476,7 @@
   result = gnome_vfs_read (priv->handle,
                            priv->buffer,
                            BUFFER_SIZE,
-                           &bytes_read);
+                           &priv->bytes_read);
 
   hbm_background_is_oom (background, error);
   if (*error) return FALSE;
@@ -440,7 +486,7 @@
     GError     *local_error = NULL;
     gdk_pixbuf_loader_write (priv->loader,
                              priv->buffer,
-                             bytes_read,
+                             priv->bytes_read,
                              &local_error);
 
     if (local_error)
@@ -471,7 +517,7 @@
     return FALSE;
   }
 
-  if (result == GNOME_VFS_ERROR_EOF || bytes_read == 0)
+  if (result == GNOME_VFS_ERROR_EOF || priv->bytes_read == 0)
     return TRUE;
 
   return FALSE;
@@ -479,11 +525,49 @@
 }
 
 static void
+hbm_background_write_to_cache (HBMBackground *background, GError **error)
+{
+  HBMBackgroundPrivate *priv = background->priv;
+  GnomeVFSResult        result;
+  GnomeVFSFileSize      bytes_written;
+  GnomeVFSFileSize      to_write = priv->bytes_read;
+
+  hbm_background_is_oom (background, error);
+  if (*error) return;
+
+  result = GNOME_VFS_OK;
+
+  while (result == GNOME_VFS_OK && to_write > 0)
+  {
+    result = gnome_vfs_write (priv->cache_handle,
+                              priv->buffer,
+                              to_write,
+                              &bytes_written);
+
+    to_write -= bytes_written;
+  }
+
+  hbm_background_is_oom (background, error);
+  if (*error) return;
+
+  if (result != GNOME_VFS_OK)
+  {
+    g_set_error (error,
+                 BACKGROUND_MANAGER_ERROR,
+                 BACKGROUND_MANAGER_ERROR_IO,
+                 "Unable to write to cache: write failed");
+  }
+
+  return;
+}
+
+static void
 hbm_background_load_pixbuf (HBMBackground *background, GError **error)
 {
   HBMBackgroundPrivate *priv = background->priv;
-  gchar                *filename;
+  gchar                *filename = NULL;
   GError               *local_error = NULL;
+  gboolean              do_cache;
 
   g_object_get (background,
                 "filename", &filename,
@@ -512,6 +596,9 @@
   hbm_background_open_file (background, &local_error);
   if (local_error) goto error;
 
+  do_cache = hbm_background_open_cache_file (background, &local_error);
+  if (local_error) goto error;
+
   while (TRUE)
   {
     gboolean eof;
@@ -520,6 +607,13 @@
     if (local_error)
       goto error;
 
+    if (do_cache)
+    {
+      hbm_background_write_to_cache (background, &local_error);
+      if (local_error)
+        goto error;
+    }
+
     if (eof)
       break;
   }

Modified: projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-background.c
===================================================================
--- projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-background.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-background.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -40,7 +40,8 @@
 {
   PROP_FILENAME = 1,
   PROP_MODE,
-  PROP_COLOR
+  PROP_COLOR,
+  PROP_CACHE
 };
 
 enum
@@ -61,6 +62,7 @@
   GdkColor                     *color;
   HildonDesktopBackgroundMode   mode;
   gchar                        *filename;
+  gchar                        *cache;
 };
 
 G_DEFINE_TYPE (HildonDesktopBackground, hildon_desktop_background, G_TYPE_OBJECT);
@@ -141,6 +143,16 @@
                                    PROP_FILENAME,
                                    pspec);
 
+  pspec = g_param_spec_string ("cache",
+                               "cache",
+                               "Image local cache, for remote filesystems",
+                               "",
+                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+  g_object_class_install_property (object_class,
+                                   PROP_CACHE,
+                                   pspec);
+
+
   pspec = g_param_spec_enum ("mode",
                              "Mode",
                              "Background stretching mode",
@@ -254,6 +266,9 @@
   g_free (priv->filename);
   priv->filename = NULL;
 
+  g_free (priv->cache);
+  priv->cache = NULL;
+
   if (priv->color)
     gdk_color_free (priv->color);
   priv->color = NULL;
@@ -279,6 +294,10 @@
           else
             priv->filename = g_strdup (g_value_get_string (value));
           break;
+      case PROP_CACHE:
+          g_free (priv->cache);
+          priv->cache = g_strdup (g_value_get_string (value));
+          break;
       case PROP_MODE:
           priv->mode = g_value_get_enum (value);
           break;
@@ -306,6 +325,9 @@
       case PROP_FILENAME:
           g_value_set_string (value, priv->filename);
           break;
+      case PROP_CACHE:
+          g_value_set_string (value, priv->cache);
+          break;
       case PROP_MODE:
           g_value_set_enum (value, priv->mode);
           break;
@@ -331,6 +353,7 @@
                        "mode", priv->mode,
                        "color", priv->color,
                        "filename", priv->filename,
+                       "cache", priv->cache,
                        NULL);
 
   return dest;

Modified: projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-panel-window-composite.c
===================================================================
--- projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-panel-window-composite.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/libhildondesktop/hildon-desktop-panel-window-composite.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -325,16 +325,6 @@
                        "picture", GINT_TO_POINTER (picture));
 
     if (priv->home_picture != None)
-
-    {
-      g_debug ("paint home on %i, %i, %i, %i, %i, %i, %i, %i",
-                        priv->x + event->area.x, priv->y + event->area.y,
-                        priv->x + event->area.x, priv->y + event->area.y,
-                        event->area.x - x_offset,
-                        event->area.y - y_offset,
-                        event->area.width,
-                        event->area.height);
-
       XRenderComposite (GDK_DISPLAY (),
                         PictOpSrc,
                         priv->home_picture,
@@ -346,7 +336,6 @@
                         event->area.y - y_offset,
                         event->area.width,
                         event->area.height);
-    }
 
     if (priv->background_picture != None)
     {

Modified: projects/haf/trunk/hildon-desktop/src/hd-home-background-dialog.c
===================================================================
--- projects/haf/trunk/hildon-desktop/src/hd-home-background-dialog.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/src/hd-home-background-dialog.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -98,6 +98,7 @@
 {
   BG_IMAGE_NAME,
   BG_IMAGE_FILENAME,
+  BG_IMAGE_CACHE,
   BG_IMAGE_PRIORITY
 };
 
@@ -301,18 +302,18 @@
 
   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
   gtk_window_set_title (GTK_WINDOW (dialog), HH_SET_BG_TITLE);
-  
+
   /* Hildon Caption HBoxes */
   hbox_color = gtk_hbox_new (FALSE, 10);
   hbox_image = gtk_hbox_new (FALSE, 10);
   hbox_mode  = gtk_hbox_new (FALSE, 10);
-  
+
   priv->color_button = hildon_color_button_new ();
-  
+
   g_signal_connect_swapped (priv->color_button, "notify::color",
                             G_CALLBACK (hd_home_background_dialog_color_changed),
                             dialog);
-  
+
   priv->img_combo = gtk_combo_box_new ();
 
   g_signal_connect_swapped (priv->img_combo, "changed",
@@ -321,26 +322,26 @@
 
   renderer = gtk_cell_renderer_text_new ();
   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->img_combo),
-                              renderer, 
+                              renderer,
                               TRUE);
 
   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (priv->img_combo),
-                                  renderer, "text", 
+                                  renderer, "text",
                                   BG_IMAGE_NAME, NULL);
-  
+
   priv->mode_combo = gtk_combo_box_new_text ();
-  
-  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo), 
+
+  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo),
                              image_modes[BACKGROUND_CENTERED]);
-  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo), 
+  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo),
                              image_modes[BACKGROUND_SCALED]);
-  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo), 
+  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo),
                              image_modes[BACKGROUND_STRETCHED]);
-  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo), 
+  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo),
                              image_modes[BACKGROUND_TILED]);
-  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo), 
+  gtk_combo_box_append_text (GTK_COMBO_BOX (priv->mode_combo),
                              image_modes[BACKGROUND_CROPPED]);
-  
+
   g_signal_connect_swapped (priv->mode_combo, "changed",
                             G_CALLBACK (hd_home_background_dialog_mode_changed),
                             dialog);
@@ -365,9 +366,9 @@
                           priv->mode_combo,
                           NULL,
                           HILDON_CAPTION_OPTIONAL);
-     
+
   gtk_box_pack_start (GTK_BOX (hbox_color), color_caption,
-                      FALSE, FALSE, 0); 
+                      FALSE, FALSE, 0);
   gtk_box_pack_start (GTK_BOX (hbox_image), image_caption,
                       TRUE, TRUE, 0);
   gtk_box_pack_start (GTK_BOX (hbox_mode), mode_caption,
@@ -382,7 +383,7 @@
 
   hildon_caption_set_child_expand (HILDON_CAPTION (image_caption), TRUE);
   hildon_caption_set_child_expand (HILDON_CAPTION (mode_caption), TRUE);
-  
+
   /* Let the WM take care of the positioning */
   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
 
@@ -418,7 +419,7 @@
                                           priv->osso_context);
 #endif
         break;
-        
+
     case PROP_BACKGROUND:
         hd_home_background_dialog_set_background (
                            HD_HOME_BACKGROUND_DIALOG (gobject),
@@ -512,18 +513,18 @@
   fdialog = gtk_file_chooser_dialog_new (HILDON_HOME_FILE_CHOOSER_TITLE,
 					 GTK_WINDOW (dialog),
 					 GTK_FILE_CHOOSER_ACTION_OPEN,
-                                         HILDON_HOME_FILE_CHOOSER_SELECT, GTK_RESPONSE_OK, 
-                                         HILDON_HOME_FILE_CHOOSER_EMPTY, GTK_RESPONSE_CANCEL, 
+                                         HILDON_HOME_FILE_CHOOSER_SELECT, GTK_RESPONSE_OK,
+                                         HILDON_HOME_FILE_CHOOSER_EMPTY, GTK_RESPONSE_CANCEL,
 					 NULL);
 #endif
-  
+
 #ifdef HAVE_LIBHILDONHELP
   if (priv->osso_context)
-    hildon_help_dialog_help_enable (GTK_DIALOG(fdialog), 
+    hildon_help_dialog_help_enable (GTK_DIALOG(fdialog),
                                     HH_HELP_SELECT_IMAGE,
                                     priv->osso_context);
 #endif
-        
+
   mime_type_filter = gtk_file_filter_new();
   gtk_file_filter_add_mime_type (mime_type_filter, "image/jpeg");
   gtk_file_filter_add_mime_type (mime_type_filter, "image/gif");
@@ -541,23 +542,23 @@
 
   if (!gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fdialog),
                                             image_dir))
-    g_warning ("Couldn't set default image dir for dialog %s", 
+    g_warning ("Couldn't set default image dir for dialog %s",
                image_dir);
 
   g_free (image_dir);
-  
+
   response = gtk_dialog_run (GTK_DIALOG (fdialog));
-    
-  if (response == GTK_RESPONSE_OK) 
+
+  if (response == GTK_RESPONSE_OK)
     {
       const gchar  *filename;
 
       filename = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fdialog));
 
-      if (filename) 
+      if (filename)
         {
           GtkTreeModel *model;
-          GtkTreeIter   iter;            
+          GtkTreeIter   iter;
           gchar        *name;
 
           name = imagename_from_filename (filename);
@@ -576,7 +577,7 @@
 
           gtk_list_store_append (priv->combobox_contents,
                                  &iter);
-          
+
           priv->custom_image = gtk_tree_model_get_path (model,
                                                         &iter);
 
@@ -584,6 +585,7 @@
                               &iter,
                               BG_IMAGE_NAME, name?name:filename,
                               BG_IMAGE_FILENAME, filename,
+                              BG_IMAGE_CACHE, "",
                               BG_IMAGE_PRIORITY, G_MAXINT,
                               -1);
 
@@ -592,7 +594,7 @@
           g_free (name);
         }
 
-    }  
+    }
 
   gtk_widget_destroy (fdialog);
 
@@ -616,7 +618,8 @@
   HDHomeBackgroundDialogPrivate    *priv;
   GtkTreeIter                       iter;
   gchar                            *filename = NULL;
-  
+  gchar                            *cache = NULL;
+
   priv = HD_HOME_BACKGROUND_DIALOG_GET_PRIVATE (dialog);
 
   gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->img_combo),
@@ -625,11 +628,12 @@
   gtk_tree_model_get (GTK_TREE_MODEL (priv->combobox_contents),
                       &iter,
                       BG_IMAGE_FILENAME, &filename,
+                      BG_IMAGE_CACHE, &cache,
                       -1);
 
   if (!priv->background)
-    goto out;	  
-  
+    goto out;
+
   if (filename)
     g_object_set (G_OBJECT (priv->background),
                   "filename", filename,
@@ -638,8 +642,18 @@
     g_object_set (G_OBJECT (priv->background),
                   "filename", HD_HOME_BACKGROUND_NO_IMAGE,
                   NULL);
+
+  if (cache)
+    g_object_set (G_OBJECT (priv->background),
+                  "cache", cache,
+                  NULL);
+  else
+    g_object_set (G_OBJECT (priv->background),
+                  "cache", "",
+                  NULL);
 out:
   g_free (filename);
+  g_free (cache);
 }
 
 static void
@@ -687,15 +701,13 @@
 
   mode = gtk_combo_box_get_active (GTK_COMBO_BOX (priv->mode_combo));
 
-  g_debug ("Setting mode on dialog background to %i", mode);
-
   g_object_set (G_OBJECT (priv->background),
                 "mode", mode,
                 NULL);
 }
 
 static void
-hd_home_background_dialog_background_dir_changed 
+hd_home_background_dialog_background_dir_changed
                                     (HDHomeBackgroundDialog *dialog)
 {
   HDHomeBackgroundDialogPrivate *priv;
@@ -713,10 +725,12 @@
   else
     {
       priv->combobox_contents =
-          gtk_list_store_new (3,
+          gtk_list_store_new (4,
                               G_TYPE_STRING, /*localised descriptive name */
                               G_TYPE_STRING, /* image file path & name */
-                              G_TYPE_INT     /* image priority */);
+                              G_TYPE_STRING, /* image local cache */
+                              G_TYPE_INT);   /* image priority */
+
       gtk_combo_box_set_model (GTK_COMBO_BOX (priv->img_combo),
                                GTK_TREE_MODEL (priv->combobox_contents));
     }
@@ -726,8 +740,10 @@
   gtk_list_store_set (priv->combobox_contents,
                       &iterator,
                       BG_IMAGE_NAME, HH_SET_BG_IMAGE_NONE,
-                      BG_IMAGE_FILENAME, NULL,
-                      BG_IMAGE_PRIORITY, 0, -1);
+                      BG_IMAGE_FILENAME, "",
+                      BG_IMAGE_CACHE, "",
+                      BG_IMAGE_PRIORITY, 0,
+                      -1);
 
   /* It's the default */
   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->img_combo),
@@ -804,6 +820,7 @@
                            * empty strings */
                           ((image_name && *image_name) ? _(image_name) : image_path),
                           BG_IMAGE_FILENAME, image_path,
+                          BG_IMAGE_CACHE, "",
                           BG_IMAGE_PRIORITY, image_order,
                           -1);
 
@@ -816,15 +833,15 @@
       g_free (filename);
       g_free (image_name);
       g_free (image_path);
-    } 
+    }
 
-  if (dir) 
-    g_dir_close (dir);             
-      
+  if (dir)
+    g_dir_close (dir);
+
   g_key_file_free (kfile);
 
   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->combobox_contents),
-                                        BG_IMAGE_PRIORITY, 
+                                        BG_IMAGE_PRIORITY,
                                         GTK_SORT_ASCENDING);
 }
 
@@ -833,6 +850,7 @@
 {
   HDHomeBackgroundDialogPrivate    *priv;
   gchar                            *filename;
+  gchar                            *cache;
   GdkColor                         *color;
   BackgroundMode                    mode;
 
@@ -841,14 +859,15 @@
 
   if (!priv->background)
     return;
-  
+
   g_object_get (G_OBJECT (priv->background),
                 "color", &color,
                 "filename", &filename,
+                "cache", &cache,
                 "mode", &mode,
                 NULL);
 
-  if (filename && *filename && 
+  if (filename && *filename &&
       !g_str_equal (filename, HD_HOME_BACKGROUND_NO_IMAGE))
     {
       gboolean      valid;
@@ -872,6 +891,8 @@
             break;
 
           valid = gtk_tree_model_iter_next (model, &iter);
+
+          g_free (existing_filename);
         }
 
       if (!valid)
@@ -883,6 +904,7 @@
           gtk_list_store_set (priv->combobox_contents,
                               &iter,
                               BG_IMAGE_FILENAME, filename,
+                              BG_IMAGE_CACHE, cache,
                               BG_IMAGE_NAME, image_name,
                               BG_IMAGE_PRIORITY, G_MAXINT,
                               -1);
@@ -942,9 +964,7 @@
   if (priv->background)
     g_object_unref (priv->background);
 
-  g_debug ("Copying background");
   priv->background = hildon_desktop_background_copy (background);
-  g_debug ("Copying background done");
 
   g_object_notify (G_OBJECT (dialog), "background");
   hd_home_background_dialog_sync_from_background (dialog);

Modified: projects/haf/trunk/hildon-desktop/src/hd-home-background.c
===================================================================
--- projects/haf/trunk/hildon-desktop/src/hd-home-background.c	2007-07-26 11:18:51 UTC (rev 12847)
+++ projects/haf/trunk/hildon-desktop/src/hd-home-background.c	2007-07-26 11:40:14 UTC (rev 12848)
@@ -39,10 +39,12 @@
 #include <gdk/gdkx.h>
 
 #include <glib/gkeyfile.h>
+#include <glib/gstdio.h>
 
 /* Key used in the home-background.conf */
 #define HD_HOME_BACKGROUND_KEY_GROUP            "Hildon Home"
 #define HD_HOME_BACKGROUND_KEY_URI              "BackgroundImage"
+#define HD_HOME_BACKGROUND_KEY_CACHE            "CachedAs"
 #define HD_HOME_BACKGROUND_KEY_RED              "Red"
 #define HD_HOME_BACKGROUND_KEY_GREEN            "Green"
 #define HD_HOME_BACKGROUND_KEY_BLUE             "Blue"
@@ -56,6 +58,9 @@
 #define HD_HOME_BACKGROUND_VALUE_TILED          "Tiled"
 #define HD_HOME_BACKGROUND_VALUE_CROPPED        "Cropped"
 
+#define HD_ENV_MMC_MOUNTPOINT                   "MMC_MOUNTPOINT"
+#define HD_HOME_BACKGROUND_CACHE_DIR             ".background-cache"
+
 struct _HDHomeBackgroundPrivate {
   gboolean      cancelled;
 };
@@ -63,6 +68,50 @@
 G_DEFINE_TYPE (HDHomeBackground, hd_home_background, HILDON_DESKTOP_TYPE_BACKGROUND);
 
 static void
+hd_home_background_clean_up_cache (HDHomeBackground *background)
+{
+  gchar        *cache_dir;
+  gchar        *cache;
+  GDir         *dir;
+  GError       *error = NULL;
+  const gchar  *filename;
+
+  g_object_get (background,
+                "cache",    &cache,
+                NULL);
+
+  cache_dir = g_build_path (G_DIR_SEPARATOR_S,
+                            g_get_home_dir (),
+                            HD_HOME_BACKGROUND_CACHE_DIR,
+                            NULL);
+  dir = g_dir_open (cache_dir, 0, &error);
+
+  if (error)
+  {
+    g_warning ("Error when opening cache dir for cleaning: %s",
+               error->message);
+    g_error_free (error);
+    g_free (cache_dir);
+    return;
+  }
+
+  while ((filename = g_dir_read_name (dir)))
+  {
+    gchar *path = g_build_path (G_DIR_SEPARATOR_S, cache_dir, filename, NULL);
+
+    if (cache && g_str_equal (path, cache))
+      continue;
+
+    g_unlink (path);
+    g_free (path);
+  }
+
+  g_dir_close (dir);
+  g_free (cache_dir);
+
+}
+
+static void
 hd_home_background_save (HildonDesktopBackground *background,
                          const gchar *file,
                          GError **error)
@@ -72,6 +121,7 @@
   gchar                        *buffer;
   gssize                        buffer_length;
   gchar                        *filename;
+  gchar                        *cache;
   GdkColor                     *color;
   HildonDesktopBackgroundMode   mode;
 
@@ -81,6 +131,7 @@
 
   g_object_get (background,
                 "filename", &filename,
+                "cache",    &cache,
                 "color",    &color,
                 "mode",     &mode,
                 NULL);
@@ -98,7 +149,14 @@
                            HD_HOME_BACKGROUND_KEY_URI,
                            HD_HOME_BACKGROUND_VALUE_NO_IMAGE);
 
+  if (cache)
+    g_key_file_set_string (keyfile,
+                           HD_HOME_BACKGROUND_KEY_GROUP,
+                           HD_HOME_BACKGROUND_KEY_CACHE,
+                           cache);
 
+
+
   /* Color */
   if (color)
     {
@@ -161,6 +219,8 @@
                        buffer_length,
                        &local_error);
 
+  hd_home_background_clean_up_cache (HD_HOME_BACKGROUND (background));
+
 cleanup:
   g_key_file_free (keyfile);
 
@@ -180,6 +240,7 @@
   gint                          component;
   gchar                        *smode = NULL;
   gchar                        *filename = NULL;
+  gchar                        *cache = NULL;
   HildonDesktopBackgroundMode   mode;
   GdkColor                      color;
 
@@ -199,6 +260,17 @@
 
   if (local_error) goto cleanup;
 
+  cache = g_key_file_get_string (keyfile,
+                                 HD_HOME_BACKGROUND_KEY_GROUP,
+                                 HD_HOME_BACKGROUND_KEY_CACHE,
+                                 &local_error);
+
+  if (local_error)
+  {
+    g_clear_error (&local_error);
+    cache = NULL;
+  }
+
   /* Color */
   component = g_key_file_get_integer (keyfile,
                                       HD_HOME_BACKGROUND_KEY_GROUP,
@@ -264,6 +336,7 @@
                 "filename", filename,
                 "mode", mode,
                 "color", &color,
+                cache?"cache":NULL, cache,
                 NULL);
 
 cleanup:
@@ -274,6 +347,45 @@
     g_propagate_error (error, local_error);
 }
 
+/* We need to cache the file locally if the file is loaded from a
+ * memory card or a remote FS (samba, bluetooth...)
+ */
+static gboolean
+hd_home_background_requires_caching (HDHomeBackground *background)
+{
+  gchar        *filename = NULL;
+  const gchar  *mmc_mount_point;
+  gchar        *mmc_mount_point_uri = NULL;
+  gboolean      on_internal_mmc = FALSE, on_external_fs = FALSE;
+
+  g_object_get (background,
+                "filename", &filename,
+                NULL);
+
+  g_return_val_if_fail (filename, FALSE);
+
+  mmc_mount_point = g_getenv (HD_ENV_MMC_MOUNTPOINT);
+  mmc_mount_point_uri = g_strdup_printf ("file://%s", mmc_mount_point);
+
+  if (mmc_mount_point &&
+      (g_str_has_prefix (filename, mmc_mount_point) ||
+       g_str_has_prefix (filename, mmc_mount_point_uri)))
+    on_internal_mmc = TRUE;
+
+  g_free (mmc_mount_point_uri);
+
+  on_external_fs = filename[0] != G_DIR_SEPARATOR &&
+      (!g_str_has_prefix (filename, "file://"));
+
+  g_debug ("filename %s requires caching: %i",
+           filename,
+           (on_internal_mmc || on_external_fs));
+
+  return (on_internal_mmc || on_external_fs);
+
+
+}
+
 static void
 hd_home_background_apply (HildonDesktopBackground *background,
                           GdkWindow        *window,
@@ -283,27 +395,46 @@
   DBusGConnection      *connection;
   GError               *local_error = NULL;
   gint                  pixmap_xid;
-  GdkPixmap            *pixmap = NULL;
   gint32                top_offset, bottom_offset, right_offset, left_offset;
   gchar                *filename;
+  gchar                *cache;
+  gchar                *new_cache = NULL;
+  gchar                *file_to_use;
   GdkColor             *color;
   HildonDesktopBackgroundMode   mode;
 
   g_return_if_fail (HD_IS_HOME_BACKGROUND (background) && window);
 
+  connection = dbus_g_bus_get (DBUS_BUS_SESSION, &local_error);
+  if (local_error)
+    {
+      g_propagate_error (error, local_error);
+      return;
+    }
+
   g_object_get (background,
                 "filename", &filename,
+                "cache", &cache,
                 "color", &color,
                 "mode", &mode,
                 NULL);
 
-  connection = dbus_g_bus_get (DBUS_BUS_SESSION, &local_error);
-  if (local_error)
+  if (cache && *cache)
+    file_to_use = cache;
+  else
+  {
+    if (hd_home_background_requires_caching (HD_HOME_BACKGROUND (background)))
     {
-      g_propagate_error (error, local_error);
-      return;
+      new_cache = g_strdup_printf ("%s/%s/%x",
+                                   g_get_home_dir (),
+                                   HD_HOME_BACKGROUND_CACHE_DIR,
+                                   g_random_int ());
+      cache = new_cache;
     }
 
+    file_to_use = filename;
+  }
+
   background_manager_proxy =
       dbus_g_proxy_new_for_name (connection,
                                  HILDON_BACKGROUND_MANAGER_SERVICE,
@@ -314,7 +445,8 @@
 #define S(string) (string?string:"")
   org_maemo_hildon_background_manager_set_background (background_manager_proxy,
                                                       GDK_WINDOW_XID (window),
-                                                      S(filename),
+                                                      S(file_to_use),
+                                                      S(cache),
                                                       color->red,
                                                       color->green,
                                                       color->blue,
@@ -323,16 +455,12 @@
                                                       error);
 #undef S
 
-  if (pixmap_xid)
-    pixmap = gdk_pixmap_foreign_new (pixmap_xid);
+  if (new_cache && !error)
+    g_object_set (background,
+                  "cache", new_cache,
+                  NULL);
 
-  if (pixmap)
-    {
-      gdk_window_set_back_pixmap (window, pixmap, FALSE);
-      g_object_unref (pixmap);
-    }
-  else
-    g_warning ("No such pixmap: %i", pixmap_xid);
+  g_free (new_cache);
 
 }
 
@@ -342,6 +470,7 @@
   HildonDesktopBackgroundApplyCallback  callback;
   gpointer                              user_data;
   GdkWindow                            *window;
+  gchar                                *new_cache;
 };
 
 
@@ -370,6 +499,11 @@
 
 
 cleanup:
+  if (!error && data->new_cache)
+    g_object_set (data->background,
+                  "cache", data->new_cache,
+                  NULL);
+
   if (data->callback)
     data->callback (HILDON_DESKTOP_BACKGROUND (data->background),
                     picture_id,
@@ -378,6 +512,8 @@
 
   if (G_IS_OBJECT (data->background))
       g_object_unref (data->background);
+
+  g_free (data->new_cache);
   g_free (data);
 }
 
@@ -393,18 +529,15 @@
   GError                   *local_error = NULL;
   struct cb_data           *data;
   gchar                    *filename;
+  gchar                    *cache;
+  gchar                    *new_cache = NULL;
+  gchar                    *file_to_use;
   GdkColor                 *color;
   HildonDesktopBackgroundMode   mode;
 
   g_return_if_fail (HD_IS_HOME_BACKGROUND (background) && window);
   priv = HD_HOME_BACKGROUND (background)->priv;
 
-  g_object_get (background,
-                "filename", &filename,
-                "color", &color,
-                "mode", &mode,
-                NULL);
-
   connection = dbus_g_bus_get (DBUS_BUS_SESSION, &local_error);
   if (local_error)
     {
@@ -420,16 +553,45 @@
                                  HILDON_BACKGROUND_MANAGER_OBJECT_PATH,
                                  HILDON_BACKGROUND_MANAGER_INTERFACE);
 
+  g_object_get (background,
+                "filename", &filename,
+                "cache", &cache,
+                "color", &color,
+                "mode", &mode,
+                NULL);
+
+  g_debug ("Applying background %s aynchronously, cache: %s",
+           filename, cache);
+
+
+  if (cache && *cache)
+  {
+    file_to_use = cache;
+    cache = NULL;
+  }
+  else
+  {
+    if (hd_home_background_requires_caching (HD_HOME_BACKGROUND (background)))
+    {
+      new_cache = g_strdup_printf ("%s/%s/%x",
+                                   g_get_home_dir (),
+                                   HD_HOME_BACKGROUND_CACHE_DIR,
+                                   g_random_int ());
+      cache = new_cache;
+
+    }
+
+    file_to_use = filename;
+  }
+
   data = g_new (struct cb_data, 1);
 
   data->callback = cb;
   data->background = g_object_ref (background);
   data->user_data = user_data;
   data->window = window;
+  data->new_cache = new_cache;
 
-  g_debug ("Applying background %s aynchronously",
-           filename);
-
   priv->cancelled = FALSE;
 
   /* Here goes */
@@ -437,7 +599,8 @@
   org_maemo_hildon_background_manager_set_background_async
                                                 (background_manager_proxy,
                                                  GDK_WINDOW_XID (window),
-                                                 S(filename),
+                                                 S(file_to_use),
+                                                 S(cache),
                                                  color->red,
                                                  color->green,
                                                  color->blue,
@@ -462,12 +625,14 @@
   HildonDesktopBackground              *dest;
   HildonDesktopBackgroundMode           mode;
   gchar                                *filename;
+  gchar                                *cache;
   GdkColor                             *color;
 
   g_return_val_if_fail (HD_IS_HOME_BACKGROUND (src), NULL);
 
   g_object_get (src,
                 "filename", &filename,
+                "cache",    &cache,
                 "color",    &color,
                 "mode",     &mode,
                 NULL);
@@ -476,6 +641,7 @@
                        "mode",          mode,
                        "color",         color,
                        "filename",      filename,
+                       "cache",         cache,
                        NULL);
 
   return dest;
@@ -493,6 +659,7 @@
 hd_home_background_class_init (HDHomeBackgroundClass *klass)
 {
   HildonDesktopBackgroundClass *background_class;
+  gchar                        *cache_dir = NULL;
 
   background_class = HILDON_DESKTOP_BACKGROUND_CLASS (klass);
 
@@ -503,6 +670,13 @@
   background_class->apply_async = hd_home_background_apply_async;
   background_class->copy = hd_home_background_copy;
 
+  cache_dir = g_build_path (G_DIR_SEPARATOR_S,
+                            g_get_home_dir (),
+                            HD_HOME_BACKGROUND_CACHE_DIR,
+                            NULL);
+  g_mkdir (cache_dir, 0755);
+  g_free (cache_dir);
+
   g_type_class_add_private (klass, sizeof (HDHomeBackgroundPrivate));
 }
 


More information about the maemo-commits mailing list