''' mailbox_state.py - The module contains the MailboxState class. Copyright (c) 2012 Intra2net AG Author: Plamen Dimitrov This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ''' class MailboxState: """This class is responsible for containing and updating a mailbox data.""" # class attributes # string with quotation marks for the mailbox name name = None # string for the mailbox uidvalidity uidvalidity = None # string for user owning the mailbox owner = None # list of bytes for last cached mail uids uids = None # tolerance with which the mailbox was synced tolerance = None # boolean flag for committing state changes needs_save = None # integer for found date conflicts date_conflicts = None # integer for found messages with missing received headers no_received_field = None # unique key for a mailbox key = None def __init__(self, name, uidvalidity, owner): self.name = name self.uidvalidity = uidvalidity self.owner = owner self.uids = [] self.tolerance = 0 self.needs_save = False self.date_conflicts = 0 self.no_received_field = 0 #special key to ensure better mailbox uniqueness self.key = self.name.strip('"') + self.uidvalidity return def __getstate__(self): """Prepares the MailboxState instance for pickling.""" changed_dict = self.__dict__.copy() # remove the following attributes for pickling del changed_dict['needs_save'] del changed_dict['date_conflicts'] del changed_dict['no_received_field'] return changed_dict def __setstate__(self, dict): """Prepares the MailboxState instance for unpickling.""" self.name = dict["name"] self.uidvalidity = dict["uidvalidity"] self.owner = dict["owner"] self.uids = dict["uids"] self.tolerance = dict["tolerance"] self.needs_save = False self.date_conflicts = 0 self.no_received_field = 0 self.key = dict["key"] return def __str__(self): """Makes the class printable.""" return self.key def synchronize(self, list_ids, tolerance): """Adds new messages to the cache and returns a list of them. Confirm the changes to a mailbox to finally save it.""" new_ids = [] # cache is invalid if mailbox is synced with different tolerance if(len(self.uids)==0 or tolerance != self.tolerance): new_ids = list_ids self.tolerance = tolerance else: for uid in list_ids: try: self.uids.index(uid) except ValueError: new_ids.append(uid) # update this mailbox potential uids self.uids = list_ids return new_ids def confirm_change(self): """Confirm the chages to the cached mailbox and specify used tolerance.""" self.needs_save = True return