コード例 #1
0
ファイル: mainUI.py プロジェクト: Reddmist/storj-gui-client
    def __init__(self, parent=None):
        # EXIT_CODE_REBOOT = -123
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainMenu()
        self.ui.setupUi(self)
        # QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine

        self.account_manager = AccountManager()  # init AccountManager

        user_email = self.account_manager.get_user_email()
        self.ui.account_label.setText(html_format_begin +
                                      str(user_email).strip() +
                                      html_format_end)

        # BUTTONS:
        # QtCore.QObject.connect(self.ui., QtCore.SIGNAL("clicked()"), self.open_login_window) # open login window
        # QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL("clicked()"), self.open_register_window) # open login window
        QtCore.QObject.connect(
            self.ui.create_bucket_bt, QtCore.SIGNAL("clicked()"),
            self.open_bucket_create_window)  # open bucket create window
        QtCore.QObject.connect(
            self.ui.bucket_manager_bt, QtCore.SIGNAL("clicked()"),
            self.open_bucket_manager_window)  # open bucket manager window
        QtCore.QObject.connect(self.ui.settings_bt, QtCore.SIGNAL("clicked()"),
                               self.open_settings_window)  # open settings ui
        QtCore.QObject.connect(
            self.ui.file_manager_bt, QtCore.SIGNAL("clicked()"),
            self.open_file_manager_window)  # open file manager window
        QtCore.QObject.connect(
            self.ui.uploader_bt, QtCore.SIGNAL("clicked()"),
            self.open_single_file_upload_window)  # open single file upload ui
        QtCore.QObject.connect(
            self.ui.downloader_bt, QtCore.SIGNAL("clicked()"),
            self.open_file_mirrors_list_window)  # open single file download ui
コード例 #2
0
ファイル: login.py プロジェクト: enricobacis/storj-gui-client
class LoginUI(QtGui.QMainWindow):
    """Login section."""

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

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        # login UI
        self.login_ui = Ui_Login()
        self.login_ui.setupUi(self)

        # Account manager
        self.login_ui.password.setEchoMode(QtGui.QLineEdit.Password)

        QtCore.QObject.connect(self.login_ui.login_bt,
                               QtCore.SIGNAL("clicked()"),
                               self.login)  # take login action

    def login(self):
        # take login action
        self.email = str(self.login_ui.email.text()).strip()  # get mail
        self.password = str(
            self.login_ui.password.text()).strip()  # get password

        self.storj_client = storj.Client(email=self.email,
                                         password=self.password)
        success = False
        # take login action - check credentials by listing keys :D
        try:
            self.storj_client.key_list()
            success = True
        except storj.exception.StorjBridgeApiError as e:
            j = json.loads(str(e))
            if j.get("error") == "Invalid email or password":
                QtGui.QMessageBox.about(
                    self, "Warning",
                    "Invalid email or password - access denied. "
                    "Please check your credentials and try again!")
            else:
                QtGui.QMessageBox.about(self, "Unhandled exception",
                                        "Exception: " + str(e))

        if success:
            self.account_manager = AccountManager(
                self.email, self.password)  # init account manager
            self.account_manager.save_account_credentials(
            )  # save login credentials and state
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Information,
                                       "Success", "Successfully loged in!",
                                       QtGui.QMessageBox.Ok)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.__logger.info("User {} succesfully logged in".format(
                    self.email))
                self.main_ui_window = MainUI(self)
                self.main_ui_window.show()
                self.close()
コード例 #3
0
 def __init__(self):
     account_manager = AccountManager()
     self.password = None
     if account_manager.if_logged_in():
         self.password = account_manager.get_user_password()
         self.email = account_manager.get_user_email()
         # initialize Storj
         self.storj_client = storj.Client(email=self.email,
                                          password=self.password)
         logger.debug("Login from credentials xml file")
     logger.debug("testlogin, StorjEngine")
