1# -*- coding: utf-8 -*-
2#
3# (c) Copyright 2001-2015 HP 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# Authors: Don Welch
20#
21
22# StdLib
23import operator
24import signal
25
26# Local
27from base.g import *
28from base import device, utils
29from prnt import cups
30from base.codes import *
31from .ui_utils import *
32from base.sixext import to_unicode
33# Qt
34from PyQt4.QtCore import *
35from PyQt4.QtGui import *
36
37# Ui
38from .faxsetupdialog_base import Ui_Dialog
39from .deviceuricombobox import DEVICEURICOMBOBOX_TYPE_FAX_ONLY
40
41fax_enabled = prop.fax_build
42
43if fax_enabled:
44    try:
45        from fax import fax
46    except ImportError:
47        # This can fail on Python < 2.3 due to the datetime module
48        # or if fax was diabled during the build
49        fax_enabled = False
50
51if not fax_enabled:
52    log.warn("Fax disabled.")
53
54
55class FaxSetupDialog(QDialog, Ui_Dialog):
56    def __init__(self, parent, device_uri):
57        QDialog.__init__(self, parent)
58        self.setupUi(self)
59        self.device_uri = device_uri
60        self.initUi()
61        self.dev = None
62
63        self.user_settings = UserSettings()
64        self.user_settings.load()
65        self.user_settings.debug()
66
67        QTimer.singleShot(0, self.updateUi)
68
69
70    def initUi(self):
71        # connect signals/slots
72        self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
73        self.connect(self.FaxComboBox, SIGNAL("DeviceUriComboBox_noDevices"), self.FaxComboBox_noDevices)
74        self.connect(self.FaxComboBox, SIGNAL("DeviceUriComboBox_currentChanged"), self.FaxComboBox_currentChanged)
75        self.FaxComboBox.setType(DEVICEURICOMBOBOX_TYPE_FAX_ONLY)
76        signal.signal(signal.SIGINT, signal.SIG_DFL)
77
78        # Application icon
79        self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
80
81        if self.device_uri:
82            self.FaxComboBox.setInitialDevice(self.device_uri)
83
84        self.NameCompanyLineEdit.setMaxLength(50)
85        self.FaxNumberLineEdit.setMaxLength(50)
86        self.FaxNumberLineEdit.setValidator(PhoneNumValidator(self.FaxNumberLineEdit))
87        self.VoiceNumberLineEdit.setMaxLength(50)
88        self.VoiceNumberLineEdit.setValidator(PhoneNumValidator(self.VoiceNumberLineEdit))
89        self.EmailLineEdit.setMaxLength(50)
90
91        self.connect(self.NameCompanyLineEdit, SIGNAL("editingFinished()"),
92                     self.NameCompanyLineEdit_editingFinished)
93
94        self.connect(self.NameCompanyLineEdit, SIGNAL("textChanged(const QString &)"),
95                     self.NameCompanyLineEdit_textChanged)
96
97        self.connect(self.FaxNumberLineEdit, SIGNAL("editingFinished()"),
98                     self.FaxNumberLineEdit_editingFinished)
99
100        self.connect(self.FaxNumberLineEdit, SIGNAL("textChanged(const QString &)"),
101                     self.FaxNumberLineEdit_textChanged)
102
103        self.connect(self.VoiceNumberLineEdit, SIGNAL("editingFinished()"),
104                     self.VoiceNumberLineEdit_editingFinished)
105
106        self.connect(self.VoiceNumberLineEdit, SIGNAL("textChanged(const QString &)"),
107                     self.VoiceNumberLineEdit_textChanged)
108
109        self.connect(self.EmailLineEdit, SIGNAL("editingFinished()"),
110                     self.EmailLineEdit_editingFinished)
111
112        self.connect(self.EmailLineEdit, SIGNAL("textChanged(const QString &)"),
113                     self.EmailLineEdit_textChanged)
114
115        self.connect(self.tabWidget,SIGNAL("currentChanged(int)"),self.Tabs_currentChanged)
116
117        self.name_company_dirty = False
118        self.fax_number_dirty = False
119        self.voice_number_dirty = False
120        self.email_dirty = False
121
122
123    def updateUi(self):
124        if not fax_enabled:
125            FailureUI(self, self.__tr("<b>PC send fax support is not enabled.</b><p>Re-install HPLIP with fax support or use the device front panel to send a fax.</p><p>Click <i>OK</i> to exit.</p>"))
126            self.close()
127            return
128
129        self.FaxComboBox.updateUi()
130        self.tabWidget.setCurrentIndex(0)
131
132
133    def FaxComboBox_currentChanged(self, device_uri):
134        self.device_uri = device_uri
135        self.updateCoverpageTab()
136
137        if self.dev is not None:
138            self.dev.close()
139
140        try:
141            self.dev = fax.getFaxDevice(self.device_uri)
142        except Error:
143            CheckDeviceUI(self)
144            return
145
146        self.updateHeaderTab()
147
148
149
150    def FaxComboBox_noDevices(self):
151        FailureUI(self, self.__tr("<b>No devices that require fax setup found.</b>"))
152        self.close()
153
154    #
155    # Name/Company (for TTI header) (stored in device)
156    #
157
158    def NameCompanyLineEdit_editingFinished(self):
159        self.saveNameCompany(to_unicode(self.NameCompanyLineEdit.text()))
160
161
162    def NameCompanyLineEdit_textChanged(self, s):
163        self.name_company_dirty = True
164
165
166    def saveNameCompany(self, s):
167        self.name_company_dirty = False
168        beginWaitCursor()
169        try:
170            try:
171                log.debug("Saving station name %s to device" % s)
172                self.dev.setStationName(s)
173            except Error:
174                CheckDeviceUI(self)
175        finally:
176            endWaitCursor()
177
178    #
179    # Fax Number (for TTI header) (stored in device)
180    #
181
182    def FaxNumberLineEdit_editingFinished(self):
183        self.saveFaxNumber(to_unicode(self.FaxNumberLineEdit.text()))
184
185
186    def FaxNumberLineEdit_textChanged(self, s):
187        self.fax_number_dirty = True
188
189
190    def saveFaxNumber(self, s):
191        self.fax_number_dirty = False
192        beginWaitCursor()
193        try:
194            try:
195                log.debug("Saving fax number %s to device" % s)
196                self.dev.setPhoneNum(s)
197            except Error:
198                CheckDeviceUI(self)
199        finally:
200            endWaitCursor()
201
202    #
203    # Voice Number (for coverpage) (stored in ~/.hplip/hplip.conf)
204    #
205
206    def VoiceNumberLineEdit_editingFinished(self):
207        self.saveVoiceNumber(to_unicode(self.VoiceNumberLineEdit.text()))
208
209
210    def VoiceNumberLineEdit_textChanged(self, s):
211        self.voice_number_dirty = True
212
213
214    def saveVoiceNumber(self, s):
215        log.debug("Saving voice number (%s) to ~/.hplip/hplip.conf" % s)
216        self.voice_number_dirty = False
217        #user_conf.set('fax', 'voice_phone', s)
218        self.user_settings.voice_phone = s
219        self.user_settings.save()
220
221    #
222    # EMail (for coverpage) (stored in ~/.hplip/hplip.conf)
223    #
224
225    def EmailLineEdit_editingFinished(self):
226        self.saveEmail(to_unicode(self.EmailLineEdit.text()))
227
228
229    def EmailLineEdit_textChanged(self, s):
230        self.email_dirty = True
231
232
233    def saveEmail(self, s):
234        log.debug("Saving email address (%s) to ~/.hplip/hplip.conf" % s)
235        self.email_dirty = False
236        #user_conf.set('fax', 'email_address', s)
237        self.user_settings.email_address = s
238        self.user_settings.save()
239
240    #
241    #
242    #
243
244    def CancelButton_clicked(self):
245        self.close()
246
247    def Tabs_currentChanged(self, tab=0):
248        """ Called when the active tab changes.
249            Update newly displayed tab.
250        """
251        if tab == 0:
252            self.updateHeaderTab()
253        elif tab ==1:
254            self.updateCoverpageTab()
255
256
257    def updateHeaderTab(self):
258        beginWaitCursor()
259        try:
260            try:
261                name_company = to_unicode(self.dev.getStationName())
262                log.debug("name_company = '%s'" % name_company)
263                self.NameCompanyLineEdit.setText(name_company)
264                fax_number = str(self.dev.getPhoneNum())
265                log.debug("fax_number = '%s'" % fax_number)
266                self.FaxNumberLineEdit.setText(fax_number)
267            except Error:
268                CheckDeviceUI(self)
269        finally:
270            endWaitCursor()
271
272
273    def updateCoverpageTab(self):
274        #voice_phone = user_conf.get('fax', 'voice_phone')
275        voice_phone = self.user_settings.voice_phone
276        log.debug("voice_phone = '%s'" % voice_phone)
277        self.VoiceNumberLineEdit.setText(voice_phone)
278        #email_address = user_conf.get('fax', 'email_address')
279        email_address = self.user_settings.email_address
280        log.debug("email_address = '%s'" % email_address)
281        self.EmailLineEdit.setText(email_address)
282
283
284    def closeEvent(self, e):
285        if self.voice_number_dirty:
286            self.VoiceNumberLineEdit.emit(SIGNAL("editingFinished()"))
287
288        if self.name_company_dirty:
289            self.NameCompanyLineEdit.emit(SIGNAL("editingFinished()"))
290
291        if self.email_dirty:
292            self.EmailLineEdit.emit(SIGNAL("editingFinished()"))
293
294        if self.fax_number_dirty:
295            self.FaxNumberLineEdit.emit(SIGNAL("editingFinished()"))
296
297        if self.dev is not None:
298            self.dev.close()
299
300        e.accept()
301
302    #
303    # Misc
304    #
305
306    def __tr(self,s,c = None):
307        return qApp.translate("FaxSetupDialog",s,c)
308
309
310