diff options
Diffstat (limited to 'src/applet.c')
-rw-r--r-- | src/applet.c | 283 |
1 files changed, 135 insertions, 148 deletions
diff --git a/src/applet.c b/src/applet.c index a34e4081..761f92c8 100644 --- a/src/applet.c +++ b/src/applet.c @@ -55,11 +55,11 @@ extern gboolean shell_debug; extern gboolean with_agent; -#ifdef ENABLE_INDICATOR -#define INDICATOR_ENABLED(a) ((a)->app_indicator != NULL) +#ifdef WITH_APPINDICATOR +#define INDICATOR_ENABLED(a) ((a)->app_indicator) #else #define INDICATOR_ENABLED(a) (FALSE) -#endif +#endif /* WITH_APPINDICATOR */ G_DEFINE_TYPE (NMApplet, nma, G_TYPE_APPLICATION) @@ -490,31 +490,36 @@ applet_menu_item_activate_helper (NMDevice *device, void applet_menu_item_add_complex_separator_helper (GtkWidget *menu, NMApplet *applet, - const gchar* label) + const gchar *label) { -#ifdef ENABLE_INDICATOR - gtk_menu_shell_append (GTK_MENU_SHELL (menu), gtk_separator_menu_item_new ()); -#else - GtkWidget *menu_item = gtk_menu_item_new (); - GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); - GtkWidget *xlabel = NULL; + GtkWidget *menu_item; + + if (INDICATOR_ENABLED (applet)) { + /* Indicator doesn't draw complex separators */ + menu_item = gtk_separator_menu_item_new (); + } else { + GtkWidget *box, *xlabel; + + menu_item = gtk_menu_item_new (); + box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); - if (label) { - xlabel = gtk_label_new (NULL); - gtk_label_set_markup (GTK_LABEL (xlabel), label); + if (label) { + xlabel = gtk_label_new (NULL); + gtk_label_set_markup (GTK_LABEL (xlabel), label); + + gtk_box_pack_start (GTK_BOX (box), gtk_separator_new (GTK_ORIENTATION_HORIZONTAL), TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (box), xlabel, FALSE, FALSE, 2); + } gtk_box_pack_start (GTK_BOX (box), gtk_separator_new (GTK_ORIENTATION_HORIZONTAL), TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (box), xlabel, FALSE, FALSE, 2); - } - gtk_box_pack_start (GTK_BOX (box), gtk_separator_new (GTK_ORIENTATION_HORIZONTAL), TRUE, TRUE, 0); + g_object_set (G_OBJECT (menu_item), + "child", box, + "sensitive", FALSE, + NULL); + } - g_object_set (G_OBJECT (menu_item), - "child", box, - "sensitive", FALSE, - NULL); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item); -#endif } GtkWidget * @@ -540,7 +545,6 @@ applet_new_menu_item_helper (NMConnection *connection, return item; } -#ifndef ENABLE_INDICATOR #define TITLE_TEXT_R ((double) 0x5e / 255.0 ) #define TITLE_TEXT_G ((double) 0x5e / 255.0 ) #define TITLE_TEXT_B ((double) 0x5e / 255.0 ) @@ -604,7 +608,6 @@ menu_title_item_draw (GtkWidget *widget, cairo_t *cr, gpointer user_data) menu_item_draw_generic (widget, cr); return TRUE; } -#endif /* ENABLE_INDICATOR */ GtkWidget * applet_menu_item_create_device_item_helper (NMDevice *device, @@ -615,9 +618,8 @@ applet_menu_item_create_device_item_helper (NMDevice *device, item = gtk_menu_item_new_with_label (text); gtk_widget_set_sensitive (item, FALSE); -#ifndef ENABLE_INDICATOR - g_signal_connect (item, "draw", G_CALLBACK (menu_title_item_draw), NULL); -#endif + if (!INDICATOR_ENABLED (applet)) + g_signal_connect (item, "draw", G_CALLBACK (menu_title_item_draw), NULL); return item; } @@ -675,13 +677,15 @@ applet_do_notify (NMApplet *applet, g_return_if_fail (summary != NULL); g_return_if_fail (message != NULL); -#ifdef ENABLE_INDICATOR - if (app_indicator_get_status (applet->app_indicator) == APP_INDICATOR_STATUS_PASSIVE) - return; -#else - if (!gtk_status_icon_is_embedded (applet->status_icon)) - return; -#endif + if (INDICATOR_ENABLED (applet)) { +#ifdef WITH_APPINDICATOR + if (app_indicator_get_status (applet->app_indicator) == APP_INDICATOR_STATUS_PASSIVE) + return; +#endif /* WITH_APPINDICATOR */ + } else { + if (!gtk_status_icon_is_embedded (applet->status_icon)) + return; + } /* if we're not acting as a secret agent, don't notify either */ if (!applet->agent) @@ -1004,9 +1008,7 @@ activate_vpn_cb (GObject *client, g_free (info); } -#ifdef ENABLE_INDICATOR static void nma_menu_disconnect_vpn_item_activate (GtkMenuItem *item, gpointer user_data); -#endif static void nma_menu_vpn_item_clicked (GtkMenuItem *item, gpointer user_data) @@ -1030,9 +1032,9 @@ nma_menu_vpn_item_clicked (GtkMenuItem *item, gpointer user_data) } if (applet_get_active_for_connection (applet, connection)) { -#ifdef ENABLE_INDICATOR - nma_menu_disconnect_vpn_item_activate (item, applet); -#endif + if (INDICATOR_ENABLED (applet)) + nma_menu_disconnect_vpn_item_activate (item, applet); + /* Connection already active; do nothing */ return; } @@ -1605,9 +1607,8 @@ static void nma_menu_show_cb (GtkWidget *menu, NMApplet *applet) g_return_if_fail (menu != NULL); g_return_if_fail (applet != NULL); -#ifndef ENABLE_INDICATOR - gtk_status_icon_set_tooltip_text (applet->status_icon, NULL); -#endif + if (applet->status_icon) + gtk_status_icon_set_tooltip_text (applet->status_icon, NULL); if (!nm_client_get_nm_running (applet->nm_client)) { nma_menu_add_text_item (menu, _("NetworkManager is not running...")); @@ -1629,14 +1630,12 @@ static void nma_menu_show_cb (GtkWidget *menu, NMApplet *applet) nma_menu_add_create_network_item (menu, applet); } -#ifndef ENABLE_INDICATOR - gtk_widget_show_all (menu); -#endif + if (!INDICATOR_ENABLED (applet)) + gtk_widget_show_all (menu); // nmi_dbus_signal_user_interface_activated (applet->connection); } -#ifndef ENABLE_INDICATOR static gboolean destroy_old_menu (gpointer user_data) { @@ -1658,7 +1657,6 @@ nma_menu_deactivate_cb (GtkWidget *widget, NMApplet *applet) /* Re-set the tooltip */ gtk_status_icon_set_tooltip_text (applet->status_icon, applet->tip); } -#endif static gboolean is_permission_yes (NMApplet *applet, NMClientPermission perm) @@ -1998,31 +1996,32 @@ applet_add_default_connection_item (NMDevice *device, gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); } -#ifdef ENABLE_INDICATOR static gboolean applet_update_indicator_menu (gpointer user_data) { NMApplet *applet = NM_APPLET (user_data); - GtkWidget *menu = nma_context_menu_create (applet); +#ifdef WITH_APPINDICATOR + GtkWidget *menu; + menu = nma_context_menu_create (applet); nma_menu_show_cb (menu, applet); nma_menu_add_separator_item (menu); nma_context_menu_update (applet); app_indicator_set_menu (applet->app_indicator, GTK_MENU (menu)); +#endif /* WITH_APPINDICATOR */ applet->update_menu_id = 0; - return FALSE; + return G_SOURCE_REMOVE; } -#endif /* ENABLE_INDICATOR */ void applet_schedule_update_menu (NMApplet *applet) { -#ifdef ENABLE_INDICATOR - if (!applet->update_menu_id) - applet->update_menu_id = g_idle_add (applet_update_indicator_menu, applet); -#endif + if (INDICATOR_ENABLED (applet)) { + if (!applet->update_menu_id) + applet->update_menu_id = g_idle_add (applet_update_indicator_menu, applet); + } } /*****************************************************************************/ @@ -2030,22 +2029,24 @@ applet_schedule_update_menu (NMApplet *applet) static void foo_set_icon (NMApplet *applet, guint32 layer, GdkPixbuf *pixbuf, const char *icon_name) { -#ifndef ENABLE_INDICATOR gs_unref_object GdkPixbuf *pixbuf_free = NULL; -#endif g_return_if_fail (layer == ICON_LAYER_LINK || layer == ICON_LAYER_VPN); -#ifdef ENABLE_INDICATOR - /* FIXME: We rely on the fact that VPN icon gets drawn later and therefore - * wins but we cannot currently set a combined pixmap made of both the link - * icon and the VPN icon. - */ - if (icon_name == NULL && layer == ICON_LAYER_LINK) - icon_name = "nm-no-connection"; - if (icon_name != NULL && g_strcmp0 (app_indicator_get_icon (applet->app_indicator), icon_name) != 0) - app_indicator_set_icon_full (applet->app_indicator, icon_name, applet->tip); -#else +#ifdef WITH_APPINDICATOR + if (INDICATOR_ENABLED (applet)) { + /* FIXME: We rely on the fact that VPN icon gets drawn later and therefore + * wins but we cannot currently set a combined pixmap made of both the link + * icon and the VPN icon. + */ + if (icon_name == NULL && layer == ICON_LAYER_LINK) + icon_name = "nm-no-connection"; + if (icon_name != NULL && g_strcmp0 (app_indicator_get_icon (applet->app_indicator), icon_name) != 0) + app_indicator_set_icon_full (applet->app_indicator, icon_name, applet->tip); + return; + } +#endif /* WITH_APPINDICATOR */ + /* Load the pixbuf by icon name */ if (icon_name && !pixbuf) pixbuf = nma_icon_check_and_load (icon_name, applet); @@ -2082,7 +2083,6 @@ foo_set_icon (NMApplet *applet, guint32 layer, GdkPixbuf *pixbuf, const char *ic pixbuf = nma_icon_check_and_load ("nm-no-connection", applet); gtk_status_icon_set_from_pixbuf (applet->status_icon, pixbuf); -#endif } NMRemoteConnection * @@ -2230,16 +2230,12 @@ foo_client_state_changed_cb (NMClient *client, GParamSpec *pspec, gpointer user_ applet_schedule_update_menu (applet); } -#ifdef ENABLE_INDICATOR static void -foo_device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data) +foo_device_removed_cb (NMClient *client, NMDevice *device, NMApplet *applet) { - NMApplet *applet = NM_APPLET (user_data); - applet_schedule_update_icon (applet); applet_schedule_update_menu (applet); } -#endif static void foo_manager_running_cb (NMClient *client, @@ -2337,11 +2333,11 @@ foo_client_setup (NMApplet *applet) g_signal_connect (applet->nm_client, "device-added", G_CALLBACK (foo_device_added_cb), applet); -#ifdef ENABLE_INDICATOR - g_signal_connect (applet->nm_client, "device-removed", - G_CALLBACK (foo_device_removed_cb), - applet); -#endif + if (INDICATOR_ENABLED (applet)) { + g_signal_connect (applet->nm_client, "device-removed", + G_CALLBACK (foo_device_removed_cb), + applet); + } g_signal_connect (applet->nm_client, "notify::manager-running", G_CALLBACK (foo_manager_running_cb), applet); @@ -2598,11 +2594,14 @@ applet_update_icon (gpointer user_data) if (!nm_running) state = NM_STATE_UNKNOWN; -#ifdef ENABLE_INDICATOR - app_indicator_set_status (applet->app_indicator, nm_running ? APP_INDICATOR_STATUS_ACTIVE : APP_INDICATOR_STATUS_PASSIVE); -#else - gtk_status_icon_set_visible (applet->status_icon, applet->visible); -#endif +#ifdef WITH_APPINDICATOR + if (INDICATOR_ENABLED (applet)) + app_indicator_set_status (applet->app_indicator, nm_running ? APP_INDICATOR_STATUS_ACTIVE : APP_INDICATOR_STATUS_PASSIVE); + else +#endif /* WITH_APPINDICATOR */ + { + gtk_status_icon_set_visible (applet->status_icon, applet->visible); + } switch (state) { case NM_STATE_UNKNOWN: @@ -2667,13 +2666,8 @@ applet_update_icon (gpointer user_data) } else applet->tip = g_strdup (dev_tip); -#ifdef ENABLE_INDICATOR - /* FIXME: The applet->tip attribute seems to only be picked up by - * the next call to foo_set_icon() which is not particularly nice. - */ -#else - gtk_status_icon_set_tooltip_text (applet->status_icon, applet->tip); -#endif + if (applet->status_icon) + gtk_status_icon_set_tooltip_text (applet->status_icon, applet->tip); return FALSE; } @@ -3000,11 +2994,10 @@ static void nma_icons_init (NMApplet *applet) g_object_unref (G_OBJECT (applet->icon_theme)); } -#ifdef ENABLE_INDICATOR - applet->icon_theme = gtk_icon_theme_get_default (); -#else - applet->icon_theme = gtk_icon_theme_get_for_screen (gtk_status_icon_get_screen (applet->status_icon)); -#endif + if (applet->status_icon) + applet->icon_theme = gtk_icon_theme_get_for_screen (gtk_status_icon_get_screen (applet->status_icon)); + else + applet->icon_theme = gtk_icon_theme_get_default (); /* If not done yet, append our search path */ path_appended = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (applet->icon_theme), @@ -3021,7 +3014,6 @@ static void nma_icons_init (NMApplet *applet) nma_icons_reload (applet, NULL); } -#ifndef ENABLE_INDICATOR static void status_icon_screen_changed_cb (GtkStatusIcon *icon, GParamSpec *pspec, @@ -3097,49 +3089,44 @@ status_icon_popup_menu_cb (GtkStatusIcon *icon, gtk_status_icon_position_menu, icon, button, activate_time); } -#endif /* ENABLE_INDICATOR */ static gboolean setup_widgets (NMApplet *applet) { -#ifdef ENABLE_INDICATOR +#ifdef WITH_APPINDICATOR applet->app_indicator = app_indicator_new ("nm-applet", "nm-no-connection", APP_INDICATOR_CATEGORY_SYSTEM_SERVICES); + if (!applet->app_indicator) + return FALSE; app_indicator_set_title(applet->app_indicator, _("Network")); applet_schedule_update_menu (applet); -#else - applet->status_icon = gtk_status_icon_new (); - - if (shell_debug) - gtk_status_icon_set_name (applet->status_icon, "adsfasdfasdfadfasdf"); - - g_signal_connect (applet->status_icon, "notify::screen", - G_CALLBACK (status_icon_screen_changed_cb), applet); - g_signal_connect (applet->status_icon, "size-changed", - G_CALLBACK (status_icon_size_changed_cb), applet); - g_signal_connect (applet->status_icon, "activate", - G_CALLBACK (status_icon_activate_cb), applet); - g_signal_connect (applet->status_icon, "popup-menu", - G_CALLBACK (status_icon_popup_menu_cb), applet); - - applet->context_menu = nma_context_menu_create (applet); - if (!applet->context_menu) - return FALSE; -#endif +#endif /* WITH_APPINDICATOR */ - return TRUE; -} + /* Fall back to status icon if indicator isn't enabled or built */ + if (!INDICATOR_ENABLED (applet)) { + applet->status_icon = gtk_status_icon_new (); -#ifdef ENABLE_INDICATOR -static void -connection_added_cb (NMClient *client, NMRemoteConnection *connection, gpointer user_data) -{ - NMApplet *applet = NM_APPLET (user_data); + if (shell_debug) + gtk_status_icon_set_name (applet->status_icon, "adsfasdfasdfadfasdf"); - applet_schedule_update_menu (applet); + g_signal_connect (applet->status_icon, "notify::screen", + G_CALLBACK (status_icon_screen_changed_cb), applet); + g_signal_connect (applet->status_icon, "size-changed", + G_CALLBACK (status_icon_size_changed_cb), applet); + g_signal_connect (applet->status_icon, "activate", + G_CALLBACK (status_icon_activate_cb), applet); + g_signal_connect (applet->status_icon, "popup-menu", + G_CALLBACK (status_icon_popup_menu_cb), applet); + + applet->context_menu = nma_context_menu_create (applet); + if (!applet->context_menu) + return FALSE; + } + + return TRUE; } -#else + static void applet_embedded_cb (GObject *object, GParamSpec *pspec, gpointer user_data) { @@ -3148,7 +3135,6 @@ applet_embedded_cb (GObject *object, GParamSpec *pspec, gpointer user_data) g_debug ("applet now %s the notification area", embedded ? "embedded in" : "removed from"); } -#endif static void register_agent (NMApplet *applet) @@ -3172,12 +3158,13 @@ register_agent (NMApplet *applet) G_CALLBACK (applet_agent_get_secrets_cb), applet); g_signal_connect (applet->agent, APPLET_AGENT_CANCEL_SECRETS, G_CALLBACK (applet_agent_cancel_secrets_cb), applet); -#ifdef ENABLE_INDICATOR - /* Watch for new connections */ - g_signal_connect (applet->nm_client, NM_CLIENT_CONNECTION_ADDED, - G_CALLBACK (connection_added_cb), - applet); -#endif + + if (INDICATOR_ENABLED (applet)) { + /* Watch for new connections */ + g_signal_connect_swapped (applet->nm_client, NM_CLIENT_CONNECTION_ADDED, + G_CALLBACK (applet_schedule_update_menu), + applet); + } } static void @@ -3192,9 +3179,8 @@ applet_gsettings_show_changed (GSettings *settings, applet->visible = g_settings_get_boolean (settings, key); -#ifndef ENABLE_INDICATOR - gtk_status_icon_set_visible (applet->status_icon, applet->visible); -#endif + if (applet->status_icon) + gtk_status_icon_set_visible (applet->status_icon, applet->visible); } /****************************************************************/ @@ -3235,6 +3221,8 @@ applet_startup (GApplication *app, gpointer user_data) g_application_quit (app); return; } + g_assert (INDICATOR_ENABLED (applet) || applet->status_icon); + applet->icon_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, @@ -3263,14 +3251,14 @@ applet_startup (GApplication *app, gpointer user_data) mm1_client_setup (applet); #endif -#ifndef ENABLE_INDICATOR - /* Track embedding to help debug issues where user has removed the - * notification area applet from the panel, and thus nm-applet too. - */ - g_signal_connect (applet->status_icon, "notify::embedded", - G_CALLBACK (applet_embedded_cb), NULL); - applet_embedded_cb (G_OBJECT (applet->status_icon), NULL, NULL); -#endif + if (applet->status_icon) { + /* Track embedding to help debug issues where user has removed the + * notification area applet from the panel, and thus nm-applet too. + */ + g_signal_connect (applet->status_icon, "notify::embedded", + G_CALLBACK (applet_embedded_cb), NULL); + applet_embedded_cb (G_OBJECT (applet->status_icon), NULL, NULL); + } if (with_agent) register_agent (applet); @@ -3292,18 +3280,17 @@ static void finalize (GObject *object) if (applet->update_icon_id) g_source_remove (applet->update_icon_id); -#ifdef ENABLE_INDICATOR +#ifdef WITH_APPINDICATOR g_clear_object (&applet->app_indicator); - if (applet->update_menu_id) - g_source_remove (applet->update_menu_id); -#else +#endif /* WITH_APPINDICATOR */ + nm_clear_g_source (&applet->update_menu_id); + g_clear_object (&applet->status_icon); g_clear_object (&applet->menu); g_clear_pointer (&applet->icon_cache, g_hash_table_destroy); g_clear_object (&applet->fallback_icon); g_free (applet->tip); nma_icons_free (applet); -#endif while (g_slist_length (applet->secrets_reqs)) applet_secrets_request_free ((SecretsRequest *) applet->secrets_reqs->data); |