aboutsummaryrefslogtreecommitdiff
path: root/ctec/__main__.py
diff options
context:
space:
mode:
Diffstat (limited to 'ctec/__main__.py')
-rw-r--r--ctec/__main__.py47
1 files changed, 43 insertions, 4 deletions
diff --git a/ctec/__main__.py b/ctec/__main__.py
index 8d01960..84f1215 100644
--- a/ctec/__main__.py
+++ b/ctec/__main__.py
@@ -1,3 +1,4 @@
+import re
import threading
from tkinter import *
from tkinter import messagebox
@@ -7,6 +8,9 @@ from . import VERSION
from .config import get_config
from .logic import Account
+def slugify(text: str):
+ return re.sub(r'\W', '-', text)
+
class CtecFrame:
def __init__(self, root: Tk):
self.root = root
@@ -14,6 +18,8 @@ class CtecFrame:
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
+ mainframe.columnconfigure(0, weight=1)
+ mainframe.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
@@ -27,16 +33,49 @@ class CtecFrame:
self.accounts_tree.grid(column=0, row=0, sticky=(N, W, E, S))
self.accounts_tree.insert('', 'end', 'all', text='All Accounts')
for account in self.accounts:
- unread_count = sum(1 for message in account.inbox() if 'S' not in message.get_flags())
- all_count = sum(1 for message in account.inbox())
- self.accounts_tree.insert('', 'end', account.address, text=account.address, values=(unread_count, all_count))
- threading.Thread(target=lambda: account.fetch_inbox()).start()
+ self.accounts_tree.insert('', 'end', account.address, text=account.address, open=True)
+ for folder_name, folder in account.folders():
+ unread_count = sum(1 for message in folder if 'S' not in message.get_flags())
+ all_count = sum(1 for message in folder)
+ tree_item = f'{account.address}-{slugify(folder_name)}'
+ parent_tree_item = account.address
+ if '.' in folder_name:
+ parent_folder_name, child_folder_name = folder_name.rsplit('.', 1)
+ parent_tree_item = f'{account.address}-{slugify(parent_folder_name)}'
+ folder_name = child_folder_name
+ self.accounts_tree.insert(parent_tree_item, 'end', tree_item, text=folder_name, values=(unread_count, all_count), open=True)
+ # this argument is here to work around an obnoxious python misfeature
+ # local variables aren't closed over unless the function is returned??
+ # so setting a default argument value lets us close over the value instead of just using the most recent value in that name
+ def fetch_account_folders(account=account):
+ account.fetch_folders()
+ self.update_accounts_tree(updated_account=account)
+ threading.Thread(target=fetch_account_folders).start()
# self.messages =
# create a menu bar
self.make_menu_bar(root)
+ def update_accounts_tree(self, updated_account=None):
+ selected_accounts = [updated_account]
+ if updated_account is None:
+ selected_accounts = self.accounts
+ for account in selected_accounts:
+ for folder_name, folder in account.folders():
+ unread_count = sum(1 for message in folder if 'S' not in message.get_flags())
+ all_count = sum(1 for message in folder)
+ tree_item = f'{account.address}-{slugify(folder_name)}'
+ if self.accounts_tree.exists(tree_item):
+ self.accounts_tree.item(tree_item, values=(unread_count, all_count))
+ else:
+ parent_tree_item = account.address
+ if '.' in folder_name:
+ parent_folder_name, child_folder_name = folder_name.rsplit('.', 1)
+ parent_tree_item = f'{account.address}-{slugify(parent_folder_name)}'
+ folder_name = child_folder_name
+ self.accounts_tree.insert(parent_tree_item, 'end', tree_item, text=folder_name, values=(unread_count, all_count), open=True)
+
def make_menu_bar(self, root):
root.option_add('*tearOff', FALSE)