1 /*
2 * Remmina - The GTK+ Remote Desktop Client
3 * Copyright (C) 2009-2010 Vic Lee
4 * Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
5 * Copyright (C) 2016-2021 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 <glib/gi18n.h>
39 #include <glib/gprintf.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include "remmina_applet_menu_item.h"
43 #include "remmina/remmina_trace_calls.h"
44
G_DEFINE_TYPE(RemminaAppletMenuItem,remmina_applet_menu_item,GTK_TYPE_MENU_ITEM)45 G_DEFINE_TYPE( RemminaAppletMenuItem, remmina_applet_menu_item, GTK_TYPE_MENU_ITEM)
46
47 #define IS_EMPTY(s) ((!s) || (s[0] == 0))
48
49 static void remmina_applet_menu_item_destroy(RemminaAppletMenuItem* item, gpointer data)
50 {
51 TRACE_CALL(__func__);
52 g_free(item->filename);
53 g_free(item->name);
54 g_free(item->group);
55 g_free(item->protocol);
56 g_free(item->server);
57 }
58
remmina_applet_menu_item_class_init(RemminaAppletMenuItemClass * klass)59 static void remmina_applet_menu_item_class_init(RemminaAppletMenuItemClass* klass)
60 {
61 TRACE_CALL(__func__);
62 }
63
remmina_applet_menu_item_init(RemminaAppletMenuItem * item)64 static void remmina_applet_menu_item_init(RemminaAppletMenuItem* item)
65 {
66 TRACE_CALL(__func__);
67 item->filename = NULL;
68 item->name = NULL;
69 item->group = NULL;
70 item->protocol = NULL;
71 item->server = NULL;
72 item->ssh_tunnel_enabled = FALSE;
73 g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(remmina_applet_menu_item_destroy), NULL);
74 }
75
remmina_applet_menu_item_new(RemminaAppletMenuItemType item_type,...)76 GtkWidget* remmina_applet_menu_item_new(RemminaAppletMenuItemType item_type, ...)
77 {
78 TRACE_CALL(__func__);
79 va_list ap;
80 RemminaAppletMenuItem* item;
81 GKeyFile* gkeyfile;
82 GtkWidget* widget;
83
84 va_start(ap, item_type);
85
86 item = REMMINA_APPLET_MENU_ITEM(g_object_new(REMMINA_TYPE_APPLET_MENU_ITEM, NULL));
87
88 item->item_type = item_type;
89
90 switch (item_type) {
91 case REMMINA_APPLET_MENU_ITEM_FILE:
92 item->filename = g_strdup(va_arg(ap, const gchar*));
93
94 /* Load the file */
95 gkeyfile = g_key_file_new();
96
97 if (!g_key_file_load_from_file(gkeyfile, item->filename, G_KEY_FILE_NONE, NULL)) {
98 g_key_file_free(gkeyfile);
99 va_end(ap);
100 return NULL;
101 }
102
103 item->name = g_key_file_get_string(gkeyfile, "remmina", "name", NULL);
104 item->group = g_key_file_get_string(gkeyfile, "remmina", "group", NULL);
105 item->protocol = g_key_file_get_string(gkeyfile, "remmina", "protocol", NULL);
106 item->server = g_key_file_get_string(gkeyfile, "remmina", "server", NULL);
107 item->ssh_tunnel_enabled = g_key_file_get_boolean(gkeyfile, "remmina", "ssh_tunnel_enabled", NULL);
108
109 g_key_file_free(gkeyfile);
110
111 if (item->name == NULL) {
112 g_printf("WARNING: missing name= line in file %s. Skipping.\n", item->filename);
113 va_end(ap);
114 return NULL;
115 }
116
117 break;
118
119 case REMMINA_APPLET_MENU_ITEM_DISCOVERED:
120 item->name = g_strdup(va_arg(ap, const gchar *));
121 item->group = g_strdup(_("Discovered"));
122 item->protocol = g_strdup("VNC");
123 break;
124
125 case REMMINA_APPLET_MENU_ITEM_NEW:
126 item->name = g_strdup(_("New Connection"));
127 break;
128 }
129
130 va_end(ap);
131
132 /* Create the label */
133 widget = gtk_label_new(item->name);
134 gtk_widget_show(widget);
135 gtk_widget_set_valign(widget, GTK_ALIGN_START);
136 gtk_widget_set_halign(widget, GTK_ALIGN_START);
137 gtk_container_add(GTK_CONTAINER(item), widget);
138
139 if (item->server) {
140 gtk_widget_set_tooltip_text(GTK_WIDGET(item), item->server);
141 }
142
143 return GTK_WIDGET(item);
144 }
145
remmina_applet_menu_item_compare(gconstpointer a,gconstpointer b,gpointer user_data)146 gint remmina_applet_menu_item_compare(gconstpointer a, gconstpointer b, gpointer user_data)
147 {
148 TRACE_CALL(__func__);
149 gint cmp;
150 RemminaAppletMenuItem* itema;
151 RemminaAppletMenuItem* itemb;
152
153 /* Passed in parameters are pointers to pointers */
154 itema = REMMINA_APPLET_MENU_ITEM(*((void**)a));
155 itemb = REMMINA_APPLET_MENU_ITEM(*((void**)b));
156
157 /* Put ungrouped items to the last */
158 if (IS_EMPTY(itema->group) && !IS_EMPTY(itemb->group))
159 return 1;
160 if (!IS_EMPTY(itema->group) && IS_EMPTY(itemb->group))
161 return -1;
162
163 /* Put discovered items the last group */
164 if (itema->item_type == REMMINA_APPLET_MENU_ITEM_DISCOVERED && itemb->item_type != REMMINA_APPLET_MENU_ITEM_DISCOVERED)
165 return 1;
166 if (itema->item_type != REMMINA_APPLET_MENU_ITEM_DISCOVERED && itemb->item_type == REMMINA_APPLET_MENU_ITEM_DISCOVERED)
167 return -1;
168
169 if (itema->item_type != REMMINA_APPLET_MENU_ITEM_DISCOVERED && !IS_EMPTY(itema->group)) {
170 cmp = g_strcmp0(itema->group, itemb->group);
171
172 if (cmp != 0)
173 return cmp;
174 }
175
176 return g_strcmp0(itema->name, itemb->name);
177 }
178