IMAP: remove cached threading information and UID mapping when the UIDVALIDITY etc changes

Previously, the code would remove various pieces of information from the cache,
but the threading information would inadvertently persist. Thanks to ronny_ on
IRC for his bug report.

REVIEW: 110913
Cleanup: DRY when sanitizing the mailbox names before passing them to the SQL layer

This massaging is still needed due to qsqlite translating a null QString into
NIL; by now, this requirement is documented and its use is made consistent.

Thanks to ronny_ for pointing out that the old way was very ugly.

REVIEW: 110921
Refactoring
IMAP: speed up the search processing when filtering messages

The old algorithm was rather dumb -- finding a first element of a QHash is not
O(1), but O(n), leading to quadratic time complexity, and the excess memory
traffic doesn't help either. On my laptop, this change changes cuts runtime of
the newly added test to ~280ms from the original 15+ seconds.

REVIEW: 110925