empathy-accounts.c 6.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2005-2007 Imendio AB
 * Copyright (C) 2007-2010 Collabora Ltd.
 *
 * 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 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Martyn Russell <martyn@imendio.com>
 *          Xavier Claessens <xclaesse@gmail.com>
 *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
 *          Jonathan Tellier <jonathan.tellier@gmail.com>
 *          Travis Reitter <travis.reitter@collabora.co.uk>
 */

27
#include "config.h"
28
#include "empathy-accounts.h"
29 30 31

#include <glib/gi18n.h>

32 33 34 35
#ifdef HAVE_CHEESE
#include <cheese-gtk.h>
#endif

36
#include "empathy-accounts-common.h"
37 38
#include "empathy-ui-utils.h"
#include "empathy-utils.h"
39 40

#define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
41
#include "empathy-debug.h"
42 43 44

#define EMPATHY_ACCOUNTS_DBUS_NAME "org.gnome.EmpathyAccounts"

45
static gboolean only_if_needed = FALSE;
46 47 48
static gboolean hidden = FALSE;
static gchar *selected_account_name = NULL;

49
static void
50 51
maybe_show_accounts_ui (TpAccountManager *manager,
    GApplication *app)
52
{
53 54 55 56 57 58
  if (hidden)
    return;

  if (only_if_needed && empathy_accounts_has_non_salut_accounts (manager))
    return;

59
  empathy_accounts_show_accounts_ui (manager, NULL, app);
60 61
}

62 63 64 65 66 67 68
static TpAccount *
find_account (TpAccountManager *mgr,
    const gchar *path)
{
  GList *accounts, *l;
  TpAccount *found = NULL;

69
  accounts = tp_account_manager_dup_valid_accounts (mgr);
70 71 72 73 74 75 76 77 78
  for (l = accounts; l != NULL; l = g_list_next (l))
    {
      if (!tp_strdiff (tp_proxy_get_object_path (l->data), path))
        {
          found = l->data;
          break;
        }
    }

79
  g_list_free_full (accounts, g_object_unref);
80 81 82
  return found;
}

83 84 85 86 87 88 89
static void
account_manager_ready_for_accounts_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
  GError *error = NULL;
90
  GApplication *app = G_APPLICATION (user_data);
91

92
  if (!tp_proxy_prepare_finish (manager, result, &error))
93 94 95
    {
      DEBUG ("Failed to prepare account manager: %s", error->message);
      g_clear_error (&error);
96
      goto out;
97 98
    }

99
  if (selected_account_name != NULL)
100 101
    {
      gchar *account_path;
102
      TpAccount *account;
103 104 105

      /* create and prep the corresponding TpAccount so it's fully ready by the
       * time we try to select it in the accounts dialog */
106 107 108 109 110 111
      if (g_str_has_prefix (selected_account_name, TP_ACCOUNT_OBJECT_PATH_BASE))
        account_path = g_strdup (selected_account_name);
      else
        account_path = g_strdup_printf ("%s%s", TP_ACCOUNT_OBJECT_PATH_BASE,
            selected_account_name);

112 113 114
      account = find_account (manager, account_path);

      if (account != NULL)
115
        {
116
          empathy_accounts_show_accounts_ui (manager, account, app);
117
          goto out;
118 119 120
        }
      else
        {
121 122
          DEBUG ("Failed to find account with path %s", account_path);

123
          g_clear_error (&error);
124

125
          maybe_show_accounts_ui (manager, app);
126 127 128 129 130 131
        }

      g_free (account_path);
    }
  else
    {
132
      maybe_show_accounts_ui (manager, app);
133
    }
134 135 136

out:
  g_application_release (app);
137 138
}

139 140 141
static int
app_command_line_cb (GApplication *app,
    GApplicationCommandLine *cmdline)
142
{
143 144 145
  TpAccountManager *account_manager;

  account_manager = tp_account_manager_dup ();
146

147 148 149
  /* Hold the application while preparing the AM */
  g_application_hold (app);

150
  tp_proxy_prepare_async (account_manager, NULL,
151
    account_manager_ready_for_accounts_cb, app);
152

153
  g_object_unref (account_manager);
154

155 156 157 158 159 160 161
  return 0;
}

static gboolean
local_cmdline (GApplication *app,
    gchar ***arguments,
    gint *exit_status)
162
{
163 164 165 166
  gint i;
  gchar **argv;
  gint argc = 0;
  gboolean retval = FALSE;
167 168 169 170 171 172 173 174
  GError *error = NULL;

  GOptionContext *optcontext;
  GOptionEntry options[] = {
      { "hidden", 'h',
        0, G_OPTION_ARG_NONE, &hidden,
        N_("Don't display any dialogs; do any work (eg, importing) and exit"),
        NULL },
175 176
      { "if-needed", 'n',
        0, G_OPTION_ARG_NONE, &only_if_needed,
177
        N_("Don't display any dialogs unless there are only \"People Nearby\" accounts"),
178
        NULL },
179 180 181 182 183 184 185 186 187 188 189 190
      { "select-account", 's',
        G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &selected_account_name,
        N_("Initially select given account (eg, "
            "gabble/jabber/foo_40example_2eorg0)"),
        N_("<account-id>") },

      { NULL }
  };

  optcontext = g_option_context_new (N_("- Empathy Accounts"));
  g_option_context_add_group (optcontext, gtk_get_option_group (TRUE));
  g_option_context_add_main_entries (optcontext, options, GETTEXT_PACKAGE);
191
  g_option_context_set_translation_domain (optcontext, GETTEXT_PACKAGE);
192

193 194 195 196
  argv = *arguments;
  for (i = 0; argv[i] != NULL; i++)
    argc++;

197 198 199 200 201
  if (!g_option_context_parse (optcontext, &argc, &argv, &error))
    {
      g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n",
          error->message, argv[0]);
      g_warning ("Error in empathy init: %s", error->message);
202 203 204

      *exit_status = EXIT_FAILURE;
      retval = TRUE;
205 206 207 208
    }

  g_option_context_free (optcontext);

209 210 211 212 213 214 215 216
  return retval;
}

int
main (int argc, char *argv[])
{
  GtkApplication *app;
  GObjectClass *app_class;
217
  gint retval;
218

219 220 221 222 223 224 225
  g_type_init ();

#ifdef HAVE_CHEESE
  /* Used by the avatar chooser */
  g_return_val_if_fail (cheese_gtk_init (&argc, &argv), 1);
#endif

226 227 228
  empathy_init ();

  gtk_init (&argc, &argv);
229 230
  empathy_gtk_init ();

231
  g_set_application_name (_("Empathy Accounts"));
232

233 234
  /* Make empathy and empathy-accounts appear as the same app in gnome-shell */
  gdk_set_program_class ("Empathy");
235 236
  gtk_window_set_default_icon_name ("empathy");

237
  app = gtk_application_new (EMPATHY_ACCOUNTS_DBUS_NAME,
238 239 240
      G_APPLICATION_HANDLES_COMMAND_LINE);
  app_class = G_OBJECT_GET_CLASS (app);
  G_APPLICATION_CLASS (app_class)->local_command_line = local_cmdline;
241

242 243
  g_signal_connect (app, "command-line", G_CALLBACK (app_command_line_cb),
      NULL);
244

245
  retval = g_application_run (G_APPLICATION (app), argc, argv);
246

247
  g_object_unref (app);
248

249
  return retval;
250
}