[gnome-commander] Add signal handling for adding and removing a GMount



commit 5a2d2911fbdf8a722d7fd759cfe5474ec0f32eeb
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Tue Nov 9 23:05:54 2021 +0100

    Add signal handling for adding and removing a GMount
    
    This is actually important for the integration of Gnome Commander into the desktop,
    because connections to remote resources, i.e. GMounts, can also be established or closed
    with other programs than Gnome Commander. These connections should then be integrated
    in the programm, for example by closing the connection in the file pane when it the
    connection itself was closed externally.

 src/gnome-cmd-con.cc  | 35 ++++++++++++++++++++++++
 src/gnome-cmd-con.h   |  2 ++
 src/gnome-cmd-data.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)
---
diff --git a/src/gnome-cmd-con.cc b/src/gnome-cmd-con.cc
index ab7a91a8..ce02416d 100644
--- a/src/gnome-cmd-con.cc
+++ b/src/gnome-cmd-con.cc
@@ -24,6 +24,8 @@
 #include "gnome-cmd-includes.h"
 #include "gnome-cmd-con.h"
 #include "gnome-cmd-plain-path.h"
+#include "gnome-cmd-main-win.h"
+#include "gnome-cmd-con-list.h"
 
 using namespace std;
 
@@ -673,3 +675,36 @@ std::string &gnome_cmd_con_make_smb_uri (std::string &uriString, std::string &se
     return uriString;
 }
 #endif
+
+
+/**
+ * This function checks if the active or inactive file pane is showing
+ * files from the given GMount. If yes, close the connection to this GMount
+ * and open the home connection instead.
+ */
+void gnome_cmd_con_close_active_or_inactive_connection (GMount *gMount)
+{
+    g_return_if_fail(gMount != nullptr);
+    g_return_if_fail(G_IS_MOUNT(gMount));
+    auto gFile = g_mount_get_root(gMount);
+    auto uriString = g_file_get_uri(gFile);
+
+    auto activeConUri = gnome_cmd_con_get_uri(main_win->fs(ACTIVE)->get_connection());
+    auto activeConGFile = activeConUri ? g_file_new_for_uri(activeConUri) : nullptr;
+    auto inactiveConUri = gnome_cmd_con_get_uri(main_win->fs(INACTIVE)->get_connection());
+    auto inactiveConGFile = inactiveConUri ? g_file_new_for_uri(inactiveConUri) : nullptr;
+
+    if (activeConUri && g_file_equal(gFile, activeConGFile))
+    {
+        gnome_cmd_con_close (main_win->fs(ACTIVE)->get_connection());
+        main_win->fs(ACTIVE)->set_connection(get_home_con());
+    }
+    if (inactiveConUri && g_file_equal(gFile, inactiveConGFile))
+    {
+        gnome_cmd_con_close (main_win->fs(INACTIVE)->get_connection());
+        main_win->fs(INACTIVE)->set_connection(get_home_con());
+    }
+
+    g_free(uriString);
+    g_object_unref(gFile);
+}
\ No newline at end of file
diff --git a/src/gnome-cmd-con.h b/src/gnome-cmd-con.h
index eaae6073..25c385eb 100644
--- a/src/gnome-cmd-con.h
+++ b/src/gnome-cmd-con.h
@@ -522,3 +522,5 @@ inline std::string &gnome_cmd_con_make_uri (std::string &s, ConnectionMethodID m
         default:            return s;
     }
 }
+
+void gnome_cmd_con_close_active_or_inactive_connection (GMount *gMount);
diff --git a/src/gnome-cmd-data.cc b/src/gnome-cmd-data.cc
index 43875a5a..d64a8f9d 100644
--- a/src/gnome-cmd-data.cc
+++ b/src/gnome-cmd-data.cc
@@ -2074,6 +2074,70 @@ inline gboolean device_mount_point_exists (GnomeCmdConList *list, const gchar *m
 }
 
 
+inline gboolean remote_con_stored_already (GnomeCmdConList *list, GFile *gFile)
+{
+    gboolean rc = FALSE;
+    GFile *conGFile = nullptr;
+    for (GList *tmp = gnome_cmd_con_list_get_all_remote(list); tmp; tmp = tmp->next)
+    {
+        auto con = static_cast <GnomeCmdCon*> (tmp->data);
+        auto uriString = static_cast<const char*>(gnome_cmd_con_get_uri(con));
+
+        conGFile = g_file_new_for_uri(uriString);
+        if(g_file_equal(conGFile, gFile))
+        {
+            rc = TRUE;
+            break;
+        }
+    }
+
+    if (conGFile)
+        g_object_unref(conGFile);
+
+    return rc;
+}
+
+
+/**
+ * This function will add mounts which are not for the 'file' scheme
+ * to the list of stored connections. This might be usefull when opening a remote
+ * connection with an external programm. This connection can be opened / used at
+ * a later time then also in Gnome Commander as it will be selectable from
+ * the connection list.
+ */
+static void add_gmount (GMount *gMount)
+{
+    g_return_if_fail (gMount != nullptr);
+
+    auto *gIcon = g_mount_get_icon (gMount);
+    char *name = g_mount_get_name (gMount);
+    auto gFile = g_mount_get_root(gMount);
+    auto uriString = g_file_get_uri(gFile);
+    auto scheme = g_file_get_uri_scheme(gFile);
+
+    //Don't care about 'local' mounts (for the moment)
+    if(!strcmp(scheme, "file"))
+    {
+        g_free (scheme);
+        g_free (name);
+        g_object_unref(gFile);
+        return;
+    }
+
+    if (!remote_con_stored_already (gnome_cmd_data.priv->con_list, gFile))
+    {
+        auto remoteCon = gnome_cmd_con_remote_new(name, uriString);
+        gnome_cmd_data.priv->con_list->add(remoteCon);
+    }
+
+    g_free (name);
+    g_free (uriString);
+    g_free (scheme);
+    g_object_unref (gIcon);
+    g_object_unref (gFile);
+}
+
+
 static void add_gvolume (GVolume *gVolume)
 {
     g_return_if_fail (gVolume != nullptr);
@@ -2118,6 +2182,16 @@ static void add_gvolume (GVolume *gVolume)
     g_object_unref (gVolume);
 }
 
+static void mount_added (GVolumeMonitor *volume_monitor, GMount *gMount)
+{
+    add_gmount (gMount);
+}
+
+static void mount_removed (GVolumeMonitor *volume_monitor, GMount *gMount)
+{
+    gnome_cmd_con_close_active_or_inactive_connection(gMount);
+}
+
 static void volume_added (GVolumeMonitor *volume_monitor, GVolume *gVolume)
 {
     add_gvolume (gVolume);
@@ -2132,6 +2206,8 @@ inline void set_g_volume_monitor ()
 {
     auto monitor = g_volume_monitor_get();
 
+    g_signal_connect (monitor, "mount-added",       G_CALLBACK (mount_added), nullptr);
+    g_signal_connect (monitor, "mount-removed",     G_CALLBACK (mount_removed), nullptr);
     g_signal_connect (monitor, "volume-added",      G_CALLBACK (volume_added), nullptr);
     g_signal_connect (monitor, "volume-removed",    G_CALLBACK (volume_removed), nullptr);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]