diff options
author | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 17:45:00 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 20:50:06 +0200 |
commit | 944985c6a66e288f9a7f52cd25764bbfef2514ce (patch) | |
tree | 8d06a37583b1a3d0575b63d4c3ce4a110a4db88c /lib | |
parent | ad289d23b089793cd0a43f7c145bf0846a827257 (diff) |
move object classes to library Object::Groupware
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Object/Groupware/Calendar.pm | 51 | ||||
-rw-r--r-- | lib/Object/Groupware/Event.pm | 84 |
2 files changed, 135 insertions, 0 deletions
diff --git a/lib/Object/Groupware/Calendar.pm b/lib/Object/Groupware/Calendar.pm new file mode 100644 index 0000000..7226782 --- /dev/null +++ b/lib/Object/Groupware/Calendar.pm @@ -0,0 +1,51 @@ +use v5.36; +use Feature::Compat::Class 0.07; + +package Object::Groupware::Calendar 0.01; + +class Object::Groupware::Calendar; + +use utf8; + +use Log::Any qw( ); +use Data::ICal::DateTime; + +use Object::Groupware::Event; + +field $log = Log::Any->get_logger; + +# borrow from Data::ICal::new() signature +field $data : param = undef; +field $filename : param = undef; + +ADJUST { + if ($data) { + if ( $data isa Data::ICal ) { } + else { $data = Data::ICal->new( data => $data ) } + } + elsif ($filename) { $data = Data::ICal->new( filename => $filename ) } + + if ( $log->is_trace ) { + use DDP; + p $data; + } +} + +# mimick Data::ICal::DateTime::events() signature +method events ( $set = undef, $period = undef ) +{ + $log->infof( + 'will pick events between %s and %s', + $set->start, $set->end + ) if $set; + + my @events = map { Object::Groupware::Event->new( entry => $_ ) } sort { + $a->start->compare( $b->start ) + || $a->start->compare( $b->start ) + || $a->summary cmp $b->summary + } $data->events( $set || (), $period || () ); + + return @events; +} + +1; diff --git a/lib/Object/Groupware/Event.pm b/lib/Object/Groupware/Event.pm new file mode 100644 index 0000000..52fa143 --- /dev/null +++ b/lib/Object/Groupware/Event.pm @@ -0,0 +1,84 @@ +use v5.36; +use Feature::Compat::Class 0.07; + +package Object::Groupware::Event 0.01; + +class Object::Groupware::Event; + +use utf8; + +use Log::Any qw( ); +use Feature::Compat::Try; + +field $log = Log::Any->get_logger; + +field $entry : param; + +field $begin : reader = $entry->start; +field $date_begin : reader = $begin->strftime('%A %e. %B'); +field $time_begin : reader = $begin->strftime('%k.%M'); +field $end : reader = $entry->end; +field $date_end : reader; +field $time_end : reader; +field $datespan : reader; +field $timespan : reader; +field $time_brief : reader; +field $summary : reader = $entry->summary; +field $description : reader = $entry->description; +field $location : reader = $entry->_simple_property('location'); +field $price : reader; +field @attendees; +field @attachments; + +ADJUST { + if ( defined $end ) { + $date_end = $end->strftime('%A %e. %B'); + $time_end = $end->strftime('%k.%M'); + } + $datespan + = ( defined $end and $date_end ne $date_begin ) + ? ucfirst("$date_begin - $date_end") + : ucfirst("$date_begin"); + $timespan + = ( defined $end and not $entry->all_day ) + ? ucfirst("$date_begin kl. $time_begin-$time_end") + : undef; + $time_brief + = $entry->all_day + ? $datespan + : ucfirst("$date_begin kl. $time_begin"); + $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m; + $price = $1; + + if ( $entry->property('attendee') ) { + for ( @{ $entry->property('attendee') } ) { + push @attendees, $_->parameters->{'CN'} + || $_->value =~ s/^mailto://r; + } + } + if ( $entry->property('attach') ) { + for ( @{ $entry->property('attach') } ) { + my $uri; + try { $uri = URI->new( $_->value ) } + catch ($e) { + $log->errorf( 'failed to parse URI %s: %s', $uri, $e ); + next; + } + $uri->authority and $uri->host + or next; + push @attachments, $uri; + } + } + + if ( $log->is_trace ) { + use DDP; + p $entry; + p $begin; + p $end; + } +} + +method attendees { !!@attendees ? [@attendees] : undef } +method attachments { !!@attachments ? [@attachments] : undef } + +1; |