[Svn] r4205 - in trunk/plugins/kolab_addressbook: . lib

trac at roundcube.net trac at roundcube.net
Tue Nov 9 22:04:28 CET 2010


Author: thomasb
Date: 2010-11-09 15:04:28 -0600 (Tue, 09 Nov 2010)
New Revision: 4205

Modified:
   trunk/plugins/kolab_addressbook/kolab_addressbook.php
   trunk/plugins/kolab_addressbook/lib/rcube_kolab.php
   trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php
Log:
Little code cleanup: use static getters of rcube_kolab class to get Kolab_Storage objects

Modified: trunk/plugins/kolab_addressbook/kolab_addressbook.php
===================================================================
--- trunk/plugins/kolab_addressbook/kolab_addressbook.php	2010-11-09 18:04:40 UTC (rev 4204)
+++ trunk/plugins/kolab_addressbook/kolab_addressbook.php	2010-11-09 21:04:28 UTC (rev 4205)
@@ -16,7 +16,6 @@
  */
 class kolab_addressbook extends rcube_plugin
 {
-    private $kolab;
     private $folders;
     private $sources;
 
@@ -30,11 +29,21 @@
         
         $this->add_hook('addressbooks_list', array($this, 'address_sources'));
         $this->add_hook('addressbook_get', array($this, 'get_address_book'));
-        $this->add_hook('imap_init', array($this, 'imap_init'));
 
         // extend include path to load bundled Horde classes
         $include_path = $this->home . '/lib' . PATH_SEPARATOR . ini_get('include_path');
         set_include_path($include_path);
+
+        // extend list of address sources to be used for autocompletion
+        $rcmail = rcmail::get_instance();
+        if ($rcmail->action == 'autocomplete' || $rcmail->action == 'group-expand') {
+            $sources = (array) $rcmail->config->get('autocomplete_addressbooks', array());
+            foreach ($this->_list_sources() as $abook_id => $abook) {
+                if (!in_array($abook_id, $sources))
+                    $sources[] = $abook_id;
+            }
+            $rcmail->config->set('autocomplete_addressbooks', $sources);
+        }
     }
 
     /**
@@ -48,12 +57,42 @@
      */
     public function address_sources($p)
     {
-        // setup Kolab backend
-        rcube_kolab::setup();
+        foreach ($this->_list_sources() as $abook_id => $abook) {
+            // register this address source
+            $p['sources'][$abook_id] = array(
+                'id' => $abook_id,
+                'name' => $abook->get_name(),
+                'readonly' => $abook->readonly,
+                'groups' => $abook->groups,
+            );
+        }
 
+        return $p;
+    }
+
+
+    /**
+     * Getter for the rcube_addressbook instance
+     */
+    public function get_address_book($p)
+    {
+        if ($this->sources[$p['id']]) {
+            $p['instance'] = $this->sources[$p['id']];
+        }
+        
+        return $p;
+    }
+    
+    
+    private function _list_sources()
+    {
+        // already read sources
+        if (isset($this->sources))
+            return $this->sources;
+
         // get all folders that have "contact" type
-        $this->kolab = Kolab_List::singleton();
-        $this->folders = $this->kolab->getByType('contact');
+        $this->folders = rcube_kolab::get_folders('contact');
+        $this->sources = array();
 
         if (PEAR::isError($this->folders)) {
             raise_error(array(
@@ -68,40 +107,10 @@
                 $abook_id = strtolower(asciiwords(strtr($c_folder->name, '/.', '--')));
                 $abook = new rcube_kolab_contacts($c_folder->name);
                 $this->sources[$abook_id] = $abook;
-                
-                // register this address source
-                $p['sources'][$abook_id] = array(
-                    'id' => $abook_id,
-                    'name' => $c_folder->name,
-                    'readonly' => $abook->readonly,
-                    'groups' => $abook->groups,
-                );
             }
         }
-
-        return $p;
-    }
- 
- 
-    /**
-     * Getter for the rcube_addressbook instance
-     */
-    public function get_address_book($p)
-    {
-        if ($this->sources[$p['id']]) {
-            $p['instance'] = $this->sources[$p['id']];
-        }
         
-        return $p;
+        return $this->sources;
     }
- 
- 
-    /**
-     * Make sure the X-Kolab-Type headers are also fetched when listing messages
-     */
-    function imap_init($p)
-    {
-        $p['fetch_headers'] = strtoupper('X-Kolab-Type');
-        return $p;
-    }
+
 }

