transmission-rss 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +1,88 @@
1
- require( 'net/http' )
2
- require( 'json' )
3
- require( 'timeout' )
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'timeout'
4
4
 
5
5
  # TODO
6
6
  # * Why the hell do timeouts in getSessionID and addTorrent not work?!
7
7
 
8
8
  # Class for communication with transmission utilizing the RPC web interface.
9
9
  class TransmissionRSS::Client
10
- def initialize( host, port )
11
- @host = host
12
- @port = port
10
+ def initialize(host, port)
11
+ @host = host
12
+ @port = port
13
13
 
14
- @log = Log.instance
15
- end
14
+ @log = Log.instance
15
+ end
16
16
 
17
- # Get transmission session id by simple GET.
18
- def getSessionID
19
- get = Net::HTTP::Get.new(
20
- '/transmission/rpc'
21
- )
17
+ # Get transmission session id by simple GET.
18
+ def getSessionID
19
+ get = Net::HTTP::Get.new(
20
+ '/transmission/rpc'
21
+ )
22
22
 
23
- # retries = 3
24
- # begin
25
- # Timeout::timeout( 5 ) do
26
- response = Net::HTTP.new( @host, @port ).start do |http|
27
- http.request( get )
28
- end
29
- # end
30
- # rescue Timeout::Error
31
- # puts( 'timeout error exception' ) if( $verbose )
32
- # if( retries > 0 )
33
- # retries -= 1
34
- # puts( 'getSessionID timeout. retry..' ) if( $verbose )
35
- # retry
36
- # else
37
- # $stderr << "timeout http://#{@host}:#{@port}/transmission/rpc"
38
- # end
39
- # end
23
+ # retries = 3
24
+ # begin
25
+ # Timeout::timeout(5) do
26
+ response = Net::HTTP.new(@host, @port).start do |http|
27
+ http.request(get)
28
+ end
29
+ # end
30
+ # rescue Timeout::Error
31
+ # puts('timeout error exception') if($verbose)
32
+ # if(retries > 0)
33
+ # retries -= 1
34
+ # puts('getSessionID timeout. retry..') if($verbose)
35
+ # retry
36
+ # else
37
+ # $stderr << "timeout http://#{@host}:#{@port}/transmission/rpc"
38
+ # end
39
+ # end
40
40
 
41
- id = response.header['x-transmission-session-id']
41
+ id = response.header['x-transmission-session-id']
42
42
 
43
- @log.add( 'got session id ' + id )
43
+ @log.debug('got session id ' + id)
44
44
 
45
- id
46
- end
45
+ id
46
+ end
47
47
 
48
- # POST json packed torrent add command.
49
- def addTorrent( torrentFile, paused = false )
50
- post = Net::HTTP::Post.new(
51
- '/transmission/rpc',
52
- initheader = {
53
- 'Content-Type' => 'application/json',
54
- 'X-Transmission-Session-Id' => getSessionID
55
- }
56
- )
48
+ # POST json packed torrent add command.
49
+ def addTorrent(torrentFile, paused = false)
50
+ post = Net::HTTP::Post.new(
51
+ '/transmission/rpc',
52
+ initheader = {
53
+ 'Content-Type' => 'application/json',
54
+ 'X-Transmission-Session-Id' => getSessionID
55
+ }
56
+ )
57
57
 
58
- post.body = {
59
- "method" => "torrent-add",
60
- "arguments" => {
61
- "paused" => paused,
62
- "filename" => torrentFile
63
- }
64
- }.to_json
58
+ post.body = {
59
+ "method" => "torrent-add",
60
+ "arguments" => {
61
+ "paused" => paused,
62
+ "filename" => torrentFile
63
+ }
64
+ }.to_json
65
65
 
