コード例 #1
0
class StagedMoviesMenu(object):
    ''' Provide windows for displaying staged movies,
    and tools for managing the items '''

    #TODO: don't commit sql changes for "... all" until end
    #TODO: decorator for "...all" commands
    #TODO: load staged movies on init, use as instance variable, refresh as needed

    def __init__(self):
        self.dbh = DatabaseHandler()

    @staticmethod
    @utils.logged_function
    def add_all(items):
        ''' Add all staged movies to library '''
        STR_ADDING_ALL_MOVIES = utils.ADDON.getLocalizedString(32042)
        STR_ALL_MOVIES_ADDED = utils.ADDON.getLocalizedString(32043)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_ADDING_ALL_MOVIES)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.add_to_library()
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIES_ADDED)

    @staticmethod
    @utils.logged_function
    def add_all_with_metadata(items):
        ''' Add all movies with nfo files to the library '''
        # TODO: Remove code duplication with MovieItem.add_to_library_if_metadata
        STR_ADDING_ALL_MOVIES_WITH_METADATA = utils.ADDON.getLocalizedString(
            32044)
        STR_ALL_MOVIES_WITH_METADTA_ADDED = utils.ADDON.getLocalizedString(
            32045)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_ADDING_ALL_MOVIES_WITH_METADATA)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            nfo_path = os.path.join(item.metadata_dir,
                                    item.clean_title + '.nfo')
            if os.path.exists(nfo_path):
                progress_dialog.update(percent, line2=item.title)
                item.add_to_library()
            progress_dialog.update(percent, line2=' ')
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIES_WITH_METADTA_ADDED)

    @staticmethod
    @utils.logged_function
    def clean_up_metadata():
        ''' Remove all unused metadata '''
        STR_MOVIE_METADATA_CLEANED = utils.ADDON.getLocalizedString(32136)
        metadata_dir = os.path.join(utils.METADATA_FOLDER, 'Movies')
        for folder in os.listdir(metadata_dir):
            full_path = os.path.join(metadata_dir, folder)
            if os.path.isdir(full_path):
                folder_contents = os.listdir(full_path)
                if len(folder_contents) == 1 and fnmatch(
                        folder_contents[0], '*.strm'):
                    utils.log_msg(
                        'Removing metadata folder {}'.format(full_path),
                        loglevel=xbmc.LOGNOTICE)
                shutil.rmtree(full_path)
        utils.notification(STR_MOVIE_METADATA_CLEANED)

    @staticmethod
    @utils.logged_function
    def generate_all_metadata(items):
        ''' Generate metadata items for all staged movies '''
        STR_GENERATING_ALL_MOVIE_METADATA = utils.ADDON.getLocalizedString(
            32046)
        STR_ALL_MOVIE_METADTA_CREATED = utils.ADDON.getLocalizedString(32047)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_GENERATING_ALL_MOVIE_METADATA)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.create_metadata_item()
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIE_METADTA_CREATED)

    @staticmethod
    def rename_dialog(item):
        ''' Prompt input for new name, and rename if non-empty string '''
        #TODO: move to utils or parent class so it's not duplicated
        input_ret = xbmcgui.Dialog().input("Title", defaultt=item.title)
        if input_ret:
            item.rename(input_ret)

    @utils.logged_function
    def options(self, item):
        ''' Provide options for a single staged movie in a dialog window '''
        #TODO: add a back button
        STR_ADD = utils.ADDON.getLocalizedString(32048)
        STR_REMOVE = utils.ADDON.getLocalizedString(32017)
        STR_REMOVE_AND_BLOCK = utils.ADDON.getLocalizedString(32049)
        STR_RENAME = utils.ADDON.getLocalizedString(32050)
        STR_AUTOMATICALLY_RENAME_USING_METADTA = utils.ADDON.getLocalizedString(
            32051)
        STR_GENERATE_METADATA_ITEM = utils.ADDON.getLocalizedString(32052)
        STR_STAGED_MOVIE_OPTIONS = utils.ADDON.getLocalizedString(32053)
        lines = [
            STR_ADD,
            STR_REMOVE,
            STR_REMOVE_AND_BLOCK,
            #STR_RENAME, STR_AUTOMATICALLY_RENAME_USING_METADTA,
            STR_GENERATE_METADATA_ITEM
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME,
                                     STR_STAGED_MOVIE_OPTIONS, item.title),
            lines)
        if ret >= 0:
            if lines[ret] == STR_ADD:
                item.add_to_library()
                self.view_all()
            elif lines[ret] == STR_REMOVE:
                item.delete()
                self.view_all()
            elif lines[ret] == STR_REMOVE_AND_BLOCK:
                item.remove_and_block()
                self.view_all()
            elif lines[ret] == STR_RENAME:
                self.rename_dialog(item)
                self.options(item)
            elif lines[ret] == STR_GENERATE_METADATA_ITEM:
                item.create_metadata_item()
                self.options(item)
            elif lines[ret] == STR_AUTOMATICALLY_RENAME_USING_METADTA:
                item.rename_using_metadata()
                self.options(item)
        else:
            self.view_all()

    @utils.logged_function
    def remove_all(self):
        ''' Remove all staged movies '''
        STR_REMOVING_ALL_MOVIES = utils.ADDON.getLocalizedString(32013)
        STR_ALL_MOVIES_REMOVED = utils.ADDON.getLocalizedString(32014)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_MOVIES)
        self.dbh.remove_all_content_items('staged', 'movie')
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIES_REMOVED)

    @utils.logged_function
    def view_all(self):
        ''' Display all staged movies, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_STAGED_MOVIES = utils.ADDON.getLocalizedString(32037)
        STR_ADD_ALL_MOVIES = utils.ADDON.getLocalizedString(32038)
        STR_ADD_ALL_MOVIES_WITH_METADATA = utils.ADDON.getLocalizedString(
            32039)
        STR_REMOVE_ALL_MOVIES = utils.ADDON.getLocalizedString(32009)
        STR_GENERATE_ALL_METADATA_ITEMS = utils.ADDON.getLocalizedString(32040)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_STAGED_MOVIES = utils.ADDON.getLocalizedString(32041)
        STR_CLEAN_UP_METADATA = utils.ADDON.getLocalizedString(32135)
        staged_movies = self.dbh.get_content_items(status='staged',
                                                   mediatype='movie',
                                                   order='Title')
        if not staged_movies:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_STAGED_MOVIES)
            return
        lines = [str(x) for x in staged_movies]
        lines += [
            STR_ADD_ALL_MOVIES, STR_ADD_ALL_MOVIES_WITH_METADATA,
            STR_REMOVE_ALL_MOVIES, STR_GENERATE_ALL_METADATA_ITEMS,
            STR_CLEAN_UP_METADATA, STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_STAGED_MOVIES), lines)
        if ret >= 0:
            if ret < len(staged_movies):  # staged item
                for i, item in enumerate(staged_movies):
                    if ret == i:
                        self.options(item)
                        break
            elif lines[ret] == STR_ADD_ALL_MOVIES:
                self.add_all(staged_movies)
            elif lines[ret] == STR_ADD_ALL_MOVIES_WITH_METADATA:
                self.add_all_with_metadata(staged_movies)
                self.view_all()
            elif lines[ret] == STR_REMOVE_ALL_MOVIES:
                self.remove_all()
            elif lines[ret] == STR_GENERATE_ALL_METADATA_ITEMS:
                self.generate_all_metadata(staged_movies)
                self.view_all()
            elif lines[ret] == STR_CLEAN_UP_METADATA:
                self.clean_up_metadata()
                self.view_all()
            elif lines[ret] == STR_BACK:
                return
コード例 #2
0
class StagedTVMenu(object):
    ''' Provide windows for displaying staged tvshows and episodes,
    and tools for managing the items '''
    def __init__(self):
        self.dbh = DatabaseHandler()

    @staticmethod
    @utils.logged_function
    def add_all_episodes(items):
        ''' Add all episodes from specified show to library '''
        STR_ADDING_ALL_x_EPISODES = utils.getlocalizedstring(32071)
        STR_ALL_x_EPISODES_ADDED = utils.getlocalizedstring(32072)
        show_title = items[0].show_title

        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_ADDING_ALL_x_EPISODES % show_title)

        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.add_to_library()
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_ADDED % show_title)

    @utils.logged_function
    def add_all_seasons(self, show_title):
        ''' Add all episodes from specified show to library '''
        STR_ADDING_ALL_x_SEASONS = 'Adding all %s seasons...'
        STR_ALL_x_SEASONS_ADDED = 'All %s seasons added'

        staged_seasons = self.dbh.get_content_items(status='staged',
                                                    mediatype='tvshow',
                                                    order='Show_Title',
                                                    show_title=show_title)

        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_ADDING_ALL_x_SEASONS % show_title)

        for index, item in enumerate(staged_seasons):
            percent = 100 * index / len(staged_seasons)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(100)
            item.add_to_library()
        progress_dialog.close()
        utils.notification(STR_ALL_x_SEASONS_ADDED % show_title)

    @utils.logged_function
    def add_all_seasons_with_metadata(self, show_title):
        ''' Add all seasons in the specified show with metadata to the library '''
        STR_ADDING_ALL_x_SEASONS_WITH_METADATA = 'Adding all %s seasons with metadata...'
        STR_ALL_x_SEASONS_WITH_METADATA_ADDED = 'All %s seasons with metadata added'
        staged_seasons = self.dbh.get_content_items(status='staged',
                                                    mediatype='tvshow',
                                                    order='Show_Title',
                                                    show_title=show_title)

        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(
            utils.ADDON_NAME,
            STR_ADDING_ALL_x_SEASONS_WITH_METADATA % show_title)
        for index, item in enumerate(staged_seasons):
            percent = 100 * index / len(staged_seasons)

            if os.path.exists(item.episode_nfo[0]):
                progress_dialog.update(percent,
                                       line2=item.show_title,
                                       line3=item.episode_title_with_id)
                xbmc.sleep(200)
                item.add_to_library()

            progress_dialog.update(percent, line2=' ', line3=' ')
        progress_dialog.close()
        utils.notification(STR_ALL_x_SEASONS_WITH_METADATA_ADDED % show_title)

    @staticmethod
    @utils.logged_function
    def add_all_episodes_with_metadata(staged_episodes, show_title):
        ''' Add all episodes in the specified show with metadata to the library '''
        STR_ADDING_ALL_x_EPISODES_WITH_METADATA = utils.getlocalizedstring(
            32073)
        STR_ALL_x_EPISODES_WITH_METADATA_ADDED = utils.getlocalizedstring(
            32074)
        # show_title = items[0]

        # progress_dialog = xbmcgui.DialogProgress()
        # progress_dialog.create(
        #     utils.ADDON_NAME, STR_ADDING_ALL_x_EPISODES_WITH_METADATA % show_title
        # )
        # for index, item in enumerate(items):
        #     percent = 100 * index / len(items)
        #     # nfo_path = os.path.join(metadata_dir, item.clean_title + '.nfo')
        #     if os.path.exists(item.episode_nfo[0]):
        #         progress_dialog.update(
        #             percent,
        #             line2=item.show_title,
        #             line3=item.episode_title_with_id
        #         )
        #         xbmc.sleep(200)
        #         item.add_to_library()
        #     progress_dialog.update(percent, line2=' ', line3=' ')
        # progress_dialog.close()
        # utils.notification(STR_ALL_x_EPISODES_WITH_METADATA_ADDED % show_title)

    @staticmethod
    @utils.logged_function
    def generate_all_episodes_metadata(items):
        ''' Generate metadata items for all episodes in show '''
        STR_GENERATING_ALL_x_METADATA = utils.getlocalizedstring(32077)
        STR_ALL_x_METADATA_CREATED = utils.getlocalizedstring(32078)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_GENERATING_ALL_x_METADATA % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.create_metadata_item()
        progress_dialog.close()
        utils.notification(STR_ALL_x_METADATA_CREATED % show_title)

    @utils.logged_function
    def generate_all_seasons_metadata(self, show_title):
        ''' Generate metadata items for all seasons in show '''
        STR_GENERATING_ALL_x_METADATA = 'Generating all %s metadata...'
        STR_ALL_x_METADATA_CREATED = 'All %s metadata created'

        staged_seasons = self.dbh.get_content_items(status='staged',
                                                    mediatype='tvshow',
                                                    order='Show_Title',
                                                    show_title=show_title)

        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_GENERATING_ALL_x_METADATA % show_title)

        for index, item in enumerate(staged_seasons):
            percent = 100 * index / len(staged_seasons)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.create_metadata_item()

        progress_dialog.close()
        utils.notification(STR_ALL_x_METADATA_CREATED % show_title)

    @staticmethod
    def rename_dialog(item):
        ''' Prompt input for new name, and rename if non-empty string '''
        input_ret = xbmcgui.Dialog().input("Title", defaultt=item.show_title)
        if input_ret:
            item.rename(input_ret)

    @staticmethod
    @utils.logged_function
    def rename_episodes_using_metadata(items):
        ''' Rename all episodes in show using nfo files '''
        STR_RENAMING_x_EPISODES_USING_METADATA = utils.getlocalizedstring(
            32075)
        STR_x_EPISODES_RENAMED_USING_METADATA = utils.getlocalizedstring(32076)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(
            utils.ADDON_NAME,
            STR_RENAMING_x_EPISODES_USING_METADATA % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            # TODO: fix rename_using_metadata() current is not used
            # item.rename_using_metadata()
        progress_dialog.close()
        utils.notification(STR_x_EPISODES_RENAMED_USING_METADATA % show_title)

    @utils.logged_function
    def add_all_shows(self):
        ''' Add all tvshow items to library '''
        STR_ADDING_ALL_TV_SHOWS = utils.getlocalizedstring(32059)
        STR_ALL_TV_SHOWS_ADDED = utils.getlocalizedstring(32060)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_ADDING_ALL_TV_SHOWS)
        staged_tv_items = self.dbh.get_content_items(status='staged',
                                                     mediatype='tvshow',
                                                     order='Show_Title')
        for index, item in enumerate(staged_tv_items):
            percent = 100 * index / len(staged_tv_items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.add_to_library()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOWS_ADDED)

    @utils.logged_function
    def add_all_with_metadata(self):
        ''' Add all tvshow items with nfo file to library'''
        STR_ADDING_ALL_TV_SHOW_ITEMS_WITH_METADATA = utils.getlocalizedstring(
            32061)
        STR_ALL_TV_SHOW_ITEMS_WITH_METADATA_ADDED = utils.getlocalizedstring(
            32062)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_ADDING_ALL_TV_SHOW_ITEMS_WITH_METADATA)

        staged_tv_items = self.dbh.get_content_items(status='staged',
                                                     mediatype='tvshow',
                                                     order='Show_Title')
        # content menaget precisa retornar episode_nfo[0]
        for index, item in enumerate(staged_tv_items):
            percent = 100 * index / len(staged_tv_items)
            if os.path.exists(item.episode_nfo[0]):
                progress_dialog.update(percent,
                                       line2=item.show_title,
                                       line3=item.episode_title_with_id)
                xbmc.sleep(200)
                item.add_to_library()
            progress_dialog.update(percent, line2=' ', line3=' ')
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOW_ITEMS_WITH_METADATA_ADDED)

    @utils.logged_function
    def generate_all_metadata(self):
        ''' Create metadata for all staged tvshow items '''
        STR_GENERATING_ALL_TV_SHOW_METADATA = utils.getlocalizedstring(32063)
        STR_ALL_TV_SHOW_METADATA_CREATED = utils.getlocalizedstring(32064)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_GENERATING_ALL_TV_SHOW_METADATA)
        staged_tv_items = self.dbh.get_content_items(status='staged',
                                                     mediatype='tvshow',
                                                     order='Show_Title')
        for index, item in enumerate(staged_tv_items):
            percent = 100 * index / len(staged_tv_items)
            progress_dialog.update(percent, line2=item.show_title)
            xbmc.sleep(200)
            item.create_metadata_item()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOW_METADATA_CREATED)

    @utils.logged_function
    def read_all_metadata(self):
        ''' Read metadata for all staged tvshow items '''
        STR_READING_ALL_TV_SHOW_METADATA = utils.getlocalizedstring(32145)
        STR_ALL_TV_SHOW_METADATA_READ = utils.getlocalizedstring(32146)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_READING_ALL_TV_SHOW_METADATA)
        staged_tv_items = self.dbh.get_content_items(status='staged',
                                                     mediatype='tvshow',
                                                     order='Show_Title')
        for index, item in enumerate(staged_tv_items):
            percent = 100 * index / len(staged_tv_items)
            progress_dialog.update(percent, line2=item.show_title)
            xbmc.sleep(200)
            # item.read_metadata_item()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOW_METADATA_READ)

    @utils.logged_function
    def remove_all(self):
        ''' Remove all staged tvshow items '''
        STR_REMOVING_ALL_TV_SHOWS = utils.getlocalizedstring(32024)
        STR_ALL_TV_SHOW_REMOVED = utils.getlocalizedstring(32025)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_TV_SHOWS)
        self.dbh.remove_from(status='staged',
                             mediatype='tvshow',
                             show_title=None,
                             directory=None)
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOW_REMOVED)

    @utils.logged_function
    def remove_all_seasons(self, show_title):
        ''' Remove all seasons from the specified show '''
        STR_REMOVING_ALL_x_SEASONS = utils.getlocalizedstring(
            32032) % show_title
        STR_ALL_x_SEASONS_REMOVED = utils.getlocalizedstring(
            32033) % show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_x_SEASONS)
        self.dbh.remove_from(status='staged',
                             mediatype='tvshow',
                             show_title=show_title,
                             directory=None)
        progress_dialog.close()
        utils.notification(STR_ALL_x_SEASONS_REMOVED)

    @utils.logged_function
    def remove_all_episodes(self, show_title):
        ''' Remove all episodes from the specified show '''
        STR_REMOVING_ALL_x_EPISODES = utils.getlocalizedstring(
            32032) % show_title
        STR_ALL_x_EPISODES_REMOVED = utils.getlocalizedstring(
            32033) % show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_x_EPISODES)
        self.dbh.remove_from(status='staged',
                             mediatype='tvshow',
                             show_title=show_title,
                             directory=None)
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_REMOVED)

    @utils.logged_function
    def remove_and_block_show(self, show_title):
        ''' Remove all seasons from specified show from the library,
        delete metadata, and add to blocked list '''
        # Remove from staged
        self.remove_all_seasons(show_title)
        # Delete metadata folder
        clean_show_title = utils.clean_name(show_title)
        metadata_dir = os.path.join(utils.METADATA_FOLDER, 'TV',
                                    clean_show_title)
        utils.fs.remove_dir(metadata_dir)
        # Add show title to blocked
        self.dbh.add_blocked_item(show_title, 'tvshow')

    @utils.logged_function
    def episode_options(self, item, season_number):
        ''' Provide options for a single staged episode in a dialog window '''
        #TODO: rename associated metadata when renaming
        #TODO: rename show title
        #TODO: remove item (including metadata)
        STR_ADD = utils.getlocalizedstring(32048)
        STR_REMOVE = utils.getlocalizedstring(32017)
        STR_REMOVE_AND_BLOCK_EPISODE = utils.getlocalizedstring(32079)
        STR_RENAME = utils.getlocalizedstring(32050)
        STR_AUTOMATICALLY_RENAME_USING_METADTA = utils.getlocalizedstring(
            32051)
        STR_GENERATE_METADATA_ITEM = utils.getlocalizedstring(32052)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_STAGED_EPISODE_OPTIONS = utils.getlocalizedstring(32080)
        lines = [
            STR_ADD, STR_REMOVE, STR_REMOVE_AND_BLOCK_EPISODE, STR_RENAME,
            STR_AUTOMATICALLY_RENAME_USING_METADTA, STR_GENERATE_METADATA_ITEM,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME,
                                     STR_STAGED_EPISODE_OPTIONS,
                                     item.show_title), lines)
        if ret >= 0:
            if lines[ret] == STR_ADD:
                item.add_to_library()
                self.view_episodes(item.show_title, season_number)
            elif lines[ret] == STR_REMOVE:
                item.delete()
                self.view_episodes(item.show_title, season_number)
            elif lines[ret] == STR_REMOVE_AND_BLOCK_EPISODE:
                item.remove_and_block()
                self.view_episodes(item.show_title, season_number)
            elif lines[ret] == STR_RENAME:
                self.rename_dialog(item)
                self.episode_options(item, season_number)
            elif lines[ret] == STR_GENERATE_METADATA_ITEM:
                item.create_metadata_item()
                self.episode_options(item, season_number)
            elif lines[ret] == STR_AUTOMATICALLY_RENAME_USING_METADTA:
                item.rename_using_metadata()
                self.episode_options(item, season_number)
            elif lines[ret] == STR_BACK:
                self.view_episodes(item.show_title, season_number)
        else:
            self.view_episodes(item.show_title, season_number)

    @utils.logged_function
    def view_episodes(self, show_title, season_number):
        ''' Display all staged episodes in the specified show,
        which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_STAGED_x_EPISODES = utils.getlocalizedstring(32065) % show_title
        STR_ADD_ALL_EPISODES = utils.getlocalizedstring(32066)
        STR_ADD_ALL_EPISODES_WITH_METADATA = utils.getlocalizedstring(32067)
        STR_REMOVE_ALL_EPISODES = utils.getlocalizedstring(32029)
        STR_REMOVE_AND_BLOCK_TV_SHOW = utils.getlocalizedstring(32068)
        STR_AUTOMATICALLY_RENAME_ALL_EPISODES_USING_METADATA = utils.getlocalizedstring(
            32069)
        STR_GENERATE_ALL_METADATA_ITEMS = utils.getlocalizedstring(32040)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_STAGED_x_EPISODES = utils.getlocalizedstring(32070) % show_title
        staged_episodes = self.dbh.get_content_items(
            status='staged',
            mediatype='tvshow',
            order='Show_Title',
            show_title=show_title,
            season_number=season_number)
        if not staged_episodes:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_STAGED_x_EPISODES)
            self.view_shows()
            return
        lines = [str(x) for x in staged_episodes]
        lines += [
            STR_ADD_ALL_EPISODES, STR_ADD_ALL_EPISODES_WITH_METADATA,
            STR_REMOVE_ALL_EPISODES, STR_REMOVE_AND_BLOCK_TV_SHOW,
            STR_AUTOMATICALLY_RENAME_ALL_EPISODES_USING_METADATA,
            STR_GENERATE_ALL_METADATA_ITEMS, STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_STAGED_x_EPISODES), lines)
        if ret >= 0:
            if ret < len(staged_episodes):  # staged item
                for i, item in enumerate(staged_episodes):
                    if ret == i:
                        self.episode_options(item, season_number)
                        break
            elif lines[ret] == STR_ADD_ALL_EPISODES:
                self.add_all_episodes(staged_episodes)
                self.view_shows()
            elif lines[ret] == STR_ADD_ALL_EPISODES_WITH_METADATA:
                self.add_all_episodes_with_metadata(staged_episodes,
                                                    show_title)
                self.view_episodes(show_title, season_number)
            elif lines[ret] == STR_REMOVE_ALL_EPISODES:
                self.remove_all_episodes(show_title)
                self.view_shows()
            elif lines[ret] == STR_REMOVE_AND_BLOCK_TV_SHOW:
                self.remove_and_block_show(show_title)
                self.view_shows()
            elif lines[
                    ret] == STR_AUTOMATICALLY_RENAME_ALL_EPISODES_USING_METADATA:
                self.rename_episodes_using_metadata(staged_episodes)
                self.view_episodes(show_title, season_number)
            elif lines[ret] == STR_GENERATE_ALL_METADATA_ITEMS:
                self.generate_all_episodes_metadata(staged_episodes)
                self.view_episodes(show_title, season_number)
            elif lines[ret] == STR_BACK:
                self.view_seasons(show_title)
        else:
            self.view_seasons(show_title)

    @utils.logged_function
    def view_seasons(self, show_title):
        ''' Display all staged seasons in the specified show,
        which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_STAGED_x_SEASONS = 'No staged %s seasons' % show_title
        STR_ADD_ALL_SEASONS = 'Add all seasons'
        STR_ADD_ALL_SEASONS_WITH_METADATA = 'Add all seasons with metadata'
        STR_REMOVE_ALL_SEASONS = 'Remove all seasons'
        STR_REMOVE_AND_BLOCK_TV_SHOW = utils.getlocalizedstring(32068)
        STR_AUTOMATICALLY_RENAME_ALL_SEASONS_USING_METADATA = utils.getlocalizedstring(
            32069)
        STR_GENERATE_ALL_METADATA_ITEMS = utils.getlocalizedstring(32040)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_STAGED_x_SEASONS = 'Staged %s seasons' % show_title
        staged_seasons = self.dbh.get_content_items(status='staged',
                                                    mediatype='tvshow',
                                                    order='Season',
                                                    show_title=show_title)
        if not staged_seasons:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_STAGED_x_SEASONS)
            self.view_shows()
            return

        lines = [str('[B]Season %s[/B]' % x) for x in staged_seasons]
        lines += [
            STR_ADD_ALL_SEASONS, STR_ADD_ALL_SEASONS_WITH_METADATA,
            STR_REMOVE_ALL_SEASONS, STR_REMOVE_AND_BLOCK_TV_SHOW,
            STR_AUTOMATICALLY_RENAME_ALL_SEASONS_USING_METADATA,
            STR_GENERATE_ALL_METADATA_ITEMS, STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_STAGED_x_SEASONS), lines)
        selection = lines[ret]
        if ret >= 0:
            if selection == STR_ADD_ALL_SEASONS:
                self.add_all_seasons(show_title)
                self.view_shows()
            elif selection == STR_ADD_ALL_SEASONS_WITH_METADATA:
                self.add_all_seasons_with_metadata(show_title)
                self.view_seasons(show_title)
            elif selection == STR_REMOVE_ALL_SEASONS:
                self.remove_all_seasons(show_title)
                self.view_shows()
            elif selection == STR_REMOVE_AND_BLOCK_TV_SHOW:
                self.remove_and_block_show(show_title)
                self.view_shows()
            elif (selection ==
                  STR_AUTOMATICALLY_RENAME_ALL_SEASONS_USING_METADATA):
                self.rename_seasons_using_metadata(staged_seasons)
                self.view_seasons(show_title)
            elif selection == STR_GENERATE_ALL_METADATA_ITEMS:
                self.generate_all_seasons_metadata(show_title)
                self.view_seasons(show_title)
            elif selection == STR_BACK:
                self.view_shows()
            else:  # staged item
                self.view_episodes(show_title,
                                   season_number=''.join(
                                       filter(str.isdigit, selection)))
        else:
            self.view_shows()

    @utils.logged_function
    def view_shows(self):
        ''' Display all managed tvshows, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_STAGED_TV_SHOWS = utils.getlocalizedstring(32054)
        STR_ADD_ALL_TV_SHOWS = utils.getlocalizedstring(32055)
        STR_ADD_ALL_ITEMS_WITH_METADTA = utils.getlocalizedstring(32056)
        STR_REMOVE_ALL_TV_SHOWS = utils.getlocalizedstring(32057)
        STR_GENERATE_ALL_METADATA_ITEMS = utils.getlocalizedstring(32040)
        STR_READ_ALL_METADATA_ITEMS = utils.getlocalizedstring(32147)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_STAGED_TV_SHOWS = utils.getlocalizedstring(32058)
        staged_tvshows = self.dbh.get_all_shows('staged')
        if not staged_tvshows:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_STAGED_TV_SHOWS)
            return
        lines = ['[B]{}[/B]'.format(x) for x in staged_tvshows]
        lines += [
            STR_ADD_ALL_TV_SHOWS, STR_ADD_ALL_ITEMS_WITH_METADTA,
            STR_REMOVE_ALL_TV_SHOWS, STR_GENERATE_ALL_METADATA_ITEMS,
            STR_READ_ALL_METADATA_ITEMS, STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_STAGED_TV_SHOWS), lines)
        if ret >= 0:
            if ret < len(staged_tvshows):  # staged item
                for show_title in staged_tvshows:
                    if staged_tvshows[ret] == show_title:
                        self.view_seasons(show_title)
                        break
            elif lines[ret] == STR_ADD_ALL_TV_SHOWS:
                self.add_all_shows()
            elif lines[ret] == STR_ADD_ALL_ITEMS_WITH_METADTA:
                self.add_all_with_metadata()
                self.view_shows()
            elif lines[ret] == STR_REMOVE_ALL_TV_SHOWS:
                self.remove_all()
            elif lines[ret] == STR_GENERATE_ALL_METADATA_ITEMS:
                self.generate_all_metadata()
                self.view_shows()
            elif lines[ret] == STR_READ_ALL_METADATA_ITEMS:
                self.read_all_metadata()
                self.view_shows()
            elif lines[ret] == STR_BACK:
                return
