Show
Ignore:
Timestamp:
08/28/08 02:04:02 (4 months ago)
Author:
ogawa
Message:

Beautify code by perltidy.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cybozu2ical/trunk/lib/WWW/CybozuOffice6/Calendar.pm

    r503 r504  
    1414 
    1515sub new { 
    16     my($class, %param) = @_; 
    17     $param{url} ||= delete $param{cybozu_url}; 
    18     $param{host} ||= URI->new($param{url})->host; 
    19     $param{ua} ||= LWP::UserAgent->new(); 
     16    my ( $class, %param ) = @_; 
     17    $param{url}  ||= delete $param{cybozu_url}; 
     18    $param{host} ||= URI->new( $param{url} )->host; 
     19    $param{ua}   ||= LWP::UserAgent->new(); 
    2020    bless \%param, $class; 
    2121} 
    2222 
    23 sub url                 { shift->_accessor('url',               @_) } 
    24 sub host                { shift->_accessor('host',              @_) } 
    25 sub username            { shift->_accessor('username',          @_) } 
    26 sub userid              { shift->_accessor('userid',            @_) } 
    27 sub password            { shift->_accessor('password',          @_) } 
    28 sub ua                  { shift->_accessor('ua',                @_) } 
    29 sub input_encoding      { shift->_accessor('input_encoding',    @_) } 
     23sub url            { shift->_accessor( 'url',            @_ ) } 
     24sub host           { shift->_accessor( 'host',           @_ ) } 
     25sub username       { shift->_accessor( 'username',       @_ ) } 
     26sub userid         { shift->_accessor( 'userid',         @_ ) } 
     27sub password       { shift->_accessor( 'password',       @_ ) } 
     28sub ua             { shift->_accessor( 'ua',             @_ ) } 
     29sub input_encoding { shift->_accessor( 'input_encoding', @_ ) } 
     30 
    3031sub _accessor { 
    3132    my $this = shift; 
    32     my $key = shift; 
     33    my $key  = shift; 
    3334    $this->{$key} = shift if @_; 
    3435    $this->{$key}; 
     
    3839    my $this = shift; 
    3940 
    40     my $res = $this->{ua}->post($this->{url} . '?page=SyncCalendar', { 
    41         _System    => 'login', 
    42         _Login     => 1, 
    43         csv        => 1, 
    44         notimecard => 1, 
    45         defined $this->{username} ? (_Account => $this->{username}) : (), 
    46         defined $this->{userid}   ? (_Id      => $this->{userid}  ) : (), 
    47         Password   => $this->{password} || '', 
    48     }); 
     41    my $res = $this->{ua}->post( 
     42        $this->{url} . '?page=SyncCalendar', 
     43        { 
     44            _System    => 'login', 
     45            _Login     => 1, 
     46            csv        => 1, 
     47            notimecard => 1, 
     48            defined $this->{username} ? ( _Account => $this->{username} ) : (), 
     49            defined $this->{userid}   ? ( _Id      => $this->{userid} )   : (), 
     50            Password => $this->{password} || '', 
     51        } 
     52    ); 
    4953    confess 'Failed to access Cybozu Office 6: ' . $res->status_line 
    50         unless $res->is_success; 
     54      unless $res->is_success; 
    5155 
    5256    my $content = $res->content; 
    53     from_to($content, $this->{input_encoding} || 'shiftjis', 'utf8'); 
    54     my @lines = grep /^\d+,ts\.\d+,/, split(/\r?\n/, $content); 
     57    from_to( $content, $this->{input_encoding} || 'shiftjis', 'utf8' ); 
     58    my @lines = grep /^\d+,ts\.\d+,/, split( /\r?\n/, $content ); 
    5559    $this->{response} = \@lines; 
    5660 
     
    6569    my @lines; 
    6670    while (<FH>) { 
    67         chomp; push @lines, $_; 
     71        chomp; 
     72        push @lines, $_; 
    6873    } 
    6974    close(FH); 
     
    8287 
    8388    my $csv; 
    84     if (eval('require Text::CSV_XS')) { 
    85         $csv = Text::CSV_XS->new({ binary => 1 }); 
    86     } elsif (eval('require Text::CSV')) { 
    87         $csv = Text::CSV->new(); 
    88     } else { 
    89         confess 'Text::CSV_XS or Text::CSV package is required'; 
     89    if ( eval('require Text::CSV_XS') ) { 
     90        $csv = Text::CSV_XS->new( { binary => 1 } ); 
     91    } 
     92    elsif ( eval('require Text::CSV') ) { 
     93        $csv = Text::CSV->new(); 
     94    } 
     95    else { 
     96        confess 'Text::CSV_XS or Text::CSV package is required'; 
    9097    } 
    9198 
    9299    my @items; 
    93     for my $line ($this->response) { 
    94         $csv->parse($line) 
    95             or confess 'Failed to parse CSV input'; 
    96         my @fields = $csv->fields; 
    97         my $num_fields = @fields - 1; 
    98         next if $num_fields < 13; 
    99         $fields[1] =~ s/^ts\.//; # remove rubbish 
    100  
    101         # Cybozu Calendar CSV Format 
    102         #      GENERIC     | RECCURENT 
    103         # [ 0] id?         | id? 
    104         # [ 1] created     | created 
    105         # [ 2] <BLANK>     x start_date / end_date 
    106         # [ 3] start_date  x initial start_date? 
    107         # [ 4] end_date    x until_date 
    108         # [ 5] start_time  | start_time 
    109         # [ 6] end_time    | end_time 
    110         # [ 7] <BLANK>     | freq 
    111         # [ 8] <BLANK>     | freq_value 
    112         # [ 9] ???         | ??? 
    113         # [10] ???         | ??? 
    114         # [11] abbrev      | abbrev 
    115         # [12] summary     | summary 
    116         # [13] description | description 
    117  
    118         my %param; 
    119         @param{qw(id created start_time end_time freq freq_value abbrev summary description)} = @fields[0,1,5..8,11..13]; 
    120         $param{time_zone} = $this->{time_zone} || 'Asia/Tokyo'; 
    121  
    122         my $item; 
    123         if (!$param{freq}) { 
    124             @param{qw(start_date end_date)} = @fields[3,4]; 
    125             $item = WWW::CybozuOffice6::Calendar::Event->new(%param); 
    126         } else { 
    127             @param{qw(start_date end_date until_date)} = @fields[2,2,4]; 
    128             if ($num_fields > 13) { 
    129                 my @exdates = @fields[14..$num_fields]; 
    130                 $param{exdates} = \@exdates; 
    131             } 
    132             my $freq = $param{freq}; 
    133             if ($freq =~ /^[1-5]$/) { 
    134                 $param{freq} = 'm'; 
    135                 my @week_str = ('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'); 
    136                 $param{freq_value} = $freq . $week_str[$param{freq_value}]; 
    137             } 
    138             $item = WWW::CybozuOffice6::Calendar::RecurrentEvent->new(%param); 
    139         } 
    140  
    141         next unless $item; 
    142         $item->comment($line); # save the CSV line as for debug info. 
    143         push @items, $item; 
     100    for my $line ( $this->response ) { 
     101        $csv->parse($line) 
     102          or confess 'Failed to parse CSV input'; 
     103        my @fields     = $csv->fields; 
     104        my $num_fields = @fields - 1; 
     105        next if $num_fields < 13; 
     106        $fields[1] =~ s/^ts\.//;    # remove rubbish 
     107 
     108        # Cybozu Calendar CSV Format 
     109        #      GENERIC     | RECCURENT 
     110        # [ 0] id?         | id? 
     111        # [ 1] created     | created 
     112        # [ 2] <BLANK>     x start_date / end_date 
     113        # [ 3] start_date  x initial start_date? 
     114        # [ 4] end_date    x until_date 
     115        # [ 5] start_time  | start_time 
     116        # [ 6] end_time    | end_time 
     117        # [ 7] <BLANK>     | freq 
     118        # [ 8] <BLANK>     | freq_value 
     119        # [ 9] ???         | ??? 
     120        # [10] ???         | ??? 
     121        # [11] abbrev      | abbrev 
     122        # [12] summary     | summary 
     123        # [13] description | description 
     124 
     125        my %param; 
     126        @param{ 
     127            qw(id created start_time end_time freq freq_value abbrev summary description) 
     128          } = @fields[ 0, 1, 5 .. 8, 11 .. 13 ]; 
     129        $param{time_zone} = $this->{time_zone} || 'Asia/Tokyo'; 
     130 
     131        my $item; 
     132        if ( !$param{freq} ) { 
     133            @param{qw(start_date end_date)} = @fields[ 3, 4 ]; 
     134            $item = WWW::CybozuOffice6::Calendar::Event->new(%param); 
     135        } 
     136        else { 
     137            @param{qw(start_date end_date until_date)} = @fields[ 2, 2, 4 ]; 
     138            if ( $num_fields > 13 ) { 
     139                my @exdates = @fields[ 14 .. $num_fields ]; 
     140                $param{exdates} = \@exdates; 
     141            } 
     142            my $freq = $param{freq}; 
     143            if ( $freq =~ /^[1-5]$/ ) { 
     144                $param{freq} = 'm'; 
     145                my @week_str = ( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' ); 
     146                $param{freq_value} = $freq . $week_str[ $param{freq_value} ]; 
     147            } 
     148            $item = WWW::CybozuOffice6::Calendar::RecurrentEvent->new(%param); 
     149        } 
     150 
     151        next unless $item; 
     152        $item->comment($line);    # save the CSV line as for debug info. 
     153        push @items, $item; 
    144154    } 
    145155    wantarray ? @items : $items[0]; 
     
    150160sub new { 
    151161    my $class = shift; 
    152     my $self = { 
    153         is_full_day => 0, 
    154         modified => DateTime->now, 
     162    my $self  = { 
     163        is_full_day => 0, 
     164        modified    => DateTime->now, 
    155165    }; 
    156166    bless $self, $class; 
     
    159169} 
    160170 
    161 sub id          { shift->_accessor('id',                @_) } 
    162 sub start       { shift->_accessor('start',             @_) } 
    163 sub end         { shift->_accessor('end',               @_) } 
    164 sub summary     { shift->_accessor('summary',           @_) } 
    165 sub description { shift->_accessor('description',       @_) } 
    166 sub created     { shift->_accessor('created',           @_) } 
    167 sub modified    { shift->_accessor('modified',          @_) } 
    168 sub is_full_day { shift->_accessor('is_full_day',       @_) } 
    169 sub comment     { shift->_accessor('comment',           @_) } 
     171sub id          { shift->_accessor( 'id',          @_ ) } 
     172sub start       { shift->_accessor( 'start',       @_ ) } 
     173sub end         { shift->_accessor( 'end',         @_ ) } 
     174sub summary     { shift->_accessor( 'summary',     @_ ) } 
     175sub description { shift->_accessor( 'description', @_ ) } 
     176sub created     { shift->_accessor( 'created',     @_ ) } 
     177sub modified    { shift->_accessor( 'modified',    @_ ) } 
     178sub is_full_day { shift->_accessor( 'is_full_day', @_ ) } 
     179sub comment     { shift->_accessor( 'comment',     @_ ) } 
     180 
    170181sub _accessor { 
    171182    my $this = shift; 
    172     my $key = shift; 
     183    my $key  = shift; 
    173184    $this->{$key} = shift if @_; 
    174185    $this->{$key}; 
     
    176187 
    177188sub parse { 
    178     my($this, %param) = @_; 
    179  
    180     $this->{id} = $param{id} || '0'; 
     189    my ( $this, %param ) = @_; 
     190 
     191    $this->{id}        = $param{id}        || '0'; 
    181192    $this->{time_zone} = $param{time_zone} || 'Asia/Tokyo'; 
    182193 
    183     my $start = $this->to_datetime($param{start_date}, $param{start_time}); 
    184     my $end   = $this->to_datetime($param{end_date},   $param{end_time}); 
     194    my $start = $this->to_datetime( $param{start_date}, $param{start_time} ); 
     195    my $end   = $this->to_datetime( $param{end_date},   $param{end_time} ); 
    185196    return unless $start && $end; 
    186197 
    187198    # (start_time == empty) => A full-day event 
    188199    # (start_time != empty) && (end_time == empty) => A malformed event 
    189     if ($param{start_time} eq ':') { 
    190         $start = $start->truncate(to => 'day'); 
    191         $end   = $end->add(days => 1)->truncate(to => 'day'); 
    192         $this->{is_full_day} = 1; 
    193     } elsif ($param{end_time} eq ':') { 
    194         $end   = $start->clone->add(minutes => 10); 
     200    if ( $param{start_time} eq ':' ) { 
     201        $start = $start->truncate( to => 'day' ); 
     202        $end = $end->add( days => 1 )->truncate( to => 'day' ); 
     203        $this->{is_full_day} = 1; 
     204    } 
     205    elsif ( $param{end_time} eq ':' ) { 
     206        $end = $start->clone->add( minutes => 10 ); 
    195207    } 
    196208    $this->{start} = $start; 
    197209    $this->{end}   = $end; 
    198210 
    199     $this->{created} = DateTime->from_epoch(epoch => $param{created} || 0); 
    200  
    201     my $summary = ($param{abbrev} ? $param{abbrev} . ': ' : '') . $param{summary}; 
     211    $this->{created} = DateTime->from_epoch( epoch => $param{created} || 0 ); 
     212 
     213    my $summary = 
     214      ( $param{abbrev} ? $param{abbrev} . ': ' : '' ) . $param{summary}; 
    202215    $this->{summary} = $summary; 
    203216    $this->{description} = $param{description} || $summary; 
     
    208221sub to_datetime { 
    209222    my $this = shift; 
    210     my($ymd, $hms) = @_; 
     223    my ( $ymd, $hms ) = @_; 
    211224 
    212225    my %args; 
    213     return unless $ymd && ($ymd =~ m!^(\d+)/(\d+)/(\d+)$! || $ymd =~ m!^da\.(\d+)\.(\d+)\.(\d+)$!); 
    214     @args{qw(year month day)} = ($1, $2, $3); 
    215  
    216     if ($hms && $hms ne ':') { 
    217         return unless $hms =~ m!^(\d+):(\d+)(?:\:?(\d+)?)$!; 
    218         @args{qw(hour minute second)} = ($1, $2, $3 || 0); 
    219         @args{qw(hour minute second)} = (23, 59, 59) if $args{hour} > 23; 
    220     } else { 
    221         @args{qw(hour minute second)} = (0, 0, 0); 
     226    return 
     227      unless $ymd 
     228          && (   $ymd =~ m!^(\d+)/(\d+)/(\d+)$! 
     229              || $ymd =~ m!^da\.(\d+)\.(\d+)\.(\d+)$! ); 
     230    @args{qw(year month day)} = ( $1, $2, $3 ); 
     231 
     232    if ( $hms && $hms ne ':' ) { 
     233        return unless $hms =~ m!^(\d+):(\d+)(?:\:?(\d+)?)$!; 
     234        @args{qw(hour minute second)} = ( $1, $2, $3 || 0 ); 
     235        @args{qw(hour minute second)} = ( 23, 59, 59 ) if $args{hour} > 23; 
     236    } 
     237    else { 
     238        @args{qw(hour minute second)} = ( 0, 0, 0 ); 
    222239    } 
    223240 
     
    231248use base qw( WWW::CybozuOffice6::Calendar::Event ); 
    232249 
    233 sub rrule               { shift->_accessor('rrule',             @_) } 
     250sub rrule { shift->_accessor( 'rrule', @_ ) } 
     251 
    234252# for compatibility 
    235 sub frequency           { shift->_accessor('frequency',         @_) } 
    236 sub frequency_value     { shift->_accessor('frequency_value',   @_) } 
    237 sub until               { shift->_accessor('until',             @_) } 
     253sub frequency       { shift->_accessor( 'frequency',       @_ ) } 
     254sub frequency_value { shift->_accessor( 'frequency_value', @_ ) } 
     255sub until           { shift->_accessor( 'until',           @_ ) } 
    238256 
    239257sub exdates { 
     
    244262} 
    245263 
    246 our %FREQUENCY = ( y => 'YEARLY', m => 'MONTHLY', w => 'WEEKLY', 
    247                    d => 'DAILY', n => 'WEEKDAYS' ); 
     264our %FREQUENCY = ( 
     265    y => 'YEARLY', 
     266    m => 'MONTHLY', 
     267    w => 'WEEKLY', 
     268    d => 'DAILY', 
     269    n => 'WEEKDAYS' 
     270); 
     271 
    248272sub parse { 
    249     my($this, %param) = @_; 
     273    my ( $this, %param ) = @_; 
    250274    $this->SUPER::parse(%param); 
    251275 
     
    256280    # rrule 
    257281    my %rrule = (); 
    258     if ($FREQUENCY{$freq} eq 'WEEKDAYS') { 
    259         %rrule = ( FREQ => 'WEEKLY', BYDAY => 'MO,TU,WE,TH,FR' ); 
    260     } else { 
    261         %rrule = ( FREQ => $FREQUENCY{$freq} ); 
    262     } 
    263     if ($param{freq_value} =~ /^\d(SU|MO|TU|WE|TH|FR|SA)$/) { 
    264         $rrule{BYDAY} = $param{freq_value}; 
    265         $rrule{INTERVAL} = 1; 
     282    if ( $FREQUENCY{$freq} eq 'WEEKDAYS' ) { 
     283        %rrule = ( FREQ => 'WEEKLY', BYDAY => 'MO,TU,WE,TH,FR' ); 
     284    } 
     285    else { 
     286        %rrule = ( FREQ => $FREQUENCY{$freq} ); 
     287    } 
     288    if ( $param{freq_value} =~ /^\d(SU|MO|TU|WE|TH|FR|SA)$/ ) { 
     289        $rrule{BYDAY}    = $param{freq_value}; 
     290        $rrule{INTERVAL} = 1; 
    266291    } 
    267292 
    268293    # until 
    269     if ($param{until_date} =~ m!^(\d+)/(\d+)/(\d+)$! || $param{until_date} =~ m!^da\.(\d+)\.(\d+)\.(\d+)$!) { 
    270         my %args = (year => $1, month => $2, day => $3); 
    271         my $until; 
    272         if ($this->{is_full_day}) { 
    273             $until = $this->to_datetime($param{until_date}, ':'); 
    274         } else { 
    275             $until = $this->{end}->clone->set(%args); 
    276             $until->set_time_zone('UTC'); # timezone must be UTC 
    277         } 
    278         $rrule{UNTIL} = $until; 
     294    if (   $param{until_date} =~ m!^(\d+)/(\d+)/(\d+)$! 
     295        || $param{until_date} =~ m!^da\.(\d+)\.(\d+)\.(\d+)$! ) 
     296    { 
     297        my %args = ( year => $1, month => $2, day => $3 ); 
     298        my $until; 
     299        if ( $this->{is_full_day} ) { 
     300            $until = $this->to_datetime( $param{until_date}, ':' ); 
     301        } 
     302        else { 
     303            $until = $this->{end}->clone->set(%args); 
     304            $until->set_time_zone('UTC');    # timezone must be UTC 
     305        } 
     306        $rrule{UNTIL} = $until; 
    279307    } 
    280308 
     
    282310 
    283311    # exdates 
    284     if (defined $param{exdates}) { 
    285         my @exdates; 
    286         for (@{$param{exdates}}) { 
    287             push @exdates, $this->to_datetime($_, $param{start_time}); 
    288         } 
    289         $this->{exdates} = \@exdates; 
     312    if ( defined $param{exdates} ) { 
     313        my @exdates; 
     314        for ( @{ $param{exdates} } ) { 
     315            push @exdates, $this->to_datetime( $_, $param{start_time} ); 
     316        } 
     317        $this->{exdates} = \@exdates; 
    290318    } 
    291319