Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 18 → Rev 736

/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/follow-symlink.h
0,0 → 1,37
#ifndef FOLLOW_SYMLINK_H
#define FOLLOW_SYMLINK_H
 
/*
* This file contains nautilus-follow-symlink's private interface,
* its core functionality
*/
 
#include "common.h"
 
#include <libnautilus-extension/nautilus-menu-provider.h>
 
#include <glib/gprintf.h>
#include <sys/stat.h>
#include <errno.h> /* errno (3) */
#include <stdlib.h> /* realpath() (3) */
#include <string.h> /* strlen(), strerror() (3) */
 
/* Static Prototypes */
 
static void fsl_callback(NautilusMenuItem *, NautilusFileInfo *);
 
static GList * fsl_get_file_items(NautilusMenuProvider *, GtkWidget *, GList *);
 
static GList * fsl_get_background_items(NautilusMenuProvider *, GtkWidget *, NautilusFileInfo *);
 
static GList * fsl_get_items_impl(GtkWidget *, NautilusFileInfo *, gboolean);
 
static NautilusMenuItem * fsl_menu_item_new(GdkScreen *, gboolean, const gchar *);
 
/* Exported Prototypes
* Here the namespace is a bit more explicit just in case
*/
void fsl_extension_menu_provider_iface_init(NautilusMenuProviderIface *);
 
#endif /* FOLLOW_SYMLINK_H */
/* vim:set ts=4 et ai: */
/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/follow-symlink.c
0,0 → 1,184
#include "follow-symlink.h"
 
// Offset at char 7 to remove file://
static const unsigned short URI_OFFSET = 7 * sizeof(gchar);
static const size_t PATH_LENGTH_BYTES = sizeof(gchar) * (PATH_MAX + 1);
 
extern int errno;
 
/* Menu initialization */
void fsl_extension_menu_provider_iface_init(NautilusMenuProviderIface *iface)
{
TRACE();
 
iface->get_file_items = fsl_get_file_items;
iface->get_background_items = fsl_get_background_items;
}
 
/* Implementation of the menu attachment,
* this is slightly different whith file items and with background (one folder)
* items, but shares most of the code, so the common part is here.
*/
GList * fsl_get_items_impl(GtkWidget * window,
NautilusFileInfo * file_info,
gboolean is_file_item)
{
TRACE();
 
NautilusMenuItem *item;
 
// Only file uris
{
gchar * uri_scheme = nautilus_file_info_get_uri_scheme(file_info);
if (strcmp(uri_scheme, "file") != 0) {
FSL_LOG( "Not file scheme" );
return NULL;
}
g_free(uri_scheme);
}
 
// We know the file is either a directory or a symlink to a directory
// TODO: Has glib/gnome any better/faster alternatives?
{
struct stat file_stat;
// Note ..._get_name doesn't give the full path
const gchar * const file_name = nautilus_file_info_get_uri(file_info) + URI_OFFSET;
lstat(file_name, &file_stat);
if (! S_ISLNK(file_stat.st_mode)) {
FSL_LOG1( "Not S_ISLNK:", file_name );
return NULL;
}
}
 
item = fsl_menu_item_new(gtk_widget_get_screen(window),
is_file_item,
nautilus_file_info_get_name(file_info));
g_signal_connect(item, "activate", G_CALLBACK(fsl_callback),
file_info);
 
return g_list_append(NULL, item);
}
 
GList *
fsl_get_background_items(NautilusMenuProvider * provider,
GtkWidget * window,
NautilusFileInfo * current_folder)
{
TRACE();
 
if (NULL == current_folder) { // XXX: Does this ever happen?
FSL_LOG( "No folder selected" );
}
return fsl_get_items_impl(window, current_folder, FALSE);
}
 
/* Bind to menu if needed */
GList *
fsl_get_file_items (NautilusMenuProvider * provider,
GtkWidget * window,
GList * files)
{
TRACE();
 
// Number of files = g_list_length(files)
// Directory = nautilus_file_info_is_directory(files->data)
 
if (NULL==files || g_list_length(files) != 1) {
FSL_LOG( (NULL==files) ? "No file" : "More than one file" );
return NULL;
}
 
GnomeVFSFileInfo * gfi = nautilus_file_info_get_vfs_file_info(files->data);
/*
* Aparently type is never GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK and symlinks
* are resolved to the target type
*/
if (gfi->type != GNOME_VFS_FILE_TYPE_DIRECTORY) {
FSL_LOG( "Not directory" );
return NULL;
}
 
 
return fsl_get_items_impl(window, files->data, TRUE);
}
 
