Revision
107
Author
emsmith
Date
2007-01-08 14:02:03 -0800 (Mon, 08 Jan 2007)

Log Message

CC_os typos and bugs, window class now has cc prefixed config vars, config and ini class now work with -> syntax

Modified Paths

Diff

Modified: trunk/lib/config.class.php (106 => 107)


--- trunk/lib/config.class.php	2007-01-08 16:19:22 UTC (rev 106)
+++ trunk/lib/config.class.php	2007-01-08 22:02:03 UTC (rev 107)
@@ -22,7 +22,7 @@
  * CC_Config - individual drivers should READ in data during construct and
  * WRITE out data during destruct
  */
-abstract class CC_Config implements ArrayAccess
+abstract class CC_Config
 {
 
 	/**
@@ -38,8 +38,8 @@
 	static public $class;
 
 	/**
-	 * multi-dim array of current configuration information
-	 * @var $data array
+	 * multi-dim object of current configuration information
+	 * @var $data object
 	 */
 	protected $data;
 
@@ -53,37 +53,37 @@
 	abstract protected function __construct();
 
 	/**
-	 * public function offsetGet
+	 * public function __get
 	 *
 	 * gets item from data store
 	 *
 	 * @param string $name
 	 * @return mixed
 	 */
-	public function offsetGet($name)
+	public function __get($name)
 	{
-		if (isset($this->data[$name]))
+		if (isset($this->data->$name))
 		{
-			return $this->data[$name];
+			return $this->data->$name;
 		}
 		return;
 	}
 
 	/**
-	 * public function offsetExists
+	 * public function __isset
 	 *
 	 * checks if item is set in data store
 	 *
 	 * @param string $name
 	 * @return mixed
 	 */
-	public function offsetExists($name)
+	public function __isset($name)
 	{
-		return isset($this->data[$name]);
+		return isset($this->data->$name);
 	}
 
 	/**
-	 * public function offsetSet
+	 * public function __set
 	 *
 	 * sets an item in the data store and writes out the file
 	 *
@@ -91,23 +91,27 @@
 	 * @param mixed $value
 	 * @return void
 	 */
-	public function offsetSet($name, $value)
+	public function __set($name, $value)
 	{
-		$this->data[$name] = $value;
+		if (is_array($value))
+		{
+			$value = (object) $value;
+		}
+		$this->data->$name = $value;
 		return;
 	}
 
 	/**
-	 * public function offsetUnset
+	 * public function __unset
 	 *
 	 * removes item from the data store and writes out the file
 	 *
 	 * @param string $name
 	 * @return void
 	 */
-	public function offsetUnset($name)
+	public function __unset($name)
 	{
-		unset ($this->data[$name]);
+		unset ($this->data->$name);
 		return;
 	}
 

Modified: trunk/lib/ini.class.php (106 => 107)


--- trunk/lib/ini.class.php	2007-01-08 16:19:22 UTC (rev 106)
+++ trunk/lib/ini.class.php	2007-01-08 22:02:03 UTC (rev 107)
@@ -47,11 +47,11 @@
 		$this->file = $folders->get_appdata() . strtolower(CC::$program) . '.config.ini';
 		if (file_exists($this->file))
 		{
-			$this->data = parse_ini_file($this->file);
+			$this->data = (object) parse_ini_file($this->file);
 		}
 		else
 		{
-			$this->data = array();
+			$this->data = new stdclass();
 		}
 		return;
 	}
