1/*
2 * Copyright 2016 Software Freedom Conservancy Inc.
3 *
4 * This software is licensed under the GNU Lesser General Public License
5 * (version 2.1 or later).  See the COPYING file in this distribution.
6 */
7
8/**
9 * Handles an insertion of messages from a monitor's base folder.
10 */
11private class Geary.App.InsertOperation : BatchOperation<EmailIdentifier> {
12
13
14    public InsertOperation(ConversationMonitor monitor,
15                           Gee.Collection<EmailIdentifier> inserted_ids) {
16        base(monitor, inserted_ids);
17    }
18
19    public override async void execute_batch(Gee.Collection<EmailIdentifier> batch)
20        throws GLib.Error {
21        // Insert messages that are older than the current window
22        // eldest only if the window is smaller than it could be, to
23        // avoid involuntarily growing the window larger more than it
24        // needs to be.
25        bool needs_more = this.monitor.should_load_more;
26        Geary.EmailIdentifier? lowest = this.monitor.window_lowest;
27        if (lowest != null) {
28            Gee.Iterator<EmailIdentifier> iter = batch.iterator();
29            while (iter.next()) {
30                EmailIdentifier inserted = iter.get();
31                if (!needs_more && lowest.natural_sort_comparator(inserted) > 0) {
32                    iter.remove();
33                }
34            }
35        }
36
37        if (!batch.is_empty) {
38            debug("Inserting %u messages into %s",
39                  batch.size,
40                  this.monitor.base_folder.to_string());
41            yield this.monitor.load_by_sparse_id(batch);
42        } else {
43            debug("Inserting no messages into %s, none needed",
44                  this.monitor.base_folder.to_string());
45        }
46    }
47}
48