Class: Application

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_lyrics/cli/wikilyrics.rb,
lib/lyrics_ebook/lyric_download.rb,
lib/wiki_lyrics/amarok/wikilyrics.rb

Constant Summary collapse

@@MENU_ENTRY_SEARCH_CURRENT =
I18n.get( "amarok.application.search.current" )
@@MENU_ENTRY_SEARCH_SELECTED =
I18n.get( "amarok.application.search.selected" )
@@MENU_ENTRY_CLEAR_LYRICS_CACHE =
I18n.get( "amarok.application.clearlyricscache" )
@@SCRIPT_NAME =
"Wiki-Lyrics"
@@CONFIG_FILE =
"wikilyrics.xml"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 29

def initialize( featuring_fix, cleanup_lyrics, submit, review=true,
				prompt_autogen=false, prompt_no_lyrics=false, username=nil, password=nil )
	@featuring_fix = featuring_fix
	@submit_plugin = nil
	@logger = Logger.new( "#{ENV["HOME"]}/.wikilyrics.log" )
	Plugins.all_plugins().each() do |plugin|
		plugin.cleanup_lyrics = cleanup_lyrics
		plugin.logger = @logger
		if plugin.plugin_name() == submit
			@submit_plugin = plugin
			@submit_plugin.set_submit_settings( username, password, review, prompt_autogen, prompt_no_lyrics )
		end
	end
end

Class Method Details

.notify(message) ⇒ Object



48
49
50
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 48

def Application.notify( message )
	puts "Wiki-Lyrics: " + message.gsub( /\/?<[^>]+>/, "" )
end

Instance Method Details

#add_custom_menu_item(menu_item) ⇒ Object



100
101
102
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 100

def add_custom_menu_item( menu_item )
	Amarok.add_custom_menu_item( @@SCRIPT_NAME, menu_item )
end

#cleanup_lyrics=(cleanup_lyrics) ⇒ Object



79
80
81
82
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 79

def cleanup_lyrics=( cleanup_lyrics )
	@cleanup_lyrics = cleanup_lyrics
	Plugins.all_plugins().each() { |plugin| plugin.cleanup_lyrics = cleanup_lyrics }
end

#cleanup_lyrics?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 75

def cleanup_lyrics?()
	return @cleanup_lyrics
end

#execObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 393

def exec()

	@threads = []
	@messages = []

	read_config()

	begin

		on_start()

		single_threaded = self.single_threaded?

		GUI.set_run_out_of_process( ! single_threaded )

		exec_messages_loop( single_threaded )

	rescue Errno::ECONNREFUSED => e
		popup( I18n.get( "amarok.application.error.connectionrefused" ) )
		popup( e.backtrace().join( "<br/>" ) )

	rescue SocketError, TimeoutError => e
		popup( I18n.get( "amarok.application.error.connectiondown" ) )

	rescue Exception => e # unexpected error
		if ! e.is_a?( SystemExit ) || e.status != 0
			$stderr << e.message() << "\n"
			$stderr << e.backtrace().join( "\n" )
			log( e.message() )
			log( e.backtrace().join( "\n" ), 0 )
			exit( 1 )
		end

	ensure
		write_config()
		on_quit()

	end

end

#fetch_lyrics(request) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 64

def fetch_lyrics( request )

	used_plugins = Plugins.all_plugins()

	response = nil
	response_plugin = nil
	shown_review_dialog = false
	submit_plugin_searched = false

	used_plugins.each() do |plugin|
		begin
			submit_plugin_searched = true if plugin == @submit_plugin
			response = plugin.lyrics_full_search( request )
			if response.lyrics
				response_plugin = plugin
				break
			end
		rescue TimeoutError
			notify( I18n.get( "cli.application.plugintimeout", plugin.plugin_name(), plugin.site_host() ) )
		end
	end

		if @submit_plugin
		shown_review_dialog = @submit_plugin.wiki_process_response( response, response_plugin, submit_plugin_searched )
	end

	return response, response_plugin, shown_review_dialog

end

#fetch_lyrics_from_url(request, url) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 329