コード例 #3
0
class ManagedTVMenu(object):
    ''' Provide windows for displaying managed shows and episodes,
    and tools for manipulating the objects and managed file '''
    def __init__(self):
        self.dbh = DatabaseHandler()

    @staticmethod
    @utils.logged_function
    def move_episodes_to_staged(items):
        ''' Remove all managed episodes in specified show from library, and add them to staged '''
        STR_MOVING_ALL_x_EPISODES_BACK_TO_STAGED = utils.getlocalizedstring(
            32034)
        STR_ALL_x_EPISODES_MOVED_TO_STAGED = utils.getlocalizedstring(32035)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(
            utils.ADDON_NAME,
            STR_MOVING_ALL_x_EPISODES_BACK_TO_STAGED % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_MOVED_TO_STAGED % show_title)

    @utils.logged_function
    def move_all_seasons_to_staged(self, show_title):
        ''' Remove all managed episodes in specified show from library, and add them to staged '''
        STR_MOVING_ALL_x_SEASONS_BACK_TO_STAGED = 'Movendo temporadas de %s para staged'
        STR_ALL_x_SEASONS_MOVED_TO_STAGED = 'Todas as temporadas de %s movidas para staged'
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(
            utils.ADDON_NAME,
            STR_MOVING_ALL_x_SEASONS_BACK_TO_STAGED % show_title)
        items = self.dbh.get_content_items(status='managed',
                                           mediatype='tvshow',
                                           show_title=show_title,
                                           order='Show_Title')
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            xbmc.sleep(200)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_x_SEASONS_MOVED_TO_STAGED % show_title)

    @utils.logged_function
    def move_all_to_staged(self):
        ''' Remove all managed tvshow items from library, and add them to staged '''
        STR_MOVING_ALL_TV_SHOWS_BACK_TO_STAGED = utils.getlocalizedstring(
            32026)
        STR_ALL_TV_SHOWS_MOVED_TO_STAGED = utils.getlocalizedstring(32027)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_MOVING_ALL_TV_SHOWS_BACK_TO_STAGED)

        managed_tv_items = self.dbh.get_content_items(status='managed',
                                                      mediatype='tvshow',
                                                      order='Show_Title')
        for index, item in enumerate(managed_tv_items):
            percent = 100 * index / len(managed_tv_items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)

            xbmc.sleep(200)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOWS_MOVED_TO_STAGED)

    @staticmethod
    @utils.logged_function
    def remove_episodes(items):
        ''' Remove all episodes in specified show from library '''
        STR_REMOVING_ALL_x_EPISODES = utils.getlocalizedstring(32032)
        STR_ALL_x_EPISODES_REMOVED = utils.getlocalizedstring(32033)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_REMOVING_ALL_x_EPISODES % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)
            item.remove_from_library()
            item.delete()
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_REMOVED % show_title)

    @utils.logged_function
    def remove_seasons(self, items, show_title):
        ''' Remove all seasons in specified show from library '''
        STR_REMOVING_ALL_X_SEASONS = 'Removendo temporadas de: %s'
        STR_ALL_X_SEASONS_REMOVED = 'Todas as temporadas de %s, foram removidas'
        seasons = items[0]
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_REMOVING_ALL_X_SEASONS % show_title)

        for season_number in seasons:
            percent = 100 * season_number / len(seasons)
            progress_dialog.update(percent,
                                   line2=show_title,
                                   line3=str("Season: %s" % season_number))
            self.dbh.remove_from(mediatype='tvshow',
                                 show_title=show_title,
                                 season=season_number)
            xbmc.sleep(300)
        progress_dialog.close()
        utils.notification(STR_ALL_X_SEASONS_REMOVED % show_title)

    @utils.logged_function
    def remove_all(self):
        ''' Remove all managed tvshow items from library '''
        STR_REMOVING_ALL_TV_SHOWS = utils.getlocalizedstring(32024)
        STR_ALL_TV_SHOWS_REMOVED = utils.getlocalizedstring(32025)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_TV_SHOWS)
        managed_tv_items = self.dbh.get_content_items(status='managed',
                                                      mediatype='tvshow',
                                                      order='Show_Title')
        for index, item in enumerate(managed_tv_items):
            percent = 100 * index / len(managed_tv_items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.episode_title_with_id)

            item.remove_from_library()
            item.delete()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOWS_REMOVED)

    @utils.logged_function
    def episode_options(self, item, season_number):
        ''' Provide options for a single managed episode in a dialog window '''
        STR_REMOVE = utils.getlocalizedstring(32017)
        STR_MOVE_BACK_TO_STAGED = utils.getlocalizedstring(32018)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_MANAGED_EPISODE_OPTIONS = utils.getlocalizedstring(32036)
        lines = [STR_REMOVE, STR_MOVE_BACK_TO_STAGED, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME,
                                     STR_MANAGED_EPISODE_OPTIONS,
                                     item.episode_title_with_id), lines)
        if ret >= 0:
            if lines[ret] == STR_REMOVE:
                item.remove_from_library()
                item.delete()
                return self.view_episodes(item.show_title, season_number)
            elif lines[ret] == STR_MOVE_BACK_TO_STAGED:
                item.remove_from_library()
                item.set_as_staged()
                return self.view_episodes(item.show_title, season_number)
            elif lines[ret] == STR_BACK:
                return self.view_episodes(item.show_title, season_number)
        return self.view_episodes(item.show_title, season_number)

    @utils.logged_function
    def view_episodes(self, show_title, season_number):
        ''' Displays all managed episodes in the specified show,
        which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_MANAGED_x_EPISODES = utils.getlocalizedstring(
            32028) % show_title
        STR_REMOVE_ALL_EPISODES = utils.getlocalizedstring(32029)
        STR_MOVE_ALL_EPISODES_BACK_TO_STAGED = utils.getlocalizedstring(32030)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_MANAGED_x_EPISODES = utils.getlocalizedstring(32031) % show_title
        managed_episodes = self.dbh.get_content_items(
            status='managed',
            mediatype='tvshow',
            order='Show_Title',
            show_title=show_title,
            season_number=season_number)
        if not managed_episodes:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_x_EPISODES)
            return self.view_shows()
        lines = [str(x) for x in managed_episodes]
        lines += [
            STR_REMOVE_ALL_EPISODES, STR_MOVE_ALL_EPISODES_BACK_TO_STAGED,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_x_EPISODES),
            lines)
        if ret >= 0:
            if ret < len(managed_episodes):  # managed item
                for i, item in enumerate(managed_episodes):
                    if ret == i:
                        return self.episode_options(item, season_number)
            elif lines[ret] == STR_REMOVE_ALL_EPISODES:
                self.remove_episodes(managed_episodes)
                return self.view_shows()
            elif lines[ret] == STR_MOVE_ALL_EPISODES_BACK_TO_STAGED:
                self.move_episodes_to_staged(managed_episodes)
                return self.view_shows()
            elif lines[ret] == STR_BACK:
                return self.view_seasons(show_title)
        return self.view_seasons(show_title)

    @utils.logged_function
    def view_seasons(self, show_title):
        ''' Displays all managed seasons in the specified show,
        which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        # TODO: functions to remove all or add all if necessary
        STR_NO_MANAGED_X_SEASONS = str('No managed %s seasons') % show_title
        STR_REMOVE_ALL_SEASONS = 'Remove all seasons'
        STR_MOVE_ALL_SEASONS_BACK_TO_STAGED = 'Move all seasons back to staged'
        STR_BACK = utils.getlocalizedstring(32011)
        STR_MANAGED_X_SEASONS = str('Managed %s Seasons') % show_title
        managed_seasons = self.dbh.get_content_items(status='managed',
                                                     mediatype='tvshow',
                                                     order='Season',
                                                     show_title=show_title)
        if not managed_seasons:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_X_SEASONS)
            return self.view_shows()
        lines = [str('[B]Season %s[/B]' % x) for x in managed_seasons]
        lines += [
            STR_REMOVE_ALL_SEASONS, STR_MOVE_ALL_SEASONS_BACK_TO_STAGED,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_X_SEASONS), lines)
        selection = lines[ret]
        if ret >= 0:
            if selection == STR_REMOVE_ALL_SEASONS:
                # TODO: remove by title only
                self.remove_seasons(managed_seasons, show_title)
                return self.view_shows()
            elif selection == STR_MOVE_ALL_SEASONS_BACK_TO_STAGED:
                self.move_all_seasons_to_staged(show_title)
                return self.view_shows()
            elif selection == STR_BACK:
                self.view_shows()
                return self.view_shows()
            else:  # managed item
                return self.view_episodes(show_title=show_title,
                                          season_number=''.join(
                                              filter(str.isdigit, selection)))
        return self.view_shows()

    @utils.logged_function
    def view_shows(self):
        ''' Display all managed tvshows, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_MANAGED_TV_SHOWS = utils.getlocalizedstring(32020)
        STR_REMOVE_ALL_TV_SHOWS = utils.getlocalizedstring(32021)
        STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED = utils.getlocalizedstring(32022)
        STR_BACK = utils.getlocalizedstring(32011)
        STR_MANAGED_TV_SHOWS = utils.getlocalizedstring(32023)
        managed_tvshows = self.dbh.get_all_shows('managed')
        if not managed_tvshows:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_TV_SHOWS)
            return
        lines = ['[B]{}[/B]'.format(x) for x in managed_tvshows]
        lines += [
            STR_REMOVE_ALL_TV_SHOWS, STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_TV_SHOWS), lines)
        if ret >= 0:
            if ret < len(managed_tvshows):
                for show_title in managed_tvshows:
                    if managed_tvshows[ret] == show_title:
                        self.view_seasons(show_title)
                        break
            elif lines[ret] == STR_REMOVE_ALL_TV_SHOWS:
                self.remove_all()
            elif lines[ret] == STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED:
                self.move_all_to_staged()
            elif lines[ret] == STR_BACK:
                return
コード例 #4
0
class SyncedMenu(object):
    ''' Provides windows for displaying synced directories,
    and tools for managing them and updating their contents '''

    # IDEA: new "find all directories" context item that finds and consolidates directories

    def __init__(self):
        self.dbh = DatabaseHandler()

    def filter_blocked_items(self, items, mediatype):
        ''' Filters out all blocked items in the list '''
        return [x for x in items if not self.dbh.check_blocked(x['label'], mediatype)]

    @utils.logged_function
    def find_items_to_stage(self, all_items):
        ''' Find items in the list not present in database '''
        items_to_stage = []
        for dir_item in all_items:
            path = dir_item['file']
            # Don't prepare items that are already staged or managed
            if self.dbh.path_exists(path):
                continue
            label = dir_item['label']
            mediatype = dir_item['mediatype']
            if mediatype == 'movie':
                item = (path, label, mediatype)
            elif mediatype == 'tvshow':
                item = (path, label, mediatype, dir_item['show_title'])
            items_to_stage.append(item)
        return items_to_stage

    @utils.logged_function
    def find_paths_to_remove(self, all_paths, **kwargs):
        ''' Find paths in database no longer available '''
        managed_items = self.dbh.get_content_items(**kwargs)
        return [x.path for x in managed_items if x.path not in all_paths]

    @utils.logged_function
    def get_movies_in_directory(self, directory):
        ''' Get all movies in the directory and tags them '''
        dir_items = self.filter_blocked_items(
            utils.load_directory_items(directory, recursive=True), 'movie'
        )
        for item in dir_items:
            # Add tag to items
            item['mediatype'] = 'movie'
        return dir_items

    @utils.logged_function
    def get_single_tvshow(self, directory, show_title):
        ''' Get the single TV show in the directory, and tag the items'''
        show_items = self.filter_blocked_items(
            utils.load_directory_items(directory, recursive=True), 'episode'
        )
        for item in show_items:
            # Add tag to items
            item['mediatype'] = 'tvshow'
            item['show_title'] = show_title
        return show_items

    @utils.logged_function
    def get_tvshows_in_directory(self, directory):
        ''' Get all TV shows in the directory, and tag the items '''
        dir_items = self.filter_blocked_items(
            utils.load_directory_items(directory, allow_directories=True), 'tvshow'
        )
        all_items = []
        # Check every tvshow in list
        for dir_item in dir_items:
            show_title = dir_item['label']
            # Load results if show isn't blocked
            show_path = dir_item['file']
            show_items = self.filter_blocked_items(
                utils.load_directory_items(show_path, recursive=True), 'episode'
            )
            for show_item in show_items:
                # Add formatted item
                show_item['mediatype'] = 'tvshow'
                show_item['show_title'] = show_title
            all_items += show_items
        return all_items

    def options(self, item):
        ''' Provide options for a single synced directory in a dialog window '''
        # TODO: Remove all from plugin
        # TODO: Rename label
        STR_REMOVE = utils.ADDON.getLocalizedString(32017)
        STR_SYNCED_DIR_OPTIONS = utils.ADDON.getLocalizedString(32085)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        lines = [STR_REMOVE, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME, STR_SYNCED_DIR_OPTIONS, item['label']), lines
        )
        if ret >= 0:
            if lines[ret] == STR_REMOVE:
                self.dbh.remove_synced_dir(item['dir'])
            elif lines[ret] == STR_BACK:
                pass
        self.view()

    def remove_all(self):
        ''' Remove all synced directories '''
        STR_REMOVE_ALL_SYNCED_DIRS = utils.ADDON.getLocalizedString(32086)
        STR_ALL_SYNCED_DIRS_REMOVED = utils.ADDON.getLocalizedString(32087)
        STR_ARE_YOU_SURE = utils.ADDON.getLocalizedString(32088)
        if xbmcgui.Dialog().yesno('{0} - {1}'.format(utils.ADDON_NAME, STR_REMOVE_ALL_SYNCED_DIRS),
                                  STR_ARE_YOU_SURE):
            self.dbh.remove_all_synced_dirs()
            utils.notification(STR_ALL_SYNCED_DIRS_REMOVED)

    def remove_paths(self, paths_to_remove):
        ''' Remove and delete all items with the given paths '''
        for path in paths_to_remove:
            item = self.dbh.load_item(path)
            item.remove_from_library()
            item.delete()

    def stage_items(self, items_to_stage):
        ''' Stage all items in the list '''
        for item in items_to_stage:
            self.dbh.add_content_item(*item)

    @utils.logged_function
    def sync_movie_directory(self, dir_label, dir_path):
        ''' Sync movie directory and stage items '''
        STR_GETTING_ITEMS_IN_DIR = utils.ADDON.getLocalizedString(32125)
        STR_i_MOVIES_STAGED = utils.ADDON.getLocalizedString(32111)

        p_dialog = xbmcgui.DialogProgress()
        p_dialog.create(utils.ADDON_NAME)

        try:
            # Add synced directory to database
            self.dbh.add_synced_dir(dir_label, dir_path, 'movie')

            # Query json-rpc to get files in directory
            p_dialog.update(0, line1=STR_GETTING_ITEMS_IN_DIR)
            dir_items = utils.load_directory_items(dir_path, recursive=True)

            # Loop through all items and get titles and paths and stage them
            items_to_stage = 0
            for index, dir_item in enumerate(dir_items):
                # Get label & path for item
                item_label = dir_item['label']
                item_path = dir_item['file']
                if self.dbh.path_exists(item_path) or self.dbh.check_blocked(item_label, 'movie'):
                    continue
                # Update progress
                percent = 100 * index / len(dir_items)
                p_dialog.update(percent, line2=item_label)
                # Add item to database
                self.dbh.add_content_item(item_path, item_label, 'movie')
                items_to_stage += 1
                p_dialog.update(percent, line2=' ')
            utils.notification(STR_i_MOVIES_STAGED % items_to_stage)
        finally:
            p_dialog.close()

    @utils.logged_function
    def sync_single_movie(self, label, path):
        ''' Sync single movie path and stage item '''
        STR_ITEM_IS_ALREADY_STAGED = utils.ADDON.getLocalizedString(32103)
        STR_ITEM_IS_ALREADY_MANAGED = utils.ADDON.getLocalizedString(32104)
        STR_MOVIE_STAGED = utils.ADDON.getLocalizedString(32105)
        # Add synced directory to database
        self.dbh.add_synced_dir(label, path, 'single-movie')
        # Check for duplicate in database
        if self.dbh.path_exists(path, 'staged'):
            utils.notification(STR_ITEM_IS_ALREADY_STAGED)
        elif self.dbh.path_exists(path, 'managed'):
            utils.notification(STR_ITEM_IS_ALREADY_MANAGED)
        else:
            # Add item to database
            self.dbh.add_content_item(path, label, 'movie')
            utils.notification(STR_MOVIE_STAGED)

    @utils.logged_function
    def sync_single_tvshow(self, show_label, show_path):
        ''' Sync single tvshow directory and stage items '''
        STR_i_NEW_i_STAGED_i_MANAGED = utils.ADDON.getLocalizedString(32106)
        STR_i_NEW = utils.ADDON.getLocalizedString(32107)
        # Add synced directory to database
        self.dbh.add_synced_dir(show_label, show_path, 'single-tvshow')
        # Get everything inside tvshow path
        dir_items = utils.load_directory_items(show_path, recursive=True)
        # Get all items to stage
        items_to_stage = 0
        num_already_staged = 0
        num_already_managed = 0
        for dir_item in dir_items:
            item_label = dir_item['label']
            item_path = dir_item['file']
            if self.dbh.path_exists(item_path, 'staged'):
                num_already_staged += 1
                continue
            elif self.dbh.path_exists(item_path, 'managed'):
                num_already_managed += 1
                continue
            elif self.dbh.check_blocked(item_label, 'episode'):
                continue
            self.dbh.add_content_item(item_path, item_label, 'tvshow', show_label)
            items_to_stage += 1
        if num_already_staged > 0 or num_already_managed > 0:
            utils.notification(
                STR_i_NEW_i_STAGED_i_MANAGED %
                (items_to_stage, num_already_staged, num_already_managed)
            )
        else:
            utils.notification(STR_i_NEW % items_to_stage)

    @utils.logged_function
    def sync_tvshow_directory(self, dir_label, dir_path):
        ''' Sync all TV shows in directory and stage items'''
        STR_GETTING_ITEMS_IN_DIR = utils.ADDON.getLocalizedString(32125)
        STR_GETTING_ITEMS_IN_x = utils.ADDON.getLocalizedString(32126)
        STR_i_EPISODES_STAGED = utils.ADDON.getLocalizedString(32112)

        p_dialog = xbmcgui.DialogProgress()
        p_dialog.create(utils.ADDON_NAME)

        try:
            # add synced directory to database
            self.dbh.add_synced_dir(dir_label, dir_path, 'tvshow')

            # query json-rpc to get files in directory
            p_dialog.update(0, line1=STR_GETTING_ITEMS_IN_DIR)
            dir_items = utils.load_directory_items(dir_path, allow_directories=True)

            items_to_stage = 0
            for index, dir_item in enumerate(dir_items):
                # Get name of show and skip if blocked
                tvshow_label = dir_item['label']
                if self.dbh.check_blocked(tvshow_label, 'tvshow'):
                    continue
                # Update progress
                percent = 100 * index / len(dir_items)
                p_dialog.update(percent, line1=(STR_GETTING_ITEMS_IN_x % tvshow_label))
                # Get everything inside tvshow path
                tvshow_path = dir_item['file']
                show_items = utils.load_directory_items(tvshow_path, recursive=True)
                # Get all items to stage in show
                for show_item in show_items:
                    # Check for duplicate paths and blocked items
                    if self.dbh.path_exists(show_item['file']) or self.dbh.check_blocked(
                            show_item['label'], 'episode'):
                        continue
                    # Update progress
                    p_dialog.update(percent, line2=show_item['label'])
                    self.dbh.add_content_item(
                        show_item['file'], show_item['label'], 'tvshow', tvshow_label
                    )
                    items_to_stage += 1
                    p_dialog.update(percent, line2=' ')
                p_dialog.update(percent, line1=' ')
            utils.notification(STR_i_EPISODES_STAGED % items_to_stage)
        finally:
            p_dialog.close()

    def update_all(self):
        ''' Get all items from synced directories, and
        find unavailable items to remove from managed,
        and new items to stage '''
        # TODO: bugfix: single-movies won't actually get removed if they become unavailable
        #       maybe load parent dir and check for path or label?  it would be slower though
        # TODO: option to only update specified or managed items
        # TODO: option to add update frequencies for specific directories (i.e. weekly/monthly/etc.)
        # TODO: better error handling when plugins dont load during update
        STR_FINDING_ITEMS_TO_REMOVE = utils.ADDON.getLocalizedString(32090)
        STR_FINDING_ITEMS_TO_ADD = utils.ADDON.getLocalizedString(32092)
        STR_i_TO_REMOVE_i_TO_STAGE_PROCEED = utils.ADDON.getLocalizedString(32093)
        STR_REMOVING_ITEMS = utils.ADDON.getLocalizedString(32094)
        STR_STAGING_ITEMS = utils.ADDON.getLocalizedString(32095)
        STR_ALL_ITEMS_UPTODATE = utils.ADDON.getLocalizedString(32121)
        STR_SUCCESS = utils.ADDON.getLocalizedString(32122)

        p_dialog = xbmcgui.DialogProgressBG()
        p_dialog.create(utils.ADDON_NAME)

        try:
            # Get current items in all directories
            synced_dirs = self.dbh.get_synced_dirs()
            all_items = []
            for index, synced_dir in enumerate(synced_dirs):
                p_dialog.update(
                    percent=99 * index / len(synced_dirs),
                    message='{label} - {type}'.format(
                        label=synced_dir['label'], type=synced_dir.localize_type()
                    )
                )
                if synced_dir['type'] == 'single-movie':
                    # Directory is just a path to a single movie
                    all_items.append({
                        'file': synced_dir['dir'],
                        'label': synced_dir['label'],
                        'mediatype': 'movie'
                    })
                elif synced_dir['type'] == 'single-tvshow':
                    # Directory is a path to a tv show folder
                    all_items += self.get_single_tvshow(synced_dir['dir'], synced_dir['label'])
                elif synced_dir['type'] == 'movie':
                    # Directory is a path to list of movies
                    all_items += self.get_movies_in_directory(synced_dir['dir'])
                elif synced_dir['type'] == 'tvshow':
                    # Directory is a path to a list of tv shows
                    all_items += self.get_tvshows_in_directory(synced_dir['dir'])

            # Find managed paths not in dir_items, and prepare to remove
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_REMOVE)
            all_paths = [x['file'] for x in all_items]
            paths_to_remove = self.find_paths_to_remove(all_paths)

            # Find dir_items not in managed_items or staged_items, and prepare to add
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_ADD)
            items_to_stage = self.find_items_to_stage(all_items)

            # Prompt user to remove & stage
            if paths_to_remove or items_to_stage:
                if xbmcgui.Dialog().yesno(utils.ADDON_NAME, STR_i_TO_REMOVE_i_TO_STAGE_PROCEED %
                                          (len(paths_to_remove), len(items_to_stage))):
                    if paths_to_remove:
                        p_dialog.update(percent=99, message=STR_REMOVING_ITEMS)
                        self.remove_paths(paths_to_remove)
                    if items_to_stage:
                        p_dialog.update(percent=99, message=STR_STAGING_ITEMS)
                        self.stage_items(items_to_stage)
                    # TODO: update/clean managed folder
                    xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_SUCCESS)
            else:
                xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_ALL_ITEMS_UPTODATE)
        finally:
            p_dialog.close()

    def update_movies(self):
        ''' Update all synced movie directories '''
        STR_FINDING_ITEMS_TO_REMOVE = utils.ADDON.getLocalizedString(32090)
        STR_FINDING_ITEMS_TO_ADD = utils.ADDON.getLocalizedString(32092)
        STR_i_TO_REMOVE_i_TO_STAGE_PROCEED = utils.ADDON.getLocalizedString(32093)
        STR_REMOVING_ITEMS = utils.ADDON.getLocalizedString(32094)
        STR_STAGING_ITEMS = utils.ADDON.getLocalizedString(32095)
        STR_ALL_ITEMS_UPTODATE = utils.ADDON.getLocalizedString(32121)
        STR_SUCCESS = utils.ADDON.getLocalizedString(32122)

        p_dialog = xbmcgui.DialogProgressBG()
        p_dialog.create(utils.ADDON_NAME)

        try:
            all_items = []
            movie_dirs = self.dbh.get_synced_dirs(synced_type='movie')
            single_movie_dirs = self.dbh.get_synced_dirs(synced_type='single-movie')
            total_num_dirs = len(movie_dirs + single_movie_dirs)

            for index, synced_dir in enumerate(movie_dirs):
                p_dialog.update(percent=99 * index / total_num_dirs, message=synced_dir['label'])
                all_items += self.get_movies_in_directory(synced_dir['dir'])

            for index, synced_dir in enumerate(single_movie_dirs):
                p_dialog.update(
                    percent=99 * (index + len(movie_dirs)) / total_num_dirs,
                    message=synced_dir['label']
                )
                all_items.append({
                    'file': synced_dir['dir'],
                    'label': synced_dir['label'],
                    'mediatype': 'movie'
                })

            # Find managed paths not in dir_items, and prepare to remove
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_REMOVE)
            all_paths = [x['file'] for x in all_items]
            paths_to_remove = self.find_paths_to_remove(all_paths, mediatype='movie')

            # Find dir_items not in managed_items or staged_items, and prepare to add
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_ADD)
            items_to_stage = self.find_items_to_stage(all_items)

            # Prompt user to remove & stage
            if paths_to_remove or items_to_stage:
                if xbmcgui.Dialog().yesno(utils.ADDON_NAME, STR_i_TO_REMOVE_i_TO_STAGE_PROCEED %
                                          (len(paths_to_remove), len(items_to_stage))):
                    if paths_to_remove:
                        p_dialog.update(percent=99, message=STR_REMOVING_ITEMS)
                        self.remove_paths(paths_to_remove)
                    if items_to_stage:
                        p_dialog.update(percent=99, message=STR_STAGING_ITEMS)
                        self.stage_items(items_to_stage)
                    # TODO: update/clean managed folder
                    xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_SUCCESS)
            else:
                xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_ALL_ITEMS_UPTODATE)
        finally:
            p_dialog.close()

    def update_tvshows(self):
        ''' Update all TV show directories '''
        STR_FINDING_ITEMS_TO_REMOVE = utils.ADDON.getLocalizedString(32090)
        STR_FINDING_ITEMS_TO_ADD = utils.ADDON.getLocalizedString(32092)
        STR_i_TO_REMOVE_i_TO_STAGE_PROCEED = utils.ADDON.getLocalizedString(32093)
        STR_REMOVING_ITEMS = utils.ADDON.getLocalizedString(32094)
        STR_STAGING_ITEMS = utils.ADDON.getLocalizedString(32095)
        STR_ALL_ITEMS_UPTODATE = utils.ADDON.getLocalizedString(32121)
        STR_SUCCESS = utils.ADDON.getLocalizedString(32122)

        p_dialog = xbmcgui.DialogProgressBG()
        p_dialog.create(utils.ADDON_NAME)

        try:
            all_items = []
            show_dirs = self.dbh.get_synced_dirs(synced_type='tvshow')
            single_show_dirs = self.dbh.get_synced_dirs(synced_type='single-tvshow')
            total_num_dirs = len(show_dirs + single_show_dirs)

            for index, synced_dir in enumerate(show_dirs):
                p_dialog.update(percent=99 * index / total_num_dirs, message=synced_dir['label'])
                all_items += self.get_tvshows_in_directory(synced_dir['dir'])

            for index, synced_dir in enumerate(single_show_dirs):
                p_dialog.update(
                    percent=99. * (index + len(show_dirs)) / total_num_dirs,
                    message=synced_dir['label']
                )
                all_items += self.get_single_tvshow(synced_dir['dir'], synced_dir['label'])

            # Find managed paths not in dir_items, and prepare to remove
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_REMOVE)
            all_paths = [x['file'] for x in all_items]
            paths_to_remove = self.find_paths_to_remove(all_paths, mediatype='tvshow')

            # Find dir_items not in managed_items or staged_items, and prepare to add
            p_dialog.update(percent=99, message=STR_FINDING_ITEMS_TO_ADD)
            items_to_stage = self.find_items_to_stage(all_items)

            # Prompt user to remove & stage
            if paths_to_remove or items_to_stage:
                if xbmcgui.Dialog().yesno(utils.ADDON_NAME, STR_i_TO_REMOVE_i_TO_STAGE_PROCEED %
                                          (len(paths_to_remove), len(items_to_stage))):
                    if paths_to_remove:
                        p_dialog.update(percent=99, message=STR_REMOVING_ITEMS)
                        self.remove_paths(paths_to_remove)
                    if items_to_stage:
                        p_dialog.update(percent=99, message=STR_STAGING_ITEMS)
                        self.stage_items(items_to_stage)
                    # TODO: update/clean managed folder
                    xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_SUCCESS)
            else:
                xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_ALL_ITEMS_UPTODATE)
        finally:
            p_dialog.close()

    @utils.logged_function
    def view(self):
        ''' Display all synced directories, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_UPDATE_ALL = utils.ADDON.getLocalizedString(32081)
        STR_UPDATE_TV_SHOWS = utils.ADDON.getLocalizedString(32137)
        STR_UPDATE_MOVIES = utils.ADDON.getLocalizedString(32138)
        STR_REMOVE_ALL = utils.ADDON.getLocalizedString(32082)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_SYNCED_DIRECTORIES = utils.ADDON.getLocalizedString(32128)
        STR_NO_SYNCED_DIRS = utils.ADDON.getLocalizedString(32120)
        synced_dirs = self.dbh.get_synced_dirs()
        if not synced_dirs:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_SYNCED_DIRS)
            return
        lines = [
            '[B]%s[/B] - %s - [I]%s[/I]' % (x['label'], x.localize_type(), x['dir'])
            for x in synced_dirs
        ]
        lines += [STR_UPDATE_ALL, STR_UPDATE_MOVIES, STR_UPDATE_TV_SHOWS, STR_REMOVE_ALL, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_SYNCED_DIRECTORIES), lines
        )
        if ret >= 0:
            if ret < len(synced_dirs):
                for index, directory in enumerate(synced_dirs):
                    if ret == index:
                        self.options(directory)
                        break
            elif lines[ret] == STR_UPDATE_ALL:
                self.update_all()
                sys.exit()
            elif lines[ret] == STR_UPDATE_MOVIES:
                self.update_movies()
                sys.exit()
            elif lines[ret] == STR_UPDATE_TV_SHOWS:
                self.update_tvshows()
                sys.exit()
            elif lines[ret] == STR_REMOVE_ALL:
                self.remove_all()
            elif lines[ret] == STR_BACK:
                return
