static void
shell_test_button_clicked (GtkButton *button, CcMousePanel *panel)
{
  CcMousePanelPrivate *priv = panel->priv;
  CcShell *shell;

  shell = cc_panel_get_shell (CC_PANEL (panel));

  /* When running on a small screen, make the test dialog
   * the same height of the toplevel. */
  if (cc_shell_is_small_screen (shell))
    {
      gint height;

      gtk_window_get_size (GTK_WINDOW (cc_shell_get_toplevel (shell)), NULL, &height);

      gtk_widget_set_size_request (priv->test_dialog, -1, height);
    }
  else
    {
      gtk_widget_set_size_request (priv->test_widget, -1, TEST_WIDGET_DEFAULT_HEIGHT);
    }

  /* GTK_RESPONSE_NONE is returned if the dialog is being destroyed, so only
   * hide the dialog if it is not being destroyed */
  if (gtk_dialog_run (GTK_DIALOG (priv->test_dialog)) != GTK_RESPONSE_NONE)
    gtk_widget_hide (priv->test_dialog);
}
static void
remove_account_cb (GoaAccount    *account,
                   GAsyncResult  *res,
                   gpointer       user_data)
{
  GoaPanel *panel = GOA_PANEL (user_data);
  GError *error;

  error = NULL;
  if (!goa_account_call_remove_finish (account, res, &error))
    {
      GtkWidget *dialog;
      dialog = gtk_message_dialog_new (GTK_WINDOW (cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)))),
                                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                       GTK_MESSAGE_ERROR,
                                       GTK_BUTTONS_CLOSE,
                                       _("Error removing account"));
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                "%s",
                                                error->message);
      gtk_widget_show_all (dialog);
      gtk_dialog_run (GTK_DIALOG (dialog));
      gtk_widget_destroy (dialog);
      g_error_free (error);
    }
  g_object_unref (panel);
}
void
cc_network_panel_connect_to_3g_network (CcNetworkPanel   *panel,
                                        NMClient         *client,
                                        NMRemoteSettings *settings,
                                        NMDevice         *device)
{
    GtkWidget *toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
    if (toplevel == NULL) {
        toplevel = GTK_WIDGET (panel);
    }
    MobileDialogClosure *closure;
    NMAMobileWizard *wizard;
    NMDeviceModemCapabilities caps;
    gboolean visible = FALSE;

    g_debug ("connect to 3g");
    if (!NM_IS_DEVICE_MODEM (device)) {
        g_warning ("Network panel loaded with connect-3g but the selected device"
                   " is not a modem");
        return;
    }

    closure = g_slice_new (MobileDialogClosure);
    closure->client = g_object_ref (client);
    closure->settings = g_object_ref (settings);
    closure->device = g_object_ref (device);

    caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
    if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
        wizard = nma_mobile_wizard_new (GTK_WINDOW (toplevel), NULL, NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS, FALSE,
                                        gsm_mobile_wizard_done, closure);
        if (wizard == NULL) {
            g_warning ("failed to construct GSM wizard");
            return;
        }
    } else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
        wizard = nma_mobile_wizard_new (GTK_WINDOW (toplevel), NULL, NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO, FALSE,
                                        cdma_mobile_wizard_done, closure);
        if (wizard == NULL) {
            g_warning ("failed to construct CDMA wizard");
            return;
        }
    } else {
        g_warning ("Network panel loaded with connect-3g but the selected device"
                   " does not support GSM or CDMA");
        mobile_dialog_closure_free (closure);
        return;
    }

    g_object_get (G_OBJECT (toplevel), "visible", &visible, NULL);
    if (visible) {
        g_debug ("Scheduling showing the Mobile wizard");
        g_idle_add ((GSourceFunc) show_wizard_idle_cb, wizard);
    } else {
        g_debug ("Will show wizard a bit later, toplevel is not visible");
        g_signal_connect (G_OBJECT (toplevel), "notify::visible",
                          G_CALLBACK (toplevel_shown), wizard);
    }
}
static void
on_info_bar_response (GtkInfoBar *info_bar,
                      gint        response_id,
                      gpointer    user_data)
{
  GoaPanel *panel = GOA_PANEL (user_data);
  GtkTreeIter iter;

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (panel->accounts_treeview)),
                                       NULL,
                                       &iter))
    {
      GoaProvider *provider;
      const gchar *provider_type;
      GoaAccount *account;
      GoaObject *object;
      GtkWindow *parent;
      GError *error;

      gtk_tree_model_get (GTK_TREE_MODEL (panel->accounts_model),
                          &iter,
                          GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, &object,
                          -1);

      account = goa_object_peek_account (object);
      provider_type = goa_account_get_provider_type (account);
      provider = goa_provider_get_for_provider_type (provider_type);

      parent = GTK_WINDOW (cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel))));

      error = NULL;
      if (!goa_provider_refresh_account (provider,
                                         panel->client,
                                         object,
                                         parent,
                                         &error))
        {
          if (!(error->domain == GOA_ERROR && error->code == GOA_ERROR_DIALOG_DISMISSED))
            {
              GtkWidget *dialog;
              dialog = gtk_message_dialog_new (parent,
                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                               GTK_MESSAGE_ERROR,
                                               GTK_BUTTONS_CLOSE,
                                               _("Error logging into the account"));
              gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                        "%s",
                                                        error->message);
              gtk_widget_show_all (dialog);
              gtk_dialog_run (GTK_DIALOG (dialog));
              gtk_widget_destroy (dialog);
            }
          g_error_free (error);
        }
      g_object_unref (provider);
      g_object_unref (object);
    }
}
static void
run_dialog (CcDateTimePanel *self,
            const gchar     *dialog_name)
{
  CcDateTimePanelPrivate *priv = self->priv;
  GtkWidget *dialog, *parent;

  dialog = W (dialog_name);

  parent = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (self)));

  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
  gtk_dialog_run (GTK_DIALOG (dialog));
}
static void
help_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  GnomeControlCenter *shell = user_data;
  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
  const char *uri = NULL;

  if (panel)
    uri = cc_panel_get_help_uri (panel);

  gtk_show_uri (gtk_widget_get_screen (window),
                uri ? uri : "help:gnome-help/prefs",
                GDK_CURRENT_TIME, NULL);
}
static void
help_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  CcApplication *self = CC_APPLICATION (user_data);
  CcPanel *panel;
  GtkWidget *window;
  const char *uri = NULL;

  panel = cc_shell_get_active_panel (CC_SHELL (self->priv->window));
  if (panel)
    uri = cc_panel_get_help_uri (panel);

  window = cc_shell_get_toplevel (CC_SHELL (self->priv->window));
  gtk_show_uri (gtk_widget_get_screen (window),
                uri ? uri : "help:gnome-help/prefs",
                GDK_CURRENT_TIME, NULL);
}
static void
on_toolbar_remove_button_clicked (GtkToolButton *button,
                                  gpointer       user_data)
{
  GoaPanel *panel = GOA_PANEL (user_data);
  GtkTreeIter iter;

  if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (panel->accounts_treeview)),
                                       NULL,
                                       &iter))
    {
      GoaObject *object;
      GtkWidget *dialog;
      gint response;

      gtk_tree_model_get (GTK_TREE_MODEL (panel->accounts_model),
                          &iter,
                          GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, &object,
                          -1);

      dialog = gtk_message_dialog_new (GTK_WINDOW (cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)))),
                                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                       GTK_MESSAGE_QUESTION,
                                       GTK_BUTTONS_CANCEL,
                                       _("Are you sure you want to remove the account?"));
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                _("This will not remove the account on the server."));
      gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Remove"), GTK_RESPONSE_OK);
      gtk_widget_show_all (dialog);
      response = gtk_dialog_run (GTK_DIALOG (dialog));
      gtk_widget_destroy (dialog);

      if (response == GTK_RESPONSE_OK)
        {
          goa_account_call_remove (goa_object_peek_account (object),
                                   NULL, /* GCancellable */
                                   (GAsyncReadyCallback) remove_account_cb,
                                   g_object_ref (panel));
        }
      g_object_unref (object);
    }
}
static void
cc_mouse_panel_constructed (GObject *object)
{
  CcMousePanel *self = CC_MOUSE_PANEL (object);
  CcMousePanelPrivate *priv = self->priv;
  GtkWidget *button, *container, *toplevel, *label;
  CcShell *shell;

  G_OBJECT_CLASS (cc_mouse_panel_parent_class)->constructed (object);

  /* Add test area button to shell header. */
  shell = cc_panel_get_shell (CC_PANEL (self));

  label = gtk_label_new_with_mnemonic (_("Test Your _Settings"));
  gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
  gtk_widget_show (label);

  button = gtk_button_new ();
  gtk_container_add (GTK_CONTAINER (button), label);
  gtk_style_context_add_class (gtk_widget_get_style_context (button),
                               "text-button");
  gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
  gtk_widget_set_visible (button, TRUE);

  cc_shell_embed_widget_in_header (shell, button);

  g_signal_connect (GTK_BUTTON (button), "clicked",
                    G_CALLBACK (shell_test_button_clicked),
                    self);

  toplevel = cc_shell_get_toplevel (shell);
  priv->test_dialog = g_object_new (GTK_TYPE_DIALOG, "title", _("Test Your Settings"),
                                                     "transient-for", GTK_WINDOW (toplevel),
                                                     "modal", TRUE,
                                                     "use_header-bar", TRUE,
                                                     "resizable", FALSE,
                                                     NULL);

  container = gtk_dialog_get_content_area (GTK_DIALOG (priv->test_dialog));
  gtk_container_add (GTK_CONTAINER (container), priv->test_widget);
}
static void
help_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  CinnamonControlCenter *shell = user_data;
  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
  const char *uri = NULL;

  if (panel)
    uri = cc_panel_get_help_uri (panel);
    if (!g_strcmp0(g_getenv("XDG_CURRENT_DESKTOP"), "Unity"))
      gtk_show_uri (gtk_widget_get_screen (window),
                uri ? uri : "help:ubuntu-help/prefs",
                GDK_CURRENT_TIME, NULL);
    else
      gtk_show_uri (gtk_widget_get_screen (window),
                uri ? uri : "help:gnome-help/prefs",
                GDK_CURRENT_TIME, NULL);
}
static void
cc_sharing_panel_run_dialog (CcSharingPanel *self,
                             const gchar    *dialog_name)
{
  CcSharingPanelPrivate *priv = self->priv;
  GtkWidget *dialog, *parent;

  dialog = WID (dialog_name);

  /* ensure labels with the hostname are updated if the hostname has changed */
  cc_sharing_panel_setup_label_with_hostname (self,
                                              WID ("screen-sharing-label"));
  cc_sharing_panel_setup_label_with_hostname (self, WID ("remote-login-label"));
  cc_sharing_panel_setup_label_with_hostname (self,
                                              WID ("personal-file-sharing-label"));


  parent = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (self)));

  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
  gtk_dialog_run (GTK_DIALOG (dialog));
}
static void
add_account (GoaPanel *panel)
{
  GtkWindow *parent;
  GtkWidget *dialog;
  gint response;
  GList *providers;
  GList *l;
  GoaObject *object;
  GError *error;

  providers = NULL;

  parent = GTK_WINDOW (cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel))));

  dialog = goa_panel_add_account_dialog_new (panel->client);
  gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

  providers = goa_provider_get_all ();
  for (l = providers; l != NULL; l = l->next)
    {
      GoaProvider *provider;

      provider = GOA_PROVIDER (l->data);
      goa_panel_add_account_dialog_add_provider (GOA_PANEL_ADD_ACCOUNT_DIALOG (dialog), provider);
    }

  gtk_widget_show_all (dialog);
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  if (response != GTK_RESPONSE_OK)
    {
      gtk_widget_destroy (dialog);
      goto out;
    }

  error = NULL;
  object = goa_panel_add_account_dialog_get_account (GOA_PANEL_ADD_ACCOUNT_DIALOG (dialog), &error);
  gtk_widget_destroy (dialog);

  /* We might have an object even when error is set.
   * eg., if we failed to store the credentials in the keyring.
   */

  if (object != NULL)
    {
      GtkTreeIter iter;
      /* navigate to newly created object */
      if (goa_panel_accounts_model_get_iter_for_object (panel->accounts_model,
                                                        object,
                                                        &iter))
        {
          gtk_tree_selection_select_iter (gtk_tree_view_get_selection (GTK_TREE_VIEW (panel->accounts_treeview)),
                                          &iter);
        }
      g_object_unref (object);
    }

  if (error != NULL)
    {
      if (!(error->domain == GOA_ERROR && error->code == GOA_ERROR_DIALOG_DISMISSED))
        {
          dialog = gtk_message_dialog_new (parent,
                                           GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_CLOSE,
                                           _("Error creating account"));
          gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                    "%s",
                                                    error->message);
          gtk_widget_show_all (dialog);
          gtk_dialog_run (GTK_DIALOG (dialog));
          gtk_widget_destroy (dialog);
        }
      g_error_free (error);
    }

 out:
  g_list_foreach (providers, (GFunc) g_object_unref, NULL);
  g_list_free (providers);
}