1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* eel-vfs-extensions.c - gnome-vfs extensions. Its likely some of these will
4 be part of gnome-vfs in the future.
5
6 Copyright (C) 1999, 2000 Eazel, Inc.
7
8 The Gnome Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The Gnome Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with the Gnome Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 51 Franklin Street - Suite 500,
21 Boston, MA 02110-1335, USA.
22
23 Authors: Darin Adler <darin@eazel.com>
24 Pavel Cisler <pavel@eazel.com>
25 Mike Fleming <mfleming@eazel.com>
26 John Sullivan <sullivan@eazel.com>
27 */
28
29 #include <config.h>
30 #include "eel-vfs-extensions.h"
31 #include "eel-glib-extensions.h"
32 #include "eel-lib-self-check-functions.h"
33
34 #include <glib.h>
35 #include <glib/gi18n-lib.h>
36 #include <gio/gio.h>
37
38 #include "eel-string.h"
39
40 #include <string.h>
41 #include <stdlib.h>
42
43 gboolean
eel_uri_is_trash(const char * uri)44 eel_uri_is_trash (const char *uri)
45 {
46 return g_str_has_prefix (uri, "trash:");
47 }
48
49 gboolean
eel_uri_is_recent(const char * uri)50 eel_uri_is_recent (const char *uri)
51 {
52 return g_str_has_prefix (uri, "recent:");
53 }
54
55 gboolean
eel_uri_is_favorite(const char * uri)56 eel_uri_is_favorite (const char *uri)
57 {
58 return g_str_has_prefix (uri, "favorites:");
59 }
60
61 gboolean
eel_uri_is_search(const char * uri)62 eel_uri_is_search (const char *uri)
63 {
64 return g_str_has_prefix (uri, EEL_SEARCH_URI);
65 }
66
67 gboolean
eel_uri_is_desktop(const char * uri)68 eel_uri_is_desktop (const char *uri)
69 {
70 return g_str_has_prefix (uri, EEL_DESKTOP_URI);
71 }
72
73 gboolean
eel_uri_is_network(const char * uri)74 eel_uri_is_network (const char *uri)
75 {
76 return g_str_has_prefix (uri, "smb:") || g_str_has_prefix (uri, "network:");
77 }
78
79 char *
eel_make_valid_utf8(const char * name)80 eel_make_valid_utf8 (const char *name)
81 {
82 GString *string;
83 const char *remainder, *invalid;
84 int remaining_bytes, valid_bytes;
85
86 string = NULL;
87 remainder = name;
88 remaining_bytes = strlen (name);
89
90 while (remaining_bytes != 0) {
91 if (g_utf8_validate (remainder, remaining_bytes, &invalid)) {
92 break;
93 }
94 valid_bytes = invalid - remainder;
95
96 if (string == NULL) {
97 string = g_string_sized_new (remaining_bytes);
98 }
99 g_string_append_len (string, remainder, valid_bytes);
100 g_string_append_c (string, '?');
101
102 remaining_bytes -= valid_bytes + 1;
103 remainder = invalid + 1;
104 }
105
106 if (string == NULL) {
107 return g_strdup (name);
108 }
109
110 g_string_append (string, remainder);
111 g_string_append (string, _(" (invalid Unicode)"));
112 g_assert (g_utf8_validate (string->str, -1, NULL));
113
114 return g_string_free (string, FALSE);
115 }
116
117 char *
eel_filename_get_extension_offset(const char * filename)118 eel_filename_get_extension_offset (const char *filename)
119 {
120 char *end, *end2;
121 const char *start;
122
123 if (filename == NULL || filename[0] == '\0') {
124 return NULL;
125 }
126
127 /* basename must have at least one char */
128 start = filename + 1;
129
130 end = strrchr (start, '.');
131 if (end == NULL || end[1] == '\0') {
132 return NULL;
133 }
134
135 if (end != start) {
136 if (strcmp (end, ".gz") == 0 ||
137 strcmp (end, ".bz2") == 0 ||
138 strcmp (end, ".sit") == 0 ||
139 strcmp (end, ".bz") == 0 ||
140 strcmp (end, ".xz") == 0 ||
141 strcmp (end, ".Z") == 0) {
142 end2 = end - 1;
143 while (end2 > start &&
144 *end2 != '.') {
145 end2--;
146 }
147 if (end2 != start) {
148 end = end2;
149 }
150 }
151 }
152
153 return end;
154 }
155
156 char *
eel_filename_strip_extension(const char * filename_with_extension)157 eel_filename_strip_extension (const char * filename_with_extension)
158 {
159 char *filename, *end;
160
161 if (filename_with_extension == NULL) {
162 return NULL;
163 }
164
165 filename = g_strdup (filename_with_extension);
166 end = eel_filename_get_extension_offset (filename);
167
168 if (end && end != filename) {
169 *end = '\0';
170 }
171
172 return filename;
173 }
174
175 void
eel_filename_get_rename_region(const char * filename,int * start_offset,int * end_offset)176 eel_filename_get_rename_region (const char *filename,
177 int *start_offset,
178 int *end_offset)
179 {
180 char *filename_without_extension;
181
182 g_return_if_fail (start_offset != NULL);
183 g_return_if_fail (end_offset != NULL);
184
185 *start_offset = 0;
186 *end_offset = 0;
187
188 g_return_if_fail (filename != NULL);
189
190 filename_without_extension = eel_filename_strip_extension (filename);
191 *end_offset = g_utf8_strlen (filename_without_extension, -1);
192
193 g_free (filename_without_extension);
194 }
195