Example #1
0
 def load_themes(self):
     """
     Loads the theme lists and triggers updates across the whole system using direct calls or core functions and
     events for the plugins.
     The plugins will call back in to get the real list if they want it.
     """
     self.theme_list = []
     self.theme_list_widget.clear()
     files = AppLocation.get_files(self.settings_section, '.png')
     # Sort the themes by its name considering language specific
     files.sort(key=lambda file_name: get_locale_key(str(file_name)))
     # now process the file list of png files
     for name in files:
         # check to see file is in theme root directory
         theme = os.path.join(self.path, name)
         if os.path.exists(theme):
             text_name = os.path.splitext(name)[0]
             if text_name == self.global_theme:
                 name = translate('OpenLP.ThemeManager', '{name} (default)').format(name=text_name)
             else:
                 name = text_name
             thumb = os.path.join(self.thumb_path, '{name}.png'.format(name=text_name))
             item_name = QtWidgets.QListWidgetItem(name)
             if validate_thumb(theme, thumb):
                 icon = build_icon(thumb)
             else:
                 icon = create_thumb(theme, thumb)
             item_name.setIcon(icon)
             item_name.setData(QtCore.Qt.UserRole, text_name)
             self.theme_list_widget.addItem(item_name)
             self.theme_list.append(text_name)
     self._push_themes()
 def check_thumbnails(self):
     """
     Returns ``True`` if the thumbnail images exist and are more recent than the powerpoint file.
     """
     last_image = self.get_thumbnail_path(self.get_slide_count(), True)
     if not (last_image and os.path.isfile(last_image)):
         return False
     return validate_thumb(self.file_path, last_image)
Example #3
0
    def load_full_list(self, images, initial_load=False, open_group=None):
        """
        Replace the list of images and groups in the interface.

        ``images``
            A List of ImageFilenames objects that will be used to reload the mediamanager list.

        ``initial_load``
            When set to False, the busy cursor and progressbar will be shown while loading images.

        ``open_group``
            ImageGroups object of the group that must be expanded after reloading the list in the interface.
        """
        if not initial_load:
            self.application.set_busy_cursor()
            self.main_window.display_progress_bar(len(images))
        self.list_view.clear()
        # Load the list of groups and add them to the treeView.
        group_items = {}
        self.add_sub_groups(group_items, parent_group_id=0)
        if open_group is not None:
            self.expand_group(open_group.id)
        # Sort the images by its filename considering language specific.
        # characters.
        images.sort(key=lambda image_object: get_locale_key(os.path.split(str(image_object.filename))[1]))
        for imageFile in images:
            log.debug('Loading image: %s', imageFile.filename)
            filename = os.path.split(imageFile.filename)[1]
            thumb = os.path.join(self.servicePath, filename)
            if not os.path.exists(imageFile.filename):
                icon = build_icon(':/general/general_delete.png')
            else:
                if validate_thumb(imageFile.filename, thumb):
                    icon = build_icon(thumb)
                else:
                    icon = create_thumb(imageFile.filename, thumb)
            item_name = QtGui.QTreeWidgetItem(filename)
            item_name.setText(0, filename)
            item_name.setIcon(0, icon)
            item_name.setToolTip(0, imageFile.filename)
            item_name.setData(0, QtCore.Qt.UserRole, imageFile)
            if imageFile.group_id == 0:
                self.list_view.addTopLevelItem(item_name)
            else:
                group_items[imageFile.group_id].addChild(item_name)
            if not initial_load:
                self.main_window.increment_progress_bar()
        if not initial_load:
            self.main_window.finished_progress_bar()
        self.application.set_normal_cursor()
Example #4
0
    def test_validate_thumb_file_does_not_exist(self):
        """
        Test the validate_thumb() function when the thumbnail does not exist
        """
        # GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb
        with patch('openlp.core.lib.os') as mocked_os:
            file_path = 'path/to/file'
            thumb_path = 'path/to/thumb'
            mocked_os.path.exists.return_value = False

            # WHEN: we run the validate_thumb() function
            result = validate_thumb(file_path, thumb_path)

            # THEN: we should have called a few functions, and the result should be False
            mocked_os.path.exists.assert_called_with(thumb_path)
            assert result is False, 'The result should be False'
Example #5
0
    def validate_thumb_file_does_not_exist_test(self):
        """
        Test the validate_thumb() function when the thumbnail does not exist
        """
        # GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb
        with patch('openlp.core.lib.os') as mocked_os:
            file_path = 'path/to/file'
            thumb_path = 'path/to/thumb'
            mocked_os.path.exists.return_value = False

            # WHEN: we run the validate_thumb() function
            result = validate_thumb(file_path, thumb_path)

            # THEN: we should have called a few functions, and the result should be False
            mocked_os.path.exists.assert_called_with(thumb_path)
            assert result is False, 'The result should be False'
