Improved handling of character set conversions. Added a matchNamespace() method.
authorGunnar Wrobel <p@rdus.de>
Mon, 15 Mar 2010 06:19:19 +0000 (07:19 +0100)
committerGunnar Wrobel <wrobel@temple.(none)>
Mon, 15 Mar 2010 10:33:22 +0000 (11:33 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Namespace.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/NamespaceTest.php

index ea487ee..47041dd 100644 (file)
@@ -52,19 +52,67 @@ class Horde_Kolab_Storage_Namespace
     );
 
     /**
+     * Constructor.
+     */
+    public function __construct()
+    {
+        $this->_charset = Horde_Nls::getCharset();
+    }
+
+    /**
+     * Match a folder name with the corresponding namespace.
+     *
+     * @param string $name The name of the folder.
+     *
+     * @return array The corresponding namespace.
+     *
+     * @throws Horde_Kolab_Storage_Exception If the namespace of the folder
+     *                                       cannot be determined.
+     */
+    protected function matchNamespace($name)
+    {
+        foreach (array(self::PRIV, self::OTHER, self::SHARED) as $type) {
+            foreach ($this->_namespaces[$type] as $namespace => $delimiter) {
+                if (strpos($namespace, $name) === 0) {
+                    return array(
+                        'namespace' => $namespace,
+                        'delimiter' => $delimiter,
+                        'type'      => $type,
+                    );
+                }
+            }
+        }
+        throw new Horde_Kolab_Storage_Exception(
+            'Namespace of folder %s cannot be determined.', $name
+        );
+    }
+
+    /**
+     * Get the character set used/expected when calling the getTitle() or
+     * setName() methods.
+     *
+     * @return string The character set.
+     */
+    public function getCharset()
+    {
+        return $this->_charset;
+    }
+
+    /**
      * Return the title of a folder.
      *
      * @param string $name The name of the folder.
      *
-     * @return sring The title of the folder.
+     * @return string The title of the folder.
      */
     public function getTitle($name)
     {
+        $name = Horde_String::convertCharset($name, 'UTF7-IMAP', $this->_charset);
         if (substr($name, 0, 6) == 'INBOX/') {
             $name = substr($name, 6);
         }
         $name = str_replace('/', ':', $name);
-        return Horde_String::convertCharset($name, 'UTF7-IMAP');
+        return $name;
     }
 
     /**
@@ -100,7 +148,7 @@ class Horde_Kolab_Storage_Namespace
      *
      * @return string The sub path.
      */
-    function getSubpath($name)
+    public function getSubpath($name)
     {
         if (!preg_match(";(shared\.|INBOX[/]?|user/([^/]+)[/]?)([^@]*)(@.*)?;", $name, $matches)) {
             throw new Horde_Kolab_Storage_Exception(
@@ -117,12 +165,12 @@ class Horde_Kolab_Storage_Namespace
      *
      * @return string The IMAP folder name.
      */
-    function setName($name)
+    public function setName($name)
     {
         $name = str_replace(':', '/', $name);
         if (substr($name, 0, 5) != 'user/' && substr($name, 0, 7) != 'shared.') {
             $name = 'INBOX/' . $name;
         }
-        return Horde_String::convertCharset($name, Horde_Nls::getCharset(), 'UTF7-IMAP');
+        return Horde_String::convertCharset($name, $this->_charset, 'UTF7-IMAP');
     }
 }
\ No newline at end of file
index a27cf3a..6d8f07a 100644 (file)
@@ -70,7 +70,7 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase
 
     public function testFolderTitleConvertsUtf7()
     {
-        Horde_String::setDefaultCharset('UTF8');
+        Horde_Nls::setCharset('UTF8');
         $name = Horde_String::convertCharset('äöü', 'UTF8', 'UTF7-IMAP');
         $folder = new Horde_Kolab_Storage_Folder('INBOX/' . $name);
         $folder->restore($this->_storage, $this->_connection, new Horde_Kolab_Storage_Namespace());
@@ -133,7 +133,7 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('test', $folder->getOwner());
     }
 
-    public function testSetfolderDoesAddDefaultPersonalNamespace()
+    public function testSetnameDoesAddDefaultPersonalNamespace()
     {
         $folder = new Horde_Kolab_Storage_Folder(null);
         $folder->restore($this->_storage, $this->_connection, new Horde_Kolab_Storage_Namespace());
@@ -141,7 +141,7 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('INBOX/test/this', $folder->getName());
     }
 
-    public function testSetfolderReplacesDoubleColonWithSeparator()
+    public function testSetnameReplacesDoubleColonWithSeparator()
     {
         $folder = new Horde_Kolab_Storage_Folder(null);
         $folder->restore($this->_storage, $this->_connection, new Horde_Kolab_Storage_Namespace());
@@ -149,7 +149,7 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('INBOX/a/b/c', $folder->getName());
     }
 
-    public function testSetfolderConvertsToUtf7()
+    public function testSetnameConvertsToUtf7()
     {
         Horde_Nls::setCharset('UTF8');
         $folder = new Horde_Kolab_Storage_Folder(null);
@@ -157,7 +157,7 @@ class Horde_Kolab_Storage_NamespaceTest extends PHPUnit_Framework_TestCase
         $folder->setName('äöü');
         $this->assertEquals(
             'INBOX/äöü',
-            Horde_String::convertCharset($folder->getName(), 'UTF7-IMAP')
+            Horde_String::convertCharset($folder->getName(), 'UTF7-IMAP', 'UTF8')
         );
     }