Initial commit (Vesion 0.1)
[tablet-suite] / src / backup / .svn / text-base / pcsbackupwizard.py.svn-base
diff --git a/src/backup/.svn/text-base/pcsbackupwizard.py.svn-base b/src/backup/.svn/text-base/pcsbackupwizard.py.svn-base
new file mode 100644 (file)
index 0000000..bd16fc5
--- /dev/null
@@ -0,0 +1,180 @@
+from time import sleep
+import threading
+
+from functools import partial
+
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
+
+from ui.pcsuiutils import *
+from style.styleTabletSuite import *
+
+from pcsprogresswizard import *
+from pcscheckboxwizard import *
+from pcsopenfilewizard import *
+from pcspcbackupmanager import *
+from pcsdevicemanager import *
+
+class PcsBackupWizard(QWizard):
+    
+    ''' Class that creates a wizard responsible for doing backup '''
+    
+    def __init__(self, deviceInfo, windowManager, parent = None):
+        QWizard.__init__(self, parent)
+        self.windowManager = windowManager
+        self.deviceInfo = deviceInfo
+        stylesheet = '''QWizard{background-image:url('''+ BACKUP_BG + ''')};'''
+        self.setStyleSheet(stylesheet)       
+        self.setWindowIcon(QIcon(BACKUP_IMAGE))
+        self.setWindowTitle("%s Backup" % APPLICATION_NAME)
+        self.setFixedSize(WINDOW_WIDTH,WINDOW_HEIGHT)
+        
+        self.setButtonLayout([])
+        self.setWizardStyle(4)
+        
+        self.checkboxPage = PcsCheckboxWizard(self.deviceInfo, windowManager, self)
+        self.addPage(self.checkboxPage)
+        
+        self.chooseFilePage = PcsOpenFileWizard(self.deviceInfo, windowManager, self)
+        self.connect(self.chooseFilePage.finishButton, SIGNAL("clicked()"), 
+                     self.noNameTest)
+        self.addPage(self.chooseFilePage)
+        
+        self.progressWizard = PcsProgressWizard(self.deviceInfo,self, windowManager, self)
+        self.connect(self.progressWizard.cancelButton, SIGNAL("clicked()"), 
+                     self._confirmsCancel)
+        self.connect(self.progressWizard.doneButton, SIGNAL("clicked()"), self._done)
+        self.connect(self.progressWizard, SIGNAL("destroyed()"), self.test)
+        self.addPage(self.progressWizard)
+    
+    def test(self):
+        print "entrou caraiiiiii"
+    
+    def noNameTest(self):
+        if(str(self.chooseFilePage.getBackupName()).strip() == ""):
+            message = "Your backup name can't be blank."
+            showMessageBox(message, "Backup name blank")
+        else:
+            self.doNewBackup()
+            self.next()
+    
+    def _done(self):
+        self.done(0)
+        self.progressWizard._resetPage()
+        self.chooseFilePage._resetPage()
+    
+    def doNewBackup(self):
+        
+        hostIp = self.deviceInfo.ip
+        backupName = self.chooseFilePage.getBackupName()
+        backupPath = self.chooseFilePage.getPath()
+        categories = self.checkboxPage.getCategories()
+        self.backupManager = PcsPcBackupManager()
+        self.backupManager.loadBackups()
+        comments = ""
+        
+        self._updateThread = UpdateBackupProgress(backupName, backupPath, 
+                                                  hostIp,categories, comments, 
+                                                  self.backupManager, 
+                                                  self.progressWizard)
+        self.connect(self._updateThread, SIGNAL("backupFinished"),
+                      self._onBackupDone)        
+        self.connect(self._updateThread, SIGNAL("backupCanceled"),
+                      self._onBackupCancel)
+        self.connect(self._updateThread, SIGNAL("backupNameChanged"),
+                      self._onBackupNameChanged)
+        self.connect(self._updateThread, SIGNAL("backupProgress"),
+                      self._updateCategoriesAndProgress)
+        
+        self._updateThread.start()
+        
+    def _updateCategoriesAndProgress(self, information):
+        progress, category = information
+        self.progressWizard.setProgress(progress)
+        self.progressWizard.setCategory(category)
+        
+        
+    def _onBackupDone(self, info):
+        self.progressWizard.updateInfo(info[0], info[1])
+        self.progressWizard.progressDone()
+        self.windowManager.getBackupManager().pcListView.updateBackupList()
+
+    def _onBackupNameChanged(self, correct_name):
+        """
+        Check if backup name was changed and show message case positive.
+        """
+        nameChangeMessage = "Backup with same name was found in" + \
+                                    " backup list, Backup name changed to %s" \
+                                     % correct_name
+        showMessageBox(nameChangeMessage, "Backup name changed")
+    
+    def _confirmsCancel(self):
+        """
+        Confirms the backup canceling.
+        """
+        dialog = QMessageBox()
+        dialog.setText("Do you really want cancel this backup?")
+        dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
+        dialog.setWindowFlags(Qt.FramelessWindowHint)
+        dialog.setStyleSheet(MESSAGE_BOX_DEFAULT)
+        ret = dialog.exec_()
+        if ret == QMessageBox.Yes:
+            self._onBackupCancel()
+    
+    def _onBackupCancel(self):
+        """
+        Stops current backup process in backup manager.
+        Also, shows a message box informing about canceling.
+        
+        """
+        self.progressWizard.progressCanceled()
+        self.backupManager.setBackupInProgress(False)
+        
+    def setVisible (self, visible):
+        if(visible == False):
+            self.emit("")
+                
+        
+class UpdateBackupProgress(QThread):
+    
+    def __init__(self, backupName, path, hostIp, categories, comment, 
+                 backupManager, progressWizard):
+        QThread.__init__(self)
+
+        self.backupName = backupName
+        self.path = path
+        self.hostIp = hostIp
+        self.categories = categories
+        self.comment = comment
+        self.backupManager = backupManager
+        
+        
+    def run(self):
+        self._backupFlag = True
+        
+        self.correctName = self.backupManager._verify_backup_name(self.backupName)
+        self.nameChanged = self.correctName != self.backupName
+        
+        self.connect(self.backupManager, SIGNAL("backupProgress"), self._reEmitSignal)
+        self.connect(self.backupManager, SIGNAL("backupDone"), self._onBackupDone)
+        res = self.backupManager.createBackup(self.correctName, self.path,
+                                         self.hostIp, self.categories, 
+                                         self.comment)
+        
+        while (self._backupFlag):
+            sleep(0.1)
+            
+    def _reEmitSignal(self, informations):
+        self.emit(SIGNAL("backupProgress"), informations)
+    
+    def _onBackupDone(self, res, info):
+        self._backupFlag = False
+        # If backup was not canceled, emit done signal
+        if res != 0:
+            if self.nameChanged:
+                self.emit(SIGNAL("backupNameChanged"), self.correctName)
+            self.emit(SIGNAL("backupFinished"), info)
+        else:
+            self.emit(SIGNAL("backupCanceled"))
+
+        
\ No newline at end of file