summaryrefslogtreecommitdiff
path: root/src/contacts-chunk-property-filter.vala
blob: d2c823301fde9941bf1065702f38f99bf0b4a777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2022 Niels De Graef <nielsdegraef@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Folks;

/**
 * A custom GtkFilter to filter {@link Chunk}s on a given property.
 */
public class Contacts.ChunkPropertyFilter : Gtk.Filter {

  /** If not empty, only the properties in the string list will match */
  public Gtk.StringList allowed_properties {
    get { return this._allowed_properties; }
    construct set {
      this._allowed_properties = value;
      value.items_changed.connect ((list, pos, removed, added) => {
        if ((added > 0 && removed == 0) || list.get_n_items () == 0)
          changed (Gtk.FilterChange.LESS_STRICT);
        else if (added == 0 && removed > 0)
          changed (Gtk.FilterChange.MORE_STRICT);
        else
          changed (Gtk.FilterChange.DIFFERENT);
      });
    }
  }
  private Gtk.StringList _allowed_properties = null;

  /**
   * Creates a ChunkPropertyFilter for a specific property
   */
  public ChunkPropertyFilter (string[] properties) {
    Object (allowed_properties: new Gtk.StringList (properties));
  }

  /**
   * Creates a ChunkPropertyFilter for a specific property
   */
  public ChunkPropertyFilter.for_single (string property_name) {
    Object (allowed_properties: new Gtk.StringList ({ property_name }));
  }

  public override bool match (GLib.Object? item) {
    unowned var chunk = (Chunk) item;
    return match_property_name (chunk);
  }

  private bool match_property_name (Chunk chunk) {
    if (this.allowed_properties.get_n_items () == 0)
      return true;

    for (uint i = 0; i < this.allowed_properties.get_n_items (); i++) {
      if (chunk.property_name == this.allowed_properties.get_string (i))
        return true;
    }
    return false;
  }

  public override Gtk.FilterMatch get_strictness () {
    if (this.allowed_properties.get_n_items () == 0)
      return Gtk.FilterMatch.ALL;
    return Gtk.FilterMatch.SOME;
  }
}