create.py (5326B)
1 import sys, os 2 3 from PyQt4.QtGui import QApplication, QWizard 4 from PyQt4 import QtCore 5 from PyQt4 import QtGui 6 from ui_create import Ui_Wizard 7 8 if __name__ == '__main__': 9 parentdir = sys.path[0].split(os.sep)[:-1] 10 sys.path.append(os.sep.join(parentdir)) 11 from tomblib.tomb import Tomb 12 from worker import TombCreateThread 13 14 try: 15 _fromUtf8 = QtCore.QString.fromUtf8 16 except AttributeError: 17 _fromUtf8 = lambda s: s 18 19 class TombCreateWizard(QWizard): 20 def __init__(self, *args, **kwargs): 21 QWizard.__init__(self, *args, **kwargs) 22 self.ui = ui = Ui_Wizard() 23 ui.setupUi(self) 24 #instance attributes: 25 self.ignore_swap = False 26 self._tomb_check = False #ugly; it's used by check_progress_complete 27 28 ui.wizardPage_tomb_location.registerField('tombpath*', 29 ui.lineEdit_tombpath) #required field, note the * 30 ui.wizardPage_key_location.setCommitPage(True) 31 32 QtCore.QObject.connect(ui.button_tombpath, 33 QtCore.SIGNAL(_fromUtf8('clicked()')), 34 self.on_tomb_location_clicked) 35 QtCore.QObject.connect(self, 36 QtCore.SIGNAL(_fromUtf8('currentIdChanged(int)')), 37 self.on_change_page) 38 QtCore.QObject.connect(ui.radioButton_swapoff, 39 QtCore.SIGNAL(_fromUtf8('toggled(bool)')), 40 ui.wizardPage_check.completeChanged) 41 QtCore.QObject.connect(ui.radioButton_ignore, 42 QtCore.SIGNAL(_fromUtf8('toggled(bool)')), 43 ui.wizardPage_check.completeChanged) 44 def check_progress_complete(*args, **kwargs): 45 if self.ui.progressBar.value() == 100: 46 return True 47 return False 48 def check_is_solved(): 49 if self._tomb_check: 50 return True 51 if self.ui.radioButton_swapoff.isChecked() or \ 52 self.ui.radioButton_ignore.isChecked(): 53 return True 54 return False 55 self.ui.wizardPage_progress.isComplete = check_progress_complete 56 self.ui.wizardPage_check.isComplete = check_is_solved 57 self.ui.groupBox_swap.setVisible(False) 58 self.finished.connect(self.on_finish) 59 60 def _keyloc(self): 61 keyloc = None 62 if self.ui.radioButton_usb.isChecked(): 63 print 'Warning: it is not supported' 64 raise NotImplementedError 65 elif self.ui.radioButton_near.isChecked(): 66 print 'going near' 67 keyloc = None 68 else: 69 keyloc = self.ui.lineEdit_custom.text() 70 if not keyloc: 71 raise ValueError 72 return keyloc 73 74 def on_tomb_location_clicked(self, *args, **kwargs): 75 filename = QtGui.QFileDialog.getSaveFileName(self, 'Create Tomb', 76 filter="Tomb(*.tomb)") 77 self.ui.lineEdit_tombpath.setText(filename) 78 def on_change_page(self, pagenumber): 79 if self.currentPage() == self.ui.wizardPage_progress: 80 self.create_tomb() 81 if self.currentPage() == self.ui.wizardPage_check: 82 self.check_requisite() 83 84 def on_finish(self, finishedint): 85 if self.currentPage() != self.ui.wizardPage_end: 86 #there has been an error 87 return 88 89 if self.ui.checkBox_open.isChecked(): 90 Tomb.open(self.ui.lineEdit_tombpath.text(), self._keyloc()) 91 def on_thread_creation_finished(self): 92 if self.thread.get_success(): 93 self.ui.progressBar.setValue(100) 94 else: 95 self.ui.progressBar.setEnabled(False) 96 self.ui.label_progress.setText('Error while creating the tomb!') 97 self.ui.wizardPage_progress.setFinalPage(True) 98 self.ui.wizardPage_progress.completeChanged.emit() 99 def create_tomb(self): 100 self.thread = TombCreateThread(self.ui.lineEdit_tombpath.text(), 101 str(self.ui.spinBox_size.value()), self._keyloc(), 102 no_color=False, ignore_swap=self.ui.radioButton_ignore.isChecked()) 103 self.thread.finished.connect(self.on_thread_creation_finished) 104 self.thread.terminated.connect(self.on_thread_creation_finished) 105 self.thread.line_received.connect(self.ui.textBrowser_log.append) 106 def err_append_to_log(text): 107 self.ui.textBrowser_log.append('Error: <strong>' + text + 108 '</strong>') 109 self.thread.error_received.connect(err_append_to_log) 110 self.thread.start() 111 def check_requisite(self): 112 self._tomb_check = check = Tomb.check('create', no_color=False) 113 self.ui.wizardPage_check.completeChanged.emit() 114 if check: 115 self.ui.label_check.setText('Everything seems fine!') 116 return 117 self.ui.label_check.setText('Some error occurred') 118 if Tomb.check('create', no_color=False, ignore_swap=True): # swap is the only error 119 self.ui.groupBox_swap.setVisible(True) 120 #TODO: support swapoff 121 #TODO: calculate the amount of ram available vs swap used 122 self.ui.radioButton_swapoff.setEnabled(False) 123 self.ui.label_swapoff.setEnabled(False) 124 125 126 def run_create_wizard(): 127 app = QApplication(sys.argv) 128 window = TombCreateWizard() 129 window.show() 130 sys.exit(app.exec_()) 131 132 if __name__ == '__main__': 133 run_create_wizard() 134