javascriptthunderbirdlightning

How to create custom reminders in lighning with javascript?


I am creating an extension to import todos from a CRM to thunderbird/lightning. I use the calITodo interface to create my todos :

var todo = Components.classes["@mozilla.org/calendar/todo;1"].createInstance(Components.interfaces.calITodo);

But I can't find how to set reminders, or create a custom one for my todos.


Solution

  • I'm going to answer this question a bit wider to show you the alternatives, you may already be doing this.

    If you would like to use the CRM as a backend for a calendar, you may want to write a "Provider" type extension, similar to the Provider for Google Calendar. You just need to implement a few methods for the usual operations (get/add/modify/delete) to get started. See the source code for the Provider for Google Calendar as a starting point.

    If you would just like to do a one time import, then you are probably going in the right direction. Just use the addItem/adoptIte method on the calendar in question. If you need a dialog to select calendars, you can reuse this one, it is available via the uri chrome://calendar/content/chooseCalendarDialog.xul.

    Now to get to your real question. To add a reminder to an event or todo the following code helps. Of course you can choose a different alarm relation.

    Components.utils.import("resource://calendar/modules/calUtils.jsm");
    
    let todo = cal.createToDo();
    let alarm = cal.createAlarm();
    let alarmDate = cal.createDateTime();
    
    alarm.related = Components.interfaces.calIAlarm.ALARM_RELATED_ABSOLUTE;
    alarm.alarmDate = alarmDate;
    
    todo.addAlarm(alarm);
    // ...
    

    The alarm implements calIAlarm, you can find the interface description here. It is then added to the todo, which implements calITodo and also calIItemBase. For an overview of the alarm methods on an item, see here.

    If you are interested what other utility functions are available, see here. You can use functions from both files by merely importing calUtils.jsm. Just prefix each function with "cal.".