commit 3b375446aa55db67535d26478ca9b7acbf382afc
Author: Richard Hughes <richard@hughsie.com>
Date:   Thu Mar 25 15:58:05 2010 +0000

    Save the screen ICC profile in the screenshot if it has been set

diff --git a/configure.ac b/configure.ac
index 5565f4a..3c87471 100644
--- a/configure.ac
+++ b/configure.ac
@@ -77,6 +77,7 @@ LIBGTOP_REQUIRED=2.12.0
 LIBCANBERRA_GTK_REQUIRED=0.4
 
 # common checks
+PKG_CHECK_MODULES(DBUS_GLIB, dbus-glib-1)
 PKG_CHECK_MODULES(GNOME_UTILS, glib-2.0 >= $GLIB_REQUIRED dnl
                                gio-2.0 >= $GIO_REQUIRED   dnl
                                gconf-2.0                  dnl
diff --git a/gnome-screenshot/Makefile.am b/gnome-screenshot/Makefile.am
index f9c671d..0053400 100644
--- a/gnome-screenshot/Makefile.am
+++ b/gnome-screenshot/Makefile.am
@@ -8,6 +8,7 @@ INCLUDES =							\
 	$(GNOME_UTILS_CFLAGS)					\
 	$(LIBCANBERRA_GTK_CFLAGS)				\
 	$(GTHREAD_CFLAGS)					\
+	$(DBUS_GLIB_CFLAGS)					\
 	$(NULL)
 
 bin_PROGRAMS = \
@@ -35,6 +36,9 @@ gnome_screenshot_LDADD =		\
 	$(GNOME_UTILS_LIBS)		\
 	$(LIBCANBERRA_GTK_LIBS)		\
 	$(GTHREAD_LIBS)			\
+	$(DBUS_GLIB_LIBS)		\
+	-lX11				\
+	-lm				\
 	$(NULL)
 
 gnome_screenshotdir  = $(datadir)/applications
diff --git a/gnome-screenshot/gnome-screenshot.c b/gnome-screenshot/gnome-screenshot.c
index fe12c70..cf330ab 100644
--- a/gnome-screenshot/gnome-screenshot.c
+++ b/gnome-screenshot/gnome-screenshot.c
@@ -41,6 +41,7 @@
 #include <pwd.h>
 #include <X11/Xutil.h>
 #include <canberra-gtk.h>
+#include <dbus/dbus-glib.h>
 
 #include "screenshot-shadow.h"
 #include "screenshot-utils.h"
@@ -53,6 +54,7 @@
 #define GNOME_SCREENSHOT_GCONF  "/apps/gnome-screenshot"
 #define INCLUDE_BORDER_KEY      GNOME_SCREENSHOT_GCONF "/include_border"
 #define INCLUDE_POINTER_KEY     GNOME_SCREENSHOT_GCONF "/include_pointer"
+#define INCLUDE_ICC_PROFILE     GNOME_SCREENSHOT_GCONF "/include_icc_profile"
 #define LAST_SAVE_DIRECTORY_KEY GNOME_SCREENSHOT_GCONF "/last_save_directory"
 #define BORDER_EFFECT_KEY       GNOME_SCREENSHOT_GCONF "/border_effect"
 #define DELAY_KEY               GNOME_SCREENSHOT_GCONF "/delay"
@@ -103,6 +105,7 @@ static gboolean take_window_shot = FALSE;
 static gboolean take_area_shot = FALSE;
 static gboolean include_border = FALSE;
 static gboolean include_pointer = TRUE;
+static gboolean include_icc_profile = TRUE;
 static char *border_effect = NULL;
 static guint delay = 0;
 
@@ -812,9 +815,50 @@ play_sound_effect (GdkWindow *window)
 
 }
 
+static gchar *
+get_profile_for_window (GdkWindow *window)
+{
+  gboolean ret;
+  DBusGConnection *connection;
+  DBusGProxy *proxy;
+  GError *error = NULL;
+  gchar *icc_profile = NULL;
+  guint xid;
+
+  /* Connect to the color manager on the session bus */
+  connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+  proxy = dbus_g_proxy_new_for_name (connection,
+                                     "org.gnome.ColorManager",
+                                     "/org/gnome/ColorManager",
+                                     "org.gnome.ColorManager");
+
+  /* Get color profile */
+  xid = GDK_WINDOW_XID (window);
+  ret = dbus_g_proxy_call (proxy, "GetProfileForWindow", &error,
+                           G_TYPE_UINT, xid,
+                           G_TYPE_INVALID,
+                           G_TYPE_STRING, &icc_profile,
+                           G_TYPE_INVALID);
+  if (!ret) {
+    g_debug ("The request failed: %s", error->message);
+    g_error_free (error);
+    goto out;
+  }
+out:
+  g_object_unref (proxy);
+  return icc_profile;
+}
+
 static void
 finish_prepare_screenshot (char *initial_uri, GdkWindow *window, GdkRectangle *rectangle)
 {  
+  gchar *icc_profile_filename = NULL;
+  guchar *icc_profile = NULL;
+  gsize icc_profile_size;
+  gchar *icc_profile_base64 = NULL;
+  gboolean ret;
+  GError *error = NULL;
+
   ScreenshotDialog *dialog;
 
   /* always disable window border for full-desktop or selected-area screenshots */
@@ -854,7 +898,25 @@ finish_prepare_screenshot (char *initial_uri, GdkWindow *window, GdkRectangle *r
   dialog = screenshot_dialog_new (screenshot, initial_uri, take_window_shot);
   g_free (initial_uri);
 
-  screenshot_save_start (screenshot, save_done_notification, dialog);
+  /* load ICC profile */
+  if (include_icc_profile)
+    {
+      icc_profile_filename = get_profile_for_window (window);
+      if (icc_profile_filename != NULL) {
+        ret = g_file_get_contents (icc_profile_filename, (gchar **) &icc_profile, &icc_profile_size, &error);
+        if (ret) {
+          icc_profile_base64 = g_base64_encode (icc_profile, icc_profile_size);
+        } else {
+          g_debug ("could not open ICC file: %s", error->message);
+          g_error_free (error);
+        }
+      }
+    }
+
+  screenshot_save_start (screenshot, icc_profile_base64, save_done_notification, dialog);
+  g_free (icc_profile_base64);
+  g_free (icc_profile);
+  g_free (icc_profile_filename);
 
   run_dialog (dialog);
 }
@@ -1204,6 +1266,10 @@ load_options (void)
                                           INCLUDE_POINTER_KEY,
                                           NULL);
 