コード例 #5
0
class ManagedTVMenu(object):
    ''' Provide windows for displaying managed shows and episodes,
    and tools for manipulating the objects and managed file '''
    def __init__(self):
        self.dbh = DatabaseHandler()

    @staticmethod
    @utils.logged_function
    def move_episodes_to_staged(items):
        ''' Remove all managed episodes in specified show from library, and add them to staged '''
        STR_MOVING_ALL_x_EPISODES_BACK_TO_STAGED = utils.ADDON.getLocalizedString(
            32034)
        STR_ALL_x_EPISODES_MOVED_TO_STAGED = utils.ADDON.getLocalizedString(
            32035)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(
            utils.ADDON_NAME,
            STR_MOVING_ALL_x_EPISODES_BACK_TO_STAGED % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_MOVED_TO_STAGED % show_title)

    @staticmethod
    @utils.logged_function
    def remove_episodes(items):
        ''' Remove all episodes in specified show from library '''
        STR_REMOVING_ALL_x_EPISODES = utils.ADDON.getLocalizedString(32032)
        STR_ALL_x_EPISODES_REMOVED = utils.ADDON.getLocalizedString(32033)
        show_title = items[0].show_title
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_REMOVING_ALL_x_EPISODES % show_title)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.remove_from_library()
            item.delete()
        progress_dialog.close()
        utils.notification(STR_ALL_x_EPISODES_REMOVED % show_title)

    @utils.logged_function
    def episode_options(self, item):
        ''' Provide options for a single managed episode in a dialog window '''
        STR_REMOVE = utils.ADDON.getLocalizedString(32017)
        STR_MOVE_BACK_TO_STAGED = utils.ADDON.getLocalizedString(32018)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_MANAGED_EPISODE_OPTIONS = utils.ADDON.getLocalizedString(32036)
        lines = [STR_REMOVE, STR_MOVE_BACK_TO_STAGED, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME,
                                     STR_MANAGED_EPISODE_OPTIONS, item.title),
            lines)
        if ret >= 0:
            if lines[ret] == STR_REMOVE:
                item.remove_from_library()
                item.delete()
                return self.view_episodes(item.show_title)
            elif lines[ret] == STR_MOVE_BACK_TO_STAGED:
                item.remove_from_library()
                item.set_as_staged()
                return self.view_episodes(item.show_title)
            elif lines[ret] == STR_BACK:
                return self.view_episodes(item.show_title)
        return self.view_episodes(item.show_title)

    @utils.logged_function
    def move_all_to_staged(self):
        ''' Remove all managed tvshow items from library, and add them to staged '''
        STR_MOVING_ALL_TV_SHOWS_BACK_TO_STAGED = utils.ADDON.getLocalizedString(
            32026)
        STR_ALL_TV_SHOWS_MOVED_TO_STAGED = utils.ADDON.getLocalizedString(
            32027)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME,
                               STR_MOVING_ALL_TV_SHOWS_BACK_TO_STAGED)
        managed_tv_items = self.dbh.get_content_items(status='managed',
                                                      mediatype='tvshow',
                                                      order='Show_Title')
        for index, item in enumerate(managed_tv_items):
            percent = 100 * index / len(managed_tv_items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.title)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOWS_MOVED_TO_STAGED)

    @utils.logged_function
    def remove_all(self):
        ''' Remove all managed tvshow items from library '''
        STR_REMOVING_ALL_TV_SHOWS = utils.ADDON.getLocalizedString(32024)
        STR_ALL_TV_SHOWS_REMOVED = utils.ADDON.getLocalizedString(32025)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_TV_SHOWS)
        managed_tv_items = self.dbh.get_content_items(status='managed',
                                                      mediatype='tvshow',
                                                      order='Show_Title')
        for index, item in enumerate(managed_tv_items):
            percent = 100 * index / len(managed_tv_items)
            progress_dialog.update(percent,
                                   line2=item.show_title,
                                   line3=item.title)
            item.remove_from_library()
            item.delete()
        progress_dialog.close()
        utils.notification(STR_ALL_TV_SHOWS_REMOVED)

    @utils.logged_function
    def view_episodes(self, show_title):
        ''' Displays all managed episodes in the specified show,
        which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_MANAGED_x_EPISODES = utils.ADDON.getLocalizedString(
            32028) % show_title
        STR_REMOVE_ALL_EPISODES = utils.ADDON.getLocalizedString(32029)
        STR_MOVE_ALL_EPISODES_BACK_TO_STAGED = utils.ADDON.getLocalizedString(
            32030)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_MANAGED_x_EPISODES = utils.ADDON.getLocalizedString(
            32031) % show_title
        managed_episodes = self.dbh.get_content_items(status='managed',
                                                      show_title=show_title,
                                                      order='Title')
        if not managed_episodes:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_x_EPISODES)
            return self.view_shows()
        lines = [str(x) for x in managed_episodes]
        lines += [
            STR_REMOVE_ALL_EPISODES, STR_MOVE_ALL_EPISODES_BACK_TO_STAGED,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_x_EPISODES),
            lines)
        if ret >= 0:
            if ret < len(managed_episodes):  # managed item
                for i, item in enumerate(managed_episodes):
                    if ret == i:
                        return self.episode_options(item)
            elif lines[ret] == STR_REMOVE_ALL_EPISODES:
                self.remove_episodes(managed_episodes)
                return self.view_shows()
            elif lines[ret] == STR_MOVE_ALL_EPISODES_BACK_TO_STAGED:
                self.move_episodes_to_staged(managed_episodes)
                return self.view_shows()
            elif lines[ret] == STR_BACK:
                return self.view_shows()
        return self.view_shows()

    @utils.logged_function
    def view_shows(self):
        ''' Display all managed tvshows, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_MANAGED_TV_SHOWS = utils.ADDON.getLocalizedString(32020)
        STR_REMOVE_ALL_TV_SHOWS = utils.ADDON.getLocalizedString(32021)
        STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED = utils.ADDON.getLocalizedString(
            32022)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_MANAGED_TV_SHOWS = utils.ADDON.getLocalizedString(32023)
        managed_tvshows = self.dbh.get_all_shows('managed')
        if not managed_tvshows:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_TV_SHOWS)
            return
        lines = ['[B]%s[/B]' % x for x in managed_tvshows]
        lines += [
            STR_REMOVE_ALL_TV_SHOWS, STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED,
            STR_BACK
        ]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_TV_SHOWS), lines)
        if ret >= 0:
            if ret < len(managed_tvshows):
                for show_title in managed_tvshows:
                    if managed_tvshows[ret] == show_title:
                        self.view_episodes(show_title)
                        break
            elif lines[ret] == STR_REMOVE_ALL_TV_SHOWS:
                self.remove_all()
            elif lines[ret] == STR_MOVE_ALL_TV_SHOWS_BACK_TO_STAGED:
                self.move_all_to_staged()
            elif lines[ret] == STR_BACK:
                return
