Remmina - The GTK+ Remote Desktop Client  v1.4.34
Remmina is a remote desktop client written in GTK+, aiming to be useful for system administrators and travellers, who need to work with lots of remote computers in front of either large monitors or tiny netbooks. Remmina supports multiple network protocols in an integrated and consistent user interface. Currently RDP, VNC, NX, XDMCP and SSH are supported.
remmina_string_list.c
Go to the documentation of this file.
1 /*
2  * Remmina - The GTK+ Remote Desktop Client
3  * Copyright (C) 2009 - Vic Lee
4  * Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
5  * Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * In addition, as a special exception, the copyright holders give
23  * permission to link the code of portions of this program with the
24  * OpenSSL library under certain conditions as described in each
25  * individual source file, and distribute linked combinations
26  * including the two.
27  * You must obey the GNU General Public License in all respects
28  * for all of the code used other than OpenSSL. * If you modify
29  * file(s) with this exception, you may extend this exception to your
30  * version of the file(s), but you are not obligated to do so. * If you
31  * do not wish to do so, delete this exception statement from your
32  * version. * If you delete this exception statement from all source
33  * files in the program, then also delete it here.
34  *
35  */
36 
37 #include <string.h>
38 #include "config.h"
39 #include "remmina_public.h"
40 #include "remmina_string_list.h"
42 
44 
45 #define COLUMN_DESCRIPTION 0
46 #define COLUMN_VALUE 1
47 #define GET_OBJECT(object_name) gtk_builder_get_object(string_list->builder, object_name)
48 
49 /* Update the buttons state on the items in the TreeModel */
51 {
52  gint items_count = gtk_tree_model_iter_n_children(
53  GTK_TREE_MODEL(string_list->liststore_items), NULL);
54 
55  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_remove), items_count > 0);
56  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_up), items_count > 1);
57  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_down), items_count > 1);
58 }
59 
60 /* Check the text inserted in the list */
61 void remmina_string_list_on_cell_edited(GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text)
62 {
63  TRACE_CALL(__func__);
64  gchar *text;
65  gchar *error;
66  GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
67  GtkTreeIter iter;
68 
69  gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &iter, path);
70  /* Remove delimitors from the string */
71  text = remmina_public_str_replace(new_text, STRING_DELIMITOR, " ");
72  if (cell == string_list->cellrenderertext_item1) {
73  gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_DESCRIPTION, text, -1);
74  }else {
75  /* Check for validation only in second field */
77  if (!((*string_list->priv->validation_func)(text, &error))) {
78  gtk_label_set_text(string_list->label_status, error);
79  gtk_widget_show(GTK_WIDGET(string_list->label_status));
80  g_free(error);
81  }else {
82  gtk_widget_hide(GTK_WIDGET(string_list->label_status));
83  }
84  }
85  gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_VALUE, text, -1);
86  }
87  gtk_tree_path_free(path);
88  g_free(text);
89 }
90 
91 /* Move a TreeIter position */
92 static void remmina_string_list_move_iter(GtkTreeIter *from, GtkTreeIter *to)
93 {
94  TRACE_CALL(__func__);
95  GtkTreePath *path;
96 
97  gtk_list_store_swap(string_list->liststore_items, from, to);
98  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), from);
99  gtk_tree_view_scroll_to_cell(string_list->treeview_items, path, NULL, 0, 0, 0);
100  gtk_tree_path_free(path);
101 }
102 
103 /* Move down the selected TreeRow */
104 void remmina_string_list_on_action_down(GtkWidget *widget, gpointer user_data)
105 {
106  TRACE_CALL(__func__);
107  GtkTreeIter iter;
108  GtkTreeIter target_iter;
109 
110  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
111  gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
112  if (gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &target_iter)) {
113  remmina_string_list_move_iter(&iter, &target_iter);
114  }
115  }
116 }
117 
118 /* Move up the selected TreeRow */
119 void remmina_string_list_on_action_up(GtkWidget *widget, gpointer user_data)
120 {
121  TRACE_CALL(__func__);
122  GtkTreeIter iter;
123  GtkTreeIter target_iter;
124  GtkTreePath *path;
125 
126  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
127  gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
128  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &target_iter);
129  /* Before moving the TreeRow check if there’s a previous item */
130  if (gtk_tree_path_prev(path)) {
131  gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &target_iter, path);
132  gtk_tree_path_free(path);
133  remmina_string_list_move_iter(&iter, &target_iter);
134  }
135  }
136 }
137 
138 /* Add a new TreeRow to the list */
139 void remmina_string_list_on_action_add(GtkWidget *widget, gpointer user_data)
140 {
141  TRACE_CALL(__func__);
142  GtkTreeIter iter;
143  GtkTreePath *path;
144 
145  gtk_list_store_append(string_list->liststore_items, &iter);
146  gtk_tree_selection_select_iter(string_list->treeview_selection, &iter);
147 
148  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &iter);
149  gtk_tree_view_set_cursor_on_cell(string_list->treeview_items, path,
152  TRUE);
153  gtk_tree_path_free(path);
155 }
156 
157 /* Remove the selected TreeRow from the list */
158 void remmina_string_list_on_action_remove(GtkWidget *widget, gpointer user_data)
159 {
160  TRACE_CALL(__func__);
161  GtkTreeIter iter;
162 
163  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
164  gtk_list_store_remove(string_list->liststore_items, &iter);
165  }
166  gtk_widget_hide(GTK_WIDGET(string_list->label_status));
168 }
169 
170 /* Load a string list by splitting a string value */
171 void remmina_string_list_set_text(const gchar *text, const gboolean clear_data)
172 {
173  TRACE_CALL(__func__);
174  GtkTreeIter iter;
175  gchar **items;
176  gchar **values;
177  gint i;
178  /* Clear the data before to load new items */
179  if (clear_data)
180  gtk_list_store_clear(string_list->liststore_items);
181  /* Split the string and insert each snippet in the string list */
182  items = g_strsplit(text, STRING_DELIMITOR, -1);
183  for (i = 0; i < g_strv_length(items); i++) {
184  values = g_strsplit(items[i], string_list->priv->fields_separator, -1);
185  gtk_list_store_append(string_list->liststore_items, &iter);
186  if (g_strv_length(values) > 1) {
187  /* Two columns data */
188  gtk_list_store_set(string_list->liststore_items, &iter,
189  COLUMN_DESCRIPTION, values[0],
190  COLUMN_VALUE, values[1],
191  -1);
192  }else {
193  /* Single column data */
194  gtk_list_store_set(string_list->liststore_items, &iter,
195  COLUMN_VALUE, values[0],
196  -1);
197  }
198  g_strfreev(values);
199  }
200  g_strfreev(items);
202 }
203 
204 /* Get a string value representing the string list */
206 {
207  TRACE_CALL(__func__);
208  GString *str;
209  GtkTreeIter iter;
210  gboolean first;
211  gboolean ret;
212  const gchar *item_description;
213  const gchar *item_value;
214 
215  str = g_string_new(NULL);
216  first = TRUE;
217  /* Cycle each GtkTreeIter in the ListStore */
218  ret = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(string_list->liststore_items), &iter);
219  while (ret) {
220  gtk_tree_model_get(GTK_TREE_MODEL(string_list->liststore_items), &iter,
221  COLUMN_DESCRIPTION, &item_description,
222  COLUMN_VALUE, &item_value,
223  -1);
224  if (!item_description)
225  item_description = "";
226  if (item_value && strlen(item_value) > 0) {
227  /* Add a delimitor after the first element */
228  if (!first) {
229  g_string_append(str, STRING_DELIMITOR);
230  }else {
231  first = FALSE;
232  }
233  /* Add the description for two columns list */
234  if (string_list->priv->two_columns) {
235  g_string_append(str, item_description);
236  g_string_append(str, string_list->priv->fields_separator);
237  }
238  /* Add the element to the string */
239  g_string_append(str, item_value);
240  }
241  ret = gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &iter);
242  }
243  return g_string_free(str, FALSE);
244 }
245 
246 /* Set a function that will be used to validate the new rows */
248 {
249  TRACE_CALL(__func__);
251 }
252 
253 /* Set the dialog titles */
254 void remmina_string_list_set_titles(gchar *title1, gchar *title2)
255 {
256  /* Set dialog titlebar */
257  gtk_window_set_title(GTK_WINDOW(string_list->dialog),
258  (title1 && strlen(title1) > 0) ? title1 : "");
259  /* Set title label */
260  if (title2 && strlen(title2) > 0) {
261  gtk_label_set_text(string_list->label_title, title2);
262  gtk_widget_show(GTK_WIDGET(string_list->label_title));
263  }else {
264  gtk_widget_hide(GTK_WIDGET(string_list->label_title));
265  }
266 }
267 
268 /* RemminaStringList initialization */
269 static void remmina_string_list_init(void)
270 {
271  TRACE_CALL(__func__);
273  /* When two columns are requested, show also the first column */
275  gtk_cell_renderer_set_visible(GTK_CELL_RENDERER(string_list->cellrenderertext_item1), TRUE);
277 }
278 
279 /* RemminaStringList instance */
280 GtkDialog* remmina_string_list_new(gboolean two_columns, const gchar *fields_separator)
281 {
282  TRACE_CALL(__func__);
283  string_list = g_new0(RemminaStringList, 1);
284  string_list->priv = g_new0(RemminaStringListPriv, 1);
285 
286  string_list->builder = remmina_public_gtk_builder_new_from_resource("/org/remmina/Remmina/src/../data/ui/remmina_string_list.glade");
287  string_list->dialog = GTK_DIALOG(gtk_builder_get_object(string_list->builder, "DialogStringList"));
288 
289  string_list->liststore_items = GTK_LIST_STORE(GET_OBJECT("liststore_items"));
290  string_list->treeview_items = GTK_TREE_VIEW(GET_OBJECT("treeview_items"));
291  string_list->treeviewcolumn_item = GTK_TREE_VIEW_COLUMN(GET_OBJECT("treeviewcolumn_item"));
292  string_list->treeview_selection = GTK_TREE_SELECTION(GET_OBJECT("treeview_selection"));
293  string_list->cellrenderertext_item1 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item1"));
294  string_list->cellrenderertext_item2 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item2"));
295  string_list->button_add = GTK_BUTTON(GET_OBJECT("button_add"));
296  string_list->button_remove = GTK_BUTTON(GET_OBJECT("button_remove"));
297  string_list->button_up = GTK_BUTTON(GET_OBJECT("button_up"));
298  string_list->button_down = GTK_BUTTON(GET_OBJECT("button_down"));
299  string_list->label_title = GTK_LABEL(GET_OBJECT("label_title"));
300  string_list->label_status = GTK_LABEL(GET_OBJECT("label_status"));
301 
302  /* Connect signals */
303  gtk_builder_connect_signals(string_list->builder, NULL);
304  /* Initialize the window and load the values */
305  if (!fields_separator)
306  fields_separator = STRING_DELIMITOR2;
307  string_list->priv->fields_separator = fields_separator;
308  string_list->priv->two_columns = two_columns;
310 
311  return string_list->dialog;
312 }
GtkBuilder * remmina_public_gtk_builder_new_from_resource(gchar *resource)
gchar * remmina_public_str_replace(const gchar *string, const gchar *search, const gchar *replacement)
GtkDialog * remmina_string_list_new(gboolean two_columns, const gchar *fields_separator)
void remmina_string_list_on_action_up(GtkWidget *widget, gpointer user_data)
static void remmina_string_list_init(void)
void remmina_string_list_on_action_remove(GtkWidget *widget, gpointer user_data)
void remmina_string_list_on_action_add(GtkWidget *widget, gpointer user_data)
void remmina_string_list_set_validation_func(RemminaStringListValidationFunc func)
static void remmina_string_list_move_iter(GtkTreeIter *from, GtkTreeIter *to)
static RemminaStringList * string_list
gchar * remmina_string_list_get_text(void)
void remmina_string_list_update_buttons_state(void)
void remmina_string_list_set_titles(gchar *title1, gchar *title2)
void remmina_string_list_set_text(const gchar *text, const gboolean clear_data)
void remmina_string_list_on_action_down(GtkWidget *widget, gpointer user_data)
void remmina_string_list_on_cell_edited(GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text)
gboolean(* RemminaStringListValidationFunc)(const gchar *new_str, gchar **error)
GtkTreeView * treeview_items
GtkTreeViewColumn * treeviewcolumn_item
GtkCellRendererText * cellrenderertext_item1
RemminaStringListPriv * priv
GtkCellRendererText * cellrenderertext_item2
GtkTreeSelection * treeview_selection
GtkListStore * liststore_items
RemminaStringListValidationFunc validation_func