]> git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/source_manager.rb
Enables rdio. :)
[rbdr/lyricli.rb] / lib / lyricli / source_manager.rb
1 module Lyricli
2 class SourceManager
3
4 include Util
5
6 def initialize
7 @enabled_sources = []
8 @config = Configuration.instance
9 @config["enabled_sources"].each do |source|
10 if klass = parse_class(camelize(source))
11 current_source = klass.new
12 @enabled_sources << current_source
13 else
14 raise StartSourceException
15 end
16 end
17 end
18
19 def enable(source_name)
20 if available_sources.include?(source_name)
21 if klass = parse_class(camelize(source_name))
22 klass.enable
23 @config["enabled_sources"] << klass.name
24 @config["enabled_sources"].uniq!
25 @config.save_config
26 else
27 raise EnableSourceException
28 end
29 else
30 raise UnknownSource
31 end
32 end
33
34 def disable(source_name)
35 if available_sources.include?(source_name)
36 if klass = parse_class(camelize(source_name))
37 @config["enabled_sources"].delete(klass.name)
38 @config.save_config
39 else
40 raise DisableSourceException
41 end
42 else
43 raise UnknownSource
44 end
45 end
46
47 def reset(source_name)
48 if available_sources.include?(source_name)
49 if klass = parse_class(camelize(source_name))
50 klass.reset
51 disable(source_name)
52 else
53 raise ResetSourceException
54 end
55 else
56 raise UnknownSource
57 end
58 end
59
60 def current_track
61 track = nil
62 @enabled_sources.each do |source|
63 begin
64 current_track = source.current_track
65
66 unless current_track[:artist].nil? || current_track[:artist].empty? || current_track[:song].nil? || current_track[:song].empty?
67 track = current_track
68 end
69 rescue
70 raise SourceConfigurationException
71 end
72 end
73 track
74 end
75
76 def available_sources(format = false)
77 path_root = File.expand_path(File.dirname(__FILE__))
78 sources = Dir[path_root+"/sources/*"].map{ |s|
79 name = s.split("/").last.gsub(/\.rb/, "")
80
81 # Add a star to denote enabled sources
82 name
83 }
84
85 # Remove arguments (Hack?) We don't want anybody to touch tihs one.
86 sources.delete("arguments")
87 if format
88 format_sources(sources)
89 else
90 sources
91 end
92 end
93
94 def format_sources(sources)
95 sources.map{ |s|
96 s << "*" if @config["enabled_sources"].include?(s)
97 s
98 }
99 end
100 end
101 end