From e7d660625e7faf6501a8aa200c55364301f9d282 Mon Sep 17 00:00:00 2001 From: Andy Hertzfeld Date: Mon, 5 Jun 2000 08:21:16 +0000 Subject: [PATCH] added some web linksets and made nautilus-link support remote images by added some web linksets and made nautilus-link support remote images by fetching them through http and caching them locally. --- ChangeLog | 16 +++ data/linksets/Makefile.am | 2 + data/linksets/portals.xml | 5 + data/linksets/search_engines.xml | 5 + libnautilus-extensions/Makefile.am | 2 + .../nautilus-file-utilities.c | 1 + libnautilus-extensions/nautilus-link.c | 115 +++++++++++++++++- libnautilus-private/Makefile.am | 2 + libnautilus-private/nautilus-file-utilities.c | 1 + libnautilus-private/nautilus-link.c | 115 +++++++++++++++++- 10 files changed, 258 insertions(+), 6 deletions(-) create mode 100644 data/linksets/portals.xml create mode 100644 data/linksets/search_engines.xml diff --git a/ChangeLog b/ChangeLog index 5d84313e9..b59d47679 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2000-06-05 Andy Hertzfeld + + * data/linksets/Makefile.am: + * data/linksets/portals.xml: + * data/linksets/search_engines.xml: + added 2 new linksets of web links + * libnautilus-extensions/Makefile.am: + made us link with ghttp + * libnautilus-extensions/nautilus-file-utilities.c: + (nautilus_get_user_main_directory): + install the search engines linkset by default (temporarily until we have more UI) + * libnautilus-extensions/nautilus-link.c: (load_image_from_http), + (make_local_path), (nautilus_link_get_image_uri): + made links handle remote images by loading them through http using g_http, and + caching them locally. + 2000-06-04 Andy Hertzfeld * libnautilus-extensions/nautilus-global-preferences.c: diff --git a/data/linksets/Makefile.am b/data/linksets/Makefile.am index 8dd5b864d..45b7b8d8f 100644 --- a/data/linksets/Makefile.am +++ b/data/linksets/Makefile.am @@ -5,6 +5,8 @@ linkdir = $(datadir)/nautilus/linksets link_DATA = \ apps.xml \ desktop.xml \ + portals.xml \ + search_engines.xml \ $(NULL) EXTRA_DIST = $(link_DATA) diff --git a/data/linksets/portals.xml b/data/linksets/portals.xml new file mode 100644 index 000000000..efee8afad --- /dev/null +++ b/data/linksets/portals.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/data/linksets/search_engines.xml b/data/linksets/search_engines.xml new file mode 100644 index 000000000..25e3f494a --- /dev/null +++ b/data/linksets/search_engines.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/libnautilus-extensions/Makefile.am b/libnautilus-extensions/Makefile.am index 4d83dc7f7..bb164bb86 100644 --- a/libnautilus-extensions/Makefile.am +++ b/libnautilus-extensions/Makefile.am @@ -6,6 +6,7 @@ INCLUDES= -I$(top_srcdir) -I$(top_builddir) -I$(top_builddir)/libnautilus -I. \ $(GNOME_CFLAGS) \ $(BONOBO_CFLAGS) \ $(OAF_CFLAGS) \ + $(GHTTP_CFLAGS) \ $(GNOMECANVASPIXBUF_INCLUDEDIR) \ $(VFS_CFLAGS) \ $(XML_CFLAGS) \ @@ -16,6 +17,7 @@ libnautilus_extensions_la_LDFLAGS=\ $(GNOME_LIBS) \ $(OAF_LIBS) \ $(BONOBO_LIBS) \ + $(GHTTP_LIBS) \ $(GNOMECANVASPIXBUF_LIBS) \ $(VFS_LIBS) \ $(XML_LIBS) \ diff --git a/libnautilus-extensions/nautilus-file-utilities.c b/libnautilus-extensions/nautilus-file-utilities.c index e4202d166..eca446d21 100644 --- a/libnautilus-extensions/nautilus-file-utilities.c +++ b/libnautilus-extensions/nautilus-file-utilities.c @@ -209,6 +209,7 @@ nautilus_get_user_main_directory (void) /* install the default link set */ nautilus_link_set_install(user_main_directory, "apps"); + nautilus_link_set_install(user_main_directory, "search_engines"); } } diff --git a/libnautilus-extensions/nautilus-link.c b/libnautilus-extensions/nautilus-link.c index 464fb04bc..7396ca789 100644 --- a/libnautilus-extensions/nautilus-link.c +++ b/libnautilus-extensions/nautilus-link.c @@ -28,15 +28,20 @@ #include #include -#include +#include + +#include #include "nautilus-link.h" +#include "nautilus-file-utilities.h" #include "nautilus-metadata.h" #include "nautilus-string.h" #include "nautilus-xml-extensions.h" #include "nautilus-global-preferences.h" #include "nautilus-widgets/nautilus-preferences.h" +#define REMOTE_ICON_DIR_PERMISSIONS (GNOME_VFS_PERM_USER_ALL | GNOME_VFS_PERM_GROUP_ALL | GNOME_VFS_PERM_OTHER_ALL) + /* given a uri, returns TRUE if it's a link file */ gboolean @@ -70,14 +75,100 @@ char* nautilus_link_get_additional_text(const char *link_file_uri) return extra_text; } +/* utility routine to load an image through http asynchronously */ + +static void +load_image_from_http (char* uri, char* destination_path) + { + FILE* outfile; + gint length; + char* body; + ghttp_request* request; + char* proxy; + + request = NULL; + proxy = g_getenv ("http_proxy"); + + request = ghttp_request_new(); + + if (!request) + return; + + if (proxy && (ghttp_set_proxy (request, proxy) != 0)) { + ghttp_request_destroy (request); + return; + } + + if (ghttp_set_uri (request, uri) != 0) { + ghttp_request_destroy (request); + return; + } + + ghttp_set_type (request, ghttp_type_get); + ghttp_set_header (request, http_hdr_Accept, "image/*"); + ghttp_set_header (request, http_hdr_User_Agent, "Nautilus/0.1 (Linux)"); + + ghttp_set_header (request, http_hdr_Connection, "close"); + + if (ghttp_prepare (request) != 0) { + ghttp_request_destroy (request); + return; + } + + /* soon we'll be asynchronous, but this first implementation is synchrounous */ + + /* initiate the request */ + + /* here's where it spends all the time doing the actual request */ + if (ghttp_process (request) != ghttp_done) { + ghttp_request_destroy (request); + return; + } + + /* get the result and save it to the destination file */ + outfile = fopen(destination_path, "w"); + + length = ghttp_get_body_len(request); + body = ghttp_get_body(request); + + if (body != NULL) + fwrite(body, length, 1, outfile); + fclose(outfile); + + ghttp_request_destroy(request); + return; +} + +/* utility to return the local pathname of a cached icon, given the leaf name */ +/* if the icons directory hasn't been created yet, create it */ + +static char * +make_local_path(const char *image_name) +{ + GnomeVFSResult result; + char *escaped_uri, *local_directory_path, *local_file_path; + + escaped_uri = nautilus_str_escape_slashes (image_name + 7); + local_directory_path = g_strdup_printf("%s/.nautilus/remote_icons", g_get_home_dir()); + + /* we must create the directory if it doesnt exist */ + result = gnome_vfs_make_directory(local_directory_path, REMOTE_ICON_DIR_PERMISSIONS); + + local_file_path = nautilus_make_path(local_directory_path, escaped_uri); + g_free(escaped_uri); + g_free(local_directory_path); + return local_file_path; +} + /* returns the image associated with a link file */ char* nautilus_link_get_image_uri(const char *link_file_uri) { xmlDoc *doc; - char *file_uri; - char *icon_str = NULL; + char *file_uri, *icon_str; + char *local_path; + icon_str = NULL; if (link_file_uri == NULL) return NULL; @@ -92,6 +183,24 @@ nautilus_link_get_image_uri(const char *link_file_uri) xmlFreeDoc (doc); } g_free(file_uri); + + /* if the image is remote, see if we can find it in our local cache */ + + if (nautilus_str_has_prefix(icon_str, "http://")) { + + local_path = make_local_path(icon_str); + + if (g_file_exists(local_path)) { + g_free(icon_str); + return local_path; + } + + /* for now, handle things synchronously */ + load_image_from_http(icon_str, local_path); + g_free(icon_str); + return local_path; + } + return icon_str; } diff --git a/libnautilus-private/Makefile.am b/libnautilus-private/Makefile.am index 4d83dc7f7..bb164bb86 100644 --- a/libnautilus-private/Makefile.am +++ b/libnautilus-private/Makefile.am @@ -6,6 +6,7 @@ INCLUDES= -I$(top_srcdir) -I$(top_builddir) -I$(top_builddir)/libnautilus -I. \ $(GNOME_CFLAGS) \ $(BONOBO_CFLAGS) \ $(OAF_CFLAGS) \ + $(GHTTP_CFLAGS) \ $(GNOMECANVASPIXBUF_INCLUDEDIR) \ $(VFS_CFLAGS) \ $(XML_CFLAGS) \ @@ -16,6 +17,7 @@ libnautilus_extensions_la_LDFLAGS=\ $(GNOME_LIBS) \ $(OAF_LIBS) \ $(BONOBO_LIBS) \ + $(GHTTP_LIBS) \ $(GNOMECANVASPIXBUF_LIBS) \ $(VFS_LIBS) \ $(XML_LIBS) \ diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c index e4202d166..eca446d21 100644 --- a/libnautilus-private/nautilus-file-utilities.c +++ b/libnautilus-private/nautilus-file-utilities.c @@ -209,6 +209,7 @@ nautilus_get_user_main_directory (void) /* install the default link set */ nautilus_link_set_install(user_main_directory, "apps"); + nautilus_link_set_install(user_main_directory, "search_engines"); } } diff --git a/libnautilus-private/nautilus-link.c b/libnautilus-private/nautilus-link.c index 464fb04bc..7396ca789 100644 --- a/libnautilus-private/nautilus-link.c +++ b/libnautilus-private/nautilus-link.c @@ -28,15 +28,20 @@ #include #include -#include +#include + +#include #include "nautilus-link.h" +#include "nautilus-file-utilities.h" #include "nautilus-metadata.h" #include "nautilus-string.h" #include "nautilus-xml-extensions.h" #include "nautilus-global-preferences.h" #include "nautilus-widgets/nautilus-preferences.h" +#define REMOTE_ICON_DIR_PERMISSIONS (GNOME_VFS_PERM_USER_ALL | GNOME_VFS_PERM_GROUP_ALL | GNOME_VFS_PERM_OTHER_ALL) + /* given a uri, returns TRUE if it's a link file */ gboolean @@ -70,14 +75,100 @@ char* nautilus_link_get_additional_text(const char *link_file_uri) return extra_text; } +/* utility routine to load an image through http asynchronously */ + +static void +load_image_from_http (char* uri, char* destination_path) + { + FILE* outfile; + gint length; + char* body; + ghttp_request* request; + char* proxy; + + request = NULL; + proxy = g_getenv ("http_proxy"); + + request = ghttp_request_new(); + + if (!request) + return; + + if (proxy && (ghttp_set_proxy (request, proxy) != 0)) { + ghttp_request_destroy (request); + return; + } + + if (ghttp_set_uri (request, uri) != 0) { + ghttp_request_destroy (request); + return; + } + + ghttp_set_type (request, ghttp_type_get); + ghttp_set_header (request, http_hdr_Accept, "image/*"); + ghttp_set_header (request, http_hdr_User_Agent, "Nautilus/0.1 (Linux)"); + + ghttp_set_header (request, http_hdr_Connection, "close"); + + if (ghttp_prepare (request) != 0) { + ghttp_request_destroy (request); + return; + } + + /* soon we'll be asynchronous, but this first implementation is synchrounous */ + + /* initiate the request */ + + /* here's where it spends all the time doing the actual request */ + if (ghttp_process (request) != ghttp_done) { + ghttp_request_destroy (request); + return; + } + + /* get the result and save it to the destination file */ + outfile = fopen(destination_path, "w"); + + length = ghttp_get_body_len(request); + body = ghttp_get_body(request); + + if (body != NULL) + fwrite(body, length, 1, outfile); + fclose(outfile); + + ghttp_request_destroy(request); + return; +} + +/* utility to return the local pathname of a cached icon, given the leaf name */ +/* if the icons directory hasn't been created yet, create it */ + +static char * +make_local_path(const char *image_name) +{ + GnomeVFSResult result; + char *escaped_uri, *local_directory_path, *local_file_path; + + escaped_uri = nautilus_str_escape_slashes (image_name + 7); + local_directory_path = g_strdup_printf("%s/.nautilus/remote_icons", g_get_home_dir()); + + /* we must create the directory if it doesnt exist */ + result = gnome_vfs_make_directory(local_directory_path, REMOTE_ICON_DIR_PERMISSIONS); + + local_file_path = nautilus_make_path(local_directory_path, escaped_uri); + g_free(escaped_uri); + g_free(local_directory_path); + return local_file_path; +} + /* returns the image associated with a link file */ char* nautilus_link_get_image_uri(const char *link_file_uri) { xmlDoc *doc; - char *file_uri; - char *icon_str = NULL; + char *file_uri, *icon_str; + char *local_path; + icon_str = NULL; if (link_file_uri == NULL) return NULL; @@ -92,6 +183,24 @@ nautilus_link_get_image_uri(const char *link_file_uri) xmlFreeDoc (doc); } g_free(file_uri); + + /* if the image is remote, see if we can find it in our local cache */ + + if (nautilus_str_has_prefix(icon_str, "http://")) { + + local_path = make_local_path(icon_str); + + if (g_file_exists(local_path)) { + g_free(icon_str); + return local_path; + } + + /* for now, handle things synchronously */ + load_image_from_http(icon_str, local_path); + g_free(icon_str); + return local_path; + } + return icon_str; }