Source

VPlayer / options.py

# -*- coding: utf-8 -*-

import os
import sys
import inspect
from PyQt4 import QtGui
from PyQt4 import QtCore

from vplayer import common
from vplayer import queries
from vplayer.logger import log
from vplayer.options_qt import Ui_Options
from vplayer.logininfo import LoginInfo
from vplayer.directoryscanner import DirectoryScanner

class Options(QtGui.QDialog):
    plugins = []
    def __init__(self, parent = None):
        self.parent = parent
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Options()
        self.ui.setupUi(self)
        if os.name == 'nt':
            self.ui.tabWidget.removeTab(5)
        self.connect(self.ui.buttons,
                     QtCore.SIGNAL("clicked(QAbstractButton*)"),
                     self.button_clicked)
        QtCore.QObject.connect(self.ui.browse_caching,
                               QtCore.SIGNAL("clicked()"), self.browse_caching)
        QtCore.QObject.connect(self.ui.browse_collection,
                               QtCore.SIGNAL("clicked()"),
                               self.browse_collection)
        QtCore.QObject.connect(self.ui.rescan_cache,
                               QtCore.SIGNAL("clicked()"), self.rescan_cache)
        QtCore.QObject.connect(self.ui.rescan_collection,
                               QtCore.SIGNAL("clicked()"),
                               self.rescan_collection)
        QtCore.QObject.connect(self.ui.clean_collection,
                               QtCore.SIGNAL("clicked()"),
                               self.clean_collection)
        QtCore.QObject.connect(self.ui.clean_cache,
                               QtCore.SIGNAL("clicked()"),
                               self.clean_cache)
        QtCore.QObject.connect(self.ui.clear_cache,
                               QtCore.SIGNAL("clicked()"),
                               self.clear_cache)
        self.settings = QtCore.QSettings()
        def conf(key, default = ''):
            return self.settings.value(key, QtCore.QVariant(default))
        self.ui.vk_login.setText(conf('login/email').toString())
        self.ui.vk_password.setText(conf('login/password').toString())
        self.ui.lfm_use.setChecked(conf('lastfm/use').toBool())
        self.ui.lfm_login.setText(conf('lastfm/login').toString())
        self.ui.lfm_password.setText(conf('lastfm/password').toString())
        self.ui.caching_use.setChecked(conf('caching/use', False).toBool())
        std = os.path.join(common.get_config_dir(), 'cached')
        self.ui.caching.setText(conf('caching/directory', std).toString())
        self.ui.collection.setText(conf('collection/directory').toString())
        self.ui.only1251.setChecked(conf('detect_only_cp1251', True).toBool())
        self.ui.audio.setCurrentIndex(conf('audio', 1).toInt()[0])

        self.ui.show_search_tab.setChecked(conf('MainWindow/widgets/search/enabled', True).toBool())
        self.ui.show_collection_tab.setChecked(conf('MainWindow/widgets/collection/enabled', True).toBool())
        self.ui.show_files_tab.setChecked(conf('MainWindow/widgets/files/enabled', True).toBool())
        self.ui.show_trackinfo_tab.setChecked(conf('MainWindow/widgets/trackinfo/enabled', True).toBool())
        self.ui.download_lyrics.setChecked(conf('MainWindow/widgets/trackinfo/load', True).toBool())

        self.ui.show_bitrate_playlist.setChecked(conf('MainWindow/widgets/playlist/showbitrate', True).toBool())
        self.ui.show_album_playlist.setChecked(conf('MainWindow/widgets/playlist/showalbum', True).toBool())
        self.ui.show_bitrate_search.setChecked(conf('MainWindow/widgets/search/showbitrate', True).toBool())
        self.ui.show_album_search.setChecked(conf('MainWindow/widgets/search/showalbum', True).toBool())
        self.ui.minimize_on_close.setChecked(conf('MainWindow/minimize_on_close', True).toBool())

        self.mapper = QtCore.QSignalMapper(self)
        QtCore.QObject.connect(self.mapper, QtCore.SIGNAL("mapped(int)"),
             self.load_plugin)#, QtCore.SIGNAL("clicked(const QString &))"))
        self.make_plugins_list()

        self.load_hotkeys()

    def button_clicked(self, button):
        stdbutton = self.ui.buttons.standardButton(button)
        if stdbutton == QtGui.QDialogButtonBox.Ok:
            self.save()
        if stdbutton == QtGui.QDialogButtonBox.Apply:
            self.apply()
        if stdbutton == QtGui.QDialogButtonBox.Cancel:
            self.cancel()

    def load_hotkeys(self):
        sets = QtCore.QSettings()
        play_st = [ v.toInt()[0] for v in sets.value("hotkeys/str/play", QtCore.QVariant()).toList() ]
        stop_st = [ v.toInt()[0] for v in sets.value("hotkeys/str/stop", QtCore.QVariant()).toList() ]
        pause_st = [ v.toInt()[0] for v in sets.value("hotkeys/str/pause", QtCore.QVariant()).toList() ]
        prev_st = [ v.toInt()[0] for v in sets.value("hotkeys/str/prev", QtCore.QVariant()).toList() ]
        next_st = [ v.toInt()[0] for v in sets.value("hotkeys/str/next", QtCore.QVariant()).toList() ]
        self.ui.sc_play.setValue(play_st)
        self.ui.sc_stop.setValue(stop_st)
        self.ui.sc_pause.setValue(pause_st)
        self.ui.sc_prev.setValue(prev_st)
        self.ui.sc_next.setValue(next_st)

    def save_hotkeys(self):
        sets = QtCore.QSettings()
        sets.setValue("hotkeys/str/play", QtCore.QVariant(self.ui.sc_play.value))
        sets.setValue("hotkeys/str/stop", QtCore.QVariant(self.ui.sc_stop.value))
        sets.setValue("hotkeys/str/pause", QtCore.QVariant(self.ui.sc_pause.value))
        sets.setValue("hotkeys/str/prev", QtCore.QVariant(self.ui.sc_prev.value))
        sets.setValue("hotkeys/str/next", QtCore.QVariant(self.ui.sc_next.value))

        sets.setValue("hotkeys/x/play", QtCore.QVariant(self.ui.sc_play.xseq))
        sets.setValue("hotkeys/x/stop", QtCore.QVariant(self.ui.sc_stop.xseq))
        sets.setValue("hotkeys/x/pause", QtCore.QVariant(self.ui.sc_pause.xseq))
        sets.setValue("hotkeys/x/prev", QtCore.QVariant(self.ui.sc_prev.xseq))
        sets.setValue("hotkeys/x/next", QtCore.QVariant(self.ui.sc_next.xseq))

    def load_plugin(self, signal):
        self.parent.load_plugin(self.plugins[signal][0])

    def make_plugins_list(self):
        for plugin in self.plugins:
            for element in plugin:
                element.close()

        self.plugins = []

        plugin_dir = common.get_plugins_dir()

        if os.path.exists(plugin_dir):
            sys.path.append(plugin_dir)
            from base_plugin import BasePlugin
            for fname in os.listdir(plugin_dir):
                if not fname.endswith(".py"):
                    continue
                module_name = fname[: -3]
                log.debug('Loading plugin %s' % module_name)
                if module_name == "__init__" or module_name == 'base_plugin':
                    continue
                obj = __import__(module_name)
                for objn in dir(obj):
                    objm = getattr(obj, objn)
                    if inspect.isclass(objm):
                        if issubclass (objm, BasePlugin):
                            self.add_plugin(objm)

    def add_plugin(self, plugin):
        col = len(self.plugins)
        layout = self.ui.gridLayout
        info = plugin.plugin_info

        title = QtGui.QLabel(info['name'])
        author = QtGui.QLabel(info['author'])
        load = QtGui.QPushButton()
        load.setText(self.tr('Load'))
        delete = QtGui.QPushButton()
        delete.setText(self.tr('Delete'))

