2011-11-07 13:46:58 -06:00
/*
2016-02-04 03:20:42 -06:00
* xed - file - browser - plugin . c - Xed plugin providing easy file access
2011-11-07 13:46:58 -06:00
* from the sidepanel
*
* Copyright ( C ) 2006 - Jesse van den Kieboom < jesse @ icecrew . nl >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 , or ( at your option )
* any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation , Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2011-11-07 13:46:58 -06:00
*/
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
2017-01-09 13:57:48 -06:00
# include <string.h>
2011-11-07 13:46:58 -06:00
# include <glib/gi18n-lib.h>
2017-01-09 13:57:48 -06:00
# include <gmodule.h>
# include <xed/xed-app.h>
# include <xed/xed-commands.h>
2016-02-04 03:20:42 -06:00
# include <xed/xed-debug.h>
2013-01-24 14:05:58 -06:00
# include <gio/gio.h>
2017-01-09 13:57:48 -06:00
# include <xed/xed-window.h>
# include <xed/xed-window-activatable.h>
# include <xed/xed-utils.h>
2011-11-07 13:46:58 -06:00
2016-02-04 03:20:42 -06:00
# include "xed-file-browser-enum-types.h"
# include "xed-file-browser-plugin.h"
# include "xed-file-browser-utils.h"
# include "xed-file-browser-error.h"
# include "xed-file-browser-widget.h"
# include "xed-file-browser-messages.h"
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
# define FILE_BROWSER_SCHEMA "org.x.editor.plugins.filebrowser"
# define FILE_BROWSER_ONLOAD_SCHEMA "org.x.editor.plugins.filebrowser.on-load"
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
# define XED_FILE_BROWSER_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), XED_TYPE_FILE_BROWSER_PLUGIN, XedFileBrowserPluginPrivate))
2011-11-07 13:46:58 -06:00
2016-02-04 03:20:42 -06:00
struct _XedFileBrowserPluginPrivate
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedWindow * window ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
XedFileBrowserWidget * tree_widget ;
gulong merge_id ;
GtkActionGroup * action_group ;
GtkActionGroup * single_selection_action_group ;
gboolean auto_root ;
gulong end_loading_handle ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
GSettings * settings ;
GSettings * onload_settings ;
2016-12-26 15:25:50 -06:00
} ;
enum
{
PROP_0 ,
2017-01-09 13:57:48 -06:00
PROP_WINDOW
2016-12-26 15:25:50 -06:00
} ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
static void on_uri_activated_cb ( XedFileBrowserWidget * widget ,
gchar const * uri ,
XedWindow * window ) ;
static void on_error_cb ( XedFileBrowserWidget * widget ,
guint code ,
gchar const * message ,
XedFileBrowserPlugin * plugin ) ;
static void on_model_set_cb ( XedFileBrowserView * widget ,
GParamSpec * arg1 ,
XedFileBrowserPlugin * plugin ) ;
static void on_virtual_root_changed_cb ( XedFileBrowserStore * model ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin ) ;
static void on_filter_mode_changed_cb ( XedFileBrowserStore * model ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin ) ;
static void on_rename_cb ( XedFileBrowserStore * model ,
const gchar * olduri ,
const gchar * newuri ,
XedWindow * window ) ;
static void on_filter_pattern_changed_cb ( XedFileBrowserWidget * widget ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin ) ;
static void on_tab_added_cb ( XedWindow * window ,
XedTab * tab ,
XedFileBrowserPlugin * plugin ) ;
static gboolean on_confirm_delete_cb ( XedFileBrowserWidget * widget ,
XedFileBrowserStore * store ,
GList * rows ,
XedFileBrowserPlugin * plugin ) ;
static gboolean on_confirm_no_trash_cb ( XedFileBrowserWidget * widget ,
GList * files ,
XedWindow * window ) ;
static void xed_window_activatable_iface_init ( XedWindowActivatableInterface * iface ) ;
2016-12-26 15:25:50 -06:00
G_DEFINE_DYNAMIC_TYPE_EXTENDED ( XedFileBrowserPlugin ,
xed_file_browser_plugin ,
PEAS_TYPE_EXTENSION_BASE ,
0 ,
2017-01-09 13:57:48 -06:00
G_IMPLEMENT_INTERFACE_DYNAMIC ( XED_TYPE_WINDOW_ACTIVATABLE ,
xed_window_activatable_iface_init ) \
2016-12-26 15:25:50 -06:00
\
xed_file_browser_enum_and_flag_register_type ( type_module ) ; \
_xed_file_browser_store_register_type ( type_module ) ; \
_xed_file_bookmarks_store_register_type ( type_module ) ; \
_xed_file_browser_view_register_type ( type_module ) ; \
_xed_file_browser_widget_register_type ( type_module ) ;
2011-11-07 13:46:58 -06:00
)
static void
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_init ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
plugin - > priv = XED_FILE_BROWSER_PLUGIN_GET_PRIVATE ( plugin ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_dispose ( GObject * object )
2011-11-07 13:46:58 -06:00
{
2016-12-26 15:25:50 -06:00
XedFileBrowserPlugin * plugin = XED_FILE_BROWSER_PLUGIN ( object ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
g_clear_object ( & plugin - > priv - > window ) ;
2016-12-26 15:25:50 -06:00
G_OBJECT_CLASS ( xed_file_browser_plugin_parent_class ) - > dispose ( object ) ;
}
static void
xed_file_browser_plugin_set_property ( GObject * object ,
guint prop_id ,
const GValue * value ,
GParamSpec * pspec )
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPlugin * plugin = XED_FILE_BROWSER_PLUGIN ( object ) ;
2016-12-26 15:25:50 -06:00
switch ( prop_id )
{
2017-01-09 13:57:48 -06:00
case PROP_WINDOW :
plugin - > priv - > window = XED_WINDOW ( g_value_dup_object ( value ) ) ;
2016-12-26 15:25:50 -06:00
break ;
default :
G_OBJECT_WARN_INVALID_PROPERTY_ID ( object , prop_id , pspec ) ;
break ;
}
2011-11-07 13:46:58 -06:00
}
2016-12-26 15:25:50 -06:00
static void
xed_file_browser_plugin_get_property ( GObject * object ,
2017-01-09 13:57:48 -06:00
guint prop_id ,
GValue * value ,
GParamSpec * pspec )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPlugin * plugin = XED_FILE_BROWSER_PLUGIN ( object ) ;
2016-12-26 15:25:50 -06:00
switch ( prop_id )
{
2017-01-09 13:57:48 -06:00
case PROP_WINDOW :
2016-12-26 15:25:50 -06:00
g_value_set_object ( value , plugin - > priv - > window ) ;
break ;
default :
G_OBJECT_WARN_INVALID_PROPERTY_ID ( object , prop_id , pspec ) ;
break ;
}
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
on_end_loading_cb ( XedFileBrowserStore * store ,
GtkTreeIter * iter ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
/* Disconnect the signal */
g_signal_handler_disconnect ( store , priv - > end_loading_handle ) ;
priv - > end_loading_handle = 0 ;
priv - > auto_root = FALSE ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
prepare_auto_root ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
XedFileBrowserStore * store ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
priv - > auto_root = TRUE ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
store = xed_file_browser_widget_get_browser_store ( priv - > tree_widget ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( priv - > end_loading_handle ! = 0 )
{
g_signal_handler_disconnect ( store , priv - > end_loading_handle ) ;
priv - > end_loading_handle = 0 ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
priv - > end_loading_handle = g_signal_connect ( store , " end-loading " ,
G_CALLBACK ( on_end_loading_cb ) , plugin ) ;
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
restore_default_location ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * root ;
gchar * virtual_root ;
gboolean bookmarks ;
gboolean remote ;
bookmarks = ! g_settings_get_boolean ( priv - > onload_settings , " tree-view " ) ;
if ( bookmarks )
{
xed_file_browser_widget_show_bookmarks ( priv - > tree_widget ) ;
return ;
}
root = g_settings_get_string ( priv - > onload_settings , " root " ) ;
virtual_root = g_settings_get_string ( priv - > onload_settings , " virtual-root " ) ;
remote = g_settings_get_boolean ( priv - > onload_settings , " enable-remote " ) ;
if ( root ! = NULL & & * root ! = ' \0 ' ) {
GFile * file ;
file = g_file_new_for_uri ( root ) ;
if ( remote | | g_file_is_native ( file ) )
{
if ( virtual_root ! = NULL & & * virtual_root ! = ' \0 ' )
{
prepare_auto_root ( plugin ) ;
xed_file_browser_widget_set_root_and_virtual_root ( priv - > tree_widget , root , virtual_root ) ;
}
else
{
prepare_auto_root ( plugin ) ;
xed_file_browser_widget_set_root ( priv - > tree_widget , root , TRUE ) ;
}
}
g_object_unref ( file ) ;
}
g_free ( root ) ;
g_free ( virtual_root ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
restore_filter ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * filter_mode ;
XedFileBrowserStoreFilterMode mode ;
gchar * pattern ;
/* Get filter_mode */
filter_mode = g_settings_get_string ( priv - > settings , " filter-mode " ) ;
/* Filter mode */
mode = xed_file_browser_store_filter_mode_get_default ( ) ;
if ( filter_mode ! = NULL ) {
if ( strcmp ( filter_mode , " hidden " ) = = 0 ) {
mode = XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN ;
} else if ( strcmp ( filter_mode , " binary " ) = = 0 ) {
mode = XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY ;
} else if ( strcmp ( filter_mode , " hidden_and_binary " ) = = 0 | |
strcmp ( filter_mode , " binary_and_hidden " ) = = 0 ) {
mode = XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN |
XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY ;
} else if ( strcmp ( filter_mode , " none " ) = = 0 | |
* filter_mode = = ' \0 ' ) {
mode = XED_FILE_BROWSER_STORE_FILTER_MODE_NONE ;
}
}
/* Set the filter mode */
xed_file_browser_store_set_filter_mode ( xed_file_browser_widget_get_browser_store ( priv - > tree_widget ) , mode ) ;
pattern = g_settings_get_string ( priv - > settings , " filter-pattern " ) ;
xed_file_browser_widget_set_filter_pattern ( priv - > tree_widget , pattern ) ;
g_free ( filter_mode ) ;
g_free ( pattern ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
set_root_from_doc ( XedFileBrowserPlugin * plugin ,
XedDocument * doc )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
GFile * file ;
GFile * parent ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( doc = = NULL )
{
return ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
file = xed_document_get_location ( doc ) ;
if ( file = = NULL )
{
return ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
parent = g_file_get_parent ( file ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( parent ! = NULL )
{
gchar * root ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
root = g_file_get_uri ( parent ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
xed_file_browser_widget_set_root ( priv - > tree_widget ,
root ,
TRUE ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_unref ( parent ) ;
g_free ( root ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_unref ( file ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
on_action_set_active_root ( GtkAction * action ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
set_root_from_doc ( plugin , xed_window_get_active_document ( XED_WINDOW ( plugin - > priv - > window ) ) ) ;
2011-11-07 13:46:58 -06:00
}
static gchar *
2017-01-09 13:57:48 -06:00
get_terminal ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
// TODO : Identify the DE, find the preferred terminal application (xterminal shouldn't be hardcoded here, it should be set as default in the DE prefs)
return g_strdup ( " xterminal " ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
on_action_open_terminal ( GtkAction * action ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * terminal ;
gchar * wd = NULL ;
gchar * local ;
gchar * argv [ 2 ] ;
GFile * file ;
GtkTreeIter iter ;
XedFileBrowserStore * store ;
/* Get the current directory */
if ( ! xed_file_browser_widget_get_selected_directory ( priv - > tree_widget , & iter ) )
{
return ;
}
store = xed_file_browser_widget_get_browser_store ( priv - > tree_widget ) ;
gtk_tree_model_get ( GTK_TREE_MODEL ( store ) , & iter , XED_FILE_BROWSER_STORE_COLUMN_URI , & wd , - 1 ) ;
if ( wd = = NULL )
{
return ;
}
terminal = get_terminal ( plugin ) ;
file = g_file_new_for_uri ( wd ) ;
local = g_file_get_path ( file ) ;
g_object_unref ( file ) ;
argv [ 0 ] = terminal ;
argv [ 1 ] = NULL ;
g_spawn_async ( local ,
argv ,
NULL ,
G_SPAWN_SEARCH_PATH ,
NULL ,
NULL ,
NULL ,
NULL ) ;
g_free ( terminal ) ;
g_free ( wd ) ;
g_free ( local ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
on_selection_changed_cb ( GtkTreeSelection * selection ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
GtkTreeView * tree_view ;
GtkTreeModel * model ;
GtkTreeIter iter ;
gboolean sensitive ;
gchar * uri ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
tree_view = GTK_TREE_VIEW ( xed_file_browser_widget_get_browser_view ( priv - > tree_widget ) ) ;
model = gtk_tree_view_get_model ( tree_view ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
if ( ! XED_IS_FILE_BROWSER_STORE ( model ) )
{
return ;
}
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
sensitive = xed_file_browser_widget_get_selected_directory ( priv - > tree_widget , & iter ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( sensitive )
{
gtk_tree_model_get ( model , & iter , XED_FILE_BROWSER_STORE_COLUMN_URI , & uri , - 1 ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
sensitive = xed_utils_uri_has_file_scheme ( uri ) ;
g_free ( uri ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
gtk_action_set_sensitive ( gtk_action_group_get_action ( priv - > single_selection_action_group , " OpenTerminal " ) , sensitive ) ;
2011-11-07 13:46:58 -06:00
}
# define POPUP_UI "" \
" <ui> " \
" <popup name= \" FilePopup \" > " \
" <placeholder name= \" FilePopup_Opt1 \" > " \
" <menuitem action= \" SetActiveRoot \" /> " \
" </placeholder> " \
" <placeholder name= \" FilePopup_Opt4 \" > " \
" <menuitem action= \" OpenTerminal \" /> " \
" </placeholder> " \
" </popup> " \
" <popup name= \" BookmarkPopup \" > " \
" <placeholder name= \" BookmarkPopup_Opt1 \" > " \
" <menuitem action= \" SetActiveRoot \" /> " \
" </placeholder> " \
" </popup> " \
" </ui> "
2011-11-07 18:10:16 -06:00
static GtkActionEntry extra_actions [ ] =
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
{ " SetActiveRoot " , " go-jump-symbolic " , N_ ( " _Set root to active document " ) ,
NULL ,
N_ ( " Set the root to the active document location " ) ,
G_CALLBACK ( on_action_set_active_root ) }
2011-11-07 13:46:58 -06:00
} ;
static GtkActionEntry extra_single_selection_actions [ ] = {
2017-01-09 13:57:48 -06:00
{ " OpenTerminal " , " utilities-terminal " , N_ ( " _Open terminal here " ) ,
NULL ,
N_ ( " Open a terminal at the currently opened directory " ) ,
G_CALLBACK ( on_action_open_terminal ) }
2011-11-07 13:46:58 -06:00
} ;
static void
2017-01-09 13:57:48 -06:00
add_popup_ui ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
GtkUIManager * manager ;
GtkActionGroup * action_group ;
GError * error = NULL ;
manager = xed_file_browser_widget_get_ui_manager ( priv - > tree_widget ) ;
action_group = gtk_action_group_new ( " FileBrowserPluginExtra " ) ;
gtk_action_group_set_translation_domain ( action_group , NULL ) ;
gtk_action_group_add_actions ( action_group ,
extra_actions ,
G_N_ELEMENTS ( extra_actions ) ,
plugin ) ;
gtk_ui_manager_insert_action_group ( manager , action_group , 0 ) ;
priv - > action_group = action_group ;
action_group = gtk_action_group_new ( " FileBrowserPluginSingleSelectionExtra " ) ;
gtk_action_group_set_translation_domain ( action_group , NULL ) ;
gtk_action_group_add_actions ( action_group ,
extra_single_selection_actions ,
G_N_ELEMENTS ( extra_single_selection_actions ) ,
priv - > window ) ;
gtk_ui_manager_insert_action_group ( manager , action_group , 0 ) ;
priv - > single_selection_action_group = action_group ;
priv - > merge_id = gtk_ui_manager_add_ui_from_string ( manager ,
POPUP_UI ,
- 1 ,
& error ) ;
if ( priv - > merge_id = = 0 )
{
g_warning ( " Unable to merge UI: %s " , error - > message ) ;
g_error_free ( error ) ;
}
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
remove_popup_ui ( XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
GtkUIManager * manager ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
manager = xed_file_browser_widget_get_ui_manager ( priv - > tree_widget ) ;
gtk_ui_manager_remove_ui ( manager , priv - > merge_id ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
gtk_ui_manager_remove_action_group ( manager , priv - > action_group ) ;
g_object_unref ( priv - > action_group ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
gtk_ui_manager_remove_action_group ( manager , priv - > single_selection_action_group ) ;
g_object_unref ( priv - > single_selection_action_group ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_update_state ( XedWindowActivatable * activatable )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = XED_FILE_BROWSER_PLUGIN ( activatable ) - > priv ;
XedDocument * doc ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
doc = xed_window_get_active_document ( XED_WINDOW ( priv - > window ) ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
gtk_action_set_sensitive ( gtk_action_group_get_action ( priv - > action_group ,
" SetActiveRoot " ) ,
doc ! = NULL & & ! xed_document_is_untitled ( doc ) ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_activate ( XedWindowActivatable * activatable )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPlugin * plugin = XED_FILE_BROWSER_PLUGIN ( activatable ) ;
XedFileBrowserPluginPrivate * priv ;
XedPanel * panel ;
XedFileBrowserStore * store ;
gchar * data_dir ;
GSettingsSchemaSource * schema_source ;
GSettingsSchema * schema ;
priv = plugin - > priv ;
data_dir = peas_extension_base_get_data_dir ( PEAS_EXTENSION_BASE ( activatable ) ) ;
priv - > tree_widget = XED_FILE_BROWSER_WIDGET ( xed_file_browser_widget_new ( data_dir ) ) ;
g_free ( data_dir ) ;
priv - > settings = g_settings_new ( FILE_BROWSER_SCHEMA ) ;
priv - > onload_settings = g_settings_new ( FILE_BROWSER_ONLOAD_SCHEMA ) ;
g_signal_connect ( priv - > tree_widget , " uri-activated " ,
G_CALLBACK ( on_uri_activated_cb ) , priv - > window ) ;
g_signal_connect ( priv - > tree_widget , " error " ,
G_CALLBACK ( on_error_cb ) , plugin ) ;
g_signal_connect ( priv - > tree_widget , " notify::filter-pattern " ,
G_CALLBACK ( on_filter_pattern_changed_cb ) , plugin ) ;
g_signal_connect ( priv - > tree_widget , " confirm-delete " ,
G_CALLBACK ( on_confirm_delete_cb ) , plugin ) ;
g_signal_connect ( priv - > tree_widget , " confirm-no-trash " ,
G_CALLBACK ( on_confirm_no_trash_cb ) , priv - > window ) ;
g_signal_connect ( gtk_tree_view_get_selection ( GTK_TREE_VIEW ( xed_file_browser_widget_get_browser_view ( priv - > tree_widget ) ) ) ,
" changed " , G_CALLBACK ( on_selection_changed_cb ) , plugin ) ;
panel = xed_window_get_side_panel ( priv - > window ) ;
xed_panel_add_item ( panel , GTK_WIDGET ( priv - > tree_widget ) , _ ( " File Browser " ) , " system-file-manager " ) ;
gtk_widget_show ( GTK_WIDGET ( priv - > tree_widget ) ) ;
add_popup_ui ( plugin ) ;
/* Restore filter options */
restore_filter ( plugin ) ;
/* Connect signals to store the last visited location */
g_signal_connect ( xed_file_browser_widget_get_browser_view ( priv - > tree_widget ) , " notify::model " ,
G_CALLBACK ( on_model_set_cb ) , plugin ) ;
store = xed_file_browser_widget_get_browser_store ( priv - > tree_widget ) ;
g_signal_connect ( store , " notify::virtual-root " ,
G_CALLBACK ( on_virtual_root_changed_cb ) , plugin ) ;
g_signal_connect ( store , " notify::filter-mode " ,
G_CALLBACK ( on_filter_mode_changed_cb ) , plugin ) ;
g_signal_connect ( store , " rename " ,
G_CALLBACK ( on_rename_cb ) , priv - > window ) ;
g_signal_connect ( priv - > window , " tab-added " ,
G_CALLBACK ( on_tab_added_cb ) , plugin ) ;
/* Register messages on the bus */
xed_file_browser_messages_register ( priv - > window , priv - > tree_widget ) ;
xed_file_browser_plugin_update_state ( activatable ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_deactivate ( XedWindowActivatable * activatable )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPlugin * plugin = XED_FILE_BROWSER_PLUGIN ( activatable ) ;
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
XedPanel * panel ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
/* Unregister messages from the bus */
xed_file_browser_messages_unregister ( priv - > window ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
/* Disconnect signals */
g_signal_handlers_disconnect_by_func ( priv - > window , G_CALLBACK ( on_tab_added_cb ) , plugin ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_unref ( priv - > settings ) ;
g_object_unref ( priv - > onload_settings ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
remove_popup_ui ( plugin ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
panel = xed_window_get_side_panel ( priv - > window ) ;
xed_panel_remove_item ( panel , GTK_WIDGET ( priv - > tree_widget ) ) ;
2011-11-07 13:46:58 -06:00
}
static void
2016-12-26 15:25:50 -06:00
xed_file_browser_plugin_class_init ( XedFileBrowserPluginClass * klass )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
GObjectClass * object_class = G_OBJECT_CLASS ( klass ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
object_class - > dispose = xed_file_browser_plugin_dispose ;
2016-12-26 15:25:50 -06:00
object_class - > set_property = xed_file_browser_plugin_set_property ;
object_class - > get_property = xed_file_browser_plugin_get_property ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_class_override_property ( object_class , PROP_WINDOW , " window " ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_type_class_add_private ( object_class , sizeof ( XedFileBrowserPluginPrivate ) ) ;
2011-11-07 13:46:58 -06:00
}
2016-12-26 15:25:50 -06:00
static void
2017-01-09 13:57:48 -06:00
xed_window_activatable_iface_init ( XedWindowActivatableInterface * iface )
2016-12-26 15:25:50 -06:00
{
iface - > activate = xed_file_browser_plugin_activate ;
iface - > deactivate = xed_file_browser_plugin_deactivate ;
iface - > update_state = xed_file_browser_plugin_update_state ;
}
2017-01-09 13:57:48 -06:00
static void
xed_file_browser_plugin_class_finalize ( XedFileBrowserPluginClass * klass )
2016-12-26 15:25:50 -06:00
{
2017-01-09 13:57:48 -06:00
/* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */
2016-12-26 15:25:50 -06:00
}
2011-11-07 13:46:58 -06:00
/* Callbacks */
static void
2017-01-09 13:57:48 -06:00
on_uri_activated_cb ( XedFileBrowserWidget * tree_widget ,
gchar const * uri ,
XedWindow * window )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
xed_commands_load_uri ( window , uri , NULL , 0 ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
on_error_cb ( XedFileBrowserWidget * tree_widget ,
guint code ,
gchar const * message ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * title ;
GtkWidget * dlg ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
/* Do not show the error when the root has been set automatically */
if ( priv - > auto_root & & ( code = = XED_FILE_BROWSER_ERROR_SET_ROOT | |
code = = XED_FILE_BROWSER_ERROR_LOAD_DIRECTORY ) )
{
/* Show bookmarks */
xed_file_browser_widget_show_bookmarks ( priv - > tree_widget ) ;
return ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
switch ( code ) {
case XED_FILE_BROWSER_ERROR_NEW_DIRECTORY :
title = _ ( " An error occurred while creating a new directory " ) ;
break ;
case XED_FILE_BROWSER_ERROR_NEW_FILE :
title = _ ( " An error occurred while creating a new file " ) ;
break ;
case XED_FILE_BROWSER_ERROR_RENAME :
title = _ ( " An error occurred while renaming a file or directory " ) ;
break ;
case XED_FILE_BROWSER_ERROR_DELETE :
title = _ ( " An error occurred while deleting a file or directory " ) ;
break ;
case XED_FILE_BROWSER_ERROR_OPEN_DIRECTORY :
title = _ ( " An error occurred while opening a directory in the file manager " ) ;
break ;
case XED_FILE_BROWSER_ERROR_SET_ROOT :
title = _ ( " An error occurred while setting a root directory " ) ;
break ;
case XED_FILE_BROWSER_ERROR_LOAD_DIRECTORY :
title = _ ( " An error occurred while loading a directory " ) ;
break ;
default :
title = _ ( " An error occurred " ) ;
break ;
}
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
dlg = gtk_message_dialog_new ( GTK_WINDOW ( priv - > window ) ,
GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT ,
GTK_MESSAGE_ERROR , GTK_BUTTONS_OK ,
" %s " , title ) ;
gtk_message_dialog_format_secondary_text ( GTK_MESSAGE_DIALOG ( dlg ) , " %s " , message ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
gtk_dialog_run ( GTK_DIALOG ( dlg ) ) ;
gtk_widget_destroy ( dlg ) ;
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
on_model_set_cb ( XedFileBrowserView * widget ,
GParamSpec * arg1 ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
GtkTreeModel * model ;
model = gtk_tree_view_get_model ( GTK_TREE_VIEW ( xed_file_browser_widget_get_browser_view ( priv - > tree_widget ) ) ) ;
if ( model = = NULL )
{
return ;
}
g_settings_set_boolean ( priv - > onload_settings , " tree-view " , XED_IS_FILE_BROWSER_STORE ( model ) ) ;
2011-11-07 13:46:58 -06:00
}
static void
2017-01-09 13:57:48 -06:00
on_filter_mode_changed_cb ( XedFileBrowserStore * model ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
XedFileBrowserStoreFilterMode mode ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
mode = xed_file_browser_store_get_filter_mode ( model ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
if ( ( mode & XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN ) & &
( mode & XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY ) )
{
g_settings_set_string ( priv - > settings , " filter-mode " , " hidden_and_binary " ) ;
}
else if ( mode & XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN )
{
g_settings_set_string ( priv - > settings , " filter-mode " , " hidden " ) ;
}
else if ( mode & XED_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY )
{
g_settings_set_string ( priv - > settings , " filter-mode " , " binary " ) ;
} else
{
g_settings_set_string ( priv - > settings , " filter-mode " , " none " ) ;
}
}
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
static void
on_rename_cb ( XedFileBrowserStore * store ,
const gchar * olduri ,
const gchar * newuri ,
XedWindow * window )
{
XedApp * app ;
GList * documents ;
GList * item ;
XedDocument * doc ;
GFile * docfile ;
GFile * oldfile ;
GFile * newfile ;
gchar * uri ;
/* Find all documents and set its uri to newuri where it matches olduri */
app = xed_app_get_default ( ) ;
documents = xed_app_get_documents ( app ) ;
oldfile = g_file_new_for_uri ( olduri ) ;
newfile = g_file_new_for_uri ( newuri ) ;
for ( item = documents ; item ; item = item - > next )
{
doc = XED_DOCUMENT ( item - > data ) ;
uri = xed_document_get_uri ( doc ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
if ( ! uri )
{
continue ;
}
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
docfile = g_file_new_for_uri ( uri ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( g_file_equal ( docfile , oldfile ) )
{
xed_document_set_uri ( doc , newuri ) ;
}
else
{
gchar * relative ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
relative = g_file_get_relative_path ( oldfile , docfile ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( relative )
{
/* relative now contains the part in docfile without
the prefix oldfile */
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_unref ( docfile ) ;
g_free ( uri ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
docfile = g_file_get_child ( newfile , relative ) ;
uri = g_file_get_uri ( docfile ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
xed_document_set_uri ( doc , uri ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_free ( relative ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_free ( uri ) ;
g_object_unref ( docfile ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_unref ( oldfile ) ;
g_object_unref ( newfile ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_list_free ( documents ) ;
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
on_filter_pattern_changed_cb ( XedFileBrowserWidget * widget ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * pattern ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_object_get ( G_OBJECT ( widget ) , " filter-pattern " , & pattern , NULL ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
if ( pattern = = NULL )
{
g_settings_set_string ( priv - > settings , " filter-pattern " , " " ) ;
}
else
{
g_settings_set_string ( priv - > settings , " filter-pattern " , pattern ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_free ( pattern ) ;
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
on_virtual_root_changed_cb ( XedFileBrowserStore * store ,
GParamSpec * param ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * root ;
gchar * virtual_root ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
root = xed_file_browser_store_get_root ( store ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
if ( ! root )
{
return ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_settings_set_string ( priv - > onload_settings , " root " , root ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
virtual_root = xed_file_browser_store_get_virtual_root ( store ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( ! virtual_root )
{
/* Set virtual to same as root then */
g_settings_set_string ( priv - > onload_settings , " virtual-root " , root ) ;
}
else
{
g_settings_set_string ( priv - > onload_settings , " virtual-root " , virtual_root ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_signal_handlers_disconnect_by_func ( XED_WINDOW ( priv - > window ) , G_CALLBACK ( on_tab_added_cb ) , plugin ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_free ( root ) ;
g_free ( virtual_root ) ;
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2017-01-09 13:57:48 -06:00
on_tab_added_cb ( XedWindow * window ,
XedTab * tab ,
XedFileBrowserPlugin * plugin )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gboolean open ;
gboolean load_default = TRUE ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
open = g_settings_get_boolean ( priv - > settings , " open-at-first-doc " ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( open )
{
XedDocument * doc ;
gchar * uri ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
doc = xed_tab_get_document ( tab ) ;
uri = xed_document_get_uri ( doc ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( uri ! = NULL & & xed_utils_uri_has_file_scheme ( uri ) )
{
prepare_auto_root ( plugin ) ;
set_root_from_doc ( plugin , doc ) ;
load_default = FALSE ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
g_free ( uri ) ;
}
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
if ( load_default )
restore_default_location ( plugin ) ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
/* Disconnect this signal, it's only called once */
g_signal_handlers_disconnect_by_func ( window , G_CALLBACK ( on_tab_added_cb ) , plugin ) ;
2011-11-07 13:46:58 -06:00
}
static gchar *
2017-01-09 13:57:48 -06:00
get_filename_from_path ( GtkTreeModel * model ,
GtkTreePath * path )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
GtkTreeIter iter ;
gchar * uri ;
2011-11-07 13:46:58 -06:00
2017-01-09 13:57:48 -06:00
gtk_tree_model_get_iter ( model , & iter , path ) ;
gtk_tree_model_get ( model , & iter , XED_FILE_BROWSER_STORE_COLUMN_URI , & uri , - 1 ) ;
2011-11-07 18:10:16 -06:00
2017-01-09 13:57:48 -06:00
return xed_file_browser_utils_uri_basename ( uri ) ;
2011-11-07 13:46:58 -06:00
}
static gboolean
2017-01-09 13:57:48 -06:00
on_confirm_no_trash_cb ( XedFileBrowserWidget * widget ,
GList * files ,
XedWindow * window )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
gchar * normal ;
gchar * message ;
gchar * secondary ;
gboolean result ;
message = _ ( " Cannot move file to trash, do you \n want to delete permanently? " ) ;
if ( files - > next = = NULL )
{
normal = xed_file_browser_utils_file_basename ( G_FILE ( files - > data ) ) ;
secondary = g_strdup_printf ( _ ( " The file \" %s \" cannot be moved to the trash. " ) , normal ) ;
g_free ( normal ) ;
}
else
{
secondary = g_strdup ( _ ( " The selected files cannot be moved to the trash. " ) ) ;
}
result = xed_file_browser_utils_confirmation_dialog ( window ,
GTK_MESSAGE_QUESTION ,
message ,
secondary ,
GTK_STOCK_DELETE ,
NULL ) ;
g_free ( secondary ) ;
return result ;
2011-11-07 13:46:58 -06:00
}
static gboolean
2016-02-04 03:20:42 -06:00
on_confirm_delete_cb ( XedFileBrowserWidget * widget ,
2017-01-09 13:57:48 -06:00
XedFileBrowserStore * store ,
GList * paths ,
XedFileBrowserPlugin * plugin )
{
XedFileBrowserPluginPrivate * priv = plugin - > priv ;
gchar * normal ;
gchar * message ;
gchar * secondary ;
gboolean result ;
if ( paths - > next = = NULL )
{
normal = get_filename_from_path ( GTK_TREE_MODEL ( store ) , ( GtkTreePath * ) ( paths - > data ) ) ;
message = g_strdup_printf ( _ ( " Are you sure you want to permanently delete \" %s \" ? " ) , normal ) ;
g_free ( normal ) ;
}
else
{
message = g_strdup ( _ ( " Are you sure you want to permanently delete the selected files? " ) ) ;
}
secondary = _ ( " If you delete an item, it is permanently lost. " ) ;
result = xed_file_browser_utils_confirmation_dialog ( XED_WINDOW ( priv - > window ) ,
GTK_MESSAGE_QUESTION ,
message ,
secondary ,
GTK_STOCK_DELETE ,
NULL ) ;
g_free ( message ) ;
return result ;
}
G_MODULE_EXPORT void
peas_register_types ( PeasObjectModule * module )
2011-11-07 13:46:58 -06:00
{
2017-01-09 13:57:48 -06:00
xed_file_browser_plugin_register_type ( G_TYPE_MODULE ( module ) ) ;
peas_object_module_register_extension_type ( module ,
XED_TYPE_WINDOW_ACTIVATABLE ,
XED_TYPE_FILE_BROWSER_PLUGIN ) ;
2011-11-07 13:46:58 -06:00
}
// ex:ts=8:noet: