Пример #1
0
    def set_file_metadata(self, bucket_id, file_id):
        try:
            file_metadata = self.storj_engine.storj_client.file_metadata(
                str(bucket_id), str(file_id))
            self.ui_single_file_download.file_name.setText(
                html_format_begin + str(file_metadata.filename) +
                html_format_end)

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

            self.ui_single_file_download.file_save_path.setText(
                str(tools.get_home_user_directory() + "/" +
                    str(file_metadata.filename)))
        except storj.exception.StorjBridgeApiError as e:
            self.emit(QtCore.SIGNAL("showStorjBridgeException"),
                      "Error while resolving file metadata. " +
                      str(e))  # emit Storj Bridge Exception
        except Exception as e:
            self.emit(QtCore.SIGNAL("showUnhandledException"),
                      "Unhandled error while resolving file metadata. " +
                      str(e))  # emit unhandled Exception
Пример #2
0
class SyncOptionsUI(QtGui.QMainWindow):
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.sync_menu_ui = Ui_FileSyncOptions()
        self.sync_menu_ui.setupUi(self)

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

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

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

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

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

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

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

        self.sync_menu_ui.sync_directories_tableWidget.setRowCount(0)

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

        self.sync_configuration_manager.paint_config_to_ui(self.sync_menu_ui)

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

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

    def save_sync_options(self):
        # validate settings

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

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

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

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

        return True

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

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

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

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

        return True
Пример #3
0
class AccountDashUI(QtGui.QMainWindow):
    def __init__(
        self,
        parent=None,
    ):
        QtGui.QWidget.__init__(self, parent)
        self.account_dash_ui = Ui_AccountDash()
        self.account_dash_ui.setupUi(self)

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

        self.initialize_buckets_stats_table()

        self.createNewBucketsStatsGetThread()

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

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

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

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

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

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

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

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

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

            total_files_count += total_bucket_files_count
            total_files_size += total_bucket_files_size

        self.account_dash_ui.files_total_count.setText(str(total_files_count))
        self.account_dash_ui.total_used_space.setText(
            str(self.tools.human_size(total_files_size)))
Пример #4
0
class FileManagerUI(QtGui.QMainWindow):

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

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

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

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

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

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

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

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

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

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

        return True

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

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

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

        logger.debug(1)

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

    def update_files_list(self):

        self.tools = Tools()

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

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

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

        i = 0

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

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

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

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

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

                i = i + 1

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

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

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

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

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

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

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

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

        logger.debug(1)
Пример #5
0
class SingleFileUploadUI(QtGui.QMainWindow):
    __logger = logging.getLogger('%s.SingleFileUploadUI' % __name__)

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

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

        self.already_used_farmers_nodes = []

        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

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

        self.is_upload_active = False
        self.current_active_connections = 0

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

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

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

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

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

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

        self.is_files_queue_table_visible = False

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

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

        self.prepare_files_queue_table()

        self.clip = QtGui.QApplication.clipboard()

        self.ui_single_file_upload.connections_onetime.setMaximum(
            MAX_ALLOWED_UPLOAD_CONCURRENCY)

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

        self.current_row = 0

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

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

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

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

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

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

        self.animation.start()

    def hide_files_queue(self):

        return True

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        self.__logger.info(row_data)

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

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

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

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

    def initialize_upload_queue_table(self):

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

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

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

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

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

        #pool = multiprocessing.Pool()

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

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

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

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

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

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

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

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

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

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

        self.semaphore.acquire()

        contract_negotiation_tries = 0

        print self.already_used_farmers_nodes

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

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

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

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

                rowposition = self.current_row
                self.current_row += 1

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

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

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

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

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

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

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

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

                # begin recording exchange report

                current_timestamp = int(time.time())

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

                shard_size = int(shard.size)

                farmer_tries = 0
                response = None
                success_shard_upload = False

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

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

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

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

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

                        success_shard_upload = True

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

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

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

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

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

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

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

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

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

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

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

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

                self.__logger.debug(response.content)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    def file_upload_begin(self):

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

        self.ui_single_file_upload.overall_progress.setValue(0)

        file_path = None
        self.validation = {}

        self.initialize_upload_queue_table()

        encryption_enabled = True

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

        self.configuration = Configuration()

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

        if self.validation["file_path"]:

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

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

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

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

            file_mime_type = "text/plain"

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

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

            file_existence_in_bucket = False

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

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

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

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

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

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

            # file_size = get_size(file)

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

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

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

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

            push_token = None
            PUSH_token_resolve_retries = 0

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

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

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

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

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

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

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

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

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

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

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

            # set shards count
            self.all_shards_count = shards_count

            chapters = 0

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

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

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

            for t in threads:
                t.join()
Пример #6
0
class MainUI(QtGui.QMainWindow):
    """Main UI section."""

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

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

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

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

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

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

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

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

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

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

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

        self.createNewBucketResolveThread()

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

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

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

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

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

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

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

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

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

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

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

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

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

        return True

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

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

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

        self.__logger.debug(1)

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

    def update_files_list(self):

        self.tools = Tools()

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

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

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

        model.setHorizontalHeaderLabels(file_list_header_labels)

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

        i = 0

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

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

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

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

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

                #print self.file_details

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

                i = i + 1

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

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

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

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

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

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

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

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

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

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

        self.__logger.debug(1)
Пример #7
0
class SingleFileUploadUI(QtGui.QMainWindow):
    __logger = logging.getLogger('%s.SingleFileUploadUI' % __name__)

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

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


        self.tools = Tools()

        self.storj_engine = StorjEngine()

        self.initialize_upload_queue_table()
        self.dashboard_instance = dashboard_instance

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

        self.is_upload_active = False
        self.current_active_connections = 0

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

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

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

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

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

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

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

        self.max_retries_upload_to_same_farmer = MAX_RETRIES_UPLOAD_TO_SAME_FARMER
        self.max_retries_negotiate_contract = MAX_RETRIES_NEGOTIATE_CONTRACT

        # self.initialize_shard_queue_table(file_pointers)

        self.shard_upload_percent_list = []

        self.ui_single_file_upload.overall_progress.setValue(0)

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


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


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

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

        actual_percent_uploaded = total_percent_uploaded / total_percent_to_upload

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

        self.__logger.info(row_data)

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

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

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

        # Refactor to QtTrhead

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

    def initialize_upload_queue_table(self):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        contract_negotiation_tries = 0

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

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

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

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

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

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

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

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

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

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

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

                # begin recording exchange report

                current_timestamp = int(time.time())

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

                shard_size = int(shard.size)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                self.__logger.debug(response.content)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        # end upload finishing function #

        file_path = None
        self.validation = {}

        self.initialize_upload_queue_table()

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

        # progress.valueChanged.connect(item.updateValue)

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

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

        self.configuration = Configuration()

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

        if self.validation["file_path"]:

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

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

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

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

            file_mime_type = "text/plain"

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

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

            file_existence_in_bucket = False

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

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

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

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

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

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

            # file_size = get_size(file)

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



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

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

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

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

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

            push_token = None

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

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

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

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

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

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

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

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

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

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

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

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

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

            chapters = 0


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

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