]> rtime.felk.cvut.cz Git - sojka/lightdm.git/commitdiff
move Accounts class into User class
authorMichael Terry <michael.terry@canonical.com>
Thu, 18 Aug 2011 16:07:20 +0000 (12:07 -0400)
committerMichael Terry <michael.terry@canonical.com>
Thu, 18 Aug 2011 16:07:20 +0000 (12:07 -0400)
src/Makefile.am
src/accounts.c [deleted file]
src/accounts.h [deleted file]
src/display.c
src/user.c
src/user.h

index bc0ddf59fc87b5e81f6632ba2d5485204dfb574c..c2143b74c0308f064de7d881997d6ac46326fb27 100644 (file)
@@ -7,8 +7,6 @@ ldm-marshal.h: ldm-marshal.list
        @GLIB_GENMARSHAL@ --prefix=ldm_marshal $(srcdir)/ldm-marshal.list --header > ldm-marshal.h
 
 lightdm_SOURCES = \
-       accounts.c \
-       accounts.h \
        configuration.c \
        configuration.h \
        display.c \
diff --git a/src/accounts.c b/src/accounts.c
deleted file mode 100644 (file)
index 87fb9f2..0000000
+++ /dev/null
@@ -1,260 +0,0 @@
-/* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
- *
- * Copyright (C) 2011 Canonical Ltd
- * Author: Michael Terry <michael.terry@canonical.com>
- * 
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
- * license.
- */
-
-#include "accounts.h"
-#include "dmrc.h"
-#include <gio/gio.h>
-
-struct AccountsPrivate
-{
-    gchar      *username;
-    GDBusProxy *proxy;
-};
-
-G_DEFINE_TYPE (Accounts, accounts, G_TYPE_OBJECT);
-
-static gboolean
-call_method (GDBusProxy *proxy, const gchar *method, GVariant *args,
-             const gchar *expected, GVariant **result)
-{
-    GVariant *answer;
-    GError *error = NULL;
-
-    if (!proxy)
-        return FALSE;
-
-    answer = g_dbus_proxy_call_sync (proxy,
-                                     method,
-                                     args,
-                                     G_DBUS_CALL_FLAGS_NONE,
-                                     -1,
-                                     NULL,
-                                     &error);
-
-    if (!answer) {
-        g_warning ("Could not call %s: %s", method, error->message);
-        g_error_free (error);
-        return FALSE;
-    }
-
-    if (!g_variant_is_of_type (answer, G_VARIANT_TYPE (expected))) {
-        g_warning ("Unexpected response from %s: %s",
-                   method, g_variant_get_type_string (answer));
-        g_variant_unref (answer);
-        return FALSE;
-    }
-
-    if (result)
-        *result = answer;
-    else
-        g_variant_unref (answer);
-    return TRUE;
-}
-
-static gboolean
-get_property (GDBusProxy *proxy, const gchar *property,
-              const gchar *expected, GVariant **result)
-{
-    GVariant *answer;
-
-    if (!proxy)
-        return FALSE;
-
-    answer = g_dbus_proxy_get_cached_property (proxy, property);
-
-    if (!answer) {
-        g_warning ("Could not get accounts property %s", property);
-        return FALSE;
-    }
-
-    if (!g_variant_is_of_type (answer, G_VARIANT_TYPE (expected))) {
-        g_warning ("Unexpected accounts property type for %s: %s",
-                   property, g_variant_get_type_string (answer));
-        g_variant_unref (answer);
-        return FALSE;
-    }
-
-    if (result)
-        *result = answer;
-    else
-        g_variant_unref (answer);
-    return TRUE;
-}
-
-static void
-save_string_to_dmrc (const gchar *username, const gchar *group,
-                     const gchar *key, const gchar *value)
-{
-    GKeyFile *dmrc;
-
-    dmrc = dmrc_load (username);
-    g_key_file_set_string (dmrc, group, key, value);
-    dmrc_save (dmrc, username);
-
-    g_key_file_free (dmrc);
-}
-
-static gchar *
-get_string_from_dmrc (const gchar *username, const gchar *group,
-                      const gchar *key)
-{
-    GKeyFile *dmrc;
-    gchar *value;
-
-    dmrc = dmrc_load (username);
-    value = g_key_file_get_string (dmrc, group, key, NULL);
-
-    g_key_file_free (dmrc);
-    return value;
-}
-
-Accounts *
-accounts_new (const gchar *user)
-{
-    g_return_val_if_fail (user != NULL, NULL);
-
-    Accounts *accounts;
-    GDBusProxy *proxy;
-    GError *error = NULL;
-    GVariant *result;
-    gboolean success;
-    gchar *user_path = NULL;
-
-    accounts = g_object_new (ACCOUNTS_TYPE, NULL);
-    accounts->priv->username = g_strdup (user);
-
-    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
-                                           G_DBUS_PROXY_FLAGS_NONE,
-                                           NULL,
-                                           "org.freedesktop.Accounts",
-                                           "/org/freedesktop/Accounts",
-                                           "org.freedesktop.Accounts",
-                                           NULL, &error);
-
-    if (!proxy) {
-        g_warning ("Could not get accounts proxy: %s", error->message);
-        g_error_free (error);
-        return accounts;
-    }
-
-    success = call_method (proxy, "FindUserByName", g_variant_new ("(s)", user),
-                           "(o)", &result);
-    g_object_unref (proxy);
-
-    if (!success)
-        return accounts;
-
-    g_variant_get (result, "(o)", &user_path);
-    g_variant_unref (result);
-
-    if (!user_path)
-        return accounts;
-
-    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
-                                           G_DBUS_PROXY_FLAGS_NONE,
-                                           NULL,
-                                           "org.freedesktop.Accounts",
-                                           user_path,
-                                           "org.freedesktop.Accounts.User",
-                                           NULL, &error);
-    g_free (user_path);
-
-    if (!proxy) {
-        g_warning ("Could not get accounts user proxy: %s", error->message);
-        g_error_free (error);
-        return accounts;
-    }
-
-    accounts->priv->proxy = proxy;
-    return accounts;
-}
-
-void
-accounts_set_session (Accounts *accounts, const gchar *session)
-{
-    g_return_if_fail (accounts != NULL);
-
-    call_method (accounts->priv->proxy, "SetXSession",
-                 g_variant_new ("(s)", session), "()", NULL);
-
-    save_string_to_dmrc (accounts->priv->username, "Desktop", "Session", session);
-}
-
-gchar *
-accounts_get_session (Accounts *accounts)
-{
-    g_return_val_if_fail (accounts != NULL, NULL);
-
-    GVariant *result;
-    gchar *session;
-
-    if (!get_property (accounts->priv->proxy, "XSession",
-                       "s", &result))
-        return get_string_from_dmrc (accounts->priv->username, "Desktop", "Session");
-
-    g_variant_get (result, "s", &session);
-    g_variant_unref (result);
-
-    if (g_strcmp0 (session, "") == 0) {
-        g_free (session);
-        return NULL;
-    }
-
-    return session;
-}
-
-static void
-accounts_init (Accounts *accounts)
-{
-    accounts->priv = G_TYPE_INSTANCE_GET_PRIVATE (accounts, ACCOUNTS_TYPE, AccountsPrivate);
-}
-
-static void
-accounts_dispose (GObject *object)
-{
-    Accounts *self;
-
-    self = ACCOUNTS (object);
-
-    if (self->priv->proxy) {
-        g_object_unref (self->priv->proxy);
-        self->priv->proxy = NULL;
-    }
-
-    G_OBJECT_CLASS (accounts_parent_class)->dispose (object);  
-}
-
-static void
-accounts_finalize (GObject *object)
-{
-    Accounts *self;
-
-    self = ACCOUNTS (object);
-
-    if (self->priv->username) {
-        g_free (self->priv->username);
-        self->priv->username = NULL;
-    }
-
-    G_OBJECT_CLASS (accounts_parent_class)->finalize (object);  
-}
-
-static void
-accounts_class_init (AccountsClass *klass)
-{
-    GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-    object_class->dispose = accounts_dispose;
-    object_class->finalize = accounts_finalize;
-
-    g_type_class_add_private (klass, sizeof (AccountsPrivate));
-}
diff --git a/src/accounts.h b/src/accounts.h
deleted file mode 100644 (file)
index 7ed6cba..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (C) 2011 Canonical Ltd
- * Author: Michael Terry <michael.terry@canonical.com>
- * 
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
- * license.
- */
-
-#ifndef _ACCOUNTS_H_
-#define _ACCOUNTS_H_
-
-#include <glib-object.h>
-
-G_BEGIN_DECLS
-
-#define ACCOUNTS_TYPE (accounts_get_type())
-#define ACCOUNTS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ACCOUNTS_TYPE, Accounts));
-
-typedef struct AccountsPrivate AccountsPrivate;
-
-typedef struct
-{
-    GObject          parent_instance;
-    AccountsPrivate *priv;
-} Accounts;
-
-typedef struct
-{
-    GObjectClass parent_class;
-} AccountsClass;
-
-GType accounts_get_type (void);
-
-Accounts *accounts_new (const gchar *user);
-
-void accounts_set_session (Accounts *accounts, const gchar *section);
-
-gchar *accounts_get_session (Accounts *accounts);
-
-G_END_DECLS
-
-#endif /* _ACCOUNTS_H_ */
index 8ba256b2a6845a62f979bf1d73aa10f7d0aa517e..a7c433a2892f3bf35653d567f21e488e726cda53 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
+ *
  * Copyright (C) 2010-2011 Robert Ancell.
  * Author: Robert Ancell <robert.ancell@canonical.com>
  *
