Class: Ektoplayer::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/ektoplayer/application.rb

Constant Summary collapse

VERSION =
'0.1.21'.freeze
GITHUB_URL =
'https://github.com/braph/ektoplayer'.freeze
EKTOPLAZM_URL =
'http://www.ektoplazm.com'.freeze
EKTOPLAZM_ALBUM_BASE_URL =
"#{EKTOPLAZM_URL}/free-music".freeze
EKTOPLAZM_COVER_BASE_URL =
"#{EKTOPLAZM_URL}/img".freeze
EKTOPLAZM_TRACK_BASE_URL =
"#{EKTOPLAZM_URL}/mp3".freeze
EKTOPLAZM_STYLE_BASE_URL =
"#{EKTOPLAZM_URL}/style".freeze
EKTOPLAZM_ARCHIVE_BASE_URL =
"#{EKTOPLAZM_URL}/files".freeze
CONFIG_DIR =
File.join(Dir.home, '.config', 'ektoplayer').freeze
CONFIG_FILE =
File.join(CONFIG_DIR, 'ektoplayer.rc').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.album_url(album) ⇒ Object



26
27
28
# File 'lib/ektoplayer/application.rb', line 26

def self.album_url(album)
   "#{EKTOPLAZM_ALBUM_BASE_URL}/#{album}"
end

.archive_url(archive) ⇒ Object



42
43
44
# File 'lib/ektoplayer/application.rb', line 42

def self.archive_url(archive)
   "#{EKTOPLAZM_ARCHIVE_BASE_URL}/#{archive}"
end

.cover_url(cover) ⇒ Object



30
31
32
# File 'lib/ektoplayer/application.rb', line 30

def self.cover_url(cover)
   "#{EKTOPLAZM_COVER_BASE_URL}/#{cover}"
end

.log(from, *msgs) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ektoplayer/application.rb', line 46

def self.log(from, *msgs)
   func = caller[0][/`.*'/][1..-2]
   from = from.class unless from.is_a?String
   $stderr.puts("#{DateTime.now.rfc3339} #{from}.#{func}: " + msgs.join(' '))

   if e = msgs.select { |m| m.kind_of?Exception }[0] 
      $stderr.puts "#{e.backtrace.first}: #{e.message} (#{e.class})", e.backtrace.join(?\n)
   end
   $stderr.flush
end

.open_log(file) ⇒ Object



57
58
59
60
# File 'lib/ektoplayer/application.rb', line 57

def self.open_log(file)
   $stderr.reopen(file, ?a)
   $stderr.sync=(true)
end

.style_url(style) ⇒ Object



38
39
40
# File 'lib/ektoplayer/application.rb', line 38

def self.style_url(style)
   "#{EKTOPLAZM_STYLE_BASE_URL}/#{style}"
end

.track_url(track) ⇒ Object



34
35
36
# File 'lib/ektoplayer/application.rb', line 34

def self.track_url(track)
   "#{EKTOPLAZM_TRACK_BASE_URL}/#{track}"
end

Instance Method Details

#runObject



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
207
208
209
# File 'lib/ektoplayer/application.rb', line 62

def run
   puts "\033]0;ektoplayer\007"
   Process.setproctitle('ektoplayer')
   Thread.report_on_exception=(true) if Thread.respond_to? :report_on_exception

   # make each configuration object globally accessible as a singleton
   [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

      # ... models ...
      player      = Models::Player.new(client)
      browser     = Models::Browser.new(client)
      playlist    = Models::Playlist.new
      database    = Models::Database.new(client)
      trackloader = Models::Trackloader.new(client)

      # ... operations ...
      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)

      # ... views ...
      main_w = UI::Canvas.sub(Views::MainWindow)

      # next operations may take some time, espacially the ones
      # using the database (browser), so we put this inside a thread
      Thread.new do
         begin

         # ... controllers ...
         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)

         # ... events ...
         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 ...
         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

         # Preload playlist
         if (n = Config[:playlist_load_newest]) > 0
            r = client.database.select(
               order_by: 'date DESC,album,number',
               limit: n
            )
            playlist.add(*r)
         end

         # If database is empty, start an initial update
         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