~tristan957/tllt-cp

ref: 03ccb981a2fb2121f66c1ab73ec3189685ad11b0 tllt-cp/src/tllt-cp-new-recipe-window.c -rw-r--r-- 4.6 KiB
03ccb981 — Tristan Partin Make the recipe UI work. Everying not toast 1 year, 6 months ago
                                                                                
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
#include <glib-object.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "tllt-cp-new-recipe-window.h"
#include "tllt-cp.h"

struct _TlltCpNewRecipeWindow
{
	GtkWindow parent_instance;
};

typedef struct TlltCpNewRecipeWindowPrivate
{
	GtkEntry *recipe_name_entry;
	GtkComboBoxText *recipe_type_combo;

	TlltCpClient *client;
	TlltCpUser *author;
} TlltCpNewRecipeWindowPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(TlltCpNewRecipeWindow, tllt_cp_new_recipe_window, GTK_TYPE_WINDOW)

typedef enum TlltCpNewRecipeWindowProps
{
	PROP_CLIENT = 1,
	PROP_AUTHOR,
	N_PROPS,
} TlltCpNewRecipeWindowProps;

static GParamSpec *obj_properties[N_PROPS];

static void
tllt_cp_new_recipe_window_finalize(GObject *obj)
{
	TlltCpNewRecipeWindow *self		   = TLLT_CP_NEW_RECIPE_WINDOW(obj);
	TlltCpNewRecipeWindowPrivate *priv = tllt_cp_new_recipe_window_get_instance_private(self);

	g_object_unref(priv->author);
	g_object_unref(priv->client);

	G_OBJECT_CLASS(tllt_cp_new_recipe_window_parent_class)->finalize(obj);
}

static void
tllt_cp_new_recipe_window_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *pspec)
{
	TlltCpNewRecipeWindow *self		   = TLLT_CP_NEW_RECIPE_WINDOW(obj);
	TlltCpNewRecipeWindowPrivate *priv = tllt_cp_new_recipe_window_get_instance_private(self);

	switch (prop_id) {
	case PROP_CLIENT:
		g_value_set_object(val, priv->client);
		break;
	case PROP_AUTHOR:
		g_value_set_object(val, priv->author);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
tllt_cp_new_recipe_window_set_property(GObject *obj, guint prop_id, const GValue *val,
									   GParamSpec *pspec)
{
	TlltCpNewRecipeWindow *self		   = TLLT_CP_NEW_RECIPE_WINDOW(obj);
	TlltCpNewRecipeWindowPrivate *priv = tllt_cp_new_recipe_window_get_instance_private(self);

	switch (prop_id) {
	case PROP_CLIENT:
		if (priv->client != NULL) {
			g_object_unref(priv->client);
		}
		priv->client = g_value_dup_object(val);
		break;
	case PROP_AUTHOR:
		if (priv->author != NULL) {
			g_object_unref(priv->author);
		}
		priv->author = g_value_dup_object(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
on_cancel_button_clicked(G_GNUC_UNUSED GtkButton *widget, gpointer user_data)
{
	TlltCpNewRecipeWindow *self = TLLT_CP_NEW_RECIPE_WINDOW(user_data);

	gtk_window_close(GTK_WINDOW(self));
}

static void
on_create_recipe_button_clicked(G_GNUC_UNUSED GtkButton *widget, gpointer user_data)
{
	TlltCpNewRecipeWindow *self		   = TLLT_CP_NEW_RECIPE_WINDOW(user_data);
	TlltCpNewRecipeWindowPrivate *priv = tllt_cp_new_recipe_window_get_instance_private(self);

	g_autoptr(GError) err = NULL;
	tllt_cp_user_add_recipe(priv->author, priv->client, gtk_entry_get_text(priv->recipe_name_entry),
							gtk_combo_box_get_active(GTK_COMBO_BOX(priv->recipe_type_combo)), &err);

	// TODO: do something meaningful with error
	if (err != NULL) {
		g_printerr("%s\n", err->message);
	}

	gtk_window_close(GTK_WINDOW(self));
}

static void
tllt_cp_new_recipe_window_class_init(TlltCpNewRecipeWindowClass *klass)
{
	GObjectClass *obj_class   = G_OBJECT_CLASS(klass);
	GtkWidgetClass *wid_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize		= tllt_cp_new_recipe_window_finalize;
	obj_class->get_property = tllt_cp_new_recipe_window_get_property;
	obj_class->set_property = tllt_cp_new_recipe_window_set_property;

	obj_properties[PROP_CLIENT] = g_param_spec_object(
		"client", _("Client"), _("Client to make requests with"), TLLT_CP_TYPE_CLIENT,
		G_PARAM_PRIVATE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
	obj_properties[PROP_AUTHOR] =
		g_param_spec_object("author", _("Author"), _("Author of the recipe"), TLLT_CP_TYPE_USER,
							G_PARAM_PRIVATE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);

	gtk_widget_class_set_template_from_resource(
		wid_class, "/ht/sr/git/tristan957/tllt-cp/ui/tllt-cp-new-recipe-window.ui");
	gtk_widget_class_bind_template_child_private(wid_class, TlltCpNewRecipeWindow,
												 recipe_name_entry);
	gtk_widget_class_bind_template_child_private(wid_class, TlltCpNewRecipeWindow,
												 recipe_type_combo);
	gtk_widget_class_bind_template_callback(wid_class, on_cancel_button_clicked);
	gtk_widget_class_bind_template_callback(wid_class, on_create_recipe_button_clicked);
}

static void
tllt_cp_new_recipe_window_init(TlltCpNewRecipeWindow *self)
{
	gtk_widget_init_template(GTK_WIDGET(self));
}

TlltCpNewRecipeWindow *
tllt_cp_new_recipe_window_new(GtkWindow *parent, TlltCpClient *client, TlltCpUser *author)
{
	return g_object_new(TLLT_CP_TYPE_NEW_RECIPE_WINDOW, "transient-for", parent, "client", client,
						"author", author, NULL);
}