~hkdb/geary/disco

« back to all changes in this revision

Viewing changes to src/client/composer/email-entry.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
// A custom entry for e-mail addresses
 
8
public class EmailEntry : Gtk.Entry {
 
9
    // Whether this entry contains a valid email address
 
10
    public bool valid { get; set; default = false; }
 
11
 
 
12
    public bool empty { get; set; default = true; }
 
13
 
 
14
    public bool modified = false;
 
15
 
 
16
    // null or valid addresses
 
17
    public Geary.RFC822.MailboxAddresses? addresses { get; set; default = null; }
 
18
 
 
19
    private weak ComposerWidget composer;
 
20
 
 
21
    private bool updating = false;
 
22
 
 
23
    public EmailEntry(ComposerWidget composer) {
 
24
        changed.connect(on_changed);
 
25
        key_press_event.connect(on_key_press);
 
26
        this.composer = composer;
 
27
 
 
28
        notify["addresses"].connect(() => {
 
29
            validate_addresses();
 
30
            if (updating)
 
31
                return;
 
32
 
 
33
            updating = true;
 
34
            modified = true;
 
35
            text = (addresses == null) ? "" : addresses.to_full_display();
 
36
            updating = false;
 
37
        });
 
38
 
 
39
        show();
 
40
    }
 
41
 
 
42
    private void on_changed() {
 
43
        if (updating)
 
44
            return;
 
45
        modified = true;
 
46
 
 
47
        ContactEntryCompletion? completion = get_completion() as ContactEntryCompletion;
 
48
        if (completion != null) {
 
49
            completion.reset_selection();
 
50
        }
 
51
 
 
52
        if (Geary.String.is_empty(text.strip())) {
 
53
            updating = true;
 
54
            addresses = null;
 
55
            updating = false;
 
56
            valid = false;
 
57
            empty = true;
 
58
            return;
 
59
        }
 
60
 
 
61
        updating = true;
 
62
        addresses = new Geary.RFC822.MailboxAddresses.from_rfc822_string(text);
 
63
        updating = false;
 
64
    }
 
65
 
 
66
    private void validate_addresses() {
 
67
        if (addresses == null || addresses.size == 0) {
 
68
            valid = false;
 
69
            empty = true;
 
70
            return;
 
71
        }
 
72
        empty = false;
 
73
 
 
74
        foreach (Geary.RFC822.MailboxAddress address in addresses) {
 
75
            if (!address.is_valid()) {
 
76
                valid = false;
 
77
                return;
 
78
            }
 
79
        }
 
80
        valid = true;
 
81
    }
 
82
 
 
83
    private bool on_key_press(Gtk.Widget widget, Gdk.EventKey event) {
 
84
        if (event.keyval == Gdk.Key.Tab) {
 
85
            ((ContactEntryCompletion) get_completion()).trigger_selection();
 
86
            composer.child_focus(Gtk.DirectionType.TAB_FORWARD);
 
87
            return true;
 
88
        }
 
89
 
 
90
        return false;
 
91
    }
 
92
}
 
93