From 7ac04a3b7faa403472f56e37552fe59c63aebad2 Mon Sep 17 00:00:00 2001
From: Jerome Guelfucci <jeromeg@xfce.org>
Date: Fri, 6 Jun 2008 17:49:46 +0000
Subject: [PATCH] Reorganize code.

(Old svn revision: 4898)
---
 ChangeLog                                     |  13 ++
 Makefile.am                                   |   2 +-
 configure.ac                                  |   2 +-
 {panel-plugin => src}/Makefile.am             |   3 +-
 src/screenshooter-utils.c                     | 149 ++++++++++++++++++
 src/screenshooter-utils.h                     |  37 +++++
 {panel-plugin => src}/screenshooter.c         | 114 ++------------
 .../screenshooter.desktop.in.in               |   0
 8 files changed, 212 insertions(+), 108 deletions(-)
 rename {panel-plugin => src}/Makefile.am (94%)
 create mode 100644 src/screenshooter-utils.c
 create mode 100644 src/screenshooter-utils.h
 rename {panel-plugin => src}/screenshooter.c (85%)
 rename {panel-plugin => src}/screenshooter.desktop.in.in (100%)

diff --git a/ChangeLog b/ChangeLog
index 46d5430c..4572edef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2008-06-06 19:19 jeromeg
+
+	* Move the code to a src/ folder and remove the panel-plugin one.
+	* src/screenshooter-utils.{c,h}: moved generic code here.
+	* src/screenshooter.c: updated to use the generic code.
+	* Update the makefiles and autotools to use new code structure.
+	
+2008-06-06 15:35 jeromeg
+
+	* panel-plugin/screenshooter.c:
+	  - remove deprecated function.
+	  - set button sensitive at the end of the screenshot procedure.
+
 2008-06-06 15:19 jeromeg
 
 	* panel-plugin/screenshooter.c:
diff --git a/Makefile.am b/Makefile.am
index 23f8f3e4..69eda5ba 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
 @SET_MAKE@
 
-SUBDIRS = panel-plugin po
+SUBDIRS = src po
 
 distclean-local:
 	rm -rf *.cache *~
diff --git a/configure.ac b/configure.ac
index a1ea977c..0b7f49c2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,7 +43,7 @@ XDT_FEATURE_DEBUG()
 
 AC_OUTPUT([
 Makefile
-panel-plugin/Makefile
+src/Makefile
 po/Makefile.in
 ])
 
diff --git a/panel-plugin/Makefile.am b/src/Makefile.am
similarity index 94%
rename from panel-plugin/Makefile.am
rename to src/Makefile.am
index d35d004c..4c39a575 100644
--- a/panel-plugin/Makefile.am
+++ b/src/Makefile.am
@@ -9,7 +9,8 @@ xfce4_screenshooter_plugin_LDFLAGS =						\
 	@LIBXFCE4PANEL_LIBS@
 
 xfce4_screenshooter_plugin_SOURCES =						\
-	screenshooter.c
+	screenshooter.c																\
+	screenshooter-utils.c 												
 
 # .desktop file
 #
