查看Thunar的SOURCE包,发现其提供了用于写插件的可扩展类。
位置位于./thunarx内,我们可以通过其写些小插件,包括在右键菜单添加命令,增加文件属性卡等等。
有兴趣的可以看看./docs/reference/thunarx/html/index.html里面提供了可提供的扩展类和一个say-hello例子。
那么,下面我们就来看看它是如何实现的。
Example 5. Menu provider example
static void hello_menu_provider_init (ThunarxMenuProviderIface *iface);
static GList *hello_get_file_actions (ThunarxMenuProvider *provider,
GtkWidget *window,
GList *files);
THUNARX_DEFINE_TYPE_WITH_CODE (Hello, hello, G_TYPE_OBJECT,
THUNARX_IMPLEMENT_INTERFACE (THUNARX_TYPE_MENU_PROVIDER,
hello_menu_provider_init));//这段相当于注册插件,是我们写插件类必须包括的
static void
hello_menu_provider_init (ThunarxMenuProviderIface *iface)
{
iface->get_file_actions = hello_get_file_actions;
}
static void
hello_activated (GtkWidget *window)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"Hello World!");
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
static GList*
hello_get_file_actions (ThunarxMenuProvider *provider,
GtkWidget *window,
GList *files)
{
GtkAction *action;
GClosure *closure;
action = gtk_action_new ("Hello::say-hello", "Say hello", "Say hello", NULL);
//上面第一个参数应该是独一无二的KEY,由我们随便定。第二个参数就是显示的LABEL。第三个参数就是鼠标放上去显示提示。最后为图标ID。
// closure = g_cclosure_object_new_swap (G_CALLBACK (hello_activated), G_OBJECT (window));
// g_signal_connect_closure (G_OBJECT (action), "activate", closure, TRUE);
// 这两句我编译没问题,运行的时候就报错,所以改为:
g_signal_connect (G_OBJECT (action), "activate", G_CALLBACK (hello_activated), window);
return g_list_append (NULL, action);
}
类似的例子在./examples/tex-open-terminal里也有,实现的是右键菜单增加打开终端的项目。搜索关键字:THUNARX_DEFINE_TYPE_WITH_CODE
Thunar扩展类的API:
http://www.xfce.org/documentation/4.6/api/thunarx/index.html
Nautilus扩展类的API:
http://library.gnome.org/devel/libnautilus-extension/stable/index.html