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