def fetch_lyrics_from_url( request, url )

	used_plugins = Plugins.used_plugins()

	response = nil
	response_plugin = nil

	if used_plugins.empty?
		popup( I18n.get( "amarok.application.error.nopluginsselected" ) )
		response = Lyrics::Response.new( request )
	else
		searched_wiki_plugins = []
		used_plugins.each() do |plugin|
			begin
				next if ! plugin.known_url?( url )
				searched_wiki_plugins << plugin if plugin.is_a?( MediaWikiLyrics )
				response = plugin.lyrics_from_url( request, url )
				if response.lyrics
					response_plugin = plugin
					break
				end
			rescue TimeoutError
				notify( I18n.get( "amarok.application.search.plugintimeout", plugin.plugin_name(), plugin.site_host() ) )
			end
		end
		wikis_process_response( response, response_plugin, searched_wiki_plugins )
	end

	return response, response_plugin
end

#finalizeObject



44
45
46
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 44

def finalize()
	@logger.finalize() if @logger
end

#log(message, new_lines = 1) ⇒ Object



71
72
73
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 71

def log( message, new_lines=1 )
	@logger.log( message, new_lines ) if @logger
end

#log?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 62

def log?()
	return @logger != nil
end

#logger=(logger) ⇒ Object



66
67
68
69
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 66

def logger=( logger )
	@logger = logger
	Plugins.all_plugins().each() { |plugin| plugin.logger = @logger }
end

#notify(message) ⇒ Object



52
53
54
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 52

def notify( message )
	self.class.notify( message )
end

#on_configureObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 151

def on_configure()

	values = {
		"cleanup_lyrics" => cleanup_lyrics?(),
		"single_threaded" => single_threaded?(),
		"write_log" => log?,
		"used_plugins" => Plugins.used_names(),
		"script_name" => @@SCRIPT_NAME,
		"unused_plugins" => Plugins.all_names() - Plugins.used_names()
	}
	if GUI.show_plugins_manager_dialog( values )
		self.logger = values["write_log"].to_s() == "true" ? Logger.new( $LOG_FILEPATH, $LOG_TRUNCATE ) : nil
		self.cleanup_lyrics = values["cleanup_lyrics"].to_s() == "true"
		self.single_threaded = values["single_threaded"].to_s() == "true"
		Plugins.used_names = values["used_plugins"]
	end
	Plugins.all_plugins().each() { |plugin| plugin.configure() }

	write_config()

end

#on_custom_menu_item_selected(menu, item, urls) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 210

def on_custom_menu_item_selected( menu, item, urls )
	if menu == @@SCRIPT_NAME
		if item == @@MENU_ENTRY_SEARCH_CURRENT
			on_search_current_lyrics()
		elsif item == @@MENU_ENTRY_SEARCH_SELECTED
			url = URI.parse( urls[0] )
			return if url.scheme != "file"
			on_search_file_lyrics( URI.decode( url.path ) )
		elsif item == @@MENU_ENTRY_CLEAR_LYRICS_CACHE
			if GUI.show_confirmation_dialog( I18n.get( "amarok.application.clearlyricscache.confirm" ), @@SCRIPT_NAME )
				Amarok.query( "DELETE FROM lyrics WHERE url <> ''" )
				notify( I18n.get( "amarok.application.clearlyricscache.done" ) )
			end
		end
	elsif plugin = Plugins.plugin_by_name( menu )
		plugin.on_custom_menu_item_selected( menu, item, urls )
	end
end

#on_fetch_lyrics(request) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 289

