Changeset 504 for cybozu2ical/trunk/cybozu2ical
- Timestamp:
- 08/28/08 02:04:02 (4 months ago)
- Files:
-
- 1 modified
-
cybozu2ical/trunk/cybozu2ical (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cybozu2ical/trunk/cybozu2ical
r502 r504 22 22 ### 23 23 *Data::ICal::Property::_value_as_string = sub { 24 my $self = shift;25 my $key = shift;26 my $value = defined( $self->value()) ? $self->value() : '';27 28 unless ( $self->vcal10) {24 my $self = shift; 25 my $key = shift; 26 my $value = defined( $self->value() ) ? $self->value() : ''; 27 28 unless ( $self->vcal10 ) { 29 29 my $lc_key = lc($key); 30 30 $value =~ s/\\/\\/gs; 31 $value =~ s/\Q;/\\;/gs unless ($lc_key eq 'rrule' || $lc_key eq 'exdate'); 32 $value =~ s/,/\\,/gs unless ($lc_key eq 'rrule' || $lc_key eq 'exdate'); 31 $value =~ s/\Q;/\\;/gs 32 unless ( $lc_key eq 'rrule' || $lc_key eq 'exdate' ); 33 $value =~ s/,/\\,/gs 34 unless ( $lc_key eq 'rrule' || $lc_key eq 'exdate' ); 33 35 $value =~ s/\n/\\n/gs; 34 36 $value =~ s/\\N/\\N/gs; … … 43 45 ### 44 46 sub to_icaldate { 45 my($dt, $is_full_day) = @_; 46 $is_full_day ? 47 $dt->ymd('') : 48 $dt->ymd('') . 'T' . $dt->hms('') . ($dt->time_zone->is_utc ? 'Z' : ''); 47 my ( $dt, $is_full_day ) = @_; 48 $is_full_day 49 ? $dt->ymd('') 50 : $dt->ymd('') . 'T' 51 . $dt->hms('') 52 . ( $dt->time_zone->is_utc ? 'Z' : '' ); 49 53 } 50 54 51 55 sub encode_string { 52 my($enc, $text) = @_; 53 if ($enc eq 'ncr') { 54 $text =~ s/(\P{ASCII})/sprintf("&#%d;", ord($1))/eg; 55 } else { 56 $text = encode($enc, $text); 56 my ( $enc, $text ) = @_; 57 if ( $enc eq 'ncr' ) { 58 $text =~ s/(\P{ASCII})/sprintf("&#%d;", ord($1))/eg; 59 } 60 else { 61 $text = encode( $enc, $text ); 57 62 } 58 63 $text; … … 62 67 my $file = shift; 63 68 my $yaml; 64 if (eval('require YAML::Tiny')) { 65 ($yaml) = YAML::Tiny::LoadFile($file); 66 } elsif (eval('require YAML')) { 67 ($yaml) = YAML::LoadFile($file); 69 if ( eval('require YAML::Tiny') ) { 70 ($yaml) = YAML::Tiny::LoadFile($file); 71 } 72 elsif ( eval('require YAML') ) { 73 ($yaml) = YAML::LoadFile($file); 68 74 } 69 75 if ($@) { 70 die "Faild to read yaml file: $@";76 die "Faild to read yaml file: $@"; 71 77 } 72 78 $yaml; … … 79 85 # Handle command-line options 80 86 my %opt = ( 81 conf => 'config.yaml',82 'compat-google-calendar' => 0,87 conf => 'config.yaml', 88 'compat-google-calendar' => 0, 83 89 ); 84 GetOptions(\%opt, 85 'output=s', 86 'conf=s', 87 'compat-google-calendar', 88 'debug', 89 'input-csv=s', 90 'output-csv=s', 91 'help', 92 ) or pod2usage(2); 90 GetOptions( \%opt, 'output=s', 'conf=s', 'compat-google-calendar', 'debug', 91 'input-csv=s', 'output-csv=s', 'help', ) 92 or pod2usage(2); 93 93 pod2usage(1) if $opt{help}; 94 94 95 95 # Read configuration file 96 my $cfg = read_yaml( $opt{conf});96 my $cfg = read_yaml( $opt{conf} ); 97 97 my $time_zone = $cfg->{time_zone} || 'Asia/Tokyo'; 98 98 … … 103 103 calscale => 'GREGORIAN', 104 104 method => 'PUBLISH', 105 $cfg->{calname} ? ( 'X-WR-CALNAME' => $cfg->{calname}) : (),105 $cfg->{calname} ? ( 'X-WR-CALNAME' => $cfg->{calname} ) : (), 106 106 'X-WR-TIMEZONE' => $time_zone 107 107 ); 108 108 109 109 # Obtain Cybozu Office 6/7 Calendar items 110 my $cal_class = $cfg->{cybozu_version} == 7 ? 111 'WWW::CybozuOffice7::Calendar' : 'WWW::CybozuOffice6::Calendar'; 110 my $cal_class = 111 $cfg->{cybozu_version} == 7 112 ? 'WWW::CybozuOffice7::Calendar' 113 : 'WWW::CybozuOffice6::Calendar'; 112 114 eval "use $cal_class;"; 113 115 my $cal = $cal_class->new(%$cfg); 114 116 115 if ($opt{'input-csv'}) { 116 $cal->read_from_csv_file($opt{'input-csv'}) 117 or die "Failed to read CSV file: $opt{'input-csv'}"; 118 } else { 117 if ( $opt{'input-csv'} ) { 118 $cal->read_from_csv_file( $opt{'input-csv'} ) 119 or die "Failed to read CSV file: $opt{'input-csv'}"; 120 } 121 else { 119 122 $cal->request() 120 or die "Failed to get Cybozu Office 6 Calendar";123 or die "Failed to get Cybozu Office 6 Calendar"; 121 124 } 122 125 123 126 # Output the calendar CSV for debugging 124 if ( $opt{'output-csv'}) {127 if ( $opt{'output-csv'} ) { 125 128 local *FH; 126 129 open FH, ">$opt{'output-csv'}" or die "Failed to write $opt{'output-csv'}"; … … 130 133 131 134 # For each items, generate an 'event entry' and append it to the calendar 132 for my $item ( $cal->get_items()) {135 for my $item ( $cal->get_items() ) { 133 136 my $vevent = Data::ICal::Entry::Event->new(); 134 my %args = (135 summary => decode_utf8($item->summary),136 description => decode_utf8($item->description),137 created => to_icaldate($item->created),138 dtstamp => to_icaldate($item->modified),139 url => $cal->url . '?page=ScheduleView&EID=' . $item->id,137 my %args = ( 138 summary => decode_utf8( $item->summary ), 139 description => decode_utf8( $item->description ), 140 created => to_icaldate( $item->created ), 141 dtstamp => to_icaldate( $item->modified ), 142 url => $cal->url . '?page=ScheduleView&EID=' . $item->id, 140 143 ); 141 144 142 $args{comment} = decode_utf8($item->comment) 143 if $opt{debug} && $item->comment; 144 145 if ($item->is_full_day) { 146 $args{dtstart} = [ to_icaldate($item->start, 1), { VALUE => 'DATE' } ]; 147 $args{dtend} = [ to_icaldate($item->end, 1), { VALUE => 'DATE' } ]; 148 } else { 149 $args{dtstart} = [ to_icaldate($item->start, 0), { TZID => $time_zone } ]; 150 $args{dtend} = [ to_icaldate($item->end, 0), { TZID => $time_zone } ]; 145 $args{comment} = decode_utf8( $item->comment ) 146 if $opt{debug} && $item->comment; 147 148 if ( $item->is_full_day ) { 149 $args{dtstart} = 150 [ to_icaldate( $item->start, 1 ), { VALUE => 'DATE' } ]; 151 $args{dtend} = [ to_icaldate( $item->end, 1 ), { VALUE => 'DATE' } ]; 152 } 153 else { 154 $args{dtstart} = 155 [ to_icaldate( $item->start, 0 ), { TZID => $time_zone } ]; 156 $args{dtend} = [ to_icaldate( $item->end, 0 ), { TZID => $time_zone } ]; 151 157 } 152 158 153 159 # handle frequency 154 if ($item->can('rrule')) { 155 # rrule 156 my %rrule = %{$item->rrule}; 157 $rrule{UNTIL} = to_icaldate($rrule{UNTIL}, $item->is_full_day) 158 if $rrule{UNTIL}; 159 $rrule{WKST} = 'SU' 160 if $opt{'compat-google-calendar'}; 161 162 my @rrule_list; 163 for (qw(FREQ COUNT INTERVAL BYMONTH BYMONTHDAY WKST BYDAY UNTIL)) { 164 push @rrule_list, $_ . '=' . $rrule{$_} 165 if exists $rrule{$_}; 166 } 167 $args{rrule} = join ';', @rrule_list; 168 169 # exdate 170 if ($item->exdates) { 171 if ($item->is_full_day) { 172 my $exdate = join ',', map { to_icaldate($_, 1) } $item->exdates; 173 $args{exdate} = [ $exdate, { VALUE => 'DATE' } ]; 174 } else { 175 my $exdate = join ',', map { to_icaldate($_, 0) } $item->exdates; 176 $args{exdate} = [ $exdate, { TZID => $time_zone } ]; 177 } 178 } 160 if ( $item->can('rrule') ) { 161 162 # rrule 163 my %rrule = %{ $item->rrule }; 164 $rrule{UNTIL} = to_icaldate( $rrule{UNTIL}, $item->is_full_day ) 165 if $rrule{UNTIL}; 166 $rrule{WKST} = 'SU' 167 if $opt{'compat-google-calendar'}; 168 169 my @rrule_list; 170 for (qw(FREQ COUNT INTERVAL BYMONTH BYMONTHDAY WKST BYDAY UNTIL)) { 171 push @rrule_list, $_ . '=' . $rrule{$_} 172 if exists $rrule{$_}; 173 } 174 $args{rrule} = join ';', @rrule_list; 175 176 # exdate 177 if ( $item->exdates ) { 178 if ( $item->is_full_day ) { 179 my $exdate = join ',', 180 map { to_icaldate( $_, 1 ) } $item->exdates; 181 $args{exdate} = [ $exdate, { VALUE => 'DATE' } ]; 182 } 183 else { 184 my $exdate = join ',', 185 map { to_icaldate( $_, 0 ) } $item->exdates; 186 $args{exdate} = [ $exdate, { TZID => $time_zone } ]; 187 } 188 } 179 189 180 190 } … … 189 199 # Generate a 'timezone entry' and append it to the calendar 190 200 my $vtimezone = Data::ICal::Entry::TimeZone->new(); 191 $vtimezone->add_properties( tzid => $time_zone);201 $vtimezone->add_properties( tzid => $time_zone ); 192 202 193 203 # probably we need to support the Daylight Saving Time, but not yet supported. 194 204 my $standard = Data::ICal::Entry::TimeZone::Standard->new(); 195 my $std = DateTime->new(year => 1970, month => 1, day => 1, 196 hour => 0, minute => 0, second => 0, 197 time_zone => $time_zone); 198 my $offset = DateTime::TimeZone::offset_as_string($std->offset) || '+0900'; 205 my $std = DateTime->new( 206 year => 1970, 207 month => 1, 208 day => 1, 209 hour => 0, 210 minute => 0, 211 second => 0, 212 time_zone => $time_zone 213 ); 214 my $offset = DateTime::TimeZone::offset_as_string( $std->offset ) || '+0900'; 199 215 my $tzname = $cfg->{tzname} || 'JST'; 200 216 … … 210 226 211 227 # Outputs the calendar as a string 212 my $text = encode_string($cfg->{output_encoding} || 'utf8', $vcalendar->as_string); 213 if ($opt{output}) { 228 my $text = 229 encode_string( $cfg->{output_encoding} || 'utf8', $vcalendar->as_string ); 230 if ( $opt{output} ) { 214 231 local *FH; 215 232 open FH, ">$opt{output}" or die "Failed to write $opt{output}"; 216 233 print FH $text; 217 234 close FH; 218 } else { 235 } 236 else { 219 237 print $text; 220 238 }
![(please configure the [header_logo] section in trac.ini)](/public/chrome/common/trac_banner.png)