Example #1
0
    def getDirContents(self, directory):
        """ Return a tuple of sorted rows (directories, playlists, mediaFiles) for the given directory """
        mediaFiles = []
        directories = []

        for (file, path) in tools.listDir(directory):
            # Make directory names prettier
            junk = ['_']
            pretty_name = file
            for item in junk:
                pretty_name = pretty_name.replace(item, ' ')

            if isdir(path):
                directories.append(
                    (icons.dirMenuIcon(), tools.htmlEscape(pretty_name),
                     TYPE_DIR, path))
            elif isfile(path):
                if media.isSupported(file):
                    mediaFiles.append(
                        (icons.mediaFileMenuIcon(),
                         tools.htmlEscape(pretty_name), TYPE_FILE, path))

        # Individually sort each type of file by name
        mediaFiles.sort(key=self._filename)
        directories.sort(key=self._filename)

        return (directories, mediaFiles)
Example #2
0
    def set_track_playing(self, iter, playing):
        if not iter:
            return
        track = self.tree.getTrack(iter)
        if not track:
            return

        for parent in self.tree.get_all_parents(iter):
            parent_label = self.tree.getLabel(parent)
            parent_label = tools.htmlUnescape(parent_label)
            is_bold = parent_label.startswith('<b>') and parent_label.endswith('</b>')
            if playing and not is_bold:
                parent_label = tools.htmlEscape(parent_label)
                self.tree.setLabel(parent, '<b>%s</b>' % parent_label)
            elif not playing and is_bold:
                parent_label = tools.htmlEscape(parent_label[3:-4])
                self.tree.setLabel(parent, parent_label)

        parent = self.tree.store.iter_parent(iter)
        parent_label = self.tree.getLabel(parent) if parent else None
        label = track.get_label(parent_label=parent_label, playing=playing)
        if playing:
            self.tree.setLabel(iter, label)
            self.tree.setItem(iter, ROW_ICO, icons.playMenuIcon())
            self.tree.expand(iter)
        else:
            self.tree.setLabel(iter, label)
            icon = self.tree.getItem(iter, ROW_ICO)
            has_error = (icon == icons.errorMenuIcon())
            is_dir = (icon == icons.mediaDirMenuIcon())
            if not is_dir and not has_error:
                self.tree.setItem(iter, ROW_ICO, icons.nullMenuIcon())
Example #3
0
    def insertDir(self, trackdir, target=None, drop_mode=None, highlight=False):
        '''
        Insert a directory recursively, return the iter of the first
        added element
        '''
        model = self.tree.store
        if trackdir.flat:
            new = target
        else:
            string = trackdir.dirname.replace('_', ' ')
            string = tools.htmlEscape(string)
            source_row = (icons.mediaDirMenuIcon(), string, None)

            new = self.tree.insert(target, source_row, drop_mode)
            drop_mode = Gtk.TreeViewDropPosition.INTO_OR_AFTER
            if highlight:
                self.tree.select(new)

        dest = new
        for index, subdir in enumerate(trackdir.subdirs):
            drop = drop_mode if index == 0 else Gtk.TreeViewDropPosition.AFTER
            dest = self.insertDir(subdir, dest, drop, highlight)

        dest = new
        for index, track in enumerate(trackdir.tracks):
            drop = drop_mode if index == 0 else Gtk.TreeViewDropPosition.AFTER
            highlight &= trackdir.flat
            dest = self.insertTrack(track, dest, drop, highlight)

        if not trackdir.flat:
            # Open albums on the first layer
            if target is None or model.iter_depth(new) == 0:
                self.tree.expand(new)

        return new
Example #4
0
 def add_dir(self, path):
     '''
     Add a directory with one fake child to the tree
     '''
     name = tools.dirname(path)
     name = tools.htmlEscape(name)
     parent = self.tree.appendRow(
         (icons.dirMenuIcon(), name, TYPE_DIR, path), None)
     # add fake child
     self.tree.appendRow((icons.dirMenuIcon(), '', TYPE_NONE, ''), parent)
