Initial commit (Vesion 0.1)
[tablet-suite] / src / backup / pcsbackuplistui.py
diff --git a/src/backup/pcsbackuplistui.py b/src/backup/pcsbackuplistui.py
new file mode 100644 (file)
index 0000000..daca374
--- /dev/null
@@ -0,0 +1,110 @@
+# Authors: Amaury Medeiros and Paulo Ouriques
+# Software License: GPL
+
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
+
+class PCSBackupListUi(QTableView):
+    
+    ''' Class that creates a table, where the backups will be shown '''
+    
+    def __init__(self, backupManager):
+        super(PCSBackupListUi, self).__init__()
+        
+        self.setSelectionBehavior(QAbstractItemView.SelectRows)
+        self.setSelectionMode(QAbstractItemView.ExtendedSelection)
+        self.setAlternatingRowColors(True)
+        self.setShowGrid(False)
+        self.setEditTriggers(QAbstractItemView.NoEditTriggers)
+        self.model = QStandardItemModel()
+        self.setModel(self.model)
+        
+        hHeader = QHeaderView(Qt.Horizontal)
+        hHeader.setObjectName("listHeader")
+        hHeader.setAttribute(Qt.WA_NoSystemBackground)
+        hHeader.setStretchLastSection(True)
+        hHeader.setResizeMode(QHeaderView.ResizeToContents)
+        hHeader.setMinimumSectionSize(100)
+
+        hHeader.setClickable(False)
+
+        self.setHorizontalHeader(hHeader)
+        
+        vHeader = QHeaderView(Qt.Vertical)
+        vHeader.setVisible(False)
+        self.setVerticalHeader(vHeader)
+        self._backupManager = backupManager
+        
+    def updateBackupList(self):
+        self.model.clear()
+        self.model.setHorizontalHeaderItem(0, QStandardItem("NAME"))
+        self.model.setHorizontalHeaderItem(1, QStandardItem("SIZE"))
+        self.model.setHorizontalHeaderItem(2, QStandardItem("DATE"))
+
+        backupList = self._backupManager.getBackupList()
+        for backupInfo in backupList:
+            name = backupInfo.getName()
+            date = str(backupInfo.getDate())
+            size = self._formatBackupSize(backupInfo.getSize())
+            backupData = [QStandardItem(name), QStandardItem(size), QStandardItem(date)]
+            self.model.appendRow(backupData)
+
+    def removeSelectedBackups(self):
+        selectionModel = self.selectionModel()
+        indexList = selectionModel.selectedRows()
+        for index in reversed(sorted(indexList)):
+            if index.isValid():
+                row = index.row()
+                data = self.model.itemData(index)
+                backupName = data[0].toString()
+                if self._backupManager.removeBackup((str(backupName).strip())):
+                    self.model.removeRow(row)
+        self.updateBackupList()
+
+    def renameSelectedBackup (self, newName):
+        #!!!!!!! getSelectedBackup
+        backupName = (str(self.getSelectedBackup())).strip()
+        if backupName != None:
+            if self._backupManager.renameBackup(backupName, newName):
+                self.updateBackupList()
+                return True
+        
+        return False
+    
+    def getSelectedBackup(self):
+        list = self.getSelectedBackupList()
+        if list and len(list) > 0:
+            return list[0]
+        
+        return None
+    
+    def getSelectedBackupList(self):
+        selectionModel = self.selectionModel()
+        indexList = selectionModel.selectedRows()
+        backupList = []
+        for index in indexList:
+            if index.isValid():
+                row = index.row()
+                data = self.model.itemData(index)
+                backupList.append(data[0].toString())
+        return backupList
+
+    def getBackupManager(self):
+        return self._backupManager
+            
+    def _formatBackupSize(self, size):
+        """ Return a string with a more suited size and byte multiple for the
+        received size.
+        
+        Attributes:
+            String/Float/Int size - size in bytes or string representing it.
+        
+        """
+        size = float(size)
+        multiples = ["B", "KB", "MB", "GB"]
+        divisions = 0
+        while size > 1000 and divisions <= 3:
+            size = size / 1024.
+            divisions += 1
+            
+        return "%.1f %s" % (size, multiples[divisions])