Modified: trunk/plugins/kolab_addressbook/lib/rcube_kolab.php
===================================================================
--- trunk/plugins/kolab_addressbook/lib/rcube_kolab.php	2010-11-09 18:04:40 UTC (rev 4204)
+++ trunk/plugins/kolab_addressbook/lib/rcube_kolab.php	2010-11-09 21:04:28 UTC (rev 4205)
@@ -1,6 +1,5 @@
 <?php
 
-require_once 'Horde/Kolab/Format/XML.php';
 require_once 'Horde/Kolab/Storage/List.php';
 require_once 'Horde/Auth.php';
 require_once 'Horde/Auth/kolab.php';
@@ -16,6 +15,7 @@
 {
     private static $horde_auth;
     
+    
     /**
      * Setup the environment needed by the Kolab_* classes to access Kolab data
      */
@@ -49,5 +49,47 @@
         }
     }
 
+    /**
+     * Get a list of storage folders for the given data type
+     *
+     * @param string Data type to list folders for (contact,event,task,note)
+     * @return array List of Kolab_Folder objects
+     */
+    public static function get_folders($type)
+    {
+        self::setup();
+        $kolab = Kolab_List::singleton();
+        return $kolab->getByType($type);
+    }
 
+    /**
+     * Get storage object for read/write access to the Kolab backend
+     *
+     * @param string IMAP folder to access
+     * @param string Object type to deal with (leave empty for auto-detection using annotations)
+     * @return object Kolab_Data The data storage object
+     */
+    public static function get_storage($folder, $data_type = null)
+    {
+        self::setup();
+        $kolab = Kolab_List::singleton();
+        return $kolab->getFolder($folder)->getData($data_type);
+    }
+
+    /**
+     * Cleanup session data when done
+     */
+    public static function shutdown()
+    {
+        if (isset($_SESSION['__auth'])) {
+            // unset auth data from session. no need to store it persistantly
+            unset($_SESSION['__auth']);
+            
+            // FIXME: remove strange numeric entries
+            foreach ($_SESSION as $key => $val) {
+                if (!$val && is_numeric($key))
+                    unset($_SESSION[$key]);
+            }
+        }
+    }
 }

Modified: trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php
===================================================================
--- trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php	2010-11-09 18:04:40 UTC (rev 4204)
+++ trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php	2010-11-09 21:04:28 UTC (rev 4205)
@@ -16,8 +16,6 @@
     public $readonly = true;
     public $groups = true;
 
-    private static $instance;
-    
     private $gid;
     private $imap;
     private $kolab;
@@ -38,19 +36,28 @@
             $this->imap_folder = $imap_folder;
         
         // fetch objects from the given IMAP folder
-        $this->kolab = Kolab_List::singleton();
-        $this->folder = $this->kolab->getFolder($this->imap_folder);
-        $this->contactstorage = $this->folder->getData();
-        $this->liststorage = $this->folder->getData('distributionlist');
+        $this->contactstorage = rcube_kolab::get_storage($this->imap_folder);
+        $this->liststorage = rcube_kolab::get_storage($this->imap_folder, 'distributionlist');
 
         $this->ready = !PEAR::isError($this->contactstorage) && !PEAR::isError($this->liststorage);
     }
 
 
     /**
+     * Getter for the address book name to be displayed
+     *
+     * @return string Name of this address book
+     */
+    public function get_name()
+    {
+        return strtr(preg_replace('!^(INBOX|user)/!i', '', $this->imap_folder), '/', ':');
+    }
+
+
+    /**
      * Setter for the current group
      */
-    function set_group($gid)
+    public function set_group($gid)
     {
         $this->gid = $gid;
     }
@@ -132,57 +139,8 @@
         
         return $this->result;
     }
