1 /* gtd-initial-setup-window.c
2  *
3  * Copyright (C) 2015-2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #define G_LOG_DOMAIN "GtdInitialSetupWindow"
20 
21 #include "gtd-provider.h"
22 #include "gtd-application.h"
23 #include "gtd-provider-selector.h"
24 #include "gtd-initial-setup-window.h"
25 #include "gtd-manager.h"
26 
27 #include <glib/gi18n.h>
28 
29 typedef struct
30 {
31   GtkWidget                 *cancel_button;
32   GtkWidget                 *done_button;
33   GtkWidget                 *storage_selector;
34 
35   GtdManager                *manager;
36 } GtdInitialSetupWindowPrivate;
37 
38 struct _GtdInitialSetupWindow
39 {
40   GtkApplicationWindow          parent;
41 
42   /*<private>*/
43   GtdInitialSetupWindowPrivate *priv;
44 };
45 
46 G_DEFINE_TYPE_WITH_PRIVATE (GtdInitialSetupWindow, gtd_initial_setup_window, GTK_TYPE_APPLICATION_WINDOW)
47 
48 enum {
49   PROP_0,
50   PROP_MANAGER,
51   LAST_PROP
52 };
53 
54 enum {
55   CANCEL,
56   DONE,
57   LAST_SIGNAL
58 };
59 
60 static guint signals[LAST_SIGNAL] = { 0, };
61 
62 static void
gtd_initial_setup_window__location_selected(GtdInitialSetupWindow * window,GtdProvider * provider)63 gtd_initial_setup_window__location_selected (GtdInitialSetupWindow *window,
64                                              GtdProvider           *provider)
65 {
66   GtdInitialSetupWindowPrivate *priv;
67 
68   g_return_if_fail (GTD_IS_INITIAL_SETUP_WINDOW (window));
69 
70   priv = window->priv;
71 
72   gtk_widget_set_sensitive (priv->done_button, provider != NULL);
73 
74   if (provider)
75     gtd_manager_set_default_provider (priv->manager, provider);
76 }
77 
78 static void
gtd_initial_setup_window__button_clicked(GtdInitialSetupWindow * window,GtkWidget * button)79 gtd_initial_setup_window__button_clicked (GtdInitialSetupWindow *window,
80                                           GtkWidget             *button)
81 {
82   GtdInitialSetupWindowPrivate *priv;
83 
84   g_return_if_fail (GTD_IS_INITIAL_SETUP_WINDOW (window));
85 
86   priv = window->priv;
87 
88   if (button == priv->cancel_button)
89     {
90       g_signal_emit (window,
91                      signals[CANCEL],
92                      0);
93     }
94   else if (button == priv->done_button)
95     {
96       g_signal_emit (window,
97                      signals[DONE],
98                      0);
99     }
100 }
101 
102 static void
gtd_initial_setup_window_finalize(GObject * object)103 gtd_initial_setup_window_finalize (GObject *object)
104 {
105   G_OBJECT_CLASS (gtd_initial_setup_window_parent_class)->finalize (object);
106 }
107 
108 static void
gtd_initial_setup_window_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)109 gtd_initial_setup_window_get_property (GObject    *object,
110                                        guint       prop_id,
111                                        GValue     *value,
112                                        GParamSpec *pspec)
113 {
114   GtdInitialSetupWindow *self = GTD_INITIAL_SETUP_WINDOW (object);
115 
116   switch (prop_id)
117     {
118     case PROP_MANAGER:
119       g_value_set_object (value, self->priv->manager);
120       break;
121 
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124     }
125 }
126 
127 static void
gtd_initial_setup_window_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)128 gtd_initial_setup_window_set_property (GObject      *object,
129                                        guint         prop_id,
130                                        const GValue *value,
131                                        GParamSpec   *pspec)
132 {
133   GtdInitialSetupWindow *self = GTD_INITIAL_SETUP_WINDOW (object);
134 
135   switch (prop_id)
136     {
137     case PROP_MANAGER:
138       self->priv->manager = g_value_get_object (value);
139       g_object_notify (object, "manager");
140       break;
141 
142     default:
143       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144     }
145 }
146 
147 static void
gtd_initial_setup_window_constructed(GObject * object)148 gtd_initial_setup_window_constructed (GObject *object)
149 {
150   GtdInitialSetupWindowPrivate *priv;
151 
152   G_OBJECT_CLASS (gtd_initial_setup_window_parent_class)->constructed (object);
153 
154   priv = GTD_INITIAL_SETUP_WINDOW (object)->priv;
155 
156   g_object_bind_property (object,
157                           "manager",
158                           priv->storage_selector,
159                           "manager",
160                           G_BINDING_DEFAULT);
161 }
162 
163 static void
gtd_initial_setup_window_class_init(GtdInitialSetupWindowClass * klass)164 gtd_initial_setup_window_class_init (GtdInitialSetupWindowClass *klass)
165 {
166   GObjectClass *object_class = G_OBJECT_CLASS (klass);
167   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
168 
169   object_class->finalize = gtd_initial_setup_window_finalize;
170   object_class->constructed = gtd_initial_setup_window_constructed;
171   object_class->get_property = gtd_initial_setup_window_get_property;
172   object_class->set_property = gtd_initial_setup_window_set_property;
173 
174   /**
175    * GtdInitialSetupWindow::cancel:
176    *
177    * Emitted when the Cancel button is clicked. Should quit the
178    * application.
179    */
180   signals[CANCEL] = g_signal_new ("cancel",
181                                   GTD_TYPE_INITIAL_SETUP_WINDOW,
182                                   G_SIGNAL_RUN_LAST,
183                                   0,
184                                   NULL,
185                                   NULL,
186                                   NULL,
187                                   G_TYPE_NONE,
188                                   0);
189 
190   /**
191    * GtdInitialSetupWindow::done:
192    *
193    * Emitted when the Done button is clicked. Should start the
194    * application.
195    */
196   signals[DONE] = g_signal_new ("done",
197                                 GTD_TYPE_INITIAL_SETUP_WINDOW,
198                                 G_SIGNAL_RUN_LAST,
199                                 0,
200                                 NULL,
201                                 NULL,
202                                 NULL,
203                                 G_TYPE_NONE,
204                                 0);
205 
206   /**
207    * GtdInitialSetupWindow::manager:
208    *
209    * Manager of the application.
210    */
211   g_object_class_install_property (
212         object_class,
213         PROP_MANAGER,
214         g_param_spec_object ("manager",
215                              "Manager of the task",
216                              "The singleton manager instance of the task",
217                              GTD_TYPE_MANAGER,
218                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
219 
220   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/todo/ui/gtd-initial-setup.ui");
221 
222   gtk_widget_class_bind_template_child_private (widget_class, GtdInitialSetupWindow, cancel_button);
223   gtk_widget_class_bind_template_child_private (widget_class, GtdInitialSetupWindow, done_button);
224   gtk_widget_class_bind_template_child_private (widget_class, GtdInitialSetupWindow, storage_selector);
225 
226   gtk_widget_class_bind_template_callback (widget_class, gtd_initial_setup_window__button_clicked);
227   gtk_widget_class_bind_template_callback (widget_class, gtd_initial_setup_window__location_selected);
228 }
229 
230 static void
gtd_initial_setup_window_init(GtdInitialSetupWindow * self)231 gtd_initial_setup_window_init (GtdInitialSetupWindow *self)
232 {
233   self->priv = gtd_initial_setup_window_get_instance_private (self);
234 
235   gtk_widget_init_template (GTK_WIDGET (self));
236 }
237 
238 GtkWidget*
gtd_initial_setup_window_new(GtdApplication * application)239 gtd_initial_setup_window_new (GtdApplication *application)
240 {
241   g_return_val_if_fail (GTD_IS_APPLICATION (application), NULL);
242 
243   return g_object_new (GTD_TYPE_INITIAL_SETUP_WINDOW,
244                        "application", application,
245                        "manager", gtd_manager_get_default (),
246                        NULL);
247 }
248