CoverArt Browser
v2.0
Browse your cover-art albums in Rhythmbox
|
00001 # -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- 00002 # 00003 # Copyright (C) 2012 - fossfreedom 00004 # Copyright (C) 2012 - Agustin Carrasco 00005 # 00006 # This program is free software; you can redistribute it and/or modify 00007 # it under the terms of the GNU General Public License as published by 00008 # the Free Software Foundation; either version 2, or (at your option) 00009 # any later version. 00010 # 00011 # This program is distributed in the hope that it will be useful, 00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 # GNU General Public License for more details. 00015 # 00016 # You should have received a copy of the GNU General Public License 00017 # along with this program; if not, write to the Free Software 00018 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 00019 00020 from gi.repository import GObject 00021 from gi.repository import Gtk 00022 from gi.repository import RB 00023 from gi.repository import Gio 00024 00025 from coverart_browser_prefs import GSetting 00026 from coverart_browser_prefs import CoverLocale 00027 from coverart_utils import Theme 00028 from coverart_controllers import PlaylistPopupController 00029 from coverart_controllers import GenrePopupController 00030 from coverart_controllers import SortPopupController 00031 from coverart_controllers import ArtistSortPopupController 00032 from coverart_controllers import PropertiesMenuController 00033 from coverart_controllers import DecadePopupController 00034 from coverart_controllers import SortOrderToggleController 00035 from coverart_controllers import ArtistSortOrderToggleController 00036 from coverart_controllers import AlbumSearchEntryController 00037 from coverart_widgets import SearchEntry 00038 from coverart_browser_prefs import webkit_support 00039 import rb 00040 00041 00042 class Toolbar(GObject.Object): 00043 def __init__(self, plugin, mainbox, controllers): 00044 super(Toolbar, self).__init__() 00045 00046 self.plugin = plugin 00047 self.mainbox = mainbox 00048 cl = CoverLocale() 00049 00050 ui_file = rb.find_plugin_file(plugin, self.ui) 00051 00052 # create the toolbar 00053 builder = Gtk.Builder() 00054 builder.set_translation_domain(cl.Locale.LOCALE_DOMAIN) 00055 00056 builder.add_from_file(ui_file) 00057 00058 # assign the controllers to the buttons 00059 for button, controller in controllers.items(): 00060 if button != 'search': 00061 builder.get_object(button).controller = controller 00062 00063 if not webkit_support(): 00064 #button = builder.get_object('flowview_button') 00065 #button.set_visible(False) 00066 separator = builder.get_object('properties_separator') 00067 if separator: 00068 separator.set_visible(False) 00069 00070 # workaround to translate the search entry tooltips 00071 cl.switch_locale(cl.Locale.RB) 00072 search_entry = SearchEntry(has_popup=True) 00073 search_entry.show_all() 00074 cl.switch_locale(cl.Locale.LOCALE_DOMAIN) 00075 00076 # add it to the ui 00077 align = builder.get_object('entry_search_alignment') 00078 align.add(search_entry) 00079 00080 # assign the controller 00081 search_entry.controller = controllers['search'] 00082 00083 Theme(self.plugin).connect('theme_changed', self._theme_changed, 00084 controllers) 00085 00086 self.builder = builder.get_object('toolbar') 00087 00088 #now theme the toolbar including child objects such as the button popups 00089 style_context = self.builder.get_style_context() 00090 style_context.add_class(Gtk.STYLE_CLASS_TOOLBAR) 00091 00092 def _theme_changed(self, toolbar, controllers): 00093 for controller in list(controllers.values()): 00094 controller.update_images(True) 00095 00096 00097 class TopToolbar(Toolbar): 00098 ui = 'ui/coverart_topbar.ui' 00099 name = 'main' 00100 00101 def hide(self): 00102 if self.builder.get_visible(): 00103 self.builder.hide() 00104 00105 def show(self): 00106 self.mainbox.pack_start(self.builder, False, True, 0) 00107 self.mainbox.reorder_child(self.builder, 0) 00108 self.builder.show() 00109 00110 00111 class LeftToolbar(Toolbar): 00112 ui = 'ui/coverart_leftsidebar.ui' 00113 name = 'left' 00114 00115 def hide(self): 00116 if self.builder.get_visible(): 00117 self.builder.hide() 00118 self.plugin.shell.remove_widget(self.builder, 00119 RB.ShellUILocation.SIDEBAR) 00120 00121 def show(self): 00122 self.plugin.shell.add_widget(self.builder, 00123 RB.ShellUILocation.SIDEBAR, expand=False, fill=False) 00124 self.builder.show() 00125 00126 00127 class RightToolbar(Toolbar): 00128 ui = 'ui/coverart_rightsidebar.ui' 00129 name = 'right' 00130 00131 def hide(self): 00132 if self.builder.get_visible(): 00133 self.builder.hide() 00134 self.plugin.shell.remove_widget(self.builder, 00135 RB.ShellUILocation.RIGHT_SIDEBAR) 00136 00137 def show(self): 00138 self.plugin.shell.add_widget(self.builder, 00139 RB.ShellUILocation.RIGHT_SIDEBAR, expand=False, fill=False) 00140 self.builder.show() 00141 00142 00143 class ToolbarObject(object): 00144 #properties 00145 00146 PROPERTIES = 'properties_button' 00147 SORT_BY = 'sort_by' 00148 SORT_ORDER = 'sort_order' 00149 SORT_BY_ARTIST = 'sort_by_artist' 00150 SORT_ORDER_ARTIST = 'sort_order_artist' 00151 GENRE = 'genre_button' 00152 PLAYLIST = 'playlist_button' 00153 DECADE = 'decade_button' 00154 SEARCH = 'search' 00155 VIEW = 'view_button' 00156 00157 00158 class ToolbarManager(GObject.Object): 00159 # properties 00160 toolbar_pos = GObject.property(type=str, default=TopToolbar.name) 00161 00162 def __init__(self, plugin, main_box, viewmgr): 00163 super(ToolbarManager, self).__init__() 00164 self.plugin = plugin 00165 # create the buttons controllers 00166 controllers = self._create_controllers(plugin, viewmgr) 00167 00168 # initialize toolbars 00169 self._bars = {} 00170 self._bars[TopToolbar.name] = TopToolbar(plugin, main_box, 00171 controllers) 00172 self._bars[LeftToolbar.name] = LeftToolbar(plugin, main_box, 00173 controllers) 00174 self._bars[RightToolbar.name] = RightToolbar(plugin, main_box, 00175 controllers) 00176 00177 self.last_toolbar_pos = None 00178 # connect signal and properties 00179 self._connect_signals() 00180 self._connect_properties() 00181 00182 self._controllers = controllers 00183 00184 def set_enabled(self, enabled, toolbar_object=None): 00185 ''' 00186 enable or disable the toolbar object. 00187 00188 :param enabled: `bool` value. 00189 :param toolbar_object: `ToolbarObject` 00190 None if enabled is to apply to all objects in the toolbar 00191 00192 ''' 00193 if toolbar_object: 00194 self._controllers[toolbar_object].enabled = enabled 00195 else: 00196 for controller in self._controllers: 00197 self._controllers[controller].enabled = enabled 00198 00199 def _connect_signals(self): 00200 self.connect('notify::toolbar-pos', self._on_notify_toolbar_pos) 00201 00202 def _connect_properties(self): 00203 gs = GSetting() 00204 setting = gs.get_setting(gs.Path.PLUGIN) 00205 setting.bind(gs.PluginKey.TOOLBAR_POS, self, 'toolbar_pos', 00206 Gio.SettingsBindFlags.GET) 00207 00208 def _create_controllers(self, plugin, viewmgr): 00209 controllers = {} 00210 00211 album_model = viewmgr.source.album_manager.model 00212 controllers[ToolbarObject.PROPERTIES] = \ 00213 PropertiesMenuController(plugin, viewmgr.source) 00214 controllers[ToolbarObject.SORT_BY] = \ 00215 SortPopupController(plugin, viewmgr) 00216 controllers[ToolbarObject.SORT_ORDER] = \ 00217 SortOrderToggleController(plugin, viewmgr) 00218 controllers[ToolbarObject.SORT_BY_ARTIST] = \ 00219 ArtistSortPopupController(plugin, viewmgr) 00220 controllers[ToolbarObject.SORT_ORDER_ARTIST] = \ 00221 ArtistSortOrderToggleController(plugin, viewmgr) 00222 controllers[ToolbarObject.GENRE] = \ 00223 GenrePopupController(plugin, album_model) 00224 controllers[ToolbarObject.PLAYLIST] = \ 00225 PlaylistPopupController(plugin, album_model) 00226 controllers[ToolbarObject.DECADE] = \ 00227 DecadePopupController(plugin, album_model) 00228 controllers[ToolbarObject.SEARCH] = \ 00229 AlbumSearchEntryController(album_model) 00230 00231 controllers[ToolbarObject.VIEW] = viewmgr.controller 00232 00233 return controllers 00234 00235 def _on_notify_toolbar_pos(self, *args): 00236 if self.last_toolbar_pos: 00237 self._bars[self.last_toolbar_pos].hide() 00238 00239 self._bars[self.toolbar_pos].show() 00240 00241 self.last_toolbar_pos = self.toolbar_pos