tomb

the crypto undertaker
git clone git://parazyd.org/tomb.git
Log | Files | Refs | README | LICENSE

open.py (3862B)


      1 import sys
      2 
      3 from PyQt4 import QtCore, QtGui
      4 
      5 from ui_open_tombfile import Ui_tombfile
      6 from ui_open_keymethod import Ui_keymethod
      7 from ui_open_success import Ui_success
      8 
      9 from tomblib.tomb import Tomb
     10 from tomblib.undertaker import Undertaker
     11 
     12 try:
     13     _fromUtf8 = QtCore.QString.fromUtf8
     14 except AttributeError:
     15     _fromUtf8 = lambda s: s
     16 
     17 class TombfilePage(QtGui.QWizardPage):
     18     def __init__(self, *args, **kwargs):
     19         QtGui.QWizardPage.__init__(self, *args)
     20         self.ui = Ui_tombfile()
     21         self.ui.setupUi(self)
     22         if 'tombfile' in kwargs and kwargs['tombfile'] is not None:
     23             self.ui.tomb_line.setText(kwargs['tombfile'])
     24         self.ui.tomb_browse.clicked.connect(self.on_tomb_location_clicked)
     25     def on_tomb_location_clicked(self, *args, **kwargs):
     26         filename = QtGui.QFileDialog.getOpenFileName(self, 'Select Tomb',
     27                 filter="Tomb (*.tomb)")
     28         self.ui.tomb_line.setText(filename)
     29 
     30 class MethodPage(QtGui.QWizardPage):
     31     def __init__(self, *args, **kwargs):
     32         QtGui.QWizardPage.__init__(self, *args, **kwargs)
     33         self.ui = Ui_keymethod()
     34         self.ui.setupUi(self)
     35         self.group = group = QtGui.QButtonGroup()
     36         for radio in self.children():
     37             if type(radio) == QtGui.QRadioButton:
     38                 group.addButton(radio)
     39 
     40     def initializePage(self):
     41         self.found = Undertaker.check( str('near://' + self.wizard().get_tombfile()) ) or []
     42         box = self.ui.radio_layout
     43 
     44         for key in self.found:
     45             radio = QtGui.QRadioButton('Automatically found: ' + key, parent=self)
     46             radio.setChecked(True)
     47             radio.setProperty('path', key)
     48             box.insertWidget(0, radio)
     49             self.group.addButton(radio)
     50 
     51 
     52     def nextId(self):
     53         '''Virtual method reimplemented to decide next page'''
     54         if self.ui.fs.isChecked():
     55             keyfile = QtGui.QFileDialog.getOpenFileName(self.wizard(), 'Key file',
     56                     filter="Tomb keys (*.tomb.key);;Buried keys (*.jpeg)")
     57             if keyfile:
     58                 #TODO: check if this really is a success :)
     59                 if Tomb.open(self.wizard().get_tombfile(), keyfile): #bugs when wrong password
     60                     return TombOpenWizard.SUCCESS_PAGE
     61                 #else: #TODO: should alert the user that we failed
     62             return TombOpenWizard.METHOD_PAGE
     63         if self.ui.usb.isChecked():
     64             return TombOpenWizard.USB_PAGE
     65         print self.group.checkedButton().property('path').toPyObject()
     66         return TombOpenWizard.SUCCESS_PAGE
     67 
     68 class SuccessPage(QtGui.QWizardPage):
     69     def __init__(self, *args, **kwargs):
     70         QtGui.QWizardPage.__init__(self, *args, **kwargs)
     71         self.ui = Ui_success()
     72         self.ui.setupUi(self)
     73 
     74 class TombOpenWizard(QtGui.QWizard):
     75     TOMBFILE_PAGE=1
     76     METHOD_PAGE=2
     77     SUCCESS_PAGE=99
     78     USB_PAGE=20
     79     def __init__(self, *args, **kwargs):
     80         QtGui.QWizard.__init__(self, *args)
     81         self.setPage(TombOpenWizard.TOMBFILE_PAGE,
     82                 TombfilePage(self, tombfile = kwargs['tombfile']
     83                     if 'tombfile' in kwargs else None))
     84         self.setPage(TombOpenWizard.METHOD_PAGE, MethodPage(self))
     85         self.setPage(TombOpenWizard.SUCCESS_PAGE, SuccessPage(self))
     86         if 'tombfile' in kwargs and kwargs['tombfile'] is not None:
     87             self.setStartId(TombOpenWizard.METHOD_PAGE)
     88 
     89     def get_tombfile(self):
     90         page = self.page(TombOpenWizard.TOMBFILE_PAGE)
     91         return page.ui.tomb_line.text()
     92 
     93         
     94 
     95 def run_open_wizard():
     96     app = QtGui.QApplication(sys.argv)
     97     window = TombOpenWizard(tombfile=sys.argv[1] if len(sys.argv) > 1 else None)
     98     window.show()
     99     sys.exit(app.exec_())
    100 
    101 if __name__ == '__main__':
    102     Undertaker.undertakerexec = '/home/davide/coding/projects/tomb/src/undertaker'
    103     run_open_wizard()
    104 
    105 
    106 
    107 
    108 
    109