コード例 #4
0
    def __init__(self):
        self.account_manager = AccountManager()
        self.password = None
        if self.account_manager.if_logged_in():
            self.password = self.account_manager.get_user_password()
            self.email = self.account_manager.get_user_email()

            # initialize Storj
            self.storj_client = storj.Client(
                email=self.email,
                password=self.password,
                do_hashing=False)
            self.__logger.debug('Login from credentials xml file')
        self.__logger.debug('testlogin, StorjEngine')
コード例 #5
0
 def __init__(self):
     self.conf_manager = Configuration()
     self.account_manager = AccountManager()
     self.password = None
     if self.account_manager.if_logged_in():
         self.password = self.account_manager.get_user_password()
         self.email = self.account_manager.get_user_email()
         # initialize Storj
         max_bridge_request_timeout = self.conf_manager.get_max_bridge_request_timeout()
         bridge_api_url = self.conf_manager.get_bridge_api_url()
         self.storj_client = storj.Client(email=self.email,
                                          password=self.password,
                                          do_hashing=False,
                                          timeout=max_bridge_request_timeout,
                                          storj_bridge=bridge_api_url)
         logger.debug('Login from credentials xml file')
     logger.debug('testlogin, StorjEngine')
コード例 #6
0
ファイル: login.py プロジェクト: enricobacis/storj-gui-client
    def login(self):
        # take login action
        self.email = str(self.login_ui.email.text()).strip()  # get mail
        self.password = str(
            self.login_ui.password.text()).strip()  # get password

        self.storj_client = storj.Client(email=self.email,
                                         password=self.password)
        success = False
        # take login action - check credentials by listing keys :D
        try:
            self.storj_client.key_list()
            success = True
        except storj.exception.StorjBridgeApiError as e:
            j = json.loads(str(e))
            if j.get("error") == "Invalid email or password":
                QtGui.QMessageBox.about(
                    self, "Warning",
                    "Invalid email or password - access denied. "
                    "Please check your credentials and try again!")
            else:
                QtGui.QMessageBox.about(self, "Unhandled exception",
                                        "Exception: " + str(e))

        if success:
            self.account_manager = AccountManager(
                self.email, self.password)  # init account manager
            self.account_manager.save_account_credentials(
            )  # save login credentials and state
            msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Information,
                                       "Success", "Successfully loged in!",
                                       QtGui.QMessageBox.Ok)
            result = msgBox.exec_()
            if result == QtGui.QMessageBox.Ok:
                self.__logger.info("User {} succesfully logged in".format(
                    self.email))
                self.main_ui_window = MainUI(self)
                self.main_ui_window.show()
                self.close()