@@ -67,28 +67,62 @@
 	{
 		$string = CC::i18n(';Preferences and Configuration for Callicore ') . CC::$program
 			. EOL . CC::i18n('; Saved ') . date('Y-m-d H:i:s') . EOL . EOL;
+		$sections = '';
 		foreach ($this->data as $key => $value)
 		{
-			if (is_array($value))
+			if(is_scalar($value))
 			{
-				$key = preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key) . '[]';
-				foreach ($value as $subvalue)
-				{
-					$string .= $key . ' = "' . str_replace('"', '', $subvalue) . '"' . EOL;
-				}
+				$string .= $this->stringify($key, $value);
 			}
-			elseif (is_bool($value))
-			{
-				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
-					. ' = ' . (($value == true) ? 'TRUE' : 'FALSE') . EOL;
-			}
 			else
 			{
-				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
-					. ' = "' . str_replace('"', '', $value) . '"' . EOL;
+				$key = preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key);
+				$sections .= '[' . $key . ']' . EOL;
+				foreach($value as $key => $value)
+				{
+					$sections .= $this->stringify($key, $value);
+				}
 			}
 		}
-		file_put_contents($this->file, $string);
+		file_put_contents($this->file, $string.$sections);
 		return;
 	}
+
+	/**
+	 * public function stringify
+	 *
+	 * takes a php variable and stringifies it so it can be stored in an ini file
+	 * properly
+	 *
+	 * @param type $name about
+	 * @return string
+	 */
+	protected function stringify($key, $var)
+	{
+		$string = '';
+		if (is_bool($var))
+		{
+			$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
+				. ' = ' . (($var == true) ? 'TRUE' : 'FALSE') . EOL;
+		}
+		elseif (is_scalar($var))
+		{
+			$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
+				. ' = "' . str_replace('"', '', $var) . '"' . EOL;
+		}
+		else
+		{
+			$key = preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key) . '[]';
+			foreach ($var as $value)
+			{
+				if(!is_scalar($value))
+				{
+					throw new CC_Exception('CC_Ini can only nest arrays two deep due to limitations in ini files');
+				}
+				$string .= $key . '[] = "' . str_replace('"', '', $value) . '"' . EOL;
+			}
+		}
+
+		return $string;
+	}
 }
\ No newline at end of file

Modified: trunk/lib/os.class.php (106 => 107)