66
- # retries = 3
67
- # begin
68
- # Timeout::timeout( 5 ) do
69
- response = Net::HTTP.new( @host, @port ).start do |http|
70
- http.request( post )
71
- end
72
- # end
73
- # rescue Timeout::Error
74
- # puts( 'timeout error exception' ) if( $verbose )
75
- # if( retries > 0 )
76
- # retries -= 1
77
- # puts( 'addTorrent timeout. retry..' ) if( $verbose )
78
- # retry
79
- # else
80
- # $stderr << "timeout http://#{@host}:#{@port}/transmission/rpc"
81
- # end
82
- # end
66
+ # retries = 3
67
+ # begin
68
+ # Timeout::timeout(5) do
69
+ response = Net::HTTP.new(@host, @port).start do |http|
70
+ http.request(post)
71
+ end
72
+ # end
73
+ # rescue Timeout::Error
74
+ # puts('timeout error exception') if($verbose)
75
+ # if(retries > 0)
76
+ # retries -= 1
77
+ # puts('addTorrent timeout. retry..') if($verbose)
78
+ # retry
79
+ # else
80
+ # $stderr << "timeout http://#{@host}:#{@port}/transmission/rpc"
81
+ # end
82
+ # end
83
83
 
84
- result = JSON.parse( response.body ).result
84
+ result = JSON.parse(response.body).result
85
85
 
86
- @log.add( 'addTorrent result: ' + result )
87
- end
86
+ @log.debug('addTorrent result: ' + result)
87
+ end
88
88
  end
@@ -1,17 +1,17 @@
1
- $:.unshift( File.dirname( __FILE__ ) )
1
+ $:.unshift(File.dirname(__FILE__))
2
2
 
3
- require( 'libglade2' )
3
+ require 'libglade2'
4
4
 
5
5
  dir = 'config-editor'
6
6
 
7
7
  blacklist = %w(
8
- listbox-original
8
+ listbox-original
9
9
  )
10
10
 
11
11
  blacklist.map! do |name|
12
- $:.first + '/' + dir + '/' + name + '.rb'
12
+ $:.first + '/' + dir + '/' + name + '.rb'
13
13
  end
14
14
 
15
- ( Dir.glob( $:.first + '/' + dir + '/*.rb' ) - blacklist ).each do |lib|
16
- require( lib )
15
+ (Dir.glob($:.first + '/' + dir + '/*.rb') - blacklist).each do |lib|
16
+ require lib
17
17
  end
@@ -1,93 +1,93 @@
1
1
  # http://www.lakeinfoworks.com/blog/wp-content/listbox.rb
2
2
  class TransmissionRSS::ListBox < Gtk::ScrolledWindow
3
- def initialize
4
- super
3
+ def initialize
4
+ super
5
5
 
6
- tbl = Gtk::Table.new( 2, 2, false )
6
+ tbl = Gtk::Table.new(2, 2, false)
7
7
 
8
- self.hscrollbar_policy = Gtk::PolicyType::AUTOMATIC
9
- self.vscrollbar_policy = Gtk::PolicyType::AUTOMATIC
10
- self.shadow_type = Gtk::ShadowType::NONE
11
- self.window_placement= Gtk::CornerType::TOP_LEFT
8
+ self.hscrollbar_policy = Gtk::PolicyType::AUTOMATIC
9
+ self.vscrollbar_policy = Gtk::PolicyType::AUTOMATIC
10
+ self.shadow_type = Gtk::ShadowType::NONE
11
+ self.window_placement= Gtk::CornerType::TOP_LEFT
12
12
 
13
- @renderer = Gtk::CellRendererText.new
13
+ @renderer = Gtk::CellRendererText.new
14
14
 
15
- # @renderer.set_property( 'background', 'lavender' )
16
- @renderer.set_property( 'background', 'white' )
17
- @renderer.set_property( 'foreground', 'black' )
15
+ # @renderer.set_property('background', 'lavender')
16
+ @renderer.set_property('background', 'white')
17
+ @renderer.set_property('foreground', 'black')
18
18
 
19
- @list_store = Gtk::ListStore.new( String )
20
- @tree_view = Gtk::TreeView.new( @list_store )
19
+ @list_store = Gtk::ListStore.new(String)
20
+ @tree_view = Gtk::TreeView.new(@list_store)
21
21
 
22
- @tree_view_col1 = Gtk::TreeViewColumn.new( '', @renderer, { :text => 0 } )
23
- @tree_view_col1.sizing = Gtk::TreeViewColumn::AUTOSIZE
22
+ @tree_view_col1 = Gtk::TreeViewColumn.new('', @renderer, { :text => 0 })
23
+ @tree_view_col1.sizing = Gtk::TreeViewColumn::AUTOSIZE
24
24
 
