From 1d40b9f87269d44dd53fe3379b31aceb34798c8f Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 20 Dec 2013 13:26:28 -0500 Subject: [PATCH] tui: implement deactivation in "nmtui connect" --- tui/nmt-connect-connection-list.c | 36 ++++++++++++----- tui/nmt-connect-connection-list.h | 6 ++- tui/nmtui-connect.c | 64 ++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 17 deletions(-) diff --git a/tui/nmt-connect-connection-list.c b/tui/nmt-connect-connection-list.c index b51d2e216a..726eaab0b6 100644 --- a/tui/nmt-connect-connection-list.c +++ b/tui/nmt-connect-connection-list.c @@ -57,6 +57,7 @@ typedef struct { NMConnection *conn; NMAccessPoint *ap; NMDevice *device; + NMActiveConnection *active; } NmtConnectConnection; typedef struct { @@ -89,6 +90,7 @@ nmt_connect_connection_free (NmtConnectConnection *nmtconn) { g_clear_object (&nmtconn->conn); g_clear_object (&nmtconn->ap); + g_clear_object (&nmtconn->active); g_free (nmtconn->ssid); } @@ -374,9 +376,9 @@ sort_nmt_devices (gconstpointer a, return strcmp (nmta->name, nmtb->name); } -static gboolean -connection_is_active (NMConnection *conn, - const GPtrArray *acs) +static NMActiveConnection * +connection_find_ac (NMConnection *conn, + const GPtrArray *acs) { NMActiveConnection *ac; const char *path, *ac_path; @@ -388,10 +390,10 @@ connection_is_active (NMConnection *conn, ac_path = nm_active_connection_get_connection (ac); if (!strcmp (path, ac_path)) - return TRUE; + return ac; } - return FALSE; + return NULL; } static void @@ -448,9 +450,12 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list) for (citer = nmtdev->conns; citer; citer = citer->next) { nmtconn = citer->data; - if (nmtconn->conn && connection_is_active (nmtconn->conn, acs)) + if (nmtconn->conn) + nmtconn->active = connection_find_ac (nmtconn->conn, acs); + if (nmtconn->active) { + g_object_ref (nmtconn->active); active_col = '*'; - else + } else active_col = ' '; if (nmtconn->ap) { @@ -481,6 +486,9 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list) } priv->nmt_devices = nmt_devices; + + g_object_notify (G_OBJECT (listbox), "active"); + g_object_notify (G_OBJECT (listbox), "active-key"); } static void @@ -548,6 +556,8 @@ nmt_connect_connection_list_class_init (NmtConnectConnectionListClass *list_clas * @connection: (out) (transfer none): the #NMConnection to be activated * @device: (out) (transfer none): the #NMDevice to activate @connection on * @specific_object: (out) (transfer none): the "specific object" to connect to + * @active: (out) (transfer none): the #NMActiveConnection corresponding + * to the selection, if any. * * Gets information about the indicated connection. * @@ -558,7 +568,8 @@ nmt_connect_connection_list_get_connection (NmtConnectConnectionList *list, const char *identifier, NMConnection **connection, NMDevice **device, - NMObject **specific_object) + NMObject **specific_object, + NMActiveConnection **active) { NmtConnectConnectionListPrivate *priv = NMT_CONNECT_CONNECTION_LIST_GET_PRIVATE (list); GSList *diter, *citer; @@ -612,6 +623,8 @@ nmt_connect_connection_list_get_connection (NmtConnectConnectionList *list, *device = nmtconn->device; if (specific_object) *specific_object = NM_OBJECT (nmtconn->ap); + if (active) + *active = nmtconn->active; return TRUE; } @@ -622,6 +635,8 @@ nmt_connect_connection_list_get_connection (NmtConnectConnectionList *list, * @connection: (out) (transfer none): the #NMConnection to be activated * @device: (out) (transfer none): the #NMDevice to activate @connection on * @specific_object: (out) (transfer none): the "specific object" to connect to + * @active: (out) (transfer none): the #NMActiveConnection corresponding + * to the selection, if any. * * Gets information about the selected row. * @@ -631,7 +646,8 @@ gboolean nmt_connect_connection_list_get_selection (NmtConnectConnectionList *list, NMConnection **connection, NMDevice **device, - NMObject **specific_object) + NMObject **specific_object, + NMActiveConnection **active) { NmtConnectConnection *nmtconn; @@ -645,6 +661,8 @@ nmt_connect_connection_list_get_selection (NmtConnectConnectionList *list, *device = nmtconn->device; if (specific_object) *specific_object = NM_OBJECT (nmtconn->ap); + if (active) + *active = nmtconn->active; return TRUE; } diff --git a/tui/nmt-connect-connection-list.h b/tui/nmt-connect-connection-list.h index 8eb7a064c4..d47900437d 100644 --- a/tui/nmt-connect-connection-list.h +++ b/tui/nmt-connect-connection-list.h @@ -48,11 +48,13 @@ gboolean nmt_connect_connection_list_get_connection (NmtConnectConnectionList * const char *identifier, NMConnection **connection, NMDevice **device, - NMObject **specific_object); + NMObject **specific_object, + NMActiveConnection **active); gboolean nmt_connect_connection_list_get_selection (NmtConnectConnectionList *list, NMConnection **connection, NMDevice **device, - NMObject **specific_object); + NMObject **specific_object, + NMActiveConnection **active); G_END_DECLS diff --git a/tui/nmtui-connect.c b/tui/nmtui-connect.c index 7d075e3f70..9849048c2b 100644 --- a/tui/nmtui-connect.c +++ b/tui/nmtui-connect.c @@ -159,11 +159,19 @@ listbox_activated (NmtNewtListbox *listbox, NMConnection *connection; NMDevice *device; NMObject *specific_object; + NMActiveConnection *ac; - if (!nmt_connect_connection_list_get_selection (list, &connection, &device, &specific_object)) + if (!nmt_connect_connection_list_get_selection (list, + &connection, + &device, + &specific_object, + &ac)) return; - activate_connection (connection, device, specific_object); + if (ac) + nm_client_deactivate_connection (nm_client, ac); + else + activate_connection (connection, device, specific_object); } static void @@ -173,6 +181,41 @@ activate_clicked (NmtNewtButton *button, listbox_activated (listbox, NULL); } +static void +listbox_active_changed (GObject *object, + GParamSpec *pspec, + gpointer button) +{ + NmtConnectConnectionList *list = NMT_CONNECT_CONNECTION_LIST (object); + static const char *activate, *deactivate; + static int deactivate_padding, activate_padding; + NMActiveConnection *ac; + gboolean has_selection; + + if (G_UNLIKELY (activate == NULL)) { + int activate_len, deactivate_len; + + activate = _("Activate"); + activate_len = g_utf8_strlen (activate, -1); + deactivate = _("Deactivate"); + deactivate_len = g_utf8_strlen (deactivate, -1); + + activate_padding = MAX (0, deactivate_len - activate_len); + deactivate_padding = MAX (0, activate_len - deactivate_len); + } + + has_selection = nmt_connect_connection_list_get_selection (list, NULL, NULL, NULL, &ac); + + nmt_newt_component_set_sensitive (button, has_selection); + if (has_selection && ac) { + nmt_newt_button_set_label (button, deactivate); + nmt_newt_widget_set_padding (button, 0, 0, deactivate_padding, 0); + } else { + nmt_newt_button_set_label (button, activate); + nmt_newt_widget_set_padding (button, 0, 0, activate_padding, 0); + } +} + static void quit_clicked (NmtNewtButton *button, gpointer user_data) @@ -209,6 +252,8 @@ nmt_connect_connection_list (void) nmt_newt_widget_set_padding (bbox, 1, 1, 0, 1); activate = nmt_newt_button_box_add_start (NMT_NEWT_BUTTON_BOX (bbox), _("Activate")); + g_signal_connect (list, "notify::active", G_CALLBACK (listbox_active_changed), activate); + listbox_active_changed (G_OBJECT (list), NULL, activate); g_signal_connect (activate, "clicked", G_CALLBACK (activate_clicked), list); quit = nmt_newt_button_box_add_end (NMT_NEWT_BUTTON_BOX (bbox), _("Quit")); @@ -227,13 +272,20 @@ nmt_connect_connection (const char *identifier) NMConnection *connection; NMDevice *device; NMObject *specific_object; + NMActiveConnection *ac; list = nmt_connect_connection_list_new (); - if (nmt_connect_connection_list_get_connection (NMT_CONNECT_CONNECTION_LIST (list), identifier, - &connection, &device, &specific_object)) - activate_connection (connection, device, specific_object); - else + if (!nmt_connect_connection_list_get_connection (NMT_CONNECT_CONNECTION_LIST (list), + identifier, + &connection, + &device, + &specific_object, + &ac)) nmt_newt_message_dialog (_("No such connection '%s'"), identifier); + else if (ac) + nmt_newt_message_dialog (_("Connection is already active")); + else + activate_connection (connection, device, specific_object); g_object_unref (list); nmtui_quit ();