示例#1
0
    def set_file_metadata(self, bucket_id, file_id):
        try:
            file_metadata = self.storj_engine.storj_client.file_metadata(
                str(bucket_id), str(file_id))
            self.ui_single_file_download.file_name.setText(
                html_format_begin + str(file_metadata.filename) +
                html_format_end)

            tools = Tools()
            self.ui_single_file_download.file_size.setText(
                html_format_begin +
                str(tools.human_size(int(file_metadata.size))) +
                html_format_end)
            self.ui_single_file_download.file_id.setText(html_format_begin +
                                                         str(file_id) +
                                                         html_format_end)

            self.ui_single_file_download.file_save_path.setText(
                str(tools.get_home_user_directory() + "/" +
                    str(file_metadata.filename)))
        except storj.exception.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                      "Error while resolving file metadata. " +
                      str(e))  # emit Storj Bridge Exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledException"),
                      "Unhandled error while resolving file metadata. " +
                      str(e))  # emit unhandled Exception
    def register(self):
        # validate fields
        self.email = self.register_ui.email.text()
        self.password = self.register_ui.password.text()
        self.password_repeat = self.register_ui.password_2.text()
        self.tools = Tools()

        success = False

        if self.email != "" and self.password != "" and self.password_repeat != "":

            if self.password == self.password_repeat:

                if self.tools.check_email(self.email):

                    # take login action
                    try:
                        self.storj_client = \
                            storj.Client(None, '').user_create(
                                str(self.email).strip(),
                                str(self.password).strip())
                        success = True
                    except storj.exception.StorjBridgeApiError as e:
                        j = json.loads(str(e))
                        if j.get('error', None) == 'Email is already registered':
                            QtGui.QMessageBox.about(
                                self,
                                'Warning',
                                'User with this e-mail is \
                                already registered! Please \
                                login or try a different \
                                e-mail!')
                        else:
                            QtGui.QMessageBox.about(self, 'Unhandled exception', 'Exception: %s' % e)
                else:
                    QtGui.QMessageBox.about(
                        self, 'Warning', 'Your e-mail seems to be invalid! Please check e-mail and try again')
            else:
                QtGui.QMessageBox.about(self, 'Warning', 'Given passwords are different! Please check and try again!')
        else:
            QtGui.QMessageBox.about(self, 'Warning', 'Please fill out all fields!')

        if success:
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Information,
                'Success',
                'Successfully registered in Storj Distributed Storage Network! '
                'Now, you must verify your email by'
                'clicking the link that has been sent to you. '
                'Then you can login',
                QtGui.QMessageBox.Ok)
            self.__logger.debug('New user registered')
            self.__logger.debug('Email: %s', self.email)
            self.__logger.debug('Password: %s', self.password)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.login_window = LoginUI(self)
                self.login_window.show()
                self.close()
示例#3
0
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.account_dash_ui = Ui_AccountDash()
        self.account_dash_ui.setupUi(self)

        self.storj_engine = StorjEngine()  # init StorjEngine
        self.tools = Tools()

        self.initialize_buckets_stats_table()

        self.createNewBucketsStatsGetThread()
示例#4
0
    def register(self):
        # validate fields
        self.email = self.register_ui.email.text()
        self.password = self.register_ui.password.text()
        self.password_repeat = self.register_ui.password_2.text()

        self.tools = Tools()
        success = False
        if self.email != "" and self.password != "" and self.password_repeat != "":
            if self.password == self.password_repeat:
                if (self.tools.check_email(self.email)):
                    # take login action
                    try:
                        self.storj_client = storj.Client(None, "").user_create(str(self.email).strip(), str(self.password).strip())
                        success = True
                    except storj.exception.StorjBridgeApiError as e:
                        j = json.loads(str(e))
                        if j.get("error", None) == "Email is already registered":
                            QtGui.QMessageBox.about(self, "Warning",
                                                    "User with this e-mail is \
                                                    already registered! Please \
                                                    login or try a different \
                                                    e-mail!")
                        else:
                            QtGui.QMessageBox.about(self, "Unhandled exception", "Exception: " + str(e))
                else:
                    QtGui.QMessageBox.about(self, "Warning",
                                            "Your e-mail seems to be invalid! Please chech e-mail  and try again")
            else:
                QtGui.QMessageBox.about(self, "Warning",
                                        "Given passwords are different! Please check and try again!")
        else:
            QtGui.QMessageBox.about(self, "Warning",
                                    "Please fill out all fields!")

        if success:
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Success",
                                       "Successfully registered in Storj Distributed Storage Network! "
                                       "Now, you must verify your email by"
                                       "clicking the link that has been sent to you. "
                                       "Then you can login", QtGui.QMessageBox.Ok)
            logger.debug("New user registrated")
            logger.debug("Email: " + self.email)
            logger.debug("Password: " + self.password)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.login_window = LoginUI(self)
                self.login_window.show()
                self.close()
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.sync_menu_ui = Ui_FileSyncOptions()
        self.sync_menu_ui.setupUi(self)

        self.sync_configuration_manager = SyncConfiguration()
        self.tools = Tools()

        QtCore.QObject.connect(self.sync_menu_ui.restore_defaults_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.restore_default_settings)

        QtCore.QObject.connect(self.sync_menu_ui.save_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.save_sync_options)

        QtCore.QObject.connect(self.sync_menu_ui.cancel_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.save_sync_options)

        QtCore.QObject.connect(self.sync_menu_ui.add_sync_dir_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.add_new_sync_directory)

        QtCore.QObject.connect(self.sync_menu_ui.remove_sync_dir,
                               QtCore.SIGNAL('clicked()'),
                               self.delete_sync_directory)

        # prepare table
        self.sync_dirs_table_header = [
            'Directory', 'Add date', 'Files count', 'Actual total size'
        ]

        self.sync_menu_ui.sync_directories_tableWidget.setRowCount(0)

        self.sync_menu_ui.sync_directories_tableWidget.resizeColumnsToContents(
        )
        self.sync_menu_ui.sync_directories_tableWidget.resizeRowsToContents()
        self.sync_menu_ui.sync_directories_tableWidget.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

        self.sync_configuration_manager.paint_config_to_ui(self.sync_menu_ui)

        self.sync_menu_ui.sync_directories_tableWidget.setHorizontalHeaderLabels(
            self.sync_dirs_table_header)
        self.sync_menu_ui.sync_directories_tableWidget.setColumnCount(4)
    def set_file_metadata(self, bucket_id, file_id):
        try:
            self.emit(QtCore.SIGNAL("setCurrentState"),
                      "Getting file metadata...")
            file_metadata = self.storj_engine.storj_client.file_metadata(
                str(bucket_id), str(file_id))
            self.ui_single_file_download.file_name.setText(
                str(file_metadata.filename.replace("[DECRYPTED]",
                                                   "")).decode('utf-8'))

            tools = Tools()
            # self.ui_single_file_download.file_size.setText(
            #   html_format_begin + str(tools.human_size(int(file_metadata.size))) + html_format_end)
            self.ui_single_file_download.file_id.setText(str(file_id))

            if platform == "linux" or platform == "linux2":
                # linux
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "/" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))
            elif platform == "darwin":
                # OS X
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "/" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))
            elif platform == "win32":
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "\\" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))

            self.filename_from_bridge = str(file_metadata.filename)

            self.resolved_file_metadata = True
            self.emit(QtCore.SIGNAL("setCurrentState"),
                      "Waiting for user action...")

        except storj.exception.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                      "Error while resolving file metadata. " +
                      str(e))  # emit Storj Bridge Exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledException"),
                      "Unhandled error while resolving file metadata. " +
                      str(e))  # emit unhandled Exception
    def update_files_list(self):

        self.tools = Tools()

        # initialize model for inserting to table
        model = QtGui.QStandardItemModel(1, 1)

        model.setHorizontalHeaderLabels(['File name', 'File size', 'Mimetype', 'File ID'])

        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex()
        self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]

        i = 0

        try:
            for self.file_details in self.storj_engine.storj_client.bucket_files(str(self.current_selected_bucket_id)):
                item = QtGui.QStandardItem(str(self.file_details['filename'].replace('[DECRYPTED]', "")))
                model.setItem(i, 0, item)  # row, column, item (StandardItem)

                file_size_str = self.tools.human_size(int(self.file_details["size"]))  # get human readable file size

                item = QtGui.QStandardItem(str(file_size_str))
                model.setItem(i, 1, item)  # row, column, item (QQtGui.StandardItem)

                item = QtGui.QStandardItem(str(self.file_details['mimetype']))
                model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                item = QtGui.QStandardItem(str(self.file_details['id']))
                model.setItem(i, 3, item)  # row, column, item (QStandardItem)

                i = i + 1

                self.__logger.info(self.file_details)

        except sjexc.StorjBridgeApiError as e:
            self.__logger.error(e)

        self.file_manager_ui.files_list_tableview.clearFocus()
        self.file_manager_ui.files_list_tableview.setModel(model)
        self.file_manager_ui.files_list_tableview.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
示例#8
0
    def __init__(self, parent=None, nodeid=None):
        QtGui.QWidget.__init__(self, parent)
        self.storj_engine = StorjEngine()  # init StorjEngine
        # login UI
        self.node_details_ui = Ui_NodeDetails()
        self.node_details_ui.setupUi(self)

        self.nodeid = nodeid
        self.tools = Tools()

        QtCore.QObject.connect(self.node_details_ui.ok_bt, QtCore.SIGNAL("clicked()"), self.close)  # close window
        self.connect(self, QtCore.SIGNAL("showBridgeExceptionMessageBox"), self.show_storj_bridge_exception)

        self.createNewNodeDetailsResolveThread()
示例#9
0
    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_download = Ui_SingleFileDownload()
        self.ui_single_file_download.setupUi(self)
        # QtCore.QObject.connect(self.ui_single_file_download., QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine

        self.tools = Tools()

        # init loggers
        # self.log_handler = LogHandler()
        # logging.setLoggerClass(get_global_logger(self.log_handler))
        # logger.addHandler(self.log_handler)

        # self.initialize_shard_queue_table(file_pointers)

        self.account_manager = AccountManager()  # init AccountManager

        self.user_password = self.account_manager.get_user_password()

        ########3

        QtCore.QObject.connect(
            self.ui_single_file_download.file_save_path_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_file_save_path)  # open file select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.tmp_dir_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_tmp_directory)  # open tmp directory select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.start_download_bt,
            QtCore.SIGNAL("clicked()"),
            lambda: self.createNewDownloadInitThread(
                bucketid, fileid))  # begin file downloading process

        # QtCore.QObject.connect(self.ui_single_file_download.open_log_bt, QtCore.SIGNAL("clicked()"),
        #                        self.open_logs_window)  # open logs window

        self.connect(self,
                     QtCore.SIGNAL("incrementShardsDownloadProgressCounters"),
                     self.increment_shards_download_progress_counters)
        self.connect(self, QtCore.SIGNAL("updateShardDownloadProgress"),
                     self.update_shard_download_progess)
        self.connect(self, QtCore.SIGNAL("beginDownloadProccess"),
                     self.download_begin)
        self.connect(self, QtCore.SIGNAL("refreshOverallDownloadProgress"),
                     self.refresh_overall_download_progress)
        self.connect(self,
                     QtCore.SIGNAL("showDestinationFileNotSelectedError"),
                     self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL("showInvalidDestinationPathError"),
                     self.show_error_invalid_file_path)
        self.connect(self,
                     QtCore.SIGNAL("showInvalidTemporaryDownloadPathError"),
                     self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL("updateDownloadTaskState"),
                     self.update_download_task_state)
        self.connect(self, QtCore.SIGNAL("showStorjBridgeException"),
                     self.show_storj_bridge_exception)
        self.connect(self, QtCore.SIGNAL("showUnhandledException"),
                     self.show_unhandled_exception)
        self.connect(self, QtCore.SIGNAL("showFileDownloadedSuccessfully"),
                     self.show_download_finished_message)

        self.shards_already_downloaded = 0

        self.createNewInitializationThread(bucketid, fileid)

        self.shard_download_percent_list = []

        # init limit variables
        self.max_retries_download_from_same_farmer = 3
        self.max_retries_get_file_pointers = 10

        # set default paths
        self.ui_single_file_download.tmp_dir.setText(str("/tmp/"))

        # set config variables

        self.combine_tmpdir_name_with_token = False

        # set overall progress to 0
        self.ui_single_file_download.overall_progress.setValue(0)
示例#10
0
class RegisterUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        # register UI
        self.register_ui = Ui_UI_Register()
        self.register_ui.setupUi(self)

        self.register_ui.password.setEchoMode(QtGui.QLineEdit.Password)
        self.register_ui.password_2.setEchoMode(QtGui.QLineEdit.Password)

        QtCore.QObject.connect(self.register_ui.register_bt,
                               QtCore.SIGNAL("clicked()"),
                               self.register)  # validate and register user

    def register(self):
        # validate fields
        self.email = self.register_ui.email.text()
        self.password = self.register_ui.password.text()
        self.password_repeat = self.register_ui.password_2.text()
        self.bridge_api_url = str(
            self.register_ui.bridge_url.text()).strip()  # get bridge api url

        if self.bridge_api_url == "":
            self.bridge_api_url = 'https://api.storj.io/'

        self.tools = Tools()
        success = False
        if self.email != "" and self.password != "" and self.password_repeat != "":
            if self.password == self.password_repeat:
                if (self.tools.check_email(self.email)):
                    # take login action
                    try:
                        self.storj_client = storj.Client(
                            None, "", api_url=self.bridge_api_url).user_create(
                                str(self.email).strip(),
                                str(self.password).strip())
                        success = True
                    except storj.exception.StorjBridgeApiError as e:
                        j = json.loads(str(e))
                        if j[0]["error"] == "Email is already registered":
                            QtGui.QMessageBox.about(
                                self, "Warning", "User with this e-mail is \
                                                    already registered! Please \
                                                    login or try a different \
                                                    e-mail!")
                        else:
                            QtGui.QMessageBox.about(self,
                                                    "Unhandled exception",
                                                    "Exception: " + str(e))
                else:
                    QtGui.QMessageBox.about(
                        self, "Warning",
                        "Your e-mail seems to be invalid! Please check e-mail and try again"
                    )
            else:
                QtGui.QMessageBox.about(
                    self, "Warning",
                    "Given passwords are different! Please check and try again!"
                )
        else:
            QtGui.QMessageBox.about(self, "Warning",
                                    "Please fill out all fields!")

        if success:
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Information, "Success",
                "Successfully registered in Storj Distributed Storage Network! "
                "Now, you must verify your email by"
                "clicking the link that has been sent to you. "
                "Then you can login", QtGui.QMessageBox.Ok)
            logger.debug("New user registrated")
            logger.debug("Email: " + self.email)
            logger.debug("Password: " + self.password)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.login_window = LoginUI(self)
                self.login_window.show()
                self.close()
示例#11
0
    def register(self):
        # validate fields
        self.email = self.register_ui.email.text()
        self.password = self.register_ui.password.text()
        self.password_repeat = self.register_ui.password_2.text()
        self.bridge_api_url = str(
            self.register_ui.bridge_url.text()).strip()  # get bridge api url

        if len(self.bridge_api_url) < 6:  # xxx.yy
            self.bridge_api_url = 'https://api.storj.io/'

        self.tools = Tools()
        success = False
        if self.email != '' and self.password != '' and self.password_repeat != '':
            if self.password == self.password_repeat:
                if (self.tools.check_email(self.email)):
                    # take login action
                    try:
                        self.storj_client = storj.Client(
                            None, '',
                            storj_bridge=self.bridge_api_url).user_create(
                                str(self.email).strip(),
                                str(self.password).strip())
                        success = True
                    except storj.exception.StorjBridgeApiError as e:
                        j = json.loads(str(e))
                        if j[0]['error'] == 'Email is already registered':
                            QtGui.QMessageBox.about(
                                self, 'Warning', 'User with this e-mail is \
                                                    already registered! Please \
                                                    login or try a different \
                                                    e-mail!')
                        else:
                            QtGui.QMessageBox.about(self,
                                                    'Unhandled exception',
                                                    'Exception: ' + str(e))
                else:
                    QtGui.QMessageBox.about(
                        self, 'Warning', 'Your e-mail \
                                            seems to be invalid! Please check \
                                            e-mail and try again')
            else:
                QtGui.QMessageBox.about(
                    self, 'Warning', 'Given passwords are different! Please\
                                        check and try again!')
        else:
            QtGui.QMessageBox.about(self, 'Warning',
                                    'Please fill out all fields!')

        if success:
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Information, 'Success',
                'Successfully registered to Storj!'
                'Verify your email by clicking on the'
                'link that was sent to you.'
                'Then, you can login.', QtGui.QMessageBox.Ok)

            logger.debug('New user registrated')
            logger.debug('Email: %s' % self.email)
            logger.debug('Password: %s' % self.password)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.login_window = LoginUI(self)
                self.login_window.show()
                self.close()
示例#12
0
class AccountDashUI(QtGui.QMainWindow):
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.account_dash_ui = Ui_AccountDash()
        self.account_dash_ui.setupUi(self)

        self.storj_engine = StorjEngine()  # init StorjEngine
        self.tools = Tools()

        self.initialize_buckets_stats_table()

        self.createNewBucketsStatsGetThread()

    def createNewBucketsStatsGetThread(self):
        thread = threading.Thread(target=self.fill_buckets_stats_table,
                                  args=())
        thread.start()

    def initialize_buckets_stats_table(self):
        self.table_header = ['Bucket name', 'Files count', 'Total used space']
        self.account_dash_ui.buckets_stats_table.setColumnCount(3)
        self.account_dash_ui.buckets_stats_table.setRowCount(0)
        horHeaders = self.table_header
        self.account_dash_ui.buckets_stats_table.setHorizontalHeaderLabels(
            horHeaders)
        self.account_dash_ui.buckets_stats_table.resizeColumnsToContents()
        self.account_dash_ui.buckets_stats_table.resizeRowsToContents()

        self.account_dash_ui.buckets_stats_table.horizontalHeader().\
            setResizeMode(QtGui.QHeaderView.Stretch)

    def fill_buckets_stats_table(self):
        total_files_size = 0
        total_files_count = 0
        for bucket in self.storj_engine.storj_client.bucket_list():
            total_bucket_files_size = 0
            total_bucket_files_count = 0
            # fill table
            table_row_count = self.account_dash_ui.\
                buckets_stats_table.rowCount()

            self.account_dash_ui.buckets_stats_table.setRowCount(
                table_row_count + 1)

            for file in self.storj_engine.storj_client.bucket_files(
                    bucket_id=bucket.id):
                total_bucket_files_size += int(file['size'])
                total_bucket_files_count += 1

            self.account_dash_ui.buckets_stats_table.setItem(
                table_row_count, 0, QtGui.QTableWidgetItem(bucket.name))

            self.account_dash_ui.buckets_stats_table.setItem(
                table_row_count, 1,
                QtGui.QTableWidgetItem(str(total_bucket_files_count)))

            self.account_dash_ui.buckets_stats_table.setItem(
                table_row_count, 2,
                QtGui.QTableWidgetItem(
                    str(self.tools.human_size(total_bucket_files_size))))

            total_files_count += total_bucket_files_count
            total_files_size += total_bucket_files_size

        self.account_dash_ui.files_total_count.setText(str(total_files_count))
        self.account_dash_ui.total_used_space.setText(
            str(self.tools.human_size(total_files_size)))
示例#13
0
    def update_files_list(self):

        self.tools = Tools()

        #model = MyTableModel(headerdata = )
        model = TableModel(1, 1)
        #model = QtGui.QStandardItemModel(1, 1)  # initialize model for inserting to table

        file_list_header_labels = ['File name', 'File size', 'File ID']

        if DISPLAY_FILE_CREATION_DATE_IN_MAIN:
            file_list_header_labels.append("Creation date")

        model.setHorizontalHeaderLabels(file_list_header_labels)

        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex(
        )
        self.current_selected_bucket_id = self.bucket_id_list[
            self.current_bucket_index]

        i = 0

        try:
            self.emit(QtCore.SIGNAL("changeLoadingGif"), True)
            for self.file_details in self.storj_engine.storj_client.bucket_files(
                    str(self.current_selected_bucket_id)):
                item = QtGui.QStandardItem(
                    str(self.file_details['filename'].replace(
                        '[DECRYPTED]', "")).decode('utf8'))
                model.setItem(i, 0, item)  # row, column, item (StandardItem)

                file_size_str = self.tools.human_size(
                    int(self.file_details['size'])
                )  # get human readable file size

                item = QtGui.QStandardItem(str(file_size_str))
                model.setItem(i, 1,
                              item)  # row, column, item (QQtGui.StandardItem)

                # item = QtGui.QStandardItem(str(self.file_details["mimetype"]))
                # model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                item = QtGui.QStandardItem(str(self.file_details['id']))
                model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                #print self.file_details

                if DISPLAY_FILE_CREATION_DATE_IN_MAIN:
                    item = QtGui.QStandardItem(
                        str(self.file_details['created']).replace(
                            'Z', "").replace('T', " "))
                    model.setItem(i, 3,
                                  item)  # row, column, item (QStandardItem)

                i = i + 1

                self.__logger.info(self.file_details)
        except sjexc.StorjBridgeApiError as e:
            self.__logger.error(e)

        self.file_manager_ui.files_list_tableview.clearFocus()
        self.file_manager_ui.files_list_tableview.setModel(model)
        self.file_manager_ui.files_list_tableview.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

        if FILE_LIST_SORTING_MAIN_ENABLED:
            self.file_manager_ui.files_list_tableview.setSortingEnabled(True)
            self.file_manager_ui.files_list_tableview.horizontalHeader(
            ).sortIndicatorChanged.connect(self.handleSortIndicatorChanged)
            self.file_manager_ui.files_list_tableview.sortByColumn(
                0, QtCore.Qt.AscendingOrder)
        self.emit(QtCore.SIGNAL("changeLoadingGif"), False)