コード例 #6
0
class ManagedMoviesMenu(object):
    ''' Provide windows for displaying managed movies,
    and tools for manipulating the objects and managed file '''

    #TODO: context menu for managed items in library
    #TODO: synced watched status with plugin item

    def __init__(self):
        self.dbh = DatabaseHandler()

    @staticmethod
    @utils.logged_function
    def move_all_to_staged(items):
        ''' Remove all managed movies from library, and add them to staged '''
        STR_MOVING_ALL_MOVIES_BACK_TO_STAGED = utils.ADDON.getLocalizedString(32015)
        STR_ALL_MOVIES_MOVED_TO_STAGED = utils.ADDON.getLocalizedString(32016)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_MOVING_ALL_MOVIES_BACK_TO_STAGED)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.remove_from_library()
            item.set_as_staged()
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIES_MOVED_TO_STAGED)

    @staticmethod
    @utils.logged_function
    def remove_all(items):
        ''' Remove all managed movies from library '''
        STR_REMOVING_ALL_MOVIES = utils.ADDON.getLocalizedString(32013)
        STR_ALL_MOVIES_REMOVED = utils.ADDON.getLocalizedString(32014)
        progress_dialog = xbmcgui.DialogProgress()
        progress_dialog.create(utils.ADDON_NAME, STR_REMOVING_ALL_MOVIES)
        for index, item in enumerate(items):
            percent = 100 * index / len(items)
            progress_dialog.update(percent, line2=item.title)
            item.remove_from_library()
            item.delete()
        progress_dialog.close()
        utils.notification(STR_ALL_MOVIES_REMOVED)

    @utils.logged_function
    def options(self, item):
        ''' Provide options for a single managed movie in a dialog window '''
        # TODO: add rename option
        # TODO: add reload metadata option
        STR_REMOVE = utils.ADDON.getLocalizedString(32017)
        STR_MOVE_BACK_TO_STAGED = utils.ADDON.getLocalizedString(32018)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_MANAGED_MOVIE_OPTIONS = utils.ADDON.getLocalizedString(32019)
        lines = [STR_REMOVE, STR_MOVE_BACK_TO_STAGED, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1} - {2}'.format(utils.ADDON_NAME, STR_MANAGED_MOVIE_OPTIONS, item.title), lines
        )
        if ret >= 0:
            if lines[ret] == STR_REMOVE:
                item.remove_from_library()
                item.delete()
                return self.view_all()
            elif lines[ret] == STR_MOVE_BACK_TO_STAGED:
                item.remove_from_library()
                item.set_as_staged()
                return self.view_all()
            elif lines[ret] == STR_BACK:
                return self.view_all()
        return self.view_all()

    def view_all(self):
        ''' Display all managed movies, which are selectable and lead to options.
        Also provides additional options at bottom of menu '''
        STR_NO_MANAGED_MOVIES = utils.ADDON.getLocalizedString(32008)
        STR_REMOVE_ALL_MOVIES = utils.ADDON.getLocalizedString(32009)
        STR_MOVE_ALL_BACK_TO_STAGED = utils.ADDON.getLocalizedString(32010)
        STR_BACK = utils.ADDON.getLocalizedString(32011)
        STR_MANAGED_MOVIES = utils.ADDON.getLocalizedString(32012)
        managed_movies = self.dbh.get_content_items(
            status='managed', mediatype='movie', order='Title'
        )
        if not managed_movies:
            xbmcgui.Dialog().ok(utils.ADDON_NAME, STR_NO_MANAGED_MOVIES)
            return
        lines = [str(x) for x in managed_movies]
        lines += [STR_REMOVE_ALL_MOVIES, STR_MOVE_ALL_BACK_TO_STAGED, STR_BACK]
        ret = xbmcgui.Dialog().select(
            '{0} - {1}'.format(utils.ADDON_NAME, STR_MANAGED_MOVIES), lines
        )
        if ret >= 0:
            if ret < len(managed_movies):
                for i, item in enumerate(managed_movies):
                    if ret == i:
                        self.options(item)
                        break
            elif lines[ret] == STR_REMOVE_ALL_MOVIES:
                self.remove_all(managed_movies)
            elif lines[ret] == STR_MOVE_ALL_BACK_TO_STAGED:
                self.move_all_to_staged(managed_movies)
            elif lines[ret] == STR_BACK:
                return