diff --git a/src/screenshooter-utils.c b/src/screenshooter-utils.c
new file mode 100644
index 00000000..7fbd7983
--- /dev/null
+++ b/src/screenshooter-utils.c
@@ -0,0 +1,149 @@
+/*  $Id$
+ *
+ *  Copyright © 2004 German Poo-Caaman~o <gpoo@ubiobio.cl>
+ *  Copyright © 2005,2006 Daniel Bobadilla Leal <dbobadil@dcc.uchile.cl>
+ *  Copyright © 2005 Jasper Huijsmans <jasper@xfce.org>
+ *  Copyright © 2006 Jani Monoses <jani@ubuntu.com>
+ *  Copyright © 2008 Jérôme Guelfucci <jerome.guelfucci@gmail.com>
+ *
+ *  Portions from the Gimp sources by
+ *  Copyright © 1998-2000 Sven Neumann <sven@gimp.org>
+ *  Copyright © 2003 Henrik Brix Andersen <brix@gimp.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <screenshooter-utils.h>
+
+
+/* Prototypes */
+
+static Window get_window_property (Window xwindow, Atom atom);
+static Window find_toplevel_window (Window xid);
+static Window screenshot_find_active_window (void);
+
+/* Internals */
+
+/* Borrowed from libwnck */
+static Window
+get_window_property (Window  xwindow,
+		     Atom    atom)
+{
+  Atom type;
+  int format;
+  gulong nitems;
+  gulong bytes_after;
+  Window *w;
+  int err, result;
+  Window retval;
+
+  gdk_error_trap_push ();
+
+  type = None;
+  result = XGetWindowProperty (gdk_display,
+			       xwindow,
+			       atom,
+			       0, G_MAXLONG,
+			       False, XA_WINDOW, &type, &format, &nitems,
+			       &bytes_after, (unsigned char **) &w);  
+  err = gdk_error_trap_pop ();
+
+  if (err != Success ||
+      result != Success)
+    return None;
+  
+  if (type != XA_WINDOW)
+    {
+      XFree (w);
+      return None;
+    }
+
+  retval = *w;
+  XFree (w);
+  
+  return retval;
+}
+
+/* Borrowed from gnome-screenshot */
+static Window
+screenshot_find_active_window (void)
+{
+  Window retval = None;
+  Window root_window;
+
+  root_window = GDK_ROOT_WINDOW ();
+
+  if (gdk_net_wm_supports (gdk_atom_intern ("_NET_ACTIVE_WINDOW", FALSE)))
+    {
+      retval = get_window_property (root_window,
+				    gdk_x11_get_xatom_by_name ("_NET_ACTIVE_WINDOW"));
+    }
+
+  return retval;  
+}
+
+/* Borrowed from gnome-screenshot */
+static Window
+find_toplevel_window (Window xid)
+{
+  Window root, parent, *children;
+  unsigned int nchildren;
+
+  do
+    {
+      if (XQueryTree (GDK_DISPLAY (), xid, &root,
+		      &parent, &children, &nchildren) == 0)
+	{
+	  g_warning ("Couldn't find window manager window");
+	  return None;
+	}
+
+      if (root == parent)
+	return xid;
+
+      xid = parent;
+    }
+  while (TRUE);
+}
+
+/* Public */
+
+GdkPixbuf *take_screenshot (gint fullscreen, gint delay)
+{
+  GdkPixbuf * screenshot;
+  GdkWindow * window;
+  gint width;
+  gint height;
+  
+  if (fullscreen)
+  {
+    window = gdk_get_default_root_window();
+  } 
+  else 
+  {
+    window = gdk_window_foreign_new (find_toplevel_window (screenshot_find_active_window ()));
+  }
+  
+  sleep(delay);
+  
+  gdk_drawable_get_size(window, &width, &height);
+
+  screenshot = gdk_pixbuf_get_from_drawable (NULL,
+					     window,
+					     NULL, 0, 0, 0, 0,
+					     width, height);
+	
+	return screenshot;
+}
diff --git a/src/screenshooter-utils.h b/src/screenshooter-utils.h
new file mode 100644
index 00000000..35e777c5
--- /dev/null
+++ b/src/screenshooter-utils.h
@@ -0,0 +1,37 @@
+/*  $Id$
+ *
+ *  Copyright © 2004 German Poo-Caaman~o <gpoo@ubiobio.cl>
+ *  Copyright © 2005,2006 Daniel Bobadilla Leal <dbobadil@dcc.uchile.cl>
+ *  Copyright © 2005 Jasper Huijsmans <jasper@xfce.org>
+ *  Copyright © 2006 Jani Monoses <jani@ubuntu.com>
+ *  Copyright © 2008 Jérôme Guelfucci <jerome.guelfucci@gmail.com>
+ *
+ *  Portions from the Gimp sources by
+ *  Copyright © 1998-2000 Sven Neumann <sven@gimp.org>
+ *  Copyright © 2003 Henrik Brix Andersen <brix@gimp.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+ 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gtk/gtk.h>
+#include <gdk/gdkx.h>
+
+#include <X11/Xatom.h>
+
+GdkPixbuf *take_screenshot (gint fullscreen, gint delay);
diff --git a/panel-plugin/screenshooter.c b/src/screenshooter.c
similarity index 85%
rename from panel-plugin/screenshooter.c
rename to src/screenshooter.c
index 1926d80c..83c07a7e 100644
--- a/panel-plugin/screenshooter.c
+++ b/src/screenshooter.c
@@ -4,6 +4,7 @@
  *  Copyright © 2005,2006 Daniel Bobadilla Leal <dbobadil@dcc.uchile.cl>
  *  Copyright © 2005 Jasper Huijsmans <jasper@xfce.org>
  *  Copyright © 2006 Jani Monoses <jani@ubuntu.com>
+ *  Copyright © 2008 Jérôme Guelfucci <jerome.guelfucci@gmail.com>
  *
  *  Portions from the Gimp sources by
  *  Copyright © 1998-2000 Sven Neumann <sven@gimp.org>
@@ -40,6 +41,8 @@
 #include <fcntl.h>
 #include <X11/Xatom.h>
 
+#include "screenshooter-utils.h"
+
 #define SCREENSHOT_ICON_NAME  "applets-screenshooter"
 #define MODE 0644
 
@@ -68,7 +71,6 @@ typedef struct
 }
 ScreenshotData;
 
-
 /* Panel Plugin Interface */
 
 static void screenshot_properties_dialog (XfcePanelPlugin *plugin,
@@ -77,7 +79,6 @@ static void screenshot_construct (XfcePanelPlugin * plugin);
 
 XFCE_PANEL_PLUGIN_REGISTER_INTERNAL (screenshot_construct);
 
-
 /* internal functions */
 
 static gboolean
@@ -113,88 +114,6 @@ screenshot_free_data (XfcePanelPlugin * plugin, ScreenshotData * sd)
     g_free (sd);
 }
 
