1 | /****************************************************************************** |
---|
2 | * $Id: util.h 7464 2008-12-22 05:39:03Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 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 | #ifndef UNUSED |
---|
33 | #define UNUSED G_GNUC_UNUSED |
---|
34 | #endif |
---|
35 | |
---|
36 | /* return number of items in array */ |
---|
37 | #define ALEN( a ) ( (signed)G_N_ELEMENTS( a ) ) |
---|
38 | |
---|
39 | /* used for a callback function with a data parameter */ |
---|
40 | typedef void ( *callbackfunc_t )( void* ); |
---|
41 | |
---|
42 | /* return a human-readable string for the size given in bytes. */ |
---|
43 | char* tr_strlsize( char * buf, |
---|
44 | guint64 size, |
---|
45 | size_t buflen ); |
---|
46 | |
---|
47 | /* return a human-readable string for the transfer rate given in bytes. */ |
---|
48 | char* tr_strlspeed( char * buf, |
---|
49 | double KiBps, |
---|
50 | size_t buflen ); |
---|
51 | |
---|
52 | /* return a human-readable string for the given ratio. */ |
---|
53 | char* tr_strlratio( char * buf, |
---|
54 | double ratio, |
---|
55 | size_t buflen ); |
---|
56 | |
---|
57 | /* return a human-readable string for the time given in seconds. */ |
---|
58 | char* tr_strltime( char * buf, |
---|
59 | int secs, |
---|
60 | size_t buflen ); |
---|
61 | |
---|
62 | char* gtr_localtime( time_t time ); |
---|
63 | |
---|
64 | /* create a directory and any missing parent directories */ |
---|
65 | int mkdir_p( const char *name, |
---|
66 | mode_t mode ); |
---|
67 | |
---|
68 | /* create a copy of a GSList of strings, this dups the actual strings too */ |
---|
69 | GSList * dupstrlist( GSList * list ); |
---|
70 | |
---|
71 | /* joins a GSList of strings into one string using an optional separator */ |
---|
72 | char * joinstrlist( GSList *list, |
---|
73 | char * sep ); |
---|
74 | |
---|
75 | /* free a GSList of strings */ |
---|
76 | void freestrlist( GSList *list ); |
---|
77 | |
---|
78 | /* decodes a string that has been urlencoded */ |
---|
79 | char * decode_uri( const char * uri ); |
---|
80 | |
---|
81 | /* return a list of cleaned-up paths, with invalid directories removed */ |
---|
82 | GSList * checkfilenames( int argc, |
---|
83 | char ** argv ); |
---|
84 | |
---|
85 | void gtr_open_file( const char * path ); |
---|
86 | |
---|
87 | gboolean gtr_dbus_add_torrent( const char * filename ); |
---|
88 | |
---|
89 | gboolean gtr_dbus_present_window( void ); |
---|
90 | |
---|
91 | char* gtr_get_help_url( void ); |
---|
92 | |
---|
93 | #ifdef GTK_MAJOR_VERSION |
---|
94 | |
---|
95 | GtkWidget * gtr_button_new_from_stock( const char * stock, |
---|
96 | const char * mnemonic ); |
---|
97 | |
---|
98 | guint gtr_timeout_add_seconds( guint interval, |
---|
99 | GSourceFunc function, |
---|
100 | gpointer data ); |
---|
101 | |
---|
102 | void addTorrentErrorDialog( GtkWidget * window_or_child, |
---|
103 | int err, |
---|
104 | const char * filename ); |
---|
105 | |
---|
106 | /* create an error dialog, if wind is NULL or mapped then show dialog now, |
---|
107 | otherwise show it when wind becomes mapped */ |
---|
108 | void errmsg( GtkWindow * wind, |
---|
109 | const char * format, |
---|
110 | ... ) G_GNUC_PRINTF( 2, 3 ); |
---|
111 | |
---|
112 | /* create an error dialog but do not gtk_widget_show() it, |
---|
113 | calls func( data ) when the dialog is closed */ |
---|
114 | GtkWidget * errmsg_full( GtkWindow * wind, |
---|
115 | callbackfunc_t func, |
---|
116 | void * data, |
---|
117 | const char * format, |
---|
118 | ... ) G_GNUC_PRINTF( 4, 5 ); |
---|
119 | |
---|
120 | /* pop up the context menu if a user right-clicks. |
---|
121 | if the row they right-click on isn't selected, select it. */ |
---|
122 | gboolean on_tree_view_button_pressed( GtkWidget * view, |
---|
123 | GdkEventButton * event, |
---|
124 | gpointer unused ); |
---|
125 | |
---|
126 | /* if the click didn't specify a row, clear the selection */ |
---|
127 | gboolean on_tree_view_button_released( GtkWidget * view, |
---|
128 | GdkEventButton * event, |
---|
129 | gpointer unused ); |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | gpointer tr_object_ref_sink( gpointer object ); |
---|
134 | |
---|
135 | void tr_file_trash_or_unlink( const char * filename ); |
---|
136 | |
---|
137 | #endif /* GTK_MAJOR_VERSION */ |
---|
138 | |
---|
139 | #endif /* TG_UTIL_H */ |
---|