<?php
/**
 * The SyncML_Device_Synthesis:: class provides functionality that is
 * specific to the Synthesis.ch SyncML clients.
 *
 * Copyright 2005-2006 Karsten Fourmont <karsten@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * $Horde: framework/SyncML/SyncML/Device/Synthesis.php,v 1.2.2.4 2006/01/01 21:28:37 jan Exp $
 *
 * @author  Karsten Fourmont <karsten@horde.org>
 * @package SyncML
 */
class SyncML_Device_Synthesis extends SyncML_Device {

    /**
     * Converts the content from the backend to a format suitable for the
     * client device.
     *
     * Strips the uid (primary key) information as client and server might use
     * different ones.
     *
     * @param string $content       The content to convert
     * @param string $contentType   The contentType of content as returned from
     *                              the backend
     * @return array                array($newcontent, $newcontentType):
     *                              the converted content and the
     *                              (possibly changed) new ContentType.
     */
    function convertServer2Client($content, $contentType)
    {
        global $backend;

        list($content, $contentType) =
            parent::convertServer2Client($content, $contentType);

        // FIXME: just swapping out the version number in the header
        // so that the client doesn't immediately deny with a "Format
        // not supported". See http://bugs.horde.org/ticket/?id=1881.
        $content = preg_replace('/(\r\n|\r|\n)VERSION:2.0/', '\1VERSION:1.0', $content, 1);

        if (DEBUGLOG_ICALENDARDATA) {
            $fp = @fopen('/tmp/sync/log.txt', 'a');
            if ($fp) {
                @fwrite($fp, "\noutput converted for client ($contentType):\n");
                @fwrite($fp, $content . "\n");
                @fclose($fp);
            }
        }

        return array($content, $contentType);
    }

    /**
     * Synthesis currently expects notes as text/plain. Maybe we can
     * extract this from DevInf rather than hardcode it.
     */
    function getPreferredContentType($type)
    {
        if ($type == 'notes') {
            return 'text/plain';
        }
        return parent::getPreferredContentType($type);
    }

    /**
     * Returns the preferrred contenttype of the client for the given
     * sync data type (contacts/tasks/notes/calendar).
     *
     * Synthesis currently sends notes as text/plain. Maybe we can extract
     * this from DevInf rather than hardcode it.
     */
    function getPreferredContentTypeClient($type)
    {
        if ($type == 'notes') {
            return 'text/plain';
        } elseif ($type == 'tasks') {
            return array('ContentType' => 'text/x-vcalendar',
                         'ENCODING' => 'QUOTED-PRINTABLE',
                         'CHARSET' => $GLOBALS['_HORDE_STRING_CHARSET'],
                         'VERSION' => '1.0');
        } elseif ($type == 'calendar') {
            return array('ContentType' => 'text/x-vcalendar',
                         'ENCODING' => 'QUOTED-PRINTABLE',
                         'CHARSET' => $GLOBALS['_HORDE_STRING_CHARSET'],
                         'VERSION' => '1.0');
        } elseif ($type == 'contacts') {
            return array('ContentType' => 'text/x-vcard',
                         'ENCODING' => 'QUOTED-PRINTABLE',
                         'CHARSET' => $GLOBALS['_HORDE_STRING_CHARSET']);
        }

        return parent::getPreferredContentTypeClient($type);
    }

}