Example #6
0
    def load_full_list(self, images, initial_load=False, open_group=None):
        """
        Replace the list of images and groups in the interface.

        :param images: A List of Image Filenames objects that will be used to reload the mediamanager list.
        :param initial_load: When set to False, the busy cursor and progressbar will be shown while loading images.
        :param open_group: ImageGroups object of the group that must be expanded after reloading the list in the
            interface.
        """
        if not initial_load:
            self.application.set_busy_cursor()
            self.main_window.display_progress_bar(len(images))
        self.list_view.clear()
        # Load the list of groups and add them to the treeView.
        group_items = {}
        self.add_sub_groups(group_items, parent_group_id=0)
        if open_group is not None:
            self.expand_group(open_group.id)
        # Sort the images by its filename considering language specific.
        # characters.
        images.sort(key=lambda image_object: get_locale_key(
            os.path.split(str(image_object.filename))[1]))
        for image_file in images:
            log.debug('Loading image: %s', image_file.filename)
            filename = os.path.split(image_file.filename)[1]
            thumb = self.generate_thumbnail_path(image_file)
            if not os.path.exists(image_file.filename):
                icon = build_icon(':/general/general_delete.png')
            else:
                if validate_thumb(image_file.filename, thumb):
                    icon = build_icon(thumb)
                else:
                    icon = create_thumb(image_file.filename, thumb)
            item_name = QtWidgets.QTreeWidgetItem([filename])
            item_name.setText(0, filename)
            item_name.setIcon(0, icon)
            item_name.setToolTip(0, image_file.filename)
            item_name.setData(0, QtCore.Qt.UserRole, image_file)
            if image_file.group_id == 0:
                self.list_view.addTopLevelItem(item_name)
            else:
                group_items[image_file.group_id].addChild(item_name)
            if not initial_load:
                self.main_window.increment_progress_bar()
        if not initial_load:
            self.main_window.finished_progress_bar()
        self.application.set_normal_cursor()
Example #7
0
    def test_validate_thumb_file_exists_and_older(self):
        """
        Test the validate_thumb() function when the thumbnail exists but is older than the file
        """
        # GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
        file_path = MagicMock(**{'stat.return_value': MagicMock(st_mtime=10)})
        thumb_path = MagicMock(
            **{
                'exists.return_value': True,
                'stat.return_value': MagicMock(st_mtime=9)
            })

        # WHEN: we run the validate_thumb() function
        result = validate_thumb(file_path, thumb_path)

        # THEN: `validate_thumb` should return False
        thumb_path.stat.assert_called_once_with()
        assert result is False, 'The result should be False'
Example #8
0
    def test_validate_thumb_file_exists_and_newer(self):
        """
        Test the validate_thumb() function when the thumbnail exists and has a newer timestamp than the file
        """
        with patch.object(Path, 'exists'), patch.object(Path, 'stat'):
            # GIVEN: Mocked file_path and thumb_path which return different values fo the modified times
            file_path = MagicMock(
                **{'stat.return_value': MagicMock(st_mtime=10)})
            thumb_path = MagicMock(
                **{
                    'exists.return_value': True,
                    'stat.return_value': MagicMock(st_mtime=11)
                })

            # WHEN: we run the validate_thumb() function
            result = validate_thumb(file_path, thumb_path)

            # THEN: `validate_thumb` should return True
            assert result is True
Example #9
0
    def test_validate_thumb_file_exists_and_older(self):
        """
        Test the validate_thumb() function when the thumbnail exists but is older than the file
        """
        # GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb
        with patch('openlp.core.lib.os') as mocked_os:
            file_path = 'path/to/file'
            thumb_path = 'path/to/thumb'
            file_mocked_stat = MagicMock()
            file_mocked_stat.st_mtime = datetime.now()
            thumb_mocked_stat = MagicMock()
            thumb_mocked_stat.st_mtime = datetime.now() - timedelta(seconds=10)
            mocked_os.path.exists.return_value = True
            mocked_os.stat.side_effect = lambda fname: file_mocked_stat if fname == file_path else thumb_mocked_stat

            # WHEN: we run the validate_thumb() function
            result = validate_thumb(file_path, thumb_path)

            # THEN: we should have called a few functions, and the result should be False
            mocked_os.path.exists.assert_called_with(thumb_path)
            mocked_os.stat.assert_any_call(file_path)
            mocked_os.stat.assert_any_call(thumb_path)
            assert result is False, 'The result should be False'
