Remmina - The GTK+ Remote Desktop Client  v1.4.2
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-2020 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 <gtk/gtk.h>
38 #include <string.h>
39 #include "config.h"
40 #include "remmina_public.h"
41 #include "remmina_string_list.h"
43 
45 
46 #define COLUMN_DESCRIPTION 0
47 #define COLUMN_VALUE 1
48 #define GET_OBJECT(object_name) gtk_builder_get_object(string_list->builder, object_name)
49 
50 /* Update the buttons state on the items in the TreeModel */
52 {
53  gint items_count = gtk_tree_model_iter_n_children(
54  GTK_TREE_MODEL(string_list->liststore_items), NULL);
55 
56  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_remove), items_count > 0);
57  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_up), items_count > 1);
58  gtk_widget_set_sensitive(GTK_WIDGET(string_list->button_down), items_count > 1);
59 }
60 
61 /* Check the text inserted in the list */
62 void remmina_string_list_on_cell_edited(GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text)
63 {
64  TRACE_CALL(__func__);
65  gchar *text;
66  gchar *error;
67  GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
68  GtkTreeIter iter;
69 
70  gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &iter, path);
71  /* Remove delimitors from the string */
72  text = remmina_public_str_replace(new_text, STRING_DELIMITOR, " ");
73  if (cell == string_list->cellrenderertext_item1) {
74  gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_DESCRIPTION, text, -1);
75  }else {
76  /* Check for validation only in second field */
77  if (string_list->priv->validation_func) {
78  if (!((*string_list->priv->validation_func)(text, &error))) {
79  gtk_label_set_text(string_list->label_status, error);
80  gtk_widget_show(GTK_WIDGET(string_list->label_status));
81  g_free(error);
82  }else {
83  gtk_widget_hide(GTK_WIDGET(string_list->label_status));
84  }
85  }
86  gtk_list_store_set(string_list->liststore_items, &iter, COLUMN_VALUE, text, -1);
87  }
88  gtk_tree_path_free(path);
89  g_free(text);
90 }
91 
92 /* Move a TreeIter position */
93 static void remmina_string_list_move_iter(GtkTreeIter *from, GtkTreeIter *to)
94 {
95  TRACE_CALL(__func__);
96  GtkTreePath *path;
97 
98  gtk_list_store_swap(string_list->liststore_items, from, to);
99  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), from);
100  gtk_tree_view_scroll_to_cell(string_list->treeview_items, path, NULL, 0, 0, 0);
101  gtk_tree_path_free(path);
102 }
103 
104 /* Move down the selected TreeRow */
105 void remmina_string_list_on_action_down(GtkWidget *widget, gpointer user_data)
106 {
107  TRACE_CALL(__func__);
108  GtkTreeIter iter;
109  GtkTreeIter target_iter;
110 
111  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
112  gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
113  if (gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &target_iter)) {
114  remmina_string_list_move_iter(&iter, &target_iter);
115  }
116  }
117 }
118 
119 /* Move up the selected TreeRow */
120 void remmina_string_list_on_action_up(GtkWidget *widget, gpointer user_data)
121 {
122  TRACE_CALL(__func__);
123  GtkTreeIter iter;
124  GtkTreeIter target_iter;
125  GtkTreePath *path;
126 
127  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
128  gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &target_iter);
129  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &target_iter);
130  /* Before moving the TreeRow check if there’s a previous item */
131  if (gtk_tree_path_prev(path)) {
132  gtk_tree_model_get_iter(GTK_TREE_MODEL(string_list->liststore_items), &target_iter, path);
133  gtk_tree_path_free(path);
134  remmina_string_list_move_iter(&iter, &target_iter);
135  }
136  }
137 }
138 
139 /* Add a new TreeRow to the list */
140 void remmina_string_list_on_action_add(GtkWidget *widget, gpointer user_data)
141 {
142  TRACE_CALL(__func__);
143  GtkTreeIter iter;
144  GtkTreePath *path;
145 
146  gtk_list_store_append(string_list->liststore_items, &iter);
147  gtk_tree_selection_select_iter(string_list->treeview_selection, &iter);
148 
149  path = gtk_tree_model_get_path(GTK_TREE_MODEL(string_list->liststore_items), &iter);
150  gtk_tree_view_set_cursor_on_cell(string_list->treeview_items, path,
151  string_list->treeviewcolumn_item,
152  GTK_CELL_RENDERER(string_list->priv->two_columns ? string_list->cellrenderertext_item1 : string_list->cellrenderertext_item2),
153  TRUE);
154  gtk_tree_path_free(path);
156 }
157 
158 /* Remove the selected TreeRow from the list */
159 void remmina_string_list_on_action_remove(GtkWidget *widget, gpointer user_data)
160 {
161  TRACE_CALL(__func__);
162  GtkTreeIter iter;
163 
164  if (gtk_tree_selection_get_selected(string_list->treeview_selection, NULL, &iter)) {
165  gtk_list_store_remove(string_list->liststore_items, &iter);
166  }
167  gtk_widget_hide(GTK_WIDGET(string_list->label_status));
169 }
170 
171 /* Load a string list by splitting a string value */
172 void remmina_string_list_set_text(const gchar *text, const gboolean clear_data)
173 {
174  TRACE_CALL(__func__);
175  GtkTreeIter iter;
176  gchar **items;
177  gchar **values;
178  gint i;
179  /* Clear the data before to load new items */
180  if (clear_data)
181  gtk_list_store_clear(string_list->liststore_items);
182  /* Split the string and insert each snippet in the string list */
183  items = g_strsplit(text, STRING_DELIMITOR, -1);
184  for (i = 0; i < g_strv_length(items); i++) {
185  values = g_strsplit(items[i], string_list->priv->fields_separator, -1);
186  gtk_list_store_append(string_list->liststore_items, &iter);
187  if (g_strv_length(values) > 1) {
188  /* Two columns data */
189  gtk_list_store_set(string_list->liststore_items, &iter,
190  COLUMN_DESCRIPTION, values[0],
191  COLUMN_VALUE, values[1],
192  -1);
193  }else {
194  /* Single column data */
195  gtk_list_store_set(string_list->liststore_items, &iter,
196  COLUMN_VALUE, values[0],
197  -1);
198  }
199  g_strfreev(values);
200  }
201  g_strfreev(items);
203 }
204 
205 /* Get a string value representing the string list */
207 {
208  TRACE_CALL(__func__);
209  GString *str;
210  GtkTreeIter iter;
211  gboolean first;
212  gboolean ret;
213  const gchar *item_description;
214  const gchar *item_value;
215 
216  str = g_string_new(NULL);
217  first = TRUE;
218  /* Cycle each GtkTreeIter in the ListStore */
219  ret = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(string_list->liststore_items), &iter);
220  while (ret) {
221  gtk_tree_model_get(GTK_TREE_MODEL(string_list->liststore_items), &iter,
222  COLUMN_DESCRIPTION, &item_description,
223  COLUMN_VALUE, &item_value,
224  -1);
225  if (!item_description)
226  item_description = "";
227  if (item_value && strlen(item_value) > 0) {
228  /* Add a delimitor after the first element */
229  if (!first) {
230  g_string_append(str, STRING_DELIMITOR);
231  }else {
232  first = FALSE;
233  }
234  /* Add the description for two columns list */
235  if (string_list->priv->two_columns) {
236  g_string_append(str, item_description);
237  g_string_append(str, string_list->priv->fields_separator);
238  }
239  /* Add the element to the string */
240  g_string_append(str, item_value);
241  }
242  ret = gtk_tree_model_iter_next(GTK_TREE_MODEL(string_list->liststore_items), &iter);
243  }
244  return g_string_free(str, FALSE);
245 }
246 
247 /* Set a function that will be used to validate the new rows */
249 {
250  TRACE_CALL(__func__);
251  string_list->priv->validation_func = func;
252 }
253 
254 /* Set the dialog titles */
255 void remmina_string_list_set_titles(gchar *title1, gchar *title2)
256 {
257  /* Set dialog titlebar */
258  gtk_window_set_title(GTK_WINDOW(string_list->dialog),
259  (title1 && strlen(title1) > 0) ? title1 : "");
260  /* Set title label */
261  if (title2 && strlen(title2) > 0) {
262  gtk_label_set_text(string_list->label_title, title2);
263  gtk_widget_show(GTK_WIDGET(string_list->label_title));
264  }else {
265  gtk_widget_hide(GTK_WIDGET(string_list->label_title));
266  }
267 }
268 
269 /* RemminaStringList initialization */
270 static void remmina_string_list_init(void)
271 {
272  TRACE_CALL(__func__);
273  string_list->priv->validation_func = NULL;
274  /* When two columns are requested, show also the first column */
275  if (string_list->priv->two_columns)
276  gtk_cell_renderer_set_visible(GTK_CELL_RENDERER(string_list->cellrenderertext_item1), TRUE);
278 }
279 
280 /* RemminaStringList instance */
281 GtkDialog* remmina_string_list_new(gboolean two_columns, const gchar *fields_separator)
282 {
283  TRACE_CALL(__func__);
284  string_list = g_new0(RemminaStringList, 1);
285  string_list->priv = g_new0(RemminaStringListPriv, 1);
286 
287  string_list->builder = remmina_public_gtk_builder_new_from_file("remmina_string_list.glade");
288  string_list->dialog = GTK_DIALOG(gtk_builder_get_object(string_list->builder, "DialogStringList"));
289 
290  string_list->liststore_items = GTK_LIST_STORE(GET_OBJECT("liststore_items"));
291  string_list->treeview_items = GTK_TREE_VIEW(GET_OBJECT("treeview_items"));
292  string_list->treeviewcolumn_item = GTK_TREE_VIEW_COLUMN(GET_OBJECT("treeviewcolumn_item"));
293  string_list->treeview_selection = GTK_TREE_SELECTION(GET_OBJECT("treeview_selection"));
294  string_list->cellrenderertext_item1 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item1"));
295  string_list->cellrenderertext_item2 = GTK_CELL_RENDERER_TEXT(GET_OBJECT("cellrenderertext_item2"));
296  string_list->button_add = GTK_BUTTON(GET_OBJECT("button_add"));
297  string_list->button_remove = GTK_BUTTON(GET_OBJECT("button_remove"));
298  string_list->button_up = GTK_BUTTON(GET_OBJECT("button_up"));
299  string_list->button_down = GTK_BUTTON(GET_OBJECT("button_down"));
300  string_list->label_title = GTK_LABEL(GET_OBJECT("label_title"));
301  string_list->label_status = GTK_LABEL(GET_OBJECT("label_status"));
302 
303  /* Connect signals */
304  gtk_builder_connect_signals(string_list->builder, NULL);
305  /* Initialize the window and load the values */
306  if (!fields_separator)
307  fields_separator = STRING_DELIMITOR2;
308  string_list->priv->fields_separator = fields_separator;
309  string_list->priv->two_columns = two_columns;
311 
312  return string_list->dialog;
313 }
static RemminaStringList * string_list
GtkDialog * remmina_string_list_new(gboolean two_columns, const gchar *fields_separator)
RemminaStringListPriv * priv
void remmina_string_list_set_validation_func(RemminaStringListValidationFunc func)
GtkCellRendererText * cellrenderertext_item1
void remmina_string_list_on_action_remove(GtkWidget *widget, gpointer user_data)
static void remmina_string_list_init(void)
GtkBuilder * remmina_public_gtk_builder_new_from_file(gchar *filename)
RemminaStringListValidationFunc validation_func
GtkTreeViewColumn * treeviewcolumn_item
GtkTreeSelection * treeview_selection
GtkCellRendererText * cellrenderertext_item2
void remmina_string_list_on_action_down(GtkWidget *widget, gpointer user_data)
GtkTreeView * treeview_items
gboolean(* RemminaStringListValidationFunc)(const gchar *new_str, gchar **error)
gchar * remmina_public_str_replace(const gchar *string, const gchar *search, const gchar *replacement)
void remmina_string_list_set_text(const gchar *text, const gboolean clear_data)
void remmina_string_list_on_cell_edited(GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text)
void remmina_string_list_set_titles(gchar *title1, gchar *title2)
GtkListStore * liststore_items
void remmina_string_list_update_buttons_state(void)
void remmina_string_list_on_action_add(GtkWidget *widget, gpointer user_data)
static void remmina_string_list_move_iter(GtkTreeIter *from, GtkTreeIter *to)
gchar * remmina_string_list_get_text(void)
void remmina_string_list_on_action_up(GtkWidget *widget, gpointer user_data)