class Tilia::CalDav::Calendar

This object represents a CalDAV calendar.

A calendar can contain multiple TODO and or Events. These are represented as SabreCalDAVCalendarObject objects.

Public Class Methods

new(caldav_backend, calendar_info) click to toggle source

Constructor

@param BackendBackendInterface caldav_backend @param array calendar_info

# File lib/tilia/cal_dav/calendar.rb, line 29
def initialize(caldav_backend, calendar_info)
  @caldav_backend = caldav_backend
  @calendar_info = calendar_info
end

Public Instance Methods

acl() click to toggle source

Returns a list of ACE's for this node.

Each ACE has the following properties:

* 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  currently the only supported privileges
* 'principal', a url to the principal who owns the node
* 'protected' (optional), indicating that this ACE is not allowed to
   be updated.

@return array

# File lib/tilia/cal_dav/calendar.rb, line 207
def acl
  acl = [
    {
      'privilege' => '{DAV:}read',
      'principal' => owner,
      'protected' => true
    },
    {
      'privilege' => '{DAV:}read',
      'principal' => owner + '/calendar-proxy-write',
      'protected' => true
    },
    {
      'privilege' => '{DAV:}read',
      'principal' => owner + '/calendar-proxy-read',
      'protected' => true
    },
    {
      'privilege' => "{#{Plugin::NS_CALDAV}}read-free-busy",
      'principal' => '{DAV:}authenticated',
      'protected' => true
    }

  ]
  unless @calendar_info['{http://sabredav.org/ns}read-only']
    acl << {
      'privilege' => '{DAV:}write',
      'principal' => owner,
      'protected' => true
    }
    acl << {
      'privilege' => '{DAV:}write',
      'principal' => owner + '/calendar-proxy-write',
      'protected' => true
    }
  end

  acl
end
acl=(_acl) click to toggle source

Updates the ACL

This method will receive a list of new ACE's.

@param array acl @return void

# File lib/tilia/cal_dav/calendar.rb, line 293
def acl=(_acl)
  fail Dav::Exception::MethodNotAllowed, 'Changing ACL is not yet supported'
end
calendar_query(filters) click to toggle source

Performs a calendar-query on the contents of this calendar.

The calendar-query is defined in RFC4791 : CalDAV. Using the calendar-query it is possible for a client to request a specific set of object, based on contents of iCalendar properties, date-ranges and iCalendar component types (VTODO, VEVENT).

This method should just return a list of (relative) urls that match this query.

The list of filters are specified as an array. The exact array is documented by SabreCalDAVCalendarQueryParser.

@param array filters @return array

# File lib/tilia/cal_dav/calendar.rb, line 338
def calendar_query(filters)
  @caldav_backend.calendar_query(@calendar_info['id'], filters)
end
changes(sync_token, sync_level, limit = nil) click to toggle source

The getChanges method returns all the changes that have happened, since the specified syncToken and the current collection.

This function should return an array, such as the following:

[

'syncToken' => 'The current synctoken',
'added'   => [
   'new.txt',
],
'modified'   => [
   'modified.txt',
],
'deleted' => [
   'foo.php.bak',
   'old.txt'
]

]

The syncToken property should reflect the current syncToken of the collection, as reported get_sync_token. This is needed here too, to ensure the operation is atomic.

If the syncToken is specified as null, this is an initial sync, and all members should be reported.

The modified property is an array of nodenames that have changed since the last token.

The deleted property is an array with nodenames, that have been deleted from collection.

The second argument is basically the 'depth' of the report. If it's 1, you only have to report changes that happened only directly in immediate descendants. If it's 2, it should also include changes from the nodes below the child collections. (grandchildren)

The third (optional) argument allows a client to specify how many results should be returned at most. If the limit is not specified, it should be treated as infinite.

If the limit (infinite or not) is higher than you're willing to return, you should throw a SabreDAVExceptionTooMuchMatches exception.

If the syncToken is expired (due to data cleanup) or unknown, you must return null.

The limit is 'suggestive'. You are free to ignore it.

@param string sync_token @param int sync_level @param int limit @return array

# File lib/tilia/cal_dav/calendar.rb, line 414
def changes(sync_token, sync_level, limit = nil)
  return nil unless @caldav_backend.is_a?(Backend::SyncSupport)

  @caldav_backend.changes_for_calendar(
    @calendar_info['id'],
    sync_token,
    sync_level,
    limit
  )
end
child(name) click to toggle source

Returns a calendar object

The contained calendar objects are for example Events or Todo's.

@param string name @return SabreCalDAVICalendarObject

# File lib/tilia/cal_dav/calendar.rb, line 75
def child(name)
  obj = @caldav_backend.calendar_object(@calendar_info['id'], name)

  fail Dav::Exception::NotFound, 'Calendar object not found' unless obj

  obj['acl'] = child_acl

  CalendarObject.new(@caldav_backend, @calendar_info, obj)
end
child_acl() click to toggle source

This method returns the ACL's for calendar objects in this calendar. The result of this method automatically gets passed to the calendar-object nodes in the calendar.

@return array

# File lib/tilia/cal_dav/calendar.rb, line 252
def child_acl
  acl = [
    {
      'privilege' => '{DAV:}read',
      'principal' => owner,
      'protected' => true
    },
    {
      'privilege' => '{DAV:}read',
      'principal' => owner + '/calendar-proxy-write',
      'protected' => true
    },
    {
      'privilege' => '{DAV:}read',
      'principal' => owner + '/calendar-proxy-read',
      'protected' => true
    }

  ]
  unless @calendar_info['{http://sabredav.org/ns}read-only']
    acl << {
      'privilege' => '{DAV:}write',
      'principal' => owner,
      'protected' => true
    }
    acl << {
      'privilege' => '{DAV:}write',
      'principal' => owner + '/calendar-proxy-write',
      'protected' => true
    }
  end

  acl
