- Qt 5.0
- Qt Organizer Overview
- Qt 5.0.0 Reference Documentation

# Qt Organizer Overview

The Qt Organizer API enables a client to request calendar, schedule and personal data from local or remote backends.

With the Qt Organizer API typical use cases are:

- Access a list of calendar events from the calendar database supported by the selected backend.
- Import iCalendar events into the selected calendar database.
- Share events by exporting iCalendar content.

The Qt Organizer provides both a synchronous and an asynchronous API. Note that for clarity, the short examples on this page demonstrate the synchronous API. While these code snippets might be useful for non-GUI applications, it is highly recommended that the asynchronous API is used for GUI applications.

A full list of classes can be found in Qt Organizer C++ API.

## Instantiating Organizer Manager

Organizer information is stored in datastores whose functionality is exposed via the QOrganizerManager class.

Most users of the API will want to use the default manager for the platform, which provides access to the system address book. Instantiating a manager by using the default constructor will result in the default manager for that platform being instantiated:

     QOrganizerManager defaultManager;

## Creating New Items

You can create a new item simply by instantiating one and saving it in a manager.

     // a default constructed journal will have it's date/time set to the current date/time.
     QOrganizerJournal journal;
     journal.setDescription("The conference went well.  We all agree that marshmallows are awesome, "\
                     "but we were unable to reach any agreement as to how we could possibly "\
                     "increase our intake of marshmallows.  Several action points were assigned "\
                     "to various members of the group; I have been tasked with finding a good "\
                     "recipe that combines both marshmallows and chocolate, by next Wednesday.");
     defaultManager.saveItem(&journal);

### Retrieving Items

You can request all items from the manager that occur in a given time range.

     QList<QOrganizerItem> entries =
         defaultManager.items(QDate(2010, 1, 1).startOfDay(),
                              QDate(2010, 1, 31).endOfDay());

It is also possible to filter the items on the value of a detail.

     entries = defaultManager.items(QOrganizerItemLocation::match("Meeting Room 8"));

The above code will retrieve both items that have been saved to the manager and items which are generated based on recurrence rules. Given a recurring item, it is possible to retrieve a list of items that it generates; that is, to get a list of the upcoming occurrences of a single recurring item. This can be done using QOrganizerManager::itemOccurrences():

You can also retrieve a particular existing item from a manager, by directly requesting the item with a particular (previously known) id. The synchronous API provides the QOrganizerManager::item() function to retrieve a single item by its id. With the asynchronous API, this can be done using a QOrganizerItemFetchRequest and a QOrganizerItemIdFilter.

## Updating Existing Items

You can update a previously saved item retrieving the item, modifying it, and saving it back to the manager. The manager uses the id of the item to match up the provided item with the one in the database.

     journal.addComment("Serves: 8.  Ingredients: 500g Milk Chocolate, 500g Marshmallows."\
                     "  Step 1: Put the marshmallows into 8 separate bowls."\
                     "  Step 2: Melt the chocolate."\
                     "  Step 3: Pour the chocolate over the marshmallows in the bowls."\
                     "  Step 4: Put the bowls into the refrigerator for 20 minutes; serve chilled.");
     if (!defaultManager.saveItem(&journal))
         qDebug() << "Unable to save updated journal!  Error:" << defaultManager.error();

## Removing Items

You can remove an item from the manager by using its id.

     defaultManager.removeItem(journal.id());

## The Organizer Item Model

### Items

A QOrganizerItem represents an event, todo, journal or note.

Each item stored in a manager is identified by a QOrganizerItemId. The id is the means by which the manager can:

- Determine whether a save operation should make a new item or update an existing one. (If an item has a null id, it should be saved as a new item)
- Match an item to an existing one for updating.
- Link between items (for example, in QOrganizerItemParent).

The QOrganizerItem class provides a generic interface for accessing events, todos, journals and notes. To actually access specific fields of an item, convenience subclasses of QOrganizerItem are offered. These are QOrganizerEvent, QOrganizerTodo, QOrganizerJournal and QOrganizerNote. Additionally, QOrganizerEventOccurrence and QOrganizerTodoOccurrence can be used for manipulating occurrences of event or todos (see the Recurring Items section). Here is an example of how to retrieve details specific to an item:

     QList<QOrganizerItem> items = defaultManager.items();
     foreach (QOrganizerItem item, entries) {
         if (item.type() == QOrganizerItemType::TypeEvent) {
             QOrganizerEvent event(item);
             qDebug() << "Event:" << event.startDateTime() << ", " << event.displayLabel();
         } else if (item.type() == QOrganizerItemType::TypeEventOccurrence) {
             QOrganizerEventOccurrence event(item);
             qDebug() << "Event:" << event.startDateTime() << ", " << event.displayLabel();
         } else if (item.type() == QOrganizerItemType::TypeTodo) {
             // process todos
         } else if (item.type() == QOrganizerItemType::TypeTodoOccurrence) {
             // process recurring todos
         } else if (item.type() == QOrganizerItemType::TypeJournal) {
             // process journals
         } else if (item.type() == QOrganizerItemType::TypeNote) {
             // process notes
         }
     }

