"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/EventList.pm" (11 Sep 2020, 6945 Bytes) of package /linux/privat/email-reminder-0.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "EventList.pm" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.7.8_vs_0.8.0.

    1 # This file is part of Email-Reminder.
    2 #
    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.
    7 #
    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.
   12 #
   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.
   17 
   18 package EmailReminder::EventList;
   19 
   20 # Holds all user information and events.
   21 #
   22 # Events are stored in the proper EventStore and they can be accessed
   23 # using this class.  The main XML parsing and generation happen here.
   24 
   25 use strict;
   26 use warnings;
   27 
   28 use Email::Valid;
   29 use XML::DOM;
   30 
   31 use EmailReminder::AnniversaryEvent;
   32 use EmailReminder::AnniversaryStore;
   33 use EmailReminder::BirthdayEvent;
   34 use EmailReminder::BirthdayStore;
   35 use EmailReminder::MonthlyEvent;
   36 use EmailReminder::MonthlyStore;
   37 use EmailReminder::Utils;
   38 use EmailReminder::WeeklyEvent;
   39 use EmailReminder::WeeklyStore;
   40 use EmailReminder::YearlyEvent;
   41 use EmailReminder::YearlyStore;
   42 
   43 # XML tags, attributes and values
   44 
   45 my $EMAIL_TAG       = 'email';
   46 my $EVENT_TAG       = 'event';
   47 my $EVENTS_TAG      = 'events';
   48 my $FIRST_NAME_TAG  = 'first_name';
   49 my $LAST_NAME_TAG   = 'last_name';
   50 my $USER_TAG        = 'email-reminder_user';
   51 
   52 my $TYPE_ATTR       = 'type';
   53 
   54 sub new
   55 {
   56     my ($class, $filename, $create) = @_;
   57     
   58     my $self = { "NEXT_EVENT_ID" => 0,
   59                  "LOADED_FILENAME" => $filename,
   60                  "XML_DOC" => undef,
   61                  "EVENTS_NODE" => undef,
   62                  "STORES" => {},
   63              };
   64 
   65     bless $self, $class;
   66     
   67     $self->process_file($create);
   68     
   69     return $self;
   70 }
   71 
   72 sub process_file
   73 {
   74     my ($self, $create, $readonly) = @_;
   75 
   76     # Make sure the config file exists and is readable
   77     my $filename = $self->{LOADED_FILENAME};
   78     unless (-e $filename) {
   79         die "File '$filename' does not exist.\n";
   80     }
   81     unless (-r $filename) {
   82         die "File '$filename' exists but is not readable.\n";
   83     }
   84     if (!$readonly && !(-w $filename)) {
   85         warn "WARNING: File '$filename' is not writable, your changes will be lost!\n";
   86     }
   87 
   88     # Start parsing the XML file
   89     my $parser = XML::DOM::Parser->new();
   90     my $doc;
   91     eval {
   92         $doc = $parser->parsefile($filename);
   93     };
   94     unless (defined($doc)) {
   95     die "File '$filename' is an invalid XML file. Fix it or delete it.\n";
   96     }
   97     $self->{XML_DOC} = $doc;
   98 
   99     # Read user info
  100     my $user = $doc->getElementsByTagName($USER_TAG)->item(0);   
  101     unless (defined($user)) {
  102     die "File '$filename' is an invalid XML file. Fix it or delete it.\n";
  103     }
  104     $self->{USER_NODE} = $user;
  105 
  106     # Read events
  107     my $events = $doc->getElementsByTagName($EVENTS_TAG)->item(0);
  108     return unless defined($events);
  109     $self->{EVENTS_NODE} = $events;
  110 
  111     foreach my $event_node ($events->getElementsByTagName($EVENT_TAG)) {
  112     my $type = $event_node->getAttribute($TYPE_ATTR);
  113     my $event = $self->create_event($type, $event_node);
  114         next unless defined($event);
  115 
  116         # Add to proper EventStore
  117         my $store = $self->get_model($type);
  118         $store->add_event($event);
  119     }
  120     return 1;
  121 }
  122 
  123 sub create_event
  124 {
  125     my ($self, $type, $event_node) = @_;
  126 
  127     if (!defined($event_node))
  128     {
  129         $event_node = $self->{XML_DOC}->createElement($EVENT_TAG);
  130         $event_node->setAttribute($TYPE_ATTR, $type);
  131         
  132         my $events = $self->{EVENTS_NODE};
  133         unless (defined($events))
  134         {
  135             $events = $self->{XML_DOC}->createElement($EVENTS_TAG);
  136             $self->{USER_NODE}->appendChild($events);
  137             $self->{EVENTS_NODE} = $events;
  138         }
  139         $events->appendChild($event_node);
  140     }
  141 
  142     my $event;
  143     my $id = $self->{NEXT_EVENT_ID}++;
  144 
  145     if ($type eq EmailReminder::BirthdayEvent->get_type()) {
  146         $event = EmailReminder::BirthdayEvent->new($event_node, $id);
  147     } 
  148     elsif ($type eq EmailReminder::AnniversaryEvent->get_type()) {
  149         $event = EmailReminder::AnniversaryEvent->new($event_node, $id);
  150     } 
  151     elsif ($type eq EmailReminder::MonthlyEvent->get_type()) {
  152         $event = EmailReminder::MonthlyEvent->new($event_node, $id);
  153     }
  154     elsif ($type eq EmailReminder::WeeklyEvent->get_type()) {
  155         $event = EmailReminder::WeeklyEvent->new($event_node, $id);
  156     }
  157     elsif ($type eq EmailReminder::YearlyEvent->get_type()) {
  158         $event = EmailReminder::YearlyEvent->new($event_node, $id);
  159     }
  160 
  161     return $event;
  162 }
  163 
  164 sub get_model
  165 {
  166     my ($self, $type) = @_;
  167 
  168     my $store = $self->{STORES}->{$type};
  169     unless (defined($store)) {
  170         if ($type eq EmailReminder::AnniversaryEvent->get_type()) {
  171             $store = EmailReminder::AnniversaryStore->new();
  172         } 
  173         elsif ($type eq EmailReminder::BirthdayEvent->get_type()) {
  174             $store = EmailReminder::BirthdayStore->new();
  175         } 
  176         elsif ($type eq EmailReminder::MonthlyEvent->get_type()) {
  177             $store = EmailReminder::MonthlyStore->new();
  178         }
  179         elsif ($type eq EmailReminder::WeeklyEvent->get_type()) {
  180             $store = EmailReminder::WeeklyStore->new();
  181         }
  182         elsif ($type eq EmailReminder::YearlyEvent->get_type()) {
  183             $store = EmailReminder::YearlyStore->new();
  184         }
  185 
  186         $store->init();
  187         $self->{STORES}->{$type} = $store;
  188     }
  189 
  190     return $store;
  191 }
  192 
  193 sub get_events
  194 {
  195     my $self = shift;
  196 
  197     my @events = ();
  198     foreach my $store (values(%{$self->{STORES}})) {
  199         push(@events, @{$store->get_events()});
  200     }
  201 
  202     return @events;
  203 }
  204 
  205 # View/edit user properties
  206 
  207 sub _get_user_fname {
  208     my $self = shift;
  209     return EmailReminder::Utils::get_node_value($self->{USER_NODE}, $FIRST_NAME_TAG) || '';
  210 }
  211 
  212 sub _get_user_lname {
  213     my $self = shift;
  214     return EmailReminder::Utils::get_node_value($self->{USER_NODE}, $LAST_NAME_TAG) || '';
  215 }
  216 
  217 sub get_user_name
  218 {
  219     my $self = shift;
  220 
  221     my $fname = $self->_get_user_fname;
  222     my $lname = $self->_get_user_lname;
  223 
  224     if (!$fname && !$lname) {
  225         # Get name from UNIX password file
  226         my @pwinfo = getpwuid($>);
  227         my $fullname = $pwinfo[6]; 
  228         $fullname =~ s/[^0-9A-Za-z_\- ]//g;
  229         my @name_parts = split(/ /, $fullname);
  230         
  231         $fname = $name_parts[0];
  232         $lname = $name_parts[-1] if @name_parts > 1;
  233     } elsif (!$fname) {
  234         $fname = $lname;
  235         $lname = '';
  236     }
  237 
  238     return ($fname, $lname);
  239 }
  240 
  241 sub get_user_email
  242 {
  243     my $self = shift;
  244     return EmailReminder::Utils::get_node_value($self->{USER_NODE}, $EMAIL_TAG) || "";
  245 }
  246 
  247 1;