@@ -18,7 +19,6 @@
 #include "configuration.h"
 #include "user.h"
 #include "pam-session.h"
-#include "accounts.h"
 #include "ldm-marshal.h"
 #include "greeter.h"
 #include "xserver-local.h" // FIXME: Shouldn't know if it's an xserver
@@ -779,7 +779,6 @@ static gboolean
 start_user_session (Display *display, PAMSession *authentication)
 {
     User *user;
-    Accounts *accounts;
     gchar *log_filename;
     gboolean result = FALSE;
 
@@ -788,9 +787,7 @@ start_user_session (Display *display, PAMSession *authentication)
     user = pam_session_get_user (authentication);
 
     /* Update user's xsession setting */
-    accounts = accounts_new (user_get_name (user));
-    accounts_set_session (accounts, display->priv->user_session);
-    g_object_unref (accounts);
+    user_set_session (user, display->priv->user_session);
 
     // FIXME: Copy old error file
     log_filename = g_build_filename (user_get_home_directory (user), ".xsession-errors", NULL);
index ff5460b2fa51f18ca0a53c30e11afd7a14744a46..8dd7312f8aab81371e7165dd1a24e860d5c0611e 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
+ *
  * Copyright (C) 2010-2011 Robert Ancell.
  * Author: Robert Ancell <robert.ancell@canonical.com>
  * 
 #include <string.h>
 
 #include "user.h"
+#include "dmrc.h"
 
 struct UserPrivate
 {
     /* Name of user */
     gchar *name;
 
+    /* Accounts interface proxy */
+    GDBusProxy *proxy;
+
     /* User ID */
     uid_t uid;
 
@@ -41,6 +46,157 @@ G_DEFINE_TYPE (User, user, G_TYPE_OBJECT);
 
 static gchar *passwd_file = NULL;
 
+static gboolean
+call_method (GDBusProxy *proxy, const gchar *method, GVariant *args,
+             const gchar *expected, GVariant **result)
+{
+    GVariant *answer;
+    GError *error = NULL;
+
+    if (!proxy)
+        return FALSE;
+
+    answer = g_dbus_proxy_call_sync (proxy,
+                                     method,
+                                     args,
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     -1,
+                                     NULL,
+                                     &error);
+
+    if (!answer) {
+        g_warning ("Could not call %s: %s", method, error->message);
+        g_error_free (error);
+        return FALSE;
+    }
+
+    if (!g_variant_is_of_type (answer, G_VARIANT_TYPE (expected))) {
+        g_warning ("Unexpected response from %s: %s",
+                   method, g_variant_get_type_string (answer));
+        g_variant_unref (answer);
+        return FALSE;
+    }
+
+    if (result)
+        *result = answer;
+    else
+        g_variant_unref (answer);
+    return TRUE;
+}
+
+static gboolean
+get_property (GDBusProxy *proxy, const gchar *property,
+              const gchar *expected, GVariant **result)
+{
+    GVariant *answer;
+
+    if (!proxy)
+        return FALSE;
+
+    answer = g_dbus_proxy_get_cached_property (proxy, property);
+
+    if (!answer) {
+        g_warning ("Could not get accounts property %s", property);
+        return FALSE;
+    }
+
+    if (!g_variant_is_of_type (answer, G_VARIANT_TYPE (expected))) {
+        g_warning ("Unexpected accounts property type for %s: %s",
+                   property, g_variant_get_type_string (answer));
+        g_variant_unref (answer);
+        return FALSE;
+    }
+
+    if (result)
+        *result = answer;
+    else
+        g_variant_unref (answer);
+    return TRUE;
+}
+
+static void
+save_string_to_dmrc (const gchar *username, const gchar *group,
+                     const gchar *key, const gchar *value)
+{
+    GKeyFile *dmrc;
+
+    dmrc = dmrc_load (username);
+    g_key_file_set_string (dmrc, group, key, value);
+    dmrc_save (dmrc, username);
+
+    g_key_file_free (dmrc);
+}
+
+static gchar *
+get_string_from_dmrc (const gchar *username, const gchar *group,
+                      const gchar *key)
+{
+    GKeyFile *dmrc;
+    gchar *value;
+
+    dmrc = dmrc_load (username);
+    value = g_key_file_get_string (dmrc, group, key, NULL);
+
+    g_key_file_free (dmrc);
+    return value;
+}
+
+static GDBusProxy *
+get_accounts_proxy_for_user (const gchar *user)
+{
+    g_return_val_if_fail (user != NULL, NULL);
+
+    GDBusProxy *proxy;
+    GError *error = NULL;
+    GVariant *result;
+    gboolean success;
+    gchar *user_path = NULL;
+
+    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                           G_DBUS_PROXY_FLAGS_NONE,
+                                           NULL,
+                                           "org.freedesktop.Accounts",
+                                           "/org/freedesktop/Accounts",
+                                           "org.freedesktop.Accounts",
+                                           NULL, &error);
+
+    if (!proxy) {
+        g_warning ("Could not get accounts proxy: %s", error->message);
+        g_error_free (error);
+        return NULL;
+    }
+
+    success = call_method (proxy, "FindUserByName", g_variant_new ("(s)", user),
+                           "(o)", &result);
+    g_object_unref (proxy);
+
+    if (!success)
+        return NULL;
+
+    g_variant_get (result, "(o)", &user_path);
+    g_variant_unref (result);
+
+    if (!user_path)
+        return NULL;
+
+    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                           G_DBUS_PROXY_FLAGS_NONE,
+                                           NULL,
+                                           "org.freedesktop.Accounts",
+                                           user_path,
+                                           "org.freedesktop.Accounts.User",
+                                           NULL, &error);
+    g_free (user_path);
+
+    if (!proxy) {
+        g_warning ("Could not get accounts user proxy: %s", error->message);
+        g_error_free (error);
+        return NULL;
+    }
+
+    return proxy;
+}
+
 static User *
 user_from_passwd (struct passwd *user_info)
 {
@@ -53,6 +209,7 @@ user_from_passwd (struct passwd *user_info)
     user->priv->gecos = g_strdup (user_info->pw_gecos);
     user->priv->home_directory = g_strdup (user_info->pw_dir);
     user->priv->shell = g_strdup (user_info->pw_shell);
+    user->priv->proxy = get_accounts_proxy_for_user (user->priv->name);
 
     return user;
 }
