RealRaven2000 / quickFilters

Thunderbird Add-on: quickFilters
http://quickfilters.quickfolders.org/
Other
46 stars 11 forks source link

Merge Rules: Adding rules on top doesn't work anymore #170

Closed RealRaven2000 closed 1 year ago

RealRaven2000 commented 1 year ago

The option to add new filter rules to the top of the list when merging, which is in the section Merge Rules, doesn't work anymore. New rules are always appended to the end of the list:

image

RealRaven2000 commented 1 year ago

The code that inserted the search term at the top position had to be removed because the function insertElementAt(term, 0) had been removed by Mozilla.

https://github.com/RealRaven2000/quickFilters/blob/ESR102/chrome/content/qFilters-worker.js#L789

In Thunderbird 102 an exception is thrown and the search term is appended to the end.

        try {
          // [Bug 26664] add condition on top instead of appending at bottom
          let ts = target.searchTerms; // returns  nsIMutableArray
          ts.insertElementAt(term, 0);
        }
        catch (ex) {
          util.logException("Could not insert term at top - appending to end instead.", ex);
          target.appendTerm(term);
        }

Fixing it is not quite as trivial as it seems...

RealRaven2000 commented 1 year ago

Apparently we need to clone the array, then we can do all the modifications - later on we write back across the XPCOM boundaries. The attribute itself is not readonly:

https://searchfox.org/comm-central/rev/d6c5002addb19c200b31444c69e51676e54a253c/mailnews/search/public/nsIMsgFilter.idl#76

attribute Array<nsIMsgSearchTerm> searchTerms;

my first attempt is based on this core code:

  let terms = target.searchTerms.Clone();

  if (bGrouping) {
    term.beginsGrouping = bGrouping;
    terms[0].beginsGrouping = 
      (typeof term.beginsGrouping == "number") ? 0 : false; // Betterbird stores number of parentheses
  }
  term.booleanAnd = terms[0].booleanAnd;
  terms.splice(0,0,term); 

  target.searchTerms = terms; // write back!

...but unfortunately this doesn't work because termList.Clone() only exists in C++ (beyond the XPCOM barrier)

RealRaven2000 commented 1 year ago

Looks like the call to Clone() wasn't necessary (or possible) - the getter (called when we are reading searchTerms) automatically returns a cloned array. So the new code is simply:

  let terms = target.searchTerms;

  if (bGrouping) {
    term.beginsGrouping = bGrouping;
    terms[0].beginsGrouping = 
      (typeof term.beginsGrouping == "number") ? 0 : false; // Betterbird stores number of parentheses
  }
  term.booleanAnd = terms[0].booleanAnd;
  terms.splice(0,0,term); 

  target.searchTerms = terms; // write back!

.. with a bit of a speed penalty.

RealRaven2000 commented 1 year ago

Here is a version with a fix. I also added change log and translated into the 19 other languages, as I intend to publish soon this week.

quickFilters-wx-5.9pre72.zip

To test the version above, download the zip file and drag it into Thunderbird Add-ons manager (without extracting the contents!). Github does not allow attaching xpi files but zip files work just as well.

RealRaven2000 commented 1 year ago

Fixed in v5.9 - Published 28/03/2023