aboutsummaryrefslogtreecommitdiff
path: root/ctec/__main__.py
blob: 84f1215fd6fd50785b73ad8687db0e2dad3b04da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import re
import threading
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

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
        root.title('ctec')

        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)

        config = get_config()
        self.accounts = [Account(address=section, info=config[section]) for section in config.sections() if '@' in section]
        self.accounts_tree = ttk.Treeview(mainframe, columns=('unread', 'all'))
        self.accounts_tree.column('unread', width=50, anchor=E)
        self.accounts_tree.heading('unread', text='Unread')
        self.accounts_tree.column('all', width=50, anchor=E)
        self.accounts_tree.heading('all', text='All')
        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:
            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)

        menubar = Menu(root)
        root['menu'] = menubar

        file_menu = Menu(menubar)
        menubar.add_cascade(menu=file_menu, label='File', underline=0)
        file_menu.add_command(label='Exit', command=self.on_exit, underline=1)

        help_menu = Menu(menubar)
        menubar.add_cascade(menu=help_menu, label='Help', underline=0)
        help_menu.add_command(label='About', command=self.on_about, underline=0)

    def on_exit(self, *args):
        self.root.destroy()

    def on_about(self, *args):
        messagebox.showinfo(message=f"ctec {'.'.join(str(x) for x in VERSION)}")


def main():
    root = Tk()
    frame = CtecFrame(root)
    root.mainloop()


main()