Commit 11ca4920 authored by Reuben Green's avatar Reuben Green
Browse files

Implement per directory saving of sort-column and sort-order (#8)

Implements the saving of sort-column and sort-order for each directory.
parent 85d12512
Pipeline #949 passed with stages
in 6 minutes and 7 seconds
......@@ -121,6 +121,48 @@ thunar_column_get_type (void)
const gchar*
thunar_column_string_from_value (ThunarColumn value)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_ref (THUNAR_TYPE_COLUMN);
enum_value = g_enum_get_value (enum_class, value);
g_type_class_unref (enum_class);
if (enum_value != NULL)
return enum_value->value_name;
else
return NULL;
}
gboolean
thunar_column_value_from_string (const gchar *value_string,
gint *value)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_ref (THUNAR_TYPE_COLUMN);
enum_value = g_enum_get_value_by_name (enum_class, value_string);
g_type_class_unref (enum_class);
if (enum_value != NULL)
{
*value = enum_value->value;
return TRUE;
}
else
return FALSE;
}
GType
thunar_icon_size_get_type (void)
{
......
......@@ -116,7 +116,10 @@ typedef enum
THUNAR_N_VISIBLE_COLUMNS = THUNAR_COLUMN_FILE,
} ThunarColumn;
GType thunar_column_get_type (void) G_GNUC_CONST;
GType thunar_column_get_type (void) G_GNUC_CONST;
const gchar* thunar_column_string_from_value (ThunarColumn value);
gboolean thunar_column_value_from_string (const gchar *value_string,
gint *value);
#define THUNAR_TYPE_ICON_SIZE (thunar_icon_size_get_type ())
......
......@@ -4624,9 +4624,15 @@ thunar_file_clear_view_metadata (ThunarFile *file)
return;
g_file_info_remove_attribute (file->info, "metadata::thunar::view-type");
g_file_info_remove_attribute (file->info, "metadata::thunar::sort-column");
g_file_info_remove_attribute (file->info, "metadata::thunar::sort-order");
g_file_set_attribute (file->gfile, "metadata::thunar::view-type", G_FILE_ATTRIBUTE_TYPE_INVALID,
NULL, G_FILE_QUERY_INFO_NONE, NULL, NULL);
g_file_set_attribute (file->gfile, "metadata::thunar::sort-column", G_FILE_ATTRIBUTE_TYPE_INVALID,
NULL, G_FILE_QUERY_INFO_NONE, NULL, NULL);
g_file_set_attribute (file->gfile, "metadata::thunar::sort-order", G_FILE_ATTRIBUTE_TYPE_INVALID,
NULL, G_FILE_QUERY_INFO_NONE, NULL, NULL);
thunar_file_changed (file);
}
......@@ -4651,6 +4657,10 @@ thunar_file_has_view_metadata (ThunarFile *file)
if (g_file_info_has_attribute (file->info, "metadata::thunar::view-type"))
return TRUE;
if (g_file_info_has_attribute (file->info, "metadata::thunar::sort-column"))
return TRUE;
if (g_file_info_has_attribute (file->info, "metadata::thunar::sort-order"))
return TRUE;
return FALSE;
}
......@@ -71,6 +71,7 @@ enum
PROP_SHOW_HIDDEN,
PROP_STATUSBAR_TEXT,
PROP_ZOOM_LEVEL,
PROP_PER_DIRECTORY,
PROP_THUMBNAIL_DRAW_FRAMES,
PROP_ACCEL_GROUP,
N_PROPERTIES
......@@ -131,6 +132,10 @@ static ThunarZoomLevel thunar_standard_view_get_zoom_level (Thu
static void thunar_standard_view_set_zoom_level (ThunarView *view,
ThunarZoomLevel zoom_level);
static void thunar_standard_view_reset_zoom_level (ThunarView *view);
static void thunar_standard_view_apply_per_directory (ThunarStandardView *standard_view,
ThunarFile *directory);
static void thunar_standard_view_set_per_directory (ThunarStandardView *standard_view,
gboolean per_directory);
static void thunar_standard_view_reload (ThunarView *view,
gboolean reload_info);
static gboolean thunar_standard_view_get_visible_range (ThunarView *view,
......@@ -275,6 +280,9 @@ struct _ThunarStandardViewPrivate
/* zoom-level support */
ThunarZoomLevel zoom_level;
/* per-directory settings */
gboolean per_directory;
/* scroll_to_file support */
GHashTable *scroll_to_files;
......@@ -435,6 +443,18 @@ thunar_standard_view_class_init (ThunarStandardViewClass *klass)
NULL,
EXO_PARAM_READABLE);
/**
* ThunarStandardView:per-directory:
*
* Whether to use per directory settings.
**/
standard_view_props[PROP_PER_DIRECTORY] =
g_param_spec_boolean ("per-directory",
"per-directory",
"per-directory",
FALSE,
EXO_PARAM_READWRITE);
/**
* ThunarStandardView:thumbnail-draw-frames:
*
......@@ -702,6 +722,9 @@ thunar_standard_view_constructor (GType type,
g_signal_connect (adjustment, "value-changed",
G_CALLBACK (thunar_standard_view_scrolled), object);
/* synchronise the "per-directory" property with the global "misc-per-directory" property */
exo_binding_new (G_OBJECT (standard_view->preferences), "misc-per-directory", G_OBJECT (standard_view), "per-directory");
/* done, we have a working object */
return object;
}
......@@ -921,6 +944,10 @@ thunar_standard_view_set_property (GObject *object,
thunar_view_set_zoom_level (THUNAR_VIEW (object), g_value_get_enum (value));
break;
case PROP_PER_DIRECTORY:
thunar_standard_view_set_per_directory (standard_view, g_value_get_boolean (value));
break;
case PROP_THUMBNAIL_DRAW_FRAMES:
g_object_set (G_OBJECT (standard_view->icon_factory), "thumbnail-draw-frames", g_value_get_boolean (value), NULL);
thunar_standard_view_reload(THUNAR_VIEW (object), TRUE);
......@@ -1277,6 +1304,10 @@ thunar_standard_view_set_current_directory (ThunarNavigator *navigator,
/* store the directory in the history */
thunar_navigator_set_current_directory (THUNAR_NAVIGATOR (standard_view->priv->history), current_directory);
/* if per-directory settings are enabled, set the sort column and sort order*/
if (standard_view->priv->per_directory)
thunar_standard_view_apply_per_directory (standard_view, current_directory);
/* We drop the model from the view as a simple optimization to speed up
* the process of disconnecting the model data from the view.
*/
......@@ -1531,6 +1562,86 @@ thunar_standard_view_reset_zoom_level (ThunarView *view)
static void
thunar_standard_view_apply_per_directory (ThunarStandardView *standard_view,
ThunarFile *directory)
{
const gchar *sort_column_name;
const gchar *sort_order_name;
gint sort_column;
GtkSortType sort_order;
/* get the default sort column and sort order */
g_object_get (G_OBJECT (standard_view->preferences), "last-sort-column", &sort_column, "last-sort-order", &sort_order, NULL);
/* get the stored per-directory settings (if any) */
sort_column_name = thunar_file_get_metadata_setting (directory, "sort-column");
sort_order_name = thunar_file_get_metadata_setting (directory, "sort-order");
/* convert the sort column name to a value */
if (sort_column_name != NULL)
thunar_column_value_from_string (sort_column_name, &sort_column);
/* convert the sort order name to a value */
if (sort_order_name != NULL)
{
if (g_strcmp0 (sort_order_name, "GTK_SORT_ASCENDING") == 0)
sort_order = GTK_SORT_ASCENDING;
if (g_strcmp0 (sort_order_name, "GTK_SORT_DESCENDING") == 0)
sort_order = GTK_SORT_DESCENDING;
}
/* thunar_standard_view_sort_column_changed saves the per-directory settings to the directory, but we do not
* want that behaviour here so we disconnect the signal before calling gtk_tree_sortable_set_sort_column_id */
g_signal_handlers_disconnect_by_func (G_OBJECT (standard_view->model),
G_CALLBACK (thunar_standard_view_sort_column_changed),
standard_view);
/* apply the sort column and sort order */
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (standard_view->model), sort_column, sort_order);
/* keep the currently selected files selected after the change */
thunar_component_restore_selection (THUNAR_COMPONENT (standard_view));
/* reconnect the signal */
g_signal_connect (G_OBJECT (standard_view->model),
"sort-column-changed",
G_CALLBACK (thunar_standard_view_sort_column_changed),
standard_view);
}
static void
thunar_standard_view_set_per_directory (ThunarStandardView *standard_view,
gboolean per_directory)
{
/* save the setting */
standard_view->priv->per_directory = per_directory;
/* if there is no current directory then return */
if (standard_view->priv->current_directory == NULL)
return;
/* apply the appropriate settings */
if (per_directory)
{
/* apply the per-directory settings (if any) */
thunar_standard_view_apply_per_directory (standard_view, standard_view->priv->current_directory);
}
else
{
gint sort_column;
GtkSortType sort_order;
/* apply the last sort column and sort order */
g_object_get (G_OBJECT (standard_view->preferences), "last-sort-column", &sort_column, "last-sort-order", &sort_order, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (standard_view->model), sort_column, sort_order);
}
}
static void
thunar_standard_view_reload (ThunarView *view,
gboolean reload_info)
......@@ -3010,14 +3121,36 @@ thunar_standard_view_sort_column_changed (GtkTreeSortable *tree_sortable,
/* keep the currently selected files selected after the change */
thunar_component_restore_selection (THUNAR_COMPONENT (standard_view));
/* determine the new sort column and sort order */
/* determine the new sort column and sort order, and save them */
if (gtk_tree_sortable_get_sort_column_id (tree_sortable, &sort_column, &sort_order))
{
/* remember the new values as default */
g_object_set (G_OBJECT (standard_view->preferences),
"last-sort-column", sort_column,
"last-sort-order", sort_order,
NULL);
if (!standard_view->priv->per_directory)
{
/* remember the new values as default */
g_object_set (G_OBJECT (standard_view->preferences),
"last-sort-column", sort_column,
"last-sort-order", sort_order,
NULL);
}
else
{
const gchar *sort_column_name;
const gchar *sort_order_name;
/* save the sort column name */
sort_column_name = thunar_column_string_from_value (sort_column);
if (sort_column_name != NULL)
thunar_file_set_metadata_setting (standard_view->priv->current_directory, "sort-column", sort_column_name);
/* convert the sort order to a string */
if (sort_order == GTK_SORT_ASCENDING)
sort_order_name = "GTK_SORT_ASCENDING";
if (sort_order == GTK_SORT_DESCENDING)
sort_order_name = "GTK_SORT_DESCENDING";
/* save the sort order */
thunar_file_set_metadata_setting (standard_view->priv->current_directory, "sort-order", sort_order_name);
}
}
}
......
......@@ -48,7 +48,7 @@ G_BEGIN_DECLS
"trash::*," \
"unix::gid,unix::uid,unix::mode," \
"metadata::emblems," \
"metadata::thunar::view-type,"
"metadata::thunar::view-type,metadata::thunar::sort-column,metadata::thunar::sort-order"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment