[frogr] Activate/remove accounts by username, not by ID



commit a57dee524ef6f2bee488b3ffa512df55a5eab36b
Author: Mario Sanchez Prada <msanchez gnome org>
Date:   Mon Nov 26 12:09:11 2012 +0100

    Activate/remove accounts by username, not by ID

 src/frogr-config.c     |   31 +++++-----
 src/frogr-config.h     |    4 +-
 src/frogr-controller.c |  161 ++++++++++++++++++++++++++---------------------
 src/frogr-controller.h |    3 +-
 4 files changed, 108 insertions(+), 91 deletions(-)
---
diff --git a/src/frogr-config.c b/src/frogr-config.c
index 80d2ad7..deeb10d 100644
--- a/src/frogr-config.c
+++ b/src/frogr-config.c
@@ -84,7 +84,8 @@ static FrogrConfig *_instance = NULL;
 
 /* Prototypes */
 
-static FrogrAccount *_find_account_by_id (FrogrConfig *self, const gchar *id);
+static FrogrAccount *_find_account_by_username (FrogrConfig *self,
+                                                const gchar *username);
 
 static void _load_settings (FrogrConfig *self);
 
@@ -127,20 +128,20 @@ static xmlNodePtr _xml_add_string_child (xmlNodePtr   parent,
 /* Private functions */
 
 static FrogrAccount *
-_find_account_by_id (FrogrConfig *self, const gchar *id)
+_find_account_by_username (FrogrConfig *self, const gchar *username)
 {
   FrogrConfigPrivate *priv = NULL;
   FrogrAccount *current = NULL;
   GSList *item = NULL;
 
   g_return_val_if_fail (FROGR_IS_CONFIG (self), NULL);
-  g_return_val_if_fail (id != NULL, NULL);
+  g_return_val_if_fail (username != NULL, NULL);
 
   priv = FROGR_CONFIG_GET_PRIVATE (self);
   for (item = priv->accounts; item; item = g_slist_next (item))
     {
       current = FROGR_ACCOUNT (item->data);
-      if (!g_strcmp0 (id, frogr_account_get_id (current)))
+      if (!g_strcmp0 (username, frogr_account_get_username (current)))
         return current;
     }
 
@@ -1042,7 +1043,7 @@ frogr_config_add_account (FrogrConfig  *self,
 {
   FrogrConfigPrivate *priv = NULL;
   FrogrAccount *found_account = NULL;
-  const gchar *account_id = NULL;
+  const gchar *username = NULL;
 
   g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
   g_return_val_if_fail (FROGR_IS_ACCOUNT (faccount), FALSE);
@@ -1050,21 +1051,21 @@ frogr_config_add_account (FrogrConfig  *self,
   priv = FROGR_CONFIG_GET_PRIVATE (self);
 
   /* Only add the account if not already in */
-  account_id = frogr_account_get_id (faccount);
-  found_account = _find_account_by_id (self, account_id);
+  username = frogr_account_get_username (faccount);
+  found_account = _find_account_by_username (self, username);
 
   /* Remove old account if found */
   if (found_account)
     {
-      frogr_config_remove_account (self, account_id);
-      DEBUG ("Account of ID %s already in the configuration system", account_id);
+      frogr_config_remove_account (self, username);
+      DEBUG ("Account %s already in the configuration system", username);
     }
 
   priv->accounts = g_slist_append (priv->accounts, g_object_ref (faccount));
 
   /* Set it as active if needed */
   if (frogr_account_is_active (faccount))
-    frogr_config_set_active_account (self, account_id);
+    frogr_config_set_active_account (self, username);
 
   /* Return TRUE if a new account was actually added */
   return !found_account;
@@ -1082,7 +1083,7 @@ frogr_config_get_accounts (FrogrConfig *self)
 }
 
 gboolean
-frogr_config_set_active_account (FrogrConfig *self, const gchar *id)
+frogr_config_set_active_account (FrogrConfig *self, const gchar *username)
 {
   FrogrConfigPrivate *priv = NULL;
   FrogrAccount *current = NULL;
@@ -1096,7 +1097,7 @@ frogr_config_set_active_account (FrogrConfig *self, const gchar *id)
     {
       current = FROGR_ACCOUNT (item->data);
 
-      if (!g_strcmp0 (id, frogr_account_get_id (current)))
+      if (!g_strcmp0 (username, frogr_account_get_username (current)))
         {
           frogr_account_set_is_active (current, TRUE);
           priv->active_account = current;
@@ -1121,16 +1122,16 @@ frogr_config_get_active_account (FrogrConfig *self)
 }
 
 gboolean
-frogr_config_remove_account (FrogrConfig *self, const gchar *id)
+frogr_config_remove_account (FrogrConfig *self, const gchar *username)
 {
   FrogrConfigPrivate *priv = NULL;
   FrogrAccount *found_account = NULL;
 
   g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
-  g_return_val_if_fail (id != NULL, FALSE);
+  g_return_val_if_fail (username != NULL, FALSE);
 
   priv = FROGR_CONFIG_GET_PRIVATE (self);
-  found_account = _find_account_by_id (self, id);
+  found_account = _find_account_by_username (self, username);
 
   if (found_account)
     {
diff --git a/src/frogr-config.h b/src/frogr-config.h
index fc9d999..c257acb 100644
--- a/src/frogr-config.h
+++ b/src/frogr-config.h
@@ -79,11 +79,11 @@ gboolean frogr_config_add_account (FrogrConfig  *self,
 
 GSList *frogr_config_get_accounts (FrogrConfig *self);
 
-gboolean frogr_config_set_active_account (FrogrConfig *self, const gchar *id);
+gboolean frogr_config_set_active_account (FrogrConfig *self, const gchar *username);
 
 FrogrAccount *frogr_config_get_active_account (FrogrConfig *self);
 
-gboolean frogr_config_remove_account (FrogrConfig *self, const gchar *id);
+gboolean frogr_config_remove_account (FrogrConfig *self, const gchar *username);
 
 void frogr_config_set_default_public (FrogrConfig *self, gboolean value);
 
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index ef6c4bf..f7dd956 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -151,6 +151,8 @@ static void _g_application_open_files_cb (GApplication *app, GFile **files, gint
 
 static void _g_application_shutdown_cb (GApplication *app, gpointer data);
 
+static void _set_active_account (FrogrController *self, FrogrAccount *account);
+
 static void _set_state (FrogrController *self, FrogrControllerState state);
 
 static GCancellable *_register_new_cancellable (FrogrController *self);
@@ -299,7 +301,7 @@ _g_application_startup_cb (GApplication *app, gpointer data)
   /* Select the right account */
   account = frogr_config_get_active_account (priv->config);
   if (account)
-    frogr_controller_set_active_account (self, account);
+    _set_active_account (self, account);
 }
 
 static void
@@ -359,6 +361,83 @@ _g_application_shutdown_cb (GApplication *app, gpointer data)
     }
 }
 
+static void
+_set_active_account (FrogrController *self, FrogrAccount *account)
+{
+  FrogrControllerPrivate *priv = NULL;
+  FrogrAccount *new_account = NULL;
+  gboolean accounts_changed = FALSE;
+  const gchar *token = NULL;
+  const gchar *token_secret = NULL;
+  const gchar *account_version = NULL;
+
+  g_return_if_fail(FROGR_IS_CONTROLLER (self));
+
+  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+
+  new_account = FROGR_IS_ACCOUNT (account) ? g_object_ref (account) : NULL;
+  if (new_account)
+    {
+      const gchar *new_username = NULL;
+
+      new_username = frogr_account_get_username (new_account);
+      if (!frogr_config_set_active_account (priv->config, new_username))
+        {
+          /* 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);
+      token_secret = frogr_account_get_token_secret (new_account);
+    }
+  else if (FROGR_IS_ACCOUNT (priv->account))
+    {
+      /* If NULL is passed it means 'delete current account' */
+      const gchar *username = frogr_account_get_username (priv->account);
+      accounts_changed = frogr_config_remove_account (priv->config, username);
+    }
+
+  /* Update internal pointer in the controller */
+  if (priv->account)
+    g_object_unref (priv->account);
+  priv->account = new_account;
+
+  /* Update token in the session */
+  fsp_session_set_token (priv->session, token);
+  fsp_session_set_token_secret (priv->session, token_secret);
+
+  /* Fetch needed info for this account or update tokens stored */
+  account_version = new_account ? frogr_account_get_version (new_account) : NULL;
+  if (account_version && g_strcmp0 (account_version, ACCOUNTS_CURRENT_VERSION))
+    {
+      /* We won't use a cancellable for this request */
+      _clear_cancellable (self);
+
+      priv->fetching_token_replacement = TRUE;
+      fsp_session_exchange_token (priv->session, NULL, _exchange_token_cb, self);
+      gdk_threads_add_timeout (DEFAULT_TIMEOUT, (GSourceFunc) _show_progress_on_idle, GINT_TO_POINTER (FETCHING_TOKEN_REPLACEMENT));
+
+      /* Make sure we show proper feedback if connection is too slow */
+      gdk_threads_add_timeout (MAX_AUTH_TIMEOUT, (GSourceFunc) _cancel_authorization_on_timeout, self);
+    }
+  else
+    {
+      /* If a new account has been activated, fetch everything */
+      if (new_account)
+        _fetch_everything (self, TRUE);
+
+      /* Emit proper signals */
+      g_signal_emit (self, signals[ACTIVE_ACCOUNT_CHANGED], 0, new_account);
+      if (accounts_changed)
+        g_signal_emit (self, signals[ACCOUNTS_CHANGED], 0);
+    }
+
+  /* Save new state in configuration */
+  frogr_config_save_accounts (priv->config);
+}
+
 void
 _set_state (FrogrController *self, FrogrControllerState state)
 {
@@ -628,7 +707,7 @@ _complete_auth_cb (GObject *object, GAsyncResult *result, gpointer data)
           frogr_account_set_permissions (account, "write");
 
           /* Try to set the active account again */
-          frogr_controller_set_active_account (controller, account);
+          _set_active_account (controller, account);
 
           DEBUG ("%s", "Authorization successfully completed!");
         }
@@ -677,7 +756,7 @@ _exchange_token_cb (GObject *object, GAsyncResult *result, gpointer data)
       frogr_account_set_version (priv->account, ACCOUNTS_CURRENT_VERSION);
 
       /* Finally, try to set the active account again */
-      frogr_controller_set_active_account (controller, priv->account);
+      _set_active_account (controller, priv->account);
     }
   else
     {
@@ -2301,81 +2380,19 @@ frogr_controller_get_model (FrogrController *self)
 }
 
 void
-frogr_controller_set_active_account (FrogrController *self,
-                                     FrogrAccount *account)
+frogr_controller_set_active_account (FrogrController *self, const gchar *username)
 {
   FrogrControllerPrivate *priv = NULL;
-  FrogrAccount *new_account = NULL;
-  gboolean accounts_changed = FALSE;
-  const gchar *token = NULL;
-  const gchar *token_secret = NULL;
-  const gchar *account_version = NULL;
+  FrogrAccount *account = NULL;
 
   g_return_if_fail(FROGR_IS_CONTROLLER (self));
+  g_return_if_fail(username);
 
   priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  frogr_config_set_active_account (priv->config, username);
+  account = frogr_config_get_active_account (priv->config);
 
-  new_account = FROGR_IS_ACCOUNT (account) ? g_object_ref (account) : NULL;
-  if (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);
-      token_secret = frogr_account_get_token_secret (new_account);
-    }
-  else if (FROGR_IS_ACCOUNT (priv->account))
-    {
-      /* If NULL is passed it means 'delete current account' */
-      const gchar *account_id = frogr_account_get_id (priv->account);
-      accounts_changed = frogr_config_remove_account (priv->config, account_id);
-    }
-
-  /* Update internal pointer in the controller */
-  if (priv->account)
-    g_object_unref (priv->account);
-  priv->account = new_account;
-
-  /* Update token in the session */
-  fsp_session_set_token (priv->session, token);
-  fsp_session_set_token_secret (priv->session, token_secret);
-
-  /* Fetch needed info for this account or update tokens stored */
-  account_version = new_account ? frogr_account_get_version (new_account) : NULL;
-  if (account_version && g_strcmp0 (account_version, ACCOUNTS_CURRENT_VERSION))
-    {
-      /* We won't use a cancellable for this request */
-      _clear_cancellable (self);
-
-      priv->fetching_token_replacement = TRUE;
-      fsp_session_exchange_token (priv->session, NULL, _exchange_token_cb, self);
-      gdk_threads_add_timeout (DEFAULT_TIMEOUT, (GSourceFunc) _show_progress_on_idle, GINT_TO_POINTER (FETCHING_TOKEN_REPLACEMENT));
-
-      /* Make sure we show proper feedback if connection is too slow */
-      gdk_threads_add_timeout (MAX_AUTH_TIMEOUT, (GSourceFunc) _cancel_authorization_on_timeout, self);
-    }
-  else
-    {
-      /* If a new account has been activated, fetch everything */
-      if (new_account)
-        _fetch_everything (self, TRUE);
-
-      /* Emit proper signals */
-      g_signal_emit (self, signals[ACTIVE_ACCOUNT_CHANGED], 0, new_account);
-      if (accounts_changed)
-        g_signal_emit (self, signals[ACCOUNTS_CHANGED], 0);
-    }
-
-  /* Save new state in configuration */
-  frogr_config_save_accounts (priv->config);
+  _set_active_account (self, account);
 }
 
 FrogrAccount *
@@ -2694,7 +2711,7 @@ frogr_controller_revoke_authorization (FrogrController *self)
   /* Ensure there's the token/account is no longer active anywhere */
   fsp_session_set_token (priv->session, NULL);
   fsp_session_set_token_secret (priv->session, NULL);
-  frogr_controller_set_active_account (self, NULL);
+  _set_active_account (self, NULL);
 }
 
 void
diff --git a/src/frogr-controller.h b/src/frogr-controller.h
index 58944af..e265989 100644
--- a/src/frogr-controller.h
+++ b/src/frogr-controller.h
@@ -71,8 +71,7 @@ FrogrMainView *frogr_controller_get_main_view (FrogrController *self);
 
 FrogrModel *frogr_controller_get_model (FrogrController *self);
 
-void frogr_controller_set_active_account (FrogrController *self,
-                                          FrogrAccount *account);
+void frogr_controller_set_active_account (FrogrController *self, const gchar *username);
 
 FrogrAccount *frogr_controller_get_active_account (FrogrController *self);
 



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