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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2009 - Vic Lee
* Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
* Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. * If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. * If you
* do not wish to do so, delete this exception statement from your
* version. * If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include <string.h>
#include "config.h"
#include "remmina_public.h"
#include "remmina_string_list.h"
#include "remmina/remmina_trace_calls.h"
static RemminaStringList *string_list;
#define COLUMN_DESCRIPTION 0
#define COLUMN_VALUE 1
#define GET_OBJECT(object_name) gtk_builder_get_object(string_list->builder, object_name)
/* Update the buttons state on the items in the TreeModel */
void remmina_string_list_update_buttons_state(void)
{
gint items_count = gtk_tree_model_iter_n_children(
GTK_TREE_MODEL(string_list->liststore_items), NULL);
gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_remove), items_count > 0);
gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_up), items_count > 1);
gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_down), items_count > 1);
}
/* Check the text inserted in the list */
void remmina_string_list_on_cell_edited(GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text)
{
TRACE_CALL(__func__);
gchar *text;
gchar *error;
GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
GtkTreeIter iter;
gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &iter, path);
/* Remove delimitors from the string */
text = remmina_public_str_replace(new_text, STRING_DELIMITOR, " ");
if (cell == string_list->cellrenderertext_item1) {
gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_DESCRIPTION, text, -1);
}else {
/* Check for validation only in second field */
if (string_list->priv->validation_func) {
if (!((*string_list->priv->validation_func)(text, &error))) {
gtk_label_set_text(string_list->label_status, error);
gtk_widget_show(GTK_WIDGET(string_list->label_status));
g_free(error);
}else {
gtk_widget_hide(GTK_WIDGET(string_list->label_status));
}
}
gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_VALUE, text, -1);
}
gtk_tree_path_free(path);
g_free(text);
}
/* Move a TreeIter position */
static void remmina_string_list_move_iter(GtkTreeIter *from, GtkTreeIter *to)
{
TRACE_CALL(__func__);
GtkTreePath *path;
gtk_list_store_swap(string_list->liststore_items, from, to);
path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), from);
gtk_tree_view_scroll_to_cell(string_list->treeview_items, path, NULL, 0, 0, 0);
gtk_tree_path_free(path);
}
/* Move down the selected TreeRow */
void remmina_string_list_on_action_down(GtkWidget *widget, gpointer user_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
GtkTreeIter target_iter;
if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
if (gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &target_iter)) {
remmina_string_list_move_iter(&iter, &target_iter);
}
}
}
/* Move up the selected TreeRow */
void remmina_string_list_on_action_up(GtkWidget *widget, gpointer user_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
GtkTreeIter target_iter;
GtkTreePath *path;
if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &target_iter);
/* Before moving the TreeRow check if there’s a previous item */
if (gtk_tree_path_prev(path)) {
gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &target_iter, path);
gtk_tree_path_free(path);
remmina_string_list_move_iter(&iter, &target_iter);
}
}
}
/* Add a new TreeRow to the list */
void remmina_string_list_on_action_add(GtkWidget *widget, gpointer user_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
GtkTreePath *path;
gtk_list_store_append(string_list->liststore_items, &iter);
gtk_tree_selection_select_iter(string_list->treeview_selection, &iter);
path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &iter);
gtk_tree_view_set_cursor_on_cell(string_list->treeview_items, path,
string_list->treeviewcolumn_item,
GTK_CELL_RENDERER(string_list->priv->two_columns ? string_list->cellrenderertext_item1 : string_list->cellrenderertext_item2),
TRUE);
gtk_tree_path_free(path);
remmina_string_list_update_buttons_state();
}
/* Remove the selected TreeRow from the list */
void remmina_string_list_on_action_remove(GtkWidget *widget, gpointer user_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
gtk_list_store_remove(string_list->liststore_items, &iter);
}
gtk_widget_hide(GTK_WIDGET(string_list->label_status));
remmina_string_list_update_buttons_state();
}
/* Load a string list by splitting a string value */
void remmina_string_list_set_text(const gchar *text, const gboolean clear_data)
{
TRACE_CALL(__func__);
GtkTreeIter iter;
gchar **items;
gchar **values;
gint i;
/* Clear the data before to load new items */
if (clear_data)
gtk_list_store_clear(string_list->liststore_items);
/* Split the string and insert each snippet in the string list */
items = g_strsplit(text, STRING_DELIMITOR, -1);
for (i = 0; i < g_strv_length(items); i++) {
values = g_strsplit(items[i], string_list->priv->fields_separator, -1);
gtk_list_store_append(string_list->liststore_items, &iter);
if (g_strv_length(values) > 1) {
/* Two columns data */
gtk_list_store_set(string_list->liststore_items, &iter,
COLUMN_DESCRIPTION, values[0],
COLUMN_VALUE, values[1],
-1);
}else {
/* Single column data */
gtk_list_store_set(string_list->liststore_items, &iter,
COLUMN_VALUE, values[0],
-1);
}
g_strfreev(values);
}
g_strfreev(items);
remmina_string_list_update_buttons_state();
}
/* Get a string value representing the string list */
gchar* remmina_string_list_get_text(void)
{
TRACE_CALL(__func__);
GString *str;
GtkTreeIter iter;
gboolean first;
gboolean ret;
const gchar *item_description;
const gchar *item_value;
str = g_string_new(NULL);
first = TRUE;
/* Cycle each GtkTreeIter in the ListStore */
ret = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(string_list->liststore_items), &iter);
while (ret) {
gtk_tree_model_get(GTK_TREE_MODEL(string_list->liststore_items), &iter,
COLUMN_DESCRIPTION, &item_description,
COLUMN_VALUE, &item_value,
-1);
if (!item_description)
item_description = "";
if (item_value && strlen(item_value) > 0) {
/* Add a delimitor after the first element */
if (!first) {
g_string_append(str, STRING_DELIMITOR);
}else {
first = FALSE;
}
/* Add the description for two columns list */
if (string_list->priv->two_columns) {
g_string_append(str, item_description);
g_string_append(str, string_list->priv->fields_separator);
}
/* Add the element to the string */
g_string_append(str, item_value);
}
ret = gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &iter);
}
return g_string_free(str, FALSE);
}
/* Set a function that will be used to validate the new rows */
void remmina_string_list_set_validation_func(RemminaStringListValidationFunc func)
{
TRACE_CALL(__func__);
string_list->priv->validation_func = func;
}
/* Set the dialog titles */
void remmina_string_list_set_titles(gchar *title1, gchar *title2)
{
/* Set dialog titlebar */
gtk_window_set_title(GTK_WINDOW(string_list->dialog),
(title1 && strlen(title1) > 0) ? title1 : "");
/* Set title label */
if (title2 && strlen(title2) > 0) {
gtk_label_set_text(string_list->label_title, title2);
gtk_widget_show(GTK_WIDGET(string_list->label_title));
}else {
gtk_widget_hide(GTK_WIDGET(string_list->label_title));
}
}
/* RemminaStringList initialization */
static void remmina_string_list_init(void)
{
TRACE_CALL(__func__);
string_list->priv->validation_func = NULL;
/* When two columns are requested, show also the first column */
if (string_list->priv->two_columns)
gtk_cell_renderer_set_visible(GTK_CELL_RENDERER(string_list->cellrenderertext_item1), TRUE);
remmina_string_list_update_buttons_state();
}
/* RemminaStringList instance */
GtkDialog* remmina_string_list_new(gboolean two_columns, const gchar *fields_separator)
{
TRACE_CALL(__func__);
string_list = g_new0(RemminaStringList, 1);
string_list->priv = g_new0(RemminaStringListPriv, 1);
string_list->builder = remmina_public_gtk_builder_new_from_resource("/org/remmina/Remmina/src/../data/ui/remmina_string_list.glade");
string_list->dialog = GTK_DIALOG(gtk_builder_get_object(string_list->builder, "DialogStringList"));
string_list->liststore_items = GTK_LIST_STORE(GET_OBJECT("liststore_items"));
string_list->treeview_items = GTK_TREE_VIEW(GET_OBJECT("treeview_items"));
string_list->treeviewcolumn_item = GTK_TREE_VIEW_COLUMN(GET_OBJECT("treeviewcolumn_item"));
string_list->treeview_selection = GTK_TREE_SELECTION(GET_OBJECT("treeview_selection"));
string_list->cellrenderertext_item1 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item1"));
string_list->cellrenderertext_item2 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item2"));
string_list->button_add = GTK_BUTTON(GET_OBJECT("button_add"));
string_list->button_remove = GTK_BUTTON(GET_OBJECT("button_remove"));
string_list->button_up = GTK_BUTTON(GET_OBJECT("button_up"));
string_list->button_down = GTK_BUTTON(GET_OBJECT("button_down"));
string_list->label_title = GTK_LABEL(GET_OBJECT("label_title"));
string_list->label_status = GTK_LABEL(GET_OBJECT("label_status"));
/* Connect signals */
gtk_builder_connect_signals(string_list->builder, NULL);
/* Initialize the window and load the values */
if (!fields_separator)
fields_separator = STRING_DELIMITOR2;
string_list->priv->fields_separator = fields_separator;
string_list->priv->two_columns = two_columns;
remmina_string_list_init();
return string_list->dialog;
}
|