summaryrefslogtreecommitdiff
path: root/pidgin/pidginstatuseditor.c
blob: 3b878997f5caa8fb6d4c19470d7eeeff0043693e (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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 2 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 <https://www.gnu.org/licenses/>.
 */

#include <talkatu/talkatu.h>

#include "pidginstatuseditor.h"

#include "pidginstatusprimitivechooser.h"

enum {
	PROP_0,
	PROP_STATUS,
	N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

enum {
	RESPONSE_USE,
	RESPONSE_SAVE
};

struct _PidginStatusEditor {
	GtkDialog parent;

	PurpleSavedStatus *status;

	GtkTextBuffer *buffer;

	GtkWidget *title;
	GtkWidget *primitive;
};

G_DEFINE_TYPE(PidginStatusEditor, pidgin_status_editor, GTK_TYPE_DIALOG)

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
pidgin_status_editor_set_status(PidginStatusEditor *editor,
                                PurpleSavedStatus *status)
{
	PurpleStatusPrimitive primitive;
	PidginStatusPrimitiveChooser *chooser = NULL;
	const gchar *title = NULL, *message = NULL;

	editor->status = status;

	if(editor->status != NULL) {
		title = purple_savedstatus_get_title(editor->status);
		primitive = purple_savedstatus_get_primitive_type(editor->status);
		message = purple_savedstatus_get_message(editor->status);
	} else {
		primitive = PURPLE_STATUS_AWAY;
	}

	if(title == NULL) {
		title = "";
	}

	if(message == NULL) {
		message = "";
	}

	gtk_editable_set_text(GTK_EDITABLE(editor->title), title);
	chooser = PIDGIN_STATUS_PRIMITIVE_CHOOSER(editor->primitive);
	pidgin_status_primitive_chooser_set_selected(chooser, primitive);
	talkatu_markup_set_html(TALKATU_BUFFER(editor->buffer), message, -1);

	g_object_notify_by_pspec(G_OBJECT(editor), properties[PROP_STATUS]);
}

static void
pidgin_status_editor_save_status(PidginStatusEditor *editor) {
	PidginStatusPrimitiveChooser *chooser = NULL;
	PurpleStatusPrimitive primitive;
	gchar *message = NULL;
	const gchar *title = NULL;

	title = gtk_editable_get_text(GTK_EDITABLE(editor->title));

	chooser = PIDGIN_STATUS_PRIMITIVE_CHOOSER(editor->primitive);
	primitive = pidgin_status_primitive_chooser_get_selected(chooser);

	message = talkatu_markup_get_html(editor->buffer, NULL);

	if(editor->status == NULL) {
		editor->status = purple_savedstatus_new(title, primitive);
	} else {
		const gchar *current_title = NULL;

		/* purple_savedstatus_set_title throws a warning if you try to save a
		 * status with an existing title, which means we can't just save the
		 * title if it hasn't changed.
		 */
		current_title = purple_savedstatus_get_title(editor->status);
		if(!purple_strequal(title, current_title)) {
			purple_savedstatus_set_title(editor->status, title);
		}

		purple_savedstatus_set_primitive_type(editor->status, primitive);
	}

	purple_savedstatus_set_message(editor->status, message);

	g_free(message);
}

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
pidgin_status_editor_response_cb(GtkDialog *dialog, gint response_id,
                                 gpointer data)
{
	PidginStatusEditor *editor = data;

	switch(response_id) {
		case RESPONSE_USE:
			pidgin_status_editor_save_status(editor);
			purple_savedstatus_activate(editor->status);
			break;
		case RESPONSE_SAVE:
			pidgin_status_editor_save_status(editor);
			break;
	}

	gtk_window_destroy(GTK_WINDOW(dialog));
}

static void
pidgin_status_editor_title_changed_cb(G_GNUC_UNUSED GtkEditable *editable,
                                      gpointer data)
{
	PidginStatusEditor *editor = data;
	gboolean title_changed = FALSE, sensitive = FALSE;
	const gchar *title = NULL;

	title = gtk_editable_get_text(GTK_EDITABLE(editor->title));

	if(editor->status != NULL) {
		/* If we're editing a status, check if the title is the same. */
		title_changed = !purple_strequal(title,
		                                 purple_savedstatus_get_title(editor->status));
	} else {
		/* If this is a new status, check if the title is empty or not. */
		title_changed = !purple_strequal(title, "");
	}

	if(title_changed) {
		gboolean duplicated = purple_savedstatus_find(title) != NULL;

		if(duplicated) {
			gtk_dialog_set_response_sensitive(GTK_DIALOG(editor),
			                                  RESPONSE_USE, FALSE);
			gtk_dialog_set_response_sensitive(GTK_DIALOG(editor),
			                                  RESPONSE_SAVE, FALSE);

			return;
		}
	}

	sensitive = !purple_strequal(title, "");

	gtk_dialog_set_response_sensitive(GTK_DIALOG(editor), RESPONSE_USE,
	                                  sensitive);
	gtk_dialog_set_response_sensitive(GTK_DIALOG(editor), RESPONSE_SAVE,
	                                  sensitive);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
static void
pidgin_status_editor_get_property(GObject *obj, guint param_id, GValue *value,
                                  GParamSpec *pspec)
{
	PidginStatusEditor *editor = PIDGIN_STATUS_EDITOR(obj);

	switch(param_id) {
		case PROP_STATUS:
			g_value_set_pointer(value,
			                    pidgin_status_editor_get_status(editor));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
pidgin_status_editor_set_property(GObject *obj, guint param_id,
                                  const GValue *value, GParamSpec *pspec)
{
	PidginStatusEditor *editor = PIDGIN_STATUS_EDITOR(obj);

	switch(param_id) {
		case PROP_STATUS:
			pidgin_status_editor_set_status(editor,
			                                g_value_get_pointer(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
pidgin_status_editor_init(PidginStatusEditor *manager) {
	gtk_widget_init_template(GTK_WIDGET(manager));
}

static void
pidgin_status_editor_class_init(PidginStatusEditorClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	obj_class->get_property = pidgin_status_editor_get_property;
	obj_class->set_property = pidgin_status_editor_set_property;

	/**
	 * PidginStatusEditor:status:
	 *
	 * The [type@Purple.SavedStatus] that this editor is responsible for.
	 * This may be %NULL if it is creating a new status.
	 *
	 * Since: 3.0.0.
	 */
	/* we don't used boxed here because of the way status are currently
	 * managed.
	 */
	properties[PROP_STATUS] = g_param_spec_pointer(
		"status", "status",
		"The saved status we're editing",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	gtk_widget_class_set_template_from_resource(
		widget_class,
		"/im/pidgin/Pidgin3/Status/editor.ui"
	);

	gtk_widget_class_bind_template_child(widget_class, PidginStatusEditor,
	                                     buffer);

	gtk_widget_class_bind_template_child(widget_class, PidginStatusEditor,
	                                     title);
	gtk_widget_class_bind_template_child(widget_class, PidginStatusEditor,
	                                     primitive);

	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_status_editor_response_cb);
	gtk_widget_class_bind_template_callback(widget_class,
	                                        pidgin_status_editor_title_changed_cb);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
GtkWidget *
pidgin_status_editor_new(PurpleSavedStatus *status) {
	return g_object_new(
		PIDGIN_TYPE_STATUS_EDITOR,
		"status", status,
		NULL);
}

PurpleSavedStatus *
pidgin_status_editor_get_status(PidginStatusEditor *editor) {
	g_return_val_if_fail(PIDGIN_IS_STATUS_EDITOR(editor), NULL);

	return editor->status;
}