Example #5
0
    def formatHTMLSafe(self, fmtString):
        """
            Replace the special fields in the given string by their corresponding value
            Also ensure that the fields don't contain HTML special characters (&, <, >)
        """
        result = fmtString

        result = result.replace('{path}', tools.htmlEscape(self.getFilePath()))
        result = result.replace('{album}', tools.htmlEscape(self.getAlbum()))
        result = result.replace('{track}', str(self.getNumber()))
        result = result.replace('{title}', tools.htmlEscape(self.getTitle()))
        result = result.replace('{artist}', tools.htmlEscape(self.getArtist()))
        result = result.replace('{genre}', tools.htmlEscape(self.getGenre()))
        result = result.replace('{date}', str(self.getDate()))
        result = result.replace('{disc}', str(self.getDiscNumber()))
        result = result.replace('{bitrate}', self.getBitrate())
        result = result.replace('{sample_rate}', self.getSampleRate())
        result = result.replace('{duration_sec}', str(self.getLength()))
        result = result.replace('{duration_str}', sec2str(self.getLength()))

        return result
Example #6
0
    def fillList(self):
        """ Fill the list of modules """
        rows = []
        for (name, data) in modules.getModules():
            instance = data[modules.MOD_INSTANCE]
            mandatory = data[modules.MOD_INFO][modules.MODINFO_MANDATORY]
            configurable = data[modules.MOD_INFO][modules.MODINFO_CONFIGURABLE]

            if configurable or not mandatory:
                if configurable and instance is not None:
                    icon = icons.prefsBtnIcon()
                else:
                    icon = None
                text = '<b>%s</b>\n<small>%s</small>' % (
                    tools.htmlEscape(_(name)),
                    tools.htmlEscape(
                        data[modules.MOD_INFO][modules.MODINFO_DESC]))
                rows.append((instance is not None, text, icon, not mandatory,
                             instance, data[modules.MOD_INFO]))

        rows.sort(key=lambda row: row[ROW_TEXT])
        self.list.store.clear()
        for row in rows:
            self.list.store.append(row)
Example #7
0
        def get_name(path):
            # Remove the search path from the name
            if path == search_path:
                name = tools.dirname(path)
            else:
                name = path.replace(search_path, '')
                # Only show filename and at most one parent dir for each file.
                name = '/'.join(name.split('/')[-2:])
            name = name.strip('/')

            name = regex.sub(same_case_bold, name)

            name = tools.htmlEscape(name)
            name = name.replace('STARTBOLD', '<b>').replace('ENDBOLD', '</b>')
            return name
Example #8
0
    def get_label(self, parent_label=None, playing=False):
        """
        Return a treeview representation
        """
        title = self.tags.get(TAG_TIT, '')
        artist = self.tags.get(TAG_ART, '')

        album = self.getExtendedAlbum()
        if album == consts.UNKNOWN_ALBUM:
            album = ''

        number = self.tags.get(TAG_NUM, '')
        length = self.getLength()

        if number:
            number = str(number).zfill(2)

        # Delete whitespace at the end
        connectors = [
            'the', 'and', '&', ',', '.', '?', '!', "'", ':', '-', ' '
        ]

        if parent_label:
            parent_label = parent_label.lower()
            short_album = album.lower()
            short_artist = artist.lower()
            for connector in connectors:
                parent_label = parent_label.replace(connector, '')
                short_album = short_album.replace(connector, '')
                short_artist = short_artist.replace(connector, '')
            if short_album.strip() in parent_label:
                album = ''
            if short_artist.strip() in parent_label:
                artist = ''

        if title:
            label = ' - '.join(
                [part for part in [artist, album, number, title] if part])
        else:
            label = self.getBasename()

        label = tools.htmlEscape(label)
        if playing:
            label = '<b>%s</b>' % label
        label += ' [%s]' % tools.sec2str(length)
        return label