@@ -236,12 +393,61 @@ user_get_shell (User *user)
     return user->priv->shell;
 }
 
+void
+user_set_session (User *user, const gchar *session)
+{
+    g_return_if_fail (user != NULL);
+
+    call_method (user->priv->proxy, "SetXSession",
+                 g_variant_new ("(s)", session), "()", NULL);
+
+    save_string_to_dmrc (user->priv->name, "Desktop", "Session", session);
+}
+
+gchar *
+user_get_session (User *user)
+{
+    g_return_val_if_fail (user != NULL, NULL);
+
+    GVariant *result;
+    gchar *session;
+
+    if (!get_property (user->priv->proxy, "XSession",
+                       "s", &result))
+        return get_string_from_dmrc (user->priv->name, "Desktop", "Session");
+
+    g_variant_get (result, "s", &session);
+    g_variant_unref (result);
+
+    if (g_strcmp0 (session, "") == 0) {
+        g_free (session);
+        return NULL;
+    }
+
+    return session;
+}
+
 static void
 user_init (User *user)
 {
     user->priv = G_TYPE_INSTANCE_GET_PRIVATE (user, USER_TYPE, UserPrivate);
 }
 
+static void
+user_dispose (GObject *object)
+{
+    User *self;
+
+    self = USER (object);
+
+    if (self->priv->proxy) {
+        g_object_unref (self->priv->proxy);
+        self->priv->proxy = NULL;
+    }
+
+    G_OBJECT_CLASS (user_parent_class)->dispose (object);
+}
+
 static void
 user_finalize (GObject *object)
 {
@@ -262,6 +468,7 @@ user_class_init (UserClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+    object_class->dispose = user_dispose;
     object_class->finalize = user_finalize;  
 
     g_type_class_add_private (klass, sizeof (UserPrivate));
index 83372f0a9e0eac0c992004ab78155e6f9032f167..7a2cb61c03d3982b6ab323f1fe3c7334feecc7f3 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
+ *
  * Copyright (C) 2010-2011 Robert Ancell.
  * Author: Robert Ancell <robert.ancell@canonical.com>
  * 
@@ -57,6 +58,10 @@ const gchar *user_get_home_directory (User *user);
 
 const gchar *user_get_shell (User *user);
 
+gchar *user_get_session (User *user);
+
+void user_set_session (User *user, const gchar *session);
+
 G_END_DECLS
 
 #endif /* _USER_H_ */