--- api.php.dist	2004-11-29 10:21:54.000000000 +0100
+++ api.php	2004-11-30 11:04:15.000000000 +0100
@@ -50,6 +50,12 @@
     'type' => 'string'
 );
 
+$_services['exportCalendar'] = array(
+    'args' => array('calendar' => 'string', 'contentType' => 'string'),
+    'type' => 'string'
+);
+
+
 $_services['delete'] = array(
     'args' => array('guid' => 'string'),
     'type' => 'boolean'
@@ -251,6 +257,111 @@
     }
 }
 
+
+/**
+ * Export a calendar in the requested contentType.
+ *
+ * @param string $calendar     Identify the calendar to export.
+ * @param mixed  $contentType  What format should the data be in?
+ *                             Either a string with one of:
+ *                             text/x-icalendar
+ *                             text/x-vcalendar
+ *                             or an array with options:
+ *                             'ContentType':  as above
+ *                             'ENCODING': (optional) character encoding
+ *                                         for strings fields
+ *                             'CHARSET':  (optional) charset. Like UTF-8
+ *
+ * @return string  The requested data.
+ */
+
+function _kronolith_exportCalendar($calendarName, $contentType)
+{
+
+    require_once dirname(__FILE__) . '/base.php';
+    global $kronolith, $kronolith_shares;
+
+    $calendars = Kronolith::listCalendars(false, PERMS_EDIT);
+    
+    $calendarId = false;
+
+    /* 
+      Probably there is a better way to find the id of 
+      a calendar giving its name, but...
+    */ 
+    foreach ($calendars as $id => $cal) {
+      if ( $cal->get('name') == $calendarName ) {
+        $calendarId = $id;
+      }
+    }
+
+    if ( !$calendarId ) {
+        return PEAR::raiseError(_("Calendar not found"));
+    }
+
+
+    if (!array_key_exists($calendarId, Kronolith::listCalendars(false, PERMS_EDIT))) {
+        return PEAR::raiseError(_("Permission Denied"));
+    }
+
+
+    if (is_array($contentType)) {
+        $options = $contentType;
+        $contentType = $options['ContentType'];
+        unset($options['ContentType']);
+    } else {
+        $options = array();
+    }
+
+    switch ($contentType) {
+    case 'text/calendar':
+    case 'text/x-icalendar':
+    case 'text/x-vcalendar':
+
+        require_once 'Horde/iCalendar.php';
+
+        
+        if ($kronolith->getCalendar() != $calendarId) {
+          $kronolith->close();
+          $kronolith->open($calendarId);
+        }
+
+        $events = array();
+        $events[$calendarId] = $kronolith->listEvents(null, null);
+        
+        if (!count($events)) {
+          return PEAR::raiseError(_("There were no events to export"));
+        } else {
+            $iCal = &new Horde_iCalendar();
+
+            foreach ($events as $cal => $calevents) {
+                if ($kronolith->getCalendar() != $cal) {
+                    $kronolith->close();
+                    $kronolith->open($cal);
+                }
+
+                $identity = &$kronolith_shares->getIdentityByShare($kronolith_shares->getShare($cal));
+                foreach ($calevents as $id) {
+                    $event = &$kronolith->getEvent($id);
+                    $vEvent = $event->toiCalendar($iCal, $identity);
+                    $vEvent->setParameter('SUMMARY', $options);
+                    $vEvent->setParameter('DESCRIPTION', $options);
+                    $vEvent->setParameter('LOCATION', $options);
+                    $iCal->addComponent($vEvent);
+                }
+            }
+
+          return $iCal->exportvCalendar();
+        }
+    default:
+        return PEAR::raiseError(_("Unsupported Content-Type."));
+    }
+
+
+}
+
+
+
 /**
  * Delete an event identified by GUID.
  *