~hkdb/geary/disco

« back to all changes in this revision

Viewing changes to src/engine/imap-db/imap-db-email-identifier.vala

  • Committer: hkdb
  • Date: 2019-09-26 19:40:48 UTC
  • Revision ID: hkdb@3df.io-20190926194048-n0vggm3yfo8p1ubr
Tags: upstream-3.32.2-disco
ImportĀ upstreamĀ versionĀ 3.32.2-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
private class Geary.ImapDB.EmailIdentifier : Geary.EmailIdentifier {
 
8
    public int64 message_id { get; private set; }
 
9
    public Imap.UID? uid { get; private set; }
 
10
 
 
11
    public EmailIdentifier(int64 message_id, Imap.UID? uid) {
 
12
        assert(message_id != Db.INVALID_ROWID);
 
13
 
 
14
        base (message_id.to_string());
 
15
 
 
16
        this.message_id = message_id;
 
17
        this.uid = uid;
 
18
    }
 
19
 
 
20
    // Used when a new message comes off the wire and doesn't have a rowid associated with it (yet)
 
21
    // Requires a UID in order to find or create such an association
 
22
    public EmailIdentifier.no_message_id(Imap.UID uid) {
 
23
        base (Db.INVALID_ROWID.to_string());
 
24
 
 
25
        message_id = Db.INVALID_ROWID;
 
26
        this.uid = uid;
 
27
    }
 
28
 
 
29
    // Used to promote an id created with no_message_id to one that has a
 
30
    // message id.  Warning: this causes the hash value to change, so if you
 
31
    // have any EmailIdentifiers in a hashed data structure, this will cause
 
32
    // you not to be able to find them.
 
33
    public void promote_with_message_id(int64 message_id) {
 
34
        assert(this.message_id == Db.INVALID_ROWID);
 
35
 
 
36
        unique = message_id.to_string();
 
37
        this.message_id = message_id;
 
38
    }
 
39
 
 
40
    public bool has_uid() {
 
41
        return (uid != null) && uid.is_valid();
 
42
    }
 
43
 
 
44
    public override int natural_sort_comparator(Geary.EmailIdentifier o) {
 
45
        ImapDB.EmailIdentifier? other = o as ImapDB.EmailIdentifier;
 
46
        if (other == null)
 
47
            return 1;
 
48
 
 
49
        if (uid == null)
 
50
            return 1;
 
51
 
 
52
        if (other.uid == null)
 
53
            return -1;
 
54
 
 
55
        return uid.compare_to(other.uid);
 
56
    }
 
57
 
 
58
    public override string to_string() {
 
59
        return "[%s/%s]".printf(message_id.to_string(), (uid == null ? "null" : uid.to_string()));
 
60
    }
 
61
 
 
62
    public static Gee.Set<Imap.UID> to_uids(Gee.Collection<ImapDB.EmailIdentifier> ids) {
 
63
        Gee.HashSet<Imap.UID> uids = new Gee.HashSet<Imap.UID>();
 
64
        foreach (ImapDB.EmailIdentifier id in ids) {
 
65
            if (id.uid != null)
 
66
                uids.add(id.uid);
 
67
        }
 
68
 
 
69
        return uids;
 
70
    }
 
71
}