-    
-    
-    /**
-     * Simply fetch all records and store them in private member vars
-     */
-    private function _fetch_data()
-    {
-        if (!isset($this->contacts)) {
-            // read contacts
-            $this->contacts = $this->id2uid = array();
-            foreach ((array)$this->contactstorage->getObjects() as $record) {
-                $contact = $this->_to_rcube_contact($record);
-                $id = $contact['ID'];
-                $this->contacts[$id] = $contact;
-                $this->id2uid[$id] = $record['uid'];
-            }
-            
-            // read distribution-lists AKA groups
-            $this->distlists = array();
-            foreach ((array)$this->liststorage->getObjects() as $record) {
-                $record['ID'] = md5($record['uid']);
-                foreach ($record['member'] as $i => $member)
-                    $record['member'][$i]['ID'] = md5($member['uid']);
-                $this->distlists[$record['ID']] = $record;
-            }
 
-            // TODO: sort data arrays according to desired list sorting
-        }
-    }
-    
-    
-    /**
-     * Map fields from internal Kolab_Format to Roundcube contact format
-     */
-    private function _to_rcube_contact($record)
-    {
-        return array(
-          'ID' => md5($record['uid']),
-          'name' => $record['full-name'],
-          'firstname' => $record['given-name'],
-          'surname' => $record['last-name'],
-          'email' => $record['emails'],
-        );
-    }
 
-    private function _from_rcube_contact($contact)
-    {
-        // TBD.
-    }
-
-
     /**
      * Search records
      *
@@ -193,7 +151,7 @@
      */
     public function search($fields, $value, $strict=false, $select=true)
     {
-        // no search implemented, just list all records
+        // TODO: currently not implemented, just list all records
         return $this->list_records();
     }
 
@@ -231,10 +189,13 @@
     public function get_record($id, $assoc=false)
     {
         $this->_fetch_data();
-        if ($this->contacts[$id]) {
+        if ($this->contacts[$id] && $assoc) {
+            return $this->contacts[$id];
+        }
+        else if ($this->contacts[$id]) {
             $this->result = new rcube_result_set(1);
             $this->result->add($this->contacts[$id]);
-            return $assoc ? $rec : $this->result;
+            return $this->result;
         }
 
         return false;
@@ -268,7 +229,7 @@
      */
     function close()
     {
-        
+        rcube_kolab::shutdown();
     }
 
 
@@ -296,5 +257,57 @@
     {
          return false;
     }
-  
+
+
+    /**
+     * Simply fetch all records and store them in private member vars
+     */
+    private function _fetch_data()
+    {
+        if (!isset($this->contacts)) {
+            // read contacts
+            $this->contacts = $this->id2uid = array();
+            foreach ((array)$this->contactstorage->getObjects() as $record) {
+                $contact = $this->_to_rcube_contact($record);
+                $id = $contact['ID'];
+                $this->contacts[$id] = $contact;
+                $this->id2uid[$id] = $record['uid'];
+            }
+            
+            // read distribution-lists AKA groups
+            $this->distlists = array();
+            foreach ((array)$this->liststorage->getObjects() as $record) {
+                // FIXME: folders without any distribution-list objects return contacts instead ?!
+                if ($record['__type'] != 'Group')
+                    continue;
+                $record['ID'] = md5($record['uid']);
+                foreach ($record['member'] as $i => $member)
+                    $record['member'][$i]['ID'] = md5($member['uid']);
+                $this->distlists[$record['ID']] = $record;
+            }
+
+            // TODO: sort data arrays according to desired list sorting
+        }
+    }
+    
+    
+    /**
+     * Map fields from internal Kolab_Format to Roundcube contact format
+     */
+    private function _to_rcube_contact($record)
+    {
+        return array(
+          'ID' => md5($record['uid']),
+          'name' => $record['full-name'],
+          'firstname' => $record['given-name'],
+          'surname' => $record['last-name'],
+          'email' => $record['emails'],
+        );
+    }
+
+    private function _from_rcube_contact($contact)
+    {
+        // TBD.
+    }
+
 }

_______________________________________________
http://lists.roundcube.net/mailman/listinfo/svn



More information about the Svn mailing list