PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/picard/ui/options/fingerprinting.py

https://github.com/lalinsky/picard
Python | 144 lines | 121 code | 5 blank | 18 comment | 0 complexity | 57d465115457e2b574ce5aad0ab460b0 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Picard, the next-generation MusicBrainz tagger
  4. # Copyright (C) 2011 Lukáš Lalinský
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. import os
  20. from PyQt4 import QtGui, QtCore
  21. from picard import config
  22. from picard.util import webbrowser2, find_executable
  23. from picard.const import FPCALC_NAMES
  24. from picard.ui.options import OptionsPage, OptionsCheckError, register_options_page
  25. from picard.ui.ui_options_fingerprinting import Ui_FingerprintingOptionsPage
  26. class FingerprintingOptionsPage(OptionsPage):
  27. NAME = "fingerprinting"
  28. TITLE = N_("Fingerprinting")
  29. PARENT = None
  30. SORT_ORDER = 45
  31. ACTIVE = True
  32. options = [
  33. config.BoolOption("setting", "ignore_existing_acoustid_fingerprints", False),
  34. config.TextOption("setting", "fingerprinting_system", "acoustid"),
  35. config.TextOption("setting", "acoustid_fpcalc", ""),
  36. config.TextOption("setting", "acoustid_apikey", ""),
  37. ]
  38. def __init__(self, parent=None):
  39. super(FingerprintingOptionsPage, self).__init__(parent)
  40. self._fpcalc_valid = True
  41. self.ui = Ui_FingerprintingOptionsPage()
  42. self.ui.setupUi(self)
  43. self.ui.disable_fingerprinting.clicked.connect(self.update_groupboxes)
  44. self.ui.use_acoustid.clicked.connect(self.update_groupboxes)
  45. self.ui.acoustid_fpcalc.textChanged.connect(self._acoustid_fpcalc_check)
  46. self.ui.acoustid_fpcalc_browse.clicked.connect(self.acoustid_fpcalc_browse)
  47. self.ui.acoustid_fpcalc_download.clicked.connect(self.acoustid_fpcalc_download)
  48. self.ui.acoustid_apikey_get.clicked.connect(self.acoustid_apikey_get)
  49. def load(self):
  50. if config.setting["fingerprinting_system"] == "acoustid":
  51. self.ui.use_acoustid.setChecked(True)
  52. else:
  53. self.ui.disable_fingerprinting.setChecked(True)
  54. self.ui.acoustid_fpcalc.setText(config.setting["acoustid_fpcalc"])
  55. self.ui.acoustid_apikey.setText(config.setting["acoustid_apikey"])
  56. self.ui.ignore_existing_acoustid_fingerprints.setChecked(config.setting["ignore_existing_acoustid_fingerprints"])
  57. self.update_groupboxes()
  58. def save(self):
  59. if self.ui.use_acoustid.isChecked():
  60. config.setting["fingerprinting_system"] = "acoustid"
  61. else:
  62. config.setting["fingerprinting_system"] = ""
  63. config.setting["acoustid_fpcalc"] = unicode(self.ui.acoustid_fpcalc.text())
  64. config.setting["acoustid_apikey"] = unicode(self.ui.acoustid_apikey.text())
  65. config.setting["ignore_existing_acoustid_fingerprints"] = self.ui.ignore_existing_acoustid_fingerprints.isChecked()
  66. def update_groupboxes(self):
  67. if self.ui.use_acoustid.isChecked():
  68. self.ui.acoustid_settings.setEnabled(True)
  69. if not self.ui.acoustid_fpcalc.text():
  70. fpcalc_path = find_executable(*FPCALC_NAMES)
  71. if fpcalc_path:
  72. self.ui.acoustid_fpcalc.setText(fpcalc_path)
  73. else:
  74. self.ui.acoustid_settings.setEnabled(False)
  75. self._acoustid_fpcalc_check()
  76. def acoustid_fpcalc_browse(self):
  77. path = QtGui.QFileDialog.getOpenFileName(self, "", self.ui.acoustid_fpcalc.text())
  78. if path:
  79. path = os.path.normpath(unicode(path))
  80. self.ui.acoustid_fpcalc.setText(path)
  81. def acoustid_fpcalc_download(self):
  82. webbrowser2.goto('chromaprint')
  83. def acoustid_apikey_get(self):
  84. webbrowser2.goto('acoustid_apikey')
  85. def _acoustid_fpcalc_check(self):
  86. if not self.ui.use_acoustid.isChecked():
  87. self._acoustid_fpcalc_set_success("")
  88. return
  89. fpcalc = unicode(self.ui.acoustid_fpcalc.text())
  90. if not fpcalc:
  91. self._acoustid_fpcalc_set_success("")
  92. return
  93. self._fpcalc_valid = False
  94. process = QtCore.QProcess(self)
  95. process.finished.connect(self._on_acoustid_fpcalc_check_finished)
  96. process.error.connect(self._on_acoustid_fpcalc_check_error)
  97. process.start(fpcalc, ["-v"])
  98. def _on_acoustid_fpcalc_check_finished(self, exit_code, exit_status):
  99. process = self.sender()
  100. if exit_code == 0 and exit_status == 0:
  101. output = str(process.readAllStandardOutput())
  102. if output.startswith("fpcalc version"):
  103. self._acoustid_fpcalc_set_success(output.strip())
  104. else:
  105. self._acoustid_fpcalc_set_error()
  106. else:
  107. self._acoustid_fpcalc_set_error()
  108. def _on_acoustid_fpcalc_check_error(self, error):
  109. self._acoustid_fpcalc_set_error()
  110. def _acoustid_fpcalc_set_success(self, version):
  111. self._fpcalc_valid = True
  112. self.ui.acoustid_fpcalc_info.setStyleSheet("")
  113. self.ui.acoustid_fpcalc_info.setText(version)
  114. def _acoustid_fpcalc_set_error(self):
  115. self._fpcalc_valid = False
  116. self.ui.acoustid_fpcalc_info.setStyleSheet(self.STYLESHEET_ERROR)
  117. self.ui.acoustid_fpcalc_info.setText(_("Please select a valid fpcalc executable."))
  118. def check(self):
  119. if not self._fpcalc_valid:
  120. raise OptionsCheckError(_("Invalid fpcalc executable"), _("Please select a valid fpcalc executable."))
  121. def display_error(self, error):
  122. pass
  123. register_options_page(FingerprintingOptionsPage)