#!/usr/bin/perl -w # oz2remind: A Small but Useful(tm) utility to convert # OpenZaurus datebook.xml files to remind format. # Version 0.2 # # Copyright (C) 2006 Hugh Brown # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. # # $URL: http://localhost/svn/oz2remind/branches/0.2/oz2remind $ # $Id: oz2remind 247 2006-05-07 19:24:50Z aardvark $ use strict; use Getopt::Std; use XML::Simple; use Data::Dumper; use IO::File; #use Date::Simple; use Class::Date qw(-DateParse); sub usage { print <) { $uid++; $start = $description = $duration = $end = ""; my $reminder_ref; if ($option{r}) { push @{$events{'event'}}, (&parse_remind_file($uid, $_)); } elsif ($option{s}) { push @{$events{'event'}}, (&parse_remind_output($uid, $_)); } } $DATEBOOK{'events'} = \%events; $calendar = XMLout(\%DATEBOOK, RootName => 'DATEBOOK', XMLDecl => 1); print $calendar; exit; } $calendar = XMLin("-"); if ($option{d}) { print Dumper($calendar); exit; } foreach (@{$calendar->{'events'}->{'event'}}) { $start = new Class::Date ($_->{'start'}); $duration = $_->{'end'} - $_->{'start'}; $line = ""; if ($_->{'type'} and $_->{'type'} eq "AllDay") { # All-day event # Cheat a bit by formatting the date just so local $Class::Date::DATE_FORMAT="%b %d %Y"; $line = "REM $start "; } else { local $Class::Date::DATE_FORMAT="%b %d %Y AT %H:%M"; $line = "REM $start "; if ($duration > 0) { $hours = int ($duration/3600); $minutes = int (($duration - $hours*3600)/60); if ($minutes < 10) { $minutes = "0$minutes"; } $line .= "DURATION $hours:$minutes "; } } $line .= "MSG $_->{'description'}\n"; $reminder{$_->{'start'}} = $line; } foreach $i (sort keys %reminder) { print $reminder{$i}; } exit; sub parse_remind_file { my ($start, $duration, $end, $description, $location); my %reminder; my ($uid, $line) = @_; print STDERR $line if ($option{v}); ($line =~ /REM/) or return; ($line =~ /REM (\w+\s+\d+\s+\d+(\s+AT \d+\:\d+)?)/) and ($start = $1) =~ s/AT//; ($line =~ /DURATION\s+(\d+):(\d+)/) and $duration = "${1}h ${2}m"; $duration = new Class::Date::Rel($duration); if ($line =~ !/AT/) { # FIXME: Account for all-day event $start .= " 00:00:00"; $reminder{'type'} = "AllDay"; $duration = "86399 s" } $start = new Class::Date($start); if (! defined $start->date) { # We don't handle recurring events...let remind take care of that. return; } $end = new Class::Date; $end = $start + $duration; ($line =~ /MSG\s+(.*)$/) && ($description = $1); $reminder{'description'} = $description; $reminder{'start'} = $start->epoch; $reminder{'end'} = $end->epoch; $reminder{'uid'} = "-" . $uid->epoch; $reminder{'location'} = "unknown"; return \%reminder; } sub parse_remind_output { my ($start, $start_time, $date, $duration, $end, $description, $location); my %reminder; my ($uid, $line) = @_; print STDERR $line if ($option{v}); if ((split / /, $line)[4] eq "*") { ($date, $duration, $start_time) = (split / /, $line)[0,3,4]; $description = ($line =~ /^(\S+\s){5}(.*)$/)[1]; } elsif ((split / /, $line)[4] =~ /\d+/) { ($date, $duration, $start_time) = (split / /, $line)[0,3,5]; $description = ($line =~ /^(\S+\s){6}(.*)$/)[1]; } if ($duration eq "*" and $start_time eq "*") { # FIXME: Account for all-day event $start_time = " 00:00:00"; $reminder{'type'} = "AllDay"; $duration = "86399 s" } elsif ($duration =~ /\d/) { $duration .= "m"; } elsif ($duration =~ /\*/) { $duration = "0m"; } print STDERR "\$date = $date\t\$duration = $duration\t\$start_time = $start_time\t\$description = $description\n" if $option{v}; $duration = new Class::Date::Rel($duration); $start = new Class::Date ("$date $start_time"); $end = new Class::Date; $end = $start + $duration; $reminder{'description'} = $description; $reminder{'start'} = $start->epoch; $reminder{'end'} = $end->epoch; $reminder{'uid'} = "-" . $uid->epoch; $reminder{'location'} = "unknown"; return \%reminder; }