+ const char *tmp;
+ char *name;
+ int fd;
+
+ if ((tmp = _ev_tmp_dir (error)) == NULL)
+ return -1;
+
+ name = g_build_filename (tmp, template, NULL);
+ fd = g_mkstemp (name);
+
+ if (fd == -1) {
+ int errsv = errno;
+
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+ _("Failed to create a temporary file: %s"),
+ g_strerror (errsv));
+
+ g_free (name);
+ return -1;
+ }
+
+ if (file_name)
+ *file_name = name;
+
+ return fd;
+}
+
+static void
+close_fd_cb (gpointer fdptr)
+{
+ int fd = GPOINTER_TO_INT (fdptr);
+
+ close (fd);
+}
+
+/**
+ * ev_mkstemp_file:
+ * @template: a template string; must contain 'XXXXXX', but not necessarily as a suffix
+ * @error: a location to store a #GError
+ *
+ * Creates a temp #GFile in the evince temp directory. See ev_mkstemp() for more information.
+ *
+ * Returns: a newly allocated #GFile for the newly created temp file name, or %NULL
+ * on error with @error filled in
+ */
+GFile *
+ev_mkstemp_file (const char *template,
+ GError **error)
+{
+ char *file_name;
+ int fd;
+ GFile *file;
+
+ fd = ev_mkstemp (template, &file_name, error);
+ if (fd == -1)
+ return NULL;
+
+ file = g_file_new_for_path (file_name);
+ g_free (file_name);
+
+ g_object_set_data_full (G_OBJECT (file), "ev-mkstemp-fd",
+ GINT_TO_POINTER (fd), (GDestroyNotify) close_fd_cb);
+
+ return file;
+}
+
+/**
+ * This function is copied from
+ * http://bugzilla.gnome.org/show_bug.cgi?id=524831
+ * and renamed from g_mkdtemp to _ev_g_mkdtemp.
+ *
+ * If/when this function gets added to glib, it can be removed from
+ * evince' sources.
+ *
+ *
+ * g_mkdtemp:
+ * @tmpl: template directory name
+ *
+ * Creates a temporary directory. See the mkdtemp() documentation
+ * on most UNIX-like systems.
+ *
+ * The parameter is a string that should follow the rules for
+ * mkdtemp() templates, i.e. contain the string "XXXXXX". g_mkdtemp()
+ * is slightly more flexible than mkdtemp() in that the sequence does
+ * not have to occur at the very end of the template. The X string
+ * will be modified to form the name of a directory that didn't
+ * already exist. The string should be in the GLib file name
+ * encoding. Most importantly, on Windows it should be in UTF-8.
+ *
+ * Return value: If a temporary directory was successfully created,
+ * @tmpl will be returned with the XXXXXX string modified in such a
+ * way as to make the path unique. In case of errors, %NULL is
+ * returned.
+ */
+static gchar *
+_ev_g_mkdtemp (gchar *tmpl)
+{
+ static const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ static const int NLETTERS = sizeof (letters) - 1;
+ static int counter = 0;
+ char *XXXXXX;
+ GTimeVal tv;
+ glong value;
+ int count;
+
+ /* find the last occurrence of "XXXXXX" */
+ XXXXXX = g_strrstr (tmpl, "XXXXXX");
+
+ if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ /* Get some more or less random data. */
+ g_get_current_time (&tv);
+ value = (tv.tv_usec ^ tv.tv_sec) + counter++;
+
+ for (count = 0; count < 100; value += 7777, ++count)
+ {
+ glong v = value;
+
+ /* Fill in the random bits. */
+ XXXXXX[0] = letters[v % NLETTERS];
+ v /= NLETTERS;
+ XXXXXX[1] = letters[v % NLETTERS];
+ v /= NLETTERS;
+ XXXXXX[2] = letters[v % NLETTERS];
+ v /= NLETTERS;
+ XXXXXX[3] = letters[v % NLETTERS];
+ v /= NLETTERS;
+ XXXXXX[4] = letters[v % NLETTERS];
+ v /= NLETTERS;
+ XXXXXX[5] = letters[v % NLETTERS];
+
+ /* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
+ if (g_mkdir (tmpl, 0700) == 0)
+ return tmpl;
+
+ if (errno != EEXIST)
+ /* Any other error will apply also to other names we might
+ * try, and there are 2^32 or so of them, so give up now.
+ */
+ return NULL;
+ }
+
+ /* We got out of the loop because we ran out of combinations to try. */
+ errno = EEXIST;
+ return NULL;
+}
+
+/**
+ * ev_mkdtemp:
+ * @template: a template string; must end in 'XXXXXX'
+ * @error: a location to store a #GError
+ *
+ * Creates a temp directory in the evince temp directory.
+ *
+ * Returns: a newly allocated string with the temp directory name, or %NULL
+ * on error with @error filled in
+ */
+gchar *
+ev_mkdtemp (const char *template,
+ GError **error)
+{
+ const char *tmp;
+ char *name;
+
+ if ((tmp = _ev_tmp_dir (error)) == NULL)
+ return NULL;
+
+ name = g_build_filename (tmp, template, NULL);
+ if (_ev_g_mkdtemp (name) == NULL) {
+ int errsv = errno;
+
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
+ _("Failed to create a temporary directory: %s"),
+ g_strerror (errsv));
+
+ g_free (name);
+ return NULL;
+ }
+
+ return name;
+}
+
+/* Remove a local temp file created by evince */
+void
+ev_tmp_filename_unlink (const gchar *filename)
+{
+ if (!filename)
+ return;
+
+ if (!tmp_dir)
+ return;
+
+ if (g_str_has_prefix (filename, tmp_dir)) {
+ g_unlink (filename);
+ }
+}
+
+void
+ev_tmp_file_unlink (GFile *file)
+{
+ gboolean res;
+ GError *error = NULL;
+
+ if (!file)
+ return;
+
+ res = g_file_delete (file, NULL, &error);
+ if (!res) {
+ char *uri;