1# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
2#
3# Copyright (c) 2008 - 2015 by Wilbert Berendsen
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18# See http://www.gnu.org/licenses/ for more information.
19
20"""
21A Dialog to run an external command and simply show the log.
22"""
23
24from PyQt5.QtCore import Qt, QSize
25
26import log
27import qutil
28import signals
29import widgets.dialog
30
31class Dialog(widgets.dialog.Dialog):
32    """Dialog to run arbitrary external job.Job commands and show the log."""
33
34    job_done = signals.Signal()
35
36    def __init__(self, parent, auto_accept=False):
37        super(Dialog, self).__init__(
38            parent,
39            buttons=('cancel', 'ok',)
40        )
41        self.setWindowModality(Qt.WindowModal)
42        self.setIconSize(32)
43        self.auto_accept = auto_accept
44        self.log = log.Log(self)
45        self.job = None
46        self.setMainWidget(self.log)
47        qutil.saveDialogSize(self, "job-dialog/dialog/size", QSize(480, 800))
48
49    def abort_job(self):
50        self.job.abort()
51        self.reject()
52
53    def run(self, j, msg=_("Run external command")):
54        """Run the given job."""
55        self.job = j
56        self.job.done.connect(self.slot_job_done)
57        self.log.connectJob(j)
58        self.setWindowTitle(j.title())
59        self.setMessage(msg)
60        self.button('ok').setEnabled(False)
61        self.button('cancel').clicked.connect(self.abort_job)
62        j.start()
63        self.exec()
64
65    def slot_job_done(self):
66        if self.job.success:
67            if self.auto_accept:
68                self.accept()
69            self.button('ok').setEnabled(True)
70        else:
71            self.setMessage(_("Job failed! Please inspect log"))
72            self.setIcon('critical')
73        self.button('cancel').clicked.connect(self.reject)
74        self.job_done.emit()
75