CoverArt Browser  v2.0
Browse your cover-art albums in Rhythmbox
/home/foss/Downloads/coverart-browser/coverart_search.py
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 # Based on Rupesh Kumar's and Luqman Aden'a AlbumArtSearch plugin
00006 #
00007 # This program is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2, or (at your option)
00010 # any later version.
00011 #
00012 # This program is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program; if not, write to the Free Software
00019 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
00020 
00021 from gi.repository import Gtk
00022 from mako.template import Template
00023 
00024 import rb
00025 import coverart_rb3compat as rb3compat
00026 from coverart_album import Album
00027 from coverart_browser_prefs import webkit_support
00028 
00029 
00030 class CoverSearchPane(Gtk.Box):
00031     '''
00032     This UI represents a pane where different covers can be presented
00033     given an album or artist to look for. It also allows to make custom image searchs,
00034     customize the default search and select covers from the pane and use them
00035     as the covers (either with a double click or dragging them).
00036     '''
00037 
00038     def __init__(self, plugin, selection_color):
00039         '''
00040         Initializes the pane, loading it's html templates and it's ui.
00041         '''
00042         super(CoverSearchPane, self).__init__()
00043         self.set_orientation(Gtk.Orientation.VERTICAL)
00044         self.selection_color = selection_color
00045 
00046         self.file = ""
00047         self.basepath = 'file://' + plugin.plugin_info.get_data_dir()
00048 
00049         self.load_templates(plugin)
00050         if webkit_support():
00051             self.init_gui()
00052 
00053             # init the pane with the empty template
00054             self.clear()
00055 
00056     def load_templates(self, plugin):
00057         '''
00058         Loads the templates and stylesheets to be used by the pane.
00059         '''
00060         #            input_encoding='utf-8',
00061 
00062         path = rb.find_plugin_file(plugin,
00063                                    'tmpl/albumartsearch-tmpl.html')
00064         self.template = Template(filename=path,
00065                                  default_filters=['decode.utf8'],
00066                                  module_directory='/tmp/',
00067                                  encoding_errors='replace')
00068         path = rb.find_plugin_file(plugin,
00069                                    'tmpl/albumartsearchempty-tmpl.html')
00070         self.empty_template = Template(filename=path,
00071                                        default_filters=['decode.utf8'],
00072                                        module_directory='/tmp/',
00073                                        encoding_errors='replace')
00074         path = rb.find_plugin_file(plugin,
00075                                    'tmpl/artistartsearch-tmpl.html')
00076         self.artist_template = Template(filename=path,
00077                                         default_filters=['decode.utf8'],
00078                                         module_directory='/tmp/',
00079                                         encoding_errors='replace')
00080         self.styles = rb.find_plugin_file(plugin, 'tmpl/main.css')
00081 
00082     def init_gui(self):
00083         '''
00084         Initializes the pane ui.
00085         '''
00086         #---- set up webkit pane -----#
00087         from gi.repository import WebKit
00088 
00089         self.webview = WebKit.WebView()
00090         settings = self.webview.get_settings()
00091         settings.set_property('enable-default-context-menu', False)
00092         self.webview.set_settings(settings)
00093         scroll = Gtk.ScrolledWindow()
00094         scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
00095         scroll.add(self.webview)
00096 
00097         self.pack_start(scroll, expand=True, fill=True, padding=0)
00098         self.show_all()
00099 
00100         # connect the title changed signal
00101         self.webview.connect('notify::title', self.set_cover)
00102 
00103     def do_search(self, coverobject, callback):
00104         '''
00105         When this method is called, the webview gets refreshed with the info
00106         of the album or artist passed.
00107         
00108         '''
00109         print("coverart-search do_search")
00110         if coverobject is self.current_searchobject:
00111             return
00112 
00113         self.current_searchobject = coverobject
00114         self.callback = callback
00115 
00116         if isinstance(coverobject, Album):
00117             artist = coverobject.artist
00118             album_name = coverobject.name
00119 
00120             if album_name.upper() == "UNKNOWN":
00121                 album_name = ""
00122 
00123             if artist.upper() == "UNKNOWN":
00124                 artist = ""
00125 
00126             if not (album_name == "" and artist == ""):
00127                 artist = rb3compat.unicodestr(artist.replace('&', '&'),
00128                                               'utf-8')
00129                 album_name = rb3compat.unicodestr(album_name.replace('&', '&'), 'utf-8')
00130                 self.render_album_art_search(artist, album_name)
00131         else:
00132             artist_name = coverobject.name
00133 
00134             if artist_name.upper() == "UNKNOWN":
00135                 artist_name = ""
00136 
00137             if not (artist_name == ""):
00138                 artist = rb3compat.unicodestr(artist_name.replace('&', '&'),
00139                                               'utf-8')
00140                 self.render_artist_art_search(artist)
00141 
00142 
00143     def render_album_art_search(self, artist, album_name):
00144         '''
00145         Renders the template on the webview.
00146         '''
00147         temp_file = self.template.render(artist=artist, album=album_name,
00148                                          stylesheet=self.styles, selection_color=self.selection_color)
00149 
00150         print("here")
00151         self.webview.load_string(temp_file, 'text/html', 'utf-8',
00152                                  self.basepath)
00153 
00154     def render_artist_art_search(self, artist):
00155         '''
00156         Renders the template on the webview.
00157         '''
00158         temp_file = self.artist_template.render(artist=artist,
00159                                                 stylesheet=self.styles, selection_color=self.selection_color)
00160 
00161         print("here")
00162         self.webview.load_string(temp_file, 'text/html', 'utf-8',
00163                                  self.basepath)
00164 
00165     def clear(self):
00166         '''
00167         Clears the webview of any specific info/covers.
00168         '''
00169         self.current_searchobject = None
00170         temp_file = self.empty_template.render(stylesheet=self.styles)
00171 
00172         self.webview.load_string(temp_file, 'text/html', 'utf-8',
00173                                  self.basepath)
00174 
00175     def set_cover(self, webview, arg):
00176         '''
00177         Callback called when a image in the pane is double-clicked. It takes
00178         care of updating the searched object cover.
00179         Some titles have spurious characters beginning with % - remove these
00180         '''
00181         # update the cover
00182         title = webview.get_title()
00183 
00184         print(title)
00185         if title:
00186             #self.album_manager.cover_man.update_cover(self.current_searchobject,
00187             #    uri=title)
00188             self.callback(self.current_searchobject, uri=title)
 All Classes Functions