示例#14
0
class FileManagerUI(QtGui.QMainWindow):

    def __init__(self, parent=None, bucketid=None):
        QtGui.QWidget.__init__(self, parent)
        self.file_manager_ui = Ui_FileManager()
        self.file_manager_ui.setupUi(self)

        QtCore.QObject.connect(self.file_manager_ui.bucket_select_combo_box,
                               QtCore.SIGNAL("currentIndexChanged(const QString&)"),
                               self.createNewFileListUpdateThread)  # connect ComboBox change listener
        QtCore.QObject.connect(self.file_manager_ui.file_mirrors_bt, QtCore.SIGNAL("clicked()"),
                               self.open_mirrors_list_window)  # create bucket action
        QtCore.QObject.connect(self.file_manager_ui.quit_bt, QtCore.SIGNAL("clicked()"),
                               self.close)  # create bucket action
        QtCore.QObject.connect(self.file_manager_ui.file_download_bt, QtCore.SIGNAL("clicked()"),
                               self.open_single_file_download_window)  # create bucket action
        QtCore.QObject.connect(self.file_manager_ui.file_delete_bt, QtCore.SIGNAL("clicked()"),
                               self.delete_selected_file)  # delete selected file

        QtCore.QObject.connect(self.file_manager_ui.new_file_upload_bt, QtCore.SIGNAL("clicked()"),
                               self.open_single_file_upload_window)  # delete selected file

        self.storj_engine = StorjEngine()  # init StorjEngine
        self.createNewBucketResolveThread()

    def open_single_file_upload_window(self):
        self.single_file_upload_window = SingleFileUploadUI(self)
        self.single_file_upload_window.show()

    def delete_selected_file(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex()
        self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(set(index.row() for index in
                          self.file_manager_ui.files_list_tableview.selectedIndexes()))

        selected = False
        for row in rows:
            selected = True
            index = tablemodel.index(row, 3)  # get file ID index
            index_filename = tablemodel.index(row, 0)  # get file name index

            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            selected_file_name = str(tablemodel.data(index_filename).toString())
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, "Question",
                                       "Are you sure you want to delete this file? File name: " + selected_file_name, (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
            result = msgBox.exec_()
            logger.debug(result)
            if result == QtGui.QMessageBox.Yes:
                try:
                    self.storj_engine.storj_client.file_remove(str(self.current_selected_bucket_id), str(selected_file_id))
                    self.createNewFileListUpdateThread()  # update files list
                    QtGui.QMessageBox.about(self, "Success", 'File "' + str(selected_file_name) + '" was deleted successfully')
                except sjexc.StorjBridgeApiError as e:
                    QtGui.QMessageBox.about(self, "Error", "Bridge exception occured while trying to delete file: " + str(e))
                except Exception as e:
                    QtGui.QMessageBox.about(self, "Error", "Unhandled exception occured while trying to delete file: " + str(e))

        if not selected:
            QtGui.QMessageBox.about(self, "Information", "Please select file which you want to delete")

        return True

    def open_mirrors_list_window(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex()
        self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(set(index.row() for index in
                          self.file_manager_ui.files_list_tableview.selectedIndexes()))
        i = 0
        for row in rows:
            logger.info('Row %d is selected' % row)
            index = tablemodel.index(row, 3)  # get file ID
            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            self.file_mirrors_list_window = FileMirrorsListUI(self, str(self.current_selected_bucket_id),
                                                              selected_file_id)
            self.file_mirrors_list_window.show()
            i += 1

        if i == 0:
            QtGui.QMessageBox.about(self, "Warning!", "Please select file from file list!")

        logger.debug(1)

    def createNewFileListUpdateThread(self):
        download_thread = threading.Thread(target=self.update_files_list, args=())
        download_thread.start()

    def update_files_list(self):

        self.tools = Tools()

        model = QtGui.QStandardItemModel(1, 1)  # initialize model for inserting to table

        model.setHorizontalHeaderLabels(['File name', 'File size', 'Mimetype', 'File ID'])

        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex()
        self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]

        i = 0

        try:
            for self.file_details in self.storj_engine.storj_client.bucket_files(str(self.current_selected_bucket_id)):
                item = QtGui.QStandardItem(str(self.file_details["filename"]))
                model.setItem(i, 0, item)  # row, column, item (StandardItem)

                file_size_str = self.tools.human_size(int(self.file_details["size"]))  # get human readable file size

                item = QtGui.QStandardItem(str(file_size_str))
                model.setItem(i, 1, item)  # row, column, item (QQtGui.StandardItem)

                item = QtGui.QStandardItem(str(self.file_details["mimetype"]))
                model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                item = QtGui.QStandardItem(str(self.file_details["id"]))
                model.setItem(i, 3, item)  # row, column, item (QStandardItem)

                i = i + 1

                logger.info(self.file_details)
        except sjexc.StorjBridgeApiError as e:
            logger.error(e)

        self.file_manager_ui.files_list_tableview.clearFocus()
        self.file_manager_ui.files_list_tableview.setModel(model)
        self.file_manager_ui.files_list_tableview.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

    def createNewBucketResolveThread(self):
        download_thread = threading.Thread(target=self.initialize_bucket_select_combobox, args=())
        download_thread.start()

    def initialize_bucket_select_combobox(self):
        self.buckets_list = []
        self.bucket_id_list = []
        self.storj_engine = StorjEngine()  # init StorjEngine
        i = 0
        try:
            for bucket in self.storj_engine.storj_client.bucket_list():
                self.buckets_list.append(str(bucket.name))  # append buckets to list
                self.bucket_id_list.append(str(bucket.id))  # append buckets to list
                i = i + 1
        except sjexc.StorjBridgeApiError as e:
            QtGui.QMessageBox.about(self, "Unhandled bucket resolving exception", "Exception: " + str(e))

        self.file_manager_ui.bucket_select_combo_box.addItems(self.buckets_list)

    def open_single_file_download_window(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex()
        self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(set(index.row() for index in
                          self.file_manager_ui.files_list_tableview.selectedIndexes()))
        i = 0
        for row in rows:
            logger.info('Row %d is selected' % row)
            index = tablemodel.index(row, 3)  # get file ID
            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            self.file_mirrors_list_window = SingleFileDownloadUI(self, str(self.current_selected_bucket_id),
                                                                 selected_file_id)
            self.file_mirrors_list_window.show()
            i += 1

        if i == 0:
            QtGui.QMessageBox.about(self, "Warning!", "Please select file from file list!")

        logger.debug(1)
示例#15
0
    def __init__(self,
                 parent=None,
                 bucketid=None,
                 fileid=None,
                 dashboard_instance=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_upload = Ui_SingleFileUpload()
        self.ui_single_file_upload.setupUi(self)
        # open bucket manager
        QtCore.QObject.connect(self.ui_single_file_upload.start_upload_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.createNewUploadThread)
        # open file select dialog
        QtCore.QObject.connect(self.ui_single_file_upload.file_path_select_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.select_file_path)
        # open tmp directory select dialog
        QtCore.QObject.connect(self.ui_single_file_upload.tmp_path_select_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.select_tmp_directory)

        # handle cancel action
        QtCore.QObject.connect(self.ui_single_file_upload.cancel_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.handle_cancel_action)

        self.already_used_farmers_nodes = []

        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

        self.ui_single_file_upload.uploaded_shards.setText("Waiting...")

        self.is_upload_active = False
        self.current_active_connections = 0

        self.ui_single_file_upload.connections_onetime.setValue(
            int(MAX_UPLOAD_CONNECTIONS_AT_SAME_TIME)
        )  # user can set it manually default value from constants file

        if platform == 'linux' or platform == 'linux2':
            # linux
            self.temp_dir = '/tmp'
        elif platform == 'darwin':
            # OS X
            self.temp_dir = '/tmp'
        elif platform == 'win32':
            # Windows
            self.temp_dir = 'C:\\Windows\\temp\\'
        self.ui_single_file_upload.tmp_path.setText(self.temp_dir)

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.connect(self, QtCore.SIGNAL('addRowToUploadQueueTable'),
                     self.add_row_upload_queue_table)

        self.connect(self, QtCore.SIGNAL('incrementShardsProgressCounters'),
                     self.increment_shards_progress_counters)
        self.connect(self, QtCore.SIGNAL('updateUploadTaskState'),
                     self.update_upload_task_state)
        self.connect(self, QtCore.SIGNAL('updateShardUploadProgress'),
                     self.update_shard_upload_progess)
        self.connect(self, QtCore.SIGNAL('showFileNotSelectedError'),
                     self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL('showInvalidPathError'),
                     self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL('showInvalidTemporaryPathError'),
                     self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL('refreshOverallProgress'),
                     self.refresh_overall_progress)
        self.connect(self, QtCore.SIGNAL('showFileUploadedSuccessfully'),
                     self.show_upload_finished_message)
        self.connect(
            self, QtCore.SIGNAL('finishUpload'), lambda: self.finish_upload(
                os.path.split(str(self.ui_single_file_upload.file_path.text()))
                [1], str(self.current_selected_bucket_id)))
        self.connect(self, QtCore.SIGNAL('setCurrentUploadState'),
                     self.set_current_status)
        self.connect(self, QtCore.SIGNAL('updateShardUploadCounters'),
                     self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL('setCurrentActiveConnections'),
                     self.set_current_active_connections)
        self.connect(self, QtCore.SIGNAL('setShardSize'), self.set_shard_size)
        self.connect(self, QtCore.SIGNAL('createShardUploadThread'),
                     self.createNewShardUploadThread)
        # self.connect(self, QtCore.SIGNAL('handleCancelAction'), self.ha)

        # resolve buckets and put to buckets combobox
        self.createBucketResolveThread()
        self.ui_single_file_upload.files_list_view_bt.mousePressEvent = self.display_files_queue_change

        self.is_files_queue_table_visible = False

        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("incrementShardsProgressCounters"))

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

        self.prepare_files_queue_table()

        self.clip = QtGui.QApplication.clipboard()

        self.ui_single_file_upload.connections_onetime.setMaximum(
            MAX_ALLOWED_UPLOAD_CONCURRENCY)

        if DATA_TABLE_EDIT_ENABLED == False:
            self.ui_single_file_upload.shard_queue_table_widget.setEditTriggers(
                QtGui.QAbstractItemView.NoEditTriggers)

        self.current_row = 0
示例#16
0
    def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_upload = Ui_SingleFileUpload()
        self.ui_single_file_upload.setupUi(self)
        # open bucket manager
        QtCore.QObject.connect(
            self.ui_single_file_upload.start_upload_bt,
            QtCore.SIGNAL('clicked()'),
            self.createNewUploadThread)
        # open file select dialog
        QtCore.QObject.connect(
            self.ui_single_file_upload.file_path_select_bt,
            QtCore.SIGNAL('clicked()'),
            self.select_file_path)
        # open tmp directory select dialog
        QtCore.QObject.connect(
            self.ui_single_file_upload.tmp_path_select_bt,
            QtCore.SIGNAL('clicked()'),
            self.select_tmp_directory)

        # handle cancel action
        QtCore.QObject.connect(
            self.ui_single_file_upload.cancel_bt,
            QtCore.SIGNAL('clicked()'),
            self.handle_cancel_action)


        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

        self.ui_single_file_upload.uploaded_shards.setText("Waiting...")

        self.is_upload_active = False
        self.current_active_connections = 0

        if platform == 'linux' or platform == 'linux2':
            # linux
            self.temp_dir = '/tmp'
        elif platform == 'darwin':
            # OS X
            self.temp_dir = '/tmp'
        elif platform == 'win32':
            # Windows
            self.temp_dir = 'C:\\Windows\\temp\\'
        self.ui_single_file_upload.tmp_path.setText(self.temp_dir)

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.connect(self, QtCore.SIGNAL('addRowToUploadQueueTable'), self.add_row_upload_queue_table)

        self.connect(self, QtCore.SIGNAL('incrementShardsProgressCounters'), self.increment_shards_progress_counters)
        self.connect(self, QtCore.SIGNAL('updateUploadTaskState'), self.update_upload_task_state)
        self.connect(self, QtCore.SIGNAL('updateShardUploadProgress'), self.update_shard_upload_progess)
        self.connect(self, QtCore.SIGNAL('showFileNotSelectedError'), self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL('showInvalidPathError'), self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL('showInvalidTemporaryPathError'), self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL('refreshOverallProgress'), self.refresh_overall_progress)
        self.connect(self, QtCore.SIGNAL('showFileUploadedSuccessfully'), self.show_upload_finished_message)
        self.connect(self, QtCore.SIGNAL('finishUpload'),
            lambda: self.finish_upload(os.path.split(
                str(self.ui_single_file_upload.file_path.text()))[1],
                str(self.current_selected_bucket_id)))
        self.connect(self, QtCore.SIGNAL('setCurrentUploadState'), self.set_current_status)
        self.connect(self, QtCore.SIGNAL('updateShardUploadCounters'), self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL('setCurrentActiveConnections'), self.set_current_active_connections)
        self.connect(self, QtCore.SIGNAL('setShardSize'), self.set_shard_size)
        #self.connect(self, QtCore.SIGNAL('handleCancelAction'), self.ha)

        # resolve buckets and put to buckets combobox
        self.createBucketResolveThread()

        # file_pointers = self.storj_engine.storj_client.file_pointers(
        #   "6acfcdc62499144929cf9b4a", "dfba26ab34466b1211c60d02")

        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("incrementShardsProgressCounters"))

        self.max_retries_upload_to_same_farmer = MAX_RETRIES_UPLOAD_TO_SAME_FARMER
        self.max_retries_negotiate_contract = MAX_RETRIES_NEGOTIATE_CONTRACT

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)
示例#17
0
class SyncOptionsUI(QtGui.QMainWindow):
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.sync_menu_ui = Ui_FileSyncOptions()
        self.sync_menu_ui.setupUi(self)

        self.sync_configuration_manager = SyncConfiguration()
        self.tools = Tools()

        QtCore.QObject.connect(self.sync_menu_ui.restore_defaults_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.restore_default_settings)

        QtCore.QObject.connect(self.sync_menu_ui.save_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.save_sync_options)

        QtCore.QObject.connect(self.sync_menu_ui.cancel_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.save_sync_options)

        QtCore.QObject.connect(self.sync_menu_ui.add_sync_dir_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.add_new_sync_directory)

        QtCore.QObject.connect(self.sync_menu_ui.remove_sync_dir,
                               QtCore.SIGNAL('clicked()'),
                               self.delete_sync_directory)

        # prepare table
        self.sync_dirs_table_header = [
            'Directory', 'Add date', 'Files count', 'Actual total size'
        ]

        self.sync_menu_ui.sync_directories_tableWidget.setRowCount(0)

        self.sync_menu_ui.sync_directories_tableWidget.resizeColumnsToContents(
        )
        self.sync_menu_ui.sync_directories_tableWidget.resizeRowsToContents()
        self.sync_menu_ui.sync_directories_tableWidget.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

        self.sync_configuration_manager.paint_config_to_ui(self.sync_menu_ui)

        self.sync_menu_ui.sync_directories_tableWidget.setHorizontalHeaderLabels(
            self.sync_dirs_table_header)
        self.sync_menu_ui.sync_directories_tableWidget.setColumnCount(4)

    def restore_default_settings(self):
        msgBox = QtGui.QMessageBox(
            QtGui.QMessageBox.Question, "Question",
            "Are you sure that you want to restore sync settings to default?",
            (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
        result = msgBox.exec_()
        if result == QtGui.QMessageBox.Yes:
            QtGui.QMessageBox.about(
                self, 'Success',
                'Default settings have been restored successfully!')
            return True
        else:
            return False

    def save_sync_options(self):
        # validate settings

        self.sync_configuration_manager.save_sync_configuration(
            self.sync_menu_ui)  # save configuration
        QtGui.QMessageBox.about(
            self, 'Success',
            'Synchronization configuration saved successfully!')

    def add_new_sync_directory(self):
        self.selected_sync_sirectory = QtGui.QFileDialog.getExistingDirectory(
            None, 'Select a sync directory: ', "",
            QtGui.QFileDialog.ShowDirsOnly)

        if self.selected_sync_sirectory != "":
            self.current_time = time.ctime()
            total_files_count = self.tools.count_files_in_dir(
                directory=self.selected_sync_sirectory)
            total_files_size = self.tools.human_size(
                self.tools.count_directory_size(self.selected_sync_sirectory,
                                                True))

            table_row_count = self.sync_menu_ui.sync_directories_tableWidget.rowCount(
            )
            self.sync_menu_ui.sync_directories_tableWidget.setRowCount(
                table_row_count + 1)
            self.sync_menu_ui.sync_directories_tableWidget.setItem(
                table_row_count, 0,
                QtGui.QTableWidgetItem(str(self.selected_sync_sirectory)))
            self.sync_menu_ui.sync_directories_tableWidget.setItem(
                table_row_count, 1,
                QtGui.QTableWidgetItem(str(self.current_time)))
            self.sync_menu_ui.sync_directories_tableWidget.setItem(
                table_row_count, 2,
                QtGui.QTableWidgetItem(str(total_files_count)))
            self.sync_menu_ui.sync_directories_tableWidget.setItem(
                table_row_count, 3,
                QtGui.QTableWidgetItem(str(total_files_size)))

        return True

    def delete_sync_directory(self):
        tablemodel = self.sync_menu_ui.sync_directories_tableWidget.model()
        rows = sorted(
            set(index.row() for index in self.sync_menu_ui.
                sync_directories_tableWidget.selectedIndexes()))

        selected = False
        for row in rows:
            selected = True
            index = tablemodel.index(row, 0)  # get directory index

            # We suppose data are strings
            selected_directory_in_table = str(
                tablemodel.data(index).toString())
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Question, 'Question',
                'Are you sure you want to delete this sync directory from table? Directory which you want to delete from table: %s'
                % str('"' + selected_directory_in_table + '"').decode('utf-8'),
                (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Yes:
                # delete directory from table
                self.sync_menu_ui.sync_directories_tableWidget.removeRow(
                    int(row))
                QtGui.QMessageBox.about(
                    self, 'Information',
                    'Directory %s have been successfully deleted form table' %
                    str('"' + selected_directory_in_table +
                        '"').decode('utf-8'))

        if not selected:
            QtGui.QMessageBox.about(
                self, 'Information',
                'Please select sync directory which you want to delete form table'
            )

        return True
示例#18
0
class MainUI(QtGui.QMainWindow):
    """Main UI section."""

    __logger = logging.getLogger('%s.MainUI' % __name__)

    def __init__(self, parent=None, bucketid=None):
        QtGui.QWidget.__init__(self, parent)
        self.file_manager_ui = Ui_MainMenu()
        self.file_manager_ui.setupUi(self)

        #self.change_loading_gif()
        QtCore.QObject.connect(
            self.file_manager_ui.bucket_select_combo_box,
            QtCore.SIGNAL('currentIndexChanged(const QString&)'), self.
            createNewFileListUpdateThread)  # connect ComboBox change listener
        QtCore.QObject.connect(
            self.file_manager_ui.file_mirrors_bt, QtCore.SIGNAL('clicked()'),
            self.open_mirrors_list_window)  # create bucket action
        # create bucket action
        # QtCore.QObject.connect(self.file_manager_ui.quit_bt, QtCore.SIGNAL('clicked()'),  self.close)
        QtCore.QObject.connect(
            self.file_manager_ui.file_download_bt, QtCore.SIGNAL('clicked()'),
            self.open_single_file_download_window)  # create bucket action
        QtCore.QObject.connect(
            self.file_manager_ui.file_delete_bt, QtCore.SIGNAL('clicked()'),
            self.delete_selected_file)  # delete selected file

        self.connect(self, QtCore.SIGNAL("changeLoadingGif"),
                     self.change_loading_gif)

        self.file_manager_ui.settings_bt.mousePressEvent = self.open_settings_window
        self.file_manager_ui.refresh_bt.mousePressEvent = self.createNewFileListUpdateThread

        # self.file_manager_ui.refresh_bt.mousePressEvent = self.createNewFileListUpdateThread()

        QtCore.QObject.connect(
            self.file_manager_ui.new_file_upload_bt,
            QtCore.SIGNAL('clicked()'),
            self.open_single_file_upload_window)  # delete selected file

        # open bucket edit window
        QtCore.QObject.connect(
            self.file_manager_ui.edit_bucket_bt, QtCore.SIGNAL('clicked()'),
            lambda: self.open_bucket_editing_window(action='edit'))

        # open bucket edit window
        QtCore.QObject.connect(
            self.file_manager_ui.create_bucket_bt, QtCore.SIGNAL('clicked()'),
            lambda: self.open_bucket_editing_window(action='add'))

        self.storj_engine = StorjEngine()  # init StorjEngine
        # self.account_manager = AccountManager()  # init AccountManager

        user_email = self.storj_engine.account_manager.get_user_email()
        self.file_manager_ui.account_label.setText(str(user_email))

        self.createNewBucketResolveThread()

    def change_loading_gif(self, is_visible):
        if is_visible:
            movie = QtGui.QMovie(':/resources/loading.gif')
            self.file_manager_ui.refresh_bt.setMovie(movie)
            movie.start()
        else:
            self.file_manager_ui.refresh_bt.setPixmap(
                QtGui.QPixmap((":/resources/refresh.png")))

    def open_bucket_editing_window(self, action):
        if action == 'edit':
            self.bucket_editing_window = BucketEditingUI(
                self,
                action=action,
                bucketid=str(self.current_selected_bucket_id),
                dashboard_instance=self)

        else:
            self.bucket_editing_window = BucketEditingUI(
                self, action=action, dashboard_instance=self)
        self.bucket_editing_window.show()

    def open_single_file_upload_window(self):
        self.single_file_upload_window = SingleFileUploadUI(
            self, dashboard_instance=self)
        self.single_file_upload_window.show()

    def open_settings_window(self, b):
        self.open_settings_window = ClientConfigurationUI(self)
        self.open_settings_window.show()

    def delete_selected_file(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex(
        )
        self.current_selected_bucket_id = self.bucket_id_list[
            self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(
            set(index.row() for index in
                self.file_manager_ui.files_list_tableview.selectedIndexes()))

        selected = False
        for row in rows:
            selected = True
            index = tablemodel.index(row, 2)  # get file ID index
            index_filename = tablemodel.index(row, 0)  # get file name index

            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            selected_file_name = str(
                tablemodel.data(index_filename).toString())
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Question, 'Question',
                'Are you sure you want to delete this file? File name: %s' %
                str(selected_file_name).decode('utf-8'),
                (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))

            result = msgBox.exec_()
            self.__logger.debug(result)

            if result == QtGui.QMessageBox.Yes:
                try:
                    self.storj_engine.storj_client.file_remove(
                        str(self.current_selected_bucket_id),
                        str(selected_file_id))
                    # update files list
                    self.createNewFileListUpdateThread()
                    QtGui.QMessageBox.about(
                        self, 'Success',
                        'File "%s" has been deleted successfully' %
                        selected_file_name)
                except sjexc.StorjBridgeApiError as e:
                    self.__logger.error(e)
                    QtGui.QMessageBox.about(
                        self, 'Error',
                        'Bridge exception occured while trying to delete file: %s'
                        % e)

                except Exception as e:
                    self.__logger.error(e)
                    QtGui.QMessageBox.about(
                        self, 'Error',
                        'Unhandled exception occured while trying to delete file: %s'
                        % e)

        if not selected:
            QtGui.QMessageBox.about(
                self, 'Information',
                'Please select file which you want to delete')

        return True

    def open_mirrors_list_window(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex(
        )
        self.current_selected_bucket_id = self.bucket_id_list[
            self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(
            set(index.row() for index in
                self.file_manager_ui.files_list_tableview.selectedIndexes()))
        i = 0
        for row in rows:
            self.__logger.info('Row %d is selected' % row)
            index = tablemodel.index(row, 2)  # get file ID
            index_filename = tablemodel.index(row, 0)  # get file ID
            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            selected_file_name = str(
                tablemodel.data(index_filename).toString())
            self.file_mirrors_list_window = FileMirrorsListUI(
                self,
                str(self.current_selected_bucket_id),
                selected_file_id,
                filename=selected_file_name)
            self.file_mirrors_list_window.show()
            i += 1

        if i == 0:
            QtGui.QMessageBox.about(self, 'Warning!',
                                    'Please select file from file list!')

        self.__logger.debug(1)

    def createNewFileListUpdateThread(self, a=None):
        download_thread = threading.Thread(target=self.update_files_list,
                                           args=())
        download_thread.start()

    def update_files_list(self):

        self.tools = Tools()

        #model = MyTableModel(headerdata = )
        model = TableModel(1, 1)
        #model = QtGui.QStandardItemModel(1, 1)  # initialize model for inserting to table

        file_list_header_labels = ['File name', 'File size', 'File ID']

        if DISPLAY_FILE_CREATION_DATE_IN_MAIN:
            file_list_header_labels.append("Creation date")

        model.setHorizontalHeaderLabels(file_list_header_labels)

        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex(
        )
        self.current_selected_bucket_id = self.bucket_id_list[
            self.current_bucket_index]

        i = 0

        try:
            self.emit(QtCore.SIGNAL("changeLoadingGif"), True)
            for self.file_details in self.storj_engine.storj_client.bucket_files(
                    str(self.current_selected_bucket_id)):
                item = QtGui.QStandardItem(
                    str(self.file_details['filename'].replace(
                        '[DECRYPTED]', "")).decode('utf8'))
                model.setItem(i, 0, item)  # row, column, item (StandardItem)

                file_size_str = self.tools.human_size(
                    int(self.file_details['size'])
                )  # get human readable file size

                item = QtGui.QStandardItem(str(file_size_str))
                model.setItem(i, 1,
                              item)  # row, column, item (QQtGui.StandardItem)

                # item = QtGui.QStandardItem(str(self.file_details["mimetype"]))
                # model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                item = QtGui.QStandardItem(str(self.file_details['id']))
                model.setItem(i, 2, item)  # row, column, item (QStandardItem)

                #print self.file_details

                if DISPLAY_FILE_CREATION_DATE_IN_MAIN:
                    item = QtGui.QStandardItem(
                        str(self.file_details['created']).replace(
                            'Z', "").replace('T', " "))
                    model.setItem(i, 3,
                                  item)  # row, column, item (QStandardItem)

                i = i + 1

                self.__logger.info(self.file_details)
        except sjexc.StorjBridgeApiError as e:
            self.__logger.error(e)

        self.file_manager_ui.files_list_tableview.clearFocus()
        self.file_manager_ui.files_list_tableview.setModel(model)
        self.file_manager_ui.files_list_tableview.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

        if FILE_LIST_SORTING_MAIN_ENABLED:
            self.file_manager_ui.files_list_tableview.setSortingEnabled(True)
            self.file_manager_ui.files_list_tableview.horizontalHeader(
            ).sortIndicatorChanged.connect(self.handleSortIndicatorChanged)
            self.file_manager_ui.files_list_tableview.sortByColumn(
                0, QtCore.Qt.AscendingOrder)
        self.emit(QtCore.SIGNAL("changeLoadingGif"), False)

    def handleSortIndicatorChanged(self, index, order):
        if index != 0:
            self.file_manager_ui.files_list_tableview.horizontalHeader(
            ).setSortIndicator(
                0,
                self.file_manager_ui.files_list_tableview.model().sortOrder())

    def createNewBucketResolveThread(self):
        download_thread = threading.Thread(
            target=self.initialize_bucket_select_combobox, args=())
        download_thread.start()

    def initialize_bucket_select_combobox(self):
        self.file_manager_ui.bucket_select_combo_box.clear()
        self.buckets_list = []
        self.bucket_id_list = []
        self.storj_engine = StorjEngine()  # init StorjEngine
        i = 0
        self.emit(QtCore.SIGNAL("changeLoadingGif"), True)
        try:
            for bucket in self.storj_engine.storj_client.bucket_list():
                self.buckets_list.append(str(
                    bucket.name).decode('utf8'))  # append buckets to list
                self.bucket_id_list.append(str(
                    bucket.id))  # append buckets to list
                i = i + 1
        except sjexc.StorjBridgeApiError as e:
            self.__logger.error(e)
            QtGui.QMessageBox.about(self,
                                    'Unhandled bucket resolving exception',
                                    'Exception: ' % e)

        self.file_manager_ui.bucket_select_combo_box.addItems(
            self.buckets_list)
        self.emit(QtCore.SIGNAL("changeLoadingGif"), False)

    def open_single_file_download_window(self):
        self.current_bucket_index = self.file_manager_ui.bucket_select_combo_box.currentIndex(
        )
        self.current_selected_bucket_id = self.bucket_id_list[
            self.current_bucket_index]

        tablemodel = self.file_manager_ui.files_list_tableview.model()
        rows = sorted(
            set(index.row() for index in
                self.file_manager_ui.files_list_tableview.selectedIndexes()))
        i = 0
        for row in rows:
            self.__logger.info('Row %d is selected' % row)
            index = tablemodel.index(row, 2)  # get file ID
            # We suppose data are strings
            selected_file_id = str(tablemodel.data(index).toString())
            self.file_mirrors_list_window = SingleFileDownloadUI(
                self, str(self.current_selected_bucket_id), selected_file_id)
            self.file_mirrors_list_window.show()
            i += 1

        if i == 0:
            QtGui.QMessageBox.about(self, 'Warning!',
                                    'Please select file from file list!')

        self.__logger.debug(1)
    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_download = Ui_SingleFileDownload()
        self.ui_single_file_download.setupUi(self)
        # QtCore.QObject.connect(self.ui_single_file_download., QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine
        self.filename_from_bridge = ""
        self.tools = Tools()

        self.bucket_id = bucketid
        self.file_id = fileid

        # init loggers
        # self.log_handler = LogHandler()
        # logging.setLoggerClass(get_global_logger(self.log_handler))
        # logger.addHandler(self.log_handler)

        # self.initialize_shard_queue_table(file_pointers)

        self.is_upload_active = False

        self.account_manager = AccountManager()  # init AccountManager

        self.user_password = self.account_manager.get_user_password()

        ########3

        QtCore.QObject.connect(
            self.ui_single_file_download.file_path_select_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_file_save_path)  # open file select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.tmp_dir_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_tmp_directory)  # open tmp directory select dialog

        QtCore.QObject.connect(
            self.ui_single_file_download.cancel_bt, QtCore.SIGNAL("clicked()"),
            self.handle_cancel_action)  # open tmp directory select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.start_download_bt,
            QtCore.SIGNAL("clicked()"),
            lambda: self.createNewDownloadInitThread(
                bucketid, fileid))  # begin file downloading process

        # print fileid + " id pliku"
        # QtCore.QObject.connect(self.ui_single_file_download.open_log_bt, QtCore.SIGNAL("clicked()"),
        #                        self.open_logs_window)  # open logs window

        self.connect(self,
                     QtCore.SIGNAL("incrementShardsDownloadProgressCounters"),
                     self.increment_shards_download_progress_counters)
        self.connect(self, QtCore.SIGNAL("updateShardDownloadProgress"),
                     self.update_shard_download_progess)
        self.connect(self, QtCore.SIGNAL("beginShardDownloadProccess"),
                     self.shard_download)
        self.connect(self, QtCore.SIGNAL("refreshOverallDownloadProgress"),
                     self.refresh_overall_download_progress)
        self.connect(self,
                     QtCore.SIGNAL("showDestinationFileNotSelectedError"),
                     self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL("showInvalidDestinationPathError"),
                     self.show_error_invalid_file_path)
        self.connect(self,
                     QtCore.SIGNAL("showInvalidTemporaryDownloadPathError"),
                     self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL("updateDownloadTaskState"),
                     self.update_download_task_state)
        self.connect(self, QtCore.SIGNAL("showStorjBridgeException"),
                     self.show_storj_bridge_exception)
        self.connect(self, QtCore.SIGNAL("showUnhandledException"),
                     self.show_unhandled_exception)
        self.connect(self, QtCore.SIGNAL("showFileDownloadedSuccessfully"),
                     self.show_download_finished_message)
        self.connect(self, QtCore.SIGNAL("showException"),
                     self.show_unhandled_exception)
        self.connect(self, QtCore.SIGNAL("addRowToDownloadQueueTable"),
                     self.add_row_download_queue_table)
        self.connect(self, QtCore.SIGNAL("getNextSetOfPointers"),
                     self.request_and_download_next_set_of_pointers)
        self.connect(self, QtCore.SIGNAL("setCurrentState"),
                     self.set_current_status)
        self.connect(self, QtCore.SIGNAL("updateShardCounters"),
                     self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL("retryWithNewDownloadPointer"),
                     self.retry_download_with_new_pointer)
        self.connect(self, QtCore.SIGNAL("showDestinationPathNotSelectedMsg"),
                     self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL("selectFileDestinationPath"),
                     self.select_file_save_path)
        self.connect(self, QtCore.SIGNAL("askFileOverwrite"),
                     self.ask_overwrite)

        self.connect(
            self, QtCore.SIGNAL("finishDownload"),
            lambda: self.create_download_finish_thread(
                str(
                    os.path.split(
                        str(self.ui_single_file_download.file_save_path.text())
                    )[1]).decode('utf-8')))

        self.overwrite_question_result = None
        self.overwrite_question_closed = False

        self.ui_single_file_download.current_state.setText(
            "Waiting for user action...")
        self.ui_single_file_download.downloaded_shards.setText(
            "Waiting for user...")
        self.shards_already_downloaded = 0

        self.createNewInitializationThread(bucketid, fileid)

        self.shard_download_percent_list = []

        # init limit variables
        self.max_retries_download_from_same_farmer = 3
        self.max_retries_get_file_pointers = 30

        # set default paths
        temp_dir = ""
        if platform == "linux" or platform == "linux2":
            # linux
            temp_dir = "/tmp/"
        elif platform == "darwin":
            # OS X
            temp_dir = "/tmp/"
        elif platform == "win32":
            # Windows
            temp_dir = "C:\\Windows\\temp\\"

        self.ui_single_file_download.tmp_dir.setText(str(temp_dir))

        # set config variables

        self.combine_tmpdir_name_with_token = False

        # set overall progress to 0
        self.ui_single_file_download.overall_progress.setValue(0)

        self.current_active_connections = 0

        self.already_started_shard_downloads_count = 0
        self.all_shards_count = 0
class SingleFileDownloadUI(QtGui.QMainWindow):
    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_download = Ui_SingleFileDownload()
        self.ui_single_file_download.setupUi(self)
        # QtCore.QObject.connect(self.ui_single_file_download., QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine
        self.filename_from_bridge = ""
        self.tools = Tools()

        self.bucket_id = bucketid
        self.file_id = fileid

        # init loggers
        # self.log_handler = LogHandler()
        # logging.setLoggerClass(get_global_logger(self.log_handler))
        # logger.addHandler(self.log_handler)

        # self.initialize_shard_queue_table(file_pointers)

        self.is_upload_active = False

        self.account_manager = AccountManager()  # init AccountManager

        self.user_password = self.account_manager.get_user_password()

        ########3

        QtCore.QObject.connect(
            self.ui_single_file_download.file_path_select_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_file_save_path)  # open file select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.tmp_dir_bt,
            QtCore.SIGNAL("clicked()"),
            self.select_tmp_directory)  # open tmp directory select dialog

        QtCore.QObject.connect(
            self.ui_single_file_download.cancel_bt, QtCore.SIGNAL("clicked()"),
            self.handle_cancel_action)  # open tmp directory select dialog
        QtCore.QObject.connect(
            self.ui_single_file_download.start_download_bt,
            QtCore.SIGNAL("clicked()"),
            lambda: self.createNewDownloadInitThread(
                bucketid, fileid))  # begin file downloading process

        # print fileid + " id pliku"
        # QtCore.QObject.connect(self.ui_single_file_download.open_log_bt, QtCore.SIGNAL("clicked()"),
        #                        self.open_logs_window)  # open logs window

        self.connect(self,
                     QtCore.SIGNAL("incrementShardsDownloadProgressCounters"),
                     self.increment_shards_download_progress_counters)
        self.connect(self, QtCore.SIGNAL("updateShardDownloadProgress"),
                     self.update_shard_download_progess)
        self.connect(self, QtCore.SIGNAL("beginShardDownloadProccess"),
                     self.shard_download)
        self.connect(self, QtCore.SIGNAL("refreshOverallDownloadProgress"),
                     self.refresh_overall_download_progress)
        self.connect(self,
                     QtCore.SIGNAL("showDestinationFileNotSelectedError"),
                     self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL("showInvalidDestinationPathError"),
                     self.show_error_invalid_file_path)
        self.connect(self,
                     QtCore.SIGNAL("showInvalidTemporaryDownloadPathError"),
                     self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL("updateDownloadTaskState"),
                     self.update_download_task_state)
        self.connect(self, QtCore.SIGNAL("showStorjBridgeException"),
                     self.show_storj_bridge_exception)
        self.connect(self, QtCore.SIGNAL("showUnhandledException"),
                     self.show_unhandled_exception)
        self.connect(self, QtCore.SIGNAL("showFileDownloadedSuccessfully"),
                     self.show_download_finished_message)
        self.connect(self, QtCore.SIGNAL("showException"),
                     self.show_unhandled_exception)
        self.connect(self, QtCore.SIGNAL("addRowToDownloadQueueTable"),
                     self.add_row_download_queue_table)
        self.connect(self, QtCore.SIGNAL("getNextSetOfPointers"),
                     self.request_and_download_next_set_of_pointers)
        self.connect(self, QtCore.SIGNAL("setCurrentState"),
                     self.set_current_status)
        self.connect(self, QtCore.SIGNAL("updateShardCounters"),
                     self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL("retryWithNewDownloadPointer"),
                     self.retry_download_with_new_pointer)
        self.connect(self, QtCore.SIGNAL("showDestinationPathNotSelectedMsg"),
                     self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL("selectFileDestinationPath"),
                     self.select_file_save_path)
        self.connect(self, QtCore.SIGNAL("askFileOverwrite"),
                     self.ask_overwrite)

        self.connect(
            self, QtCore.SIGNAL("finishDownload"),
            lambda: self.create_download_finish_thread(
                str(
                    os.path.split(
                        str(self.ui_single_file_download.file_save_path.text())
                    )[1]).decode('utf-8')))

        self.overwrite_question_result = None
        self.overwrite_question_closed = False

        self.ui_single_file_download.current_state.setText(
            "Waiting for user action...")
        self.ui_single_file_download.downloaded_shards.setText(
            "Waiting for user...")
        self.shards_already_downloaded = 0

        self.createNewInitializationThread(bucketid, fileid)

        self.shard_download_percent_list = []

        # init limit variables
        self.max_retries_download_from_same_farmer = 3
        self.max_retries_get_file_pointers = 30

        # set default paths
        temp_dir = ""
        if platform == "linux" or platform == "linux2":
            # linux
            temp_dir = "/tmp/"
        elif platform == "darwin":
            # OS X
            temp_dir = "/tmp/"
        elif platform == "win32":
            # Windows
            temp_dir = "C:\\Windows\\temp\\"

        self.ui_single_file_download.tmp_dir.setText(str(temp_dir))

        # set config variables

        self.combine_tmpdir_name_with_token = False

        # set overall progress to 0
        self.ui_single_file_download.overall_progress.setValue(0)

        self.current_active_connections = 0

        self.already_started_shard_downloads_count = 0
        self.all_shards_count = 0

    def show_destination_path_not_selected_msg(self):
        return 1

    def handle_cancel_action(self):
        if self.is_upload_active:
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Question, "Question",
                "Are you sure that you want cancel download and close this window?",
                (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Yes:
                self.close()
        else:
            self.close()

    def update_shards_counters(self):
        self.ui_single_file_download.downloaded_shards.setText(
            str(self.shards_already_downloaded) + "/" +
            str(self.all_shards_count))

    def initialize_download_queue_table(self):
        # initialize variables
        self.shards_already_downloaded = 0
        self.downloaded_shards_count = 0
        self.download_queue_progressbar_list = []

        self.download_queue_table_header = [
            'Progress', 'Hash', 'Farmer', 'State', 'Shard index'
        ]
        self.ui_single_file_download.shard_queue_table.setColumnCount(5)
        self.ui_single_file_download.shard_queue_table.setRowCount(0)
        horHeaders = self.download_queue_table_header
        self.ui_single_file_download.shard_queue_table.setHorizontalHeaderLabels(
            horHeaders)
        self.ui_single_file_download.shard_queue_table.resizeColumnsToContents(
        )
        self.ui_single_file_download.shard_queue_table.resizeRowsToContents()

        self.ui_single_file_download.shard_queue_table.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

        # print "inijalizacja tabeli"

    def add_row_download_queue_table(self, row_data):
        self.download_queue_progressbar_list.append(QtGui.QProgressBar())

        self.download_queue_table_row_count = self.ui_single_file_download.shard_queue_table.rowCount(
        )

        self.ui_single_file_download.shard_queue_table.setRowCount(
            self.download_queue_table_row_count + 1)

        self.ui_single_file_download.shard_queue_table.setCellWidget(
            self.download_queue_table_row_count, 0,
            self.download_queue_progressbar_list[
                self.download_queue_table_row_count])
        self.ui_single_file_download.shard_queue_table.setItem(
            self.download_queue_table_row_count, 1,
            QtGui.QTableWidgetItem(row_data["hash"]))
        self.ui_single_file_download.shard_queue_table.setItem(
            self.download_queue_table_row_count, 2,
            QtGui.QTableWidgetItem(
                str(row_data["farmer_address"]) + ":" +
                str(row_data["farmer_port"])))
        self.ui_single_file_download.shard_queue_table.setItem(
            self.download_queue_table_row_count, 3,
            QtGui.QTableWidgetItem(str(row_data["state"])))
        self.ui_single_file_download.shard_queue_table.setItem(
            self.download_queue_table_row_count, 4,
            QtGui.QTableWidgetItem(str(row_data["shard_index"])))

        self.download_queue_progressbar_list[
            self.download_queue_table_row_count].setValue(0)
        # print "dodawanie wiersza 2"

    def _add_shard_to_table(self, pointers_content, shard, chapters):
        """
        Add a row to the shard table and return the row number
        """
        # Add items to shard queue table view
        tablerowdata = {}
        tablerowdata["farmer_address"] = pointers_content["farmer"]["address"]
        tablerowdata["farmer_port"] = pointers_content["farmer"]["port"]
        tablerowdata["hash"] = str(pointers_content["hash"])
        tablerowdata["state"] = "Downloading..."
        tablerowdata["shard_index"] = str(chapters)

        # logger.warning('"log_event_type": "debug"')
        #logger.debug('"title": "Contract negotiated"')

        logger.debug('Resolved pointer for download \
                     : ' + str(pointers_content["farmer"]["address"]) + ":" +
                     str(pointers_content["farmer"]["port"]))
        # logger.warning(str({"log_event_type": "debug", "title": "Contract negotiated",
        #                     "description": "Storage contract negotiated with: " + str(frame_content["farmer"]["address"] + ":" + str(frame_content["farmer"]["port"]))}))

        self.emit(QtCore.SIGNAL("addRowToDownloadQueueTable"),
                  tablerowdata)  # add row to table

        rowcount = self.ui_single_file_download.shard_queue_table.rowCount()
        # print "dodawanie wiersza"

        return rowcount

    def show_download_finished_message(self):
        self.ui_single_file_download.start_download_bt.setStyleSheet(
            ("QPushButton:hover{\n"
             "  background-color: #83bf20;\n"
             "  border-color: #83bf20;\n"
             "}\n"
             "QPushButton:active {\n"
             "  background-color: #93cc36;\n"
             "  border-color: #93cc36;\n"
             "}\n"
             "QPushButton{\n"
             "  background-color: #88c425;\n"
             "    border: 1px solid #88c425;\n"
             "    color: #fff;\n"
             "    border-radius: 7px;\n"
             "}"))
        QMessageBox.information(self, "Success!",
                                "File downloaded successfully!")

    """
    def open_logs_window(self):
        self.logs_window = LogsUI(self)
        self.logs_window.show()
    """

    def show_unhandled_exception(self, exception_content):
        QMessageBox.critical(self, "Unhandled error", str(exception_content))

    def show_storj_bridge_exception(self, exception_content):
        try:
            j = json.loads(str(exception_content))
            if j.get("error") == "Failed to get retrieval token":
                QMessageBox.critical(
                    self, "Bridge error",
                    str(j["error"]) + ". Please wait and try again.")
            else:
                QMessageBox.critical(self, "Bridge error", str(j["error"]))

        except:
            QMessageBox.critical(self, "Bridge error", str(exception_content))

    def update_download_task_state(self, row_position, state):
        self.ui_single_file_download.shard_queue_table.setItem(
            int(row_position), 3, QtGui.QTableWidgetItem(str(state)))

    def show_error_not_selected_file(self):
        QMessageBox.about(self, "Error",
                          "Please select destination file save path!")

    def show_error_invalid_file_path(self):
        QMessageBox.about(self, "Error",
                          "Destination file save path seems to be invalid!")

    def show_error_invalid_temporary_path(self):
        QMessageBox.about(self, "Error", "Temporary path seems to be invalid!")

    def refresh_overall_download_progress(self, base_percent):
        total_percent_to_download = self.all_shards_count * 100
        total_percent_downloaded = sum(self.shard_download_percent_list) * 100

        actual_percent_downloaded = total_percent_downloaded / total_percent_to_download

        total_percent = (base_percent * 100) + (0.90 *
                                                actual_percent_downloaded)

        logger.info(
            str(actual_percent_downloaded) + str(base_percent) +
            "total_percent_downloaded")

        # actual_upload_progressbar_value = self.ui_single_file_upload.overall_progress.value()

        self.ui_single_file_download.overall_progress.setValue(
            int(total_percent))

    def create_download_finish_thread(self, file_name):
        download_finish_thread = threading.Thread(
            target=self.finish_download(file_name=file_name), args=())
        download_finish_thread.start()

    #### Begin file download finish function ####
    # Wait for signal to do shards joining and encryption
    def finish_download(self, file_name):
        print "konczenie downloadu"
        fileisencrypted = False
        if "[DECRYPTED]" in self.filename_from_bridge:
            fileisencrypted = False
        else:
            fileisencrypted = True

            # Join shards
        sharing_tools = ShardingTools()
        self.emit(QtCore.SIGNAL("setCurrentState"), "Joining shards...")
        # logger.warning(str({"log_event_type": "debug", "title": "Sharding", "description": "Joining shards..."}))
        # logger.warning('"log_event_type": "debug"')
        #logger.debug('"title": "Sharding"')
        logger.debug('Joining shards...')

        if fileisencrypted:
            sharing_tools.join_shards(
                self.tmp_path + "/" + str(file_name), "-",
                self.destination_file_path + ".encrypted")
        else:
            sharing_tools.join_shards(self.tmp_path + "/" + str(file_name),
                                      "-", self.destination_file_path)

        logger.debug(self.tmp_path + "/" + str(file_name) + ".encrypted")

        if fileisencrypted:
            # decrypt file
            self.emit(QtCore.SIGNAL("setCurrentState"), "Decrypting file...")

            # logger.warning(str({"log_event_type": "debug", "title": "Decryption", "description": "Decrypting file..."}))
            # logger.warning('"log_event_type": "debug"')
            #logger.debug('"title": "Decryption"')
            logger.debug('Decrypting file...')

            file_crypto_tools = FileCrypto()
            file_crypto_tools.decrypt_file(
                "AES",
                str(self.destination_file_path) + ".encrypted",
                str(self.destination_file_path),
                str(self.user_password))  # begin file decryption

        logger.debug("pobrano")
        # logger.warning(str({"log_event_type": "success", "title": "Finished", "description": "Downloading completed successfully!"}))
        # logger.warning('"log_event_type": "success"')
        #logger.debug('"title": "Finished"')
        logger.debug('Downloading completed successfully!')
        self.emit(QtCore.SIGNAL("setCurrentState"),
                  "Downloading completed successfully!")
        self.is_upload_active = False
        self.emit(QtCore.SIGNAL("showFileDownloadedSuccessfully"))

        return True

    def request_and_download_next_set_of_pointers(self):
        print "nowe wskazniki"
        i = self.already_started_shard_downloads_count
        i2 = 1
        while i < self.all_shards_count and self.current_active_connections + i2 < 4:
            i2 += 1
            tries_get_file_pointers = 0
            while self.max_retries_get_file_pointers > tries_get_file_pointers:
                tries_get_file_pointers += 1
                try:
                    options_array = {}
                    options_array["tmp_path"] = self.tmp_path
                    options_array["progressbars_enabled"] = "1"
                    options_array["file_size_is_given"] = "1"
                    options_array["shards_count"] = str(self.all_shards_count)
                    shard_pointer = self.storj_engine.storj_client.file_pointers(
                        str(self.bucket_id),
                        self.file_id,
                        limit="1",
                        skip=str(i))
                    print shard_pointer[0]
                    options_array["shard_index"] = shard_pointer[0]["index"]

                    options_array["file_size_shard_" +
                                  str(i)] = shard_pointer[0]["size"]
                    self.emit(QtCore.SIGNAL("beginShardDownloadProccess"),
                              shard_pointer[0], self.destination_file_path,
                              options_array)
                except stjex.StorjBridgeApiError as e:
                    logger.debug('"title": "Bridge error"')
                    logger.debug('Error while resolving file pointers \
                                                         to download file with ID: '
                                 + str(self.file_id) + "...")
                    self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                              str(e))  # emit Storj Bridge Exception
                    continue
                except Exception:
                    continue
                else:
                    break

            self.already_started_shard_downloads_count += 1

            i += 1
        return 1

    def retry_download_with_new_pointer(self, shard_index):
        print "ponowienie"
        tries_get_file_pointers = 0
        while self.max_retries_get_file_pointers > tries_get_file_pointers:
            tries_get_file_pointers += 1
            time.sleep(1)
            try:
                options_array = {}
                options_array["tmp_path"] = self.tmp_path
                options_array["progressbars_enabled"] = "1"
                options_array["file_size_is_given"] = "1"
                options_array["shards_count"] = str(self.all_shards_count)
                shard_pointer = self.storj_engine.storj_client.file_pointers(
                    str(self.bucket_id),
                    self.file_id,
                    limit="1",
                    skip=str(shard_index))
                print shard_pointer[0]
                options_array["shard_index"] = shard_pointer[0]["index"]

                options_array["file_size_shard_" +
                              str(shard_index)] = shard_pointer[0]["size"]
                self.emit(QtCore.SIGNAL("beginShardDownloadProccess"),
                          shard_pointer[0], self.destination_file_path,
                          options_array)
            except stjex.StorjBridgeApiError as e:
                logger.debug('"title": "Bridge error"')
                logger.debug(
                    '"description": "Error while resolving file pointers \
                                                         to download file"')
                self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                          str(e))  # emit Storj Bridge Exception
                continue
            except Exception:
                continue
            else:
                break

        return 1

    def ask_overwrite(self, file_name):
        msgBox = QtGui.QMessageBox(
            QtGui.QMessageBox.Question, 'Question',
            'File %s already exist! Do you want to overwrite?' %
            str(file_name).decode('utf-8'),
            (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))

        self.overwrite_question_result = msgBox.exec_()
        self.overwrite_question_closed = True

    def download_begin(self, bucket_id, file_id):
        self.overwrite_question_closed = False
        self.validation = {}

        self.all_shards_count = self.get_file_pointers_count(
            bucket_id, file_id)
        self.shards_already_downloaded = 0

        self.destination_file_path = str(
            self.ui_single_file_download.file_save_path.text()).decode('utf-8')
        self.tmp_path = str(
            self.ui_single_file_download.tmp_dir.text()).decode('utf-8')

        if self.tmp_path == "":
            if platform == "linux" or platform == "linux2":
                # linux
                self.tmp_path = "/tmp/"
            elif platform == "darwin":
                # OS X
                self.tmp_path = "/tmp/"
            elif platform == "win32":
                # Windows
                self.tmp_path = "C:\\Windows\\temp\\"

        file_name = os.path.split(self.destination_file_path)[1]

        if self.destination_file_path == "":
            self.validation["file_path"] = False
            self.emit(QtCore.SIGNAL("showDestinationPathNotSelectedMsg")
                      )  # show error missing destination path
            logger.error("missing destination file path")
        else:
            self.validation["file_path"] = True

        if os.path.isfile(self.destination_file_path):

            self.emit(QtCore.SIGNAL("askFileOverwrite"), str(file_name))

            while not self.overwrite_question_closed:
                pass

            if self.overwrite_question_result == QtGui.QMessageBox.Yes:
                self.validation["file_path"] = True
            else:
                self.validation["file_path"] = False
                # emit signal to select new file path
                self.emit(QtCore.SIGNAL("selectFileDestinationPath"))

        if self.validation["file_path"]:
            self.ui_single_file_download.start_download_bt.setStyleSheet(
                ("QPushButton:hover{\n"
                 "  background-color: #8C8A87;\n"
                 "  border-color: #8C8A87;\n"
                 "}\n"
                 "QPushButton:active {\n"
                 "  background-color: #8C8A87;\n"
                 "  border-color: #8C8A87;\n"
                 "}\n"
                 "QPushButton{\n"
                 "  background-color: #8C8A87;\n"
                 "    border: 1px solid #8C8A87;\n"
                 "    color: #fff;\n"
                 "    border-radius: 7px;\n"
                 "}"))

            self.emit(QtCore.SIGNAL("updateShardCounters"))

            try:
                # logger.warning("log_event_type": "debug")
                # logger.debug('"title": "File pointers"')
                logger.debug('Resolving file pointers to download\
                                     file with ID: ' + str(file_id) + "...")
                # logger.warning(str({"log_event_type": "debug", "title": "File pointers",
                #                     "description": "Resolving file pointers to download file with ID: " + str(
                #                         file_id) + "..."}))
                # get_file_pointers_count(self, bucket_id, file_id)

                i = 0
                # while i < self.all_shards_count:
                while i < 4 and i < self.all_shards_count:
                    self.is_upload_active = True
                    tries_get_file_pointers = 0
                    while self.max_retries_get_file_pointers > tries_get_file_pointers:
                        tries_get_file_pointers += 1
                        time.sleep(1)
                        try:
                            options_array = {}
                            options_array["tmp_path"] = self.tmp_path
                            options_array["progressbars_enabled"] = "1"
                            options_array["file_size_is_given"] = "1"
                            options_array["shards_count"] = str(
                                self.all_shards_count)
                            shard_pointer = self.storj_engine.storj_client.file_pointers(
                                str(bucket_id),
                                file_id,
                                limit="1",
                                skip=str(i))
                            print str(shard_pointer) + "wskaznik"
                            # if shard_pointer[0]["parity"] == False:
                            #   print "Shard parity error!"
                            #  break
                            options_array["shard_index"] = shard_pointer[0][
                                "index"]

                            options_array["file_size_shard_" +
                                          str(i)] = shard_pointer[0]["size"]
                            self.emit(
                                QtCore.SIGNAL("beginShardDownloadProccess"),
                                shard_pointer[0], self.destination_file_path,
                                options_array)
                        except stjex.StorjBridgeApiError as e:
                            logger.debug('"title": "Bridge error"')
                            logger.debug(
                                '"description": "Error while resolving file pointers \
                                                             to download file with ID: "'
                                + str(file_id) + "...")
                            self.emit(
                                QtCore.SIGNAL("showStorjBridgeException"),
                                str(e))  # emit Storj Bridge Exception
                            continue
                        except Exception as e:
                            continue

                        else:
                            break

                    self.already_started_shard_downloads_count += 1

                    i += 1

            except storj.exception.StorjBridgeApiError as e:
                self.is_upload_active = False
                # logger.warning("log_event_type": "error")
                logger.debug('"title": "Bridge error"')
                logger.debug(
                    '"description": "Error while resolving file pointers \
                                     to download file with ID: "' +
                    str(file_id) + "...")
                # logger.warning(str({"log_event_type": "error", "title": "Bridge error",
                #                     "description": "Error while resolving file pointers to download file with ID: " + str(
                #                         file_id) + "..."}))
                self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                          str(e))  # emit Storj Bridge Exception
                # except Exception as e:
                # logger.warning('"log_event_type": "error"')
                #   logger.debug('"title": "Unhandled error"'),
                #  logger.debug('"description": "Unhandled error while resolving file\
                #             pointers to download file with ID: "' +
                #            str(file_id) + "...")
                # logger.warning(str({"log_event_type": "error", "title": "Unhandled error",
                #                     "description": "Unhandled error while resolving file pointers to download file with ID: " + str(
                #                         file_id) + "..."}))
                #  self.emit(QtCore.SIGNAL("showUnhandledException"), str(e))  # emit unhandled Exception
                #  logger.error(e)

            i = 0
            # model = QStandardItemModel(1, 1)  # initialize model for inserting to table

    def createNewDownloadInitThread(self, bucket_id, file_id):
        self.ui_single_file_download.overall_progress.setValue(0)
        self.initialize_download_queue_table()
        file_name_resolve_thread = threading.Thread(target=self.download_begin,
                                                    args=(bucket_id, file_id))
        file_name_resolve_thread.start()

    def createNewInitializationThread(self, bucket_id, file_id):
        file_name_resolve_thread = threading.Thread(
            target=self.set_file_metadata, args=(bucket_id, file_id))
        file_name_resolve_thread.start()

    def get_file_frame_id(self, bucket_id, file_id):
        try:
            file_metadata = self.storj_engine.storj_client.file_metadata(
                str(bucket_id), str(file_id))
            self.file_frame = file_metadata.frame

        except storj.exception.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                      "Error while resolving file frame ID. " +
                      str(e))  # emit Storj Bridge Exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledException"),
                      "Unhandled error while resolving file frame ID. " +
                      str(e))  # emit unhandled Exception
        else:
            return self.file_frame

    def set_file_metadata(self, bucket_id, file_id):
        try:
            self.emit(QtCore.SIGNAL("setCurrentState"),
                      "Getting file metadata...")
            file_metadata = self.storj_engine.storj_client.file_metadata(
                str(bucket_id), str(file_id))
            self.ui_single_file_download.file_name.setText(
                str(file_metadata.filename.replace("[DECRYPTED]",
                                                   "")).decode('utf-8'))

            tools = Tools()
            # self.ui_single_file_download.file_size.setText(
            #   html_format_begin + str(tools.human_size(int(file_metadata.size))) + html_format_end)
            self.ui_single_file_download.file_id.setText(str(file_id))

            if platform == "linux" or platform == "linux2":
                # linux
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "/" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))
            elif platform == "darwin":
                # OS X
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "/" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))
            elif platform == "win32":
                self.ui_single_file_download.file_save_path.setText(
                    str(tools.get_home_user_directory() + "\\" +
                        str(file_metadata.filename.replace("[DECRYPTED]", ""))
                        ).decode('utf-8'))

            self.filename_from_bridge = str(file_metadata.filename)

            self.resolved_file_metadata = True
            self.emit(QtCore.SIGNAL("setCurrentState"),
                      "Waiting for user action...")

        except storj.exception.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                      "Error while resolving file metadata. " +
                      str(e))  # emit Storj Bridge Exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledException"),
                      "Unhandled error while resolving file metadata. " +
                      str(e))  # emit unhandled Exception

    def update_shard_download_progess(self, row_position_index, value):
        self.download_queue_progressbar_list[row_position_index].setValue(
            value)
        return 1

    def increment_shards_download_progress_counters(self):
        # self.shards_already_downloaded += 1
        self.ui_single_file_download.downloaded_shards.setText(
            html_format_begin + str(self.shards_already_downloaded) +
            html_format_end)

    def set_current_status(self, current_status):
        self.ui_single_file_download.current_state.setText(str(current_status))

    def select_tmp_directory(self):
        self.selected_tmp_dir = QtGui.QFileDialog.getExistingDirectory(
            None, 'Select a folder:', '', QtGui.QFileDialog.ShowDirsOnly)
        self.ui_single_file_download.tmp_dir.setText(str(
            self.selected_tmp_dir))

    def get_file_pointers_count(self, bucket_id, file_id):
        file_frame = self.get_file_frame_id(bucket_id, file_id)
        frame_data = self.storj_engine.storj_client.frame_get(file_frame.id)
        return len(frame_data.shards)

    def select_file_save_path(self):
        file_save_path = QtGui.QFileDialog.getSaveFileName(
            self, 'Save file to...',
            str(self.ui_single_file_download.file_save_path.text()).decode(
                'utf-8'))
        self.ui_single_file_download.file_save_path.setText(
            str(file_save_path))

    def calculate_final_hmac(self):
        return 1

    def create_download_connection(self, url, path_to_save, options_chain,
                                   rowposition, shard_index):
        local_filename = str(path_to_save).decode('utf-8')
        downloaded = False
        farmer_tries = 0

        # logger.warning('"log_event_type": "debug"')
        #logger.debug('Downloading"')
        logger.debug('Downloading shard at index ' + str(shard_index) +
                     " from farmer: " + str(url))
        # logger.warning(str({"log_event_type": "debug", "title": "Downloading",
        #                    "description": "Downloading shard at index " + str(shard_index) + " from farmer: " + str(
        #                        url)}))

        tries_download_from_same_farmer = 0
        while self.max_retries_download_from_same_farmer > tries_download_from_same_farmer:
            tries_download_from_same_farmer += 1
            farmer_tries += 1
            try:
                self.current_active_connections += 1
                self.emit(QtCore.SIGNAL("updateDownloadTaskState"),
                          rowposition,
                          "Downloading...")  # update shard downloading state
                if options_chain["handle_progressbars"] != "1":
                    r = requests.get(url)
                    # requests.
                    with open(local_filename, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            if chunk:  # filter out keep-alive new chunks
                                f.write(chunk)
                else:
                    r = requests.get(url, stream=True)
                    f = open(local_filename, 'wb')
                    if options_chain["file_size_is_given"] == "1":
                        file_size = options_chain["shard_file_size"]
                    else:
                        file_size = int(r.headers['Content-Length'])

                    chunk = 1
                    num_bars = file_size / chunk
                    t1 = float(file_size) / float((32 * 1024))
                    logger.debug(t1)

                    if file_size <= (32 * 1024):
                        t1 = 1

                    i = 0
                    logger.debug(file_size)
                    logger.debug(str(t1) + "kotek")
                    for chunk in r.iter_content(32 * 1024):
                        i += 1
                        f.write(chunk)
                        # logger.debug(str(i) + " " + str(t1))
                        # logger.debug(round(float(i) / float(t1), 1))
                        # logger.debug(str(int(round((100.0 * i) / t1))) + " %")
                        if int(round((100.0 * i) / t1)) > 100:
                            percent_downloaded = 100
                        else:
                            percent_downloaded = int(round((100.0 * i) / t1))
                        self.emit(
                            QtCore.SIGNAL("updateShardDownloadProgress"),
                            int(rowposition), percent_downloaded
                        )  # update progress bar in upload queue table
                        self.shard_download_percent_list[
                            shard_index] = percent_downloaded
                        self.emit(
                            QtCore.SIGNAL("refreshOverallDownloadProgress"),
                            0.1)  # update progress bar in upload queue table
                        # logger.debug(str(rowposition) + "pozycja")
                        # logger.debug(str(rowposition) + "pozycja")
                        # progress_bar.setValue(percent_downloaded)

                    f.close()
                logger.debug(str(rowposition) + "rowposition started")
                print str(r.status_code) + "statushttp"
                if r.status_code != 200 and r.status_code != 304:
                    raise stjex.StorjFarmerError()
                downloaded = True

            except stjex.StorjFarmerError as e:
                self.emit(QtCore.SIGNAL("updateDownloadTaskState"),
                          rowposition, "First try failed. Retrying... (" +
                          str(farmer_tries) +
                          ")")  # update shard download state
                continue

            except Exception as e:
                logger.error(e)
                # logger.warning('"log_event_type": "warning"')
                logger.debug(
                    'Unhandled error while transfering data to farmer')
                logger.debug('Error occured while downloading\
                             shard at index ' + str(shard_index) +
                             ". Retrying... (" + str(farmer_tries) + ")")
                # logger.warning(str({"log_event_type": "warning", "title": "Unhandled error",
                #                     "description": "Error occured while downloading shard at index " + str(
                #                         shard_index) + ". Retrying... (" + str(farmer_tries) + ")"}))

                self.emit(QtCore.SIGNAL("updateDownloadTaskState"),
                          rowposition, "First try failed. Retrying... (" +
                          str(farmer_tries) +
                          ")")  # update shard download state
                continue
            else:
                downloaded = True
                break

        if not downloaded:
            self.current_active_connections -= 1
            self.emit(
                QtCore.SIGNAL("updateDownloadTaskState"), rowposition,
                "Error while downloading from this farmer. Getting another farmer pointer..."
            )  # update shard download state
            time.sleep(1)
            self.emit(QtCore.SIGNAL("retryWithNewDownloadPointer"),
                      shard_index)  # retry download with new download pointer

        else:
            self.emit(QtCore.SIGNAL("getNextSetOfPointers"))
            self.current_active_connections -= 1
            # logger.warning(str({"log_event_type": "success", "title": "Shard downloaded", "description": "Shard at index " + str(shard_index) + " downloaded successfully."}))
            # logger.warning('"log_event_type": "success"')
            logger.debug('Shard downloaded')
            logger.debug('Shard at index ' + str(shard_index) +
                         " downloaded successfully.")
            self.shards_already_downloaded += 1
            self.emit(QtCore.SIGNAL("incrementShardsDownloadProgressCounters")
                      )  # update already downloaded shards count
            self.emit(QtCore.SIGNAL("updateShardCounters")
                      )  # update already downloaded shards count
            self.emit(QtCore.SIGNAL("updateDownloadTaskState"), rowposition,
                      "Downloaded!")  # update shard download state
            if int(self.all_shards_count) <= int(
                    self.shards_already_downloaded):
                self.emit(
                    QtCore.SIGNAL("finishDownload")
                )  # send signal to begin file shards joind and decryption after all shards are downloaded
                # print "sygnal konca pobierania" + str(self.all_shards_count) + " " + str(self.shards_already_downloaded)

            return

    def createNewDownloadThread(self, url, filelocation, options_chain,
                                rowposition, shard_index):
        # self.download_thread = DownloadTaskQtThread(url, filelocation, options_chain, progress_bars_list)
        # self.download_thread.start()
        # self.download_thread.connect(self.download_thread, SIGNAL('setStatus'), self.test1, Qt.QueuedConnection)
        # self.download_thread.tick.connect(progress_bars_list.setValue)

        # Refactor to QtTrhead
        download_thread = threading.Thread(
            target=self.create_download_connection,
            args=(url, filelocation, options_chain, rowposition, shard_index))
        download_thread.start()
        logger.debug(str(options_chain["rowposition"]) + "position")

    def test1(self, value1, value2):
        logger.debug(str(value1) + " aaa " + str(value2))

    def shard_download(self, pointer, file_save_path, options_array):
        # logger.warning(str({"log_event_type": "debug", "title": "Downloading", "description": "Beginning download proccess..."}))
        # logger.warning('"log_event_type": "debug"')
        #logger.debug('"title": "Downloading"')
        logger.debug('Beginning download proccess...')
        options_chain = {}
        # self.storj_engine.storj_client.logger.info('file_pointers(%s, %s)', bucket_id, file_id)
        file_name = os.path.split(file_save_path)[1]

        ##### End file download finish point #####

        try:
            # check ability to write files to selected directories
            if self.tools.isWritable(os.path.split(file_save_path)[0]) != True:
                raise IOError("13")
            if self.tools.isWritable(self.tmp_path) != True:
                raise IOError("13")

            try:
                if options_array["progressbars_enabled"] == "1":
                    options_chain["handle_progressbars"] = "1"

                if options_array["file_size_is_given"] == "1":
                    options_chain["file_size_is_given"] = "1"

                shards_count = int(options_array["shards_count"])

                shard_size_array = []
                shard_size_array.append(
                    int(options_array["file_size_shard_" +
                                      str(options_array["shard_index"])]))
                logger.debug(shard_size_array)

                part = options_array["shard_index"]

                self.tmp_path = options_array["tmp_path"]

                self.emit(QtCore.SIGNAL("setCurrentState"),
                          "Starting download threads...")
                self.emit(
                    QtCore.SIGNAL("setCurrentState"),
                    "Started download shard at index " + str(part) + "...")

                options_chain["rowposition"] = part
                self.shard_download_percent_list.append(0)

                rowposition = self._add_shard_to_table(
                    pointer, 0, part)  # Add a row to the table

                logger.debug(pointer)
                options_chain["shard_file_size"] = shard_size_array[0]
                url = "http://" + pointer.get('farmer')['address'] + \
                      ":" + \
                      str(pointer.get('farmer')['port']) + \
                      "/shards/" + pointer["hash"] + \
                      "?token=" + pointer["token"]
                logger.debug(url)

                if self.combine_tmpdir_name_with_token:
                    self.createNewDownloadThread(
                        url, self.tmp_path + "/" + str(pointer["token"]) +
                        "/" + str(file_name) + "-" + str(part), options_chain,
                        rowposition - 1, part)
                else:
                    self.createNewDownloadThread(
                        url,
                        self.tmp_path + "/" + str(file_name) + "-" + str(part),
                        options_chain, rowposition - 1, part)

                logger.debug(self.tmp_path + "/" + str(file_name) + "-" +
                             str(part))
                part = part + 1

            except Exception as e:
                logger.error(e)
                # logger.warning('"log_event_type": "warning"')
                logger.debug('Unhandled error' + str(e))
                # except Exception as e:
                #   self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                #            "Unhandled error while resolving file pointers for download. " + str(
                #               e))  # emit unhandled Exception

        except IOError as e:
            print " perm error " + str(e)
            if str(e) == str(13):
                self.emit(QtCore.SIGNAL(
                    "showException"
                ), "Error while saving or reading file or temporary file. Probably this is caused by insufficient permisions. Please check if you have permissions to write or read from selected directories.  "
                          )  # emit Storj Bridge Exception

        except Exception as e:
            logger.error(e)
            # logger.warning('"log_event_type": "warning"')
            logger.debug('Unhandled error')
示例#21
0
class SingleFileUploadUI(QtGui.QMainWindow):
    __logger = logging.getLogger('%s.SingleFileUploadUI' % __name__)

    def __init__(self,
                 parent=None,
                 bucketid=None,
                 fileid=None,
                 dashboard_instance=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_upload = Ui_SingleFileUpload()
        self.ui_single_file_upload.setupUi(self)
        # open bucket manager
        QtCore.QObject.connect(self.ui_single_file_upload.start_upload_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.createNewUploadThread)
        # open file select dialog
        QtCore.QObject.connect(self.ui_single_file_upload.file_path_select_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.select_file_path)
        # open tmp directory select dialog
        QtCore.QObject.connect(self.ui_single_file_upload.tmp_path_select_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.select_tmp_directory)

        # handle cancel action
        QtCore.QObject.connect(self.ui_single_file_upload.cancel_bt,
                               QtCore.SIGNAL('clicked()'),
                               self.handle_cancel_action)

        self.already_used_farmers_nodes = []

        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

        self.ui_single_file_upload.uploaded_shards.setText("Waiting...")

        self.is_upload_active = False
        self.current_active_connections = 0

        self.ui_single_file_upload.connections_onetime.setValue(
            int(MAX_UPLOAD_CONNECTIONS_AT_SAME_TIME)
        )  # user can set it manually default value from constants file

        if platform == 'linux' or platform == 'linux2':
            # linux
            self.temp_dir = '/tmp'
        elif platform == 'darwin':
            # OS X
            self.temp_dir = '/tmp'
        elif platform == 'win32':
            # Windows
            self.temp_dir = 'C:\\Windows\\temp\\'
        self.ui_single_file_upload.tmp_path.setText(self.temp_dir)

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.connect(self, QtCore.SIGNAL('addRowToUploadQueueTable'),
                     self.add_row_upload_queue_table)

        self.connect(self, QtCore.SIGNAL('incrementShardsProgressCounters'),
                     self.increment_shards_progress_counters)
        self.connect(self, QtCore.SIGNAL('updateUploadTaskState'),
                     self.update_upload_task_state)
        self.connect(self, QtCore.SIGNAL('updateShardUploadProgress'),
                     self.update_shard_upload_progess)
        self.connect(self, QtCore.SIGNAL('showFileNotSelectedError'),
                     self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL('showInvalidPathError'),
                     self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL('showInvalidTemporaryPathError'),
                     self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL('refreshOverallProgress'),
                     self.refresh_overall_progress)
        self.connect(self, QtCore.SIGNAL('showFileUploadedSuccessfully'),
                     self.show_upload_finished_message)
        self.connect(
            self, QtCore.SIGNAL('finishUpload'), lambda: self.finish_upload(
                os.path.split(str(self.ui_single_file_upload.file_path.text()))
                [1], str(self.current_selected_bucket_id)))
        self.connect(self, QtCore.SIGNAL('setCurrentUploadState'),
                     self.set_current_status)
        self.connect(self, QtCore.SIGNAL('updateShardUploadCounters'),
                     self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL('setCurrentActiveConnections'),
                     self.set_current_active_connections)
        self.connect(self, QtCore.SIGNAL('setShardSize'), self.set_shard_size)
        self.connect(self, QtCore.SIGNAL('createShardUploadThread'),
                     self.createNewShardUploadThread)
        # self.connect(self, QtCore.SIGNAL('handleCancelAction'), self.ha)

        # resolve buckets and put to buckets combobox
        self.createBucketResolveThread()
        self.ui_single_file_upload.files_list_view_bt.mousePressEvent = self.display_files_queue_change

        self.is_files_queue_table_visible = False

        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("incrementShardsProgressCounters"))

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

        self.prepare_files_queue_table()

        self.clip = QtGui.QApplication.clipboard()

        self.ui_single_file_upload.connections_onetime.setMaximum(
            MAX_ALLOWED_UPLOAD_CONCURRENCY)

        if DATA_TABLE_EDIT_ENABLED == False:
            self.ui_single_file_upload.shard_queue_table_widget.setEditTriggers(
                QtGui.QAbstractItemView.NoEditTriggers)

        self.current_row = 0

    def keyPressEvent(self, e):
        # copy upload queue table content to clipboard #
        if (e.modifiers() & QtCore.Qt.ControlModifier):
            selected = self.ui_single_file_upload.shard_queue_table_widget.selectedRanges(
            )

            if e.key() == QtCore.Qt.Key_C:  # copy
                s = ""

                for r in xrange(selected[0].topRow(),
                                selected[0].bottomRow() + 1):
                    for c in xrange(selected[0].leftColumn(),
                                    selected[0].rightColumn() + 1):
                        try:
                            s += str(
                                self.ui_single_file_upload.
                                shard_queue_table_widget.item(r,
                                                              c).text()) + "\t"
                        except AttributeError:
                            s += "\t"
                    s = s[:-1] + "\n"  # eliminate last '\t'
                self.clip.setText(s)

    def shardUploadInitThread(self, shard, chapters, frame, file_name):
        shard_upload_init_thread = threading.Thread(
            target=self.createNewShardUploadThread(shard=shard,
                                                   chapters=chapters,
                                                   frame=frame,
                                                   file_name=file_name),
            args=())
        shard_upload_init_thread.start()

    def display_files_queue_change(self, x):
        self.animation = QtCore.QPropertyAnimation(self, "size")
        # self.animation.setDuration(1000) #Default 250ms

        if self.is_files_queue_table_visible:
            self.animation.setEndValue(QtCore.QSize(980, 590))
            self.is_files_queue_table_visible = False
            self.ui_single_file_upload.files_list_view_bt.setPixmap(
                QtGui.QPixmap(":/resources/rarrow.png"))
        else:
            self.animation.setEndValue(QtCore.QSize(1371, 590))
            self.is_files_queue_table_visible = True
            self.ui_single_file_upload.files_list_view_bt.setPixmap(
                QtGui.QPixmap(":/resources/larrow.jpg"))

        self.animation.start()

    def hide_files_queue(self):

        return True

    def prepare_files_queue_table(self):
        self.files_queue_table_header = [
            'File name', 'Path', 'Size', 'Progress'
        ]
        self.ui_single_file_upload.files_queue_table_widget.setColumnCount(4)
        self.ui_single_file_upload.files_queue_table_widget.setRowCount(0)
        horHeaders = self.files_queue_table_header
        self.ui_single_file_upload.files_queue_table_widget.setHorizontalHeaderLabels(
            horHeaders)
        self.ui_single_file_upload.files_queue_table_widget.resizeColumnsToContents(
        )
        self.ui_single_file_upload.files_queue_table_widget.resizeRowsToContents(
        )
        self.ui_single_file_upload.files_queue_table_widget.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

    def set_shard_size(self, shard_size):
        self.ui_single_file_upload.shardsize.setText(
            str(self.tools.human_size(int(shard_size))))

    def handle_cancel_action(self):
        if self.is_upload_active:
            msgBox = QtGui.QMessageBox(
                QtGui.QMessageBox.Question, "Question",
                "Are you sure that you want cancel upload and close this window?",
                (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Yes:
                self.close()
        else:
            self.close()

    def show_upload_finished_message(self):
        self.is_upload_active = False
        self.ui_single_file_upload.connections_onetime.setEnabled(True)
        self.ui_single_file_upload.start_upload_bt.setStyleSheet(
            ("QPushButton:hover{\n"
             "  background-color: #83bf20;\n"
             "  border-color: #83bf20;\n"
             "}\n"
             "QPushButton:active {\n"
             "  background-color: #93cc36;\n"
             "  border-color: #93cc36;\n"
             "}\n"
             "QPushButton{\n"
             "  background-color: #88c425;\n"
             "    border: 1px solid #88c425;\n"
             "    color: #fff;\n"
             "    border-radius: 7px;\n"
             "}"))

        self.ui_single_file_upload.start_upload_bt.setEnabled(True)
        self.ui_single_file_upload.file_path.setText("")
        QMessageBox.information(self, 'Success!',
                                'File uploaded successfully!')

    def refresh_overall_progress(self, base_percent):
        """
        """
        total_percent_to_upload = self.all_shards_count * 100
        total_percent_uploaded = sum(self.shard_upload_percent_list) * 100
        actual_percent_uploaded = total_percent_uploaded / total_percent_to_upload
        total_percent = (base_percent * 100) + (0.90 * actual_percent_uploaded)

        if int(total_percent) >= 100:
            self.ui_single_file_upload.overall_progress.setValue(int(99))
        else:
            self.ui_single_file_upload.overall_progress.setValue(
                int(total_percent))

    def set_current_active_connections(self):
        self.ui_single_file_upload.current_active_connections.setText(
            str(self.current_active_connections))

    def update_shards_counters(self):
        self.ui_single_file_upload.uploaded_shards.setText(
            str(self.shards_already_uploaded) + "/" +
            str(self.all_shards_count))

    def update_shard_upload_progess(self, row_position_index, value):
        self.upload_queue_progressbar_list[row_position_index].setValue(value)
        return 1

    def update_upload_task_state(self, row_position, state):
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            int(row_position), 3, QtGui.QTableWidgetItem(str(state)))

    def show_error_not_selected_file(self):
        QMessageBox.about(self, 'Error',
                          'Please select file which you want to upload!')

    def show_error_invalid_file_path(self):
        QMessageBox.about(self, 'Error', 'File path seems to be invalid!')

    def show_error_invalid_temporary_path(self):
        QMessageBox.about(self, 'Error', 'Temporary path seems to be invalid!')

    def createBucketResolveThread(self):
        bucket_resolve_thread = threading.Thread(
            target=self.initialize_buckets_select_list, args=())
        bucket_resolve_thread.start()

    def initialize_buckets_select_list(self):
        """Get all the buckets in which it is possible to store files, and
        show the names in the dropdown list"""
        self.__logger.debug('Buckets')
        self.__logger.debug(
            'Resolving buckets from Bridge to buckets combobox...')

        self.buckets_list = []
        self.bucket_id_list = []
        self.bucket_id_name_2D_list = []
        self.storj_engine = StorjEngine()
        try:
            for bucket in self.storj_engine.storj_client.bucket_list():
                self.bucket_id_name_2D_list.append([
                    str(bucket.id),
                    str(bucket.name).decode('utf8')
                ])  # append buckets to list

            if BUCKETS_LIST_SORTING_ENABLED:
                self.bucket_id_name_2D_list = sorted(
                    self.bucket_id_name_2D_list,
                    key=lambda x: x[1],
                    reverse=False)

            for arr_data in self.bucket_id_name_2D_list:
                self.buckets_list.append(arr_data[1])
                self.bucket_id_list.append(arr_data[0])
        except storj.exception.StorjBridgeApiError as e:
            self.__logger.error(e)
            QMessageBox.about(self, 'Unhandled bucket resolving exception',
                              'Exception: %s' % e)

        self.ui_single_file_upload.save_to_bucket_select.addItems(
            self.buckets_list)

        if APPLY_SELECTED_BUCKET_TO_UPLOADER:
            self.ui_single_file_upload.save_to_bucket_select.setCurrentIndex(
                int(self.dashboard_instance.current_bucket_index))

    def increment_shards_progress_counters(self):
        # self.shards_already_uploaded += 1
        # self.ui_single_file_upload.shards_uploaded.setText(
        #   html_format_begin + str(self.shards_already_uploaded) + html_format_end)
        return 1

    def add_row_upload_queue_table(self, row_data):
        self.upload_queue_progressbar_list.append(QtGui.QProgressBar())

        self.upload_queue_table_row_count = self.ui_single_file_upload.shard_queue_table_widget.rowCount(
        )

        self.ui_single_file_upload.shard_queue_table_widget.setRowCount(
            self.upload_queue_table_row_count + 1)
        self.ui_single_file_upload.shard_queue_table_widget.setCellWidget(
            self.upload_queue_table_row_count, 0,
            self.upload_queue_progressbar_list[
                self.upload_queue_table_row_count])
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 1,
            QtGui.QTableWidgetItem(row_data['hash']))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 2,
            QtGui.QTableWidgetItem(
                '%s:%d' %
                (row_data['farmer_address'], row_data['farmer_port'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 3,
            QtGui.QTableWidgetItem(str(row_data['state'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 4,
            QtGui.QTableWidgetItem(str(row_data['token'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 5,
            QtGui.QTableWidgetItem(str(row_data['shard_index'])))

        if AUTO_SCROLL_UPLOAD_DOWNLOAD_QUEUE:
            self.ui_single_file_upload.shard_queue_table_widget.scrollToBottom(
            )

        self.upload_queue_progressbar_list[
            self.upload_queue_table_row_count].setValue(0)

        self.__logger.info(row_data)

    def select_tmp_directory(self):
        self.selected_tmp_dir = QtGui.QFileDialog.getExistingDirectory(
            None, 'Select a folder:', self.temp_dir,
            QtGui.QFileDialog.ShowDirsOnly)
        self.__logger.debug('Chosen temp dir: %s', self.selected_tmp_dir)
        self.ui_single_file_upload.tmp_path.setText(
            str(self.selected_tmp_dir).decode('utf-8'))

    def select_file_path(self):
        self.ui_single_file_upload.file_path.setText(
            str(QtGui.QFileDialog.getOpenFileName()).decode('utf-8'))

    def createNewUploadThread(self):
        # self.download_thread = DownloadTaskQtThread(url, filelocation, options_chain, progress_bars_list)
        # self.download_thread.start()
        # self.download_thread.connect(self.download_thread, QtCore.SIGNAL('setStatus'), self.test1, Qt.QueuedConnection)
        # self.download_thread.tick.connect(progress_bars_list.setValue)
        # Refactor to QtTrhead

        #upload_thread = multiprocessing.Process(target=self.file_upload_begin, args=())
        upload_thread = threading.Thread(target=self.file_upload_begin,
                                         args=())
        upload_thread.start()

    def initialize_upload_queue_table(self):

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.upload_queue_table_header = [
            'Progress', 'Hash', 'Farmer', 'State', 'Token', 'Shard index'
        ]
        self.ui_single_file_upload.shard_queue_table_widget.setColumnCount(6)
        self.ui_single_file_upload.shard_queue_table_widget.setRowCount(0)
        horHeaders = self.upload_queue_table_header
        self.ui_single_file_upload.shard_queue_table_widget.setHorizontalHeaderLabels(
            horHeaders)
        self.ui_single_file_upload.shard_queue_table_widget.resizeColumnsToContents(
        )
        self.ui_single_file_upload.shard_queue_table_widget.resizeRowsToContents(
        )

        self.ui_single_file_upload.shard_queue_table_widget.horizontalHeader(
        ).setResizeMode(QtGui.QHeaderView.Stretch)

    def set_current_status(self, current_status):
        self.ui_single_file_upload.current_state.setText(html_format_begin +
                                                         current_status +
                                                         html_format_end)

    def createNewShardUploadThread(self, shard, chapters, frame, file_name):
        # another worker thread for single shard uploading and it will retry if download fail

        #pool = multiprocessing.Pool()

        print "starting thread for shard"
        # upload_thread = multiprocessing.Process(
        # upload_thread = pool.apply_async(
        upload_thread = threading.Thread(self.upload_shard(
            shard=shard,
            chapters=chapters,
            frame=frame,
            file_name_ready_to_shard_upload=file_name),
                                         args=())
        #pool.close()
        #pool.join()
        upload_thread.start()
        print "zakonczono"

    def _add_shard_to_table(self, frame_content, shard, chapters):
        """
        Add a row to the shard table and return the row number
        """
        # Add items to shard queue table view
        tablerowdata = {}
        tablerowdata['farmer_address'] = frame_content['farmer']['address']
        tablerowdata['farmer_port'] = frame_content['farmer']['port']
        tablerowdata['hash'] = str(shard.hash)
        tablerowdata['state'] = 'Uploading...'
        tablerowdata['token'] = frame_content['token']
        tablerowdata['shard_index'] = str(chapters)

        # self.__logger.warning('"log_event_type": "debug"')
        self.__logger.debug('"title": "Contract negotiated"')
        self.__logger.debug('"description": "Storage contract negotiated \
                     with: "' + str(frame_content["farmer"]["address"]) + ":" +
                            str(frame_content["farmer"]["port"]))

        # add row to table
        self.emit(QtCore.SIGNAL('addRowToUploadQueueTable'), tablerowdata)

        rowcount = self.ui_single_file_upload.shard_queue_table_widget.rowCount(
        )
        return rowcount

    def _read_in_chunks(self,
                        file_object,
                        shard_size,
                        rowposition,
                        blocksize=1024,
                        chunks=-1,
                        shard_index=None):
        """Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1k."""
        # chunk number (first is 0)
        i = 0
        while chunks:
            data = file_object.read(blocksize)
            if not data:
                break
            yield data
            i += 1
            t1 = float(shard_size) / float(blocksize)
            if shard_size <= blocksize:
                t1 = 1

            percent_uploaded = int(round((100.0 * i) / t1))

            # self.__logger.debug(i)
            chunks -= 1

            # update progress bar in upload queue table
            self.emit(QtCore.SIGNAL("updateShardUploadProgress"),
                      int(rowposition), percent_uploaded)
            self.shard_upload_percent_list[shard_index] = percent_uploaded
            self.emit(QtCore.SIGNAL("refreshOverallProgress"),
                      0.1)  # update overall progress bar

    def upload_shard(self, shard, chapters, frame,
                     file_name_ready_to_shard_upload):

        self.semaphore.acquire()

        contract_negotiation_tries = 0

        print self.already_used_farmers_nodes

        while MAX_RETRIES_NEGOTIATE_CONTRACT > contract_negotiation_tries:
            contract_negotiation_tries += 1
            exchange_report = storj.model.ExchangeReport()

            # emit signal to add row to upload queue table
            # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")

            self.__logger.debug('Negotiating contract')
            self.__logger.debug('Trying to negotiate storage contract for \
shard at index %s' % chapters)
            if contract_negotiation_tries > 1:
                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Trying to negotiate storage contract for shard at index %s... Retry %s... '
                    % (str(chapters), contract_negotiation_tries))
            else:
                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Trying to negotiate storage contract for shard at index %s...'
                    % str(chapters))

            try:
                if FARMER_NODES_EXCLUSION_FOR_UPLOAD_ENABLED:
                    frame_content = self.storj_engine.storj_client.frame_add_shard(
                        shard,
                        frame.id,
                        excludes=self.already_used_farmers_nodes)
                else:
                    frame_content = self.storj_engine.storj_client.frame_add_shard(
                        shard, frame.id)
                # Add a row to the table
                rowposition = self._add_shard_to_table(frame_content, shard,
                                                       chapters)

                rowposition = self.current_row
                self.current_row += 1

                self.__logger.debug('-' * 30)
                self.__logger.debug(frame_content['farmer']['address'])

                farmerNodeID = frame_content['farmer']['nodeID']

                if BLACKLISTING_MODE == 1:
                    self.already_used_farmers_nodes.append(
                        farmerNodeID
                    )  # add item to array of already used farmers nodes

                url = 'http://' + frame_content['farmer']['address'] + ':' + \
                      str(frame_content['farmer']['port']) + '/shards/' + \
                      frame_content['hash'] + '?token=' + \
                      frame_content['token']
                self.__logger.debug('URL: %s', url)

                self.__logger.debug('-' * 30)

                # files = {'file': open(file_path + '.part%s' % chapters)}
                # headers = {'content-type: application/octet-stream', 'x-storj-node-id: ' + str(farmerNodeID)}

                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Uploading shard %s to farmer...' % str(chapters + 1))

                self.emit(
                    QtCore.SIGNAL("setCurrentUploadState"),
                    'Uploading shard %s to farmer...' % str(chapters + 1))

                # begin recording exchange report

                current_timestamp = int(time.time())

                exchange_report.exchangeStart = str(current_timestamp)
                exchange_report.farmerId = str(farmerNodeID)
                exchange_report.dataHash = str(shard.hash)

                shard_size = int(shard.size)

                farmer_tries = 0
                response = None
                success_shard_upload = False

                while MAX_RETRIES_UPLOAD_TO_SAME_FARMER > farmer_tries:
                    farmer_tries += 1
                    try:
                        self.__logger.debug(
                            'Upload shard at index ' + str(shard.index) +
                            ' to ' + str(frame_content['farmer']['address']) +
                            ':' + str(frame_content['farmer']['port']))

                        mypath = os.path.join(
                            self.tmp_path, file_name_ready_to_shard_upload +
                            '-' + str(chapters + 1))

                        self.current_active_connections += 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))
                        with open(mypath, 'rb') as f:
                            response = requests.post(url,
                                                     data=self._read_in_chunks(
                                                         f,
                                                         shard_size,
                                                         rowposition,
                                                         shard_index=chapters),
                                                     timeout=1)

                        j = json.loads(str(response.content))
                        if j.get('result'
                                 ) == 'The supplied token is not accepted':
                            raise storj.exception.StorjFarmerError(
                                storj.exception.SuppliedTokenNotAcceptedError)

                        if response.status_code != 200 and response.status_code != 304:
                            raise storj.exception.StorjFarmerError(
                                77)  # Raise general farmer failure

                        success_shard_upload = True

                    except storj.exception.StorjFarmerError as e:
                        print str(e)
                        self.__logger.error(e)
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))

                        # upload failed due to Farmer Failure
                        if e.code == 10002:
                            self.__logger.error(
                                'The supplied token not accepted')
                        continue

                    except Exception as e:
                        print str(e)
                        self.__logger.error(e)
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))

                        # update shard upload state
                        self.emit(
                            QtCore.SIGNAL('updateUploadTaskState'),
                            rowposition, 'First try failed. Retrying... (' +
                            str(farmer_tries) + ')')

                        self.emit(
                            QtCore.SIGNAL("setCurrentUploadState"),
                            'First try failed. Retrying... (' +
                            str(farmer_tries) + ')')

                        self.__logger.warning('Shard upload error')
                        self.__logger.warning(
                            'Error while uploading shard to:\
                                %s:%s. Retrying... (%s)' %
                            (frame_content["farmer"]["address"],
                             frame_content["farmer"]["port"], farmer_tries))
                        continue

                    else:
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))
                        self.emit(
                            # update already uploaded shards count
                            QtCore.SIGNAL('incrementShardsProgressCounters'))
                        self.shards_already_uploaded += 1

                        # update already uploaded shards count
                        self.emit(QtCore.SIGNAL('updateShardUploadCounters'))
                        self.__logger.debug(
                            'Shard uploaded successfully to %s:%s' %
                            (frame_content["farmer"]["address"],
                             frame_content["farmer"]["port"]))

                        self.emit(QtCore.SIGNAL("updateUploadTaskState"),
                                  rowposition,
                                  "Uploaded!")  # update shard upload state

                        self.__logger.debug('%s shards, %s sent' %
                                            (self.all_shards_count,
                                             self.shards_already_uploaded))

                        if int(self.all_shards_count) <= int(
                                self.shards_already_uploaded):
                            # send signal to save to bucket after all files are uploaded
                            self.emit(QtCore.SIGNAL('finishUpload'))
                        break

                if not success_shard_upload:
                    if BLACKLISTING_MODE == 2:
                        self.already_used_farmers_nodes.append(
                            farmerNodeID
                        )  # Add item to array of already used farmers nodes
                    # Update shard upload state
                    self.emit(QtCore.SIGNAL('updateUploadTaskState'),
                              rowposition,
                              'Failed. Trying to upload to another farmer...')

                self.__logger.debug(response.content)

                j = json.loads(str(response.content))
                if j.get('result') == 'The supplied token is not accepted':
                    farmer_error_class = storj.exception.FarmerError(10002)
                    SUPPLIED_TOKEN_NOT_ACCEPTED_ERROR = farmer_error_class.SUPPLIED_TOKEN_NOT_ACCEPTED
                    raise storj.exception.StorjFarmerError(
                        storj.exception.SuppliedTokenNotAcceptedError)

            except storj.exception.StorjBridgeApiError as e:
                print str(e)
                self.__logger.error(e)

                # upload failed due to Storj Bridge failure
                self.__logger.error(
                    'Exception raised while trying to negotiate contract')
                self.__logger.error('Bridge exception')
                self.__logger.error('Exception raised while trying \
to negotiate storage contract for shard at index %s' % chapters)
                continue

            except Exception as e:
                print str(e)
                self.__logger.error(e)

                # now send Exchange Report
                # upload failed probably while sending data to farmer
                self.__logger.error(
                    'Error occured while trying to upload shard or negotiate contract. Retrying... '
                )

                self.__logger.error('Unhandled exception')
                self.__logger.error('Unhandled exception occured while trying \
to upload shard or negotiate contract for shard at index %s. Retrying...' %
                                    str(chapters))
                current_timestamp = int(time.time())

                exchange_report.exchangeEnd = str(current_timestamp)
                exchange_report.exchangeResultCode = exchange_report.FAILURE
                exchange_report.exchangeResultMessage = exchange_report.STORJ_REPORT_UPLOAD_ERROR

                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Sending Exchange Report for shard %s' % str(chapters + 1))
                # self.storj_engine.storj_client.send_exchange_report(exchange_report) # send exchange report
                continue

            # uploaded with success
            current_timestamp = int(time.time())
            # prepare second half of exchange heport
            exchange_report.exchangeEnd = str(current_timestamp)
            exchange_report.exchangeResultCode = exchange_report.SUCCESS
            exchange_report.exchangeResultMessage = exchange_report.STORJ_REPORT_SHARD_UPLOADED
            self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                      "Sending Exchange Report for shard " + str(chapters + 1))
            self.__logger.info("Shard " + str(chapters + 1) +
                               " successfully added and exchange report sent.")
            # self.storj_engine.storj_client.send_exchange_report(exchange_report) # send exchange report
            # Release the semaphore when the download is finished
            self.semaphore.release()
            break

    def finish_upload(self, bname, bucket_id):
        self.crypto_tools = CryptoTools()
        self.__logger.debug('HMAC')
        self.__logger.debug('Generating HMAC...')
        hash_sha512_hmac_b64 = self.crypto_tools.prepare_bucket_entry_hmac(
            self.shard_manager_result.shards)
        hash_sha512_hmac = hashlib.sha224(str(
            hash_sha512_hmac_b64["SHA-512"])).hexdigest()
        self.__logger.debug(hash_sha512_hmac)
        # save

        # import magic
        # mime = magic.Magic(mime=True)
        # mime.from_file(file_path)

        self.__logger.debug(self.frame.id)
        self.__logger.debug("Now upload file")

        data = {
            'x-token': self.push_token.id,
            'x-filesize': str(self.uploaded_file_size),
            'frame': self.frame.id,
            'mimetype': self.file_mime_type,
            'filename': str(bname) + str(self.fileisdecrypted_str),
            'hmac': {
                'type': "sha512",
                # 'value': hash_sha512_hmac["sha512_checksum"]
                'value': hash_sha512_hmac
            },
        }

        self.__logger.debug('Finishing upload')
        self.__logger.debug('Adding file %s to bucket...' % str(bname))
        self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                  "Adding file to bucket...")

        success = False
        try:
            response = self.storj_engine.storj_client._request(
                method='POST',
                path='/buckets/%s/files' % bucket_id,
                # files={'file' : file},
                headers={
                    'x-token': self.push_token.id,
                    'x-filesize': str(self.uploaded_file_size),
                },
                json=data,
            )
            success = True
        except storj.exception.StorjBridgeApiError as e:
            QMessageBox.about(self, "Unhandled bridge exception",
                              "Exception: " + str(e))
        if success:
            self.__logger.debug('"title": "File uploaded"')
            self.__logger.debug('"description": "File uploaded successfully!"')
            self.emit(QtCore.SIGNAL("showFileUploadedSuccessfully"))
            self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                      "File uploaded successfully!")
            self.dashboard_instance.createNewFileListUpdateThread()

    def file_upload_begin(self):

        self.semaphore = threading.BoundedSemaphore(
            int(self.ui_single_file_upload.connections_onetime.value()))

        self.ui_single_file_upload.overall_progress.setValue(0)

        file_path = None
        self.validation = {}

        self.initialize_upload_queue_table()

        encryption_enabled = True

        # get temporary files path
        self.tmp_path = str(self.ui_single_file_upload.tmp_path.text())
        self.__logger.debug('Temporary path chosen: %s' % self.tmp_path)

        self.configuration = Configuration()

        # TODO: redundant lines?
        # get temporary files path
        if self.ui_single_file_upload.file_path.text() == "":
            self.tmp_path = "/tmp"
            self.validation["file_path"] = False
            self.emit(QtCore.SIGNAL(
                "showFileNotSelectedError"))  # show error missing file path
            self.__logger.error("temporary path missing")
        else:
            self.tmp_path = str(self.ui_single_file_upload.tmp_path.text())
            self.validation["file_path"] = True
            file_path = str(
                self.ui_single_file_upload.file_path.text()).decode('utf-8')

        if self.validation["file_path"]:

            self.current_bucket_index = self.ui_single_file_upload.save_to_bucket_select.currentIndex(
            )
            self.current_selected_bucket_id = self.bucket_id_list[
                self.current_bucket_index]
            bucket_id = str(self.current_selected_bucket_id)

            bname = os.path.split(file_path)[1]  # File name

            self.__logger.debug(bname + "npliku")

            # Temporary replace magic with mimetypes python library
            if mimetypes.guess_type(file_path)[0] is not None:
                file_mime_type = mimetypes.guess_type(file_path)[0]
            else:
                file_mime_type = "text/plain"

            file_mime_type = "text/plain"

            # mime = magic.Magic(mime=True)
            # file_mime_type = str(mime.from_file(file_path))

            self.__logger.debug(file_mime_type)
            # file_mime_type = str("A")

            file_existence_in_bucket = False

            # if self.configuration.sameFileNamePrompt or self.configuration.sameFileHashPrompt:
            # file_existence_in_bucket =
            # self.storj_engine.storj_client.check_file_existence_in_bucket(bucket_id=bucket_id,
            # filepath=file_path) # check if exist file with same file name

            if file_existence_in_bucket == 1:
                # QInputDialog.getText(self, 'Warning!', 'File with name ' + str(bname) + " already exist in bucket! Please use different name:", "test" )
                self.__logger.warning("Same file exist!")

            self.fileisdecrypted_str = ""
            if self.ui_single_file_upload.encrypt_files_checkbox.isChecked():
                # encrypt file
                self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                          "Encrypting file...")
                # self.__logger.warning('"log_event_type": "debug"')
                self.__logger.debug('"title": "Encryption"')
                self.__logger.debug('"description": "Encrypting file..."')

                file_crypto_tools = FileCrypto()
                # Path where to save the encrypted file in temp dir
                file_path_ready = os.path.join(self.tmp_path,
                                               '%s.encrypted' % bname)
                self.__logger.debug('Call encryption method')
                # begin file encryption
                file_crypto_tools.encrypt_file(
                    "AES", file_path, file_path_ready,
                    self.storj_engine.account_manager.get_user_password())
                file_name_ready_to_shard_upload = bname + ".encrypted"
                self.fileisdecrypted_str = ""
            else:
                self.fileisdecrypted_str = "[DECRYPTED]"
                file_path_ready = file_path
                file_name_ready_to_shard_upload = bname

            self.__logger.debug('Temp path: %s' % self.tmp_path)
            self.__logger.debug(file_path_ready + "sciezka2")

            def get_size(file_like_object):
                return os.stat(file_like_object.name).st_size

            # file_size = get_size(file)

            file_size = os.stat(file_path).st_size
            self.uploaded_file_size = file_size
            self.file_mime_type = file_mime_type

            self.ui_single_file_upload.file_size.setText(
                str(self.tools.human_size(int(file_size))))

            self.is_upload_active = True
            self.ui_single_file_upload.connections_onetime.setEnabled(False)
            self.ui_single_file_upload.start_upload_bt.setDisabled(True)
            self.ui_single_file_upload.start_upload_bt.setStyleSheet(
                ("QPushButton:hover{\n"
                 "  background-color: #8C8A87;\n"
                 "  border-color: #8C8A87;\n"
                 "}\n"
                 "QPushButton:active {\n"
                 "  background-color: #8C8A87;\n"
                 "  border-color: #8C8A87;\n"
                 "}\n"
                 "QPushButton{\n"
                 "  background-color: #8C8A87;\n"
                 "    border: 1px solid #8C8A87;\n"
                 "    color: #fff;\n"
                 "    border-radius: 7px;\n"
                 "}"))

            self.__logger.debug('PUSH token')
            self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                      "Resolving PUSH Token for upload...")
            self.__logger.debug('Resolving PUSH Token for upload...')

            push_token = None
            PUSH_token_resolve_retries = 0

            while PUSH_token_resolve_retries < MAX_RETRIES_TOKEN_RESOLVING:
                try:
                    PUSH_token_resolve_retries += 1
                    # Get the PUSH token from Storj Bridge
                    push_token = self.storj_engine.storj_client.token_create(
                        bucket_id, 'PUSH')
                    self.push_token = push_token
                except storj.exception.StorjBridgeApiError as e:
                    self.is_upload_active = False
                    QMessageBox.about(self,
                                      "Unhandled PUSH token create exception",
                                      "Exception: " + str(e))
                else:
                    break

            self.ui_single_file_upload.push_token.setText(str(
                push_token.id))  # set the PUSH Token

            self.__logger.debug("PUSH Token ID: " + push_token.id)

            self.__logger.debug('Frame')
            self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                      "RResolving frame for file upload...")
            self.__logger.debug('Resolving frame for file upload...')

            frame = None  # initialize variable
            try:
                frame = self.storj_engine.storj_client.frame_create(
                )  # Create file frame
                self.frame = frame
            except storj.exception.StorjBridgeApiError as e:
                self.is_upload_active = False
                QMessageBox.about(
                    self,
                    'Unhandled exception while creating file staging frame',
                    'Exception: %s' % e)
                self.__logger.debug('"title": "Frame"')
                self.__logger.debug(
                    '"description": "Error while resolving frame for\
                    file upload..."')

            self.ui_single_file_upload.file_frame_id.setText(str(frame.id))

            self.__logger.debug('Frame ID: %s', frame.id)
            # Now encrypt file
            self.__logger.debug('%s sciezka', file_path_ready)

            # Now generate shards
            self.emit(QtCore.SIGNAL('setCurrentUploadState'),
                      'Splitting file to shards...')
            self.__logger.debug('Sharding')
            self.__logger.debug('Splitting file to shards...')

            max_shard_size_setting = self.configuration.max_shard_size()
            print str(max_shard_size_setting) + " max shard size"
            shards_manager = storj.model.ShardManager(
                filepath=file_path_ready,
                tmp_path=self.tmp_path,
                max_shard_size=int(max_shard_size_setting))
            self.all_shards_count = len(shards_manager.shards)
            self.emit(QtCore.SIGNAL("updateShardUploadCounters"))

            self.shard_manager_result = shards_manager
            # self.ui_single_file_upload.current_state.setText(
            #   html_format_begin + "Generating shards..." + html_format_end)
            # shards_manager._make_shards()
            #shards_count = shards_manager.num_chunks # fix because new version of sdk
            shards_count = self.all_shards_count
            # create file hash
            self.__logger.debug('file_upload() push_token=%s', push_token)

            # upload shards to frame
            self.__logger.debug('Shards count %s' % shards_count)

            # set shards count
            self.all_shards_count = shards_count

            chapters = 0

            for shard in shards_manager.shards:
                self.emit(QtCore.SIGNAL("setShardSize"), int(shard.size))

                self.shard_upload_percent_list.append(0)
                #self.emit(QtCore.SIGNAL("createShardUploadThread"), shard, chapters, frame, file_name_ready_to_shard_upload)
                #self.emit(QtCore.SIGNAL("_createShardUploadThread"), shard, chapters, frame, file_name_ready_to_shard_upload)
                #self.createNewShardUploadThread(shard, chapters, frame, file_name_ready_to_shard_upload)
                #self.createNewShardUploadThread(shard, chapters, frame, file_name_ready_to_shard_upload)
                #print "wysylanie sharda..." + str(shard.index)
                #chapters += 1
                #time.sleep(1)

            threads = [
                threading.Thread(target=self.upload_shard,
                                 args=(shard, int(shard.index), frame,
                                       file_name_ready_to_shard_upload))
                for shard in shards_manager.shards
            ]
            self.current_line = 0
            for t in threads:
                #self.shards_already_uploaded += 1
                #row_lock.acquire()
                print "starting thread..."
                t.start()
                self.current_line += 1
                #row_lock.release()
                time.sleep(CONTRACT_NEGOTIATION_ITERATION_DELAY)

            for t in threads:
                t.join()