--- trunk/lib/os.class.php	2007-01-08 16:19:22 UTC (rev 106)
+++ trunk/lib/os.class.php	2007-01-08 22:02:03 UTC (rev 107)
@@ -81,7 +81,7 @@
 	 */
 	public function get_profile()
 	{
-		if (stristr(PHP_OS, 'win32'))
+		if (stristr(PHP_OS, 'win'))
 		{
 			$env = $_ENV + $_SERVER;
 			if (isset($env['USERPROFILE']))
@@ -107,8 +107,8 @@
 			{
 				return $_ENV['HOME'] . DS;
 			}
-			elseif ($this->os == self::MAC && isset($_ENV['USER']) &&
-				file_exists('/Users/' . $_ENV['USER']))
+			elseif ((stristr(PHP_OS, 'darwin') || stristr(PHP_OS, 'mac')) &&
+				isset($_ENV['USER']) && file_exists('/Users/' . $_ENV['USER']))
 			{
 				return '/Users/' . $_ENV['USER'] . DS;
 			}
@@ -135,7 +135,7 @@
 	public function get_appdata()
 	{
 		// if win doesn't have appdata, we force the issue in the user profile
-		if (stristr(PHP_OS, 'win32'))
+		if (stristr(PHP_OS, 'win'))
 		{
 			$path = $this->get_profile() . 'Application Data' . DS . 'Callicore' . DS;
 		}
@@ -171,7 +171,7 @@
 	public function get_documents()
 	{
 		// we always use wscript and com on windows because we want "my documents"
-		if (stristr(PHP_OS, 'win32'))
+		if (stristr(PHP_OS, 'win'))
 		{
 			$shell = new COM('WScript.Shell');
 			$documents = $shell->SpecialFolders('MyDocuments');

Modified: trunk/lib/window.class.php (106 => 107)


--- trunk/lib/window.class.php	2007-01-08 16:19:22 UTC (rev 106)
+++ trunk/lib/window.class.php	2007-01-08 22:02:03 UTC (rev 107)
@@ -76,7 +76,7 @@
 
 		if (empty($this->name))
 		{
-			$this->set_name(strtolower(get_class($this)));
+			$this->set_name(str_replace('cc_', '', strtolower(get_class($this))));
 		}
 
 		// size and config restoration cannot be done during show signal
@@ -86,22 +86,26 @@
 			CC_Config::$class = 'CC_Ini';
 		}
 		$config = CC_Config::instance();
+		if (!isset($config->cc))
+		{
+			$config->cc = new stdclass();
+		}
 
 		// default size and location
-		$width = isset($config->{$name . '_height'}) ? (int) $config->{$name . '_height'} : 800;
-		$height = isset($config->{$name . '_width'}) ? (int) $config->{$name . '_width'} : 600;
+		$width = isset($config->cc->{$name . '_height'}) ? (int) $config->cc->{$name . '_height'} : 800;
+		$height = isset($config->cc->{$name . '_width'}) ? (int) $config->cc->{$name . '_width'} : 600;
 		$this->set_default_size($width, $height);
 
-		$x = isset($config->{$name . '_x'}) ? (int) $config->{$name . '_x'} : null;
-		$y = isset($config->{$name . '_y'}) ? (int) $config->{$name . '_y'} : null;
+		$x = isset($config->cc->{$name . '_x'}) ? (int) $config->cc->{$name . '_x'} : null;
+		$y = isset($config->cc->{$name . '_y'}) ? (int) $config->cc->{$name . '_y'} : null;
 		if (!is_null($x) && !is_null($y))
 		{
 			$this->move($x, $y);
 		}
 
-		$this->fullscreen = isset($config->{$name . '_fullscreen'}) ? (bool) $config->{$name . '_fullscreen'} : false;
-		$this->maximized = isset($config->{$name . '_maximized'}) ? (bool) $config->{$name . '_maximized'} : false;
-		$this->minimized = isset($config->{$name . '_minimized'}) ? (bool) $config->{$name . '_minimized'} : false;
+		$this->fullscreen = isset($config->cc->{$name . '_fullscreen'}) ? (bool) $config->cc->{$name . '_fullscreen'} : false;
+		$this->maximized = isset($config->cc->{$name . '_maximized'}) ? (bool) $config->cc->{$name . '_maximized'} : false;
+		$this->minimized = isset($config->cc->{$name . '_minimized'}) ? (bool) $config->cc->{$name . '_minimized'} : false;
 
 		$this->connect('window-state-event', array($this, 'on_state_event'));
 		$this->connect_simple_after('delete-event', array($this, 'on_state_save'));
@@ -196,9 +200,9 @@
 		$config = CC_Config::instance();
 
 		// unmax/min/fullscreen
-		$config->{$name . '_fullscreen'} = (bool) $this->fullscreen;
-		$config->{$name . '_maximized'} = (bool) $this->maximized;
-		$config->{$name . '_minimized'} = (bool) $this->minimized;
+		$config->cc->{$name . '_fullscreen'} = (bool) $this->fullscreen;
+		$config->cc->{$name . '_maximized'} = (bool) $this->maximized;
+		$config->cc->{$name . '_minimized'} = (bool) $this->minimized;
 
 		if ($this->minimized)
 		{
@@ -215,13 +219,13 @@
 
 		// size
 		list($height, $width) = $this->get_size();
-		$config->{$name . '_height'} = (int) $height;
-		$config->{$name . '_width'} = (int) $width;
+		$config->cc->{$name . '_height'} = (int) $height;
+		$config->cc->{$name . '_width'} = (int) $width;
 
 		// position
 		list($x, $y) = $this->get_position();
-		$config->{$name . '_x'} = (int) $x;
-		$config->{$name . '_y'} = (int) $y;
+		$config->cc->{$name . '_x'} = (int) $x;
+		$config->cc->{$name . '_y'} = (int) $y;
 
 		$this->destroy();
 		return;