1 /* eel-vfs-extensions.c - gnome-vfs extensions. Its likely some of these will
2 * be part of gnome-vfs in the future.
3 *
4 * Copyright (C) 1999, 2000 Eazel, Inc.
5 *
6 * The Gnome Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * The Gnome Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * see <http://www.gnu.org/licenses/>.
19 *
20 * Authors: Darin Adler <darin@eazel.com>
21 * Pavel Cisler <pavel@eazel.com>
22 * Mike Fleming <mfleming@eazel.com>
23 * John Sullivan <sullivan@eazel.com>
24 */
25
26 #include <config.h>
27 #include "eel-vfs-extensions.h"
28 #include "eel-glib-extensions.h"
29 #include "eel-lib-self-check-functions.h"
30
31 #include <glib.h>
32 #include <glib/gi18n-lib.h>
33 #include <gio/gio.h>
34
35 #include "eel-string.h"
36
37 #include <string.h>
38 #include <stdlib.h>
39
40 gboolean
eel_uri_is_starred(const gchar * uri)41 eel_uri_is_starred (const gchar *uri)
42 {
43 return g_str_has_prefix (uri, "starred:");
44 }
45
46 /* It also matches trashed folders inside Trash,
47 * use `eel_uri_is_trash_root` if that's not desirable. */
48 gboolean
eel_uri_is_trash(const char * uri)49 eel_uri_is_trash (const char *uri)
50 {
51 return g_str_has_prefix (uri, "trash:");
52 }
53
54 gboolean
eel_uri_is_trash_root(const char * uri)55 eel_uri_is_trash_root (const char *uri)
56 {
57 return g_strcmp0 (uri, "trash:///") == 0;
58 }
59
60 gboolean
eel_uri_is_recent(const char * uri)61 eel_uri_is_recent (const char *uri)
62 {
63 return g_str_has_prefix (uri, "recent:");
64 }
65
66 gboolean
eel_uri_is_search(const char * uri)67 eel_uri_is_search (const char *uri)
68 {
69 return g_str_has_prefix (uri, EEL_SEARCH_URI);
70 }
71
72 gboolean
eel_uri_is_other_locations(const char * uri)73 eel_uri_is_other_locations (const char *uri)
74 {
75 return g_str_has_prefix (uri, "other-locations:");
76 }
77
78 gboolean
eel_uri_is_in_xdg_dirs(const gchar * uri)79 eel_uri_is_in_xdg_dirs (const gchar *uri)
80 {
81 GUserDirectory dir;
82 g_autoptr (GFile) location = NULL;
83 gboolean has_prefix = FALSE;
84
85 location = g_file_new_for_uri (uri);
86 for (dir = 0; dir < G_USER_N_DIRECTORIES; dir++)
87 {
88 g_autoptr (GFile) xdg_dir_location = NULL;
89 const gchar *path;
90
91 path = g_get_user_special_dir (dir);
92 if (path == NULL)
93 {
94 continue;
95 }
96
97 xdg_dir_location = g_file_new_for_path (path);
98 has_prefix = g_file_has_prefix (location, xdg_dir_location) ||
99 g_file_equal (location, xdg_dir_location);
100
101 if (has_prefix)
102 {
103 break;
104 }
105 }
106
107 return has_prefix;
108 }
109
110 /**
111 * eel_filename_get_extension_offset:
112 * @filename: a null-terminated string representing the basename of a file, with
113 * or without extension.
114 *
115 * Returns: (nullable) (transfer none): A pointer to the substring containing
116 * the dot and extension, or %NULL if there
117 * is no extension.
118 */
119 char *
eel_filename_get_extension_offset(const char * filename)120 eel_filename_get_extension_offset (const char *filename)
121 {
122 char *end, *end2;
123 const char *start;
124
125 if (filename == NULL || filename[0] == '\0')
126 {
127 return NULL;
128 }
129
130 /* basename must have at least one char */
131 start = filename + 1;
132
133 end = strrchr (start, '.');
134 if (end == NULL || end[1] == '\0')
135 {
136 return NULL;
137 }
138
139 if (end != start)
140 {
141 if (strcmp (end, ".gz") == 0 ||
142 strcmp (end, ".bz2") == 0 ||
143 strcmp (end, ".sit") == 0 ||
144 strcmp (end, ".Z") == 0 ||
145 strcmp (end, ".bz") == 0 ||
146 strcmp (end, ".xz") == 0)
147 {
148 end2 = end - 1;
149 while (end2 > start &&
150 *end2 != '.')
151 {
152 end2--;
153 }
154 if (end2 != start)
155 {
156 end = end2;
157 }
158 }
159 }
160
161 return end;
162 }
163
164 char *
eel_filename_strip_extension(const char * filename_with_extension)165 eel_filename_strip_extension (const char *filename_with_extension)
166 {
167 char *filename, *end;
168
169 if (filename_with_extension == NULL)
170 {
171 return NULL;
172 }
173
174 filename = g_strdup (filename_with_extension);
175 end = eel_filename_get_extension_offset (filename);
176
177 if (end && end != filename)
178 {
179 *end = '\0';
180 }
181
182 return filename;
183 }
184
185 void
eel_filename_get_rename_region(const char * filename,int * start_offset,int * end_offset)186 eel_filename_get_rename_region (const char *filename,
187 int *start_offset,
188 int *end_offset)
189 {
190 char *filename_without_extension;
191
192 g_return_if_fail (start_offset != NULL);
193 g_return_if_fail (end_offset != NULL);
194
195 *start_offset = 0;
196 *end_offset = 0;
197
198 g_return_if_fail (filename != NULL);
199
200 filename_without_extension = eel_filename_strip_extension (filename);
201 *end_offset = g_utf8_strlen (filename_without_extension, -1);
202
203 g_free (filename_without_extension);
204 }
205