Example #1
0
    def rename_item(self, name_item):
        item_type = name_item.data(USER_ROLE_ITEM_TYPE)

        if item_type == ITEM_TYPE_FILE:
            title = 'Rename File'
            type_name = 'file'
        else:
            title = 'Rename Directory'
            type_name = 'directory'

        old_name = name_item.text()

        # get new name
        dialog = ExpandingInputDialog(get_main_window())
        dialog.setModal(True)
        dialog.setWindowTitle(title)
        dialog.setLabelText('Enter new {0} name:'.format(type_name))
        dialog.setInputMode(QInputDialog.TextInput)
        dialog.setTextValue(old_name)
        dialog.setOkButtonText('Rename')

        if dialog.exec_() != QDialog.Accepted:
            return

        new_name = dialog.textValue()

        if new_name == old_name:
            return

        # check that new name is valid
        if len(
                new_name
        ) == 0 or new_name == '.' or new_name == '..' or '/' in new_name:
            QMessageBox.critical(
                get_main_window(), title + ' Error',
                'A {0} name cannot be empty, cannot be one dot [.], cannot be two dots [..] and cannot contain a forward slash [/].'
                .format(type_name))
            return

        # check that new name is not already in use
        name_item_parent = name_item.parent()

        if name_item_parent == None:
            name_item_parent = self.tree_files_model.invisibleRootItem()

        for i in range(name_item_parent.rowCount()):
            if new_name == name_item_parent.child(i).text():
                QMessageBox.critical(
                    get_main_window(), title + ' Error',
                    'The new {0} name is already in use.'.format(type_name))
                return

        absolute_old_name = posixpath.join(self.bin_directory,
                                           get_full_item_path(name_item))
        absolute_new_name = posixpath.join(
            posixpath.split(absolute_old_name)[0], new_name)

        def cb_rename(result):
            if not report_script_result(
                    result, title + ' Error',
                    'Could not rename {0}'.format(type_name)):
                return

            name_item.setText(new_name)

            if self.tree_files.header().sortIndicatorSection() == 0:
                self.tree_files.header().setSortIndicator(
                    0,
                    self.tree_files.header().sortIndicatorOrder())

        self.script_manager.execute_script(
            'rename', cb_rename, [absolute_old_name, absolute_new_name])
Example #2
0
    def rename_item(self, name_item):
        item_type = name_item.data(USER_ROLE_ITEM_TYPE)

        if item_type == ITEM_TYPE_FILE:
            title     = 'Rename File'
            type_name = 'file'
        else:
            title     = 'Rename Directory'
            type_name = 'directory'

        old_name = name_item.text()

        # get new name
        dialog = ExpandingInputDialog(get_main_window())
        dialog.setModal(True)
        dialog.setWindowTitle(title)
        dialog.setLabelText('Enter new {0} name:'.format(type_name))
        dialog.setInputMode(QInputDialog.TextInput)
        dialog.setTextValue(old_name)
        dialog.setOkButtonText('Rename')

        if dialog.exec_() != QDialog.Accepted:
            return

        new_name = dialog.textValue()

        if new_name == old_name:
            return

        # check that new name is valid
        if len(new_name) == 0 or new_name == '.' or new_name == '..' or '/' in new_name:
            QMessageBox.critical(get_main_window(), title + ' Error',
                                 'A {0} name cannot be empty, cannot be one dot [.], cannot be two dots [..] and cannot contain a forward slash [/].'
                                 .format(type_name))
            return

        # check that new name is not already in use
        name_item_parent = name_item.parent()

        if name_item_parent == None:
            name_item_parent = self.tree_files_model.invisibleRootItem()

        for i in range(name_item_parent.rowCount()):
            if new_name == name_item_parent.child(i).text():
                QMessageBox.critical(get_main_window(), title + ' Error',
                                     'The new {0} name is already in use.'.format(type_name))
                return

        absolute_old_name = posixpath.join(self.bin_directory, get_full_item_path(name_item))
        absolute_new_name = posixpath.join(posixpath.split(absolute_old_name)[0], new_name)

        def cb_rename(result):
            if not report_script_result(result, title + ' Error', 'Could not rename {0}'.format(type_name)):
                return

            name_item.setText(new_name)

            if self.tree_files.header().sortIndicatorSection() == 0:
                self.tree_files.header().setSortIndicator(0, self.tree_files.header().sortIndicatorOrder())

        self.script_manager.execute_script('rename', cb_rename,
                                           [absolute_old_name, absolute_new_name])
