summaryrefslogtreecommitdiff
path: root/src/Net-ICal-Libical/lib/Net/ICal/Libical/Component.pm
blob: 54d9e0bab2e22de8ea6cf5adc0ad1d7b9f39a984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl
#======================================================================
# FILE: Component.pm
# CREATOR: eric 1 Mar 01
#
# (C) COPYRIGHT 2000, Eric Busboom <eric@civicknowledge.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of either:
#
#    The LGPL as published by the Free Software Foundation, version
#    2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
#
# Or:
#
#    The Mozilla Public License Version 2.0. You may obtain a copy of
#    the License at https://www.mozilla.org/MPL/
#======================================================================

package Net::ICal::Libical::Component;
use Net::ICal::Libical;

use strict;

sub new{
  my $class = shift;
  my $ical_str = shift; # Ical data in string form
  my $self = {};

  $self->{'comp_p'} = Net::ICal::Libical::icalparser_parse_string($ical_str);

  die "Can't parse string into component" if !$self->{'comp_p'};

  bless $self, $class;
}

sub new_from_ref {
  my $class = shift;
  my $r = shift;
  my $self = {};

  $self->{'comp_p'} = $r;

  bless $self, $class;
}

# Destroy must call icalcomponent_free() if icalcomponent_get_parent()
# returns NULL
sub DESTROY {
  my $self = shift;

  my $c = $self->{'comp_p'};

  if($c && !Net::ICal::Libical::icalcomponent_get_parent($c)){
    Net::ICal::Libical::icalcomponent_free($c);
  }

}

# Return an array of all properties of the given type
sub properties{

  my $self = shift;
  my $prop_name = shift;

  my @props;

  if(!$prop_name){
    $prop_name = 'ANY';
  }

  # To loop over properties
  # $comp_p = $self->{'comp_p'}
  # $p = icallangbind_get_first_property($comp_p,$prop_name)
  # $p = icallangbind_get_next_property($comp_p,$prop_name)

  my $c = $self->{'comp_p'};
  my $p;

  for($p = Net::ICal::Libical::icallangbind_get_first_property($c,$prop_name);
     $p;
     $p = Net::ICal::Libical::icallangbind_get_next_property($c,$prop_name)){

    my $d_string = Net::ICal::Libical::icallangbind_property_eval_string($p,"=>");
    my %dict = %{eval($d_string)};

    $dict{'ref'} = $p;

  # Now, look at $dict{'value_type'} or $dict{'name'} to construct a
  # derived class of Property. I'll do this later.

    my $prop;

    if($dict{'value_type'} eq 'DATE' or $dict{'value_type'} eq 'DATE-TIME'){
      $prop = new Net::ICal::Libical::Time(\%dict);
    } elsif($dict{'value_type'} eq 'DURATION' ) {
      $prop = new Net::ICal::Libical::Duration(\%dict);
    } else  {
      $prop = new Net::ICal::Libical::Property(\%dict);
    }

    push(@props,$prop);

  }


  return @props;

}


sub add_property {

  # if there is a 'ref' key in the prop's dict, then it is owned by
  # an icalcomponent, so dont add it again. But, you may check that
  # it is owned by this component with:
  # icalproperty_get_parent(p->{'ref'}') != $self->{'comp_p'}

  # If there is no 'ref' key, then create one with $p->{'ref'} =
  # icalproperty_new_from_string($p->as_ical_string)

}

sub remove_property {

# If $p->{'ref'} is set, then remove the property with
# icalcomponent_remove_property() }
}

# Return an array of all components of the given type
sub components {

  my $self = shift;
  my $comp_name = shift;

  my @comps;

  if(!$comp_name){
    $comp_name = 'ANY';
  }

  my $c = $self->{'comp_p'};
  my $p;

  for($p = Net::ICal::Libical::icallangbind_get_first_component($c,$comp_name);
     $p;
     $p = Net::ICal::Libical::icallangbind_get_next_component($c,$comp_name)){

    push(@comps, Net::ICal::Libical::Component->new_from_ref($p));

  }

  return @comps;

}


sub add_component {}

sub remove_component {}

sub as_ical_string {
  my $self = shift;

  return Net::ICal::Libical::icalcomponent_as_ical_string($self->{'comp_p'})
}



1;