def on_fetch_lyrics( request )

	# NOTE: we must check that the current song doesn't change while we're fetching
	# the lyrics, otherwise Amarok will associate the lyrics with the wrong song
	prev_artist = Strings.normalize!( Amarok.get_current_artist().to_s() )
	prev_title  = Strings.normalize!( Amarok.get_current_title().to_s() )

	response, plugin, suggestions = fetch_lyrics( request )
	lyrics_data = build_lyrics_data_hash( response, plugin )

	return if Strings.normalize!( Amarok.get_current_artist().to_s() ) != prev_artist ||
			  Strings.normalize!( Amarok.get_current_title().to_s() ) != prev_title

	if lyrics_data["lyrics"]
		Amarok.show_lyrics(
			lyrics_data["artist"],
			lyrics_data["title"],
			lyrics_data["lyrics"],
			lyrics_data["site_name"],
			lyrics_data["site_host"],
			lyrics_data["url"],
			lyrics_data["add_url"]
		)
	elsif suggestions.size > 0
		Amarok.show_suggestions(
			lyrics_data["artist"],
			lyrics_data["title"],
			suggestions,
			lyrics_data["add_url"]
		)
	else
		Amarok.show_not_found(
			lyrics_data["artist"],
			lyrics_data["title"],
			lyrics_data["add_url"]
		)
	end

end

#on_fetch_lyrics_from_url(request, url) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 360

def on_fetch_lyrics_from_url( request, url )

	# NOTE: we must check that the current song doesn't change while we're fetching
	# the lyrics, otherwise Amarok will associate the lyrics with the wrong song
	prev_artist = Strings.normalize!( Amarok.get_current_artist().to_s() )
	prev_title  = Strings.normalize!( Amarok.get_current_title().to_s() )

	response, plugin = fetch_lyrics_from_url( request, url )
	lyrics_data = build_lyrics_data_hash( response, plugin )

	return if Strings.normalize!( Amarok.get_current_artist().to_s() ) != prev_artist ||
			  Strings.normalize!( Amarok.get_current_title().to_s() ) != prev_title

	if lyrics_data["lyrics"]
		Amarok.show_lyrics(
			lyrics_data["artist"],
			lyrics_data["title"],
			lyrics_data["lyrics"],
			lyrics_data["site_name"],
			lyrics_data["site_host"],
			lyrics_data["url"],
			lyrics_data["add_url"]
		)
	else
		Amarok.show_not_found(
			lyrics_data["artist"],
			lyrics_data["title"],
			lyrics_data["add_url"]
		)
	end

end

#on_quitObject



144
145
146
147
148
149
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 144

def on_quit()
	remove_custom_menu_item( @@MENU_ENTRY_SEARCH_CURRENT )
	remove_custom_menu_item( @@MENU_ENTRY_SEARCH_SELECTED )
	remove_custom_menu_item( @@MENU_ENTRY_CLEAR_LYRICS_CACHE )
	Plugins.all_plugins().each() { |plugin| plugin.on_quit() }
end

#on_search_current_lyricsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 174

def on_search_current_lyrics()
	if Amarok.playing?()
		song_data = Amarok.query_song_data( Amarok.get_current_path() )
		if GUI.show_search_lyrics_dialog( song_data )
			on_fetch_lyrics( Lyrics::Request.new(
				song_data["artist"],
				song_data["title"],
				song_data["album"],
				song_data["year"]
			) )
		end
	else
		notify( I18n.get( "amarok.application.search.current.nosongplaying" ) )
	end
end

#on_search_file_lyrics(file) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 190

def on_search_file_lyrics( file )
	if (song_data = Amarok.query_song_data( file )).empty?
		notify( I18n.get( "amarok.application.search.noinfofound" ) )
	elsif GUI.show_search_lyrics_dialog( song_data )
		request = Lyrics::Request.new( song_data["artist"], song_data["title"], song_data["album"], song_data["year"] )
		response, plugin, suggestions = fetch_lyrics( request )
		if response.lyrics
			dialog_data = {
				"artist" =>		response.artist,
				"title" =>		response.title,
				"lyrics" =>		response.lyrics,
				"site_name" =>	plugin.site_name(),
			}
			GUI.show_lyrics_dialog( dialog_data )
		else
			notify( I18n.get( "amarok.application.search.nolyricsfound", song_data["title"], song_data["artist"] ) )
		end
	end
end

#on_startObject

EVENT HANDLERS



137
138
139
140
141
142
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 137

