1 #!/usr/bin/env python 2 3 """ 4 Common calendar client utilities. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.data import get_window_end, uri_items 23 from imiptools.dates import get_default_timezone 24 from imiptools.profile import Preferences 25 26 def update_attendees(obj, added, removed): 27 28 """ 29 Update the attendees in 'obj' with the given 'added' and 'removed' 30 attendee lists. A list is returned containing the attendees whose 31 attendance should be cancelled. 32 """ 33 34 to_cancel = [] 35 36 if added or removed: 37 attendees = uri_items(obj.get_items("ATTENDEE") or []) 38 sequence = obj.get_value("SEQUENCE") 39 40 if removed: 41 remaining = [] 42 43 for attendee, attendee_attr in attendees: 44 if attendee in removed: 45 46 # Without a sequence number, assume that the event has not 47 # been published and that attendees can be silently removed. 48 49 if sequence is not None: 50 to_cancel.append((attendee, attendee_attr)) 51 else: 52 remaining.append((attendee, attendee_attr)) 53 54 attendees = remaining 55 56 if added: 57 for attendee in added: 58 attendees.append((attendee, {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) 59 60 obj["ATTENDEE"] = attendees 61 62 return to_cancel 63 64 class Client: 65 66 "Common handler and manager methods." 67 68 default_window_size = 100 69 70 def __init__(self, user): 71 self.user = user 72 self.preferences = None 73 74 def get_preferences(self): 75 if not self.preferences and self.user: 76 self.preferences = Preferences(self.user) 77 return self.preferences 78 79 def get_tzid(self): 80 prefs = self.get_preferences() 81 return prefs and prefs.get("TZID") or get_default_timezone() 82 83 def get_window_size(self): 84 prefs = self.get_preferences() 85 try: 86 return prefs and int(prefs.get("window_size")) or self.default_window_size 87 except (TypeError, ValueError): 88 return self.default_window_size 89 90 def get_window_end(self): 91 return get_window_end(self.get_tzid(), self.get_window_size()) 92 93 def is_sharing(self): 94 prefs = self.get_preferences() 95 return prefs and prefs.get("freebusy_sharing") == "share" or False 96 97 def is_bundling(self): 98 prefs = self.get_preferences() 99 return prefs and prefs.get("freebusy_bundling") == "always" or False 100 101 def is_notifying(self): 102 prefs = self.get_preferences() 103 return prefs and prefs.get("freebusy_messages") == "notify" or False 104 105 # vim: tabstop=4 expandtab shiftwidth=4