end
child_exists(name) click to toggle source

Checks if a child-node exists.

@param string name @return bool

# File lib/tilia/cal_dav/calendar.rb, line 123
def child_exists(name)
  obj = @caldav_backend.calendar_object(@calendar_info['id'], name)

  if !obj
    return false
  else
    return true
  end
end
children() click to toggle source

Returns the full list of calendar objects

@return array

# File lib/tilia/cal_dav/calendar.rb, line 88
def children
  objs = @caldav_backend.calendar_objects(@calendar_info['id'])

  children = []
  objs.each do |obj|
    obj['acl'] = child_acl
    children << CalendarObject.new(@caldav_backend, @calendar_info, obj)
  end

  children
end
create_directory(_name) click to toggle source

Creates a new directory

We actually block this, as subdirectories are not allowed in calendars.

@param string name @return void

# File lib/tilia/cal_dav/calendar.rb, line 139
def create_directory(_name)
  fail Dav::Exception::MethodNotAllowed, 'Creating collections in calendar objects is not allowed'
end
create_file(name, calendar_data = nil) click to toggle source

Creates a new file

The contents of the new file must be a valid ICalendar string.

@param string name @param resource calendar_data @return string|null

# File lib/tilia/cal_dav/calendar.rb, line 150
def create_file(name, calendar_data = nil)
  calendar_data = calendar_data.read unless calendar_data.is_a?(String)

  @caldav_backend.create_calendar_object(@calendar_info['id'], name, calendar_data)
end
delete() click to toggle source

Deletes the calendar.

@return void

# File lib/tilia/cal_dav/calendar.rb, line 159
def delete
  @caldav_backend.delete_calendar(@calendar_info['id'])
end
group() click to toggle source

Returns a group principal

This must be a url to a principal, or null if there's no owner

@return string|null

# File lib/tilia/cal_dav/calendar.rb, line 193
def group
  nil
end
last_modified() click to toggle source

Returns the last modification date as a unix timestamp.

@return void

# File lib/tilia/cal_dav/calendar.rb, line 175
def last_modified
  nil
end
multiple_children(paths) click to toggle source

This method receives a list of paths in it's first argument. It must return an array with Node objects.

If any children are not found, you do not have to return them.

@param string[] paths @return array

# File lib/tilia/cal_dav/calendar.rb, line 107
def multiple_children(paths)
  objs = @caldav_backend.multiple_calendar_objects(@calendar_info['id'], paths)

  children = []
  objs.each do |obj|
    obj['acl'] = child_acl
    children << CalendarObject.new(@caldav_backend, @calendar_info, obj)
  end

  children
end
name() click to toggle source

Returns the name of the calendar

@return string

# File lib/tilia/cal_dav/calendar.rb, line 37
def name
  @calendar_info['uri']
end
name=(_new_name) click to toggle source

Renames the calendar. Note that most calendars use the {DAV:}displayname to display a name to display a name.

@param string new_name @return void

# File lib/tilia/cal_dav/calendar.rb, line 168
def name=(_new_name)
  fail Dav::Exception::MethodNotAllowed, 'Renaming calendars is not yet supported'
end
owner() click to toggle source

Returns the owner principal

This must be a url to a principal, or null if there's no owner

@return string|null

# File lib/tilia/cal_dav/calendar.rb, line 184
def owner
  @calendar_info['principaluri']
end
prop_patch(prop_patch) click to toggle source

Updates properties on this node.

This method received a PropPatch object, which contains all the information about the update.

To update specific properties, call the 'handle' method on this object. Read the PropPatch documentation for more information.

@param PropPatch prop_patch @return void

# File lib/tilia/cal_dav/calendar.rb, line 51
def prop_patch(prop_patch)
  @caldav_backend.update_calendar(@calendar_info['id'], prop_patch)
end
properties(_requested_properties) click to toggle source

Returns the list of properties

@param array requested_properties @return array

# File lib/tilia/cal_dav/calendar.rb, line 59
def properties(_requested_properties)
  response = {}

  @calendar_info.each do |prop_name, _prop_value|
    response[prop_name] = @calendar_info[prop_name] if prop_name[0] == '{'
  end

  response
end
supported_privilege_set() click to toggle source

Returns the list of supported privileges for this node.

The returned data structure is a list of nested privileges. See SabreDAVACLPlugin::getDefaultSupportedPrivilegeSet for a simple standard structure.

If null is returned from this method, the default privilege set is used, which is fine for most common usecases.

@return array|null

# File lib/tilia/cal_dav/calendar.rb, line 307
def supported_privilege_set
  default = DavAcl::Plugin.default_supported_privilege_set

  # We need to inject 'read-free-busy' in the tree, aggregated under
  # {DAV:}read.
  default['aggregates'].each do |agg|
    next unless agg['privilege'] == '{DAV:}read'

    agg['aggregates'] << {
      'privilege' => "{#{Plugin::NS_CALDAV}}read-free-busy"
    }
  end

  default
end
sync_token() click to toggle source

This method returns the current sync-token for this collection. This can be any string.

If null is returned from this function, the plugin assumes there's no sync information available.

@return string|null

# File lib/tilia/cal_dav/calendar.rb, line 349
def sync_token
  if @caldav_backend.is_a?(Backend::SyncSupport) &&
     @calendar_info.key?('{DAV:}sync-token')
    return @calendar_info['{DAV:}sync-token']
  end

  if @caldav_backend.is_a?(Backend::SyncSupport) &&
     @calendar_info.key?('{http://sabredav.org/ns}sync-token')
    return @calendar_info['{http://sabredav.org/ns}sync-token']
  end
end