void fsl_callback (NautilusMenuItem * item, NautilusFileInfo * file_info)
{
TRACE();
 
gchar ** argv;
const gchar * link_name = nautilus_file_info_get_uri(file_info) + URI_OFFSET;
gchar * target = g_malloc(PATH_LENGTH_BYTES);
 
/* unlike readlink(man 2), realpath(man 3) resolves the symlink, while
* readlink returns the pointed file, which might be a relative path
* Xref: <http://www.gnu.org/software/libc/manual/html_node/Symbolic-Links.html>
*/
if (NULL == realpath(link_name, target)) {
g_printf("ERROR in realpath(): %s\n", strerror(errno));
g_assert( FALSE );
}
 
const gchar const * BASE_CMD = "nautilus --no-desktop --no-default-window '%s'";
gchar * command_line = g_malloc( sizeof(gchar) * ( strlen(BASE_CMD) + strlen(target) + 1 ) );
g_sprintf(command_line, BASE_CMD, target);
 
if (FALSE == g_shell_parse_argv(command_line, NULL, &argv, NULL)) {
g_assert( FALSE );
}
 
g_printf("nautilus-follow-symlink: Spawning nautilus with\n '%s'\n", command_line);
 
g_spawn_async( nautilus_file_info_get_parent_uri(file_info) + URI_OFFSET,
argv,
NULL,
G_SPAWN_SEARCH_PATH,
NULL, NULL, NULL, NULL);
 
g_free(command_line);
g_strfreev(argv);
}
 
/* Create the new menu item */
NautilusMenuItem *
fsl_menu_item_new(GdkScreen *screen, gboolean is_file_item, const gchar * base_name)
{
TRACE();
 
NautilusMenuItem *ret;
 
char * name;
char * tooltip;
 
if (is_file_item) {
const gchar * fmt_name = _("Follow symbolic _link '%s'");
const gchar * fmt_tooltip = _("Open the directory pointed by the "
"symbolic link '%s'");
 
name = g_malloc(sizeof(gchar) * (strlen(fmt_name) + strlen(base_name)));
tooltip = g_malloc(sizeof(gchar) * (strlen(fmt_tooltip) + strlen(base_name)));
g_sprintf(name, fmt_name, base_name);
g_sprintf(tooltip, fmt_tooltip, base_name);
}
else {
const gchar * fmt_name = _("Open real path of '%s'");
const gchar * fmt_tooltip = _("Open the real path of the folder "
"pointed by '%s'");
 
name = g_malloc(sizeof(gchar) * (strlen(fmt_name) + strlen(base_name + 1)));
tooltip = g_malloc(sizeof(gchar) * (strlen(fmt_tooltip) + strlen(base_name + 1)));
g_sprintf(name, fmt_name, base_name);
g_sprintf(tooltip, fmt_tooltip, base_name);
}
 
// (name, label, tip, icon)
ret = nautilus_menu_item_new("FsymlinkExtension::follow_symlink",
name, tooltip, NULL);
g_free(name);
g_free(tooltip);
 
//g_object_set_data(G_OBJECT(ret), "FsymlinkExtension::screen", screen);
return ret;
}
 
/* vim:set ts=4 et ai: */
/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/common.h
0,0 → 1,61
#ifndef FOLLOW_SYMLINK_COMMON_H
#define FOLLOW_SYMLINK_COMMON_H
 
/*
* This file defines common debug utilities.
* Also, includes config.h.
*/
 
#include <glib/gmessages.h> /* g_print() */
#include <glib/gprintf.h> /* g_printf() */
 
#include <stdlib.h> /* getenv() (3) */
#include <string.h> /* strcmp() (3) */
 
#include "libintl.h"
#define _(STR) gettext(STR)
 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
 
#ifndef __inline__
#define __inline
#endif
 
#ifdef _DEBUG
/* Debugging facilities */
 
/* Prefix for messages */
#define FSL_ "nautilus-follow-symlink: "
/* Environment variable, set to 1 to enable verbosity */
#define DBGENV_ (getenv("FSL_DBG"))
/* Check on runtime the environment variable's value */
#define DEBUG_ON_() (DBGENV_ != NULL && 0 == strcmp(DBGENV_,"1"))
 
/* Informational message shown on initialisation */
#define FSL_DEBUG_INIT() { \
const int ENABLED = DEBUG_ON_(); \
g_print( FSL_ "DEBUG mode is available, and "); \
g_print( (ENABLED) ? "enabled.\n" : "disabled.\n"); \
g_print( FSL_ "set the environment variable FSL_DBG to \n" \
FSL_ "1 to enable it or to any other value to disable it.\n"); \
};
 
/* Display the name of the current function name */
#define TRACE() if (DEBUG_ON_())\
g_printf("nautilus-follow-symlink trace: %s()\n", __FUNCTION__);
/* Display a message */
#define FSL_LOG(str) if (DEBUG_ON_()) g_printf("%s\n", (str));
/* Display a formatted message with one string argument */
#define FSL_LOG1(str1, str2) if (DEBUG_ON_()) g_printf("%s %s\n", (str1), (str2));
#else
/* Debugging facilities disabled */
#define TRACE()
#define FSL_LOG(a)
#define FSL_LOG1(a,b)
#define FSL_DEBUG_INIT()
#endif
 
