music-dl/src/controller/mainController.py

253 lines
10 KiB
Python
Raw Normal View History

2018-01-24 17:43:35 -06:00
# -*- coding: utf-8 -*-
""" main controller for MusicDL"""
from __future__ import unicode_literals # at top of module
import webbrowser
2018-01-24 17:43:35 -06:00
import wx
import logging
import widgetUtils
import utils
import application
2018-01-24 17:43:35 -06:00
from pubsub import pub
from wxUI import mainWindow, menus
2018-03-02 14:05:38 -06:00
from extractors import zaycev, youtube, vk, mailru
from update import updater
2018-01-24 17:43:35 -06:00
from . import player
log = logging.getLogger("controller.main")
class Controller(object):
def __init__(self):
super(Controller, self).__init__()
log.debug("Starting main controller...")
# Setting up the player object
player.setup()
# Get main window
self.window = mainWindow.mainWindow()
log.debug("Main window created")
2018-03-12 09:27:35 -06:00
self.window.change_status(_(u"Ready"))
2018-01-24 17:43:35 -06:00
# Here we will save results for searches as song objects.
self.results = []
self.connect_events()
self.timer = wx.Timer(self.window)
self.window.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(75)
self.window.vol_slider.SetValue(player.player.volume)
2018-01-24 17:43:35 -06:00
# Shows window.
utils.call_threaded(updater.do_update)
log.debug("Music DL is ready")
2018-01-24 17:43:35 -06:00
self.window.Show()
def get_status_info(self):
""" Formatting string for status bar messages """
if len(self.results) > 0:
2018-03-12 09:27:35 -06:00
results = _(u"Showing {0} results.").format(len(self.results))
2018-01-24 17:43:35 -06:00
else:
2018-03-12 09:27:35 -06:00
results = u""
2018-01-26 12:03:07 -06:00
if player.player.shuffle:
2018-03-12 09:27:35 -06:00
shuffle = _(u"Shuffle on")
2018-01-26 12:03:07 -06:00
else:
2018-03-12 09:27:35 -06:00
shuffle = u""
final = u"{0} {1}".format(results, shuffle)
2018-01-24 17:43:35 -06:00
return final
def connect_events(self):
""" connects all widgets to their corresponding events."""
log.debug("Binding events...")
2018-01-24 17:43:35 -06:00
widgetUtils.connect_event(self.window.search, widgetUtils.BUTTON_PRESSED, self.on_search)
widgetUtils.connect_event(self.window.list, widgetUtils.LISTBOX_ITEM_ACTIVATED, self.on_activated)
widgetUtils.connect_event(self.window.list, widgetUtils.KEYPRESS, self.on_keypress)
2018-01-25 17:18:51 -06:00
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_play, menuitem=self.window.player_play)
2018-01-24 17:43:35 -06:00
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_next, menuitem=self.window.player_next)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_previous, menuitem=self.window.player_previous)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_stop, menuitem=self.window.player_stop)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_volume_down, menuitem=self.window.player_volume_down)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_volume_up, menuitem=self.window.player_volume_up)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_mute, menuitem=self.window.player_mute)
2018-01-26 12:03:07 -06:00
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_shuffle, menuitem=self.window.player_shuffle)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.window.about_dialog, menuitem=self.window.about)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_check_for_updates, menuitem=self.window.check_for_updates)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_visit_changelog, menuitem=self.window.changelog)
widgetUtils.connect_event(self.window, widgetUtils.MENU, self.on_visit_website, menuitem=self.window.website)
widgetUtils.connect_event(self.window.previous, widgetUtils.BUTTON_PRESSED, self.on_previous)
widgetUtils.connect_event(self.window.play, widgetUtils.BUTTON_PRESSED, self.on_play_pause)
widgetUtils.connect_event(self.window.stop, widgetUtils.BUTTON_PRESSED, self.on_stop)
widgetUtils.connect_event(self.window.next, widgetUtils.BUTTON_PRESSED, self.on_next)
self.window.Bind(wx.EVT_COMMAND_SCROLL_THUMBTRACK, self.on_set_volume, self.window.vol_slider)
self.window.Bind(wx.EVT_COMMAND_SCROLL_CHANGED, self.on_set_volume, self.window.vol_slider)
self.window.Bind(wx.EVT_COMMAND_SCROLL_THUMBTRACK, self.on_time_change, self.window.time_slider)
self.window.Bind(wx.EVT_COMMAND_SCROLL_CHANGED, self.on_time_change, self.window.time_slider)
self.window.list.Bind(wx.EVT_LISTBOX_DCLICK, self.on_play)
self.window.list.Bind(wx.EVT_CONTEXT_MENU, self.on_context)
self.window.Bind(wx.EVT_CLOSE, self.on_close)
2018-01-24 17:43:35 -06:00
pub.subscribe(self.change_status, "change_status")
pub.subscribe(self.on_download_finished, "download_finished")
2018-03-02 14:05:38 -06:00
pub.subscribe(self.on_notify, "notify")
2018-01-24 17:43:35 -06:00
# Event functions. These functions will call other functions in a thread and are bound to widget events.
def on_search(self, *args, **kwargs):
utils.call_threaded(self.search)
def on_activated(self, *args, **kwargs):
2018-01-25 17:18:51 -06:00
self.on_play()
2018-01-24 17:43:35 -06:00
def on_keypress(self, ev):
if ev.GetKeyCode() == wx.WXK_RETURN:
return self.on_play()
2018-01-25 17:18:51 -06:00
elif ev.GetKeyCode() == wx.WXK_SPACE:
return self.on_play_pause()
elif ev.GetKeyCode() == wx.WXK_LEFT and ev.ShiftDown():
position = player.player.player.get_time()
if position > 5000:
player.player.player.set_time(position-5000)
else:
player.player.player.set_time(0)
return
elif ev.GetKeyCode() == wx.WXK_RIGHT and ev.ShiftDown():
position = player.player.player.get_time()
player.player.player.set_time(position+5000)
return
elif ev.GetKeyCode() == wx.WXK_UP and ev.ControlDown():
return self.on_volume_up()
elif ev.GetKeyCode() == wx.WXK_DOWN and ev.ControlDown():
return self.on_volume_down()
elif ev.GetKeyCode() == wx.WXK_LEFT and ev.AltDown():
return self.on_previous()
elif ev.GetKeyCode() == wx.WXK_RIGHT and ev.AltDown():
return self.on_next()
2018-01-24 17:43:35 -06:00
ev.Skip()
def on_play_pause(self, *args, **kwargs):
2018-02-19 11:45:57 -06:00
if player.player.player.is_playing() == 1:
2018-03-12 09:27:35 -06:00
self.window.play.SetLabel(_(u"Play"))
2018-01-24 17:43:35 -06:00
return player.player.pause()
2018-01-25 17:18:51 -06:00
else:
2018-03-12 09:27:35 -06:00
self.window.play.SetLabel(_(u"Pause"))
return player.player.player.play()
2018-01-24 17:43:35 -06:00
def on_next(self, *args, **kwargs):
2018-01-26 11:52:49 -06:00
return utils.call_threaded(player.player.next)
2018-01-24 17:43:35 -06:00
def on_previous(self, *args, **kwargs):
2018-01-26 11:52:49 -06:00
return utils.call_threaded(player.player.previous)
2018-01-24 17:43:35 -06:00
2018-01-25 17:18:51 -06:00
def on_play(self, *args, **kwargs):
2018-01-26 11:52:49 -06:00
items = self.results[::]
playing_item = self.window.get_item()
2018-03-12 09:27:35 -06:00
self.window.play.SetLabel(_(u"Pause"))
2018-01-26 11:52:49 -06:00
return utils.call_threaded(player.player.play_all, items, playing=playing_item, shuffle=self.window.player_shuffle.IsChecked())
2018-01-24 17:43:35 -06:00
def on_stop(self, *args, **kwargs):
player.player.stop()
2018-03-12 09:27:35 -06:00
self.window.play.SetLabel(_(u"Play"))
2018-01-24 17:43:35 -06:00
def on_volume_down(self, *args, **kwargs):
self.window.vol_slider.SetValue(self.window.vol_slider.GetValue()-5)
self.on_set_volume()
2018-01-24 17:43:35 -06:00
def on_volume_up(self, *args, **kwargs):
self.window.vol_slider.SetValue(self.window.vol_slider.GetValue()+5)
self.on_set_volume()
2018-01-24 17:43:35 -06:00
def on_mute(self, *args, **kwargs):
self.window.vol_slider.SetValue(0)
self.on_set_volume()
2018-01-24 17:43:35 -06:00
2018-01-26 12:03:07 -06:00
def on_shuffle(self, *args, **kwargs):
player.player.shuffle = self.window.player_shuffle.IsChecked()
def on_context(self, *args, **kwargs):
item = self.window.get_item()
if item == -1:
return wx.Bell()
menu = menus.contextMenu()
widgetUtils.connect_event(menu, widgetUtils.MENU, self.on_play, menuitem=menu.play)
widgetUtils.connect_event(menu, widgetUtils.MENU, self.on_download, menuitem=menu.download)
self.window.PopupMenu(menu, wx.GetMousePosition())
menu.Destroy()
def on_download(self, *args, **kwargs):
item = self.results[self.window.get_item()]
log.debug("Starting requested download: {0} (using extractor: {1})".format(item.title, self.extractor.name))
f = "{0}.mp3".format(item.format_track())
if item.download_url == "":
item.get_download_url()
path = self.window.get_destination_path(f)
if path != None:
log.debug("User has requested the following path: {0}".format(path,))
if self.extractor.needs_transcode == True: # Send download to vlc based transcoder
utils.call_threaded(player.player.transcode_audio, item, path)
else:
log.debug("downloading %s URL to %s filename" % (item.download_url, path,))
utils.call_threaded(utils.download_file, item.download_url, path)
def on_set_volume(self, *args, **kwargs):
volume = self.window.vol_slider.GetValue()
player.player.volume = volume
def on_time_change(self, event, *args, **kwargs):
p = event.GetPosition()
player.player.player.set_position(p/100.0)
event.Skip()
def on_timer(self, *args, **kwargs):
if not self.window.time_slider.HasFocus():
progress = player.player.player.get_position()*100
self.window.time_slider.SetValue(progress)
def on_close(self, event):
log.debug("Exiting...")
self.timer.Stop()
pub.unsubscribe(self.on_download_finished, "download_finished")
event.Skip()
widgetUtils.exit_application()
2018-01-24 17:43:35 -06:00
def change_status(self, status):
""" Function used for changing the status bar from outside the main controller module."""
2018-03-12 09:27:35 -06:00
self.window.change_status(u"{0} {1}".format(status, self.get_status_info()))
2018-01-24 17:43:35 -06:00
def on_visit_website(self, *args, **kwargs):
webbrowser.open_new_tab(application.url)
def on_visit_changelog(self, *args, **kwargs):
webbrowser.open_new_tab(application.url+"/news")
def on_check_for_updates(self, *args, **kwargs):
utils.call_threaded(updater.do_update)
def on_download_finished(self, file):
title = "MusicDL"
2018-03-12 09:27:35 -06:00
msg = _(u"File downloaded: {0}").format(file,)
self.window.notify(title, msg)
2018-03-02 14:05:38 -06:00
def on_notify(self, title, message):
self.window.notify(title, message)
2018-01-24 17:43:35 -06:00
# real functions. These functions really are doing the work.
def search(self, *args, **kwargs):
text = self.window.get_text()
if text == "":
return
extractor = self.window.extractor.GetValue()
if extractor == "youtube":
self.extractor = youtube.interface()
2018-03-01 10:42:47 -06:00
elif extractor == "vk":
self.extractor = vk.interface()
2018-03-02 14:05:38 -06:00
elif extractor == "mail.ru":
self.extractor = mailru.interface()
elif extractor == "zaycev.net":
self.extractor = zaycev.interface()
elif extractor == "":
return
log.debug("Started search for {0} (selected extractor: {1})".format(text, self.extractor.name))
2018-01-24 17:43:35 -06:00
self.window.list.Clear()
2018-03-12 09:27:35 -06:00
self.change_status(_(u"Searching {0}... ").format(text))
2018-01-24 17:43:35 -06:00
self.extractor.search(text)
self.results = self.extractor.results
for i in self.results:
self.window.list.Append(i.format_track())
if len(self.results) == 0:
2018-03-12 09:27:35 -06:00
self.change_status(_(u"No results found. "))
else:
2018-03-12 09:27:35 -06:00
self.change_status(u"")
wx.CallAfter(self.window.list.SetFocus)