def on_start()
	add_custom_menu_item( @@MENU_ENTRY_SEARCH_CURRENT )
	add_custom_menu_item( @@MENU_ENTRY_SEARCH_SELECTED )
	add_custom_menu_item( @@MENU_ENTRY_CLEAR_LYRICS_CACHE )
	Plugins.all_plugins().each() { |plugin| plugin.on_start() }
end


96
97
98
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 96

def popup( message )
	Amarok.popup( "<b>[" + @@SCRIPT_NAME + "]</b><br/>" + message )
end

#process(artist, title, album, year) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 94

def process( artist, title, album, year )

	if @featuring_fix
		artist = Strings.cleanup_artist( artist, title )
		title  = Strings.cleanup_title( title )
	end

	notify( I18n.get( "cli.application.searchinglyrics", title, artist ) )

	request = Lyrics::Request.new( artist, title, album, year )
	response, response_plugin, shown_review_dialog = fetch_lyrics( request )

	if response.lyrics
		artist = response.artist ? response.artist : response.request.artist
		title = response.title ? response.title : response.request.title
		notify( I18n.get( "cli.application.lyricsfound", title, artist, response_plugin.site_name ) )
		if ! shown_review_dialog
			if GUI.get_current_toolkit()
				GUI.show_lyrics_dialog( {
					"artist" => artist,
					"title" => title,
					"lyrics" => response.lyrics,
					"site_name" => response_plugin.site_name
				} )
			else
				puts "\n#{response.lyrics}\n\n"
			end
		end
	else
		notify( I18n.get( "cli.application.nolyricsfound", request.title, request.artist ) )
	end

end

#read_configObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 108

def read_config()
	values = {
		"cleanup_lyrics" => cleanup_lyrics?(),
		"single_threaded" => single_threaded?(),
		"write_log" => log?,
		"used_plugins" => Plugins.used_names()
	}
	XMLHash.read( @@CONFIG_FILE, values )
	self.logger = values["write_log"].to_s() == "true" ? Logger.new( $LOG_FILEPATH, $LOG_TRUNCATE ) : nil
	self.cleanup_lyrics = values["cleanup_lyrics"].to_s() == "true"
	self.single_threaded = values["single_threaded"].to_s() == "true"
	Plugins.used_names = values["used_plugins"]
	Plugins.all_plugins().each() { |plugin| plugin.read_config( @@CONFIG_FILE ) }
end

#remove_custom_menu_item(menu_item) ⇒ Object



104
105
106
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 104

def remove_custom_menu_item( menu_item )
	Amarok.remove_custom_menu_item( @@SCRIPT_NAME, menu_item )
end

#restore_session(session_file) ⇒ Object



56
57
58
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 56

def restore_session( session_file )
	@submit_plugin.restore_session( session_file ) if @submit_plugin
end

#save_session(session_file) ⇒ Object



60
61
62
# File 'lib/wiki_lyrics/cli/wikilyrics.rb', line 60

def save_session( session_file )
	@submit_plugin.save_session( session_file ) if @submit_plugin
end

#single_threaded=(single_threaded) ⇒ Object



88
89
90
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 88

def single_threaded=( single_threaded )
	@single_threaded = single_threaded
end

#single_threaded?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 84

def single_threaded?()
	return @single_threaded
end

#wikis_process_response(response, response_plugin, searched_wiki_plugins) ⇒ Object



242
243
244
245
246
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 242

def wikis_process_response( response, response_plugin, searched_wiki_plugins )
	Plugins.wiki_plugins().each() do |plugin|
		plugin.wiki_process_response( response, response_plugin, searched_wiki_plugins.include?( plugin ) )
	end
end

#write_configObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/wiki_lyrics/amarok/wikilyrics.rb', line 123

def write_config()
	values = {
		"cleanup_lyrics" => cleanup_lyrics?(),
		"single_threaded" => single_threaded?(),
		"write_log" => log?,
		"used_plugins" => Plugins.used_names()
	}
	XMLHash.write( @@CONFIG_FILE, values )
	Plugins.all_plugins().each() { |plugin| plugin.write_config( @@CONFIG_FILE ) }
end