#endif /* FOLLOW_SYMLINK_COMMON_H */
/* vim:set ts=4 et ai: */
/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/nautilus-ext-follow-symlink.c
0,0 → 1,88
#include "nautilus-ext-follow-symlink.h"
 
/* Public interface */
static GType fsl_type;
static GType provider_types[1];
 
void nautilus_module_initialize (GTypeModule *module)
{
TRACE();
FSL_DEBUG_INIT();
 
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
textdomain(GETTEXT_PACKAGE);
 
g_printf("Initializing nautilus-follow-symlink extension (v.%s)\n", VERSION);
 
fsl_register_type(module);
provider_types[0] = fsl_get_type();
}
 
void nautilus_module_shutdown (void)
{
TRACE();
 
/* Module-specific shutdown */
g_print ("Shutting down nautilus-follow-symlink extension\n");
}
 
void nautilus_module_list_types (const GType **types, int *num_types)
{
TRACE();
 
*types = provider_types;
*num_types = G_N_ELEMENTS(provider_types);
}
 
void fsl_register_type (GTypeModule *module)
{
TRACE();
 
static const GTypeInfo info = {
sizeof(FsymlinkExtensionClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) fsl_class_init,
NULL,
NULL,
sizeof (FsymlinkExtension),
0,
(GInstanceInitFunc) fsl_instance_init,
};
 
fsl_type = g_type_module_register_type (module,
G_TYPE_OBJECT,
"FsymlinkExtension",
&info, 0);
/* Menu provider interface */
static const GInterfaceInfo menu_provider_iface_info = {
(GInterfaceInitFunc)fsl_extension_menu_provider_iface_init,
NULL,
NULL,
};
 
g_type_module_add_interface(module, fsl_type,
NAUTILUS_TYPE_MENU_PROVIDER, &menu_provider_iface_info);
 
/* Other Interfaces */
}
 
GType fsl_get_type(void)
{
TRACE();
 
return fsl_type;
}
 
void fsl_instance_init(FsymlinkExtension *cvs)
{
TRACE();
}
 
void fsl_class_init(FsymlinkExtensionClass *class)
{
TRACE();
}
 
/* vim:set ts=4 et ai: */
/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/nautilus-ext-follow-symlink.h
0,0 → 1,54
#ifndef NAUTILUS_EXT_FOLLOW_SYMLINK_H
#define NAUTILUS_EXT_FOLLOW_SYMLINK_H
 
#include "common.h"
 
#include <glib-object.h>
#include <libnautilus-extension/nautilus-menu-provider.h>
 
#include <locale.h>
#include <libintl.h>
 
#include <glib/gprintf.h>
 
/*
* This file contains nautilus-follow-symlink's "public" interface,
* the functions required to bind the extension to nautilus
*/
 
void nautilus_module_initialize(GTypeModule *);
 
void nautilus_module_shutdown(void);
 
void nautilus_module_list_types(const GType **, int *);
 
/* These ones don't need public visibility */
 
static void fsl_register_type(GTypeModule *);
 
static GType fsl_get_type(void);
 
/* Data Types */
 
struct _FsymlinkExtensionClass {
GObjectClass parent_slot;
};
 
struct _FsymlinkExtension {
GObject parent_slot;
};
 
typedef struct _FsymlinkExtensionClass FsymlinkExtensionClass;
 
typedef struct _FsymlinkExtension FsymlinkExtension;
 
/* Data initializers */
static void fsl_class_init (FsymlinkExtensionClass *class);
 
static void fsl_instance_init (FsymlinkExtension *cvs);
 
/* Defined in the private interface */
extern void fsl_extension_menu_provider_iface_init(NautilusMenuProviderIface *iface);
 
#endif /* NAUTILUS_EXT_FOLLOW_SYMLINK_H */
/* vim:set ts=4 et ai: */
/ATTIC/nautilus-follow-symlink/tags/0.7.9/src/Makefile.am
0,0 → 1,18
 
# Required to correctly install the locale files
CPPFLAGS+=-DGNOMELOCALEDIR=\""$(datadir)/locale"\"
 
nautilus_extension_lib_LTLIBRARIES = libnautilus-follow-symlink.la
 
# Must be installed in nautilus' extension dir
#nautilus_extension_libdir = $(libdir)/nautilus/extensions-1.0
# Cleaner way:
nautilus_extension_libdir = `pkg-config --variable=extensiondir libnautilus-extension`
 
libnautilus_follow_symlink_la_SOURCES = follow-symlink.c nautilus-ext-follow-symlink.c
 
# There's really no need to have versioned file names
libnautilus_follow_symlink_la_LDFLAGS = -avoid-version
# See e.g. <http://www.seul.org/docs/autotut/#libtool> for version instructions
#libnautilus_follow_symlink_la_LDFLAGS = -version-info 0:0:0