[gnome-online-accounts] Factor out the code for checking duplicate accounts into a function



commit 3f43b71232f82bb7c574045e51003cdc694ae70e
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Apr 18 14:01:49 2012 +0200

    Factor out the code for checking duplicate accounts into a function

 po/POTFILES.in                     |    1 +
 src/goabackend/Makefile.am         |    1 +
 src/goabackend/goaoauth2provider.c |   40 +++---------------
 src/goabackend/goaoauthprovider.c  |   40 +++---------------
 src/goabackend/goautils.c          |   77 ++++++++++++++++++++++++++++++++++++
 src/goabackend/goautils.h          |   45 +++++++++++++++++++++
 6 files changed, 138 insertions(+), 66 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index efd38d0..4c6cc2d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -11,5 +11,6 @@ src/goabackend/goaoauth2provider.c
 src/goabackend/goaoauthprovider.c
 src/goabackend/goaprovider.c
 src/goabackend/goatwitterprovider.c
+src/goabackend/goautils.c
 src/goabackend/goawebview.c
 src/goabackend/goayahooprovider.c
diff --git a/src/goabackend/Makefile.am b/src/goabackend/Makefile.am
index 0221b4b..ed03355 100644
--- a/src/goabackend/Makefile.am
+++ b/src/goabackend/Makefile.am
@@ -76,6 +76,7 @@ libgoa_backend_1_0_la_SOURCES =						\
 	goayahooprovider.h		goayahooprovider.c		\
 	goatwitterprovider.h		goatwitterprovider.c		\
 	goawindowsliveprovider.h	goawindowsliveprovider.c	\
+	goautils.h			goautils.c			\
 	goawebview.h			goawebview.c			\
 	nautilus-floating-bar.h		nautilus-floating-bar.c		\
 	$(NULL)
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index 8dbbc0f..663fbf8 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -32,6 +32,7 @@
 
 #include "goalogging.h"
 #include "goaprovider.h"
+#include "goautils.h"
 #include "goawebview.h"
 #include "goaoauth2provider.h"
 