Example #10
0
    def test_validate_thumb_file_exists_and_older(self):
        """
        Test the validate_thumb() function when the thumbnail exists but is older than the file
        """
        # GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb
        with patch('openlp.core.lib.os') as mocked_os:
            file_path = 'path/to/file'
            thumb_path = 'path/to/thumb'
            file_mocked_stat = MagicMock()
            file_mocked_stat.st_mtime = datetime.now()
            thumb_mocked_stat = MagicMock()
            thumb_mocked_stat.st_mtime = datetime.now() - timedelta(seconds=10)
            mocked_os.path.exists.return_value = True
            mocked_os.stat.side_effect = lambda fname: file_mocked_stat if fname == file_path else thumb_mocked_stat

            # WHEN: we run the validate_thumb() function
            result = validate_thumb(file_path, thumb_path)

            # THEN: we should have called a few functions, and the result should be False
            mocked_os.path.exists.assert_called_with(thumb_path)
            mocked_os.stat.assert_any_call(file_path)
            mocked_os.stat.assert_any_call(thumb_path)
            assert result is False, 'The result should be False'
Example #11
0
 def load_list(self, files, target_group=None, initial_load=False):
     """
     Add presentations into the media manager. This is called both on initial load of the plugin to populate with
     existing files, and when the user adds new files via the media manager.
     """
     current_list = self.get_file_list()
     titles = [os.path.split(file)[1] for file in current_list]
     self.application.set_busy_cursor()
     if not initial_load:
         self.main_window.display_progress_bar(len(files))
     # Sort the presentations by its filename considering language specific characters.
     files.sort(key=lambda filename: get_locale_key(
         os.path.split(str(filename))[1]))
     for file in files:
         if not initial_load:
             self.main_window.increment_progress_bar()
         if current_list.count(file) > 0:
             continue
         filename = os.path.split(file)[1]
         if not os.path.exists(file):
             item_name = QtWidgets.QListWidgetItem(filename)
             item_name.setIcon(build_icon(ERROR_IMAGE))
             item_name.setData(QtCore.Qt.UserRole, file)
             item_name.setToolTip(file)
             self.list_view.addItem(item_name)
         else:
             if titles.count(filename) > 0:
                 if not initial_load:
                     critical_error_message_box(
                         translate('PresentationPlugin.MediaItem',
                                   'File Exists'),
                         translate(
                             'PresentationPlugin.MediaItem',
                             'A presentation with that filename already exists.'
                         ))
                 continue
             controller_name = self.find_controller_by_type(filename)
             if controller_name:
                 controller = self.controllers[controller_name]
                 doc = controller.add_document(file)
                 thumb = os.path.join(doc.get_thumbnail_folder(),
                                      'icon.png')
                 preview = doc.get_thumbnail_path(1, True)
                 if not preview and not initial_load:
                     doc.load_presentation()
                     preview = doc.get_thumbnail_path(1, True)
                 doc.close_presentation()
                 if not (preview and os.path.exists(preview)):
                     icon = build_icon(':/general/general_delete.png')
                 else:
                     if validate_thumb(preview, thumb):
                         icon = build_icon(thumb)
                     else:
                         icon = create_thumb(preview, thumb)
             else:
                 if initial_load:
                     icon = build_icon(':/general/general_delete.png')
                 else:
                     critical_error_message_box(
                         UiStrings().UnsupportedFile,
                         translate(
                             'PresentationPlugin.MediaItem',
                             'This type of presentation is not supported.'))
                     continue
             item_name = QtWidgets.QListWidgetItem(filename)
             item_name.setData(QtCore.Qt.UserRole, file)
             item_name.setIcon(icon)
             item_name.setToolTip(file)
             self.list_view.addItem(item_name)
     if not initial_load:
         self.main_window.finished_progress_bar()
     self.application.set_normal_cursor()