-/* Borrowed from libwnck */
-static Window
-get_window_property (Window  xwindow,
-		     Atom    atom)
-{
-  Atom type;
-  int format;
-  gulong nitems;
-  gulong bytes_after;
-  Window *w;
-  int err, result;
-  Window retval;
-
-  gdk_error_trap_push ();
-
-  type = None;
-  result = XGetWindowProperty (gdk_display,
-			       xwindow,
-			       atom,
-			       0, G_MAXLONG,
-			       False, XA_WINDOW, &type, &format, &nitems,
-			       &bytes_after, (unsigned char **) &w);  
-  err = gdk_error_trap_pop ();
-
-  if (err != Success ||
-      result != Success)
-    return None;
-  
-  if (type != XA_WINDOW)
-    {
-      XFree (w);
-      return None;
-    }
-
-  retval = *w;
-  XFree (w);
-  
-  return retval;
-}
-
-/* Borrowed from gnome-screenshot */
-static Window
-screenshot_find_active_window (void)
-{
-  Window retval = None;
-  Window root_window;
-
-  root_window = GDK_ROOT_WINDOW ();
-
-  if (gdk_net_wm_supports (gdk_atom_intern ("_NET_ACTIVE_WINDOW", FALSE)))
-    {
-      retval = get_window_property (root_window,
-				    gdk_x11_get_xatom_by_name ("_NET_ACTIVE_WINDOW"));
-    }
-
-  return retval;  
-}
-
-/* Borrowed from gnome-screenshot */
-static Window
-find_toplevel_window (Window xid)
-{
-  Window root, parent, *children;
-  unsigned int nchildren;
-
-  do
-    {
-      if (XQueryTree (GDK_DISPLAY (), xid, &root,
-		      &parent, &children, &nchildren) == 0)
-	{
-	  g_warning ("Couldn't find window manager window");
-	  return None;
-	}
-
-      if (root == parent)
-	return xid;
-
-      xid = parent;
-    }
-  while (TRUE);
-}
-
 gchar *generate_filename_for_uri(char *uri){
     int test;
     gchar *file_name;
@@ -221,32 +140,17 @@ button_clicked(GtkWidget * button,  ScreenshotData * sd)
 {
     GdkPixbuf * screenshot;
     GdkPixbuf * thumbnail;
-    GdkWindow * window;
     gint width;
     gint height;
     gint dialog_response;
     gchar * filename = NULL;
-
-    if (sd->whole_screen)
-    {
-       window = gdk_get_default_root_window();
-    } 
-    else 
-    {
-       window = gdk_window_foreign_new (find_toplevel_window (screenshot_find_active_window ()));
-    }
-
-    /* delay, we make the button unclickable so that no other screenshot 
-       can be taken at the same time */
+    
     gtk_widget_set_sensitive(GTK_WIDGET (sd->button), FALSE);
-    sleep( sd->screenshot_delay);
-
-    gdk_drawable_get_size(window, &width, &height);
-
-    screenshot = gdk_pixbuf_get_from_drawable (NULL,
-					       window,
-					       NULL, 0, 0, 0, 0,
-					       width, height);
+    
+    screenshot = take_screenshot(sd->whole_screen, sd->screenshot_delay);
+    
+    width = gdk_pixbuf_get_width(screenshot);
+    height = gdk_pixbuf_get_height(screenshot);
 
     thumbnail = gdk_pixbuf_scale_simple (screenshot,
 				         width/5,
diff --git a/panel-plugin/screenshooter.desktop.in.in b/src/screenshooter.desktop.in.in
similarity index 100%
rename from panel-plugin/screenshooter.desktop.in.in
rename to src/screenshooter.desktop.in.in
-- 
GitLab