25
- @text_col = 0
25
+ @text_col = 0
26
26
 
27
- @tree_view.append_column( @tree_view_col1 )
28
- @tree_view.headers_visible = false
29
- @tree_view.selection.mode = Gtk::SELECTION_SINGLE
27
+ @tree_view.append_column(@tree_view_col1)
28
+ @tree_view.headers_visible = false
29
+ @tree_view.selection.mode = Gtk::SELECTION_SINGLE
30
30
 
31
- tbl.attach_defaults( @tree_view, 0, 2, 0, 2 )
31
+ tbl.attach_defaults(@tree_view, 0, 2, 0, 2)
32
32
 
33
- self.add_with_viewport(tbl)
34
- end
33
+ self.add_with_viewport(tbl)
34
+ end
35
35
 
36
- def add( *args )
37
- args.flatten.each do |arg|
38
- iter = @list_store.append
39
- iter.set_value( 0, arg.to_s )
40
- end
41
- end
36
+ def add(*args)
37
+ args.flatten.each do |arg|
38
+ iter = @list_store.append
39
+ iter.set_value(0, arg.to_s)
40
+ end
41
+ end
42
42
 
43
- def button_release( widget, event, type = 'text' )
44
- path, column, cell_x, cell_y = @tree_view.get_path_at_pos( event.x, event.y )
43
+ def button_release(widget, event, type = 'text')
44
+ path, column, cell_x, cell_y = @tree_view.get_path_at_pos(event.x, event.y)
45
45
 
46
- return( '' ) if( column.nil? )
46
+ return('') if(column.nil?)
47
47
 
48
- entry = case( type )
49
- when 'line_nbr'
50
- path
51
- when 'text'
52
- @tree_view.selection.selected[@tree_view.columns.index( column )]
53
- end
48
+ entry = case(type)
49
+ when 'line_nbr'
50
+ path
51
+ when 'text'
52
+ @tree_view.selection.selected[@tree_view.columns.index(column)]
53
+ end
54
54
 
55
- return( entry )
56
- end
55
+ return(entry)
56
+ end
57
57
 
58
- def clear
59
- @list_store.clear
60
- end
58
+ def clear
59
+ @list_store.clear
60
+ end
61
61
 
62
- def header=( string )
63
- @tree_view.headers_visible = true
64
- @tree_view_col1.title = string
65
- end
62
+ def header=(string)
63
+ @tree_view.headers_visible = true
64
+ @tree_view_col1.title = string
65
+ end
66
66
 
67
- # TODO produces Gtk-CRITICAL
68
- def remove( *args )
69
- args.each do |arg|
70
- iter = @list_store.iter_first
67
+ # TODO produces Gtk-CRITICAL
68
+ def remove(*args)
69
+ args.each do |arg|
70
+ iter = @list_store.iter_first
71
71
 
72
- begin
73
- if( iter.get_value( 0 ) == arg )
74
- @list_store.remove( iter )
75
- end
76
- end while( iter.next! )
77
- end
78
- end
72
+ begin
73
+ if(iter.get_value(0) == arg)
74
+ @list_store.remove(iter)
75
+ end
76
+ end while(iter.next!)
77
+ end
78
+ end
79
79
 
80
- def items
81
- array = []
80
+ def items
81
+ array = []
82
82
 
83
- iter = @list_store.iter_first
83
+ iter = @list_store.iter_first
84
84
 
85
- if( not iter.nil? )
86
- begin
87
- array << iter.get_value( 0 )
88
- end while( iter.next! )
89
- end
85
+ if(not iter.nil?)
86
+ begin
87
+ array << iter.get_value(0)
88
+ end while(iter.next!)
89
+ end
90
90
 
91
- array
92
- end
91
+ array
92
+ end
93
93
  end
@@ -1,319 +1,319 @@
1
1
  # Represents the config editor window.
2
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( configFile, config )
20
- @configFile = configFile
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
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(configFile, config)
20
+ @configFile = configFile
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
319
  end