From ebb32020e60f09f99882d5c529e4119439929c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Tue, 6 Feb 2007 21:24:16 +0100 Subject: [PATCH] Add a menu to the CD lookup button. --- picard/tagger.py | 34 +++++++++++++++++--------------- picard/ui/mainwindow.py | 12 ++++++++++++ picard/ui/options/cdlookup.py | 14 ++----------- picard/util/cdrom.py | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 picard/util/cdrom.py diff --git a/picard/tagger.py b/picard/tagger.py index d1a6ef087..25e7fd330 100644 --- a/picard/tagger.py +++ b/picard/tagger.py @@ -88,8 +88,11 @@ MUSICDNS_KEY = "80eaa76658f99dbac1c58cc06aa44779" class Tagger(QtGui.QApplication): + __instance = None + def __init__(self, localedir): QtGui.QApplication.__init__(self, sys.argv) + self.__class__.__instance = self self.config = Config() @@ -697,15 +700,18 @@ class Tagger(QtGui.QApplication): return CachedWebService(cachedir=self.cachedir, force=not cached, **kwargs) - def lookup_cd(self): + def lookup_cd(self, action=None): + if action is None: + drive = self.config.setting["cd_lookup_device"] + else: + drive = unicode(action.text()) self.set_wait_cursor() - self.thread_assist.spawn(self.__lookup_cd_thread) + self.thread_assist.spawn(self.__lookup_cd_thread, drive) - def __lookup_cd_thread(self): + def __lookup_cd_thread(self, drive): from musicbrainz2.disc import readDisc, getSubmissionUrl, DiscError try: - disc = readDisc( - encode_filename(self.config.setting["cd_lookup_device"])) + disc = readDisc(encode_filename(drive)) except (NotImplementedError, DiscError), e: self.thread_assist.proxy_to_main(self.__lookup_cd_error, e) return @@ -717,20 +723,15 @@ class Tagger(QtGui.QApplication): self.thread_assist.proxy_to_main(self.__lookup_cd_error, e) return - url = getSubmissionUrl(disc, self.config.setting["server_host"], - self.config.setting["server_port"]) - self.thread_assist.proxy_to_main(self.__lookup_cd_finished, - releases, url) + url = getSubmissionUrl(disc, self.config.setting["server_host"], self.config.setting["server_port"]) + self.thread_assist.proxy_to_main(self.__lookup_cd_finished, releases, url) def __lookup_cd_error(self, exception): self.restore_cursor() if isinstance(exception, NotImplementedError): - QtGui.QMessageBox.critical(self.window, _(u"CD Lookup Error"), - _(u"CD lookup not implemented. You need to have ctypes and " - u"libdiscid installed.")) + QtGui.QMessageBox.critical(self.window, _("CD Lookup Error"), _("You need to install libdiscid and ctypes to use CD lookups.")) else: - QtGui.QMessageBox.critical(self.window, _(u"CD Lookup Error"), - _(u"Error while reading CD. Is there a CD in the drive?")) + QtGui.QMessageBox.critical(self.window, _(u"CD Lookup Error"), _("Error while reading CD. Is there a CD in the drive?")) def __lookup_cd_finished(self, releases, url): self.restore_cursor() @@ -885,9 +886,10 @@ class Tagger(QtGui.QApplication): for album in albums: self.reload_album(album) -tagger = None + @classmethod + def instance(cls): + return cls.__instance def main(localedir=None): - global tagger tagger = Tagger(localedir) sys.exit(tagger.run()) diff --git a/picard/ui/mainwindow.py b/picard/ui/mainwindow.py index 9bd62f313..7c84f2d95 100644 --- a/picard/ui/mainwindow.py +++ b/picard/ui/mainwindow.py @@ -36,6 +36,7 @@ from picard.ui.filebrowser import FileBrowser from picard.ui.options.dialog import OptionsDialog from picard.ui.tageditor import TagEditor from picard.util import icontheme, webbrowser2 +from picard.util.cdrom import get_cdrom_drives class MainWindow(QtGui.QMainWindow): @@ -315,7 +316,18 @@ class MainWindow(QtGui.QMainWindow): toolbar.addAction(self.save_action) toolbar.addAction(self.submit_action) toolbar.addSeparator() + toolbar.addAction(self.cd_lookup_action) + drives = get_cdrom_drives() + if len(drives) > 1: + self.cd_lookup_menu = QtGui.QMenu() + for drive in drives: + self.cd_lookup_menu.addAction(drive) + self.connect(self.cd_lookup_menu, QtCore.SIGNAL("triggered(QAction*)"), self.tagger.lookup_cd) + button = toolbar.widgetForAction(self.cd_lookup_action) + button.setPopupMode(QtGui.QToolButton.MenuButtonPopup) + button.setMenu(self.cd_lookup_menu) + toolbar.addAction(self.autotag_action) toolbar.addAction(self.analyze_action) toolbar.addAction(self.cluster_action) diff --git a/picard/ui/options/cdlookup.py b/picard/ui/options/cdlookup.py index 57693b791..d66ef8dd0 100644 --- a/picard/ui/options/cdlookup.py +++ b/picard/ui/options/cdlookup.py @@ -22,8 +22,8 @@ import sys from PyQt4 import QtGui from picard.config import TextOption from picard.ui.options import OptionsPage, register_options_page +from picard.util.cdrom import get_cdrom_drives if sys.platform == "win32": - import win32file from picard.ui.ui_options_cdlookup_win32 import Ui_CDLookupOptionsPage else: from picard.ui.ui_options_cdlookup import Ui_CDLookupOptionsPage @@ -46,7 +46,7 @@ class CDLookupOptionsPage(OptionsPage): self.ui = Ui_CDLookupOptionsPage() self.ui.setupUi(self) if sys.platform == "win32": - self.drives = self.__get_cdrom_drives() + self.drives = get_cdrom_drives() self.ui.cd_lookup_device.addItems(self.drives) def load(self): @@ -64,15 +64,5 @@ class CDLookupOptionsPage(OptionsPage): else: self.config.setting["cd_lookup_device"] = unicode(self.ui.cd_lookup_device.text()) - def __get_cdrom_drives(self): - drives = [] - mask = win32file.GetLogicalDrives() - for i in range(26): - if mask >> i & 1: - drive = unicode(chr(i + ord("A"))) + u":\\" - if win32file.GetDriveType(drive) == win32file.DRIVE_CDROM: - drives.append(drive) - return drives - register_options_page(CDLookupOptionsPage) diff --git a/picard/util/cdrom.py b/picard/util/cdrom.py new file mode 100644 index 000000000..309e878a7 --- /dev/null +++ b/picard/util/cdrom.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# Picard, the next-generation MusicBrainz tagger +# Copyright (c) 2004 Robert Kaye +# Copyright (C) 2007 Lukáš Lalinský +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +try: + import win32file +except ImportError: + def get_cdrom_drives(): + from picard.tagger import Tagger + tagger = Tagger.instance() + return [d.strip() for d in tagger.config.setting["cd_lookup_device"].split(",")] +else: + def get_cdrom_drives(self): + drives = [] + mask = win32file.GetLogicalDrives() + for i in range(26): + if mask >> i & 1: + drive = unicode(chr(i + ord("A"))) + u":\\" + if win32file.GetDriveType(drive) == win32file.DRIVE_CDROM: + drives.append(drive) + return drives