2011-10-24 02:04:50 +00:00
|
|
|
import sys, os
|
2011-10-28 00:29:03 +00:00
|
|
|
|
2011-10-24 02:04:50 +00:00
|
|
|
from PyQt4.QtGui import QApplication, QWizard
|
|
|
|
from PyQt4 import QtCore
|
2011-10-27 00:22:39 +00:00
|
|
|
from PyQt4 import QtGui
|
2011-10-24 02:04:50 +00:00
|
|
|
from ui_create import Ui_Wizard
|
|
|
|
|
2011-11-01 02:07:05 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parentdir = sys.path[0].split(os.sep)[:-1]
|
|
|
|
sys.path.append(os.sep.join(parentdir))
|
2011-10-28 00:29:03 +00:00
|
|
|
from tomblib.tomb import Tomb
|
2011-10-30 21:07:16 +00:00
|
|
|
from worker import TombCreateThread
|
2011-10-24 02:04:50 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
_fromUtf8 = QtCore.QString.fromUtf8
|
|
|
|
except AttributeError:
|
|
|
|
_fromUtf8 = lambda s: s
|
|
|
|
|
2011-10-24 16:52:10 +00:00
|
|
|
class TombCreateWizard(QWizard):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
QWizard.__init__(self, *args, **kwargs)
|
|
|
|
self.ui = ui = Ui_Wizard()
|
|
|
|
ui.setupUi(self)
|
2011-11-01 02:07:05 +00:00
|
|
|
#instance attributes:
|
|
|
|
self.ignore_swap = False
|
|
|
|
self._tomb_check = False #ugly; it's used by check_progress_complete
|
|
|
|
|
|
|
|
ui.wizardPage_tomb_location.registerField('tombpath*',
|
|
|
|
ui.lineEdit_tombpath) #required field, note the *
|
2011-10-27 00:22:39 +00:00
|
|
|
ui.wizardPage_key_location.setCommitPage(True)
|
2011-10-24 16:52:10 +00:00
|
|
|
|
2011-11-01 02:07:05 +00:00
|
|
|
QtCore.QObject.connect(ui.button_tombpath,
|
|
|
|
QtCore.SIGNAL(_fromUtf8('clicked()')),
|
|
|
|
self.on_tomb_location_clicked)
|
|
|
|
QtCore.QObject.connect(self,
|
|
|
|
QtCore.SIGNAL(_fromUtf8('currentIdChanged(int)')),
|
|
|
|
self.on_change_page)
|
|
|
|
QtCore.QObject.connect(ui.radioButton_swapoff,
|
|
|
|
QtCore.SIGNAL(_fromUtf8('toggled(bool)')),
|
|
|
|
ui.wizardPage_check.completeChanged)
|
|
|
|
QtCore.QObject.connect(ui.radioButton_ignore,
|
|
|
|
QtCore.SIGNAL(_fromUtf8('toggled(bool)')),
|
|
|
|
ui.wizardPage_check.completeChanged)
|
2011-10-28 10:06:00 +00:00
|
|
|
def check_progress_complete(*args, **kwargs):
|
|
|
|
if self.ui.progressBar.value() == 100:
|
|
|
|
return True
|
|
|
|
return False
|
2011-11-01 02:07:05 +00:00
|
|
|
def check_is_solved():
|
|
|
|
if self._tomb_check:
|
|
|
|
return True
|
|
|
|
if self.ui.radioButton_swapoff.isChecked() or \
|
|
|
|
self.ui.radioButton_ignore.isChecked():
|
|
|
|
return True
|
|
|
|
return False
|
2011-10-28 10:06:00 +00:00
|
|
|
self.ui.wizardPage_progress.isComplete = check_progress_complete
|
2011-11-01 02:07:05 +00:00
|
|
|
self.ui.wizardPage_check.isComplete = check_is_solved
|
|
|
|
self.ui.groupBox_swap.setVisible(False)
|
2011-10-28 10:06:00 +00:00
|
|
|
self.finished.connect(self.on_finish)
|
2011-10-24 16:52:10 +00:00
|
|
|
|
2011-10-28 10:06:00 +00:00
|
|
|
def _keyloc(self):
|
2011-10-27 13:01:27 +00:00
|
|
|
keyloc = None
|
|
|
|
if self.ui.radioButton_usb.isChecked():
|
|
|
|
print 'Warning: it is not supported'
|
|
|
|
raise NotImplementedError
|
|
|
|
elif self.ui.radioButton_near.isChecked():
|
|
|
|
print 'going near'
|
|
|
|
keyloc = None
|
|
|
|
else:
|
|
|
|
keyloc = self.ui.lineEdit_custom.text()
|
|
|
|
if not keyloc:
|
|
|
|
raise ValueError
|
2011-10-28 10:06:00 +00:00
|
|
|
return keyloc
|
2011-10-27 13:01:27 +00:00
|
|
|
|
2011-10-28 10:06:00 +00:00
|
|
|
def on_tomb_location_clicked(self, *args, **kwargs):
|
2011-11-01 02:07:05 +00:00
|
|
|
filename = QtGui.QFileDialog.getSaveFileName(self, 'Create Tomb',
|
2012-01-24 00:18:34 +00:00
|
|
|
filter="Tomb(*.tomb)")
|
2011-10-28 10:06:00 +00:00
|
|
|
self.ui.lineEdit_tombpath.setText(filename)
|
|
|
|
def on_change_page(self, pagenumber):
|
|
|
|
if self.currentPage() == self.ui.wizardPage_progress:
|
|
|
|
self.create_tomb()
|
2011-11-01 02:07:05 +00:00
|
|
|
if self.currentPage() == self.ui.wizardPage_check:
|
|
|
|
self.check_requisite()
|
2011-10-28 10:06:00 +00:00
|
|
|
|
|
|
|
def on_finish(self, finishedint):
|
|
|
|
if self.currentPage() != self.ui.wizardPage_end:
|
|
|
|
#there has been an error
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.ui.checkBox_open.isChecked():
|
|
|
|
Tomb.open(self.ui.lineEdit_tombpath.text(), self._keyloc())
|
|
|
|
def on_thread_creation_finished(self):
|
|
|
|
if self.thread.get_success():
|
|
|
|
self.ui.progressBar.setValue(100)
|
|
|
|
else:
|
|
|
|
self.ui.progressBar.setEnabled(False)
|
|
|
|
self.ui.label_progress.setText('Error while creating the tomb!')
|
|
|
|
self.ui.wizardPage_progress.setFinalPage(True)
|
|
|
|
self.ui.wizardPage_progress.completeChanged.emit()
|
2011-11-01 02:07:05 +00:00
|
|
|
def create_tomb(self):
|
|
|
|
self.thread = TombCreateThread(self.ui.lineEdit_tombpath.text(),
|
|
|
|
str(self.ui.spinBox_size.value()), self._keyloc(),
|
|
|
|
no_color=False, ignore_swap=self.ui.radioButton_ignore.isChecked())
|
2011-10-28 10:06:00 +00:00
|
|
|
self.thread.finished.connect(self.on_thread_creation_finished)
|
|
|
|
self.thread.terminated.connect(self.on_thread_creation_finished)
|
2011-10-30 21:07:16 +00:00
|
|
|
self.thread.line_received.connect(self.ui.textBrowser_log.append)
|
|
|
|
def err_append_to_log(text):
|
2011-11-01 02:07:05 +00:00
|
|
|
self.ui.textBrowser_log.append('Error: <strong>' + text +
|
|
|
|
'</strong>')
|
2011-10-30 21:07:16 +00:00
|
|
|
self.thread.error_received.connect(err_append_to_log)
|
2011-10-28 00:29:03 +00:00
|
|
|
self.thread.start()
|
2011-11-01 02:07:05 +00:00
|
|
|
def check_requisite(self):
|
|
|
|
self._tomb_check = check = Tomb.check('create', no_color=False)
|
|
|
|
self.ui.wizardPage_check.completeChanged.emit()
|
|
|
|
if check:
|
|
|
|
self.ui.label_check.setText('Everything seems fine!')
|
|
|
|
return
|
|
|
|
self.ui.label_check.setText('Some error occurred')
|
|
|
|
if Tomb.check('create', no_color=False, ignore_swap=True): # swap is the only error
|
|
|
|
self.ui.groupBox_swap.setVisible(True)
|
|
|
|
#TODO: support swapoff
|
|
|
|
#TODO: calculate the amount of ram available vs swap used
|
|
|
|
self.ui.radioButton_swapoff.setEnabled(False)
|
|
|
|
self.ui.label_swapoff.setEnabled(False)
|
2011-10-28 00:29:03 +00:00
|
|
|
|
2011-10-24 02:04:50 +00:00
|
|
|
|
2011-11-03 23:40:35 +00:00
|
|
|
def run_create_wizard():
|
2011-10-24 16:52:10 +00:00
|
|
|
app = QApplication(sys.argv)
|
|
|
|
window = TombCreateWizard()
|
|
|
|
window.show()
|
|
|
|
sys.exit(app.exec_())
|
2011-11-03 23:40:35 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run_create_wizard()
|
|
|
|
|