Example #12
0
 def load_list(self, files, target_group=None, initial_load=False):
     """
     Add presentations into the media manager. This is called both on initial load of the plugin to populate with
     existing files, and when the user adds new files via the media manager.
     """
     current_list = self.get_file_list()
     titles = [os.path.split(file)[1] for file in current_list]
     self.application.set_busy_cursor()
     if not initial_load:
         self.main_window.display_progress_bar(len(files))
     # Sort the presentations by its filename considering language specific characters.
     files.sort(key=lambda filename: get_locale_key(os.path.split(str(filename))[1]))
     for file in files:
         if not initial_load:
             self.main_window.increment_progress_bar()
         if current_list.count(file) > 0:
             continue
         filename = os.path.split(str(file))[1]
         if not os.path.exists(file):
             item_name = QtGui.QListWidgetItem(filename)
             item_name.setIcon(build_icon(ERROR_IMAGE))
             item_name.setData(QtCore.Qt.UserRole, file)
             item_name.setToolTip(file)
             self.list_view.addItem(item_name)
         else:
             if titles.count(filename) > 0:
                 if not initial_load:
                     critical_error_message_box(translate('PresentationPlugin.MediaItem', 'File Exists'),
                         translate('PresentationPlugin.MediaItem',
                             'A presentation with that filename already exists.')
                         )
                 continue
             controller_name = self.findControllerByType(filename)
             if controller_name:
                 controller = self.controllers[controller_name]
                 doc = controller.add_document(str(file))
                 thumb = os.path.join(doc.get_thumbnail_folder(), 'icon.png')
                 preview = doc.get_thumbnail_path(1, True)
                 if not preview and not initial_load:
                     doc.load_presentation()
                     preview = doc.get_thumbnail_path(1, True)
                 doc.close_presentation()
                 if not (preview and os.path.exists(preview)):
                     icon = build_icon(':/general/general_delete.png')
                 else:
                     if validate_thumb(preview, thumb):
                         icon = build_icon(thumb)
                     else:
                         icon = create_thumb(preview, thumb)
             else:
                 if initial_load:
                     icon = build_icon(':/general/general_delete.png')
                 else:
                     critical_error_message_box(UiStrings().UnsupportedFile,
                         translate('PresentationPlugin.MediaItem', 'This type of presentation is not supported.'))
                     continue
             item_name = QtGui.QListWidgetItem(filename)
             item_name.setData(QtCore.Qt.UserRole, file)
             item_name.setIcon(icon)
             item_name.setToolTip(file)
             self.list_view.addItem(item_name)
     if not initial_load:
         self.main_window.finished_progress_bar()
     self.application.set_normal_cursor()
Example #13
0
    def load_list(self, file_paths, target_group=None, initial_load=False):
        """
        Add presentations into the media manager. This is called both on initial load of the plugin to populate with
        existing files, and when the user adds new files via the media manager.

        :param list[pathlib.Path] file_paths: List of file paths to add to the media manager.
        """
        current_paths = self.get_file_list()
        titles = [file_path.name for file_path in current_paths]
        self.application.set_busy_cursor()
        if not initial_load:
            self.main_window.display_progress_bar(len(file_paths))
        # Sort the presentations by its filename considering language specific characters.
        file_paths.sort(key=lambda file_path: get_natural_key(file_path.name))
        for file_path in file_paths:
            if not initial_load:
                self.main_window.increment_progress_bar()
            if current_paths.count(file_path) > 0:
                continue
            file_name = file_path.name
            if not file_path.exists():
                item_name = QtWidgets.QListWidgetItem(file_name)
                item_name.setIcon(UiIcons().delete)
                item_name.setData(QtCore.Qt.UserRole, file_path)
                item_name.setToolTip(str(file_path))
                self.list_view.addItem(item_name)
            else:
                if titles.count(file_name) > 0:
                    if not initial_load:
                        critical_error_message_box(
                            translate('PresentationPlugin.MediaItem',
                                      'File Exists'),
                            translate(
                                'PresentationPlugin.MediaItem',
                                'A presentation with that filename already exists.'
                            ))
                    continue
                controller_name = self.find_controller_by_type(file_path)
                if controller_name:
                    controller = self.controllers[controller_name]
                    doc = controller.add_document(file_path)
                    thumbnail_path = doc.get_thumbnail_folder() / 'icon.png'
                    preview_path = doc.get_thumbnail_path(1, True)
                    if not preview_path and not initial_load:
                        doc.load_presentation()
                        preview_path = doc.get_thumbnail_path(1, True)
                    doc.close_presentation()
                    if not (preview_path and preview_path.exists()):
                        icon = UiIcons().delete
                    else:
                        if validate_thumb(preview_path, thumbnail_path):
                            icon = build_icon(thumbnail_path)
                        else:
                            icon = create_thumb(preview_path, thumbnail_path)
                else:
                    if initial_load:
                        icon = UiIcons().delete
                    else:
                        critical_error_message_box(
                            UiStrings().UnsupportedFile,
                            translate(
                                'PresentationPlugin.MediaItem',
                                'This type of presentation is not supported.'))
                        continue
                item_name = QtWidgets.QListWidgetItem(file_name)
                item_name.setData(QtCore.Qt.UserRole, file_path)
                item_name.setIcon(icon)
                item_name.setToolTip(str(file_path))
                self.list_view.addItem(item_name)
        if not initial_load:
            self.main_window.finished_progress_bar()
        self.application.set_normal_cursor()