1 /*
2 * Copyright (C) 2005 Marc Pavot <marc.pavot@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 */
19
20 #include "servers/ario-server.h"
21 #include <glib.h>
22 #include <gdk/gdk.h>
23
24 /* Number of covers used to generate the drag & drop image */
25 #define MAX_COVERS_IN_DRAG 3
26
27 /* Maximum length of the string representing a int */
28 #define INTLEN (sizeof(int) * CHAR_BIT + 1) / 3 + 1
29
30 /* Maximum size of a time like 1:23:45 */
31 #define ARIO_MAX_TIME_SIZE 3*INTLEN+2
32
33 /* Maximum size of a track */
34 #define ARIO_MAX_TRACK_SIZE INTLEN
35
36 struct curl_slist;
37
38 /**
39 * Format a track time to the form 1:23:45.
40 *
41 * @param time The time to format, in seconds
42 *
43 * @return A newly allocated string with the formated time
44 */
45 char* ario_util_format_time (const int time) G_GNUC_CONST G_GNUC_MALLOC;
46
47 /**
48 * Format a track time to the form 1:23:45 in a buffer
49 *
50 * @param time The time to format, in seconds
51 * @param buf The buffer to fill
52 * @param buf_len The len of the buffer
53 */
54 void ario_util_format_time_buf (const int time,
55 char *buf,
56 int buf_len);
57 /**
58 * Format a time to the form : x days, y hours, z minutes, n secondes
59 *
60 * @param time The time to format, in seconds
61 *
62 * @return A newly allocated string with the formated time
63 */
64 char* ario_util_format_total_time (const int time) G_GNUC_CONST G_GNUC_MALLOC;
65
66 /**
67 * Format a track number to be displayed
68 *
69 * @param track The track number
70 * @param buf The buffer to fill
71 * @param buf_len The len of the buffer
72 */
73 void ario_util_format_track_buf (const gchar *track,
74 char *buf,
75 int buf_len);
76 /**
77 * Try to get the best title to display from an ArioServerSong
78 *
79 * @param server_song The server song
80 *
81 * @return A pointer to the title (should not be freed)
82 */
83 gchar* ario_util_format_title (ArioServerSong *server_song);
84
85 /**
86 * Initialise default icon factory with a few icons
87 */
88 void ario_util_init_icons (void);
89
90 /**
91 * Get the path of Ario configuration
92 *
93 * @return The path of the directory. It should not be freed.
94 */
95 const char* ario_util_config_dir (void);
96
97 /**
98 * Check whether a file exists or not
99 *
100 * @param uri The uri of the file
101 *
102 * @return True if the file exists, FALSE otherwise
103 */
104 gboolean ario_util_uri_exists (const char *uri);
105
106 /**
107 * Delete a file on the disk
108 *
109 * @param uri The uri of the file to delete
110 */
111 void ario_util_unlink_uri (const char *uri);
112
113 /**
114 * Create a new directory
115 *
116 * @param uri The uri of the directory to create
117 */
118 void ario_util_mkdir (const char *uri);
119
120 /**
121 * Copy a file on disk
122 *
123 * @param src_uri The source file to copy
124 * @param dest_uri The destination place to copy the file
125 */
126 void ario_util_copy_file (const char *src_uri,
127 const char *dest_uri);
128 /**
129 * Download a file on internet
130 *
131 * @param uri The uri of the file to download
132 * @param post_data Post data to use for POST requests or NULL
133 * @param post_size The size of post data (no used if post_data is NULL)
134 * @param headers Http headers to use or NULL
135 * @param size A pointer to a int that will contain the size of the downloaded data
136 * @param data Newly allocated data containing the downloaded file
137 */
138 void ario_util_download_file (const char *uri,
139 const char *post_data,
140 const int post_size,
141 const struct curl_slist *headers,
142 int* size,
143 char** data);
144 /**
145 * Replace string 'old' by string 'new' in 'string'
146 *
147 * @param string The string where the replacement will be done
148 * @param old The string to replace
149 * @param new The string that will replace 'old'
150 */
151 void ario_util_string_replace (char **string,
152 const char *old,
153 const char *new);
154 /**
155 * Load a URL in user's web browser
156 *
157 * @param uri The URL to load
158 */
159 void ario_util_load_uri (const char *uri);
160
161 /**
162 * Format a keyword to be used for example in an online search
163 *
164 * @param keyword The keyword to format
165 *
166 * @return A newly allocated formated keyword
167 */
168 char * ario_util_format_keyword (const char *keyword) G_GNUC_MALLOC;
169
170 /**
171 * Format a keyword to be used for a search on last.fm
172 *
173 * @param keyword The keyword to format
174 *
175 * @return A newly allocated formated keyword
176 */
177 char * ario_util_format_keyword_for_lastfm (const char *keyword) G_GNUC_MALLOC;
178
179 /**
180 * Generate an icon to use for Drag & Drop from a list of albums
181 *
182 * @param albums The list of albums
183 *
184 * @return A newly allocated pixbuf
185 */
186 GdkPixbuf * ario_util_get_dnd_pixbuf_from_albums (const GSList *albums) G_GNUC_MALLOC;
187
188 /**
189 * Generate an icon to use for Drag & Drop from a list of criteria
190 *
191 * @param criterias The list of ArioServerCriteria
192 *
193 * @return A newly allocated pixbuf
194 */
195 GdkPixbuf * ario_util_get_dnd_pixbuf (const GSList *criterias) G_GNUC_MALLOC;
196
197 /**
198 * Convert a string from iso8859 to locale
199 *
200 * @param string The string to convert
201 *
202 * @return A newly allocated string
203 */
204 gchar * ario_util_convert_from_iso8859 (const char *string) G_GNUC_MALLOC;
205
206 /**
207 * Remove bad caracters from a filename
208 *
209 * @param filename The filename to convert
210 */
211 void ario_util_sanitize_filename (char *filename);
212
213 /**
214 * Get the content of a file from disk
215 *
216 * @param filename The filename
217 * @param contents A pointer to a newly allocated file content
218 * @param length A pointer to the size of file content
219 * @param error return location for a GError, or NULL
220 *
221 * @return TRUE on success, FALSE if an error occurred
222 */
223 gboolean ario_file_get_contents (const gchar *filename,
224 gchar **contents,
225 gsize *length,
226 GError **error);
227 /**
228 * Writes all of contents to a file named filename
229 *
230 * @param filename name of a file to write contents
231 * @param contents string to write to the file
232 * @param length length of contents, or -1 if contents is a nul-terminated string
233 * @param error return location for a GError, or NULL
234 *
235 * @return TRUE on success, FALSE if an error occurred
236 */
237 gboolean ario_file_set_contents (const gchar *filename,
238 const gchar *contents,
239 gsize length,
240 GError **error);
241 /**
242 * Returns TRUE if any of the tests in the bitfield test are TRUE.
243 *
244 * @param filename a filename to test
245 * @param test bitfield of GFileTest flags
246 *
247 * @return whether a test was TRUE
248 */
249 gboolean ario_file_test (const gchar *filename,
250 GFileTest test);
251
252 /**
253 * Case insensitive strstr (locate a substring in a string)
254 *
255 * @param haystack The string to do the location
256 * @param needle String to locate
257 *
258 * @return
259 */
260 const char * ario_util_stristr (const char *haystack,
261 const char *needle);
262
263 /**
264 * Randomize a GSList
265 *
266 * @param The GSList to randomize. This list should not be used anymore after
267 * this function has been called. Elements of the list should be freed if
268 * needed and the list should be freed in addition to the one returned by
269 * ario_util_gslist_randomize.
270 * @param max The number of items to randomize in the list
271 *
272 * @return A pointer to the GSList to use instead of 'list'
273 */
274 GSList * ario_util_gslist_randomize (GSList **list,
275 const int max);
276
277 /**
278 * Format a string so that it can be used in an HTTP requests
279 *
280 * @param text The string to format
281 *
282 * @return A newly allocated string formated for HTTP requests
283 */
284 gchar * ario_util_format_for_http (const gchar *text);
285
286 /**
287 * Computes the absolute value of a int
288 *
289 * @param a An integer
290 *
291 * @return a if a is positive, -a if a is negative
292 */
293 static inline gint
ario_util_abs(const gint a)294 ario_util_abs (const gint a)
295 {
296 return (a > 0 ? a : -a);
297 }
298
299 /**
300 * Returns the min of two values
301 *
302 * @param a First value
303 * @param b Second value
304 *
305 * @return Minimum of a and b
306 */
307 static inline gint
ario_util_min(const gint a,const gint b)308 ario_util_min (const gint a,
309 const gint b)
310 {
311 return (a > b ? b : a);
312 }
313
314 /**
315 * Returns the max of two values
316 *
317 * @param a First value
318 * @param b Second value
319 *
320 * @return Maximum of a and b
321 */
322 static inline gint
ario_util_max(const gint a,const gint b)323 ario_util_max (const gint a,
324 const gint b)
325 {
326 return (a > b ? a : b);
327 }
328
329 /**
330 * Compare two strings. These two strings can be NULL
331 *
332 * @param a First string or NULL
333 * @param b Second string or NULL
334 *
335 * @return < 0 if a compares before b, 0 if they compare equal, > 0 if a compares after b
336 */
337 static inline gint
ario_util_strcmp(const gchar * a,const gchar * b)338 ario_util_strcmp (const gchar *a,
339 const gchar* b)
340 {
341 if (!a && !b)
342 return 0;
343 if (!a && b)
344 return 1;
345 if (a && !b)
346 return -1;
347 return g_utf8_collate (a, b);
348 }
349