]>
Commit | Line | Data |
---|---|---|
823e558b BB |
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) | |
4f3dbb13 BB |
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 | |
823e558b | 29 | else |
4f3dbb13 | 30 | raise UnknownSource |
823e558b BB |
31 | end |
32 | end | |
33 | ||
34 | def disable(source_name) | |
4f3dbb13 BB |
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 | |
823e558b | 42 | else |
4f3dbb13 | 43 | raise UnknownSource |
823e558b BB |
44 | end |
45 | end | |
46 | ||
47 | def reset(source_name) | |
4f3dbb13 BB |
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 | |
823e558b | 55 | else |
4f3dbb13 | 56 | raise UnknownSource |
823e558b BB |
57 | end |
58 | end | |
59 | ||
60 | def current_track | |
61 | track = nil | |
62 | @enabled_sources.each do |source| | |
63 | begin | |
4f3dbb13 BB |
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 | |
823e558b | 69 | rescue |
4f3dbb13 | 70 | raise SourceConfigurationException |
823e558b BB |
71 | end |
72 | end | |
73 | track | |
74 | end | |
b9f550e9 | 75 | |
4f3dbb13 | 76 | def available_sources(format = false) |
b9f550e9 BB |
77 | path_root = File.expand_path(File.dirname(__FILE__)) |
78 | sources = Dir[path_root+"/sources/*"].map{ |s| | |
4f3dbb13 BB |
79 | name = s.split("/").last.gsub(/\.rb/, "") |
80 | ||
81 | # Add a star to denote enabled sources | |
82 | name | |
b9f550e9 BB |
83 | } |
84 | ||
85 | # Remove arguments (Hack?) We don't want anybody to touch tihs one. | |
86 | sources.delete("arguments") | |
4f3dbb13 BB |
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 | } | |
b9f550e9 | 99 | end |
823e558b BB |
100 | end |
101 | end |