Skip to content

Commit

Permalink
Eliminate usage of GVolumeMonitor if no FmFolder object was created.
Browse files Browse the repository at this point in the history
If libfm is used without FmFolder usage then there is no reason to run
the volume monitor. Therefore it should be activated when some folder
was opened and deactivated when all of them were closed.
This may be a culprit of external green drives been waked up (see the
http://sourceforge.net/p/lxde/bugs/751/ bugreport against lxpanel).
  • Loading branch information
LStranger committed May 13, 2015
1 parent e9575e9 commit 994a1e2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

* Rewritten broken rubberband rendering in icon view for GTK3.

* Eliminated usage of GVolumeMonitor if no FmFolder object was created.


Changes on 1.2.3 since 1.2.2:

Expand Down
63 changes: 45 additions & 18 deletions src/base/fm-folder.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct _FmFolder
};

static void fm_folder_dispose(GObject *object);
static void fm_folder_finalize(GObject *object);
static void fm_folder_content_changed(FmFolder* folder);

static GList* _fm_folder_get_file_by_path(FmFolder* folder, FmPath *path);
Expand All @@ -99,9 +100,13 @@ G_DEFINE_TYPE(FmFolder, fm_folder, G_TYPE_OBJECT);

static guint signals[N_SIGNALS];
static GHashTable* hash = NULL;
static int hash_uses = 0;

static GVolumeMonitor* volume_monitor = NULL;

static void on_mount_added(GVolumeMonitor* vm, GMount* mount, gpointer user_data);
static void on_mount_removed(GVolumeMonitor* vm, GMount* mount, gpointer user_data);

/* used for on_query_filesystem_info_finished() to lock folder */
G_LOCK_DEFINE_STATIC(query);
/* protects hash access */
Expand All @@ -115,6 +120,7 @@ static void fm_folder_class_init(FmFolderClass *klass)
FmFolderClass* folder_class;
g_object_class = G_OBJECT_CLASS(klass);
g_object_class->dispose = fm_folder_dispose;
g_object_class->finalize = fm_folder_finalize;
fm_folder_parent_class = (GObjectClass*)g_type_class_peek(G_TYPE_OBJECT);

folder_class = FM_FOLDER_CLASS(klass);
Expand Down Expand Up @@ -336,6 +342,19 @@ static void fm_folder_class_init(FmFolderClass *klass)
static void fm_folder_init(FmFolder *folder)
{
folder->files = fm_file_info_list_new();
G_LOCK(hash);
if (G_UNLIKELY(hash_uses == 0))
{
hash = g_hash_table_new((GHashFunc)fm_path_hash, (GEqualFunc)fm_path_equal);
volume_monitor = g_volume_monitor_get();
if (G_LIKELY(volume_monitor))
{
g_signal_connect(volume_monitor, "mount-added", G_CALLBACK(on_mount_added), NULL);
g_signal_connect(volume_monitor, "mount-removed", G_CALLBACK(on_mount_removed), NULL);
}
}
hash_uses++;
G_UNLOCK(hash);
}

static gboolean on_idle_reload(FmFolder* folder)
Expand Down Expand Up @@ -831,7 +850,7 @@ static FmFolder* fm_folder_get_internal(FmPath* path, GFile* gf)
* to associate all kinds of data structures with FmPaths? */

G_LOCK(hash);
folder = (FmFolder*)g_hash_table_lookup(hash, path);
folder = hash ? (FmFolder*)g_hash_table_lookup(hash, path) : NULL;

if( G_UNLIKELY(!folder) )
{
Expand Down Expand Up @@ -966,6 +985,27 @@ static void fm_folder_dispose(GObject *object)
(* G_OBJECT_CLASS(fm_folder_parent_class)->dispose)(object);
}

static void fm_folder_finalize(GObject *object)
{
G_LOCK(hash);
hash_uses--;
if (G_UNLIKELY(hash_uses == 0))
{
g_hash_table_destroy(hash);
hash = NULL;
if(volume_monitor)
{
g_signal_handlers_disconnect_by_func(volume_monitor, on_mount_added, NULL);
g_signal_handlers_disconnect_by_func(volume_monitor, on_mount_removed, NULL);
g_object_unref(volume_monitor);
volume_monitor = NULL;
}
}
G_UNLOCK(hash);

(* G_OBJECT_CLASS(fm_folder_parent_class)->finalize)(object);
}

/**
* fm_folder_from_gfile
* @gf: #GFile file descriptor
Expand Down Expand Up @@ -1423,8 +1463,11 @@ void fm_folder_query_filesystem_info(FmFolder* folder)
*/
FmFolder *fm_folder_find_by_path(FmPath *path)
{
FmFolder *folder = (FmFolder*)g_hash_table_lookup(hash, path);
FmFolder *folder;

G_LOCK(hash);
folder = hash ? (FmFolder*)g_hash_table_lookup(hash, path) : NULL;
G_UNLOCK(hash);
return folder ? g_object_ref(folder) : NULL;
}

Expand Down Expand Up @@ -1611,24 +1654,8 @@ static void on_mount_removed(GVolumeMonitor* vm, GMount* mount, gpointer user_da

void _fm_folder_init()
{
hash = g_hash_table_new((GHashFunc)fm_path_hash, (GEqualFunc)fm_path_equal);
volume_monitor = g_volume_monitor_get();
if(G_LIKELY(volume_monitor))
{
g_signal_connect(volume_monitor, "mount-added", G_CALLBACK(on_mount_added), NULL);
g_signal_connect(volume_monitor, "mount-removed", G_CALLBACK(on_mount_removed), NULL);
}
}

void _fm_folder_finalize()
{
g_hash_table_destroy(hash);
hash = NULL;
if(volume_monitor)
{
g_signal_handlers_disconnect_by_func(volume_monitor, on_mount_added, NULL);
g_signal_handlers_disconnect_by_func(volume_monitor, on_mount_removed, NULL);
g_object_unref(volume_monitor);
volume_monitor = NULL;
}
}

1 comment on commit 994a1e2

@R-a-l-f
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you,

this commit fixed the lxpanel issue.
The green drive no longer is woken up by lxpanel.

$ pacman -Q lxpanel libfm-gtk-git
lxpanel 0.8.0-1
libfm-gtk-git 1.2.3.31.g994a1e2-1

Regards,
Ralf

Please sign in to comment.