#         QPushButton *button = new QPushButton(texts[i]);
#         connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
#         signalMapper->setMapping(button, texts[i]);
#         gridLayout->addWidget(button, i / 3, i % 3);

        QtCore.QObject.connect(load, QtCore.SIGNAL('clicked()'), self.mapper,
                               QtCore.SLOT("map()"))
        self.mapper.setMapping(load, col)


        self.plugins.append((plugin, title,author,load,delete))
        layout.addWidget(title, col, 0)
        layout.addWidget(author, col, 1)
        layout.addWidget(load, col, 2)
        layout.addWidget(delete, col, 3)

    def browse_caching(self):
        dialog = QtGui.QFileDialog()
        dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
        dialog.setDirectory(self.ui.caching.text())
        directory = dialog.getExistingDirectory()
        if directory:
            self.ui.caching.setText(directory)

    def browse_collection(self):
        dialog = QtGui.QFileDialog()
        dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
        dialog.setDirectory(self.ui.collection.text())
        directory = dialog.getExistingDirectory()
        if directory:
            self.ui.collection.setText(directory)

    def save(self):
        self.apply()
        self.hide()

    def apply(self):
        def conf(key, value):
            self.settings.setValue(key, QtCore.QVariant(value))

        conf('login/email', self.ui.vk_login.text())
        conf('login/password', self.ui.vk_password.text())
        conf('lastfm/use', self.ui.lfm_use.isChecked())
        conf('lastfm/login', self.ui.lfm_login.text())
        conf('lastfm/password', self.ui.lfm_password.text())
        conf('caching/use', self.ui.caching_use.isChecked())
        conf('collection/directory', self.ui.collection.text())
        conf('detect_only_cp1251', self.ui.only1251.isChecked())
        conf('audio', self.ui.audio.currentIndex())
        conf('MainWindow/widgets/search/enabled',
             self.ui.show_search_tab.isChecked())
        conf('MainWindow/widgets/collection/enabled',
             self.ui.show_collection_tab.isChecked())
        conf('MainWindow/widgets/files/enabled',
             self.ui.show_files_tab.isChecked())
        conf('MainWindow/widgets/trackinfo/enabled',
             self.ui.show_trackinfo_tab.isChecked())
        conf('MainWindow/widgets/trackinfo/load',
             self.ui.download_lyrics.isChecked())

        conf('MainWindow/widgets/playlist/showbitrate',
             self.ui.show_bitrate_playlist.isChecked())
        conf('MainWindow/widgets/playlist/showalbum',
             self.ui.show_album_playlist.isChecked())
        conf('MainWindow/widgets/search/showbitrate',
             self.ui.show_bitrate_search.isChecked())
        conf('MainWindow/widgets/search/showalbum',
             self.ui.show_album_search.isChecked())
        conf('MainWindow/minimize_on_close',
             self.ui.minimize_on_close.isChecked())

        if self.ui.caching_use.isChecked():
            if os.path.exists(unicode(self.ui.caching.text())):
                conf('caching/directory', self.ui.caching.text())
            else:
                QtGui.QMessageBox.critical(None, self.tr('Error'),
                                self.tr('Chosen directory does not exist'))
                return

        self.settings.sync()

        self.parent.update_tabwidget()

        self.save_hotkeys()
        self.parent.register_xshortcuts()

        if self.ui.lfm_use.isChecked():
            self.parent.connect_lastfm()

        if self.ui.vk_login.text():
            self.parent.login()

    def cancel(self):
        self.close()

    def rescan_collection(self):
        directory = unicode(self.ui.collection.text())
        if not (os.path.exists(directory) and os.path.isdir(directory)):
            QtGui.QMessageBox.information(None, self.tr('Error'),
        self.tr('Looks like you have specified directory that does not exist'))
        self.scanner = DirectoryScanner([directory], 'scan', False)
        self.connect(self.scanner, QtCore.SIGNAL('status'),
                     self.parent.scan_processor)
        self.connect(self.scanner, QtCore.SIGNAL('progress'),
                     self.parent.scan_processor)
        self.scanner.start()

    def rescan_cache(self):
        directory = unicode(self.ui.caching.text())
        if not (os.path.exists(directory) and os.path.isdir(directory)):
            QtGui.QMessageBox.information(None, self.tr('Error'),
        self.tr('Looks like you have specified directory that does not exist'))
        self.scanner = DirectoryScanner([directory], 'scan', True)
        self.connect(self.scanner, QtCore.SIGNAL('status'),
                     self.parent.scan_processor)
        self.connect(self.scanner, QtCore.SIGNAL('progress'),
                     self.parent.scan_processor)
        self.scanner.start()

    def clear_cache(self):
        for item in queries.get_all_cached_files():
            try: os.remove(item)
            except: continue
            albdir = os.path.dirname(item)
            artdir = os.path.dirname(albdir)
            if not os.listdir(albdir):
                try: os.path.rmdir(albdir)
                except: continue
                if not os.listdir(artdir):
                    try: os.path.rmdir(artdir)
                    except: continue
        queries.clear_cache()

    def clean_cache(self):
        self.scanner = DirectoryScanner([], 'clean', True)
        self.connect(self.scanner, QtCore.SIGNAL('status'),
                     self.parent.scan_processor)
        self.connect(self.scanner, QtCore.SIGNAL('progress'),
                     self.parent.scan_processor)
        self.scanner.start()

    def clean_collection(self):
        self.scanner = DirectoryScanner([], 'clean', False)
        self.connect(self.scanner, QtCore.SIGNAL('status'),
                     self.parent.scan_processor)
        self.connect(self.scanner, QtCore.SIGNAL('progress'),
                     self.parent.scan_processor)
        self.scanner.start()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.