コード例 #7
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)
コード例 #8
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.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)

    def show_download_finished_message(self):
        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 download_begin(self, file_pointers):
        self.destination_file_path = str(
            self.ui_single_file_download.file_save_path.text())
        self.tmp_path = str(self.ui_single_file_download.tmp_dir.text())

        options_array = {}
        options_array["tmp_path"] = self.tmp_path

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

        self.ui_single_file_download.shard_queue_table.setColumnCount(5)
        self.ui_single_file_download.shard_queue_table.setRowCount(
            len(file_pointers))

        self.ui_single_file_download.shard_queue_table.setHorizontalHeaderLabels(
            ['Progress', 'Hash', 'Farmer addres', 'State', 'Shard index'])
        for pointer in file_pointers:
            item = QTableWidgetItem(str(""))
            self.ui_single_file_download.shard_queue_table.setItem(
                i, 0, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str(pointer["hash"]))
            self.ui_single_file_download.shard_queue_table.setItem(
                i, 1, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(
                str(pointer["farmer"]["address"] + ":" +
                    str(pointer["farmer"]["port"])))
            self.ui_single_file_download.shard_queue_table.setItem(
                i, 2, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str("Waiting..."))
            self.ui_single_file_download.shard_queue_table.setItem(
                i, 3, item)  # row, column, item (QStandardItem)

            item = QTableWidgetItem(str(pointer["index"]))
            self.ui_single_file_download.shard_queue_table.setItem(
                i, 4, item)  # row, column, item (QStandardItem)

            options_array["file_size_shard_" + str(i)] = pointer["size"]
            i = i + 1
            # print  str(pointer["index"])+"index"

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

        i2 = 0
        self.progressbar_list = []
        for pointer in file_pointers:
            tablemodel = self.ui_single_file_download.shard_queue_table.model()
            index = tablemodel.index(i2, 0)
            self.progressbar_list.append(QProgressBar())
            self.ui_single_file_download.shard_queue_table.setIndexWidget(
                index, self.progressbar_list[i2])
            i2 = i2 + 1

        options_array["file_pointers"] = file_pointers
        options_array["file_pointers_is_given"] = "1"
        options_array["progressbars_enabled"] = "1"
        options_array["file_size_is_given"] = "1"
        options_array["shards_count"] = i

        self.all_shards_count = i

        self.ui_single_file_download.total_shards.setText(html_format_begin +
                                                          str(i) +
                                                          html_format_end)

        # storj_sdk_overrides = StorjSDKImplementationsOverrides()

        # self.file_download(None, None, "/home/lakewik/kotek2", options_array, self.progressbar_list)
        self.file_download(None, None, self.destination_file_path,
                           options_array, self.progressbar_list)
        # progressbar_list[0].setValue(20)
        # progressbar_list[2].setValue(17)

    def createNewDownloadInitThread(self, bucket_id, file_id):
        self.ui_single_file_download.overall_progress.setValue(0)
        file_name_resolve_thread = threading.Thread(
            target=self.init_download_file_pointers, 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 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 update_shard_download_progess(self, row_position_index, value):
        self.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(html_format_begin +
                                                           current_status +
                                                           html_format_end)

    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 init_download_file_pointers(self, bucket_id, file_id):
        try:
            # logger.warning("log_event_type": "debug")
            logger.debug('"title": "File pointers"')
            logger.debug('"description": "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) + "..."}))

            file_pointers = self.storj_engine.storj_client.file_pointers(
                str(bucket_id), file_id)
            self.emit(QtCore.SIGNAL("beginDownloadProccess"), file_pointers)
        except storj.exception.StorjBridgeApiError as e:
            # 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)

    def select_file_save_path(self):
        file_save_path = QtGui.QFileDialog.getSaveFileName(
            self, 'Save file to...', '')
        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,
                                   progress_bar, rowposition, shard_index):
        local_filename = path_to_save
        downloaded = False
        farmer_tries = 0

        # logger.warning('"log_event_type": "debug"')
        logger.debug('"title": "Downloading"')
        logger.debug('"description": "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.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")
                        # progress_bar.setValue(percent_downloaded)

                    f.close()
                    downloaded = True
            except Exception as e:
                logger.error(e)
                # logger.warning('"log_event_type": "warning"')
                logger.debug('"title": "Unhandled error"')
                logger.debug('"description": "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.emit(QtCore.SIGNAL("retryWithNewDownloadPointer"),
                      options_chain["shard_index"]
                      )  # retry download with new download pointer
        else:
            # 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('"title": "Shard downloaded"')
            logger.debug('"description": "Shard at index "' +
                         str(shard_index) + " downloaded successfully.")
            self.emit(QtCore.SIGNAL("incrementShardsDownloadProgressCounters")
                      )  # update already uploaded shards count
            self.emit(QtCore.SIGNAL("updateDownloadTaskState"), rowposition,
                      "Downloaded!")  # update shard download state
            if int(self.all_shards_count) <= int(
                    self.shards_already_downloaded + 1):
                self.emit(
                    QtCore.SIGNAL("finishDownload")
                )  # send signal to begin file shards joind and decryption after all shards are downloaded

            return

    def createNewDownloadThread(self, url, filelocation, options_chain,
                                progress_bars_list, 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, progress_bars_list,
                  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 upload_file(self):
        logger.debug(1)

    def file_download(self, bucket_id, file_id, file_save_path, options_array,
                      progress_bars_list):
        # 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('"description": "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]

        #### Begin file download finish function ####
        # Wait for signal to do shards joining and encryption
        def finish_download(self):

            fileisencrypted = False

            # Join shards
            sharing_tools = ShardingTools()
            self.set_current_status("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('"description": "Joining shards..."')

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

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

            if fileisencrypted:
                # decrypt file
                self.set_current_status("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('"description": "Decrypting file..."')
                # self.set_current_status()
                file_crypto_tools = FileCrypto()
                file_crypto_tools.decrypt_file(
                    "AES",
                    str(file_save_path) + ".encrypted", file_save_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(
                '"description": "Downloading completed successfully!"')
            self.emit(QtCore.SIGNAL("showFileDownloadedSuccessfully"))

            return True

        self.connect(self, QtCore.SIGNAL("finishDownload"),
                     lambda: finish_download(self))

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

        get_file_pointers_tries = 0
        while self.max_retries_get_file_pointers > get_file_pointers_tries:
            get_file_pointers_tries += 1
            try:
                # Determine file pointers
                if options_array["file_pointers_is_given"] == "1":
                    pointers = options_array["file_pointers"]
                else:
                    pointers = self.storj_engine.storj_client.file_pointers(
                        bucket_id=bucket_id, file_id=file_id)

                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"])

                i = 0
                shard_size_array = []
                while i < shards_count:
                    shard_size_array.append(
                        int(options_array["file_size_shard_" + str(i)]))
                    i += 1
                logger.debug(shard_size_array)
                part = 0

                self.tmp_path = options_array["tmp_path"]

                self.set_current_status("Starting download threads...")
                for pointer in pointers:
                    self.set_current_status("Downloading shard at index " +
                                            str(part) + "...")
                    options_chain["rowposition"] = part
                    self.shard_download_percent_list.append(0)

                    logger.debug(pointer)
                    options_chain["shard_file_size"] = shard_size_array[part]
                    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, progress_bars_list[part], part,
                            part)
                    else:
                        self.createNewDownloadThread(
                            url, self.tmp_path + "/" + str(file_name) + "-" +
                            str(part), options_chain, progress_bars_list[part],
                            part, part)

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

            except storj.exception.StorjBridgeApiError as e:
                self.emit(
                    QtCore.SIGNAL("showStorjBridgeException"),
                    "Error while resolving file pointers for download. " +
                    str(e))  # emit Storj Bridge Exception
                continue
            except Exception as e:
                self.emit(QtCore.SIGNAL(
                    "showStorjBridgeException"
                ), "Unhandled error while resolving file pointers for download. "
                          + str(e))  # emit unhandled Exception
                continue
            else:
                break
コード例 #9
0
ファイル: mainUI.py プロジェクト: Reddmist/storj-gui-client
class MainUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        # EXIT_CODE_REBOOT = -123
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainMenu()
        self.ui.setupUi(self)
        # QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL("clicked()"), self.save_config) # open bucket manager
        self.storj_engine = StorjEngine()  # init StorjEngine

        self.account_manager = AccountManager()  # init AccountManager

        user_email = self.account_manager.get_user_email()
        self.ui.account_label.setText(html_format_begin +
                                      str(user_email).strip() +
                                      html_format_end)

        # BUTTONS:
        # QtCore.QObject.connect(self.ui., QtCore.SIGNAL("clicked()"), self.open_login_window) # open login window
        # QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL("clicked()"), self.open_register_window) # open login window
        QtCore.QObject.connect(
            self.ui.create_bucket_bt, QtCore.SIGNAL("clicked()"),
            self.open_bucket_create_window)  # open bucket create window
        QtCore.QObject.connect(
            self.ui.bucket_manager_bt, QtCore.SIGNAL("clicked()"),
            self.open_bucket_manager_window)  # open bucket manager window
        QtCore.QObject.connect(self.ui.settings_bt, QtCore.SIGNAL("clicked()"),
                               self.open_settings_window)  # open settings ui
        QtCore.QObject.connect(
            self.ui.file_manager_bt, QtCore.SIGNAL("clicked()"),
            self.open_file_manager_window)  # open file manager window
        QtCore.QObject.connect(
            self.ui.uploader_bt, QtCore.SIGNAL("clicked()"),
            self.open_single_file_upload_window)  # open single file upload ui
        QtCore.QObject.connect(
            self.ui.downloader_bt, QtCore.SIGNAL("clicked()"),
            self.open_file_mirrors_list_window)  # open single file download ui

    """
    def open_login_window(self):
        self.login_window = LoginUI(self)
        self.login_window.show()

        self.login_window = ClientConfigurationUI(self)
        self.login_window.show()

        # take login action
        print 1
    """
    """
    def open_register_window(self):
        self.register_window = RegisterUI(self)
        self.register_window.show()
    """

    def open_bucket_create_window(self):
        """Create a new bucket"""
        logger.debug("Create a new bucket")
        self.bucket_create_window = BucketCreateUI(self)
        self.bucket_create_window.show()

    def open_bucket_manager_window(self):
        """Bucket manager"""
        logger.debug("Bucket manager")
        self.bucket_manager_window = BucketManagerUI(self)
        self.bucket_manager_window.show()

    def open_file_manager_window(self):
        """File manager"""
        logger.debug("File manager")
        self.file_manager_window = FileManagerUI(self)
        self.file_manager_window.show()

    def open_file_mirrors_list_window(self):
        """File download"""
        logger.debug("Download file")
        self.file_mirrors_list_window = FileMirrorsListUI(self)
        self.file_mirrors_list_window.show()

    def open_single_file_upload_window(self):
        """File upload"""
        logger.debug("Upload file")
        self.single_file_upload_window = SingleFileUploadUI(self)
        self.single_file_upload_window.show()

    def open_settings_window(self):
        """Settings"""
        logger.debug("Open settings")
        self.settings_window = ClientConfigurationUI(self)
        self.settings_window.show()
コード例 #10
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.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
コード例 #11
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')
コード例 #12
0
    def __init__(self, parent=None, bucketid=None, fileid=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui_single_file_upload = Ui_SingleFileUpload()
        self.ui_single_file_upload.setupUi(self)
        QtCore.QObject.connect(self.ui_single_file_upload.start_upload_bt, QtCore.SIGNAL("clicked()"),
                               self.createNewUploadThread)  # open bucket manager
        QtCore.QObject.connect(self.ui_single_file_upload.file_path_select_bt, QtCore.SIGNAL("clicked()"),
                               self.select_file_path)  # open file select dialog
        QtCore.QObject.connect(self.ui_single_file_upload.tmp_path_select_bt, QtCore.SIGNAL("clicked()"),
                               self.select_tmp_directory)  # open tmp directory select dialog
        self.storj_engine = StorjEngine()  # init StorjEngine

        self.initialize_upload_queue_table()

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

        self.account_manager = AccountManager()  # init AccountManager

        self.user_password = self.account_manager.get_user_password()

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

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

        self.connect(self, SIGNAL("addRowToUploadQueueTable"), self.add_row_upload_queue_table)

        self.connect(self, SIGNAL("incrementShardsProgressCounters"), self.increment_shards_progress_counters)
        self.connect(self, SIGNAL("updateUploadTaskState"), self.update_upload_task_state)
        self.connect(self, SIGNAL("updateShardUploadProgress"), self.update_shard_upload_progess)
        self.connect(self, SIGNAL("showFileNotSelectedError"), self.show_error_not_selected_file)
        self.connect(self, SIGNAL("showInvalidPathError"), self.show_error_invalid_file_path)
        self.connect(self, SIGNAL("showInvalidTemporaryPathError"), self.show_error_invalid_temporary_path)
        self.connect(self, SIGNAL("refreshOverallProgress"), self.refresh_overall_progress)
        self.connect(self, SIGNAL("showFileUploadedSuccessfully"), self.show_upload_finished_message)

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

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

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

        self.max_retries_upload_to_same_farmer = 3
        self.max_retries_negotiate_contract = 10

        #
        # print self.config.max_retries_upload_to_same_farmer

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)
コード例 #13
0
class SingleFileUploadUI(QtGui.QMainWindow):

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

        self.initialize_upload_queue_table()

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

        self.account_manager = AccountManager()  # init AccountManager

        self.user_password = self.account_manager.get_user_password()

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

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

        self.connect(self, SIGNAL("addRowToUploadQueueTable"), self.add_row_upload_queue_table)

        self.connect(self, SIGNAL("incrementShardsProgressCounters"), self.increment_shards_progress_counters)
        self.connect(self, SIGNAL("updateUploadTaskState"), self.update_upload_task_state)
        self.connect(self, SIGNAL("updateShardUploadProgress"), self.update_shard_upload_progess)
        self.connect(self, SIGNAL("showFileNotSelectedError"), self.show_error_not_selected_file)
        self.connect(self, SIGNAL("showInvalidPathError"), self.show_error_invalid_file_path)
        self.connect(self, SIGNAL("showInvalidTemporaryPathError"), self.show_error_invalid_temporary_path)
        self.connect(self, SIGNAL("refreshOverallProgress"), self.refresh_overall_progress)
        self.connect(self, SIGNAL("showFileUploadedSuccessfully"), self.show_upload_finished_message)

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

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

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

        self.max_retries_upload_to_same_farmer = 3
        self.max_retries_negotiate_contract = 10

        #
        # print self.config.max_retries_upload_to_same_farmer

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

    def show_upload_finished_message(self):
        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)

        logger.info(str(actual_percent_uploaded) + str(base_percent) +
                    "total_percent_uploaded")

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

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

    def update_shard_upload_progess(self, row_position_index, value):
        self.upload_queue_progressbar_list[row_position_index].setValue(value)
        logger.debug("kotek")
        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):
        logger.warning(str({"log_event_type": "info", "title": "Buckets", "description": "Resolving buckets from Bridge to buckets combobox..."}))

        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 storj.exception.StorjBridgeApiError as e:
            QMessageBox.about(self, "Unhandled bucket resolving exception", "Exception: " + str(e))

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

    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)

    def add_row_upload_queue_table(self, row_data):
        self.upload_queue_progressbar_list.append(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(str(row_data["farmer_address"]) + ":" + str(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)

        logger.info(row_data)

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

    def select_file_path(self):
        self.ui_single_file_upload.file_path.setText(QFileDialog.getOpenFileName())

    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, 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 upload_shard(self, shard, chapters, frame, file_name_ready_to_shard_upload):

        self.uploadblocksize = 4096

        def read_in_chunks(file_object, shard_size, rowposition, blocksize=self.uploadblocksize, chunks=-1, shard_index=None):
            """Lazy function (generator) to read a file piece by piece.
            Default chunk size: 1k."""

            i = 0
            while chunks:
                data = file_object.read(blocksize)
                if not data:
                    break
                yield data
                i += 1
                t1 = float(shard_size) / float((self.uploadblocksize))
                if shard_size <= (self.uploadblocksize):
                    t1 = 1

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

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

        it = 0
        contract_negotiation_tries = 0
        while self.max_retries_negotiate_contract > contract_negotiation_tries:
            contract_negotiation_tries += 1

            # emit signal to add row to upload queue table
            # self.emit(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)

            # logger.warning('"log_event_type": "debug"')
            logger.warning('"title": "Negotiating contract"')
            logger.warning('"description": "Trying to negotiate storage \
                    contract for shard at inxed " + str(chapters) + "..."')
            # logger.warning(str({"log_event_type": "debug", "title": "Negotiating contract",
            #                     "description": "Trying to negotiate storage contract for shard at inxed " + str(chapters) + "..."}))

            try:
                frame_content = self.storj_engine.storj_client.frame_add_shard(shard, frame.id)

                # 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)

                # logger.warning('"log_event_type": "debug"')
                logger.warning('"title": "Contract negotiated"')
                logger.warning('"description": "Storage contract negotiated with: "' +
                               str(frame_content["farmer"]["address"]) + ":" +
                               str(frame_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(SIGNAL("addRowToUploadQueueTable"), tablerowdata)  # add row to table

                rowcount = self.ui_single_file_upload.shard_queue_table_widget.rowCount()

                logger.debug(frame_content)
                logger.debug(shard)
                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"]
                logger.debug(url)

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

                self.set_current_status("Uploading shard " + str(chapters + 1) + " to farmer...")

                # begin recording exchange report
                exchange_report = storj.model.ExchangeReport()

                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)

                rowposition = rowcount
                farmer_tries = 0
                response = None
                while self.max_retries_upload_to_same_farmer > farmer_tries:
                    farmer_tries += 1
                    try:
                        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"]))}))

                        with open(self.parametrs.tmpPath + file_name_ready_to_shard_upload + '-' + str(chapters + 1),
                                  'rb') as f:
                            response = requests.post(url, data=read_in_chunks(f, shard_size, rowposition, shard_index=chapters), timeout=1)

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

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

                    except Exception as e:
                        self.emit(SIGNAL("updateUploadTaskState"), rowposition,
                                  "First try failed. Retrying... (" + str(farmer_tries) + ")")  # update shard upload state

                        # logger.warning('"log_event_type": "warning"')
                        logger.warning('"title": "Shard upload error"')
                        logger.warning('"description": "Error while uploading \
                                       shard to: "' +
                                       frame_content["farmer"]["address"] +
                                       ":" +
                                       str(frame_content["farmer"]["port"]) +
                                       " Retrying... (" + str(farmer_tries) +
                                       ")")
                        # 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) + ")"}))
                        logger.error(e)
                        continue
                    else:
                        self.emit(SIGNAL("incrementShardsProgressCounters"))  # update already uploaded shards count
                        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(SIGNAL("updateUploadTaskState"), rowposition,
                                  "Uploaded!")  # update shard upload state

                        logger.debug(str(self.all_shards_count) + "wszystkie" +
                                     str(self.shards_already_uploaded) + "wyslane")
                        if int(self.all_shards_count) <= int(self.shards_already_uploaded + 1):
                            self.emit(SIGNAL("finishUpload"))  # send signal to save to bucket after all files are uploaded
                        break

                logger.debug(response.content)

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

                firstiteration = False
                it += 1

            except storj.exception.StorjBridgeApiError as e:
                # upload failed due to Storj Bridge failure
                logger.error("Exception raised while trying to negitiate \
                             contract: " + str(e))
                # logger.warning('"log_event_type": "error"')
                logger.warning('"title": "Bridge exception"')
                logger.warning('"description": "Exception raised while trying \
                               to negotiate storage contract for shard at index\
                               "' + str(chapters))
                # 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:
                # now send Exchange Report
                # upload failed probably while sending data to farmer
                logger.error("Error occured while trying to upload shard or\
                             negotiate contract. Retrying... " + str(e))
                # logger.warning('"log_event_type": "error"')
                logger.warning('"title": "Unhandled exception"')
                logger.warning('"description": "Unhandled exception occured\
                               while trying to upload shard or negotiate \
                               contract for shard at index "' +
                               str(chapters) +
                               " . Retrying...")
                # 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.set_current_status("Sending Exchange Report for shard " + str(chapters + 1))
                # self.storj_engine.storj_client.send_exchange_report(exchange_report) # send exchange report
                continue
            else:
                # 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.set_current_status("Sending Exchange Report for shard " + str(chapters + 1))
                # logger.warning('"log_event_type": "debug"')
                logger.debug('"title":"Shard added"')
                logger.info('"description": "Shard "' + str(chapters + 1) +
                            " successfully added and exchange report sent.")
                # 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 file_upload_begin(self):
        self.ui_single_file_upload.overall_progress.setValue(0)
        # upload finish function #

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

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

            logger.debug(frame.id)
            logger.debug("Now upload file")

            data = {
                'x-token': push_token.id,
                'x-filesize': str(file_size),
                'frame': frame.id,
                'mimetype': file_mime_type,
                'filename': str(bname),
                '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)

            # logger.warning('"log_event_type": "debug"')
            logger.debug('"title": "Finishing upload"')
            logger.debug('"description": "Adding file "' +
                         str(bname) + " to bucket...")
            # 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': push_token.id,
                        'x-filesize': str(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)
                logger.warning(str({"log_event_type": "success", "title": "File uploaded",
                                    "description": "File uploaded successfully!"}))
                self.emit(SIGNAL("showFileUploadedSuccessfully"))

        self.connect(self, SIGNAL("finishUpload"), lambda: finish_upload(self))

        # 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
        if self.ui_single_file_upload.tmp_path.text() == "":
            self.parametrs.tmpPath = "/tmp/"
        else:
            self.parametrs.tmpPath = str(self.ui_single_file_upload.tmp_path.text())

        self.configuration = Configuration()

        # get temporary files path
        if self.ui_single_file_upload.file_path.text() == "":
            self.validation["file_path"] = False
            self.emit(SIGNAL("showFileNotSelectedError"))  # show error missing file path
        else:
            self.validation["file_path"] = True
            file_path = str(self.ui_single_file_upload.file_path.text())

        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]

            logger.debug(bname + "npliku")

            mime = magic.Magic(mime=True)
            file_mime_type = str(mime.from_file(str(file_path)))
            file_mime_type = "text/plain"
            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) # chech 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" )
                logger.warning("Same file exist!")

            if self.ui_single_file_upload.encrypt_files_checkbox.isChecked():
                # encrypt file
                self.set_current_status("Encrypting file...")
                # logger.warning('"log_event_type": "debug"')
                logger.debug('"title": "Encryption"')
                logger.debug('"description": "Encrypting file..."')

                file_crypto_tools = FileCrypto()
                file_crypto_tools.encrypt_file("AES", str(file_path), self.parametrs.tmpPath + "/" + bname + ".encrypted",
                                               str(self.user_password))  # begin file encryption
                file_path_ready = self.parametrs.tmpPath + "/" + bname +\
                    ".encrypted"  # get path to encrypted file in temp dir
                file_name_ready_to_shard_upload = bname + ".encrypted"
            else:
                file_path_ready = file_path
                file_name_ready_to_shard_upload = bname

            logger.debug(self.parametrs.tmpPath)
            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

            tools = Tools()

            self.ui_single_file_upload.file_size.setText(html_format_begin + str(tools.human_size(int(file_size))) + html_format_end)

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

            # logger.warning('"log_event_type": "debug"')
            logger.debug('"title": "PUSH token"')
            logger.debug('"description": "Resolving PUSH Token for upload..."')
            # 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
            except storj.exception.StorjBridgeApiError as e:
                QMessageBox.about(self, "Unhandled PUSH token create exception", "Exception: " + str(e))

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

            logger.debug(push_token.id)

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

            # logger.warning('"log_event_type": "debug"')
            logger.debug('"title": "Frame"')
            logger.debug('"description": "Resolving frame for file upload..."')
            # 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
            except storj.exception.StorjBridgeApiError as e:
                QMessageBox.about(self, "Unhandled exception while creating file staging frame", "Exception: " + str(e))
                # logger.warning('"log_event_type": "error"')
                logger.debug('"title": "Frame"')
                logger.debug('"description": "Error while resolving frame for\
                    file upload..."')
                # 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(html_format_begin + str(frame.id) + html_format_end)

            logger.debug(frame.id)
            # Now encrypt file
            logger.debug(file_path_ready + "sciezka")

            # Now generate shards
            self.set_current_status("Splitting file to shards...")
            logger.warning(str({"log_event_type": "debug", "title": "Sharding",
                                "description": "Splitting file to shards..."}))
            shards_manager = storj.model.ShardManager(filepath=str(file_path_ready), tmp_path=self.parametrs.tmpPath)

            # 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.storj_engine.storj_client.logger.debug('file_upload() push_token=%s', push_token)

            # upload shards to frame
            logger.debug(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
            firstiteration = True

            for shard in shards_manager.shards:
                self.shard_upload_percent_list.append(0)
                self.createNewShardUploadThread(shard, chapters, frame, file_name_ready_to_shard_upload)
                chapters += 1