1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* screenshot-save.c - image saving functions for CAFE Screenshot
 *
 * Copyright (C) 2001-2006  Jonathan Blandford <jrb@alum.mit.edu>
 *
 * 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
 * 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., 51 Franklin St, Fifth Floor,
 */

#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <glib/gi18n.h>

#include "screenshot-save.h"

static char *parent_dir = NULL;
static char *tmp_filename = NULL;

static void
clean_up_temporary_dir (void)
{
	char *message;
	gboolean error_occurred = FALSE;
	if (tmp_filename && g_file_test (tmp_filename, G_FILE_TEST_EXISTS))
		error_occurred = unlink (tmp_filename);
	if (parent_dir && g_file_test (parent_dir, G_FILE_TEST_EXISTS))
		error_occurred = rmdir (parent_dir) || error_occurred;

	if (error_occurred)
	{
		message = g_strdup_printf (_("Unable to clear the temporary folder:\n%s"), tmp_filename);
		g_warning ("%s", message);
		g_free (message);
	}

	g_free (tmp_filename);
	g_free (parent_dir);

	tmp_filename = NULL;
	parent_dir = NULL;
}

static char *
make_temp_directory (void)
{
	gchar *dir_name;

	// mkdtemp uses XXXXXX as a template to create a unique name for the temp dir
	dir_name = g_build_filename (g_get_tmp_dir (),
				     "cafe-screenshot.XXXXXX",
				     NULL);

	if (mkdtemp (dir_name) == NULL)
	{
		g_warning ("Failed to create temporary directory: %s", g_strerror (errno));
		g_free (dir_name);
		return NULL;
	}

	return dir_name;
}

static void
cleanup_handler (void)
{
	clean_up_temporary_dir ();
}

void
screenshot_save_start (GdkPixbuf    *pixbuf,
		       SaveFunction  callback,
		       gpointer      user_data)
{
	GError *error = NULL;

	static gboolean cleanup_registered = FALSE;

	if (!cleanup_registered)
	{
		atexit (cleanup_handler);
		cleanup_registered = TRUE;
	}

	clean_up_temporary_dir ();

	parent_dir = make_temp_directory ();

	if (parent_dir == NULL)
	{
		if (callback)
			callback (user_data);
	return;
}

	tmp_filename = g_build_filename (parent_dir,
					 _("Screenshot.png"),
					 NULL);

	if (! gdk_pixbuf_save (pixbuf, tmp_filename,
			       "png", &error,
			       "tEXt::Software", "cafe-screenshot",
			       NULL))
	{
		if (error && error->message)
			g_warning ("Failed to save screenshot: %s", error->message);
		else
			g_warning ("Failed to save screenshot: Unknown error");

		g_error_free (error);

		clean_up_temporary_dir ();
	}

	if (callback)
		callback (user_data);
}

const char *
screenshot_save_get_filename (void)
{
  return tmp_filename;
}