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