class AptOfflineQtInstall(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.ui = Ui_AptOfflineQtInstall()
        self.ui.setupUi(self)

        # Connect the clicked signal of the Browse button to it's slot
        self.ui.browseFilePathButton.clicked.connect(self.popupDirectoryDialog)

        # Connect the clicked signal of the Save to it's Slot - accept
        self.ui.startInstallButton.clicked.connect(self.StartInstall)

        # Connect the clicked signal of the Cancel to it's Slot - reject
        self.ui.cancelButton.clicked.connect(self.reject)

        self.ui.bugReportsButton.clicked.connect(self.showBugReports)
        self.ui.changelogButton.clicked.connect(self.showChangelog)
        self.ui.zipFilePath.editingFinished.connect(
            self.ControlStartInstallBox)
        self.ui.zipFilePath.textChanged.connect(self.ControlStartInstallBox)

        self.worker = Worker(parent=self)
        self.worker.output.connect(self.updateLog)
        self.worker.progress.connect(self.updateProgress)
        self.worker.status.connect(self.updateStatus)
        self.worker.finished.connect(self.finishedWork)
        self.worker.terminated.connect(self.finishedWork)

    def StartInstall(self):
        # gui validation
        # Clear the consoleOutputHolder
        self.ui.rawLogHolder.setText("")
        self.filepath = str(self.ui.zipFilePath.text())

        # parse args
        args = InstallerArgs(filename=self.filepath,
                             progress_bar=self.ui.statusProgressBar,
                             progress_label=self.ui.progressStatusDescription)

        self.disableActions()
        self.ui.progressStatusDescription.setText("Syncing updates")
        self.worker.setArgs(args)
        self.worker.start()

    def showBugReports(self):
        self.filepath = str(self.ui.zipFilePath.text())
        self.bugReportsDialog = AptOfflineQtInstallBugList(self.filepath)
        self.bugReportsDialog.filepath = self.filepath
        self.bugReportsDialog.show()

    def showChangelog(self):
        self.filepath = str(self.ui.zipFilePath.text())
        self.changelogDialog = AptOfflineQtInstallChangelog(self.filepath)
        self.changelogDialog.filepath = self.filepath
        self.changelogDialog.show()

    def popupDirectoryDialog(self):

        # Popup a Directory selection box
        if self.ui.browseFileFoldercheckBox.isChecked() is True:
            directory = QtWidgets.QFileDialog.getExistingDirectory(
                self, 'Select the folder')
        else:
            directory = QtWidgets.QFileDialog.getOpenFileName(
                self, 'Select the Zip File')

        # Show the selected file path in the field marked for showing directory path
        self.ui.zipFilePath.setText(directory)
        self.ui.zipFilePath.setFocus()

    def ControlStartInstallBox(self):
        if os.path.isdir(self.ui.zipFilePath.text()) or os.path.isfile(
                self.ui.zipFilePath.text()):
            self.ui.startInstallButton.setEnabled(True)
            self.ui.bugReportsButton.setEnabled(True)
            self.ui.changelogButton.setEnabled(True)
        else:
            self.ui.startInstallButton.setEnabled(False)
            self.ui.bugReportsButton.setEnabled(False)
            self.ui.changelogButton.setEnabled(False)

    def updateLog(self, text):
        guicommon.updateInto(self.ui.rawLogHolder, text)

    def updateStatus(self, text):
        # status handler
        self.ui.progressStatusDescription.setText(text)

    def updateProgress(self, progress, total):
        try:
            # try parsing numbers and updating progressBar
            percent = (float(progress) / float(total)) * 100
            self.ui.statusProgressBar.setValue(percent)
        except:
            ''' nothing to do '''

    def finishedWork(self):
        self.enableActions()
        guicommon.updateInto(
            self.ui.rawLogHolder,
            guicommon.style("Finished syncing updates/packages", "green_fin"))
        self.ui.progressStatusDescription.setText("Finished Syncing")
        self.ui.cancelButton.setText("Close")

    def disableActions(self):
        self.ui.browseFileFoldercheckBox.setEnabled(False)
        self.ui.cancelButton.setEnabled(False)
        self.ui.startInstallButton.setEnabled(False)
        self.ui.bugReportsButton.setEnabled(False)
        self.ui.browseFilePathButton.setEnabled(False)
        self.ui.zipFilePath.setEnabled(False)
        self.ui.changelogButton.setEnabled(False)

    def enableActions(self):
        self.ui.browseFileFoldercheckBox.setEnabled(True)
        self.ui.cancelButton.setEnabled(True)
        self.ui.startInstallButton.setEnabled(True)
        self.ui.bugReportsButton.setEnabled(True)
        self.ui.browseFilePathButton.setEnabled(True)
        self.ui.zipFilePath.setEnabled(True)
        self.ui.changelogButton.setEnabled(True)
class AptOfflineQtInstall(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_AptOfflineQtInstall()
        self.ui.setupUi(self)

        # Connect the clicked signal of the Browse button to it's slot
        QtCore.QObject.connect(self.ui.browseFilePathButton, QtCore.SIGNAL("clicked()"), self.popupDirectoryDialog)

        # Connect the clicked signal of the Save to it's Slot - accept
        QtCore.QObject.connect(self.ui.startInstallButton, QtCore.SIGNAL("clicked()"), self.StartInstall)

        # Connect the clicked signal of the Cancel to it's Slot - reject
        QtCore.QObject.connect(self.ui.cancelButton, QtCore.SIGNAL("clicked()"), self.reject)

        QtCore.QObject.connect(self.ui.bugReportsButton, QtCore.SIGNAL("clicked()"), self.showBugReports)

        QtCore.QObject.connect(self.ui.changelogButton, QtCore.SIGNAL("clicked()"), self.showChangelog)

        QtCore.QObject.connect(self.ui.zipFilePath, QtCore.SIGNAL("editingFinished()"), self.ControlStartInstallBox)

        QtCore.QObject.connect(self.ui.zipFilePath, QtCore.SIGNAL("textChanged(QString)"), self.ControlStartInstallBox)

        self.worker = Worker(parent=self)
        QtCore.QObject.connect(self.worker, QtCore.SIGNAL("output(QString)"), self.updateLog)
        QtCore.QObject.connect(self.worker, QtCore.SIGNAL("progress(QString,QString)"), self.updateProgress)
        QtCore.QObject.connect(self.worker, QtCore.SIGNAL("status(QString)"), self.updateStatus)
        QtCore.QObject.connect(self.worker, QtCore.SIGNAL("finished()"), self.finishedWork)
        QtCore.QObject.connect(self.worker, QtCore.SIGNAL("terminated()"), self.finishedWork)

    def StartInstall(self):
        # gui validation
        # Clear the consoleOutputHolder
        self.ui.rawLogHolder.setText("")
        self.filepath = str(self.ui.zipFilePath.text())

        # parse args
        args = InstallerArgs(
            filename=self.filepath,
            progress_bar=self.ui.statusProgressBar,
            progress_label=self.ui.progressStatusDescription,
        )

        self.disableActions()
        self.ui.progressStatusDescription.setText("Syncing updates")
        self.worker.setArgs(args)
        self.worker.start()

    def showBugReports(self):
        self.filepath = str(self.ui.zipFilePath.text())
        self.bugReportsDialog = AptOfflineQtInstallBugList(self.filepath)
        self.bugReportsDialog.filepath = self.filepath
        self.bugReportsDialog.show()

    def showChangelog(self):
        self.filepath = str(self.ui.zipFilePath.text())
        self.changelogDialog = AptOfflineQtInstallChangelog(self.filepath)
        self.changelogDialog.filepath = self.filepath
        self.changelogDialog.show()

    def popupDirectoryDialog(self):

        # Popup a Directory selection box
        if self.ui.browseFileFoldercheckBox.isChecked() is True:
            directory = QtGui.QFileDialog.getExistingDirectory(self, u"Select the folder")
        else:
            directory = QtGui.QFileDialog.getOpenFileName(self, u"Select the Zip File")

        # Show the selected file path in the field marked for showing directory path
        self.ui.zipFilePath.setText(directory)
        self.ui.zipFilePath.setFocus()

    def ControlStartInstallBox(self):
        if os.path.isdir(self.ui.zipFilePath.text()) or os.path.isfile(self.ui.zipFilePath.text()):
            self.ui.startInstallButton.setEnabled(True)
            self.ui.bugReportsButton.setEnabled(True)
            self.ui.changelogButton.setEnabled(True)
        else:
            self.ui.startInstallButton.setEnabled(False)
            self.ui.bugReportsButton.setEnabled(False)
            self.ui.changelogButton.setEnabled(False)

    def updateLog(self, text):
        guicommon.updateInto(self.ui.rawLogHolder, text)

    def updateStatus(self, text):
        # status handler
        self.ui.progressStatusDescription.setText(text)

    def updateProgress(self, progress, total):
        try:
            # try parsing numbers and updating progressBar
            percent = (float(progress) / float(total)) * 100
            self.ui.statusProgressBar.setValue(percent)
        except:
            """ nothing to do """

    def finishedWork(self):
        self.enableActions()
        guicommon.updateInto(self.ui.rawLogHolder, guicommon.style("Finished syncting updates/packages", "green_fin"))
        self.ui.progressStatusDescription.setText("Finished Syncing")

    def disableActions(self):
        self.ui.cancelButton.setEnabled(False)
        self.ui.startInstallButton.setEnabled(False)
        self.ui.bugReportsButton.setEnabled(False)
        self.ui.browseFilePathButton.setEnabled(False)
        self.ui.zipFilePath.setEnabled(False)
        self.ui.changelogButton.setEnabled(False)

    def enableActions(self):
        self.ui.cancelButton.setEnabled(True)
        self.ui.startInstallButton.setEnabled(True)
        self.ui.bugReportsButton.setEnabled(True)
        self.ui.browseFilePathButton.setEnabled(True)
        self.ui.zipFilePath.setEnabled(True)
        self.ui.changelogButton.setEnabled(True)
 def showChangelog(self):
     self.filepath = str(self.ui.zipFilePath.text())
     self.changelogDialog = AptOfflineQtInstallChangelog(self.filepath)
     self.changelogDialog.filepath = self.filepath
     self.changelogDialog.show()
 def showChangelog(self):
     self.filepath = str(self.ui.zipFilePath.text())
     self.changelogDialog = AptOfflineQtInstallChangelog(self.filepath)
     self.changelogDialog.filepath = self.filepath
     self.changelogDialog.show()