aboutsummaryrefslogtreecommitdiffstats
path: root/e-util
diff options
context:
space:
mode:
Diffstat (limited to 'e-util')
-rw-r--r--e-util/ChangeLog12
-rw-r--r--e-util/e-dialog-utils.c6
-rw-r--r--e-util/e-gui-utils.c1
-rw-r--r--e-util/e-util.c233
-rw-r--r--e-util/e-util.h6
5 files changed, 240 insertions, 18 deletions
diff --git a/e-util/ChangeLog b/e-util/ChangeLog
index 5291429431..8df4256595 100644
--- a/e-util/ChangeLog
+++ b/e-util/ChangeLog
@@ -1,3 +1,15 @@
+2008-04-17 Milan Crha <mcrha@redhat.com>
+
+ ** Part of fix for bug #526739
+
+ * e-gui-utils.c: Do not include gnome-vfs.
+ * e-util.c: (e_write_file_uri), (e_file_get_save_path):
+ * e-dialog-utils.c: (e_file_can_save), (e_file_check_local):
+ Use gio instead of gnome-vfs.
+ * e-util.h:
+ * e-util.c: (e_util_guess_mime_type), (e_util_filename_to_uri),
+ (e_util_uri_to_filename), (e_util_read_file): New helper functions.
+
2008-04-12 Matthew Barnes <mbarnes@redhat.com>
* e-util-labels.c (e_util_labels_add_with_dlg):
diff --git a/e-util/e-dialog-utils.c b/e-util/e-dialog-utils.c
index 1c75dc83e5..8207e18181 100644
--- a/e-util/e-dialog-utils.c
+++ b/e-util/e-dialog-utils.c
@@ -49,8 +49,6 @@
#include <glib/gi18n.h>
#include <libgnome/gnome-util.h>
-#include <libgnomevfs/gnome-vfs-utils.h>
-
#include "e-util/e-util.h"
#include "e-util/e-error.h"
@@ -408,7 +406,7 @@ e_file_can_save(GtkWindow *parent, const char *uri)
if (!e_file_check_local(uri))
return TRUE;
- path = gnome_vfs_get_local_path_from_uri(uri);
+ path = e_util_uri_to_filename (uri);
if (!path)
return FALSE;
@@ -438,7 +436,7 @@ e_file_check_local (const char *name)
{
char *uri;
- uri = gnome_vfs_get_local_path_from_uri(name);
+ uri = e_util_uri_to_filename (name);
if (uri) {
g_free(uri);
return TRUE;
diff --git a/e-util/e-gui-utils.c b/e-util/e-gui-utils.c
index 5a828dedf6..4da3e6dcfe 100644
--- a/e-util/e-gui-utils.c
+++ b/e-util/e-gui-utils.c
@@ -29,7 +29,6 @@
#include <gtk/gtkicontheme.h>
#include <libgnome/gnome-program.h>
-#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnomeui/gnome-icon-lookup.h>
/**
diff --git a/e-util/e-util.c b/e-util/e-util.c
index 50bd7eab70..f1bc8968c4 100644
--- a/e-util/e-util.c
+++ b/e-util/e-util.c
@@ -38,8 +38,8 @@
#include <glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
+#include <gio/gio.h>
#include <libgnome/gnome-util.h>
-#include <libgnomevfs/gnome-vfs.h>
#ifdef G_OS_WIN32
#include <windows.h>
@@ -164,29 +164,59 @@ e_int_compare (gconstpointer x, gconstpointer y)
gint
e_write_file_uri (const gchar *filename, const gchar *data)
{
- guint64 length = strlen(data);
- guint64 bytes;
- GnomeVFSResult result;
- GnomeVFSHandle *handle = NULL;
+ gsize length = strlen (data);
+ gssize bytes;
+ GFile *file;
+ GOutputStream *stream;
+ GError *error = NULL;
- result = gnome_vfs_create (&handle, filename, GNOME_VFS_OPEN_WRITE, FALSE, 0644);
- if (result != GNOME_VFS_OK) {
+ file = g_file_new_for_path (filename);
+ if (!file) {
g_warning ("Couldn't save item");
return 1;
}
+ stream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error));
+ g_object_unref (file);
+
+ if (!stream || error) {
+ g_warning ("Couldn't save item%s%s", error ? ": " : "", error ? error->message : "");
+
+ if (stream)
+ g_object_unref (stream);
+
+ if (error)
+ g_error_free (error);
+
+ return 1;
+ }
+
while (length > 0) {
- gnome_vfs_write(handle, data, length, &bytes);
- if (bytes > 0) {
+ bytes = g_output_stream_write_all (stream, data, length, NULL, NULL, &error);
+ if (bytes > 0 && !error) {
length -= bytes;
data += bytes;
} else {
- gnome_vfs_close(handle);
+ g_warning ("Couldn't save item%s%s", error ? ": " : "", error ? error->message : "");
+
+ if (error)
+ g_error_free (error);
+
+ g_output_stream_close (stream, NULL, NULL);
+ g_object_unref (stream);
+
return 1;
}
}
- gnome_vfs_close (handle);
+ g_output_stream_close (stream, NULL, &error);
+ g_object_unref (stream);
+
+ if (error) {
+ g_warning ("Couldn't close output stream: %s", error->message);
+ g_error_free (error);
+ }
+
return 0;
}
@@ -1042,8 +1072,16 @@ e_file_get_save_path (void)
}
g_object_unref(gconf);
- if (uri == NULL)
- uri = gnome_vfs_get_uri_from_local_path(g_get_home_dir());
+ if (uri == NULL) {
+ GFile *file;
+
+ file = g_file_new_for_path (g_get_home_dir ());
+ if (file) {
+ uri = g_file_get_uri (file);
+ g_object_unref (file);
+ }
+ }
+
return (uri);
}
@@ -1096,3 +1134,172 @@ e_file_lock_exists ()
return g_file_test (fname, G_FILE_TEST_EXISTS);
}
+
+/**
+ * e_util_guess_mime_type:
+ * @filename: it's a local file name, or URI.
+ * Returns: NULL or newly allocated string with a mime_type of the given file. Free with g_free.
+ *
+ * Guesses mime_type for the given file_name.
+ **/
+char *
+e_util_guess_mime_type (const char *filename)
+{
+ GFile *file;
+ GFileInfo *fi;
+ char *mime_type;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ if (strstr (filename, "://"))
+ file = g_file_new_for_uri (filename);
+ else
+ file = g_file_new_for_path (filename);
+
+ if (!file)
+ return NULL;
+
+ fi = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
+ if (!fi) {
+ g_object_unref (file);
+ return NULL;
+ }
+
+ mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (fi));
+
+ g_object_unref (fi);
+ g_object_unref (file);
+
+ return mime_type;
+}
+
+/**
+ * e_util_filename_to_uri:
+ * @filename: local file name.
+ * Returns: either newly allocated string or NULL. Free with g_free.
+ *
+ * Converts local file name to URI.
+ **/
+char *
+e_util_filename_to_uri (const char *filename)
+{
+ GFile *file;
+ char *uri = NULL;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ file = g_file_new_for_path (filename);
+
+ if (file) {
+ uri = g_file_get_uri (file);
+ g_object_unref (file);
+ }
+
+ return uri;
+}
+
+/**
+ * e_util_uri_to_filename:
+ * @uri: uri.
+ * Returns: either newly allocated string or NULL. Free with g_free.
+ *
+ * Converts URI to local file name. NULL indicates no such local file name exists.
+ **/
+char *
+e_util_uri_to_filename (const char *uri)
+{
+ GFile *file;
+ char *filename = NULL;
+
+ g_return_val_if_fail (uri != NULL, NULL);
+
+ file = g_file_new_for_uri (uri);
+
+ if (file) {
+ filename = g_file_get_path (file);
+ g_object_unref (file);
+ }
+
+ return filename;
+}
+
+/**
+ * e_util_read_file:
+ * @filename: File name to read.
+ * @filename_is_uri: Whether the file name is URI, if not, then it's a local path.
+ * @buffer: Read content or the file. Should not be NULL. Returned value should be freed with g_free.
+ * @read: Number of actually read bytes. Should not be NULL.
+ * @error: Here will be returned an error from reading operations. Can be NULL. Not every time is set when returned FALSE.
+ * Returns: Whether was reading successful or not.
+ *
+ * Reads synchronously content of the file, to which is pointed either by path or by URI.
+ * Mount point should be already mounted when calling this function.
+ **/
+gboolean
+e_util_read_file (const char *filename, gboolean filename_is_uri, char **buffer, gsize *read, GError **error)
+{
+ GFile *file;
+ GFileInfo *info;
+ GError *err = NULL;
+ gboolean res = FALSE;
+
+ g_return_val_if_fail (filename != NULL, FALSE);
+ g_return_val_if_fail (buffer != NULL, FALSE);
+ g_return_val_if_fail (read != NULL, FALSE);
+
+ *buffer = NULL;
+ *read = 0;
+
+ if (filename_is_uri)
+ file = g_file_new_for_uri (filename);
+ else
+ file = g_file_new_for_path (filename);
+
+ g_return_val_if_fail (file != NULL, FALSE);
+
+ info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &err);
+
+ if (!err && info) {
+ guint64 sz;
+ char *buff;
+
+ sz = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
+ buff = g_malloc (sizeof (char) * sz);
+
+ if (buff) {
+ GInputStream *stream;
+
+ stream = G_INPUT_STREAM (g_file_read (file, NULL, &err));
+
+ if (!err && stream) {
+ res = g_input_stream_read_all (stream, buff, sz, read, NULL, &err);
+
+ if (err)
+ res = FALSE;
+
+ if (res)
+ *buffer = buff;
+ else
+ g_free (buff);
+ }
+
+ if (stream)
+ g_object_unref (stream);
+ }
+ }
+
+ if (info)
+ g_object_unref (info);
+
+ g_object_unref (file);
+
+ if (err) {
+ if (error)
+ *error = err;
+ else
+ g_error_free (err);
+ }
+
+ return res;
+}
+
diff --git a/e-util/e-util.h b/e-util/e-util.h
index 15003d7226..0f76e9e29e 100644
--- a/e-util/e-util.h
+++ b/e-util/e-util.h
@@ -117,6 +117,12 @@ gboolean e_file_lock_create (void);
void e_file_lock_destroy (void);
gboolean e_file_lock_exists (void);
+char *e_util_guess_mime_type (const char *filename);
+char *e_util_filename_to_uri (const char *filename);
+char *e_util_uri_to_filename (const char *uri);
+
+gboolean e_util_read_file (const char *filename, gboolean filename_is_uri, char **buffer, gsize *read, GError **error);
+
#ifdef __cplusplus
}
#endif /* __cplusplus */