| 1 | # $Id: TreoPhoneCallDB.pm,v 1.5 2007/07/30 14:00:48 drhyde Exp $ | ||||||||||||
| 2 | |||||||||||||
| 3 | package Palm::TreoPhoneCallDB; | ||||||||||||
| 4 | |||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 4 | ▶ | use Palm::Raw(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 4 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 4 | ▶ | use vars qw($VERSION @ISA $timezone $incl_raw); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | $VERSION = '1.1'; | |||||||||||
| 14 | 4 | @ISA = qw(Palm::Raw); | |||||||||||
| 15 | 4 | $timezone = 'Europe/London'; | |||||||||||
| 16 | 4 | $incl_raw = 0; | |||||||||||
| 17 | |||||||||||||
| 18 | sub import { | ||||||||||||
| 19 | 3 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||
| 20 | 3 | my %opts = @_; | |||||||||||
| 21 | 3 | ▶ | $timezone = $opts{timezone} if(exists($opts{timezone})); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 3 | ▶ | $incl_raw = $opts{incl_raw} if(exists($opts{incl_raw})); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 3 | Palm::PDB::RegisterPDBHandlers(__PACKAGE__, [HsPh => 'call']); | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 NAME | ||||||||||||
| 27 | |||||||||||||
| 28 | Palm::TreoPhoneCallDB - Handler for Treo PhoneCallDB databases | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||
| 31 | |||||||||||||
| 32 | use Palm::PDB; | ||||||||||||
| 33 | use Palm::TreoPhoneCallDB timezone => 'Europe/London'; | ||||||||||||
| 34 | |||||||||||||
| 35 | my $pdb = Palm::PDB->new(); | ||||||||||||
| 36 | $pdb->Load("PhoneCallDB.pdb"); | ||||||||||||
| 37 | print Dumper(@{$pdb->{records}})' | ||||||||||||
| 38 | |||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||
| 40 | |||||||||||||
| 41 | This is a helper class for the Palm::PDB package, which parses the database | ||||||||||||
| 42 | generated by Palm Treos as a record who you called, when, and for how long. | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 OPTIONS | ||||||||||||
| 45 | |||||||||||||
| 46 | You can set some global options when you 'use' the module: | ||||||||||||
| 47 | |||||||||||||
| 48 | =over | ||||||||||||
| 49 | |||||||||||||
| 50 | =item timezone | ||||||||||||
| 51 | |||||||||||||
| 52 | Defaults to 'Europe/London'. | ||||||||||||
| 53 | |||||||||||||
| 54 | =item incl_raw | ||||||||||||
| 55 | |||||||||||||
| 56 | Whether to include the raw binary blob of data in the parsed records. | ||||||||||||
| 57 | Only really useful for debuggering, and so defaults to false. | ||||||||||||
| 58 | |||||||||||||
| 59 | =back | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 METHODS | ||||||||||||
| 62 | |||||||||||||
| 63 | This class inherits from Palm::Raw, so has all of its methods. The following | ||||||||||||
| 64 | are over-ridden, and differ from that in the parent class thus: | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 ParseRecord | ||||||||||||
| 67 | |||||||||||||
| 68 | Returns data structures with the following keys: | ||||||||||||
| 69 | |||||||||||||
| 70 | =over | ||||||||||||
| 71 | |||||||||||||
| 72 | =item rawdata | ||||||||||||
| 73 | |||||||||||||
| 74 | The raw data blob passed to the method. This is only present if the | ||||||||||||
| 75 | incl_raw option is true. | ||||||||||||
| 76 | |||||||||||||
| 77 | =item date | ||||||||||||
| 78 | |||||||||||||
| 79 | The date the call started, in YYYY-MM-DD format | ||||||||||||
| 80 | |||||||||||||
| 81 | =item time | ||||||||||||
| 82 | |||||||||||||
| 83 | The time the call started, in HH:MM format | ||||||||||||
| 84 | |||||||||||||
| 85 | =item epoch | ||||||||||||
| 86 | |||||||||||||
| 87 | The epoch time the call started. Note that because the database doesn't | ||||||||||||
| 88 | store the timezone, we assume 'Europe/London'. If you want to change | ||||||||||||
| 89 | that, then suppy a timezone option when you 'use' the module. | ||||||||||||
| 90 | |||||||||||||
| 91 | Internally, this uses the DateTime module. In the case of ambiguous times | ||||||||||||
| 92 | then it uses the latest UTC time. For invalid local times, the epoch is | ||||||||||||
| 93 | set to -1, an impossible number as it's before Palm even existed. | ||||||||||||
| 94 | |||||||||||||
| 95 | Note that this is always the Unix epoch time. See L<DateTime> for details | ||||||||||||
| 96 | of what this means. | ||||||||||||
| 97 | |||||||||||||
| 98 | =item duration | ||||||||||||
| 99 | |||||||||||||
| 100 | The length of the call in seconds | ||||||||||||
| 101 | |||||||||||||
| 102 | =item name | ||||||||||||
| 103 | |||||||||||||
| 104 | The name of the other party, which the Treo extracts from the SIM phone-book | ||||||||||||
| 105 | or from the Palm address book at the time the call is connected. | ||||||||||||
| 106 | |||||||||||||
| 107 | =item number | ||||||||||||
| 108 | |||||||||||||
| 109 | The number of the other party. This is not normalised so you might see the | ||||||||||||
| 110 | same number in different formats, eg 02079813000 and +442079813000. I may | ||||||||||||
| 111 | add number normalisation in the future. | ||||||||||||
| 112 | |||||||||||||
| 113 | =item direction | ||||||||||||
| 114 | |||||||||||||
| 115 | Either 'Incoming', 'Outgoing' or 'Missed'. | ||||||||||||
| 116 | |||||||||||||
| 117 | =back | ||||||||||||
| 118 | |||||||||||||
| 119 | Other fields may be added in the future. | ||||||||||||
| 120 | |||||||||||||
| 121 | =cut | ||||||||||||
| 122 | |||||||||||||
| 123 | sub ParseRecord { | ||||||||||||
| 124 | 282 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
ParseRecord: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||
| 125 | 282 | my %record = @_; | |||||||||||
| 126 | |||||||||||||
| 127 | 282 | $record{rawdata} = delete($record{data}); | |||||||||||
| 128 | |||||||||||||
| 129 | 282 | my($flags, $date, $time, $duration, $name, $number); | |||||||||||
| 130 | # the unpack() doesn't seem to work in 5.6.2 | ||||||||||||
| 131 | 282 | ▶ | if($] >= 5.008) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | ($flags, $date, $time, $duration, $name, $number) = unpack( | ||||||||||||
| 133 | 'n3N1Z*Z*', $record{rawdata} | ||||||||||||
| 134 | 282 | ); | |||||||||||
| 135 | } else { | ||||||||||||
| 136 | # pick the record apart a byte at a time. probably makes horrible | ||||||||||||
| 137 | # charset assumptions, but that's ok, 5.6 doesn't do unicode anyway | ||||||||||||
| 138 | 0 | my @bytes = (split(//, $record{rawdata})); | |||||||||||
| 139 | 0 | $flags = 256 * ord($bytes[0]) + ord($bytes[1]); | |||||||||||
| 140 | 0 | $date = 256 * ord($bytes[2]) + ord($bytes[3]); | |||||||||||
| 141 | 0 | $time = 256 * ord($bytes[4]) + ord($bytes[5]); | |||||||||||
| 142 | |||||||||||||
| 143 | 0 | $duration = 0x1000000 * ord($bytes[6]) + | |||||||||||
| 144 | 0x10000 * ord($bytes[7]) + | ||||||||||||
| 145 | 0x100 * ord($bytes[8]) + | ||||||||||||
| 146 | ord($bytes[9]); | ||||||||||||
| 147 | |||||||||||||
| 148 | 0 | ($name, $number) = split(/\x00/, join('', @bytes[10 .. $#bytes])); | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 282 | my $year = 1904 + (($date & 0b1111111000000000) >> 9); | |||||||||||
| 152 | 282 | my $month = sprintf('%02d', ($date & 0b111100000) >> 5); | |||||||||||
| 153 | 282 | my $day = sprintf('%02d', $date & 0b11111); | |||||||||||
| 154 | 282 | my $hour = sprintf('%02d', $time >> 8); | |||||||||||
| 155 | 282 | my $minute = sprintf('%02d', $time & 255); | |||||||||||
| 156 | |||||||||||||
| 157 | @record{qw(date time duration name number direction)} = ( | ||||||||||||
| 158 | "$year-$month-$day", | ||||||||||||
| 159 | "$hour:$minute", | ||||||||||||
| 160 | $duration, | ||||||||||||
| 161 | $name, | ||||||||||||
| 162 | $number, | ||||||||||||
| 163 | 282 | (qw(Incoming Missed Outgoing))[$record{category} - 1] | |||||||||||
| 164 | ); | ||||||||||||
| 165 | 282 | ▶ | $record{epoch} = eval { DateTime->new( | ||||||||||
|
Condition50%1/2
eval { || -1
MC/DC0%0/1
eval {
do {
'DateTime'->new('year', $year, 'month', $month, 'day', $day, 'hour', $hour, 'minute', $minute, 'time_zone', $Palm::TreoPhoneCallDB::timezone)->epoch
}
} || -1
eval {
do {
'DateTime'->new('year', $year, 'month', $month, 'day', $day, 'hour', $hour, 'minute', $minute, 'time_zone', $Palm::TreoPhoneCallDB::timezone)->epoch
}
}
Truth table50%1/2
A
A= eval {
do {
'DateTime'->new('year', $year, 'month', $month, 'day', $day, 'hour', $hour, 'minute', $minute, 'time_zone', $Palm::TreoPhoneCallDB::timezone)->epoch
}
}
| |||||||||||||
| 166 | year => $year, | ||||||||||||
| 167 | month => $month, | ||||||||||||
| 168 | day => $day, | ||||||||||||
| 169 | hour => $hour, | ||||||||||||
| 170 | minute => $minute, | ||||||||||||
| 171 | time_zone => $timezone | ||||||||||||
| 172 | )->epoch(); } || -1; | ||||||||||||
| 173 | |||||||||||||
| 174 | 282 | ▶ | delete $record{rawdata} unless($incl_raw); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 175 | |||||||||||||
| 176 | 282 | return \%record; | |||||||||||
| 177 | } | ||||||||||||
| 178 | |||||||||||||
| 179 | =head1 LIMITATIONS | ||||||||||||
| 180 | |||||||||||||
| 181 | There is currently no support for creating a new database, or for editing | ||||||||||||
| 182 | the contents of an existing database. If you need that functionality, | ||||||||||||
| 183 | please submit a patch with tests. I will *not* write this myself | ||||||||||||
| 184 | unless I need it. | ||||||||||||
| 185 | |||||||||||||
| 186 | Behaviour if you try to create or edit a database is currently undefined. | ||||||||||||
| 187 | |||||||||||||
| 188 | =head1 BUGS and FEEDBACK | ||||||||||||
| 189 | |||||||||||||
| 190 | Online documentation claims that there are various flags in the | ||||||||||||
| 191 | records to indicate whether calls are incoming or outgoing and | ||||||||||||
| 192 | so on. I can't find these flags anywhere in the data generated by | ||||||||||||
| 193 | my Treo 680. Instead, it seems to be stored in the 'category' | ||||||||||||
| 194 | field. It is, however, possible that the category numbers for each | ||||||||||||
| 195 | type of call vary from one Treo to another maybe depending on the | ||||||||||||
| 196 | order in which the first calls are made. | ||||||||||||
| 197 | |||||||||||||
| 198 | If you find any other bugs please report them either using | ||||||||||||
| 199 | L<http://rt.cpan.org/> or by email. Ideally, I would like to receive a | ||||||||||||
| 200 | sample database and a test file, which fails with the latest version of | ||||||||||||
| 201 | the module but will pass when I fix the bug. | ||||||||||||
| 202 | |||||||||||||
| 203 | =head1 SEE ALSO | ||||||||||||
| 204 | |||||||||||||
| 205 | L<Palm::PDB> | ||||||||||||
| 206 | |||||||||||||
| 207 | L<DateTime> | ||||||||||||
| 208 | |||||||||||||
| 209 | =head1 AUTHOR | ||||||||||||
| 210 | |||||||||||||
| 211 | David Cantrell E<lt>F<david@cantrell.org.uk>E<gt> | ||||||||||||
| 212 | |||||||||||||
| 213 | =head1 COPYRIGHT and LICENCE | ||||||||||||
| 214 | |||||||||||||
| 215 | Copyright 2007 David Cantrell | ||||||||||||
| 216 | |||||||||||||
| 217 | This module is free-as-in-speech software, and may be used, distributed, | ||||||||||||
| 218 | and modified under the same terms as Perl itself. | ||||||||||||
| 219 | |||||||||||||
| 220 | =head1 CONSPIRACY | ||||||||||||
| 221 | |||||||||||||
| 222 | This module is also free-as-in-mason software. | ||||||||||||
| 223 | |||||||||||||
| 224 | =cut | ||||||||||||
| 225 | |||||||||||||
| 226 | 4 | 1; | |||||||||||