#!/usr/bin/env python import time import exceptions import sys import os import outlook import samsung_phone.calendar import gregorian_calendar import text_encoding.UTF8 import push def calculate_reminder_minutes(mode, value): if value is None: return 0 if mode == samsung_phone.calendar.RemindMode.no: remind_minutes = 0 elif mode == samsung_phone.calendar.RemindMode.minute: remind_minutes = value elif mode == samsung_phone.calendar.RemindMode.hour: remind_minutes = value * 60 elif mode == samsung_phone.calendar.RemindMode.day: remind_minutes = value * 60 * 24 elif mode == samsung_phone.calendar.RemindMode.week: remind_minutes = value * 60 * 24 * 7 else: assert(False) return remind_minutes def calculate_repeat_minutes(mode, value): if value is None: return 0 if mode == samsung_phone.calendar.RepeatMode.month: return value * 60 * 24 * 7 * 4 else: return calculate_reminder_minutes(mode, value) def sync_items(cellphone_calendar_connection_1): """ Note that this also fetches items FROM the cell phone and puts them into your Outlook calendar. If you don't want that, use push.push_items instead. returns: pushed_items, remote-only_items """ remote_items = set(cellphone_calendar_connection_1.get_all()) current_gregorian_time = gregorian_calendar.gregorian_from_OLE_time(gregorian_calendar.make_OLE_time_from_UNIX(time.time())) remote_min_start = min([item.start for item in remote_items] + [current_gregorian_time]) remote_min_start = gregorian_calendar.make_time(remote_min_start[ : 3] + [0, 0, 0]) # beginning of the day of the minimum entry on the phone as UNIX time. items = push.prepare_push_items(remote_min_start) for item in items: if item in remote_items: remote_items.remove(item) # #remote_items is now remote-only items. for item in remote_items: #print "remote-only FIXME put into Outlook:", item # TODO check whether only one or two fields differs or something, then assume it's the same item. # TODO repeat mode remind_minutes = calculate_reminder_minutes(item.remind_mode, item.remind_value) new_appointment = outlook.create_appointment(Start = gregorian_calendar.make_OLE_time(item.start), Duration = gregorian_calendar.duration_in_seconds(item.start, item.end) // 60, Subject = item.subject.decode("utf-8"), Body = item.details.decode("utf-8"), Location = item.location.decode("utf-8"), ReminderMinutesBeforeStart = remind_minutes, ReminderSet = remind_minutes > 0) #: # modal. .Save() if len(item.start) > 4 and item.start[3 : 5] == [0, 0] and item.end[0 : 3] == item.start[0 : 3]: # Y,m,d,h,m,s new_appointment.AllDayEvent = True new_appointment.Display(True) # modal. if new_appointment.Saved: #item.repeat_mode = RepeatMode.no #item.repeat_until = [None, None, None] # [year, month, day] # add the new item(s) to #items if neccessary (new from the cell, was not in outlook before) so it is not trashed. # unsafe to use the existing item; what if the user modified the appointment in the GUI. Better just recreate the cellphone item: B_all_day = True low_cutoff = gregorian_calendar.gregorian_from_OLE_time(new_appointment.Start)[ : len(gregorian_calendar.future_end_limit)] item, next_occurences = outlook.get_next_times_for_appointment(new_appointment, low_cutoff) for start, end in next_occurences: item = push.prepare_push_item(new_appointment, start, end) items.append(item) else: new_appointment.Delete() # #items is now all items that should be there. cellphone_calendar_connection_1.truncate() items = [item for item in items if item.start >= current_gregorian_time] # just transfer future appointments. push.push_items(cellphone_calendar_connection_1, items) return items, remote_items if __name__ == "__main__": cellphone_calendar_connection_1 = samsung_phone.calendar.Connection() sync_items(cellphone_calendar_connection_1)