~hkdb/geary/disco-3.34.0

« back to all changes in this revision

Viewing changes to src/engine/imap/message/imap-flags.vala

  • Committer: hkdb
  • Date: 2019-09-27 11:28:53 UTC
  • Revision ID: hkdb@3df.io-20190927112853-x02njxcww51q9jfp
Tags: upstream-3.34.0-disco
ImportĀ upstreamĀ versionĀ 3.34.0-disco

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2016 Software Freedom Conservancy Inc.
 
2
 *
 
3
 * This software is licensed under the GNU Lesser General Public License
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution.
 
5
 */
 
6
 
 
7
/**
 
8
 * A generic collection of {@link Flags}.
 
9
 */
 
10
 
 
11
public abstract class Geary.Imap.Flags : Geary.MessageData.AbstractMessageData, Geary.Imap.MessageData,
 
12
    Gee.Hashable<Geary.Imap.Flags> {
 
13
    public int size { get { return list.size; } }
 
14
 
 
15
    protected Gee.Set<Flag> list;
 
16
 
 
17
    protected Flags(Gee.Collection<Flag> flags) {
 
18
        list = new Gee.HashSet<Flag>();
 
19
        list.add_all(flags);
 
20
    }
 
21
 
 
22
    public bool contains(Flag flag) {
 
23
        return list.contains(flag);
 
24
    }
 
25
 
 
26
    public Gee.Set<Flag> get_all() {
 
27
        return list.read_only_view;
 
28
    }
 
29
 
 
30
    /**
 
31
     * Returns the flags in serialized form, which is each flag separated by a space (legal in
 
32
     * IMAP, as flags must be atoms and atoms prohibit spaces).
 
33
     */
 
34
    public virtual string serialize() {
 
35
        return to_string();
 
36
    }
 
37
 
 
38
    /**
 
39
     * Returns a {@link ListParameter} representation of these flags.
 
40
     *
 
41
     * If empty, this returns an empty ListParameter.
 
42
     */
 
43
    public virtual Parameter to_parameter() {
 
44
        ListParameter listp = new ListParameter();
 
45
        foreach (Flag flag in list) {
 
46
            try {
 
47
                listp.add(flag.to_parameter());
 
48
            } catch (ImapError ierr) {
 
49
                // drop on floor with warning
 
50
                message("Unable to parameterize flag \"%s\": %s", flag.to_string(), ierr.message);
 
51
            }
 
52
        }
 
53
 
 
54
        return listp;
 
55
    }
 
56
 
 
57
    public bool equal_to(Geary.Imap.Flags other) {
 
58
        if (this == other)
 
59
            return true;
 
60
 
 
61
        if (other.size != size)
 
62
            return false;
 
63
 
 
64
        return Geary.traverse<Flag>(list).all(f => other.contains(f));
 
65
    }
 
66
 
 
67
    public override string to_string() {
 
68
        StringBuilder builder = new StringBuilder();
 
69
        foreach (Flag flag in list) {
 
70
            if (!String.is_empty(builder.str))
 
71
                builder.append_c(' ');
 
72
 
 
73
            builder.append(flag.value);
 
74
        }
 
75
 
 
76
        return builder.str;
 
77
    }
 
78
 
 
79
    public uint hash() {
 
80
        return Ascii.stri_hash(to_string());
 
81
    }
 
82
}
 
83