<commit_before>/*
 * This file is part of mission-control
 *
 * Copyright (C) 2007 Nokia Corporation. 
 *
 * Contact: <NAME>  <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <string.h>
#include <strings.h>

#include <gconf/gconf-client.h>

#include "mc-account.h"
#include "mc-account-priv.h"
#include "mc-account-monitor.h"
#include "mc-signals-marshal.h"

G_DEFINE_TYPE (McAccountMonitor, mc_account_monitor, G_TYPE_OBJECT);

#define MC_ACCOUNT_MONITOR_PRIV(monitor) \
  ((McAccountMonitorPrivate *)monitor->priv)

enum
{
  SIGNAL_CREATED,
  SIGNAL_DELETED,
  SIGNAL_ENABLED,
  SIGNAL_DISABLED,
  SIGNAL_CHANGED,
  PARAM_CHANGED,
  NUM_SIGNALS
};

static guint signals[NUM_SIGNALS];

typedef struct
{
  GConfClient *gconf_client;
  guint gconf_connection;
  GHashTable *accounts;
} McAccountMonitorPrivate;

static void
mc_account_monitor_finalize (GObject *object)
{
  McAccountMonitor *self = MC_ACCOUNT_MONITOR (object);
  McAccountMonitorPrivate *priv = MC_ACCOUNT_MONITOR_PRIV (self);

  gconf_client_notify_remove (priv->gconf_client, priv->gconf_connection);
  gconf_client_remove_dir (
    priv->gconf_client, MC_ACCOUNTS_GCONF_BASE, NULL);
  g_object_unref (priv->gconf_client);

  g_hash_table_destroy (priv->accounts);
}

static void
mc_account_monitor_class_init (McAccountMonitorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  g_type_class_add_private (object_class, sizeof (McAccountMonitorPrivate));
  object_class->finalize = mc_account_monitor_finalize;

  /**
   * McAccountMonitor::account-created:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being created (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when a new account is created.
   */
  signals[SIGNAL_CREATED] = g_signal_new (
    "account-created",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-deleted:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being deleted (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is deleted.
   */
  signals[SIGNAL_DELETED] = g_signal_new (
    "account-deleted",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-enabled:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being enabled (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is enabled.
   */
  signals[SIGNAL_ENABLED] = g_signal_new (
    "account-enabled",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-disabled:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being disabled (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is disabled.
   */
  signals[SIGNAL_DISABLED] = g_signal_new (
    "account-disabled",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-changed:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being changed (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is changed.
   */
  signals[SIGNAL_CHANGED] = g_signal_new (
    "account-changed",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::param-changed:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being changed (use mc_account_lookup() to
   * retrieve the account object).
   * @param: The name of the parameter which changed.
   *
   * Emitted when an account parameter is changed.
   */
  signals[PARAM_CHANGED] = g_signal_new (
    "param-changed",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    mc_signals_marshal_VOID__STRING_STRING,
    G_TYPE_NONE, 2,
    G_TYPE_STRING, G_TYPE_STRING);
}

static gchar *
_account_name_from_key (const gchar *key)
{
  guint base_len = strlen (MC_ACCOUNTS_GCONF_BASE);
  const gchar *base, *slash;

  g_assert (key == strstr (key, MC_ACCOUNTS_GCONF_BASE));
  if (strlen (key) <= base_len + 1) return NULL;

  base = key + base_len + 1;
  slash = index (base, '/');

  if (slash == NULL)
    return g_strdup (base);
  else
    return g_strndup (base, slash - base);
}

static const gchar *
key_name (const gchar *path_key)
{
    const gchar *key = 0;

    while (*path_key != 0)
    {
	if (*path_key == '/') key = path_key + 1;
	path_key++;
    }
    return key;
}

static void
_gconf_notify_cb (GConfClient *client, guint conn_id, GConfEntry *entry,
                  gpointer user_data)
{
  McAccountMonitor *monitor = MC_ACCOUNT_MONITOR (user_data);
  McAccountMonitorPrivate *priv = MC_ACCOUNT_MONITOR_PRIV (monitor);
  gchar *name = NULL;
  McAccount *account;
  gboolean key_is_enabledness;
  const gchar *key;

  key = key_name (entry->key);
  key_is_enabledness = strcmp (key, MC_ACCOUNTS_GCONF_KEY_ENABLED) == 0;
  name = _account_name_from_key (entry->key);
  if (!name) return;
  account = g_hash_table_lookup (priv->accounts, name);
  
  /* Was account complete before? */
  if (account == NULL)
    {
      account = _mc_account_new (name);<commit_msg>Remove unused variable<commit_after>/*
 * This file is part of mission-control
 *
 * Copyright (C) 2007 Nokia Corporation. 
 *
 * Contact: <NAME>  <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <string.h>
#include <strings.h>

#include <gconf/gconf-client.h>

#include "mc-account.h"
#include "mc-account-priv.h"
#include "mc-account-monitor.h"
#include "mc-signals-marshal.h"

G_DEFINE_TYPE (McAccountMonitor, mc_account_monitor, G_TYPE_OBJECT);

#define MC_ACCOUNT_MONITOR_PRIV(monitor) \
  ((McAccountMonitorPrivate *)monitor->priv)

enum
{
  SIGNAL_CREATED,
  SIGNAL_DELETED,
  SIGNAL_ENABLED,
  SIGNAL_DISABLED,
  SIGNAL_CHANGED,
  PARAM_CHANGED,
  NUM_SIGNALS
};

static guint signals[NUM_SIGNALS];

typedef struct
{
  GConfClient *gconf_client;
  guint gconf_connection;
  GHashTable *accounts;
} McAccountMonitorPrivate;

static void
mc_account_monitor_finalize (GObject *object)
{
  McAccountMonitor *self = MC_ACCOUNT_MONITOR (object);
  McAccountMonitorPrivate *priv = MC_ACCOUNT_MONITOR_PRIV (self);

  gconf_client_notify_remove (priv->gconf_client, priv->gconf_connection);
  gconf_client_remove_dir (
    priv->gconf_client, MC_ACCOUNTS_GCONF_BASE, NULL);
  g_object_unref (priv->gconf_client);

  g_hash_table_destroy (priv->accounts);
}

static void
mc_account_monitor_class_init (McAccountMonitorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  g_type_class_add_private (object_class, sizeof (McAccountMonitorPrivate));
  object_class->finalize = mc_account_monitor_finalize;

  /**
   * McAccountMonitor::account-created:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being created (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when a new account is created.
   */
  signals[SIGNAL_CREATED] = g_signal_new (
    "account-created",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-deleted:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being deleted (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is deleted.
   */
  signals[SIGNAL_DELETED] = g_signal_new (
    "account-deleted",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-enabled:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being enabled (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is enabled.
   */
  signals[SIGNAL_ENABLED] = g_signal_new (
    "account-enabled",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-disabled:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being disabled (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is disabled.
   */
  signals[SIGNAL_DISABLED] = g_signal_new (
    "account-disabled",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::account-changed:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being changed (use mc_account_lookup() to
   * retrieve the account object).
   *
   * Emitted when an account is changed.
   */
  signals[SIGNAL_CHANGED] = g_signal_new (
    "account-changed",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    g_cclosure_marshal_VOID__STRING,
    G_TYPE_NONE, 1,
    G_TYPE_STRING);
  /**
   * McAccountMonitor::param-changed:
   * @self: The #McAccountMonitor.
   * @name: The name of the account being changed (use mc_account_lookup() to
   * retrieve the account object).
   * @param: The name of the parameter which changed.
   *
   * Emitted when an account parameter is changed.
   */
  signals[PARAM_CHANGED] = g_signal_new (
    "param-changed",
    G_TYPE_FROM_CLASS (klass),
    G_SIGNAL_RUN_LAST,
    0,
    NULL, NULL,
    mc_signals_marshal_VOID__STRING_STRING,
    G_TYPE_NONE, 2,
    G_TYPE_STRING, G_TYPE_STRING);
}

static gchar *
_account_name_from_key (const gchar *key)
{
  guint base_len = strlen (MC_ACCOUNTS_GCONF_BASE);
  const gchar *base, *slash;

  g_assert (key == strstr (key, MC_ACCOUNTS_GCONF_BASE));
  if (strlen (key) <= base_len + 1) return NULL;

  base = key + base_len + 1;
  slash = index (base, '/');

  if (slash == NULL)
    return g_strdup (base);
  else
    return g_strndup (base, slash - base);
}

static const gchar *
key_name (const gchar *path_key)
{
    const gchar *key = 0;

    while (*path_key != 0)
    {
	if (*path_key == '/') key = path_key + 1;
	path_key++;
    }
    return key;
}

static void
_gconf_notify_cb (GConfClient *client, guint conn_id, GConfEntry *entry,
                  gpointer user_data)
{
  McAccountMonitor *monitor = MC_ACCOUNT_MONITOR (user_data);
  McAccountMonitorPrivate *priv = MC_ACCOUNT_MONITOR_PRIV (monitor);
  gchar *name = NULL;
  McAccount *account;
  const gchar *key;

  key = key_name (entry->key);
  name = _account_name_from_key (entry->key);
  if (!name) return;
  account = g_hash_table_lookup (priv->accounts, name);
  
  /* Was account complete before? */
  if (account == NULL)
    {
      account = _mc_account_new (name);
{'fraction_non_alphanumeric': 0.0993729661084213, 'fraction_numerical': 0.00547662512897849, 'mean_word_length': 3.790874524714829, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6598331', 'n_tokens_mistral': 4432, 'n_tokens_neox': 4363, 'n_words': 1122}