Commit c56fa101 authored by Ernestas Kulik's avatar Ernestas Kulik 🦑

Separate the file table

parent 926810ba
......@@ -19,6 +19,8 @@ nautilus_ng_sources = ['nautilus-task.c',
'nautilus-file-changes.h',
'nautilus-signal-utilities.c',
'nautilus-signal-utilities.h',
'nautilus-file-table.c',
'nautilus-file-table.h',
'main.c']
nautilus_ng_dependencies = [gio, glib]
......
/* 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 <https://www.gnu.org/licenses/>.
*/
#include "nautilus-file-table.h"
static GMutex mutex;
static gpointer
create_hash_table (gpointer data)
{
(void) data;
return g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal,
g_object_unref, NULL);
}
static GHashTable *
get_hash_table (void)
{
static GOnce once = G_ONCE_INIT;
g_once (&once, create_hash_table, NULL);
g_assert (once.retval != NULL);
return once.retval;
}
gboolean
nautilus_file_table_insert (GFile *location,
NautilusFile *instance)
{
gboolean success;
g_mutex_lock (&mutex);
success = g_hash_table_insert (get_hash_table (), location, instance);
g_mutex_unlock (&mutex);
return success;
}
gboolean
nautilus_file_table_remove (GFile *location)
{
gboolean success;
g_mutex_lock (&mutex);
success = g_hash_table_remove (get_hash_table (), location);
g_mutex_unlock (&mutex);
return success;
}
NautilusFile *
nautilus_file_table_lookup (GFile *location)
{
gpointer instance;
g_mutex_lock (&mutex);
instance = g_hash_table_lookup (get_hash_table (), location);
g_mutex_unlock (&mutex);
return instance;
}
/* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef NAUTILUS_FILE_TABLE_H_INCLUDED
#define NAUTILUS_FILE_TABLE_H_INCLUDED
#include "nautilus-file.h"
#include <gio/gio.h>
gboolean nautilus_file_table_insert (GFile *location,
NautilusFile *instance);
gboolean nautilus_file_table_remove (GFile *location);
NautilusFile *nautilus_file_table_lookup (GFile *location);
#endif
......@@ -20,6 +20,7 @@
#include "nautilus-cache.h"
#include "nautilus-directory.h"
#include "nautilus-file-table.h"
#include "nautilus-task-manager.h"
#include "tasks/nautilus-attribute-task.h"
......@@ -53,8 +54,6 @@ enum
static GParamSpec *properties[N_PROPERTIES] = { NULL };
static guint signals[LAST_SIGNAL] = { 0 };
static GHashTable *files = NULL;
static GMutex files_mutex;
static GObject *
constructor (GType type,
......@@ -74,16 +73,7 @@ constructor (GType type,
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);
instance = nautilus_file_table_lookup (location);
if (instance != NULL)
{
instance = g_object_ref (instance);
......@@ -96,11 +86,9 @@ constructor (GType type,
instance = parent_class->constructor (type, n_construct_properties,
construct_properties);
g_assert (g_hash_table_insert (files, location, instance));
g_assert (nautilus_file_table_insert (location, instance));
}
g_mutex_unlock (&files_mutex);
return instance;
}
......@@ -136,9 +124,7 @@ finalize (GObject *object)
priv = nautilus_file_get_instance_private (NAUTILUS_FILE (object));
g_mutex_lock (&files_mutex);
g_hash_table_remove (files, priv->location);
g_mutex_unlock (&files_mutex);
g_assert (nautilus_file_table_remove (priv->location));
G_OBJECT_CLASS (nautilus_file_parent_class)->finalize (object);
}
......@@ -155,15 +141,11 @@ renamed (NautilusFile *file,
(gpointer) file, (gpointer) priv->location,
(gpointer) new_location);
g_mutex_lock (&files_mutex);
g_hash_table_remove (files, priv->location);
g_assert (nautilus_file_table_remove (priv->location));
priv->location = g_object_ref (new_location);
g_assert (g_hash_table_insert (files, new_location, file));
g_mutex_unlock (&files_mutex);
g_assert (nautilus_file_table_insert (new_location, file));
nautilus_cache_item_invalidate (priv->cache, priv->cache_items[INFO],
FALSE);
......@@ -326,23 +308,16 @@ nautilus_file_query_info (NautilusFile *file,
NautilusFile *
nautilus_file_get_existing (GFile *location)
{
NautilusFile *file = NULL;
NautilusFile *file;
g_return_val_if_fail (G_IS_FILE (location), NULL);
g_mutex_lock (&files_mutex);
if (files != NULL)
file = nautilus_file_table_lookup (location);
if (file != NULL)
{
file = g_hash_table_lookup (files, location);
if (file != NULL)
{
file = g_object_ref (file);
}
file = g_object_ref (file);
}
g_mutex_unlock (&files_mutex);
return file;
}
......
Markdown is supported
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