1# Copyright (C) 2009-2010 Aren Olson
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2, or (at your option)
6# any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16#
17#
18# The developers of the Exaile media player hereby grant permission
19# for non-GPL compatible GStreamer and Exaile plugins to be used and
20# distributed together with GStreamer and Exaile. This permission is
21# above and beyond the permissions granted by the GPL license by which
22# Exaile is covered. If you modify this code, you may extend this
23# exception to your version of the code, but you are not obligated to
24# do so. If you do not wish to do so, delete this exception statement
25# from your version.
26
27import os
28
29from xl import settings, transcoder
30from xl.nls import gettext as _
31from xlgui.preferences import widgets
32
33name = _("CD")
34basedir = os.path.dirname(os.path.realpath(__file__))
35ui = os.path.join(basedir, "cdprefs_pane.ui")
36
37FORMAT_WIDGET = None
38
39# TODO: allow setting cddb server?
40
41
42class OutputFormatPreference(widgets.ComboPreference):
43    name = 'cd_import/format'
44
45
46class OutputQualityPreference(widgets.ComboPreference, widgets.Conditional):
47    name = 'cd_import/quality'
48    condition_preference_name = 'cd_import/format'
49
50    def __init__(self, preferences, widget):
51        widgets.ComboPreference.__init__(self, preferences, widget)
52        widgets.Conditional.__init__(self)
53        self.format = settings.get_option("cd_import/format", None)
54        self.default = settings.get_option("cd_import/quality", None)
55
56    def on_check_condition(self):
57        """
58        Specifies the condition to meet
59
60        :returns: Whether the condition is met or not
61        :rtype: bool
62        """
63        model = self.widget.get_model()
64        if not model:  # happens if preferences window is shut down on close
65            return False
66
67        curiter = self.condition_widget.get_active_iter()
68        format = self.condition_widget.get_model().get_value(curiter, 0)
69        formatinfo = transcoder.FORMATS[format]
70        if self.format != format:
71            self.format = format
72            default = formatinfo['default']
73
74            if self.default != default:
75                self.default = default  # raw value
76
77        default_title = formatinfo['kbs_steps'][
78            formatinfo['raw_steps'].index(self.default)
79        ]
80        active_iter = self.widget.get_active_iter()
81
82        if active_iter is not None:
83            active_title = float(model.get_value(active_iter, 1))
84        else:
85            active_title = default_title
86
87        self.widget.set_model(None)
88        model.clear()
89
90        steps = zip(formatinfo['raw_steps'], formatinfo['kbs_steps'])
91
92        for item, title in steps:
93            model.append([item, str(title)])
94
95        self.widget.set_model(model)
96
97        if active_title not in formatinfo['kbs_steps']:
98            active_title = default_title
99
100        index = formatinfo['kbs_steps'].index(active_title)
101        self.widget.set_active(index)
102
103        return True
104
105
106class OutputPathPreference(widgets.ComboEntryPreference):
107    name = 'cd_import/outpath'
108    completion_items = {
109        '$tracknumber': _('Track number'),
110        '$title': _('Title'),
111        '$artist': _('Artist'),
112        '$composer': _('Composer'),
113        '$album': _('Album'),
114        '$__length': _('Length'),
115        '$discnumber': _('Disc number'),
116        '$__rating': _('Rating'),
117        '$date': _('Date'),
118        '$genre': _('Genre'),
119        '$bitrate': _('Bitrate'),
120        '$__loc': _('Location'),
121        '$filename': _('Filename'),
122        '$__playcount': _('Play count'),
123        '$__last_played': _('Last played'),
124        '$bpm': _('BPM'),
125    }
126    preset_items = ["%s/$artist/$album/$tracknumber - $title" % os.getenv("HOME")]
127    default = "%s/$artist/$album/$tracknumber - $title" % os.getenv("HOME")
128
129
130# vim: et sts=4 sw=4
131