Bump the changelog for the 0.7.5 release
[email-reminder.git] / EmailReminder / EventStore.pm
blob620bbf15702ab1318244d3163b75d280c6e49afe
1 # This file is part of Email-Reminder.
3 # Email-Reminder is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 3 of the
6 # License, or (at your option) any later version.
8 # Email-Reminder is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with Email-Reminder; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA.
18 package EmailReminder::EventStore;
20 # Base class for all of the event stores.
22 # This class should never be used directly, use a derived class instead.
24 use strict;
25 use warnings;
27 use Gtk2;
28 use Glib::Object::Subclass
29 Glib::Object::,
30 interfaces => [ Gtk2::TreeModel:: ],
33 # Column indices
34 my $ID_INDEX = 0;
36 sub init
38 my ($self) = @_;
39 $self->{EVENTS} = [];
40 $self->{NB_EVENTS} = 0;
41 return 1;
44 sub add_event
46 my ($self, $event) = @_;
47 push (@{$self->{EVENTS}}, $event);
48 my $path = Gtk2::TreePath->new_from_string($#{$self->{EVENTS}});
49 my $iter = $self->get_iter($path);
50 $self->row_inserted($path, $iter);
51 $self->{NB_EVENTS}++;
52 return $path;
55 sub delete_event
57 my ($self, $path) = @_;
58 my $index = $path->get_indices();
60 $self->{EVENTS}->[$index]->unlink_event();
61 splice(@{$self->{EVENTS}}, $index, 1);
62 $self->{NB_EVENTS}--;
64 # Send the necessary signals
65 $self->row_deleted($path);
66 if ($self->{NB_EVENTS} > 0) {
67 my $iter = $self->get_iter($path);
68 return unless defined($iter);
70 $iter = $self->iter_next($iter);
71 while (defined($iter)) {
72 my $path = $self->get_path($iter);
73 $self->row_changed($path, $iter);
74 $iter = $self->iter_next($iter);
77 return 1;
80 sub get_nb_events
82 my ($self) = @_;
83 return $self->{NB_EVENTS};
86 sub get_event
88 my ($self, $path) = @_;
89 my $index = $path->get_indices();
90 return $self->{EVENTS}->[$index];
93 sub get_events
95 my ($self) = @_;
96 return $self->{EVENTS};
99 sub get_event_column
101 my ($self, $event, $col) = @_;
103 if ($col == $ID_INDEX) {
104 return $event->get_id();
106 else {
107 warn "Column '$col' is not a valid column.\n";
108 return;
112 sub set_event_column
114 my ($self, $event, $col, $new_value) = @_;
115 if ($col == $ID_INDEX) {
116 warn "The ID column is read-only.\n";
118 else {
119 warn "Column '$col' is not a valid column for value '$new_value'.\n";
121 return 1;
124 sub set_value
126 my ($self, $path, $column, $new_value) = @_;
127 my $row_index = $path->get_indices();
128 my $event = $self->{EVENTS}->[$row_index];
129 $self->set_event_column($event, $column, $new_value);
130 return 1;
133 ########################
134 # Tree Model Interface #
135 ########################
137 sub GET_FLAGS
139 return 'list-only';
142 sub GET_N_COLUMNS
144 my $self = shift;
145 return $self->{NB_COLUMNS};
148 sub GET_COLUMN_TYPE
150 return 'Glib::String';
153 sub GET_ITER
155 my ($self, $path) = @_;
157 my $index = $path->get_indices();
158 my $iter = [ $index, $index, undef, undef ];
160 # Find the first non-deleted item
161 unless (exists($self->{EVENTS}->[$index])) {
162 $iter = $self->ITER_NEXT($iter);
165 return $iter;
168 sub GET_PATH
170 my ($self, $iter) = @_;
171 return Gtk2::TreePath->new($iter->[1]);
174 sub GET_VALUE
176 my ($self, $iter, $column) = @_;
177 my $index = $iter->[1];
178 my $event = $self->{EVENTS}->[$index];
179 return "(missing)" unless defined($event); # TODO: remove this
180 my $value = $self->get_event_column($event, $column);
181 return defined($value) ? $value : "";
184 sub ITER_NEXT
186 my ($self, $iter) = @_;
187 my $index = $iter->[1] + 1;
189 # Skip over deleted items
190 while ($index < @{$self->{EVENTS}}) {
191 if (exists($self->{EVENTS}->[$index])) {
192 return [ $index, $index, undef, undef ];
194 $index++;
197 return;
200 # This is a list store, there are no children
202 sub ITER_CHILDREN
204 return;
207 sub ITER_HAS_CHILD
209 return 0;
212 sub ITER_N_CHILDREN
214 return 0;
217 sub ITER_NTH_CHILD
219 return;
222 sub ITER_PARENT
224 return;