[frogr] Use check menu items for the account items



commit a7b95f04b8b28c166563b39ee17d6778acac15e3
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Mon May 2 00:59:17 2011 +0200

    Use check menu items for the account items

 src/frogr-config.c     |   13 ++++++--
 src/frogr-config.h     |    2 +-
 src/frogr-controller.c |   13 ++++++--
 src/frogr-main-view.c  |   73 +++++++++++++++++++++++++++++++++++------------
 4 files changed, 74 insertions(+), 27 deletions(-)
---
diff --git a/src/frogr-config.c b/src/frogr-config.c
index f0dcf8e..2c64549 100644
--- a/src/frogr-config.c
+++ b/src/frogr-config.c
@@ -964,14 +964,15 @@ frogr_config_get_accounts (FrogrConfig *self)
   return priv->accounts;
 }
 
-void
+gboolean
 frogr_config_set_active_account (FrogrConfig *self, const gchar *id)
 {
-  g_return_if_fail (FROGR_IS_CONFIG (self));
+  g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
 
   FrogrConfigPrivate *priv = FROGR_CONFIG_GET_PRIVATE (self);
   FrogrAccount *current = NULL;
   GSList *item = NULL;
+  gboolean result = FALSE;
 
   priv = FROGR_CONFIG_GET_PRIVATE (self);
   for (item = priv->accounts; item; item = g_slist_next (item))
@@ -979,12 +980,16 @@ frogr_config_set_active_account (FrogrConfig *self, const gchar *id)
       current = FROGR_ACCOUNT (item->data);
 
       if (!g_strcmp0 (id, frogr_account_get_id (current)))
-        frogr_account_set_is_active (current, TRUE);
+        {
+          frogr_account_set_is_active (current, TRUE);
+          priv->active_account = current;
+          result = TRUE;
+        }
       else
         frogr_account_set_is_active (current, FALSE);
     }
 
-  priv->active_account = current;
+  return result;
 }
 
 FrogrAccount *
diff --git a/src/frogr-config.h b/src/frogr-config.h
index ef79a2b..fa89b7e 100644
--- a/src/frogr-config.h
+++ b/src/frogr-config.h
@@ -76,7 +76,7 @@ gboolean frogr_config_add_account (FrogrConfig  *self,
 
 GSList *frogr_config_get_accounts (FrogrConfig *self);
 
-void frogr_config_set_active_account (FrogrConfig *self, const gchar *id);
+gboolean frogr_config_set_active_account (FrogrConfig *self, const gchar *id);
 
 FrogrAccount *frogr_config_get_active_account (FrogrConfig *self);
 
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index 6cada5f..2158590 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -1858,8 +1858,15 @@ frogr_controller_set_active_account (FrogrController *self,
   new_account = FROGR_IS_ACCOUNT (account) ? g_object_ref (account) : NULL;
   if (new_account)
     {
-      frogr_account_set_is_active (new_account, TRUE);
-      accounts_changed = frogr_config_add_account (priv->config, new_account);
+      const gchar *new_account_id = NULL;
+
+      new_account_id = frogr_account_get_id (new_account);
+      if (!frogr_config_set_active_account (priv->config, new_account_id))
+        {
+          /* Fallback to manually creating a new account */
+          frogr_account_set_is_active (new_account, TRUE);
+          accounts_changed = frogr_config_add_account (priv->config, new_account);
+        }
 
       /* Get the token for setting it later on */
       token = frogr_account_get_token (new_account);
@@ -1875,7 +1882,7 @@ frogr_controller_set_active_account (FrogrController *self,
   if (priv->account)
     g_object_unref (priv->account);
 
-  priv->account = new_account;
+  priv->account = g_object_ref (new_account);
   fsp_session_set_token (priv->session, token);
 
   /* Prefetch info for this user */
diff --git a/src/frogr-main-view.c b/src/frogr-main-view.c
index 95b966d..7f81eca 100644
--- a/src/frogr-main-view.c
+++ b/src/frogr-main-view.c
@@ -165,7 +165,7 @@ gboolean _on_icon_view_button_press_event (GtkWidget *widget,
                                            GdkEventButton *event,
                                            gpointer data);
 
-void _on_account_menu_item_activate (GtkWidget *widget, gpointer self);
+void _on_account_menu_item_toggled (GtkWidget *widget, gpointer self);
 void _on_menu_item_activate (GtkWidget *widget, gpointer self);
 void _on_check_menu_item_toggled (GtkCheckMenuItem *item, gpointer self);
 
@@ -236,6 +236,8 @@ static void _model_pictures_reordered (FrogrController *controller,
 static void _model_description_updated (FrogrController *controller,
                                         gpointer data);
 
+static void _update_account_menu_items (FrogrMainView *mainview);
+
 static void _update_state_description (FrogrMainView *mainview);
 
 static gchar *_craft_state_description (FrogrMainView *mainview);
@@ -423,12 +425,13 @@ _populate_accounts_submenu (FrogrMainView *self)
       else
         account_str = g_strdup_printf ("%s (%s)", username, fullname);
 
-      menu_item = gtk_menu_item_new_with_label (account_str);
+      menu_item = gtk_check_menu_item_new_with_label (account_str);
       g_free (account_str);
 
       g_object_set_data (G_OBJECT (menu_item), "frogr-account", account);
+
       g_signal_connect (G_OBJECT (menu_item), "activate",
-                        G_CALLBACK (_on_account_menu_item_activate),
+                        G_CALLBACK (_on_account_menu_item_toggled),
                         self);
       gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_menu), menu_item); 
     }
@@ -772,20 +775,31 @@ _on_icon_view_button_press_event (GtkWidget *widget,
 }
 
 void
-_on_account_menu_item_activate (GtkWidget *widget, gpointer self)
+_on_account_menu_item_toggled (GtkWidget *widget, gpointer self)
 {
   FrogrMainViewPrivate *priv = NULL;
   FrogrAccount *account = NULL;
+  gboolean checked = FALSE;
 
   priv = FROGR_MAIN_VIEW_GET_PRIVATE (self);
+  checked = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
   account = g_object_get_data (G_OBJECT (widget), "frogr-account");
 
-  if (account && FROGR_IS_ACCOUNT (account))
+  /* Only set the account if checked */
+  if (checked && account)
     {
-      frogr_controller_set_active_account (priv->controller, account);
       DEBUG ("Selected account %s (%s)",
              frogr_account_get_id (account),
              frogr_account_get_username (account));
+
+      frogr_controller_set_active_account (priv->controller, account);
+    }
+  else if (account)
+    {
+      /* If manually unchecked the currently active account, set it again */
+      FrogrAccount *active_account = frogr_controller_get_active_account (priv->controller);
+      if (frogr_account_equal (active_account, account))
+        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget), TRUE);
     }
 }
 
