Basic refactoring and Tom's recommendations
[imap-fix-internaldate] / mailbox_state.py
CommitLineData
7a1d4c35
PD
1'''
2mailbox_state.py - The module contains the MailboxState class.
3
4Copyright (c) 2012 Intra2net AG
5Author: Plamen Dimitrov
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16'''
17
18class MailboxState:
19 """This class is responsible for containing and updating a mailbox data."""
20
21 # class attributes
22 # string with quotation marks for the mailbox name
23 name = None
24 # string for the mailbox uidvalidity
25 uidvalidity = None
26 # string for user owning the mailbox
27 owner = None
28 # list of bytes for last cached mail uids
29 uids = None
30 # boolean flag for committing state changes
31 needs_save = None
32 # integer for found date conflicts
33 date_conflicts = None
34 # integer for found messages with missing received headers
35 no_received_field = None
36 # unique key for a mailbox
37 key = None
38
39 def __init__(self, name, uidvalidity, owner):
40 self.name = name
41 self.uidvalidity = uidvalidity
42 self.owner = owner
43
44 self.uids = []
45 self.needs_save = False
46
47 self.date_conflicts = 0
48 self.no_received_field = 0
49
50 #special key to ensure better mailbox uniqueness
51 self.key = self.name.strip('"') + self.uidvalidity
52
53 return
54
55 def __getstate__(self):
56 """Prepares the MailboxState instance for pickling."""
57 changed_dict = self.__dict__.copy()
58 # remove the following attributes for pickling
59 del changed_dict['needs_save']
60 del changed_dict['date_conflicts']
61 del changed_dict['no_received_field']
62 #print("pickling preparation complete")
63 return changed_dict
64
65 def __setstate__(self, dict):
66 """Prepares the MailboxState instance for unpickling."""
67 self.name = dict["name"]
68 self.uidvalidity = dict["uidvalidity"]
69 self.owner = dict["owner"]
70
71 self.uids = dict["uids"]
72 self.needs_save = False
73
74 self.date_conflicts = 0
75 self.no_received_field = 0
76
77 self.key = dict["key"]
78
79 #print("unpickling preparation complete")
80 return
81
82 def __str__(self):
83 """Makes the class printable."""
84 return self.key
85
86 def synchronize(self, list_ids):
87 """Adds new messages to the cache and returns a list of them.
88 Confirm the changes to a mailbox to finally save it."""
89 new_ids = []
90 if(len(self.uids)==0):
91 new_ids = list_ids
92 else:
93 for uid in list_ids:
94 try:
95 self.uids.index(uid)
96 #print("found", uid, self.key)
97 except ValueError:
98 new_ids.append(uid)
99 #print("new", uid, self.key)
100 # update this mailbox potential uids
101 self.uids = list_ids
102 return new_ids
103
104 def confirm_change(self):
105 """Confirm the chages to the cached mailbox."""
106 self.needs_save = True
107 #print(self.owner, self.key, "committed.")
108 return