~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to encuentro/ui/preferences.py

  • Committer: Facundo Batista
  • Date: 2013-04-16 01:58:03 UTC
  • mfrom: (151.2.7 trunk)
  • Revision ID: facundo@taniquetil.com.ar-20130416015803-btbp3sd6dn5sjyds
Merged trunk back in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""The preferences dialog."""
20
20
 
21
21
import os
22
 
import pickle
23
22
import sys
24
23
import logging
25
24
 
28
27
    QDialog,
29
28
    QDialogButtonBox,
30
29
    QGridLayout,
31
 
    QHBoxLayout,
32
30
    QLabel,
33
31
    QLineEdit,
34
32
    QTabWidget,
37
35
)
38
36
from PyQt4.QtCore import Qt
39
37
 
40
 
from encuentro import platform
 
38
from encuentro.config import config
41
39
 
42
40
logger = logging.getLogger('encuentro.preferences')
43
41
 
46
44
)
47
45
 
48
46
 
49
 
 
50
47
class GeneralPreferences(QWidget):
51
48
    """The general preferences input."""
52
 
    def __init__(self, config_data):
 
49
    def __init__(self):
53
50
        super(GeneralPreferences, self).__init__()
54
51
        grid = QGridLayout(self)
55
52
        grid.setSpacing(20)
 
53
        grid.setColumnStretch(1, 10)
56
54
 
57
55
        l = QLabel(
58
56
                u"<b>Ingresá el directorio donde descargar los videos...</b>")
60
58
        grid.addWidget(l, 0, 0, 1, 2)
61
59
 
62
60
        grid.addWidget(QLabel(u"Descargar en:"), 1, 0, 2, 1)
63
 
        prv = config_data.get('downloaddir', '')
 
61
        prv = config.get('downloaddir', '')
64
62
        self.downloaddir_entry = QLineEdit(prv)
65
63
        grid.addWidget(self.downloaddir_entry, 1, 1, 2, 2)
66
 
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
67
 
        # it should automatically use more of the width
68
64
 
69
65
        self.autoreload_checkbox = QCheckBox(
70
66
                u"Recargar automáticamente la lista de episodios al iniciar")
71
 
        prv = config_data.get('autorefresh', False)
 
67
        prv = config.get('autorefresh', False)
72
68
        self.autoreload_checkbox.setChecked(prv)
73
69
        grid.addWidget(self.autoreload_checkbox, 2, 0, 3, 2)
74
70
 
75
71
        self.shownotifs_checkbox = QCheckBox(
76
72
                u"Mostrar una notificación cuando termina cada descarga")
77
 
        prv = config_data.get('notification', True)
 
73
        prv = config.get('notification', True)
78
74
        self.shownotifs_checkbox.setChecked(prv)
79
75
        grid.addWidget(self.shownotifs_checkbox, 3, 0, 4, 2)
80
76
 
89
85
 
90
86
class ConectatePreferences(QWidget):
91
87
    """The preferences for Conectate backend."""
92
 
    def __init__(self, config_data):
 
88
    def __init__(self):
93
89
        super(ConectatePreferences, self).__init__()
94
90
        grid = QGridLayout(self)
95
91
        grid.setSpacing(20)
 
92
        grid.setColumnStretch(1, 10)
96
93
 
97
94
        l = QLabel(u"<b>Ingresá tus datos del portal Conectate:</b>")
98
95
        l.setTextFormat(Qt.RichText)
99
96
        grid.addWidget(l, 0, 0, 1, 2)
100
97
 
101
98
        grid.addWidget(QLabel(u"Usuario:"), 1, 0, 2, 1)
102
 
        prv = config_data.get('user', '')
 
99
        prv = config.get('user', '')
103
100
        self.user_entry = QLineEdit(prv)
104
101
        grid.addWidget(self.user_entry, 1, 1, 2, 2)
105
 
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
106
 
        # it should automatically use more of the width
107
102
 
108
103
        grid.addWidget(QLabel(u"Contraseña:"), 2, 0, 3, 1)
109
 
        prv = config_data.get('password', '')
 
104
        prv = config.get('password', '')
110
105
        self.password_entry = QLineEdit(prv)
111
106
        grid.addWidget(self.password_entry, 2, 1, 3, 2)
112
 
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
113
 
        # it should automatically use more of the width
114
107
 
115
108
        l = QLabel(u'Si no tenés estos datos, <a href="%s">registrate aquí'
116
109
                   u'</a>' % (URL_CONECTATE,))
117
 
        # FIXME: make this link to automaticall open the browser with it
118
110
        l.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
119
111
        l.setTextFormat(Qt.RichText)
120
112
        l.setOpenExternalLinks(True)
133
125
    def __init__(self):
134
126
        super(PreferencesDialog, self).__init__()
135
127
        vbox = QVBoxLayout(self)
136
 
        self._config_file = os.path.join(platform.config_dir, 'encuentro.conf')
137
 
        if os.path.exists(self._config_file):
138
 
            with open(self._config_file, 'rb') as fh:
139
 
                self.config_data = pickle.load(fh)
140
 
        else:
141
 
            self.config_data = {}
142
128
 
143
129
        tabbed = QTabWidget()
144
 
        self.gp = GeneralPreferences(self.config_data)
 
130
        self.gp = GeneralPreferences()
145
131
        tabbed.addTab(self.gp, u"General")
146
 
        self.cp = ConectatePreferences(self.config_data)
 
132
        self.cp = ConectatePreferences()
147
133
        tabbed.addTab(self.cp, u"Conectate")
148
134
        vbox.addWidget(tabbed)
149
135
 
150
136
        bbox = QDialogButtonBox(QDialogButtonBox.Ok)
151
137
        bbox.accepted.connect(self.accept)
 
138
        bbox.accepted.connect(self._save)
152
139
        vbox.addWidget(bbox)
153
140
 
154
 
    def save_config(self):
155
 
        """Save all config."""
 
141
    def closeEvent(self, event):
 
142
        """Save and close."""
 
143
        self._save()
 
144
        super(PreferencesDialog, self).closeEvent(event)
 
145
 
 
146
    def _save(self):
 
147
        """Just save."""
156
148
        # get it from tabs
157
 
        new_cfg = {}
158
 
        new_cfg.update(self.gp.get_config())
159
 
        new_cfg.update(self.cp.get_config())
160
 
 
161
 
        # save it!
162
 
        logger.info("Updating preferences config: %s", new_cfg)
163
 
        with open(self._config_file, 'wb') as fh:
164
 
            pickle.dump(new_cfg, fh)
 
149
        config.update(self.gp.get_config())
 
150
        config.update(self.cp.get_config())
 
151
        config.save()
165
152
 
166
153
 
167
154
if __name__ == '__main__':