Remmina - The GTK+ Remote Desktop Client  v1.4.33
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.
www_utils.c
Go to the documentation of this file.
1 /*
2  * Remmina - The GTK+ Remote Desktop Client
3  * Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give
21  * permission to link the code of portions of this program with the
22  * OpenSSL library under certain conditions as described in each
23  * individual source file, and distribute linked combinations
24  * including the two.
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL. * If you modify
27  * file(s) with this exception, you may extend this exception to your
28  * version of the file(s), but you are not obligated to do so. * If you
29  * do not wish to do so, delete this exception statement from your
30  * version. * If you delete this exception statement from all source
31  * files in the program, then also delete it here.
32  *
33  */
34 
35 /* Some utils taken form remmina_utils
36 * TODO: use directly remmina_utils */
37 
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/utsname.h>
41 
42 #include <glib.h>
43 #include <glib/gi18n.h>
44 #include <glib/gstdio.h>
45 #include <gio/gio.h>
47 
49 #define EMPTY(ptr) \
50  (!(ptr) || !*(ptr))
51 
52 /* Used to send desktop notifications */
53 void www_utils_send_notification(const gchar *notification_id,
54  const gchar *notification_title, const gchar *notification_message)
55 {
56  TRACE_CALL(__func__);
57 
58  GNotification *notification = g_notification_new(notification_title);
59  g_notification_set_body(notification, notification_message);
60 #if GLIB_CHECK_VERSION(2, 42, 0)
61  g_notification_set_priority(notification, G_NOTIFICATION_PRIORITY_NORMAL);
62 #endif
63  g_application_send_notification(g_application_get_default(), notification_id, notification);
64  g_object_unref(notification);
65 }
66 
67 gint www_utils_strpos(const gchar *haystack, const gchar *needle)
68 {
69  TRACE_CALL(__func__);
70  const gchar *sub;
71 
72  if (!*needle)
73  return -1;
74 
75  sub = strstr(haystack, needle);
76  if (!sub)
77  return -1;
78 
79  return sub - haystack;
80 }
81 
82 /* end can be -1 for haystack->len.
83  * returns: position of found text or -1.
84  * (C) Taken from geany */
85 gint www_utils_string_find(GString *haystack, gint start, gint end, const gchar *needle)
86 {
87  TRACE_CALL(__func__);
88  gint pos;
89 
90  g_return_val_if_fail(haystack != NULL, -1);
91  if (haystack->len == 0)
92  return -1;
93 
94  g_return_val_if_fail(start >= 0, -1);
95  if (start >= (gint)haystack->len)
96  return -1;
97 
98  g_return_val_if_fail(!EMPTY(needle), -1);
99 
100  if (end < 0)
101  end = haystack->len;
102 
103  pos = www_utils_strpos(haystack->str + start, needle);
104  if (pos == -1)
105  return -1;
106 
107  pos += start;
108  if (pos >= end)
109  return -1;
110  return pos;
111 }
112 
113 /* Replaces @len characters from offset @a pos.
114  * len can be -1 to replace the remainder of @a str.
115  * returns: pos + strlen(replace).
116  * (C) Taken from geany */
117 gint www_utils_string_replace(GString *str, gint pos, gint len, const gchar *replace)
118 {
119  TRACE_CALL(__func__);
120  g_string_erase(str, pos, len);
121  if (replace) {
122  g_string_insert(str, pos, replace);
123  pos += strlen(replace);
124  }
125  return pos;
126 }
127 
137 guint www_utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
138 {
139  TRACE_CALL(__func__);
140  guint count = 0;
141  gint pos = 0;
142  gsize needle_length = strlen(needle);
143 
144  while (1) {
145  pos = www_utils_string_find(haystack, pos, -1, needle);
146 
147  if (pos == -1)
148  break;
149 
150  pos = www_utils_string_replace(haystack, pos, needle_length, replace);
151  count++;
152  }
153  return count;
154 }
gint www_utils_strpos(const gchar *haystack, const gchar *needle)
Definition: www_utils.c:67
guint www_utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
Replaces all occurrences of needle in haystack with replace.
Definition: www_utils.c:137
gint www_utils_string_find(GString *haystack, gint start, gint end, const gchar *needle)
Definition: www_utils.c:85
gint www_utils_string_replace(GString *str, gint pos, gint len, const gchar *replace)
Definition: www_utils.c:117
void www_utils_send_notification(const gchar *notification_id, const gchar *notification_title, const gchar *notification_message)
Definition: www_utils.c:53