From 5d682f384cef41e36829c557bb1d59a5b7ebf702 Mon Sep 17 00:00:00 2001 From: thomasb Date: Tue, 9 Nov 2010 21:04:28 +0000 Subject: Little code cleanup: use static getters of rcube_kolab class to get Kolab_Storage objects git-svn-id: https://svn.roundcube.net/trunk@4205 208e9e7b-5314-0410-a742-e7e81cd9613c --- plugins/kolab_addressbook/kolab_addressbook.php | 85 +++++++------ plugins/kolab_addressbook/lib/rcube_kolab.php | 44 ++++++- plugins/kolab_addressbook/rcube_kolab_contacts.php | 135 +++++++++++---------- 3 files changed, 164 insertions(+), 100 deletions(-) (limited to 'plugins') diff --git a/plugins/kolab_addressbook/kolab_addressbook.php b/plugins/kolab_addressbook/kolab_addressbook.php index c20b3dd94..27f30cb12 100644 --- a/plugins/kolab_addressbook/kolab_addressbook.php +++ b/plugins/kolab_addressbook/kolab_addressbook.php @@ -16,7 +16,6 @@ require_once(dirname(__FILE__) . '/rcube_kolab_contacts.php'); */ class kolab_addressbook extends rcube_plugin { - private $kolab; private $folders; private $sources; @@ -30,11 +29,21 @@ class kolab_addressbook extends rcube_plugin $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 @@ class kolab_addressbook extends rcube_plugin */ 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 @@ class kolab_addressbook extends rcube_plugin $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; - } - - - /** - * 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; + return $this->sources; } + } diff --git a/plugins/kolab_addressbook/lib/rcube_kolab.php b/plugins/kolab_addressbook/lib/rcube_kolab.php index 44c37a68f..985691a51 100644 --- a/plugins/kolab_addressbook/lib/rcube_kolab.php +++ b/plugins/kolab_addressbook/lib/rcube_kolab.php @@ -1,6 +1,5 @@ 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]); + } + } + } } diff --git a/plugins/kolab_addressbook/rcube_kolab_contacts.php b/plugins/kolab_addressbook/rcube_kolab_contacts.php index 91ba10cb9..54effa63c 100644 --- a/plugins/kolab_addressbook/rcube_kolab_contacts.php +++ b/plugins/kolab_addressbook/rcube_kolab_contacts.php @@ -16,8 +16,6 @@ class rcube_kolab_contacts extends rcube_addressbook public $readonly = true; public $groups = true; - private static $instance; - private $gid; private $imap; private $kolab; @@ -38,19 +36,28 @@ class rcube_kolab_contacts extends rcube_addressbook $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,55 +139,6 @@ class rcube_kolab_contacts extends rcube_addressbook 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. - } /** @@ -193,7 +151,7 @@ class rcube_kolab_contacts extends rcube_addressbook */ 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 @@ class rcube_kolab_contacts extends rcube_addressbook 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 @@ class rcube_kolab_contacts extends rcube_addressbook */ function close() { - + rcube_kolab::shutdown(); } @@ -296,5 +257,57 @@ class rcube_kolab_contacts extends rcube_addressbook { 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. + } + } -- cgit v1.2.3