1 | /****************************************************************************** |
---|
2 | * $Id: util.h 3380 2007-10-12 19:53:30Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef TG_UTIL_H |
---|
26 | #define TG_UTIL_H |
---|
27 | |
---|
28 | #include <sys/types.h> |
---|
29 | #include <stdarg.h> |
---|
30 | |
---|
31 | /* macro to shut up "unused parameter" warnings */ |
---|
32 | #define SHUTUP G_GNUC_UNUSED |
---|
33 | #define UNUSED G_GNUC_UNUSED |
---|
34 | |
---|
35 | /* NULL-safe version of strcmp */ |
---|
36 | int tr_strcmp( const char*, const char * ); |
---|
37 | |
---|
38 | /* XXX this shouldn't be here */ |
---|
39 | enum tr_torrent_action { TR_TOR_LEAVE, TR_TOR_COPY, TR_TOR_MOVE }; |
---|
40 | |
---|
41 | /* return number of items in array */ |
---|
42 | #define ALEN(a) ((signed)G_N_ELEMENTS(a)) |
---|
43 | |
---|
44 | /* used for a callback function with a data parameter */ |
---|
45 | typedef void (*callbackfunc_t)(void*); |
---|
46 | |
---|
47 | /* return a human-readable string for the size given in bytes. |
---|
48 | the string must be g_free()d */ |
---|
49 | char * |
---|
50 | readablesize(guint64 size); |
---|
51 | |
---|
52 | /* return a human-readable string for the transfer rate given in bytes. |
---|
53 | the string must be g_free()d */ |
---|
54 | char * |
---|
55 | readablespeed (double KiBps); |
---|
56 | |
---|
57 | |
---|
58 | /* return a human-readable string for the time given in seconds. |
---|
59 | the string must be g_free()d */ |
---|
60 | char * |
---|
61 | readabletime(int secs); |
---|
62 | |
---|
63 | char * |
---|
64 | rfc822date (guint64 epoch_msec); |
---|
65 | |
---|
66 | /* returns a string representing the download ratio. |
---|
67 | the string must be g_free()d */ |
---|
68 | char * |
---|
69 | ratiostr(guint64 down, guint64 up); |
---|
70 | |
---|
71 | /* create a directory and any missing parent directories */ |
---|
72 | gboolean |
---|
73 | mkdir_p(const char *name, mode_t mode); |
---|
74 | |
---|
75 | /* create a copy of a GList of strings, this dups the actual strings too */ |
---|
76 | GList * |
---|
77 | dupstrlist( GList * list ); |
---|
78 | |
---|
79 | /* joins a GList of strings into one string using an optional separator */ |
---|
80 | char * |
---|
81 | joinstrlist(GList *list, char *sep); |
---|
82 | |
---|
83 | /* free a GList of strings */ |
---|
84 | void |
---|
85 | freestrlist(GList *list); |
---|
86 | |
---|
87 | /* decodes a string that has been urlencoded */ |
---|
88 | char * |
---|
89 | urldecode(const char *str, int len); |
---|
90 | |
---|
91 | /* return a list of cleaned-up paths, with invalid directories removed */ |
---|
92 | GList * |
---|
93 | checkfilenames( int argc, char ** argv ); |
---|
94 | |
---|
95 | /* returns the flag for an action string */ |
---|
96 | enum tr_torrent_action |
---|
97 | toraddaction( const char * action ); |
---|
98 | |
---|
99 | /* returns the action string for a flag */ |
---|
100 | const char * |
---|
101 | toractionname( enum tr_torrent_action action ); |
---|
102 | |
---|
103 | /* retrieve the global download directory */ |
---|
104 | char * |
---|
105 | getdownloaddir( void ); |
---|
106 | |
---|
107 | #ifdef GTK_MAJOR_VERSION |
---|
108 | |
---|
109 | /* here there be dragons */ |
---|
110 | void |
---|
111 | sizingmagic( GtkWindow * wind, GtkScrolledWindow * scroll, |
---|
112 | GtkPolicyType hscroll, GtkPolicyType vscroll ); |
---|
113 | |
---|
114 | /* create an error dialog, if wind is NULL or mapped then show dialog now, |
---|
115 | otherwise show it when wind becomes mapped */ |
---|
116 | void |
---|
117 | errmsg( GtkWindow * wind, const char * format, ... ) |
---|
118 | #ifdef __GNUC__ |
---|
119 | __attribute__ (( format ( printf, 2, 3 ) )) |
---|
120 | #endif |
---|
121 | ; |
---|
122 | |
---|
123 | /* create an error dialog but do not gtk_widget_show() it, |
---|
124 | calls func( data ) when the dialog is closed */ |
---|
125 | GtkWidget * |
---|
126 | errmsg_full( GtkWindow * wind, callbackfunc_t func, void * data, |
---|
127 | const char * format, ... ) |
---|
128 | #ifdef __GNUC__ |
---|
129 | __attribute__ (( format ( printf, 4, 5 ) )) |
---|
130 | #endif |
---|
131 | ; |
---|
132 | |
---|
133 | /* varargs version of errmsg_full() */ |
---|
134 | GtkWidget * |
---|
135 | verrmsg_full( GtkWindow * wind, callbackfunc_t func, void * data, |
---|
136 | const char * format, va_list ap ); |
---|
137 | |
---|
138 | /* pop up the context menu if a user right-clicks. |
---|
139 | if the row they right-click on isn't selected, select it. */ |
---|
140 | gboolean |
---|
141 | on_tree_view_button_pressed (GtkWidget * view, |
---|
142 | GdkEventButton * event, |
---|
143 | gpointer unused); |
---|
144 | |
---|
145 | #endif /* GTK_MAJOR_VERSION */ |
---|
146 | |
---|
147 | #endif /* TG_UTIL_H */ |
---|