<?php
/*
 * $RCSfile: GalleryRemoteProperties.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.6 $ $Date: 2005/08/23 03:49:52 $
 * @package Remote
 * @author Tim Miller
 */

/**
 *  This class partially mirrors the functionality of the java class
 *	java.util.Properties.
 *
 * @package Remote
 * @subpackage Classes
 */
class GalleryRemoteProperties {
    /**
     * Our internal hash map
     * @access private
     */
    var $_map;

    /**
     * Constructor
     */
    function GalleryRemoteProperties() {
	$this->_map = array();
    }

    /**
     * Retrieve a property, or null if it doesn't exist
     * @param string key
     * @param mixed default value
     * @return mixed the actual value
     */
    function getProperty($key, $defaultValue=null) {
	if (isset($this->_map[$key])) {
	    return $this->_map[$key];
	} else {
	    return $defaultValue;
	}
    }

    /**
     * Return true if the key exists in the properties map
     * @param string the key
     * @return boolean true if it exists
     */
    function hasProperty($key) {
	return isset($this->_map[$key]);
    }

    /**
     * Export the properties according to the Gallery Remote Protocol 2.0 data format
     *
     * @return string the exported properties
     */
    function listProperties() {
	$results = array();
	$results[] = '#__GR2PROTO__';
	foreach ($this->_map as $key => $value) {
	    $results[] = $key . '=' . $value;
	}

	return join("\n", $results);
    }

    /**
     * Store a key/value property.  Overwrite any existing property by the same name.  Ignore null
     * keys.  Normalize and escape line endings (\n, \r\n and \r all become \\n)
     *
     * @param string the key
     * @param string the value.
     */
    function setProperty($key, $value) {
	if ($key != null) {
	    $value = preg_replace("/(\r\n|\n|\r)/", "\\n", $value);
	    $this->_map[$key] = $value;
	}
    }
}
?>
