1 | /* |
---|
2 | * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <glib/gi18n.h> |
---|
14 | #include <gtk/gtk.h> |
---|
15 | #include <libtransmission/transmission.h> |
---|
16 | #include "conf.h" |
---|
17 | #include "hig.h" |
---|
18 | #include "tr_core.h" |
---|
19 | #include "tr_prefs.h" |
---|
20 | #include "util.h" |
---|
21 | |
---|
22 | /** |
---|
23 | * This is where we initialize the preferences file with the default values. |
---|
24 | * If you add a new preferences key, you /must/ add a default value here. |
---|
25 | */ |
---|
26 | void |
---|
27 | tr_prefs_init_global( void ) |
---|
28 | { |
---|
29 | pref_flag_set_default ( PREF_KEY_DL_LIMIT_ENABLED, FALSE ); |
---|
30 | pref_int_set_default ( PREF_KEY_DL_LIMIT, 100 ); |
---|
31 | pref_flag_set_default ( PREF_KEY_UL_LIMIT_ENABLED, FALSE ); |
---|
32 | pref_int_set_default ( PREF_KEY_UL_LIMIT, 50 ); |
---|
33 | |
---|
34 | pref_flag_set_default ( PREF_KEY_DIR_ASK, FALSE ); |
---|
35 | pref_string_set_default ( PREF_KEY_DIR_DEFAULT, g_get_home_dir() ); |
---|
36 | |
---|
37 | pref_int_set_default ( PREF_KEY_PORT, TR_DEFAULT_PORT ); |
---|
38 | |
---|
39 | pref_flag_set_default ( PREF_KEY_NAT, TRUE ); |
---|
40 | pref_flag_set_default ( PREF_KEY_PEX, TRUE ); |
---|
41 | pref_flag_set_default ( PREF_KEY_SYSTRAY, TRUE ); |
---|
42 | pref_flag_set_default ( PREF_KEY_ASKQUIT, TRUE ); |
---|
43 | |
---|
44 | pref_string_set_default ( PREF_KEY_ADDSTD, toractionname(TR_TOR_COPY) ); |
---|
45 | pref_string_set_default ( PREF_KEY_ADDIPC, toractionname(TR_TOR_COPY) ); |
---|
46 | |
---|
47 | pref_int_set_default ( PREF_KEY_MSGLEVEL, TR_MSG_INF ); |
---|
48 | |
---|
49 | pref_save( NULL ); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | *** |
---|
54 | **/ |
---|
55 | |
---|
56 | int |
---|
57 | tr_prefs_get_action( const char * key ) |
---|
58 | { |
---|
59 | char * val = pref_string_get( key ); |
---|
60 | const int ret = toraddaction( val ); |
---|
61 | g_free( val ); |
---|
62 | return ret; |
---|
63 | } |
---|
64 | |
---|
65 | void |
---|
66 | tr_prefs_set_action( const char * key, int action ) |
---|
67 | { |
---|
68 | pref_string_set( key, toractionname(action) ); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | *** |
---|
73 | **/ |
---|
74 | |
---|
75 | #define PREFS_KEY "prefs-key" |
---|
76 | |
---|
77 | static void |
---|
78 | response_cb( GtkDialog * dialog, int response UNUSED, gpointer unused UNUSED ) |
---|
79 | { |
---|
80 | gtk_widget_destroy( GTK_WIDGET(dialog) ); |
---|
81 | } |
---|
82 | |
---|
83 | static void |
---|
84 | toggled_cb( GtkToggleButton * w, gpointer core ) |
---|
85 | { |
---|
86 | const char * key = g_object_get_data( G_OBJECT(w), PREFS_KEY ); |
---|
87 | const gboolean flag = gtk_toggle_button_get_active( w ); |
---|
88 | tr_core_set_pref_bool( TR_CORE(core), key, flag ); |
---|
89 | } |
---|
90 | |
---|
91 | static GtkWidget* |
---|
92 | new_check_button( const char * mnemonic, const char * key, gpointer core ) |
---|
93 | { |
---|
94 | GtkWidget * w = gtk_check_button_new_with_mnemonic( mnemonic ); |
---|
95 | g_object_set_data_full( G_OBJECT(w), PREFS_KEY, g_strdup(key), g_free ); |
---|
96 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(w), pref_flag_get(key) ); |
---|
97 | g_signal_connect( w, "toggled", G_CALLBACK(toggled_cb), core ); |
---|
98 | return w; |
---|
99 | } |
---|
100 | |
---|
101 | static void |
---|
102 | spun_cb( GtkSpinButton * w, gpointer core ) |
---|
103 | { |
---|
104 | const char * key = g_object_get_data( G_OBJECT(w), PREFS_KEY ); |
---|
105 | const int value = gtk_spin_button_get_value_as_int( w ); |
---|
106 | tr_core_set_pref_int( TR_CORE(core), key, value ); |
---|
107 | } |
---|
108 | |
---|
109 | static GtkWidget* |
---|
110 | new_spin_button( const char * key, gpointer core, int low, int high ) |
---|
111 | { |
---|
112 | GtkWidget * w = gtk_spin_button_new_with_range( low, high, 1 ); |
---|
113 | g_object_set_data_full( G_OBJECT(w), PREFS_KEY, g_strdup(key), g_free ); |
---|
114 | gtk_spin_button_set_digits( GTK_SPIN_BUTTON(w), 0 ); |
---|
115 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(w), pref_int_get(key) ); |
---|
116 | g_signal_connect( w, "value-changed", G_CALLBACK(spun_cb), core ); |
---|
117 | return w; |
---|
118 | } |
---|
119 | |
---|
120 | static void |
---|
121 | chosen_cb( GtkFileChooser * w, gpointer core ) |
---|
122 | { |
---|
123 | const char * key = g_object_get_data( G_OBJECT(w), PREFS_KEY ); |
---|
124 | char * value = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(w) ); |
---|
125 | tr_core_set_pref( TR_CORE(core), key, value ); |
---|
126 | g_free( value ); |
---|
127 | } |
---|
128 | |
---|
129 | static GtkWidget* |
---|
130 | new_path_chooser_button( const char * key, gpointer core ) |
---|
131 | { |
---|
132 | GtkWidget * w = gtk_file_chooser_button_new( "asdf", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ); |
---|
133 | char * path = pref_string_get( key ); |
---|
134 | g_object_set_data_full( G_OBJECT(w), PREFS_KEY, g_strdup(key), g_free ); |
---|
135 | g_signal_connect( w, "selection-changed", G_CALLBACK(chosen_cb), core ); |
---|
136 | gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(w), path ); |
---|
137 | return w; |
---|
138 | } |
---|
139 | |
---|
140 | static void |
---|
141 | action_cb( GtkComboBox * w, gpointer core ) |
---|
142 | { |
---|
143 | const char * key = g_object_get_data( G_OBJECT(w), PREFS_KEY ); |
---|
144 | GtkTreeIter iter; |
---|
145 | if( gtk_combo_box_get_active_iter( GTK_COMBO_BOX(w), &iter ) ) |
---|
146 | { |
---|
147 | int action; |
---|
148 | GtkTreeModel * model = gtk_combo_box_get_model( GTK_COMBO_BOX(w) ); |
---|
149 | gtk_tree_model_get( model, &iter, 1, &action, -1 ); |
---|
150 | tr_core_set_pref( core, key, toractionname(action) ); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | static GtkWidget* |
---|
155 | new_action_combo( const char * key, gpointer core ) |
---|
156 | { |
---|
157 | GtkTreeIter iter; |
---|
158 | GtkCellRenderer * rend; |
---|
159 | GtkListStore * model; |
---|
160 | GtkWidget * w; |
---|
161 | |
---|
162 | model = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_INT ); |
---|
163 | gtk_list_store_append( model, &iter ); |
---|
164 | gtk_list_store_set( model, &iter, 1, TR_TOR_LEAVE, 0, |
---|
165 | _("Use the torrent file where it is"), -1 ); |
---|
166 | gtk_list_store_append( model, &iter ); |
---|
167 | gtk_list_store_set( model, &iter, 1, TR_TOR_COPY, 0, |
---|
168 | _("Keep a copy of the torrent file"), -1 ); |
---|
169 | gtk_list_store_append( model, &iter ); |
---|
170 | gtk_list_store_set( model, &iter, 1, TR_TOR_MOVE, 0, |
---|
171 | _("Keep a copy and remove the original"), -1 ); |
---|
172 | |
---|
173 | w = gtk_combo_box_new_with_model( GTK_TREE_MODEL(model) ); |
---|
174 | gtk_combo_box_set_active( GTK_COMBO_BOX(w), pref_int_get(key) ); |
---|
175 | g_object_set_data_full( G_OBJECT(w), PREFS_KEY, g_strdup(key), g_free ); |
---|
176 | rend = gtk_cell_renderer_text_new( ); |
---|
177 | gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(w), rend, TRUE ); |
---|
178 | gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(w), rend, "text", 0 ); |
---|
179 | g_signal_connect( w, "changed", G_CALLBACK(action_cb), core ); |
---|
180 | |
---|
181 | return w; |
---|
182 | } |
---|
183 | |
---|
184 | GtkWidget * |
---|
185 | tr_prefs_dialog_new( GObject * core, GtkWindow * parent ) |
---|
186 | { |
---|
187 | int row = 0; |
---|
188 | GtkWidget * t; |
---|
189 | GtkWidget * w; |
---|
190 | GtkWidget * l; |
---|
191 | GtkWidget * dialog = gtk_dialog_new_with_buttons( _("Transmission: Preferences"), parent, |
---|
192 | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
193 | GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
---|
194 | NULL ); |
---|
195 | gtk_window_set_role( GTK_WINDOW(dialog), "transmission-preferences-dialog" ); |
---|
196 | g_signal_connect( dialog, "response", G_CALLBACK(response_cb), core ); |
---|
197 | |
---|
198 | t = hig_workarea_create (); |
---|
199 | |
---|
200 | hig_workarea_add_section_title (t, &row, _("Speed Limits")); |
---|
201 | hig_workarea_add_section_spacer (t, row, 4); |
---|
202 | |
---|
203 | w = new_check_button( _("_Limit Upload Speed"), PREF_KEY_UL_LIMIT_ENABLED, core ); |
---|
204 | hig_workarea_add_wide_control( t, &row, w ); |
---|
205 | |
---|
206 | w = new_spin_button( PREF_KEY_UL_LIMIT, core, 20, INT_MAX ); |
---|
207 | l = hig_workarea_add_row( t, &row, _("Maximum _Upload Speed (KiB/s)"), w, NULL ); |
---|
208 | |
---|
209 | w = new_check_button( _("Li_mit Download Speed"), PREF_KEY_DL_LIMIT_ENABLED, core ); |
---|
210 | hig_workarea_add_wide_control( t, &row, w ); |
---|
211 | |
---|
212 | w = new_spin_button( PREF_KEY_DL_LIMIT, core, 1, INT_MAX ); |
---|
213 | l = hig_workarea_add_row( t, &row, _("Maximum _Download Speed (KiB/s)"), w, NULL ); |
---|
214 | |
---|
215 | hig_workarea_add_section_divider( t, &row ); |
---|
216 | hig_workarea_add_section_title (t, &row, _("Downloads")); |
---|
217 | hig_workarea_add_section_spacer (t, row, 4); |
---|
218 | |
---|
219 | w = new_check_button( _("Al_ways prompt for download directory"), PREF_KEY_DIR_ASK, core ); |
---|
220 | hig_workarea_add_wide_control( t, &row, w ); |
---|
221 | |
---|
222 | w = new_path_chooser_button( PREF_KEY_DIR_DEFAULT, core ); |
---|
223 | l = hig_workarea_add_row( t, &row, _("Download Di_rectory"), w, NULL ); |
---|
224 | |
---|
225 | w = new_action_combo( PREF_KEY_ADDSTD, core ); |
---|
226 | l = hig_workarea_add_row( t, &row, _("For torrents added _normally:"), w, NULL ); |
---|
227 | |
---|
228 | w = new_action_combo( PREF_KEY_ADDIPC, core ); |
---|
229 | l = hig_workarea_add_row( t, &row, _("For torrents added from _command-line:"), w, NULL ); |
---|
230 | |
---|
231 | hig_workarea_add_section_divider( t, &row ); |
---|
232 | hig_workarea_add_section_title (t, &row, _("Network")); |
---|
233 | hig_workarea_add_section_spacer (t, row, 2); |
---|
234 | |
---|
235 | w = new_check_button( _("_Automatic Port Mapping via NAT-PMP or UPnP"), PREF_KEY_NAT, core ); |
---|
236 | hig_workarea_add_wide_control( t, &row, w ); |
---|
237 | |
---|
238 | w = new_spin_button( PREF_KEY_PORT, core, 1, INT_MAX ); |
---|
239 | l = hig_workarea_add_row( t, &row, _("Listening _Port"), w, NULL ); |
---|
240 | |
---|
241 | hig_workarea_add_section_divider( t, &row ); |
---|
242 | hig_workarea_add_section_title (t, &row, _("Options")); |
---|
243 | hig_workarea_add_section_spacer (t, row, 3); |
---|
244 | |
---|
245 | w = new_check_button( _("Use Peer _Exchange if Possible"), PREF_KEY_PEX, core ); |
---|
246 | hig_workarea_add_wide_control( t, &row, w ); |
---|
247 | |
---|
248 | w = new_check_button( _("Display an Icon in the System _Tray"), PREF_KEY_SYSTRAY, core ); |
---|
249 | hig_workarea_add_wide_control( t, &row, w ); |
---|
250 | |
---|
251 | w = new_check_button( _("Confirm _quit"), PREF_KEY_ASKQUIT, core ); |
---|
252 | hig_workarea_add_wide_control( t, &row, w ); |
---|
253 | |
---|
254 | hig_workarea_finish (t, &row); |
---|
255 | gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(dialog)->vbox), t ); |
---|
256 | gtk_widget_show_all( GTK_DIALOG(dialog)->vbox ); |
---|
257 | return dialog; |
---|
258 | } |
---|