59
60
61
62
63
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
93
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/ektoplayer/application.rb', line 59
def run
puts "\033]0;ektoplayer\007"
Process.setproctitle('ektoplayer')
Thread.report_on_exception=(true) if Thread.respond_to? :report_on_exception
[Config, Bindings, Theme].each { |c| Common::mksingleton(c) }
if File.file? Config::CONFIG_FILE
Config.parse(Config::CONFIG_FILE, Bindings, Theme) rescue (
fail "Config: #{$!}"
)
end
FileUtils::mkdir_p Config::CONFIG_DIR rescue (
fail "Could not create config dir: #{$!}"
)
Application.open_log(Config[:log_file])
if Config[:use_cache]
unless File.directory? Config[:cache_dir]
FileUtils::mkdir Config[:cache_dir] rescue (
fail "Could not create cache dir: #{$!}"
)
end
end
[:temp_dir, :download_dir, :archive_dir].each do |key|
unless File.directory? Config[key]
FileUtils::mkdir Config[key] rescue (
fail "Could not create #{key}: #{$!}"
)
end
end
UI::Canvas.run do
Application.log(self, "using '#{$USING_CURSES}' with #{ICurses.colors} colors available")
if Config[:use_colors] == :auto
Theme.use_colors(ICurses.colors >= 256 ? 256 : 8)
else
Theme.use_colors(Config[:use_colors])
end
client = Client.new
player = Models::Player.new(client, Config[:audio_system])
browser = Models::Browser.new(client)
playlist = Models::Playlist.new
database = Models::Database.new(client)
trackloader = Models::Trackloader.new(client)
operations = Operations::Operations.new
operations.register(:quit) do
Thread.list.each { |t| t.kill if t != Thread.current }
FileUtils.rm(Dir.glob(File.join(Config[:temp_dir], '~ekto-*'))) rescue nil
raise SystemExit
end
operations.register(:reload, &browser.method(:reload))
operations.register(:update, &database.method(:update))
operations.register(:refresh) { UI::Canvas.update_screen(true, true) }
Operations::Player.new(operations, player)
Operations::Browser.new(operations, browser, playlist)
Operations::Playlist.new(operations, playlist, player, trackloader)
main_w = UI::Canvas.sub(Views::MainWindow)
Thread.new do
begin
view_ops = Operations::Operations.new
Controllers::MainWindow.new(main_w, view_ops)
Controllers::Browser.new(main_w.browser, browser, view_ops, operations)
Controllers::Playlist.new(main_w.playlist, playlist, view_ops, operations)
Controllers::Help.new(main_w.help, view_ops)
Controllers::Info.new(main_w.info, player, playlist, trackloader, database, view_ops)
main_w.progressbar.attach(player)
main_w.playinginfo.attach(playlist, player)
database.events.on(:update_finished, &browser.method(:reload))
player.events.on(:stop) do |reason|
operations.send(:'playlist.play_next') if reason == :track_completed
end
Bindings.bind_view(:global, main_w, view_ops, operations)
%w(splash playlist browser info help).each do |w|
Bindings.bind_view(w, main_w.send(w), view_ops, operations)
end
player.stop
if (n = Config[:playlist_load_newest]) > 0
r = client.database.select(
order_by: 'date DESC,album,number',
limit: n
)
playlist.add(*r)
end
if browser.tracks(0).size < 1
operations.send(:update)
elsif (c = Config[:small_update_pages]) > 0
operations.send(:update, pages: c)
end
if Config[:prefetch]
Thread.new do
current_download_track = nil
loop do
sleep 5
next_track = playlist.get_next_pos
next if current_download_track == next_track
if player.length > 30 and player.position_percent > 0.5
trackloader.get_track_file(playlist[next_track]['url'])
current_download_track = next_track
sleep 5
end
end
end
end
rescue
Application.log(self, $!)
end
end
UI::Canvas.update_screen
UI::Input.start_loop
end
rescue
puts "Error: #{$!}"
Application.log(self, $!)
end
|