示例#22
0
class SingleFileUploadUI(QtGui.QMainWindow):
    __logger = logging.getLogger('%s.SingleFileUploadUI' % __name__)

    def __init__(self, parent=None, bucketid=None, fileid=None, dashboard_instance=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_upload = Ui_SingleFileUpload()
        self.ui_single_file_upload.setupUi(self)
        # open bucket manager
        QtCore.QObject.connect(
            self.ui_single_file_upload.start_upload_bt,
            QtCore.SIGNAL('clicked()'),
            self.createNewUploadThread)
        # open file select dialog
        QtCore.QObject.connect(
            self.ui_single_file_upload.file_path_select_bt,
            QtCore.SIGNAL('clicked()'),
            self.select_file_path)
        # open tmp directory select dialog
        QtCore.QObject.connect(
            self.ui_single_file_upload.tmp_path_select_bt,
            QtCore.SIGNAL('clicked()'),
            self.select_tmp_directory)

        # handle cancel action
        QtCore.QObject.connect(
            self.ui_single_file_upload.cancel_bt,
            QtCore.SIGNAL('clicked()'),
            self.handle_cancel_action)


        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

        self.ui_single_file_upload.uploaded_shards.setText("Waiting...")

        self.is_upload_active = False
        self.current_active_connections = 0

        if platform == 'linux' or platform == 'linux2':
            # linux
            self.temp_dir = '/tmp'
        elif platform == 'darwin':
            # OS X
            self.temp_dir = '/tmp'
        elif platform == 'win32':
            # Windows
            self.temp_dir = 'C:\\Windows\\temp\\'
        self.ui_single_file_upload.tmp_path.setText(self.temp_dir)

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.connect(self, QtCore.SIGNAL('addRowToUploadQueueTable'), self.add_row_upload_queue_table)

        self.connect(self, QtCore.SIGNAL('incrementShardsProgressCounters'), self.increment_shards_progress_counters)
        self.connect(self, QtCore.SIGNAL('updateUploadTaskState'), self.update_upload_task_state)
        self.connect(self, QtCore.SIGNAL('updateShardUploadProgress'), self.update_shard_upload_progess)
        self.connect(self, QtCore.SIGNAL('showFileNotSelectedError'), self.show_error_not_selected_file)
        self.connect(self, QtCore.SIGNAL('showInvalidPathError'), self.show_error_invalid_file_path)
        self.connect(self, QtCore.SIGNAL('showInvalidTemporaryPathError'), self.show_error_invalid_temporary_path)
        self.connect(self, QtCore.SIGNAL('refreshOverallProgress'), self.refresh_overall_progress)
        self.connect(self, QtCore.SIGNAL('showFileUploadedSuccessfully'), self.show_upload_finished_message)
        self.connect(self, QtCore.SIGNAL('finishUpload'),
            lambda: self.finish_upload(os.path.split(
                str(self.ui_single_file_upload.file_path.text()))[1],
                str(self.current_selected_bucket_id)))
        self.connect(self, QtCore.SIGNAL('setCurrentUploadState'), self.set_current_status)
        self.connect(self, QtCore.SIGNAL('updateShardUploadCounters'), self.update_shards_counters)
        self.connect(self, QtCore.SIGNAL('setCurrentActiveConnections'), self.set_current_active_connections)
        self.connect(self, QtCore.SIGNAL('setShardSize'), self.set_shard_size)
        #self.connect(self, QtCore.SIGNAL('handleCancelAction'), self.ha)

        # resolve buckets and put to buckets combobox
        self.createBucketResolveThread()

        # file_pointers = self.storj_engine.storj_client.file_pointers(
        #   "6acfcdc62499144929cf9b4a", "dfba26ab34466b1211c60d02")

        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")
        # self.emit(QtCore.SIGNAL("incrementShardsProgressCounters"))

        self.max_retries_upload_to_same_farmer = MAX_RETRIES_UPLOAD_TO_SAME_FARMER
        self.max_retries_negotiate_contract = MAX_RETRIES_NEGOTIATE_CONTRACT

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

    def set_shard_size(self, shard_size):
        self.ui_single_file_upload.shardsize.setText(str(self.tools.human_size(int(shard_size))))


    def handle_cancel_action(self):
        if self.is_upload_active:
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Question, "Question",
                                       "Are you sure that you want cancel upload and close this window?",
                                       (QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Yes:
                self.close()
        else:
            self.close()


    def show_upload_finished_message(self):
        self.is_upload_active = False
        self.ui_single_file_upload.start_upload_bt.setStyleSheet(("QPushButton:hover{\n"
                                                                  "  background-color: #83bf20;\n"
                                                                  "  border-color: #83bf20;\n"
                                                                  "}\n"
                                                                  "QPushButton:active {\n"
                                                                  "  background-color: #93cc36;\n"
                                                                  "  border-color: #93cc36;\n"
                                                                  "}\n"
                                                                  "QPushButton{\n"
                                                                  "  background-color: #88c425;\n"
                                                                  "    border: 1px solid #88c425;\n"
                                                                  "    color: #fff;\n"
                                                                  "    border-radius: 7px;\n"
                                                                  "}"))
        self.ui_single_file_upload.file_path.setText("")
        QMessageBox.information(self, 'Success!', 'File uploaded successfully!')

    def refresh_overall_progress(self, base_percent):
        """
        """
        total_percent_to_upload = self.all_shards_count * 100
        total_percent_uploaded = sum(self.shard_upload_percent_list) * 100

        actual_percent_uploaded = total_percent_uploaded / total_percent_to_upload

        total_percent = (base_percent * 100) + (0.90 * actual_percent_uploaded)

        self.__logger.info('%s %s total_percent_uploaded' % (actual_percent_uploaded, base_percent))

        # actual_upload_progressbar_value = self.ui_single_file_upload.overall_progress.value()

        self.ui_single_file_upload.overall_progress.setValue(int(total_percent))

    def set_current_active_connections(self):
        self.ui_single_file_upload.current_active_connections.setText(str(self.current_active_connections))

    def update_shards_counters(self):
        self.ui_single_file_upload.uploaded_shards.setText(str(self.shards_already_uploaded) + "/" + str(self.all_shards_count))


    def update_shard_upload_progess(self, row_position_index, value):
        self.upload_queue_progressbar_list[row_position_index].setValue(value)
        return 1

    def update_upload_task_state(self, row_position, state):
        self.ui_single_file_upload.shard_queue_table_widget.setItem(int(row_position), 3,
                                                                    QtGui.QTableWidgetItem(str(state)))

    def show_error_not_selected_file(self):
        QMessageBox.about(self, 'Error', 'Please select file which you want to upload!')

    def show_error_invalid_file_path(self):
        QMessageBox.about(self, 'Error', 'File path seems to be invalid!')

    def show_error_invalid_temporary_path(self):
        QMessageBox.about(self, 'Error', 'Temporary path seems to be invalid!')

    def createBucketResolveThread(self):
        bucket_resolve_thread = threading.Thread(target=self.initialize_buckets_select_list, args=())
        bucket_resolve_thread.start()

    def initialize_buckets_select_list(self):
        """Get all the buckets in which it is possible to store files, and
        show the names in the dropdown list"""
        # self.__logger.warning(str({
        #   "log_event_type": "info",
        #   "title": "Buckets",
        #   "description": "Resolving buckets from Bridge to buckets combobox..."}))
        # self.__logger.warning('"log_event_type": "info"')
        self.__logger.debug('"title": "Buckets"')
        self.__logger.debug('"description": "Resolving buckets from Bridge to buckets combobox..."')

        self.buckets_list = []
        self.bucket_id_list = []
        self.storj_engine = StorjEngine()
        try:
            for bucket in self.storj_engine.storj_client.bucket_list():
                self.__logger.debug('Found bucket: %s', bucket.name)
                # append buckets to list
                self.buckets_list.append(str(bucket.name).decode('utf8'))
                self.bucket_id_list.append(bucket.id)
        except storj.exception.StorjBridgeApiError as e:
            self.__logger.error(e)
            QMessageBox.about(
                self,
                'Unhandled bucket resolving exception',
                'Exception: %s' % e)

        self.ui_single_file_upload.save_to_bucket_select.addItems(self.buckets_list)

        if APPLY_SELECTED_BUCKET_TO_UPLOADER:
            self.ui_single_file_upload.save_to_bucket_select.setCurrentIndex(int(self.dashboard_instance.current_bucket_index))


    def increment_shards_progress_counters(self):
        # self.shards_already_uploaded += 1
        # self.ui_single_file_upload.shards_uploaded.setText(
        #   html_format_begin + str(self.shards_already_uploaded) + html_format_end)
        return 1

    def add_row_upload_queue_table(self, row_data):
        self.upload_queue_progressbar_list.append(QtGui.QProgressBar())

        self.upload_queue_table_row_count = self.ui_single_file_upload.shard_queue_table_widget.rowCount()

        self.ui_single_file_upload.shard_queue_table_widget.setRowCount(
            self.upload_queue_table_row_count + 1)
        self.ui_single_file_upload.shard_queue_table_widget.setCellWidget(
            self.upload_queue_table_row_count, 0, self.upload_queue_progressbar_list[self.upload_queue_table_row_count])
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 1, QtGui.QTableWidgetItem(row_data['hash']))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 2, QtGui.QTableWidgetItem(
                '%s:%d' % (row_data['farmer_address'], row_data['farmer_port'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 3, QtGui.QTableWidgetItem(
                str(row_data['state'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 4, QtGui.QTableWidgetItem(
                str(row_data['token'])))
        self.ui_single_file_upload.shard_queue_table_widget.setItem(
            self.upload_queue_table_row_count, 5, QtGui.QTableWidgetItem(
                str(row_data['shard_index'])))

        self.upload_queue_progressbar_list[self.upload_queue_table_row_count].setValue(0)

        self.__logger.info(row_data)

    def select_tmp_directory(self):
        self.selected_tmp_dir = QtGui.QFileDialog.getExistingDirectory(
            None,
            'Select a folder:',
            self.temp_dir,
            QtGui.QFileDialog.ShowDirsOnly)
        self.__logger.debug('Chosen temp dir: %s', self.selected_tmp_dir)
        self.ui_single_file_upload.tmp_path.setText(str(self.selected_tmp_dir).decode('utf-8'))

    def select_file_path(self):
        self.ui_single_file_upload.file_path.setText(str(QtGui.QFileDialog.getOpenFileName()).decode('utf-8'))

    def createNewUploadThread(self):
        # self.download_thread = DownloadTaskQtThread(url, filelocation, options_chain, progress_bars_list)
        # self.download_thread.start()
        # self.download_thread.connect(self.download_thread, QtCore.SIGNAL('setStatus'), self.test1, Qt.QueuedConnection)
        # self.download_thread.tick.connect(progress_bars_list.setValue)

        # Refactor to QtTrhead

        upload_thread = threading.Thread(target=self.file_upload_begin, args=())
        upload_thread.start()

    def initialize_upload_queue_table(self):

        # initialize variables
        self.shards_already_uploaded = 0
        self.uploaded_shards_count = 0
        self.upload_queue_progressbar_list = []

        self.upload_queue_table_header = ['Progress', 'Hash', 'Farmer', 'State', 'Token', 'Shard index']
        self.ui_single_file_upload.shard_queue_table_widget.setColumnCount(6)
        self.ui_single_file_upload.shard_queue_table_widget.setRowCount(0)
        horHeaders = self.upload_queue_table_header
        self.ui_single_file_upload.shard_queue_table_widget.setHorizontalHeaderLabels(horHeaders)
        self.ui_single_file_upload.shard_queue_table_widget.resizeColumnsToContents()
        self.ui_single_file_upload.shard_queue_table_widget.resizeRowsToContents()

        self.ui_single_file_upload.shard_queue_table_widget.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

    def set_current_status(self, current_status):
        self.ui_single_file_upload.current_state.setText(html_format_begin + current_status + html_format_end)

    def createNewShardUploadThread(self, shard, chapters, frame, file_name):
        # another worker thread for single shard uploading and it will retry if download fail
        upload_thread = threading.Thread(
            target=self.upload_shard(
                shard=shard,
                chapters=chapters,
                frame=frame,
                file_name_ready_to_shard_upload=file_name
            ), args=())
        upload_thread.start()

    def _add_shard_to_table(self, frame_content, shard, chapters):
        """
        Add a row to the shard table and return the row number
        """
        # Add items to shard queue table view
        tablerowdata = {}
        tablerowdata['farmer_address'] = frame_content['farmer']['address']
        tablerowdata['farmer_port'] = frame_content['farmer']['port']
        tablerowdata['hash'] = str(shard.hash)
        tablerowdata['state'] = 'Uploading...'
        tablerowdata['token'] = frame_content['token']
        tablerowdata['shard_index'] = str(chapters)

        # self.__logger.warning('"log_event_type": "debug"')
        self.__logger.debug('"title": "Contract negotiated"')
        self.__logger.debug('"description": "Storage contract negotiated \
                     with: "' +
                            str(frame_content["farmer"]["address"]) + ":" +
                            str(frame_content["farmer"]["port"]))
        # self.__logger.warning(str({"log_event_type": "debug", "title": "Contract negotiated",
        #   "description": "Storage contract negotiated with: " +
        #   str(frame_content["farmer"]["address"] + ":" + str(frame_content["farmer"]["port"]))}))

        # add row to table
        self.emit(QtCore.SIGNAL('addRowToUploadQueueTable'), tablerowdata)

        rowcount = self.ui_single_file_upload.shard_queue_table_widget.rowCount()
        return rowcount

    def _read_in_chunks(self, file_object, shard_size, rowposition, blocksize=1024, chunks=-1, shard_index=None):
        """Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1k."""
        # chunk number (first is 0)
        i = 0
        while chunks:
            data = file_object.read(blocksize)
            if not data:
                break
            yield data
            i += 1
            t1 = float(shard_size) / float(blocksize)
            if shard_size <= blocksize:
                t1 = 1

            percent_uploaded = int(round((100.0 * i) / t1))

            self.__logger.debug(i)
            chunks -= 1

            # update progress bar in upload queue table
            self.emit(QtCore.SIGNAL("updateShardUploadProgress"), int(rowposition), percent_uploaded)
            self.shard_upload_percent_list[shard_index] = percent_uploaded
            self.emit(QtCore.SIGNAL("refreshOverallProgress"), 0.1)  # update overall progress bar

    def upload_shard(self, shard, chapters, frame, file_name_ready_to_shard_upload):

        contract_negotiation_tries = 0

        while MAX_RETRIES_NEGOTIATE_CONTRACT > contract_negotiation_tries:
            contract_negotiation_tries += 1
            exchange_report = storj.model.ExchangeReport()

            # emit signal to add row to upload queue table
            # self.emit(QtCore.SIGNAL("addRowToUploadQueueTable"), "important", "information")

            # self.ui_single_file_upload.current_state.setText(
            #   html_format_begin + "Adding shard " + str(chapters) +
            #   " to file frame and getting contract..." + html_format_end)

            # self.__logger.warning('"log_event_type": "debug"')
            self.__logger.debug('"title": "Negotiating contract"')
            self.__logger.debug('"description": "Trying to negotiate storage \
                    contract for shard at index %s"' % chapters)
            if contract_negotiation_tries > 1:
                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Trying to negotiate storage contract for shard at index %s... Retry %s... ' % (
                        str(chapters), contract_negotiation_tries))
            else:
                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Trying to negotiate storage contract for shard at index %s...' % str(chapters))
            # self.__logger.warning(str({
            #   "log_event_type": "debug",
            #   "title": "Negotiating contract",
            #   "description": "Trying to negotiate storage contract for shard at index " + str(chapters) + "..."}))

            try:
                frame_content = self.storj_engine.storj_client.frame_add_shard(shard, frame.id)
                # Add a row to the table
                rowposition = self._add_shard_to_table(
                    frame_content,
                    shard,
                    chapters)

                self.__logger.debug('-' * 30)
                # self.__logger.debug("FRAME CONTENT: " + str(frame_content))
                # self.__logger.debug("SHARD: " + str(shard))
                self.__logger.debug(frame_content['farmer']['address'])

                farmerNodeID = frame_content['farmer']['nodeID']

                url = 'http://' + frame_content['farmer']['address'] + ':' + \
                      str(frame_content['farmer']['port']) + '/shards/' + \
                      frame_content['hash'] + '?token=' + \
                      frame_content['token']
                self.__logger.debug('URL: %s', url)

                self.__logger.debug('-' * 30)

                # files = {'file': open(file_path + '.part%s' % chapters)}
                # headers = {'content-type: application/octet-stream', 'x-storj-node-id: ' + str(farmerNodeID)}

                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Uploading shard %s to farmer...' % str(chapters + 1))

                # begin recording exchange report

                current_timestamp = int(time.time())

                exchange_report.exchangeStart = str(current_timestamp)
                exchange_report.farmerId = str(farmerNodeID)
                exchange_report.dataHash = str(shard.hash)

                shard_size = int(shard.size)

                farmer_tries = 0
                response = None
                success_shard_upload = False
                while MAX_RETRIES_UPLOAD_TO_SAME_FARMER > farmer_tries:
                    farmer_tries += 1
                    try:
                        self.__logger.debug(
                            'Upload shard at index ' +
                            str(shard.index) + ' to ' +
                            str(frame_content['farmer']['address']) +
                            ':' +
                            str(frame_content['farmer']['port']))

                        # self.__logger.warning(str({
                        #   "log_event_type": "debug",
                        #   "title": "Uploading shard",
                        #   "description": "Uploading shard at index " + str(shard.index) + " to " + str(
                        #       frame_content["farmer"]["address"] + ":" + str(frame_content["farmer"][
                        #       "port"]))}))

                        mypath = os.path.join(self.parametrs.tmpPath,
                                              file_name_ready_to_shard_upload +
                                              '-' + str(chapters + 1))

                        # with open(
                        #   self.parametrs.tmpPath + file_name_ready_to_shard_upload + '-' +
                        #   str(chapters + 1), 'rb') as f:
                        self.current_active_connections += 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))
                        with open(mypath, 'rb') as f:
                            response = requests.post(
                                url,
                                data=self._read_in_chunks(
                                    f,
                                    shard_size,
                                    rowposition,
                                    shard_index=chapters
                                ),
                                timeout=1)

                        j = json.loads(str(response.content))
                        if j.get('result') == 'The supplied token is not accepted':
                            raise storj.exception.StorjFarmerError(
                                storj.exception.StorjFarmerError.SUPPLIED_TOKEN_NOT_ACCEPTED)
                        success_shard_upload = True

                    except storj.exception.StorjFarmerError as e:
                        self.__logger.error(e)
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))

                        # upload failed due to Farmer Failure
                        if str(e) == str(storj.exception.StorjFarmerError.SUPPLIED_TOKEN_NOT_ACCEPTED):
                            self.__logger.error('The supplied token not accepted')
                        # print "Exception raised while trying to negitiate contract: " + str(e)
                        continue

                    except Exception as e:
                        self.__logger.error(e)
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))

                        # update shard upload state
                        self.emit(
                            QtCore.SIGNAL('updateUploadTaskState'),
                            rowposition,
                            'First try failed. Retrying... (' + str(farmer_tries) + ')')

                        # self.__logger.warning('"log_event_type": "warning"')
                        self.__logger.warning('"title": "Shard upload error"')
                        self.__logger.warning(
                            '"description": "Error while uploading \
                                       shard to: "' +
                            str(frame_content["farmer"]["address"]) +
                            ":" +
                            str(frame_content["farmer"]["port"]) +
                            " Retrying... (" + str(farmer_tries) +
                            ")")
                        # self.__logger.warning(str({"log_event_type": "warning", "title": "Shard upload error",
                        #   "description": "Error while uploading shard to: " + str(
                        #   frame_content["farmer"]["address"] + ":" + str(frame_content["farmer"][
                        #   "port"])) + " Retrying... (" + str(farmer_tries) + ")"}))
                        continue

                    else:
                        self.current_active_connections -= 1
                        self.emit(QtCore.SIGNAL('setCurrentActiveConnections'))
                        self.emit(
                            # update already uploaded shards count
                            QtCore.SIGNAL('incrementShardsProgressCounters'))
                        self.shards_already_uploaded += 1

                        # update already uploaded shards count
                        self.emit(QtCore.SIGNAL('updateShardUploadCounters'))
                        # logger.warning('"log_event_type": "success"')
                        self.__logger.debug("Shard uploaded successfully to " +
                                     str(frame_content["farmer"]["address"]) +
                                     ":" +
                                     str(frame_content["farmer"]["port"]))
                        # logger.warning(str({"log_event_type": "success", "title": "Uploading shard",
                        #                     "description": "Shard uploaded successfully to " + str(
                        #   frame_content["farmer"]["address"] + ":" + str(frame_content["farmer"][
                        #                             "port"]))}))

                        self.emit(QtCore.SIGNAL("updateUploadTaskState"), rowposition,
                                  "Uploaded!")  # update shard upload state

                        self.__logger.debug(str(self.all_shards_count) + " shards, " +
                                     str(self.shards_already_uploaded) + "sent")

                        if int(self.all_shards_count) <= int(self.shards_already_uploaded):
                            # send signal to save to bucket after all files are uploaded
                            self.emit(
                                QtCore.SIGNAL('finishUpload'))
                        break

                if not success_shard_upload:
                    # update shard upload state
                    self.emit(
                        QtCore.SIGNAL('updateUploadTaskState'),
                        rowposition,
                        'Failed. Trying to upload to another farmer...')

                self.__logger.debug(response.content)

                j = json.loads(str(response.content))
                if j.get('result') == 'The supplied token is not accepted':
                    raise storj.exception.StorjFarmerError(
                        storj.exception.StorjFarmerError.SUPPLIED_TOKEN_NOT_ACCEPTED)

            except storj.exception.StorjBridgeApiError as e:
                self.__logger.error(e)

                # upload failed due to Storj Bridge failure
                self.__logger.error('Exception raised while trying to negitiate contract')
                # self.__logger.warning('"log_event_type": "error"')
                self.__logger.error('"title": "Bridge exception"')
                self.__logger.error('"description": "Exception raised while trying \
                               to negotiate storage contract for shard at index\
                               "' + str(chapters))
                # self.__logger.warning(str({"log_event_type": "error", "title": "Bridge exception",
                #   "description":
                #   "Exception raised while trying to negitiate storage contract for shard at index " + str(
                #   chapters)}))
                continue

            except Exception as e:
                self.__logger.error(e)

                # now send Exchange Report
                # upload failed probably while sending data to farmer
                self.__logger.error('Error occured while trying to upload shard or negotiate contract. Retrying... ')

                # self.__logger.warning('"log_event_type": "error"')
                self.__logger.error('"title": "Unhandled exception"')
                self.__logger.error('"description": "Unhandled exception occured\
                             while trying to upload shard or negotiate \
                             contract for shard at index "' +
                                    str(chapters) +
                                    " . Retrying...")
                # self.__logger.warning(str({"log_event_type": "error", "title": "Unhandled exception",
                #   "description":
                #   "Unhandled exception occured while trying to upload shard or
                #   negotiate contract for shard at index " + str(chapters) + " . Retrying..."}))
                current_timestamp = int(time.time())

                exchange_report.exchangeEnd = str(current_timestamp)
                exchange_report.exchangeResultCode = exchange_report.FAILURE
                exchange_report.exchangeResultMessage = exchange_report.STORJ_REPORT_UPLOAD_ERROR

                self.emit(
                    QtCore.SIGNAL('setCurrentUploadState'),
                    'Sending Exchange Report for shard %s' % str(chapters + 1))
                # self.storj_engine.storj_client.send_exchange_report(exchange_report) # send exchange report
                continue

            # uploaded with success
            current_timestamp = int(time.time())
            # prepare second half of exchange heport
            exchange_report.exchangeEnd = str(current_timestamp)
            exchange_report.exchangeResultCode = exchange_report.SUCCESS
            exchange_report.exchangeResultMessage = exchange_report.STORJ_REPORT_SHARD_UPLOADED
            self.emit(QtCore.SIGNAL("setCurrentUploadState"),
                      "Sending Exchange Report for shard " + str(chapters + 1))
            # self.__logger.warning('"log_event_type": "debug"')
            self.__logger.info("Shard " + str(chapters + 1) +
                               " successfully added and exchange report sent.")
            # self.__logger.warning(str({"log_event_type": "debug", "title": "Shard added",
            #                     "description": "Shard " + str(chapters + 1) + " successfully added and exchange report sent."}))
            # self.storj_engine.storj_client.send_exchange_report(exchange_report) # send exchange report
            break

    def finish_upload(self, bname, bucket_id):
        self.crypto_tools = CryptoTools()
        # self.ui_single_file_upload.current_state.setText(
        #   html_format_begin + "Generating SHA5212 HMAC..." + html_format_end)
        # self.__logger.warning('"log_event_type": "debug"')
        self.__logger.debug('"title": "HMAC"')
        self.__logger.debug('"description": "Generating HMAC..."')
        # self.__logger.warning(str({"log_event_type": "debug", "title": "HMAC",
        #                     "description": "Generating HMAC..."}))
        hash_sha512_hmac_b64 = self.crypto_tools.prepare_bucket_entry_hmac(self.shard_manager_result.shards)
        hash_sha512_hmac = hashlib.sha224(str(hash_sha512_hmac_b64["SHA-512"])).hexdigest()
        self.__logger.debug(hash_sha512_hmac)
        # save

        # import magic
        # mime = magic.Magic(mime=True)
        # mime.from_file(file_path)

        self.__logger.debug(self.frame.id)
        self.__logger.debug("Now upload file")

        data = {
            'x-token': self.push_token.id,
            'x-filesize': str(self.uploaded_file_size),
            'frame': self.frame.id,
            'mimetype': self.file_mime_type,
            'filename': str(bname) + str(self.fileisdecrypted_str),
            'hmac': {
                'type': "sha512",
                # 'value': hash_sha512_hmac["sha512_checksum"]
                'value': hash_sha512_hmac
            },
        }
        # self.ui_single_file_upload.current_state.setText(
        #   html_format_begin + "Adding file to bucket..." + html_format_end)

        # self.__logger.warning('"log_event_type": "debug"')
        self.__logger.debug('"title": "Finishing upload"')
        self.__logger.debug('"description": "Adding file "' +
                            str(bname) + " to bucket...")
        self.emit(QtCore.SIGNAL("setCurrentUploadState"), "Adding file to bucket...")

        # self.__logger.warning(str({"log_event_type": "debug", "title": "Finishing upload",
        #                     "description": "Adding file " + str(bname) + " to bucket..."}))

        success = False
        try:
            response = self.storj_engine.storj_client._request(
                method='POST', path='/buckets/%s/files' % bucket_id,
                # files={'file' : file},
                headers={
                    'x-token': self.push_token.id,
                    'x-filesize': str(self.uploaded_file_size),
                },
                json=data,
            )
            success = True
        except storj.exception.StorjBridgeApiError as e:
            QMessageBox.about(self, "Unhandled bridge exception", "Exception: " + str(e))
        if success:
            # self.ui_single_file_upload.current_state.setText(
            #   html_format_begin + "Upload success! Waiting for user..." + html_format_end)
            # self.__logger.warning('"log_event_type": "success"')
            self.__logger.debug('"title": "File uploaded"')
            self.__logger.debug('"description": "File uploaded successfully!"')
            # self.__logger.warning(str({"log_event_type": "success", "title": "File uploaded",
            #                     "description": "File uploaded successfully!"}))
            self.emit(QtCore.SIGNAL("showFileUploadedSuccessfully"))
            self.emit(QtCore.SIGNAL("setCurrentUploadState"), "File uploaded successfully!")
            self.dashboard_instance.createNewFileListUpdateThread()

    def file_upload_begin(self):
        self.ui_single_file_upload.overall_progress.setValue(0)
        # upload finish function #

        # end upload finishing function #

        file_path = None
        self.validation = {}

        self.initialize_upload_queue_table()

        # item = ProgressWidgetItem()
        # self.ui_single_file_upload.shard_queue_table_widget.setItem(1, 1, item)
        # item.updateValue(1)

        # progress.valueChanged.connect(item.updateValue)

        encryption_enabled = True
        self.parametrs = storj.model.StorjParametrs()

        # get temporary files path
        self.parametrs.tmpPath = str(self.ui_single_file_upload.tmp_path.text())
        self.__logger.debug("Temporary path chosen: " + self.parametrs.tmpPath)

        self.configuration = Configuration()

        # TODO: redundant lines?
        # get temporary files path
        if self.ui_single_file_upload.file_path.text() == "":
            self.parametrs.tmpPath = "/tmp/"
            self.validation["file_path"] = False
            self.emit(QtCore.SIGNAL("showFileNotSelectedError"))  # show error missing file path
            self.__logger.error("temporary path missing")
        else:
            self.parametrs.tmpPath = str(self.ui_single_file_upload.tmp_path.text())
            self.validation["file_path"] = True
            file_path = str(self.ui_single_file_upload.file_path.text()).decode('utf-8')

        if self.validation["file_path"]:

            self.current_bucket_index = self.ui_single_file_upload.save_to_bucket_select.currentIndex()
            self.current_selected_bucket_id = self.bucket_id_list[self.current_bucket_index]
            bucket_id = str(self.current_selected_bucket_id)

            bname = os.path.split(file_path)[1]  # File name

            self.__logger.debug(bname + "npliku")

            # Temporary replace magic with mimetypes python library
            if mimetypes.guess_type(file_path)[0] is not None:
                file_mime_type = mimetypes.guess_type(file_path)[0]
            else:
                file_mime_type = "text/plain"

            file_mime_type = "text/plain"

            # mime = magic.Magic(mime=True)
            # file_mime_type = str(mime.from_file(file_path))

            self.__logger.debug(file_mime_type)
            # file_mime_type = str("A")

            file_existence_in_bucket = False

            # if self.configuration.sameFileNamePrompt or self.configuration.sameFileHashPrompt:
            # file_existence_in_bucket =
            # self.storj_engine.storj_client.check_file_existence_in_bucket(bucket_id=bucket_id,
            # filepath=file_path) # check if exist file with same file name

            if file_existence_in_bucket == 1:
                # QInputDialog.getText(self, 'Warning!', 'File with name ' + str(bname) + " already exist in bucket! Please use different name:", "test" )
                self.__logger.warning("Same file exist!")

            self.fileisdecrypted_str = ""
            if self.ui_single_file_upload.encrypt_files_checkbox.isChecked():
                # encrypt file
                self.emit(QtCore.SIGNAL("setCurrentUploadState"), "Encrypting file...")
                # self.__logger.warning('"log_event_type": "debug"')
                self.__logger.debug('"title": "Encryption"')
                self.__logger.debug('"description": "Encrypting file..."')

                file_crypto_tools = FileCrypto()
                # Path where to save the encrypted file in temp dir
                file_path_ready = os.path.join(self.parametrs.tmpPath,
                                               bname + ".encrypted")
                self.__logger.debug("Call encryption method")
                # begin file encryption")
                file_crypto_tools.encrypt_file(
                    "AES",
                    file_path,
                    file_path_ready,
                    self.storj_engine.account_manager.get_user_password())
                # file_path_ready = self.parametrs.tmpPath + "/" + bname +\
                #     ".encrypted"  # get path to encrypted file in temp dir
                file_name_ready_to_shard_upload = bname + ".encrypted"
                self.fileisdecrypted_str = ""
            else:
                self.fileisdecrypted_str = "[DECRYPTED]"
                file_path_ready = file_path
                file_name_ready_to_shard_upload = bname

            self.__logger.debug("Temp path: " + self.parametrs.tmpPath)
            self.__logger.debug(file_path_ready + "sciezka2")

            def get_size(file_like_object):
                return os.stat(file_like_object.name).st_size

            # file_size = get_size(file)

            file_size = os.stat(file_path).st_size
            self.uploaded_file_size = file_size
            self.file_mime_type = file_mime_type



            self.ui_single_file_upload.file_size.setText(str(self.tools.human_size(int(file_size))))

            # self.ui_single_file_upload.current_state.setText(
            #   html_format_begin + "Resolving PUSH token..." + html_format_end)

            # self.__logger.warning('"log_event_type": "debug"')

            self.is_upload_active = True
            self.ui_single_file_upload.start_upload_bt.setStyleSheet(("QPushButton:hover{\n"
                                                                      "  background-color: #8C8A87;\n"
                                                                      "  border-color: #8C8A87;\n"
                                                                      "}\n"
                                                                      "QPushButton:active {\n"
                                                                      "  background-color: #8C8A87;\n"
                                                                      "  border-color: #8C8A87;\n"
                                                                      "}\n"
                                                                      "QPushButton{\n"
                                                                      "  background-color: #8C8A87;\n"
                                                                      "    border: 1px solid #8C8A87;\n"
                                                                      "    color: #fff;\n"
                                                                      "    border-radius: 7px;\n"
                                                                      "}"))

            self.__logger.debug('"title": "PUSH token"')
            self.__logger.debug('"description": "Resolving PUSH Token for upload..."')
            # self.__logger.warning(str({"log_event_type": "debug", "title": "PUSH token",
            #                     "description": "Resolving PUSH Token for upload..."}))

            push_token = None

            try:
                push_token = self.storj_engine.storj_client.token_create(bucket_id,
                                                                         'PUSH')  # get the PUSH token from Storj Bridge
                self.push_token = push_token
            except storj.exception.StorjBridgeApiError as e:
                self.is_upload_active = False
                QMessageBox.about(self, "Unhandled PUSH token create exception", "Exception: " + str(e))

            self.ui_single_file_upload.push_token.setText(
                str(push_token.id))  # set the PUSH Token

            self.__logger.debug("PUSH Token ID: " + push_token.id)

            # self.ui_single_file_upload.current_state.setText(
            #    html_format_begin + "Resolving frame for file..." + html_format_end)

            # self.__logger.warning('"log_event_type": "debug"')
            self.__logger.debug('"title": "Frame"')
            self.__logger.debug('"description": "Resolving frame for file upload..."')
            # self.__logger.warning(str({"log_event_type": "debug", "title": "Frame",
            #                     "description": "Resolving frame for file upload..."}))

            frame = None  # initialize variable
            try:
                frame = self.storj_engine.storj_client.frame_create()  # Create file frame
                self.frame = frame
            except storj.exception.StorjBridgeApiError as e:
                self.is_upload_active = False
                QMessageBox.about(
                    self,
                    'Unhandled exception while creating file staging frame',
                    'Exception: %s' % e)
                # self.__logger.warning('"log_event_type": "error"')
                self.__logger.debug('"title": "Frame"')
                self.__logger.debug('"description": "Error while resolving frame for\
                    file upload..."')
                # self.__logger.warning(str({"log_event_type": "error", "title": "Frame",
                #                     "description": "Error while resolving frame for file upload..."}))

            self.ui_single_file_upload.file_frame_id.setText(str(frame.id))

            self.__logger.debug('Frame ID: %s', frame.id)
            # Now encrypt file
            self.__logger.debug('%s sciezka', file_path_ready)

            # Now generate shards
            self.emit(QtCore.SIGNAL('setCurrentUploadState'), 'Splitting file to shards...')
            # self.__logger.warning('"log_event_type": "debug"')
            self.__logger.debug('"title": "Sharding"')
            self.__logger.debug('"description": "Splitting file to shards..."')
            # self.__logger.warning(str({"log_event_type": "debug", "title": "Sharding",
            #                     "description": "Splitting file to shards..."}))

            shards_manager = storj.model.ShardManager(filepath=file_path_ready, tmp_path=self.parametrs.tmpPath)
            self.all_shards_count = len(shards_manager.shards)
            self.emit(QtCore.SIGNAL("updateShardUploadCounters"))

            self.shard_manager_result = shards_manager
            # self.ui_single_file_upload.current_state.setText(
            #   html_format_begin + "Generating shards..." + html_format_end)
            # shards_manager._make_shards()
            shards_count = shards_manager.index
            # create file hash
            self.__logger.debug('file_upload() push_token=%s', push_token)

            # upload shards to frame
            self.__logger.debug('Shards count %s' % shards_count)

            # set shards count
            # self.ui_single_file_upload.shards_count.setText(html_format_begin + str(shards_count) + html_format_end)
            self.all_shards_count = shards_count

            chapters = 0


            for shard in shards_manager.shards:
                self.emit(QtCore.SIGNAL("setShardSize"), int(shard.size))

                self.shard_upload_percent_list.append(0)
                self.createNewShardUploadThread(shard, chapters, frame, file_name_ready_to_shard_upload)
                chapters += 1