file: Make directory a construct property

We always call nautilus_file_set_directory() right after creating the
object, and a file is not expected to live without a directory.

Therefore, it makes sense to require the directory to be passed upon
object construction.

This is not just about adopting idiomatic GObject patterns: it's
going to become useful later for new file subclasses who will need to
know something about their parent directory upon construction.
This commit is contained in:
António Fernandes 2024-05-26 11:53:25 +01:00
parent 3153252095
commit 3c23786c8c
2 changed files with 49 additions and 13 deletions

View file

@ -134,20 +134,25 @@ real_new_file_from_filename (NautilusDirectory *directory,
{
if (self_owned)
{
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_SEARCH_DIRECTORY_FILE, NULL));
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_SEARCH_DIRECTORY_FILE,
"directory", directory,
NULL));
}
else
{
/* This doesn't normally happen, unless the user somehow types in a uri
* that references a file like this. (See #349840) */
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE, NULL));
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE,
"directory", directory,
NULL));
}
}
else
{
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE, NULL));
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE,
"directory", directory,
NULL));
}
nautilus_file_set_directory (file, directory);
return file;
}

View file

@ -182,6 +182,7 @@ G_DEFINE_TYPE_WITH_CODE (NautilusFile, nautilus_file, G_TYPE_OBJECT,
enum
{
PROP_0,
PROP_DIRECTORY,
PROP_DISPLAY_NAME,
N_PROPS
};
@ -587,16 +588,17 @@ void
nautilus_file_set_directory (NautilusFile *file,
NautilusDirectory *directory)
{
char *parent_uri;
if (!g_set_object (&file->details->directory, directory))
{
return;
}
g_autofree char *parent_uri = nautilus_file_get_parent_uri (file);
g_clear_object (&file->details->directory);
g_free (file->details->directory_name_collation_key);
file->details->directory = nautilus_directory_ref (directory);
parent_uri = nautilus_file_get_parent_uri (file);
file->details->directory_name_collation_key = g_utf8_collate_key_for_filename (parent_uri, -1);
g_free (parent_uri);
g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_DIRECTORY]);
}
NautilusFile *
@ -719,8 +721,9 @@ nautilus_file_new_from_info (NautilusDirectory *directory,
g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);
g_return_val_if_fail (info != NULL, NULL);
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE, NULL));
nautilus_file_set_directory (file, directory);
file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_VFS_FILE,
"directory", directory,
NULL));
update_info_and_name (file, info);
@ -8511,6 +8514,29 @@ nautilus_file_get_property (GObject *object,
}
}
static void
nautilus_file_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
NautilusFile *file = NAUTILUS_FILE (object);
switch (prop_id)
{
case PROP_DIRECTORY:
{
nautilus_file_set_directory (file, g_value_get_object (value));
}
break;
default:
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
}
static void
nautilus_file_class_init (NautilusFileClass *class)
{
@ -8554,6 +8580,7 @@ nautilus_file_class_init (NautilusFileClass *class)
G_OBJECT_CLASS (class)->finalize = finalize;
G_OBJECT_CLASS (class)->constructor = nautilus_file_constructor;
G_OBJECT_CLASS (class)->get_property = nautilus_file_get_property;
G_OBJECT_CLASS (class)->set_property = nautilus_file_set_property;
class->get_item_count = real_get_item_count;
class->get_deep_counts = real_get_deep_counts;
@ -8599,6 +8626,10 @@ nautilus_file_class_init (NautilusFileClass *class)
G_CALLBACK (mime_type_data_changed_callback),
NULL);
properties[PROP_DIRECTORY] = g_param_spec_object ("directory", NULL, NULL,
NAUTILUS_TYPE_DIRECTORY,
(G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
properties[PROP_DISPLAY_NAME] = g_param_spec_string ("display-name", NULL, NULL,
"",
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);