gp-initial-setup-dialog.c 6.92 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * Copyright (C) 2018 Alberts Muktupāvels
 *
 * 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.1 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "gp-initial-setup-dialog-private.h"

#include <glib/gi18n-lib.h>

struct _GpInitialSetupDialog
{
  GtkWindow               parent;

  GtkWidget              *header_bar;
  GtkWidget              *done;

  GpInitialSetupCallback  setup_callback;
  gpointer                setup_user_data;
  GDestroyNotify          setup_user_data_free_func;

  gpointer                content_user_data;
  GDestroyNotify          content_user_data_free_func;

  GHashTable             *settings;
};

G_DEFINE_TYPE (GpInitialSetupDialog, gp_initital_setup_dialog, GTK_TYPE_WINDOW)

static void
cancel_clicked_cb (GtkButton            *button,
                   GpInitialSetupDialog *dialog)
{
  g_assert (dialog->setup_callback != NULL);
  dialog->setup_callback (dialog, TRUE, dialog->setup_user_data);
  gtk_widget_destroy (GTK_WIDGET (dialog));
}

static void
done_clicked_cb (GtkButton            *button,
                 GpInitialSetupDialog *dialog)
{
  g_assert (dialog->setup_callback != NULL);
  dialog->setup_callback (dialog, FALSE, dialog->setup_user_data);
  gtk_widget_destroy (GTK_WIDGET (dialog));
}

static void
gp_initital_setup_dialog_finalize (GObject *object)
{
  GpInitialSetupDialog *dialog;

  dialog = GP_INITIAL_SETUP_DIALOG (object);

  if (dialog->setup_user_data_free_func != NULL)
    {
      dialog->setup_user_data_free_func (dialog->setup_user_data);

      dialog->setup_user_data_free_func = NULL;
      dialog->setup_user_data = NULL;
    }

  if (dialog->content_user_data_free_func != NULL)
    {
      dialog->content_user_data_free_func (dialog->content_user_data);

      dialog->content_user_data_free_func = NULL;
      dialog->content_user_data = NULL;
    }

  g_clear_pointer (&dialog->settings, g_hash_table_destroy);

  G_OBJECT_CLASS (gp_initital_setup_dialog_parent_class)->finalize (object);
}

static gboolean
gp_initital_setup_dialog_delete_event (GtkWidget   *widget,
                                       GdkEventAny *event)
{
  GpInitialSetupDialog *dialog;

  dialog = GP_INITIAL_SETUP_DIALOG (widget);

  g_assert (dialog->setup_callback != NULL);
  dialog->setup_callback (dialog, TRUE, dialog->setup_user_data);

  return FALSE;
}

static void
gp_initital_setup_dialog_class_init (GpInitialSetupDialogClass *dialog_class)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;

  object_class = G_OBJECT_CLASS (dialog_class);
  widget_class = GTK_WIDGET_CLASS (dialog_class);

  object_class->finalize = gp_initital_setup_dialog_finalize;

  widget_class->delete_event = gp_initital_setup_dialog_delete_event;
}

static void
gp_initital_setup_dialog_init (GpInitialSetupDialog *dialog)
{
  GtkWidget *header_bar;
  GtkWidget *cancel;
  GtkWidget *done;
  GtkStyleContext *style;

  dialog->settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                                            (GDestroyNotify) g_variant_unref);

  header_bar = dialog->header_bar = gtk_header_bar_new ();
  gtk_header_bar_set_title (GTK_HEADER_BAR (header_bar), _("Initial Setup"));
  gtk_window_set_titlebar (GTK_WINDOW (dialog), header_bar);
  gtk_widget_show (header_bar);

  cancel = gtk_button_new_with_label (_("Cancel"));
  gtk_header_bar_pack_start (GTK_HEADER_BAR (dialog->header_bar), cancel);
  gtk_widget_show (cancel);

  done = dialog->done = gtk_button_new_with_label (_("Done"));
  gtk_header_bar_pack_end (GTK_HEADER_BAR (dialog->header_bar), done);
  gtk_widget_set_sensitive (done, FALSE);
  gtk_widget_show (done);

  style = gtk_widget_get_style_context (done);
  gtk_style_context_add_class (style, GTK_STYLE_CLASS_SUGGESTED_ACTION);

  g_signal_connect (cancel, "clicked", G_CALLBACK (cancel_clicked_cb), dialog);
  g_signal_connect (done, "clicked", G_CALLBACK (done_clicked_cb), dialog);
}

GpInitialSetupDialog *
gp_initital_setup_dialog_new (void)
{
  return g_object_new (GP_TYPE_INITIAL_SETUP_DIALOG, NULL);
}

void
gp_initital_setup_dialog_add_callback (GpInitialSetupDialog   *dialog,
                                       GpInitialSetupCallback  callback,
                                       gpointer                user_data,
                                       GDestroyNotify          free_func)
{
  dialog->setup_callback = callback;
  dialog->setup_user_data = user_data;
  dialog->setup_user_data_free_func = free_func;
}

void
gp_initital_setup_dialog_add_content_widget (GpInitialSetupDialog *dialog,
                                             GtkWidget            *content,
                                             gpointer              user_data,
                                             GDestroyNotify        free_func)
{
  dialog->content_user_data = user_data;
  dialog->content_user_data_free_func = free_func;

  gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
  gtk_container_add (GTK_CONTAINER (dialog), content);
  gtk_widget_show (content);
}

/**
 * gp_initital_setup_dialog_set_setting:
 * @dialog: a #GpInitialSetupDialog
 * @key: the setting key
 * @value: (allow-none): the setting value, or %NULL to unset the setting
 *
 * Sets a setting for @key with value @value. If @value is %NULL,
 * a previously set setting for @key is unset.
 *
 * If @value is floating, it is consumed.
 */
void
gp_initital_setup_dialog_set_setting (GpInitialSetupDialog *dialog,
                                      const gchar          *key,
                                      GVariant             *value)
{
  if (value != NULL)
    {
      g_hash_table_insert (dialog->settings, g_strdup (key),
                           g_variant_ref_sink (value));
    }
  else
    {
      g_hash_table_remove (dialog->settings, key);
    }
}

GVariant *
gp_initital_setup_dialog_get_settings (GpInitialSetupDialog *dialog)
{
  GVariantBuilder builder;
  GHashTableIter iter;
  gpointer key;
  gpointer data;
  GVariant *settings;

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
  g_hash_table_iter_init (&iter, dialog->settings);

  while (g_hash_table_iter_next (&iter, &key, &data))
    g_variant_builder_add (&builder, "{sv}", key, data);

  settings = g_variant_builder_end (&builder);

  return g_variant_ref_sink (settings);
}

void
gp_initital_setup_dialog_set_done (GpInitialSetupDialog *dialog,
                                   gboolean              done)
{
  gtk_widget_set_sensitive (dialog->done, done);
}