@@ -995,8 +996,6 @@ goa_oauth2_provider_add_account (GoaProvider *_provider,
   gchar *refresh_token;
   gchar *identity;
   gchar *presentation_identity;
-  GList *accounts;
-  GList *l;
   AddData data;
   GVariantBuilder builder;
 
@@ -1012,7 +1011,6 @@ goa_oauth2_provider_add_account (GoaProvider *_provider,
   refresh_token = NULL;
   identity = NULL;
   presentation_identity = NULL;
-  accounts = NULL;
 
   memset (&data, '\0', sizeof (AddData));
   data.loop = g_main_loop_new (NULL, FALSE);
@@ -1032,34 +1030,12 @@ goa_oauth2_provider_add_account (GoaProvider *_provider,
   /* OK, got the identity... see if there's already an account
    * of this type with the given identity
    */
-  accounts = goa_client_get_accounts (client);
-  for (l = accounts; l != NULL; l = l->next)
-    {
-      GoaObject *object = GOA_OBJECT (l->data);
-      GoaAccount *account;
-      GoaOAuth2Based *oauth2_based;
-      const gchar *identity_from_object;
-
-      account = goa_object_peek_account (object);
-      oauth2_based = goa_object_peek_oauth2_based (object);
-      if (oauth2_based == NULL)
-        continue;
-
-      if (g_strcmp0 (goa_account_get_provider_type (account),
-                     goa_provider_get_provider_type (GOA_PROVIDER (provider))) != 0)
-        continue;
-
-      identity_from_object = goa_account_get_identity (account);
-      if (g_strcmp0 (identity_from_object, identity) == 0)
-        {
-          g_set_error (&data.error,
-                       GOA_ERROR,
-                       GOA_ERROR_ACCOUNT_EXISTS,
-                       _("There is already an account for the identity %s"),
-                       identity);
-          goto out;
-        }
-    }
+  if (!goa_utils_check_duplicate (client,
+                                  identity,
+                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                  (GoaPeekInterfaceFunc) goa_object_peek_oauth2_based,
+                                  &data.error))
+    goto out;
 
   /* we want the GoaClient to update before this method returns (so it
    * can create a proxy for the new object) so run the mainloop while
@@ -1108,8 +1084,6 @@ goa_oauth2_provider_add_account (GoaProvider *_provider,
   else
     g_assert (ret != NULL);
 
-  g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
-  g_list_free (accounts);
   g_free (identity);
   g_free (presentation_identity);
   g_free (refresh_token);
diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c
index b99038f..2f8e4dd 100644
--- a/src/goabackend/goaoauthprovider.c
+++ b/src/goabackend/goaoauthprovider.c
@@ -31,6 +31,7 @@
 
 #include "goalogging.h"
 #include "goaprovider.h"
+#include "goautils.h"
 #include "goawebview.h"
 #include "goaoauthprovider.h"
 
@@ -996,8 +997,6 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
   gint session_handle_expires_in;
   gchar *identity;
   gchar *presentation_identity;
-  GList *accounts;
-  GList *l;
   AddData data;
   GVariantBuilder builder;
 
@@ -1013,7 +1012,6 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
   session_handle = NULL;
   identity = NULL;
   presentation_identity = NULL;
-  accounts = NULL;
 
   memset (&data, '\0', sizeof (AddData));
   data.loop = g_main_loop_new (NULL, FALSE);
@@ -1034,34 +1032,12 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
   /* OK, got the identity... see if there's already an account
    * of this type with the given identity
    */
-  accounts = goa_client_get_accounts (client);
-  for (l = accounts; l != NULL; l = l->next)
-    {
-      GoaObject *object = GOA_OBJECT (l->data);
-      GoaAccount *account;
-      GoaOAuthBased *oauth_based;
-      const gchar *identity_from_object;
-
-      account = goa_object_peek_account (object);
-      oauth_based = goa_object_peek_oauth_based (object);
-      if (oauth_based == NULL)
-        continue;
-
-      if (g_strcmp0 (goa_account_get_provider_type (account),
-                     goa_provider_get_provider_type (GOA_PROVIDER (provider))) != 0)
-        continue;
-
-      identity_from_object = goa_account_get_identity (account);
-      if (g_strcmp0 (identity_from_object, identity) == 0)
-        {
-          g_set_error (&data.error,
-                       GOA_ERROR,
-                       GOA_ERROR_ACCOUNT_EXISTS,
-                       _("There is already an account for the identity %s"),
-                       identity);
-          goto out;
-        }
-    }
+  if (!goa_utils_check_duplicate (client,
+                                  identity,
+                                  goa_provider_get_provider_type (GOA_PROVIDER (provider)),
+                                  (GoaPeekInterfaceFunc) goa_object_peek_oauth_based,
+                                  &data.error))
+    goto out;
 
   /* we want the GoaClient to update before this method returns (so it
    * can create a proxy for the new object) so run the mainloop while
@@ -1112,8 +1088,6 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
   else
     g_assert (ret != NULL);
 
-  g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
-  g_list_free (accounts);
   g_free (identity);
   g_free (presentation_identity);
   g_free (access_token);
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
new file mode 100644
index 0000000..80610c7
--- /dev/null
+++ b/src/goabackend/goautils.c
@@ -0,0 +1,77 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Debarshi Ray <debarshir gnome org>
+ */
+
+#include "config.h"
+
+#include <glib/gi18n-lib.h>
+
+#include "goautils.h"
+
+gboolean
+goa_utils_check_duplicate (GoaClient              *client,
+                           const gchar            *identity,
+                           const gchar            *provider_type,
+                           GoaPeekInterfaceFunc    func,
+                           GError                **error)
+{
+  GList *accounts;
+  GList *l;
+  gboolean ret;
+
+  ret = FALSE;
+
+  accounts = goa_client_get_accounts (client);
+  for (l = accounts; l != NULL; l = l->next)
+    {
+      GoaObject *object = GOA_OBJECT (l->data);
+      GoaAccount *account;
+      gpointer *interface;
+      const gchar *identity_from_object;
+      const gchar *provider_type_from_object;
+
+      account = goa_object_peek_account (object);
+      interface = (*func) (object);
+      if (interface == NULL)
+        continue;
+
+      provider_type_from_object = goa_account_get_provider_type (account);
+      if (g_strcmp0 (provider_type_from_object, provider_type) != 0)
+        continue;
+
+      identity_from_object = goa_account_get_identity (account);
+      if (g_strcmp0 (identity_from_object, identity) == 0)
+        {
+          g_set_error (error,
+                       GOA_ERROR,
+                       GOA_ERROR_ACCOUNT_EXISTS,
+                       _("There is already an account for the identity %s"),
+                       identity);
+          goto out;
+        }
+    }
+
+  ret = TRUE;
+
+ out:
+  g_list_free_full (accounts, g_object_unref);
+  return ret;
+}
diff --git a/src/goabackend/goautils.h b/src/goabackend/goautils.h
new file mode 100644
index 0000000..6e5dc8d
--- /dev/null
+++ b/src/goabackend/goautils.h
@@ -0,0 +1,45 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Debarshi Ray <debarshir gnome org>
+ */
+
+#if !defined (__GOA_BACKEND_INSIDE_GOA_BACKEND_H__) && !defined (GOA_BACKEND_COMPILATION)
+#error "Only <goabackend/goabackend.h> can be included directly."
+#endif
+
+#ifndef __GOA_UTILS_H__
+#define __GOA_UTILS_H__
+
+#include <glib.h>
+#include <goabackend/goabackendtypes.h>
+
+G_BEGIN_DECLS
+
+typedef gpointer (*GoaPeekInterfaceFunc)   (GoaObject *);
+
+gboolean         goa_utils_check_duplicate (GoaClient              *client,
+                                            const gchar            *identity,
+                                            const gchar            *provider_type,
+                                            GoaPeekInterfaceFunc    func,
+                                            GError                **error);
+
+G_END_DECLS
+
+#endif /* __GOA_UTILS_H__ */



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