Commit fcce6bc5 authored by Canek Peláez's avatar Canek Peláez
Browse files

FileTags: A whole refactoring.

parent 34355126
Loading
Loading
Loading
Loading
+160 −278
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@ namespace MLM {

    public class FileTags {

        private Gee.HashMap<string, string?> string_frames;
        private Gee.HashMap<string, int?> int_frames;
        private Gee.HashMap<int, GLib.Bytes> data_frames;

        public string artist {
            get { return _artist; }
            set { define_artist(value); }
@@ -72,13 +76,13 @@ namespace MLM {

        public int track {
            get { return _track; }
            set { define_track(value); }
            set { define_track_total(value, _total); }
        }
        private int _track;

        public int total {
            get { return _total; }
            set { define_total(value); }
            set { define_track_total(_track, value); }
        }
        private int _total;

@@ -135,18 +139,14 @@ namespace MLM {

        public FileTags(string filename) {
            this.filename = filename;
            string_frames = new Gee.HashMap<string, string?>();
            int_frames = new Gee.HashMap<string, int?>();
            data_frames = new Gee.HashMap<int, GLib.Bytes>();
            read_tags();
        }

        private void read_tags() {
            _artist = _title = _album = _band = _comment = null;
            _composer = _original = null;
            _year = _track = _total = _disc = _genre = -1;
            _front_cover_picture = _artist_picture = null;
            front_cover_picture_description = null;
            artist_picture_description = null;
            has_tags = false;

            time = Util.get_file_time(filename);

            file = new Id3Tag.File(filename, Id3Tag.FileMode.READWRITE);
@@ -159,19 +159,20 @@ namespace MLM {
            for (int i = 0; i < tag.frames.length; i++) {
                var frame = tag.frames[i];
                if (frame.id == FrameId.ARTIST) {
                    _artist = frame.get_text();
                    string_frames[FrameId.ARTIST] = _artist = frame.get_text();
                } else if (frame.id == FrameId.TITLE) {
                    _title = frame.get_text();
                    string_frames[FrameId.TITLE] = _title = frame.get_text();
                } else if (frame.id == FrameId.ALBUM) {
                    _album = frame.get_text();
                    string_frames[FrameId.ALBUM] = _album = frame.get_text();
                } else if (frame.id == FrameId.BAND) {
                    _band = frame.get_text();
                    string_frames[FrameId.BAND] = _band = frame.get_text();
                } else if (frame.id == FrameId.YEAR) {
                    _year = int.parse(frame.get_text());
                    int_frames[FrameId.YEAR] = _year = int.parse(frame.get_text());
                } else if (frame.id == FrameId.TRACK) {
                    string track = frame.get_text();
                    if (track == null)
                        continue;
                    string_frames[FrameId.TRACK] = track;
                    if (track.index_of("/") != -1) {
                        string[] t = track.split("/");
                        _track = int.parse(t[0]);
@@ -190,28 +191,34 @@ namespace MLM {
                    for (int j = 0; j < genres.length; j++)
                        if (g == genres[j].to_string())
                            _genre = j;
                    if (_genre != -1)
                    if (_genre != -1) {
                        int_frames[FrameId.GENRE] = _genre;
                        continue;
                    }
                    int n = int.parse(g);
                    if (0 <= n && n < genres.length)
                    if (0 <= n && n < genres.length) {
                        _genre = n;
                    else
                        int_frames[FrameId.GENRE] = _genre;
                    } else {
                        invalid_frames.add(frame);
                    }
                } else if (frame.id == FrameId.COMMENT) {
                    _comment = frame.get_comment_text();
                    string_frames[FrameId.COMMENT] = _comment = frame.get_comment_text();
                } else if (frame.id == FrameId.COMPOSER) {
                    _composer = frame.get_text();
                    string_frames[FrameId.COMPOSER] = _composer = frame.get_text();
                } else if (frame.id == FrameId.ORIGINAL) {
                    _original = frame.get_text();
                    string_frames[FrameId.ORIGINAL] = _original = frame.get_text();
                } else if (frame.id == FrameId.PICTURE) {
                    var fc_data = frame.get_picture(Id3Tag.PictureType.COVERFRONT);
                    if (fc_data != null) {
                        _front_cover_picture = fc_data;
                        data_frames[Id3Tag.PictureType.COVERFRONT] = new GLib.Bytes(fc_data);
                        front_cover_picture_description = frame.get_picture_description();
                    }
                    var a_data = frame.get_picture(Id3Tag.PictureType.ARTIST);
                    if (a_data != null) {
                        _artist_picture = a_data;
                        data_frames[Id3Tag.PictureType.ARTIST] = new GLib.Bytes(a_data);
                        artist_picture_description = frame.get_picture_description();
                    }
                } else {
@@ -266,13 +273,8 @@ namespace MLM {
        }

        public void update() {
            if (_artist == null && _title == null && _album == null &&
                _band == null && _comment == null && _composer == null &&
                _original == null && _year == -1 && _track == -1 &&
                _total == -1 && _disc == -1 && _genre == -1 &&
                _front_cover_picture == null && _artist_picture == null &&
                front_cover_picture_description == null &&
                artist_picture_description == null) {
            if (string_frames.is_empty && int_frames.is_empty &&
                data_frames.is_empty) {
                remove_tags();
            } else {
                tag.options(Id3Tag.TagOption.COMPRESSION, 0);
@@ -280,51 +282,6 @@ namespace MLM {
            }
        }

        private void define_artist(string a) {
            if (a == "" && _artist == null)
                return;
            Id3Tag.Frame artist_frame;
            if (a == "") {
                artist_frame = tag.search_frame(FrameId.ARTIST);
                tag.detachframe(artist_frame);
                _artist = null;
                return;
            }
            if (_artist == null) {
                artist_frame = tag.create_text_frame(FrameId.ARTIST);
                tag.attachframe(artist_frame);
            } else {
                artist_frame = tag.search_frame(FrameId.ARTIST);
            }
            _artist = a;
            artist_frame.set_text(_artist);
            if (artist_picture != null) {
                var ap_frame =
                    tag.search_picture_frame(Id3Tag.PictureType.ARTIST);
                ap_frame.set_picture_description(_artist);
            }
        }

        private void define_band(string b) {
            if (b == "" && _band == null)
                return;
            Id3Tag.Frame band_frame;
            if (b == "") {
                band_frame = tag.search_frame(FrameId.BAND);
                tag.detachframe(band_frame);
                _band = null;
                return;
            }
            if (_band == null) {
                band_frame = tag.create_text_frame(FrameId.BAND);
                tag.attachframe(band_frame);
            } else {
                band_frame = tag.search_frame(FrameId.BAND);
            }
            _band = b;
            band_frame.set_text(_band);
        }

        ~FileTags() {
            if (file != null) {
                file.close();
@@ -332,253 +289,178 @@ namespace MLM {
            }
        }

        private void define_title(string t) {
            if (t == "" && _title == null)
                return;
            Id3Tag.Frame title_frame;
            if (t == "") {
                title_frame = tag.search_frame(FrameId.TITLE);
                tag.detachframe(title_frame);
                _title = null;
        private void define_artist(string? value) {
            define_text_value(FrameId.ARTIST, value);
            _artist = string_frames.has_key(FrameId.ARTIST) ?
                string_frames[FrameId.ARTIST] : null;
            if (_artist == null)
                return;
            }
            if (_title == null) {
                title_frame = tag.create_text_frame(FrameId.TITLE);
                tag.attachframe(title_frame);
            } else {
                title_frame = tag.search_frame(FrameId.TITLE);
            }
            _title = t;
            title_frame.set_text(_title);
            var frame = tag.search_picture_frame(Id3Tag.PictureType.ARTIST);
            if (frame != null)
                frame.set_picture_description(value);
        }

        private void define_album(string a) {
            if (a == "" && _album == null)
                return;
            Id3Tag.Frame album_frame;
            if (a == "") {
                album_frame = tag.search_frame(FrameId.ALBUM);
                tag.detachframe(album_frame);
                _album = null;
                return;
            }
            if (_album == null) {
                album_frame = tag.create_text_frame(FrameId.ALBUM);
                tag.attachframe(album_frame);
            } else {
                album_frame = tag.search_frame(FrameId.ALBUM);
            }
            _album = a;
            album_frame.set_text(_album);
            if (front_cover_picture != null) {
                var fcp_frame = tag.search_picture_frame(Id3Tag.PictureType.COVERFRONT);
                fcp_frame.set_picture_description(_album + " cover");
            }
        private void define_band(string? value) {
            define_text_value(FrameId.BAND, value);
            _band = string_frames.has_key(FrameId.BAND) ?
                string_frames[FrameId.BAND] : null;
        }

        private void define_year(int y) {
            if (y == _year)
                return;
            Id3Tag.Frame year_frame;
            if (y == -1) {
                year_frame = tag.search_frame(FrameId.YEAR);
                tag.detachframe(year_frame);
                _year = -1;
                return;
            }
            if (_year == -1) {
                year_frame = tag.create_text_frame(FrameId.YEAR);
                tag.attachframe(year_frame);
            } else {
                year_frame = tag.search_frame(FrameId.YEAR);
        private void define_title(string? value) {
            define_text_value(FrameId.TITLE, value);
            _title = string_frames.has_key(FrameId.TITLE) ?
                string_frames[FrameId.TITLE] : null;
        }
            _year = y;
            year_frame.set_text("%d".printf(_year));

        private void define_album(string? value) {
            define_text_value(FrameId.ALBUM, value);
            _album = string_frames.has_key(FrameId.ALBUM) ?
                string_frames[FrameId.ALBUM] : null;
            var frame = tag.search_picture_frame(Id3Tag.PictureType.COVERFRONT);
            if (frame != null)
                frame.set_picture_description(value + " cover");
        }

        private void define_track(int t) {
            if (t == _track)
                return;
            Id3Tag.Frame track_frame;
            if (t == -1) {
                track_frame = tag.search_frame(FrameId.TRACK);
                tag.detachframe(track_frame);
                _track = -1;
                _total = -1;
                return;
        private void define_year(int value) {
            define_int_value(FrameId.YEAR, value);
            _year = int_frames.has_key(FrameId.YEAR) ?
                int_frames[FrameId.YEAR] : -1;
        }
            if (_track == -1) {
                track_frame = tag.create_text_frame(FrameId.TRACK);
                tag.attachframe(track_frame);

        private void define_track_total(int new_track, int new_total) {
            if (new_total < new_track && new_track > 0)
                new_total = new_track;
            string value = (new_track == -1) ? null : "%d/%d".printf(new_total, new_track);
            define_text_value(FrameId.TRACK, value);
            if (string_frames.has_key(FrameId.TRACK)) {
                _track = new_track;
                _total = new_total;
            } else {
                track_frame = tag.search_frame(FrameId.TRACK);
                _track = _total = -1;
            }
            _track = t;
            if (_total == -1)
                track_frame.set_text("%02d".printf(_track));
            else
                track_frame.set_text("%02d/%02d".printf(_track, _total));
        }

        private void define_total(int t) {
            if (t == _total)
                return;
            Id3Tag.Frame track_frame;
            if (t == -1 && _track != -1) {
                track_frame = tag.search_frame(FrameId.TRACK);
                track_frame.set_text("%02d".printf(_track));
                _total = -1;
                return;
            }
            if (_track == -1)
                return;
            _total = t;
            track_frame = tag.search_frame(FrameId.TRACK);
            track_frame.set_text("%02d/%02d".printf(_track, _total));
        private void define_disc(int value) {
            define_int_value(FrameId.DISC, value);
            _disc = int_frames.has_key(FrameId.DISC) ?
                int_frames[FrameId.DISC] : -1;
        }

        private void define_disc(int d) {
            if (d == _disc)
                return;
            Id3Tag.Frame disc_frame;
            if (d == -1) {
                disc_frame = tag.search_frame(FrameId.DISC);
                tag.detachframe(disc_frame);
                _disc = -1;
                return;
            }
            if (_disc == -1) {
                disc_frame = tag.create_text_frame(FrameId.DISC);
                tag.attachframe(disc_frame);
            } else {
                disc_frame = tag.search_frame(FrameId.DISC);
            }
            _disc = d;
            disc_frame.set_text("%d".printf(_disc));
        private void define_genre(int value) {
            define_int_value(FrameId.GENRE, value);
            _genre = int_frames.has_key(FrameId.GENRE) ?
                int_frames[FrameId.GENRE] : -1;
        }

        private void define_genre(int g) {
            if (g == _genre)
                return;
            Id3Tag.Frame genre_frame;
            if (g == -1) {
                genre_frame = tag.search_frame(FrameId.GENRE);
                tag.detachframe(genre_frame);
                _genre = -1;
        private void define_comment(string? value) {
            Id3Tag.Frame frame;
            if (value == null || value == "") {
                if (string_frames.has_key(FrameId.COMMENT)) {
                    string_frames.unset(FrameId.COMMENT);
                    frame = tag.search_frame(FrameId.COMMENT);
                    tag.detachframe(frame);
                }
                return;
            }
            if (_genre == -1) {
                genre_frame = tag.create_text_frame(FrameId.GENRE);
                tag.attachframe(genre_frame);
            if (!string_frames.has_key(FrameId.COMMENT)) {
                frame = tag.create_comment_frame(FrameId.COMMENT);
                tag.attachframe(frame);
            } else {
                genre_frame = tag.search_frame(FrameId.GENRE);
                frame = tag.search_frame(FrameId.COMMENT);
            }
            _genre = g;
            genre_frame.set_text("%d".printf(_genre));
            frame.set_comment_text(value);
            string_frames[FrameId.COMMENT] = value;
            _comment = string_frames.has_key(FrameId.COMMENT) ?
                string_frames[FrameId.COMMENT] : null;
        }

        private void define_comment(string c) {
            if (c == "" && _comment == null)
                return;
            Id3Tag.Frame comment_frame;
            if (c == "") {
                comment_frame = tag.search_frame(FrameId.COMMENT);
                tag.detachframe(comment_frame);
                _comment = null;
                return;
            }
            if (_comment == null) {
                comment_frame = tag.create_comment_frame("eng");
                tag.attachframe(comment_frame);
            } else {
                comment_frame = tag.search_frame(FrameId.COMMENT);
            }
            _comment = c;
            comment_frame.set_comment_text(c);
        private void define_composer(string? value) {
            define_text_value(FrameId.COMPOSER, value);
            _composer = string_frames.has_key(FrameId.COMPOSER) ?
                string_frames[FrameId.COMPOSER] : null;
        }

        private void define_composer(string c) {
            if (c == "" && _composer == null)
                return;
            Id3Tag.Frame composer_frame;
            if (c == "") {
                composer_frame = tag.search_frame(FrameId.COMPOSER);
                tag.detachframe(composer_frame);
                _composer = null;
                return;
        private void define_original(string? value) {
            define_text_value(FrameId.ORIGINAL, value);
            _original = string_frames.has_key(FrameId.ORIGINAL) ?
                string_frames[FrameId.ORIGINAL] : null;
        }
            if (_composer == null) {
                composer_frame = tag.create_text_frame(FrameId.COMPOSER);
                tag.attachframe(composer_frame);
            } else {
                composer_frame = tag.search_frame(FrameId.COMPOSER);

        private void define_front_cover_picture(uint8[] value) {
            Id3Tag.PictureType pt = Id3Tag.PictureType.COVERFRONT;
            define_data_value(pt, value, (album != null) ?
                              album + " cover" : "");
            _front_cover_picture = data_frames.has_key(pt) ?
                data_frames[pt].get_data() : null;
        }
            _composer = c;
            composer_frame.set_text(_composer);

        private void define_artist_picture(uint8[] value) {
            Id3Tag.PictureType pt = Id3Tag.PictureType.ARTIST;
            define_data_value(pt, value, (artist != null) ? artist : "");
            _artist_picture = data_frames.has_key(pt) ?
                data_frames[pt].get_data() : null;
        }

        private void define_original(string o) {
            if (o == "" && _original == null)
                return;
            Id3Tag.Frame original_artist_frame;
            if (o == "") {
                original_artist_frame = tag.search_frame(FrameId.ORIGINAL);
                tag.detachframe(original_artist_frame);
                _original = null;
        private void define_text_value(string frame_id, string? value) {
            Id3Tag.Frame frame;
            if (value == null || value == "") {
                if (string_frames.has_key(frame_id)) {
                    string_frames.unset(frame_id);
                    frame = tag.search_frame(frame_id);
                    tag.detachframe(frame);
                }
                return;
            }
            if (_original == null) {
                original_artist_frame = tag.create_text_frame(FrameId.ORIGINAL);
                tag.attachframe(original_artist_frame);
            if (!string_frames.has_key(frame_id)) {
                frame = tag.create_text_frame(frame_id);
                tag.attachframe(frame);
            } else {
                original_artist_frame = tag.search_frame(FrameId.ORIGINAL);
                frame = tag.search_frame(frame_id);
            }
            _original = o;
            original_artist_frame.set_text(_original);
            frame.set_text(value);
            string_frames[frame_id] = value;
        }

        private void define_front_cover_picture(uint8[] f) {
            if (_front_cover_picture == null && f == null)
                return;
            Id3Tag.PictureType pt = Id3Tag.PictureType.COVERFRONT;
            Id3Tag.Frame fcp_frame;
            if (f == null) {
                fcp_frame = tag.search_picture_frame(pt);
                tag.detachframe(fcp_frame);
                _front_cover_picture = null;
        private void define_int_value(string frame_id, int value) {
            Id3Tag.Frame frame;
            if (value == -1) {
                if (int_frames.has_key(frame_id)) {
                    int_frames.unset(frame_id);
                    frame = tag.search_frame(frame_id);
                    tag.detachframe(frame);
                }
                return;
            }
            if (_front_cover_picture == null) {
                fcp_frame = tag.create_picture_frame(pt);
                tag.attachframe(fcp_frame);
            if (!int_frames.has_key(frame_id)) {
                frame = tag.create_text_frame(frame_id);
                tag.attachframe(frame);
            } else {
                fcp_frame = tag.search_picture_frame(pt);
                frame = tag.search_frame(frame_id);
            }
            _front_cover_picture = f;
            fcp_frame.set_picture(_front_cover_picture,
                                  album != null ? album + " cover" : "");
            frame.set_text("%d".printf(value));
            int_frames[frame_id] = value;
        }

        private void define_artist_picture(uint8[] a) {
            if (_artist_picture == null && a == null)
                return;
            Id3Tag.PictureType pt = Id3Tag.PictureType.ARTIST;
            Id3Tag.Frame ap_frame;
            if (a == null) {
                ap_frame = tag.search_picture_frame(pt);
                tag.detachframe(ap_frame);
                _artist_picture = null;
        private void define_data_value(Id3Tag.PictureType pt,
                                       uint8[] value,
                                       string? text) {
            Id3Tag.Frame frame;
            if (value == null) {
                if (data_frames.has_key(pt)) {
                    data_frames.unset(pt);
                    frame = tag.search_picture_frame(pt);
                    tag.detachframe(frame);
                }
                return;
            }
            if (_artist_picture == null) {
                ap_frame = tag.create_picture_frame(pt);
                tag.attachframe(ap_frame);
            if (!data_frames.has_key(pt)) {
                frame = tag.create_picture_frame(pt);
                tag.attachframe(frame);
            } else {
                ap_frame = tag.search_picture_frame(pt);
                frame = tag.search_picture_frame(pt);
            }
            _artist_picture = a;
            ap_frame.set_picture(_artist_picture,
                                 artist != null ? artist : "");
            frame.set_picture(value, text != null ? text : "");
            data_frames[pt] = new GLib.Bytes(value);
        }
    }
}