@@ -802,19 +816,6 @@ _on_menu_item_activate (GtkWidget *widget, gpointer self)
     _add_pictures_dialog (mainview);
   else if (widget == priv->remove_menu_item || widget == priv->remove_ctxt_menu_item)
     _remove_selected_pictures (mainview);
-  else if (widget == priv->accounts_menu_item)
-    {
-      FrogrAccount *account = NULL;
-
-      account = g_object_get_data (G_OBJECT (widget), "frogr-account");
-      if (account && FROGR_IS_ACCOUNT (account))
-        {
-          frogr_controller_set_active_account (priv->controller, account);
-          DEBUG ("Selected account %s (%s)",
-                 frogr_account_get_id (account),
-                 frogr_account_get_username (account));
-        }
-    }
   else if (widget == priv->auth_menu_item)
     frogr_controller_show_auth_dialog (priv->controller);
   else if (widget == priv->settings_menu_item)
@@ -1374,6 +1375,7 @@ _controller_active_account_changed (FrogrController *controller,
 
   mainview = FROGR_MAIN_VIEW (data);
   _update_state_description (mainview);
+  _update_account_menu_items (mainview);
   _update_ui (mainview);
 }
 
@@ -1444,6 +1446,39 @@ _model_description_updated (FrogrController *controller,
 }
 
 static void
+_update_account_menu_items (FrogrMainView *mainview)
+{
+  FrogrMainViewPrivate *priv = NULL;
+
+  priv = FROGR_MAIN_VIEW_GET_PRIVATE (mainview);
+  if (priv->accounts_menu && GTK_IS_CONTAINER (priv->accounts_menu))
+    {
+      FrogrAccount *active_account = NULL;
+      FrogrAccount *account = NULL;
+      GList *all_items = NULL;
+      GList *item = NULL;
+      GtkWidget *menu_item = NULL;
+
+      active_account = frogr_controller_get_active_account (priv->controller);
+      all_items = gtk_container_get_children (GTK_CONTAINER (priv->accounts_menu));
+      for (item = all_items; item; item = g_list_next (item))
+        {
+          gboolean value;
+
+          menu_item = GTK_WIDGET (item->data);
+          account = g_object_get_data (G_OBJECT (menu_item), "frogr-account"); 
+
+          if (account == active_account)
+            value = TRUE;
+          else
+            value = FALSE;
+
+          gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item), value);
+        }
+    }
+}
+
+static void
 _update_state_description (FrogrMainView *mainview)
 {
   FrogrMainViewPrivate *priv = NULL;



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