### Recurring Items

A recurring item is an item that occurs more than once; for example, a meeting that occurs every week for the next 10 weeks. A recurring item is created by creating a QOrganizerEvent or QOrganizerTodo and setting a QOrganizerRecurrenceRule on it to specify the rules for when it should recur. When QOrganizerManager::items() is called, recurring items are not returned. Rather, they expanded into multiple QOrganizerEventOccurrence and QOrganizerTodoOccurrence items. Each generated occurrence item has a null id.

You can make an exception for an occurrence by taking a generated item occurrence from the manager, making the necessary modifications, and resaving it. When the manager is then queried with QOrganizerManager::items(), it will return the list of occurrences as before, but with the modifications in place. The modified item will be given a non-null id, and replaces the generated one in the list.

Here is an example of changing a single occurrence of an item:

     QOrganizerEventOccurrence nextMarshmallowMeeting = defaultManager.itemOccurrences(marshmallowMeeting).value(0);
     nextMarshmallowMeeting.setStartDateTime(QDateTime::fromString("13.05.2010 18:00:00", "dd.MM.yy hh:mm:ss"));
     nextMarshmallowMeeting.setEndDateTime(QDateTime::fromString("13.05.2010 20:00:00", "dd.MM.yy hh:mm:ss"));
     nextMarshmallowMeeting.addComment("The next meeting will go for an hour longer (starting one "\
                                       "hour earlier than usual), since we have scheduled one hour"\
                                       "to taste the results of the recipe that I will be presenting "\
                                       "at the meeting.");
     defaultManager.saveItem(&nextMarshmallowMeeting);

You can also query the manager for a list of unexpanded items by calling QOrganizerManager::itemsForExport(). The list of returned items will contain all items that have been saved to the manager with a call to QOrganizerManager::saveItem() That is, recurring events will be returned as is, and event occurrences will not appear unless they are exceptions (ie. have a non-null id). Fetching the list in this way can be useful for transfering items to other managers or for exporting to iCalendar with QtVersit.

### Collections

Every item stored in a manager belongs to exactly one collection. A collection can have properties such as a name, a "color", a specified icon, a description, and so on. Collections may be added or removed if the manager supports those operations, or modified. There will always be at least one collection in a manager, and the manager will always have a default collection into which items are saved if no other collection is specified.

Some managers will allow users to create collections (for example, a "football fixtures" collection) while others may have built-in collections (for example, "work" and "home" collections).

A list of all collections can be retrieved from a manager with one function call:

     QList<QOrganizerCollection> collections = defaultManager.collections();

To save an item to a collection, set the collection ID on the item object. If the collection id is the null id, the item will be saved in the collection in which it is currently saved (if the item already exists) or into the manager's default collection (if it is a new item).

     marshmallowMeeting.setCollectionId(collection.id());
     defaultManager.saveItem(&marshmallowMeeting);

To retrieve all items in a collection, a QOrganizerItemCollectionFilter should be used.

     QOrganizerItemCollectionFilter collectionFilter;
     collectionFilter.setCollectionId(collection.id());
     items = defaultManager.items(collectionFilter);

### All-day Events

Events and Todos can be specified as all-day or multi-day by setting the AllDay field to `true` (using QOrganizerEvent::setAllDay()). When this field is set to true, it means that the time portion of the StartDateTime and EndDateTime should be ignored. An event or todo marked as all-day should be considered to start and end roughly on its given start and end dates (inclusive), but without specifying exact times. For example, a birthday could be specified as an all-day QOrganizerEvent where the StartDateTime and EndDateTime have the same value.

## Using Asynchronous and Synchronous API

The asynchronous API provides a way to access or modify the organizer item information managed by a particular backend via non-blocking, asynchronous requests. It is recommended for most applications that the asynchronous API be used where possible.

The asynchronous API is offered through various classes derived from the QOrganizerAbstractRequest class, including QOrganizerItemIdFetchRequest, QOrganizerItemFetchRequest, QOrganizerItemFetchForExportRequest, QOrganizerItemSaveRequest, QOrganizerItemRemoveRequest, QOrganizerItemOccurrenceFetchRequest, QOrganizerCollectionFetchRequest, QOrganizerCollectionRemoveRequest, and QOrganizerCollectionSaveRequest.

The asynchronous API allows manipulation of items, and collections, but does not provide manager capability information reporting.

The synchronous API provides the simplest way to access or modify the organizer item information managed by a particular backend. It has the disadvantage that calls block the current thread of execution until completion and is therefore most suitable only for applications which interact with local, high-speed datastores, or for applications which do not require a responsive user interface.

The synchronous API is offered through the QOrganizerManager class, and includes manipulation of items, and collections.

It is possible for third party developers to implement a manager engine plugin from which clients may request data. For more information on this topic (for example, if you intend to implement a manager backend) please see Qt Organizer Manager Engines.

See also Qt Organizer Asynchronous API, Qt Organizer Synchronous API, and Qt Organizer API Advanced Usage.