From 092d117c8c04a530df0331fab62f7033605d0979 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Thu, 5 Nov 2020 00:48:04 +1100 Subject: [PATCH] Geary.SearchQuery: Enable checking queries and terms for equality Add `SearchQuery.equal_to` method and virtual `Term.equal_to` method, overriding it as needed in subclasses. --- src/engine/api/geary-search-query.vala | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/engine/api/geary-search-query.vala b/src/engine/api/geary-search-query.vala index 84f686b9..60c6b62f 100644 --- a/src/engine/api/geary-search-query.vala +++ b/src/engine/api/geary-search-query.vala @@ -160,6 +160,14 @@ public abstract class Geary.SearchQuery : BaseObject { /** Determines opposite of the term is matched. */ public bool is_negated { get; set; default = false; } + /** Determines if this term is equal to another. */ + public virtual bool equal_to(Term other) { + return ( + this.is_negated == other.is_negated && + this.get_type() == other.get_type() + ); + } + /** Returns a string representation, for debugging. */ public abstract string to_string(); @@ -237,6 +245,27 @@ public abstract class Geary.SearchQuery : BaseObject { this.terms.add_all(terms); } + public override bool equal_to(Term other) { + if (this == other) { + return true; + } + if (!base.equal_to(other)) { + return false; + } + var text = (EmailTextTerm) other; + if (this.target != text.target || + this.matching_strategy != text.matching_strategy || + this.terms.size != text.terms.size) { + return false; + } + for (int i = 0; i < this.terms.size; i++) { + if (this.terms[i] != text.terms[i]) { + return false; + } + } + return true; + } + public override string to_string() { var builder = new GLib.StringBuilder(); if (this.is_negated) { @@ -283,6 +312,16 @@ public abstract class Geary.SearchQuery : BaseObject { this.value = value; } + public override bool equal_to(Term other) { + if (this == other) { + return true; + } + if (!base.equal_to(other)) { + return false; + } + return this.value.equal_to(((EmailFlagTerm) other).value); + } + public override string to_string() { return "%s(%s)".printf( this.is_negated ? "!" : "", @@ -317,6 +356,23 @@ public abstract class Geary.SearchQuery : BaseObject { this.raw = raw; } + /** Determines if this query's expression is equal to another's. */ + public bool equal_to(SearchQuery other) { + if (this == other) { + return true; + } + if (this.expression.size != other.expression.size) { + return false; + } + for (int i = 0; i < this.expression.size; i++) { + if (!this.expression[i].equal_to(other.expression[i])) { + return false; + } + } + return true; + } + + /** Returns a string representation of this query, for debugging. */ public string to_string() { var builder = new GLib.StringBuilder(); builder.append_printf("\"%s\": ", this.raw);