Tizen 2.1 base
[platform/upstream/hplip.git] / ui / faxsettingsform.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Author: Don Welch
20 #
21
22
23
24 from qt import *
25 from faxsettingsform_base import FaxSettingsForm_base
26 from base.g import *
27 from base import device, pml, utils
28
29 class PhoneNumValidator(QValidator):
30     def __init__(self, parent=None, name=None):
31         QValidator.__init__(self, parent, name)
32
33     def validate(self, input, pos):
34         input = unicode(input)
35         try:
36             input = input.encode('ascii')
37         except UnicodeEncodeError:
38             return QValidator.Invalid, pos
39
40         if not input:
41             return QValidator.Acceptable, pos
42         elif input[pos-1] not in '0123456789-(+) ':
43             return QValidator.Invalid, pos
44         elif len(input) > 50:
45             return QValidator.Invalid, pos
46         else:
47             return QValidator.Acceptable, pos
48
49
50 class StationNameValidator(QValidator):
51     def __init__(self, parent=None, name=None):
52         QValidator.__init__(self, parent, name)
53
54     def validate(self, input, pos):
55         input = unicode(input)
56
57         try:
58             input = input.encode('ascii')
59         except UnicodeEncodeError:
60             return QValidator.Invalid, pos
61
62         if not input:
63             return QValidator.Acceptable, pos
64         # TODO: Find valid chars for this field
65         elif input != utils.printable(input):
66             return QValidator.Invalid, pos
67         elif len(input) > 50:
68             return QValidator.Invalid, pos
69         else:
70             return QValidator.Acceptable, pos
71
72
73
74 class FaxSettingsForm(FaxSettingsForm_base):
75
76     def __init__(self, dev, fax_num, name_co, parent = None,name = None,modal = 0,fl = 0):
77         FaxSettingsForm_base.__init__(self,parent,name,modal,fl)
78         self.dev = dev
79         self.faxEdit.setValidator(PhoneNumValidator(self.faxEdit))
80         self.nameEdit.setValidator(StationNameValidator(self.nameEdit))
81         self.voiceEdit.setValidator(PhoneNumValidator(self.voiceEdit))
82         self.faxEdit.setText(fax_num)
83         self.nameEdit.setText(name_co)
84         self.setOKButton(fax_num and name_co)
85         self.voiceEdit.setText(QString(user_conf.get('fax', 'voice_phone')))
86         self.emailEdit.setText(QString(user_conf.get('fax', 'email_address')))
87
88     def faxEdit_textChanged(self,a0):
89         self.setOKButton()
90
91     def nameEdit_textChanged(self,a0):
92         self.setOKButton()
93
94     def setOKButton(self, toggle=None):
95         if toggle is not None:
96             self.pushButtonOK.setEnabled(bool(toggle))
97         else:
98             name = unicode(self.nameEdit.text())
99             fax_num = unicode(self.faxEdit.text())
100             self.pushButtonOK.setEnabled(bool(name and fax_num))
101
102     def accept(self):
103         # str() is OK here since the validators removed any non-ascii chars
104         fax = str(self.faxEdit.text())
105         log.debug(fax)
106         name = str(self.nameEdit.text())
107         log.debug(name)
108         try:
109             self.dev.setPML(pml.OID_FAX_LOCAL_PHONE_NUM, fax)
110             self.dev.setPML(pml.OID_FAX_STATION_NAME, name)
111         except Error:
112             log.error("Error setting fax settings to device.")
113
114         # TODO: This is a problem - user can enter non-ascii chars...
115         # user config needs to be in utf-8 encoding (but its not right now)
116         user_conf.set('fax', 'voice_phone', unicode(self.voiceEdit.text()).encode('utf-8'))
117         user_conf.set('fax', 'email_address', unicode(self.emailEdit.text()).encode('utf-8'))
118         FaxSettingsForm_base.accept(self)
119