+  include_icc_profile = gconf_client_get_bool (gconf_client,
+                                               INCLUDE_ICC_PROFILE,
+                                               NULL);
+
   border_effect = gconf_client_get_string (gconf_client,
                                            BORDER_EFFECT_KEY,
                                            NULL);
@@ -1230,6 +1296,9 @@ save_options (void)
   gconf_client_set_bool (gconf_client,
                          INCLUDE_POINTER_KEY, include_pointer,
                          NULL);
+  gconf_client_set_bool (gconf_client,
+                         INCLUDE_ICC_PROFILE, include_icc_profile,
+                         NULL);
   gconf_client_set_int (gconf_client, DELAY_KEY, delay, NULL);
   gconf_client_set_string (gconf_client,
                            BORDER_EFFECT_KEY, border_effect,
diff --git a/gnome-screenshot/gnome-screenshot.schemas.in b/gnome-screenshot/gnome-screenshot.schemas.in
index 224fd10..a292222 100644
--- a/gnome-screenshot/gnome-screenshot.schemas.in
+++ b/gnome-screenshot/gnome-screenshot.schemas.in
@@ -65,6 +65,18 @@
     </schema>
 
     <schema>
+      <key>/schemas/apps/gnome-screenshot/include_icc_profile</key>
+      <applyto>/apps/gnome-screenshot/include_icc_profile</applyto>
+      <owner>gnome-screenshot</owner>
+      <type>bool</type>
+      <default>true</default>
+      <locale name="C">
+        <short>Include ICC Profile</short>
+        <long>Include the ICC profile in the screenshot</long>
+      </locale>
+    </schema>
+
+    <schema>
       <key>/schemas/apps/gnome-screenshot/border_effect</key>
       <applyto>/apps/gnome-screenshot/border_effect</applyto>
       <owner>gnome-screenshot</owner>
diff --git a/gnome-screenshot/screenshot-save.c b/gnome-screenshot/screenshot-save.c
index 513ce5f..936611c 100644
--- a/gnome-screenshot/screenshot-save.c
+++ b/gnome-screenshot/screenshot-save.c
@@ -178,10 +178,12 @@ signal_handler (int sig)
 
 void
 screenshot_save_start (GdkPixbuf    *pixbuf,
+                       const gchar  *icc_profile_base64,
 		       SaveFunction  callback,
 		       gpointer      user_data)
 {
   GPid pid;
+  gboolean ret;
   int parent_exit_notification[2];
   int pipe_from_child[2];
 
@@ -210,10 +212,21 @@ screenshot_save_start (GdkPixbuf    *pixbuf,
       close (parent_exit_notification [1]);
       close (pipe_from_child [0]);
 
-      if (! gdk_pixbuf_save (pixbuf, tmp_filename,
-			     "png", &error,
-			     "tEXt::Software", "gnome-screenshot",
-			     NULL))
+      if (icc_profile_base64 != NULL)
+        {
+	  ret = gdk_pixbuf_save (pixbuf, tmp_filename,
+				 "png", &error,
+				 "icc-profile", icc_profile_base64,
+				 "tEXt::Software", "gnome-screenshot",
+				 NULL);
+	} else {
+	  ret = gdk_pixbuf_save (pixbuf, tmp_filename,
+				 "png", &error,
+				 "tEXt::Software", "gnome-screenshot",
+				 NULL);
+	}
+
+      if (!ret)
 	{
 	  if (error && error->message)
 	    write (pipe_from_child[1],
diff --git a/gnome-screenshot/screenshot-save.h b/gnome-screenshot/screenshot-save.h
index 925c66f..0fb701c 100644
--- a/gnome-screenshot/screenshot-save.h
+++ b/gnome-screenshot/screenshot-save.h
@@ -25,6 +25,7 @@
 typedef void (*SaveFunction) (gpointer data);
 
 void        screenshot_save_start        (GdkPixbuf    *pixbuf,
+					  const gchar  *icc_profile_base64,
 					  SaveFunction  callback,
 					  gpointer      user_data);
 const char *screenshot_save_get_filename (void);
