diff options
-rw-r--r-- | src-ng/main.c | 12 | ||||
-rw-r--r-- | src-ng/meson.build | 2 | ||||
-rw-r--r-- | src-ng/nautilus-directory.c | 47 | ||||
-rw-r--r-- | src-ng/nautilus-directory.h | 32 | ||||
-rw-r--r-- | src-ng/nautilus-file.c | 84 |
5 files changed, 153 insertions, 24 deletions
diff --git a/src-ng/main.c b/src-ng/main.c index 0a3ad0a60..f5474bb79 100644 --- a/src-ng/main.c +++ b/src-ng/main.c @@ -2,6 +2,7 @@ #include <glib.h> +#include "nautilus-directory.h" #include "nautilus-file.h" #include "nautilus-task-manager.h" @@ -11,8 +12,9 @@ got_info (NautilusFile *file, GError *error, gpointer user_data) { - g_message ("Got info for %p\n\tDisplay name: %s", - (gpointer) file, + g_message ("Got info for %p", + (gpointer) file); + g_message ("\tDisplay name: %s\n", g_file_info_get_display_name (info)); g_object_unref (info); @@ -41,11 +43,13 @@ main (int argc, g_message ("Creating NautilusFile"); file = nautilus_file_new (location); - g_message ("Got %p\n", (gpointer) file); + g_message ("\tGot %p", (gpointer) file); + g_message ("\tFile is directory: %s\n", + NAUTILUS_IS_DIRECTORY (file)? "yes" : "no"); g_message ("Creating another NautilusFile for the same location"); duplicate_file = nautilus_file_new (location); - g_message ("Got %p, which is %s\n", + g_message ("\tGot %p, which is %s\n", (gpointer) duplicate_file, file == duplicate_file? "the same" : "not the same"); diff --git a/src-ng/meson.build b/src-ng/meson.build index dbda07e75..8f1fec6ee 100644 --- a/src-ng/meson.build +++ b/src-ng/meson.build @@ -6,6 +6,8 @@ nautilus_ng_sources = ['nautilus-task.c', 'nautilus-file.h', 'tasks/nautilus-attribute-task.c', 'tasks/nautilus-attribute-task.h', + 'nautilus-directory.c', + 'nautilus-directory.h', 'main.c'] nautilus_ng_dependencies = [gio, glib] diff --git a/src-ng/nautilus-directory.c b/src-ng/nautilus-directory.c new file mode 100644 index 000000000..e509265a2 --- /dev/null +++ b/src-ng/nautilus-directory.c @@ -0,0 +1,47 @@ +/* Copyright (C) 2017 Ernestas Kulik <ernestask@gnome.org> + * + * This file is part of Nautilus. + * + * Nautilus 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 of the License, or + * (at your option) any later version. + * + * Nautilus 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 Nautilus. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "nautilus-directory.h" + +G_DEFINE_TYPE (NautilusDirectory, nautilus_directory, NAUTILUS_TYPE_FILE) + +static void +nautilus_directory_class_init (NautilusDirectoryClass *klass) +{ +} + +static void +nautilus_directory_init (NautilusDirectory *self) +{ +} + +NautilusFile * +nautilus_directory_new (GFile *location) +{ + gpointer instance; + + g_return_val_if_fail (G_IS_FILE (location), NULL); + + instance = g_object_new (NAUTILUS_TYPE_DIRECTORY, + "location", location, + NULL); + + g_assert (NAUTILUS_IS_DIRECTORY (instance)); + + return instance; +} diff --git a/src-ng/nautilus-directory.h b/src-ng/nautilus-directory.h new file mode 100644 index 000000000..be1c33a69 --- /dev/null +++ b/src-ng/nautilus-directory.h @@ -0,0 +1,32 @@ +/* Copyright (C) 2017 Ernestas Kulik <ernestask@gnome.org> + * + * This file is part of Nautilus. + * + * Nautilus 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 of the License, or + * (at your option) any later version. + * + * Nautilus 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 Nautilus. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "nautilus-file.h" + +#define NAUTILUS_TYPE_DIRECTORY (nautilus_directory_get_type ()) + +G_DECLARE_DERIVABLE_TYPE (NautilusDirectory, nautilus_directory, + NAUTILUS, DIRECTORY, + NautilusFile) + +struct _NautilusDirectoryClass +{ + NautilusFileClass parent_class; +}; + +NautilusFile *nautilus_directory_new (GFile *location); diff --git a/src-ng/nautilus-file.c b/src-ng/nautilus-file.c index 3b8c552cc..65692a83a 100644 --- a/src-ng/nautilus-file.c +++ b/src-ng/nautilus-file.c @@ -18,6 +18,7 @@ #include "nautilus-file.h" +#include "nautilus-directory.h" #include "nautilus-task-manager.h" #include "tasks/nautilus-attribute-task.h" @@ -50,6 +51,54 @@ static GParamSpec *properties[N_PROPERTIES] = { NULL }; static GHashTable *files = NULL; static GMutex files_mutex; +static GObject * +constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_properties) +{ + GFile *location = NULL; + gpointer instance; + + for (guint i = 0; i < n_construct_properties; i++) + { + if (construct_properties[i].pspec == properties[PROP_LOCATION]) + { + location = g_value_get_object (construct_properties[i].value); + } + } + + g_assert (location != NULL); + + g_mutex_lock (&files_mutex); + + if (files == NULL) + { + files = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, + g_object_unref, NULL); + } + + + instance = g_hash_table_lookup (files, location); + if (instance != NULL) + { + instance = g_object_ref (instance); + } + else + { + GObjectClass *parent_class; + + parent_class = G_OBJECT_CLASS (nautilus_file_parent_class); + instance = parent_class->constructor (type, n_construct_properties, + construct_properties); + + g_assert (g_hash_table_insert (files, location, instance)); + } + + g_mutex_unlock (&files_mutex); + + return instance; +} + static void set_property (GObject *object, guint property_id, @@ -98,6 +147,7 @@ nautilus_file_class_init (NautilusFileClass *klass) object_class = G_OBJECT_CLASS (klass); + object_class->constructor = constructor; object_class->set_property = set_property; object_class->finalize = finalize; @@ -211,32 +261,26 @@ nautilus_file_query_info (NautilusFile *file, NautilusFile * nautilus_file_new (GFile *location) { - NautilusFile *file; + GFileType file_type; g_return_val_if_fail (G_IS_FILE (location), NULL); - g_mutex_lock (&files_mutex); - - if (files == NULL) - { - files = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal, - g_object_unref, NULL); - } - - - file = g_hash_table_lookup (files, location); - if (file != NULL) + /* TODO: extension points? */ + file_type = g_file_query_file_type (location, G_FILE_QUERY_INFO_NONE, + NULL); + /* File does not exist. + * Search directory URIs also fall under this category. + * TODO: creation? + */ + if (file_type == G_FILE_TYPE_UNKNOWN) { - file = g_object_ref (file); + return NULL; } - else + else if (file_type == G_FILE_TYPE_DIRECTORY) { - file = g_object_new (NAUTILUS_TYPE_FILE, "location", location, NULL); - - g_assert (g_hash_table_insert (files, location, file)); + /* Asserts that the constructed file is a directory. */ + return nautilus_directory_new (location); } - g_mutex_unlock (&files_mutex); - - return file; + return g_object_new (NAUTILUS_TYPE_FILE, "location", location, NULL); } |