[network-manager-applet] applet: remove duplicated menu item creation code



commit 7a8407aadc14c4a9046ed0736662752e2d4ead36
Author: Dan Winship <danw gnome org>
Date:   Fri May 17 07:21:16 2013 -0400

    applet: remove duplicated menu item creation code
    
    applet-device-bt.c had copied a bunch of menu item creation code from
    applet-device-ethernet.c. Move that into applet.c, since it will also
    be used by other types soon.

 src/applet-device-bt.c       |   79 +------------------------------
 src/applet-device-ethernet.c |  108 +----------------------------------------
 src/applet.c                 |   96 +++++++++++++++++++++++++++++++++++++
 src/applet.h                 |   19 +++++++
 4 files changed, 120 insertions(+), 182 deletions(-)
---
diff --git a/src/applet-device-bt.c b/src/applet-device-bt.c
index 811a637..d6d864d 100644
--- a/src/applet-device-bt.c
+++ b/src/applet-device-bt.c
@@ -42,24 +42,6 @@
 #include "applet-dialogs.h"
 #include "nm-ui-utils.h"
 
-typedef struct {
-       NMApplet *applet;
-       NMDevice *device;
-       NMConnection *connection;
-} BtMenuItemInfo;
-
-static void
-bt_menu_item_info_destroy (gpointer data)
-{
-       BtMenuItemInfo *info = data;
-
-       g_object_unref (G_OBJECT (info->device));
-       if (info->connection)
-               g_object_unref (G_OBJECT (info->connection));
-
-       g_slice_free (BtMenuItemInfo, data);
-}
-
 static gboolean
 bt_new_auto_connection (NMDevice *device,
                         gpointer dclass_data,
@@ -72,63 +54,6 @@ bt_new_auto_connection (NMDevice *device,
 }
 
 static void
-bt_menu_item_activate (GtkMenuItem *item, gpointer user_data)
-{
-       BtMenuItemInfo *info = user_data;
-
-       applet_menu_item_activate_helper (info->device,
-                                         info->connection,
-                                         "/",
-                                         info->applet,
-                                         user_data);
-}
-
-
-typedef enum {
-       ADD_ACTIVE = 1,
-       ADD_INACTIVE = 2,
-} AddActiveInactiveEnum;
-
-static void
-add_connection_items (NMDevice *device,
-                      GSList *connections,
-                      NMConnection *active,
-                      AddActiveInactiveEnum flag,
-                      GtkWidget *menu,
-                      NMApplet *applet)
-{
-       GSList *iter;
-       BtMenuItemInfo *info;
-
-       for (iter = connections; iter; iter = g_slist_next (iter)) {
-               NMConnection *connection = NM_CONNECTION (iter->data);
-               GtkWidget *item;
-
-               if (active == connection) {
-                       if ((flag & ADD_ACTIVE) == 0)
-                               continue;
-               } else {
-                       if ((flag & ADD_INACTIVE) == 0)
-                               continue;
-               }
-
-               item = applet_new_menu_item_helper (connection, active, (flag & ADD_ACTIVE));
-
-               info = g_slice_new0 (BtMenuItemInfo);
-               info->applet = applet;
-               info->device = g_object_ref (G_OBJECT (device));
-               info->connection = g_object_ref (connection);
-
-               g_signal_connect_data (item, "activate",
-                                      G_CALLBACK (bt_menu_item_activate),
-                                      info,
-                                      (GClosureNotify) bt_menu_item_info_destroy, 0);
-
-               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-       }
-}
-
-static void
 bt_add_menu_item (NMDevice *device,
                   guint32 n_devices,
                   NMConnection *active,
@@ -154,7 +79,7 @@ bt_add_menu_item (NMDevice *device,
        gtk_widget_show (item);
 
        if (g_slist_length (connections))
-               add_connection_items (device, connections, active, ADD_ACTIVE, menu, applet);
+               applet_add_connection_items (device, connections, TRUE, active, NMA_ADD_ACTIVE, menu, applet);
 
        /* Notify user of unmanaged or unavailable device */
        item = nma_menu_device_get_menu_item (device, applet, NULL);
@@ -167,7 +92,7 @@ bt_add_menu_item (NMDevice *device,
                /* Add menu items for existing bluetooth connections for this device */
                if (g_slist_length (connections)) {
                        applet_menu_item_add_complex_separator_helper (menu, applet, _("Available"), -1);
-                       add_connection_items (device, connections, active, ADD_INACTIVE, menu, applet);
+                       applet_add_connection_items (device, connections, TRUE, active, NMA_ADD_INACTIVE, 
menu, applet);
                }
        }
 
diff --git a/src/applet-device-ethernet.c b/src/applet-device-ethernet.c
index fecad17..b06c06f 100644
--- a/src/applet-device-ethernet.c
+++ b/src/applet-device-ethernet.c
@@ -41,24 +41,6 @@
 #include "ethernet-dialog.h"
 #include "nm-ui-utils.h"
 
-typedef struct {
-       NMApplet *applet;
-       NMDevice *device;
-       NMConnection *connection;
-} EthernetMenuItemInfo;
-
-static void
-ethernet_menu_item_info_destroy (gpointer data)
-{
-       EthernetMenuItemInfo *info = (EthernetMenuItemInfo *) data;
-
-       g_object_unref (G_OBJECT (info->device));
-       if (info->connection)
-               g_object_unref (G_OBJECT (info->connection));
-
-       g_slice_free (EthernetMenuItemInfo, data);
-}
-
 #define DEFAULT_ETHERNET_NAME _("Auto Ethernet")
 
 static gboolean
@@ -94,90 +76,6 @@ ethernet_new_auto_connection (NMDevice *device,
 }
 
 static void
-ethernet_menu_item_activate (GtkMenuItem *item, gpointer user_data)
-{
-       EthernetMenuItemInfo *info = (EthernetMenuItemInfo *) user_data;
-
-       applet_menu_item_activate_helper (info->device,
-                                         info->connection,
-                                         "/",
-                                         info->applet,
-                                         user_data);
-}
-
-
-typedef enum {
-       ADD_ACTIVE = 1,
-       ADD_INACTIVE = 2,
-} AddActiveInactiveEnum;
-
-static void
-add_connection_items (NMDevice *device,
-                      GSList *connections,
-                      gboolean carrier,
-                      NMConnection *active,
-                      AddActiveInactiveEnum flag,
-                      GtkWidget *menu,
-                      NMApplet *applet)
-{
-       GSList *iter;
-       EthernetMenuItemInfo *info;
-
-       for (iter = connections; iter; iter = g_slist_next (iter)) {
-               NMConnection *connection = NM_CONNECTION (iter->data);
-               GtkWidget *item;
-
-               if (active == connection) {
-                       if ((flag & ADD_ACTIVE) == 0)
-                               continue;
-               } else {
-                       if ((flag & ADD_INACTIVE) == 0)
-                               continue;
-               }
-
-               item = applet_new_menu_item_helper (connection, active, (flag & ADD_ACTIVE));
-               gtk_widget_set_sensitive (item, carrier);
-
-               info = g_slice_new0 (EthernetMenuItemInfo);
-               info->applet = applet;
-               info->device = g_object_ref (G_OBJECT (device));
-               info->connection = g_object_ref (connection);
-
-               g_signal_connect_data (item, "activate",
-                                      G_CALLBACK (ethernet_menu_item_activate),
-                                      info,
-                                      (GClosureNotify) ethernet_menu_item_info_destroy, 0);
-
-               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-       }
-}
-
-static void
-add_default_connection_item (NMDevice *device,
-                             gboolean carrier,
-                             GtkWidget *menu,
-                             NMApplet *applet)
-{
-       EthernetMenuItemInfo *info;
-       GtkWidget *item;
-       
-       item = gtk_check_menu_item_new_with_label (DEFAULT_ETHERNET_NAME);
-       gtk_widget_set_sensitive (GTK_WIDGET (item), carrier);
-       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
-
-       info = g_slice_new0 (EthernetMenuItemInfo);
-       info->applet = applet;
-       info->device = g_object_ref (G_OBJECT (device));
-
-       g_signal_connect_data (item, "activate",
-                              G_CALLBACK (ethernet_menu_item_activate),
-                              info,
-                              (GClosureNotify) ethernet_menu_item_info_destroy, 0);
-
-       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-}
-
-static void
 ethernet_add_menu_item (NMDevice *device,
                         guint32 n_devices,
                         NMConnection *active,
@@ -223,7 +121,7 @@ ethernet_add_menu_item (NMDevice *device,
        gtk_widget_show (item);
 
        if (g_slist_length (connections))
-               add_connection_items (device, connections, carrier, active, ADD_ACTIVE, menu, applet);
+               applet_add_connection_items (device, connections, carrier, active, NMA_ADD_ACTIVE, menu, 
applet);
 
        /* Notify user of unmanaged or unavailable device */
        item = nma_menu_device_get_menu_item (device, applet, carrier ? NULL : _("disconnected"));
@@ -237,9 +135,9 @@ ethernet_add_menu_item (NMDevice *device,
                        applet_menu_item_add_complex_separator_helper (menu, applet, _("Available"), -1);
 
                if (g_slist_length (connections))
-                       add_connection_items (device, connections, carrier, active, ADD_INACTIVE, menu, 
applet);
+                       applet_add_connection_items (device, connections, carrier, active, NMA_ADD_INACTIVE, 
menu, applet);
                else
-                       add_default_connection_item (device, carrier, menu, applet);
+                       applet_add_default_connection_item (device, DEFAULT_ETHERNET_NAME, carrier, menu, 
applet);
        }
 
        g_slist_free (connections);
diff --git a/src/applet.c b/src/applet.c
index 8a57043..2469021 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -2138,6 +2138,102 @@ static GtkWidget *nma_context_menu_create (NMApplet *applet)
        return GTK_WIDGET (menu);
 }
 
+typedef struct {
+       NMApplet *applet;
+       NMDevice *device;
+       NMConnection *connection;
+} AppletMenuItemInfo;
+
+static void
+applet_menu_item_info_destroy (gpointer data, GClosure *closure)
+{
+       AppletMenuItemInfo *info = data;
+
+       g_clear_object (&info->device);
+       g_clear_object (&info->connection);
+
+       g_slice_free (AppletMenuItemInfo, data);
+}
+
+static void
+applet_menu_item_activate (GtkMenuItem *item, gpointer user_data)
+{
+       AppletMenuItemInfo *info = user_data;
+
+       applet_menu_item_activate_helper (info->device,
+                                         info->connection,
+                                         "/",
+                                         info->applet,
+                                         user_data);
+}
+
+void
+applet_add_connection_items (NMDevice *device,
+                             GSList *connections,
+                             gboolean sensitive,
+                             NMConnection *active,
+                             NMAAddActiveInactiveEnum flag,
+                             GtkWidget *menu,
+                             NMApplet *applet)
+{
+       GSList *iter;
+       AppletMenuItemInfo *info;
+
+       for (iter = connections; iter; iter = iter->next) {
+               NMConnection *connection = NM_CONNECTION (iter->data);
+               GtkWidget *item;
+
+               if (active == connection) {
+                       if ((flag & NMA_ADD_ACTIVE) == 0)
+                               continue;
+               } else {
+                       if ((flag & NMA_ADD_INACTIVE) == 0)
+                               continue;
+               }
+
+               item = applet_new_menu_item_helper (connection, active, (flag & NMA_ADD_ACTIVE));
+               gtk_widget_set_sensitive (item, sensitive);
+
+               info = g_slice_new0 (AppletMenuItemInfo);
+               info->applet = applet;
+               info->device = g_object_ref (device);
+               info->connection = g_object_ref (connection);
+
+               g_signal_connect_data (item, "activate",
+                                      G_CALLBACK (applet_menu_item_activate),
+                                      info,
+                                      applet_menu_item_info_destroy, 0);
+
+               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+       }
+}
+
+void
+applet_add_default_connection_item (NMDevice *device,
+                                    const char *label,
+                                    gboolean sensitive,
+                                    GtkWidget *menu,
+                                    NMApplet *applet)
+{
+       AppletMenuItemInfo *info;
+       GtkWidget *item;
+       
+       item = gtk_check_menu_item_new_with_label (label);
+       gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
+       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
+
+       info = g_slice_new0 (AppletMenuItemInfo);
+       info->applet = applet;
+       info->device = g_object_ref (device);
+
+       g_signal_connect_data (item, "activate",
+                              G_CALLBACK (applet_menu_item_activate),
+                              info,
+                              applet_menu_item_info_destroy, 0);
+
+       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+}
+
 
 /*****************************************************************************/
 
diff --git a/src/applet.h b/src/applet.h
index b7a50f1..45415d4 100644
--- a/src/applet.h
+++ b/src/applet.h
@@ -334,4 +334,23 @@ gboolean applet_wifi_connect_to_8021x_network (NMApplet *applet,
 gboolean applet_wifi_create_wifi_network (NMApplet *applet);
 gboolean applet_wifi_can_create_wifi_network (NMApplet *applet);
 
+typedef enum {
+       NMA_ADD_ACTIVE = 1,
+       NMA_ADD_INACTIVE = 2,
+} NMAAddActiveInactiveEnum;
+
+void applet_add_connection_items (NMDevice *device,
+                                  GSList *connections,
+                                  gboolean sensitive,
+                                  NMConnection *active,
+                                  NMAAddActiveInactiveEnum flag,
+                                  GtkWidget *menu,
+                                  NMApplet *applet);
+
+void applet_add_default_connection_item (NMDevice *device,
+                                         const char *label,
+                                         gboolean sensitive,
+                                         GtkWidget *menu,
+                                         NMApplet *applet);
+
 #endif


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