summaryrefslogtreecommitdiff
path: root/src/gui/calendar-management/gcal-file-chooser-button.c
blob: 9a3b15bfc62418c26fc251278ff4435b6ee1b4f0 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* gcal-file-chooser-button.c
 *
 * Copyright 2021 Georges Basile Stavracas Neto <georges.stavracas@gmail.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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "gcal-file-chooser-button.h"

#include <glib/gi18n.h>

struct _GcalFileChooserButton
{
  GtkButton           parent_instance;

  GCancellable       *cancellable;
  GtkFileFilter      *filter;
  GFile              *file;
  gchar              *title;
};

G_DEFINE_FINAL_TYPE (GcalFileChooserButton, gcal_file_chooser_button, GTK_TYPE_BUTTON)

enum
{
  PROP_0,
  PROP_FILE,
  PROP_FILTER,
  PROP_TITLE,
  N_PROPS
};

static GParamSpec *properties [N_PROPS] = { NULL, };

static const gchar *
get_title (GcalFileChooserButton *self)
{
  return self->title ? self->title : _("Select a file");
}

static void
update_label (GcalFileChooserButton *self)
{
  g_autofree gchar *label = NULL;

  if (self->file)
    label = g_file_get_basename (self->file);
  else
    label = g_strdup (get_title (self));

  gtk_button_set_label (GTK_BUTTON (self), label);
}

static void
on_file_opened_cb (GObject      *source,
                   GAsyncResult *result,
                   gpointer      user_data)
{
  GcalFileChooserButton *self;
  g_autoptr (GError) error = NULL;
  g_autoptr (GFile) file = NULL;

  file = gtk_file_dialog_open_finish (GTK_FILE_DIALOG (source), result, &error);

  if (error)
    {
      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        return;

      g_warning ("Error opening file: %s", error->message);
    }

  self = GCAL_FILE_CHOOSER_BUTTON (user_data);
  gcal_file_chooser_button_set_file (self, file);
}

static void
gcal_file_chooser_button_clicked (GtkButton *button)
{
  g_autoptr (GtkFileDialog) file_dialog = NULL;
  GcalFileChooserButton *self;
  GtkRoot *root;

  self = GCAL_FILE_CHOOSER_BUTTON (button);
  root = gtk_widget_get_root (GTK_WIDGET (self));

  file_dialog = gtk_file_dialog_new ();
  gtk_file_dialog_set_initial_file (file_dialog, self->file);
  gtk_file_dialog_set_default_filter (file_dialog, self->filter);

  gtk_file_dialog_open (file_dialog,
                        GTK_WINDOW (root),
                        self->cancellable,
                        on_file_opened_cb,
                        self);
}

static void
gcal_file_chooser_button_finalize (GObject *object)
{
  GcalFileChooserButton *self = (GcalFileChooserButton *)object;

  g_cancellable_cancel (self->cancellable);

  g_clear_pointer (&self->title, g_free);
  g_clear_object (&self->cancellable);
  g_clear_object (&self->filter);
  g_clear_object (&self->file);

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

static void
gcal_file_chooser_button_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  GcalFileChooserButton *self = GCAL_FILE_CHOOSER_BUTTON (object);

  switch (prop_id)
    {
    case PROP_FILE:
      g_value_set_object (value, self->file);
      break;

    case PROP_FILTER:
      g_value_set_object (value, self->filter);
      break;

    case PROP_TITLE:
      g_value_set_string (value, self->title);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gcal_file_chooser_button_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
  GcalFileChooserButton *self = GCAL_FILE_CHOOSER_BUTTON (object);

  switch (prop_id)
    {
    case PROP_FILE:
      gcal_file_chooser_button_set_file (self, g_value_get_object (value));
      break;

    case PROP_FILTER:
      gcal_file_chooser_button_set_filter (self, g_value_get_object (value));
      break;

    case PROP_TITLE:
      gcal_file_chooser_button_set_title (self, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gcal_file_chooser_button_class_init (GcalFileChooserButtonClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);

  button_class->clicked = gcal_file_chooser_button_clicked;

  object_class->finalize = gcal_file_chooser_button_finalize;
  object_class->get_property = gcal_file_chooser_button_get_property;
  object_class->set_property = gcal_file_chooser_button_set_property;

  properties[PROP_FILE] = g_param_spec_object ("file", "", "",
                                               G_TYPE_FILE,
                                               G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  properties[PROP_FILTER] = g_param_spec_object ("filter", "", "",
                                                 GTK_TYPE_FILE_FILTER,
                                                 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  properties[PROP_TITLE] = g_param_spec_string ("title", "", "", NULL,
                                                G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
gcal_file_chooser_button_init (GcalFileChooserButton *self)
{
  self->cancellable = g_cancellable_new ();

  update_label (self);
}

GtkWidget*
gcal_file_chooser_button_new (void)
{
  return g_object_new (GCAL_TYPE_FILE_CHOOSER_BUTTON, NULL);
}

void
gcal_file_chooser_button_set_file (GcalFileChooserButton *self,
                                   GFile                 *file)
{
  g_return_if_fail (GCAL_IS_FILE_CHOOSER_BUTTON (self));

  if (g_set_object (&self->file, file))
    {
      update_label (self);
      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FILE]);
    }
}

GFile *
gcal_file_chooser_button_get_file (GcalFileChooserButton *self)
{
  g_return_val_if_fail (GCAL_IS_FILE_CHOOSER_BUTTON (self), NULL);

  return self->file ? g_object_ref (self->file) : NULL;
}

void
gcal_file_chooser_button_set_title (GcalFileChooserButton *self,
                                    const gchar           *title)
{
  g_autofree gchar *old_title = NULL;

  g_return_if_fail (GCAL_IS_FILE_CHOOSER_BUTTON (self));

  old_title = g_steal_pointer (&self->title);
  self->title = g_strdup (title);

  update_label (self);

  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
}

const gchar*
gcal_file_chooser_button_get_title (GcalFileChooserButton *self)
{
  g_return_val_if_fail (GCAL_IS_FILE_CHOOSER_BUTTON (self), NULL);

  return self->title;
}

void
gcal_file_chooser_button_set_filter (GcalFileChooserButton *self,
                                     GtkFileFilter         *filter)
{
  g_return_if_fail (GCAL_IS_FILE_CHOOSER_BUTTON (self));
  g_return_if_fail (filter == NULL || GTK_IS_FILE_FILTER (filter));

  if (g_set_object (&self->filter, filter))
    g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FILTER]);
}