transmission-rss 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,319 +0,0 @@
1
- # Represents the config editor window.
2
- class TransmissionRSS::ConfigEditor
3
- TITLE = 'transmission-rss config editor'
4
- NAME = 'config-editor'
5
-
6
- DEFAULT_CONFIG = {
7
- 'feeds' => [],
8
- 'update_interval' => 600,
9
- 'add_paused' => false,
10
- 'server' => {
11
- 'host' => 'localhost',
12
- 'port' => 9091
13
- },
14
- 'log_target' => $stderr,
15
- 'privileges' => {}
16
- }
17
-
18
- # Loads glade file and initializes dynamic GUI elements.
19
- def initialize(config_file, config)
20
- @configFile = config_file
21
- @config = config
22
-
23
- path = File.join(File.dirname(__FILE__), 'main.glade')
24
-
25
- # Load glade file and connect to handler method. Dubious.
26
- @glade = GladeXML.new(path) do |handler|
27
- method(handler)
28
- end
29
-
30
- # Connect every widget to an instance variable.
31
- @glade.widget_names.each do |name|
32
- name.gsub!(/-/, '_')
33
- instance_variable_set("@#{name}".intern, @glade[name])
34
- end
35
-
36
- # Quit program, when it is closed by the WM.
37
- @window1.signal_connect('destroy') do
38
- Gtk.main_quit
39
- end
40
-
41
- # Initialize the ListBox widget.
42
- initialize_listbox
43
-
44
- # Reflect the current config on the GUI.
45
- initialize_config
46
- end
47
-
48
- # Reflects the current config on the GUI.
49
- def initialize_config
50
- @entry_server_host.text = @config.server.host
51
- @entry_server_port.text = @config.server.port.to_s
52
-
53
- @entry_update_interval.text = @config.update_interval.to_s
54
-
55
- @checkbutton_add_paused.active = @config.add_paused
56
-
57
- @listbox.clear
58
- @listbox.add(@config.feeds)
59
-
60
- # If log target is STDERR.
61
- if(@config.log_target.class == IO)
62
- # Deactivate log path entry.
63
- @label10.sensitive = false
64
- @entry_log_filepath.sensitive = false
65
-
66
- @combobox_logtype.active = 0
67
- else
68
- # Activate log path entry.
69
- @label10.sensitive = true
70
- @entry_log_filepath.sensitive = true
71
-
72
- # Set entry text accordingly.
73
- @entry_log_filepath.text = @config.log_target
74
-
75
- @combobox_logtype.active = 1
76
- end
77
-
78
- # If privilege section is given in config.
79
- if(@config.privileges.empty?)
80
- # Deactivate user entry.
81
- @label15.sensitive = false
82
- @entry_drop_privileges_user.sensitive = false
83
-
84
- # Deactivate group entry.
85
- @label16.sensitive = false
86
- @entry_drop_privileges_group.sensitive = false
87
-
88
- @checkbutton_drop_privileges.active = false
89
- else
90
- # Activate user entry.
91
- @label15.sensitive = true
92
- @entry_drop_privileges_user.sensitive = true
93
-
94
- # Activate group entry.
95
- @label16.sensitive = true
96
- @entry_drop_privileges_group.sensitive = true
97
-
98
- # Set entry texts accordingly.
99
- @entry_drop_privileges_user.text = @config.privileges.user
100
- @entry_drop_privileges_group.text = @config.privileges.group
101
-
102
- @checkbutton_drop_privileges.active = true
103
- end
104
- end
105
-
106
- # Initializes the ListBox widget.
107
- def initialize_listbox
108
- @listbox = ListBox.new
109
- @listbox.header = 'Feeds'
110
-
111
- @vbox2.pack_start(@listbox)
112
-
113
- @window1.show_all
114
-
115
- @listbox.signal_connect('button_release_event') do |widget, event|
116
- @entry_feed_url.text = @listbox.button_release(widget, event)
117
- end
118
- end
119
-
120
- # Add a feed to the ListBox if the Add-feed-button is pressed.
121
- def on_button_add_feed(widget)
122
- if(not @entry_feed_url.text.empty?)
123
- @listbox.add(@entry_feed_url.text)
124
- @entry_feed_url.text = ''
125
- end
126
- end
127
-
128
- # Remove a feed from the ListBox if the Remove-feed-button is pressed.
129
- def on_button_remove_feed(widget)
130
- @listbox.remove(@entry_feed_url.text)
131
- end
132
-
133
- # Activate or deactivate entry widgets if drop privileges checkbutton is
134
- # toggled.
135
- def on_checkbutton_drop_privileges_toggled(widget)
136
- if(@checkbutton_drop_privileges.active?)
137
- # Activate user entry.
138
- @label15.sensitive = true
139
- @entry_drop_privileges_user.sensitive = true
140
-
141
- # Activate group entry.
142
- @label16.sensitive = true
143
- @entry_drop_privileges_group.sensitive = true
144
- else
145
- # Deactivate user entry.
146
- @label15.sensitive = false
147
- @entry_drop_privileges_user.sensitive = false
148
-
149
- # Deactivate group entry.
150
- @label16.sensitive = false
151
- @entry_drop_privileges_group.sensitive = false
152
-
153
- # Delete entry texts.
154
- @entry_drop_privileges_user.text = ''
155
- @entry_drop_privileges_group.text = ''
156
- end
157
- end
158
-
159
- # Is called when a value in the log type ComboBox is selected.
160
- def on_combobox_logtype_changed(widget)
161
- # If STDERR is selected.
162
- if(@combobox_logtype.active == 0)
163
- # Deactivate the log file path entry.
164
- @label10.sensitive = false
165
- @entry_log_filepath.sensitive = false
166
-
167
- # Delete entry text.
168
- @entry_log_filepath.text = ''
169
- else
170
- # Activate the log file path entry.
171
- @label10.sensitive = true
172
- @entry_log_filepath.sensitive = true
173
- end
174
- end
175
-
176
- # Is called when the Help-About menu is selected.
177
- def on_menu_about(widget)
178
- Gnome::About.new(
179
- TITLE,
180
- VERSION,
181
- 'Copyright (C) 2010 henning mueller',
182
- 'Easy transmission-rss config editing. Devoted to Ann.',
183
- ['henning mueller'],
184
- ['henning mueller'],
185
- nil
186
- ).show
187
- end
188
-
189
- # Is called when the File-New menu is selected.
190
- def on_menu_new(widget)
191
- dialog = Gtk::MessageDialog.new(
192
- @window1,
193
- Gtk::Dialog::DESTROY_WITH_PARENT,
194
- Gtk::MessageDialog::WARNING,
195
- Gtk::MessageDialog::BUTTONS_YES_NO,
196
- "Unsaved configuration will be lost!\nProceed?"
197
- )
198
-
199
- if(dialog.run == Gtk::Dialog::RESPONSE_YES)
200
- @configFile = nil
201
-
202
- @config.clear
203
- @config.load(DEFAULT_CONFIG)
204
-
205
- initialize_config
206
- end
207
-
208
- dialog.destroy
209
- end
210
-
211
- # Is called when the File-Open menu is selected.
212
- def on_menu_open(widget)
213
- dialog = Gtk::FileChooserDialog.new(
214
- 'Open',
215
- @window1,
216
- Gtk::FileChooser::ACTION_OPEN,
217
- nil,
218
- [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
219
- [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT]
220
- )
221
-
222
- if(dialog.run == Gtk::Dialog::RESPONSE_ACCEPT)
223
- @configFile = dialog.filename
224
-
225
- @config.clear
226
- @config.load(DEFAULT_CONFIG)
227
- @config.load(@configFile)
228
-
229
- initialize_config
230
- end
231
-
232
- dialog.destroy
233
- end
234
-
235
- # Is called when the File-Save menu is selected.
236
- def on_menu_save(widget)
237
- if(not @configFile.nil?)
238
- save!
239
- else
240
- on_menu_save_as(nil)
241
- end
242
- end
243
-
244
- # Is called when the File-SaveAs menu is selected.
245
- def on_menu_save_as(widget)
246
- dialog = Gtk::FileChooserDialog.new(
247
- 'Save As..',
248
- @window1,
249
- Gtk::FileChooser::ACTION_SAVE,
250
- nil,
251
- [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
252
- [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT]
253
- )
254
-
255
- if(dialog.run == Gtk::Dialog::RESPONSE_ACCEPT)
256
- @configFile = dialog.filename
257
- save!
258
- end
259
-
260
- dialog.destroy
261
- end
262
-
263
- # Is called when the File-Quit menu is selected.
264
- def on_menu_quit(widget)
265
- Gtk.main_quit
266
- end
267
-
268
- # Convert GUI config to +Config+, open the config file and write a YAML
269
- # version of the Hash.
270
- def save!
271
- @config.server.host = @entry_server_host.text
272
- @config.server.port = @entry_server_port.text.to_i
273
-
274
- @config.update_interval = @entry_update_interval.text.to_i
275
-
276
- @config.add_paused = @checkbutton_add_paused.active?
277
-
278
- @config.feeds = @listbox.items
279
-
280
- # If STDERR is selected.
281
- if(@combobox_logtype.active == 0)
282
- # Delete log_target from config hash, so $stderr is chosen on load.
283
- @config.delete('log_target')
284
- else
285
- # Set log_target to entry text.
286
- @config.log_target = @entry_log_filepath.text
287
- end
288
-
289
- # If checkbutton drop privileges is activated.
290
- if(@checkbutton_drop_privileges.active?)
291
- # Set user and group to entry texts.
292
- @config.privileges.user = @entry_drop_privileges_user.text
293
- @config.privileges.group = @entry_drop_privileges_group.text
294
- else
295
- # Delete privilege section from config hash.
296
- @config.delete('privileges')
297
- end
298
-
299
- # Try writing to file; dialog on permission error.
300
- begin
301
- File.open(@configFile, 'w') do |f|
302
- f.write(@config.to_yaml)
303
- end
304
- rescue Errno::EACCES
305
- dialog = Gtk::MessageDialog.new(
306
- @window1,
307
- Gtk::Dialog::DESTROY_WITH_PARENT,
308
- Gtk::MessageDialog::ERROR,
309
- Gtk::MessageDialog::BUTTONS_CLOSE,
310
- "Permission denied:\n" + @configFile
311
- )
312
-
313
- dialog.run
314
- dialog.destroy
315
-
316
- on_menu_save_as(nil)
317
- end
318
- end
319
- end