diff --git a/calendar.php b/calendar.php index 2ceef7a..5debec6 100644 --- a/calendar.php +++ b/calendar.php @@ -184,33 +184,6 @@ class calendar extends rcube_plugin // @TODO: do EXPUNGE on kolab objects? } } - // loading preinstalled sources - $preinstalled_sources = $this->rc->config->get('calendar_preinstalled_sources', FALSE); - if ($preinstalled_sources && is_array($preinstalled_sources)) { - $username = $this->rc->get_user_name(); - $password = $this->rc->get_user_password(); - - foreach ($preinstalled_sources as $cal){ - $url = $cal['caldav_url']; - $user = $cal['caldav_user']; - $pass = $cal['caldav_pass']; - - $url = str_replace('%u', $username, $url); - $user = str_replace('%u', $username, $user); - $pass = str_replace('%p', $password, $pass); - - $cal['caldav_url'] = $url; - $cal['caldav_user'] = $user; - $cal['caldav_pass'] = $pass; - - $cal['new-source'] = true; - - if (!$this->driver->create_calendar($cal)) { - $error_msg = 'Unable to add default calendars' . ($this->driver && $thi->driver->last_error ? ': ' . $this->driver->last_error :''); - $this->rc->output->show_message($error_msg, 'error'); - } - } - } } else if ($args['task'] == 'settings') { // add hooks for Calendar settings diff --git a/drivers/caldav/caldav_driver.php b/drivers/caldav/caldav_driver.php index d17c2e7..4dc5a2e 100644 --- a/drivers/caldav/caldav_driver.php +++ b/drivers/caldav/caldav_driver.php @@ -101,9 +101,41 @@ class caldav_driver extends calendar_driver if(self::$debug === null) self::$debug = $this->rc->config->get('calendar_caldav_debug', False); + $this->_setup_preinstalled_sources(); $this->_read_calendars(); } + /** + * Setup preinstalled sources defined in config file + */ + protected function _setup_preinstalled_sources() + { + $preinstalled_sources = $this->rc->config->get('calendar_preinstalled_sources', FALSE); + if ($preinstalled_sources && is_array($preinstalled_sources)) { + $username = $this->rc->get_user_name(); + $password = $this->rc->get_user_password(); + + foreach ($preinstalled_sources as $cal){ + $url = $cal['caldav_url']; + $user = $cal['caldav_user']; + $pass = $cal['caldav_pass']; + + $url = str_replace('%u', $username, $url); + $user = str_replace('%u', $username, $user); + $pass = str_replace('%p', $password, $pass); + + $cal['caldav_url'] = $url; + $cal['caldav_user'] = $user; + $cal['caldav_pass'] = $pass; + + if (!$this->create_source($cal)) { + $error_msg = 'Unable to add default calendars' . ($this->last_error ? ': ' . $this->last_error :''); + $this->rc->output->show_message($error_msg, 'error'); + } + } + } + } + /** * Read available calendars for the current user and store them internally */ @@ -302,6 +334,7 @@ class caldav_driver extends calendar_driver { $source['caldav_url'] = self::_encode_url($source['caldav_url']); + // Skip already exiting sources $result = $this->rc->db->query("SELECT user_id, caldav_url, caldav_user FROM " . $this->db_sources . " WHERE user_id=? AND caldav_url=? AND caldav_user=?", $this->rc->user->ID, $source['caldav_url'], $source['caldav_user']); if($this->rc->db->affected_rows($result)) return true;