Bug: 'Unread count' on Taskbar icon incorrect caused by including hidden POP Inbox which was deferred to "Global inbox"
Status: NEW
Priority: --
Created: 2024-09-03 14:20:53+00:00
Resolved: 2024-09-06 10:44:43+00:00

This bug report has been recreated at the request of Francesco 
Originally - bug  1916107   Another person reported a similar issue but count for new mail and it was decided I should recreate the original bug report ...so here it is - it's not a duplicate.

Windows 10 OS
Beta 130.0b3

No chat.
Just two imap accounts and one pop account.
Note: The Pop account was originally set up as a normal mail account using it's own folders, but settings have been since altered and now Pop account uses Global Inbox (Local Folders).

Using setting : View > folders > All
Not unified etc.

- In Settings > General
- 'Incoming Mails' section
- click on 'App icon Options'
- select 'Count of unread messages'
- Force display to function by restarting Thunderbird

*Actual Results*
Thunderbird beta Icon badge on Taskbar says 78 unread messages.
Actual total number of unread messages across two imap accounts and one pop account using Local Folders as Global Inbox is 608
IF  you only count the unread messages in Only the Inboxes of all accounts (2+9+46) the total is 57
But as most emails get auto filtered and moved to different folders when incoming - the unread mail is mainly not in an 'Inbox'.

- preference:  mail.notification.count.inbox_only  is set to true, so only inboxes are counted, 

*Expected Results*
As total of unread in all the Inboxes of all accounts (2+9+46) the total is 57
Thunderbird beta Icon badge on Taskbar to say 57 unread messages.

**To Debug:**
- Settings > General
- Scroll to bottom and click on 'Config Editor'
- set preference: mail.notification.loglevel to All
- open and clear 'Error Console'
- Exit Thunderbird and restart
- Check 'Error Console'

*Actual Results in Error console*
Info says:
> mail.notification: mailbox://nobody@Local%20Folders/Inbox has **46** unread MailNotificationService.sys.mjs:97:19
> mail.notification: imap://name%40gmail.com@imap.gmail.com/INBOX has **2** unread MailNotificationService.sys.mjs:97:19
> mail.notification: imap://name%40btinternet.com@mail.btinternet.com/INBOX has **9** unread MailNotificationService.sys.mjs:97:19
> mail.notification: mailbox://mail%40mydomain.co.uk@mail.livemail.co.uk/Inbox has **21** unread MailNotificationService.sys.mjs:97:19
> mail.notification: Unread mail count changed to **78** MailNotificationManager.sys.mjs:169:18

Discovered:
The pop account says 21 unread, but the pop account is not set up as a separate account - it uses 'Local Folders' Inbox which says 46

So it seems the count of pop hidden Inbox is added thus count is 78. (57 + 21)
That explains the count anomaly, but still seems odd.

It looks like the unread count is including the pop account Inbox prior to swapping account to a Deferred account. So including data from an unseen/hidden Inbox - not in Folder Pane because account is a deferred account.

*Verified*
Access profile - the 'Mail' folder shows the pop account and the Inbox mbox file has the 21 unread emails.

*Process*
- Emptied error console.
- Exit Thunderbird
- Access profile pop Inbox mbox file and copied emails into a new mbox called 'Old Pop' which I put into Local Folders.
- Empty the pop account Inbox file - save it and delete the Inbox.msf file

It now says Inbox size is 0 and it really is empty,
So that account Inbox should now not be included as having unread mail when I restart Thunderbird.

- Restart Thunderbird

*Actual Results*
- I've got an 'Old pop' folder in Local Folders - so I can see those 21 emails.
- checked error console
- unread count in icon on taskbar remained the same 78 and the pop account data still said it had 21 mails !
- mail.notification: mailbox://mail%40mydomain.co.uk@mail.livemail.co.uk/Inbox has **21** unread MailNotificationService.sys.mjs:97:19

*Expected REsults*
- 'Old pop' folder in Local Folders
- unread count in icon on taskbar to say 57 matching total of Inbox unread.

**Check folderCache.json file**
Access profile and 'folderCache.json' file has this:

"C:\Users\User name\AppData\Roaming\thunderbird\Profiles\mni4eecl.default-beta\Mail\mail.livemail.co.uk\Inbox.msf" :
{
"expungedBytes" : 0,
"flags" : 4100,
"folderName" : "Inbox",
"folderSize" : 646121,
"pendingMsgs" : 0,
"pendingUnreadMsgs" : 0,
"totalMsgs" : 21,
**"totalUnreadMsgs" : 21,**
"useServerRetention" : "1"
},

It seems the folderCache.json does not get updated if manually altering profile mbox and .msf files.
It seems the count does not take into account when a pop account is swapped over to use a Global Inbox, so unseen folders are being included in the count.

**Process:**
- Empty Error Console
- Exit Thunderbird
- Access profile
- renamed 'folderCache.json' to 'folderCacheold.json'

- start Thunderbird
- new 'folderCache.json' is ceated.

*Actual Results*
Check folderCache.json file
It looks correct.
"C:\Users\User name\AppData\Roaming\thunderbird\Profiles\mni4eecl.default-beta\Mail\mail.livemail.co.uk\Inbox.msf" :
{
"expungedBytes" : 0,
"flags" : 4100,
"folderName" : "Inbox",
"folderSize" : -1,
"pendingMsgs" : 0,
"pendingUnreadMsgs" : 0,
"totalMsgs" : 0,
"totalUnreadMsgs" : 0
},

*Check Error Console*
error console info has updated:
- mail.notification: mailbox://mail%40mydomain.co.uk@mail.livemail.co.uk/Inbox has **0** unread MailNotificationService.sys.mjs:97:19

This is now correct

*check Taskbar icon unread count*
Total unread count on Taskbar icon says 57 and this matches the total combined unread count in just the Inboxes for all accounts.


**BUG ISSUE:**
One reason for the Taskbar icon unread count to be incorrect is because the 'folderCache.json' file contains the data of a hidden pop account Inbox which has unread mail. 
When pop account was converted from a normal mail account using own folders to a deferred account using 'Global Inbox (Local Folders)', that set of data was not removed from the folderCache.json file. 
Even after I had manually removed the emails from the hidden pop account Inbox and restarted Thunderbird, the folderCache.json file was not auto updated. I had to access again and either delete or rename the folderCache.json file to force a reload of data.
The point is this - the original pop account data is still in the 'folderCache.json' file, but no longer causes a problem because all Inbox data has been removed.  If the count preference included all folders then this issue would extend to any hidden pop folder containing unread mail. But why is this pop account data still in folderCache.json file when pop account is a deferred account using a different account Inbox.

I have provided information on how to find out what the count is including.
I have provided information on how to fix the issue for this instance should people want to check and fix themselves.   But, it is not intuitive nor user friendly to do. 

Hopefully the information will be useful in sorting out this bug.