Example #3
0
    def add_class_path_entry(self):
        dialog = ExpandingInputDialog(self)
        dialog.setModal(True)
        dialog.setWindowTitle('Add Class Path Entry')
        dialog.setLabelText('Enter/Choose new class path entry:')
        dialog.setOkButtonText('Add')
        dialog.setComboBoxItems(self.class_path_candidates)
        dialog.setComboBoxEditable(True)
        dialog.setTextValue('')

        if dialog.exec_() != QDialog.Accepted:
            return

        entry = dialog.textValue()

        if len(entry) == 0:
            QMessageBox.critical(get_main_window(),
                                 'Add Class Path Entry Error',
                                 'A valid class path entry cannot be empty.')
            return

        self.class_path_list_editor.add_item(entry, select_item=True)
Example #4
0
    def new_config(self):
        dialog = ExpandingInputDialog(get_main_window())
        dialog.setModal(True)
        dialog.setWindowTitle('New Config File')
        dialog.setLabelText('Enter name for new openHAB config file:')
        dialog.setInputMode(QInputDialog.TextInput)
        dialog.setOkButtonText('Create')

        if dialog.exec_() != QDialog.Accepted:
            return

        name = dialog.textValue()

        # check that new name is valid as filename
        if len(name) == 0 or name == '.' or name == '..' or '/' in name:
            QMessageBox.critical(
                get_main_window(), 'New Config File Error',
                'A config file name cannot be empty, cannot be one dot [.], cannot be two dots [..] and cannot contain a forward slash [/].'
            )
            return

        endswith_items = name.endswith('.items')
        endswith_sitemap = name.endswith('.sitemap')
        endswith_rules = name.endswith('.rules')
        endswith_persist = name.endswith('.persist')
        endswith_script = name.endswith('.script')
        endswith_transform = name.endswith('.transform')

        if not endswith_items and not endswith_sitemap and not endswith_rules and not endswith_persist and not endswith_script and not endswith_transform:
            QMessageBox.critical(
                get_main_window(), 'New Config File Error',
                'A config file name has to end with .items, .sitemap, .rules, .persist, .script or .transform.'
            )
            return

        if name in [
                '.items', '.sitemap', '.rules', '.persist', '.script',
                '.transform'
        ]:
            QMessageBox.critical(
                get_main_window(), 'New Config File Error',
                '.items, .sitemap, .rules, .persist, .script and .transform cannot be used as config file names.'
            )
            return

        if endswith_items:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'items', name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2', 'items',
                                             name)
        elif endswith_sitemap:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'sitemaps',
                                             name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2',
                                             'sitemaps', name)
        elif endswith_rules:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'rules', name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2', 'rules',
                                             name)
        elif endswith_persist:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'persistence',
                                             name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2',
                                             'persistence', name)
        elif endswith_script:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'scripts', name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2', 'scripts',
                                             name)
        elif endswith_transform:
            if self.image_version.number < (1, 10):
                target_path = posixpath.join('/', 'etc', 'openhab',
                                             'configurations', 'transform',
                                             name)
            else:
                target_path = posixpath.join('/', 'etc', 'openhab2',
                                             'transform', name)

        def cb_open(red_file):
            red_file.release()

            def select_new():
                index = -1

                for config in self.configs:
                    if config != None and config.display_name == name:
                        index = config.index

                if index >= 0:
                    self.combo_config.setCurrentIndex(index)

            self.refresh_all_configs(select_new)

        def cb_open_error(error):
            if isinstance(error, REDError
                          ) and error.error_code == REDError.E_ALREADY_EXISTS:
                QMessageBox.critical(
                    get_main_window(), 'New Config File Error',
                    'Config file {0} already exists.'.format(name))
            else:
                QMessageBox.critical(
                    get_main_window(), 'New Config File Error',
                    'Could not create config file {0}:\n\n{1}'.format(
                        name, error))

        async_call(REDFile(self.session).open,
                   (target_path, REDFile.FLAG_WRITE_ONLY | REDFile.FLAG_CREATE
                    | REDFile.FLAG_EXCLUSIVE, 0o644, 0, 0),
                   cb_open,
                   cb_open_error,
                   pass_exception_to_error_callback=True)
    def new_config(self):
        dialog = ExpandingInputDialog(get_main_window())
        dialog.setModal(True)
        dialog.setWindowTitle('New Config File')
        dialog.setLabelText('Enter name for new openHAB config file:')
        dialog.setInputMode(QInputDialog.TextInput)
        dialog.setOkButtonText('Create')

        if dialog.exec_() != QDialog.Accepted:
            return

        name = dialog.textValue()

        # check that new name is valid as filename
        if len(name) == 0 or name == '.' or name == '..' or '/' in name:
            QMessageBox.critical(get_main_window(), 'New Config File Error',
                                 'A config file name cannot be empty, cannot be one dot [.], cannot be two dots [..] and cannot contain a forward slash [/].')
            return

        endswith_items   = name.endswith('.items')
        endswith_sitemap = name.endswith('.sitemap')
        endswith_rules   = name.endswith('.rules')

        if not endswith_items and not endswith_sitemap and not endswith_rules:
            QMessageBox.critical(get_main_window(), 'New Config File Error',
                                 'A config file name has to end with .items, .sitemap or .rules.')
            return

        if name in ['.items', '.sitemap', '.rules']:
            QMessageBox.critical(get_main_window(), 'New Config File Error',
                                 '.items, .sitemap and .rules cannot be used as config file names.')
            return

        if endswith_items:
            target_path = posixpath.join('/', 'etc', 'openhab', 'configurations', 'items', name)
        elif endswith_sitemap:
            target_path = posixpath.join('/', 'etc', 'openhab', 'configurations', 'sitemaps', name)
        elif endswith_rules:
            target_path = posixpath.join('/', 'etc', 'openhab', 'configurations', 'rules', name)

        def cb_open(red_file):
            red_file.release()

            def select_new():
                index = -1

                for config in self.configs:
                    if config != None and config.display_name == name:
                        index = config.index

                if index >= 0:
                    self.combo_config.setCurrentIndex(index)

            self.refresh_all_configs(select_new)

        def cb_open_error(error):
            if isinstance(error, REDError) and error.error_code == REDError.E_ALREADY_EXISTS:
                QMessageBox.critical(get_main_window(), 'New Config File Error',
                                     'Config file {0} already exists.'.format(name))
            else:
                QMessageBox.critical(get_main_window(), 'New Config File Error',
                                     'Could not create config file {0}:\n\n{1}'.format(name, error))

        async_call(REDFile(self.session).open,
                   (target_path, REDFile.FLAG_WRITE_ONLY | REDFile.FLAG_CREATE | REDFile.FLAG_EXCLUSIVE, 0o644, 0, 0),
                   cb_open, cb_open_error, report_exception=True)
    def new_config(self):
        dialog = ExpandingInputDialog(get_main_window())
        dialog.setModal(True)
        dialog.setWindowTitle("New Config File")
        dialog.setLabelText("Enter name for new openHAB config file:")
        dialog.setInputMode(QInputDialog.TextInput)
        dialog.setOkButtonText("Create")

        if dialog.exec_() != QDialog.Accepted:
            return

        name = dialog.textValue()

        # check that new name is valid as filename
        if len(name) == 0 or name == "." or name == ".." or "/" in name:
            QMessageBox.critical(
                get_main_window(),
                "New Config File Error",
                "A config file name cannot be empty, cannot be one dot [.], cannot be two dots [..] and cannot contain a forward slash [/].",
            )
            return

        endswith_items = name.endswith(".items")
        endswith_sitemap = name.endswith(".sitemap")
        endswith_rules = name.endswith(".rules")
        endswith_persist = name.endswith(".persist")
        endswith_script = name.endswith(".script")
        endswith_transform = name.endswith(".transform")

        if (
            not endswith_items
            and not endswith_sitemap
            and not endswith_rules
            and not endswith_persist
            and not endswith_script
            and not endswith_transform
        ):
            QMessageBox.critical(
                get_main_window(),
                "New Config File Error",
                "A config file name has to end with .items, .sitemap, .rules, .persist, .script or .transform.",
            )
            return

        if name in [".items", ".sitemap", ".rules", ".persist", ".script", ".transform"]:
            QMessageBox.critical(
                get_main_window(),
                "New Config File Error",
                ".items, .sitemap, .rules, .persist, .script and .transform cannot be used as config file names.",
            )
            return

        if endswith_items:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "items", name)
        elif endswith_sitemap:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "sitemaps", name)
        elif endswith_rules:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "rules", name)
        elif endswith_persist:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "persistence", name)
        elif endswith_script:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "scripts", name)
        elif endswith_transform:
            target_path = posixpath.join("/", "etc", "openhab", "configurations", "transform", name)

        def cb_open(red_file):
            red_file.release()

            def select_new():
                index = -1

                for config in self.configs:
                    if config != None and config.display_name == name:
                        index = config.index

                if index >= 0:
                    self.combo_config.setCurrentIndex(index)

            self.refresh_all_configs(select_new)

        def cb_open_error(error):
            if isinstance(error, REDError) and error.error_code == REDError.E_ALREADY_EXISTS:
                QMessageBox.critical(
                    get_main_window(), "New Config File Error", "Config file {0} already exists.".format(name)
                )
            else:
                QMessageBox.critical(
                    get_main_window(),
                    "New Config File Error",
                    "Could not create config file {0}:\n\n{1}".format(name, error),
                )

        async_call(
            REDFile(self.session).open,
            (target_path, REDFile.FLAG_WRITE_ONLY | REDFile.FLAG_CREATE | REDFile.FLAG_EXCLUSIVE, 0o644, 0, 0),
            cb_open,
            cb_open_error,
            report_exception=True,
        )