blib/lib/Mail/Thread/Arc.pm

filter: Statementstmt 14.4% 15 / 104 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 22.2% 4 / 18 Podpod 92.3% 12 / 13 totaltotal 20.0% 31 / 155 CC 14 SCAR 25.4
CC
14
Cov
12%
draw_arc
30.0
javascript_chunk
30.0
render
12.0
CRAP 12.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mail::Thread::Arc;
3 1 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Date::Parse qw( str2time );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( Class::Accessor::Chained::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 __PACKAGE__->mk_accessors(qw( messages message_offsets selected_message
7 width height svg ));
8  
9 1 our $VERSION = '0.22';
10  
11 =head1 NAME
12  
13 Mail::Thread::Arc - Generates a Thread Arc reperesentation of a thread
14  
15 =head1 SYNOPSIS
16  
17 my $threader = Mail::Thread->new( @messages );
18 my $arc = Mail::Thread::Arc->new;
19  
20 $threader->thread;
21  
22 my $i;
23 for my $thread ($threader->rootset) {
24 ++$i;
25 my $svg = $arc->render( $thread );
26 write_file( "thread_$i.svg", $svg->xmlify );
27 }
28  
29 =head1 DESCRIPTION
30  
31 Mail::Thread::Arc takes a Mail::Thread::Container and generates an
32 image of the Thread Arc. Thread Arcs are described in the
33 documentation for IBM's remail project.
34  
35 http://www.research.ibm.com/remail/
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 Generic constructor, inherited from L<Class::Accessor::Chained::Fast>
42  
43 =head2 render( $root_container, %options )
44  
45 The main method.
46  
47 Renders the thread tree as a thread arc. Returns an SVG object.
48  
49 =cut
50  
51 0 ▶sub render {
Subroutine render: not called CC=3 SCAR=24.8
Pod documented
52 0 my $self = shift;
53 0 my $root = shift;
54  
55 # extract just the containers with messages
56 0 my @messages;
57 $root->iterate_down(
58 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 my $container = shift;
60 0 ▶ push @messages, $container if $container->message;
Branch: 1 branch
BranchTrueFalseTotal
if $container->message 0 0 0
61 0 } );
62  
63 # sort on date
64 @messages = sort {
65 0 $self->date_of( $a ) <=> $self->date_of( $b )
66 } @messages;
67  
68 0 $self->messages( \@messages );
69  
70 0 $self->width( ( @messages + 1 ) * $self->message_radius * 3 );
71 0 $self->height( $self->maximum_arc_height * 2 + $self->message_radius * 6 );
72 0 $self->svg( SVG->new( width => $self->width, height => $self->height, onload => "init(evt)" ) );
73  
74 {
75 # assign the numbers needed to compute X
76 0 my $i;
77 0 $self->message_offsets( { map { $_ => ++$i } @messages } );
78 }
79 0 $self->svg->script->CDATA( $self->javascript_chunk );
80  
81 0 $self->draw_arc( $_->parent, $_ ) for @messages;
82 0 $self->draw_message( $_ ) for @messages;
83  
84 0 return $self->svg;
85 }
86  
87  
88 =head2 draw_message( $message )
89  
90 Draw the message on the SVG canvas.
91  
92 =cut
93  
94 0 ▶sub draw_message {
Subroutine draw_message: not called CC=2 SCAR=17.9
Pod documented
95 0 my ($self, $message) = @_;
96  
97 0 my $i = $self->message_offsets->{$message} - 1;
98  
99 0 my $link = $self->make_link($message);
100 0 my $root = $self->svg;
101 0 ▶ $root = $root->anchor( -href=> $link ) if defined $link;
Branch: 1 branch
BranchTrueFalseTotal
if defined $link 0 0 0
102  
103 0 my $group = $root->group(
104 id => "road$i",
105 onmouseover => "set_group_color( $i, 'blue' )",
106 $self->message_style( $message ),
107 );
108  
109 0 $group->title->cdata( $message->header('from') );
110 0 $group->desc->cdata( "Date: " . $message->header('date') );
111  
112 0 $group->circle(
113 cx => $self->message_x( $message ),
114 cy => $self->message_y,
115 r => $self->message_radius,
116 );
117
118 }
119  
120  
121 =head2 make_link( $message )
122  
123 Return an URI based on the message. By default returns
124 undef meaning that that the message is not a link.
125  
126 However if this module is subclassed then a meaningful
127 URI can be returned where appropriate.
128  
129 =cut
130  
131 0 ▶sub make_link {
Subroutine make_link: not called CC=1 SCAR=6.9
Pod documented
132 0 my ($self, $message) = @_;
133 0 return;
134 }
135  
136  
137 =head2 draw_arc( $from, $to )
138  
139 draws an arc between two messages
140  
141 =cut
142  
143 0 ▶sub draw_arc {
Subroutine draw_arc: not called CC=5 SCAR=34.0
Pod documented
144 0 my ($self, $from, $to) = @_;
145  
146 0 ▶ return unless $from;
Branch: 1 branch
BranchTrueFalseTotal
unless $from 0 0 0
147  
148 0 my $distance = $self->message_x( $to ) - $self->message_x( $from );
149 0 my $radius = $distance/ 2;
150  
151 0 my $top = $self->thread_generation( $to ) % 2;
152 0 my $x = $self->message_x( $from );
153 0 ▶ my $y = $self->message_y + ( $top ? -$self->message_radius : $self->message_radius);
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 0 0 0
154  
155 0 my %offsets = %{ $self->message_offsets };
156  
157 0 my $path;
158 0 ▶ if ($radius > $self->maximum_arc_height) { # uh oh - trickyness
Branch: 1 branch
BranchTrueFalseTotal
if ($radius > $self->maximum_arc_height) { } 0 0 0
159 0 my $max = $self->maximum_arc_height;
160 # to Y - the relative part of the first curve
161 0 ▶ my $toy = $top ? -$max : $max;
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 0 0 0
162 0 my $toy2 = -$toy;
163 0 my $x2 = $self->message_x( $to ) - $max;
164 0 my $y2 = $y + $toy;
165  
166 0 $path = join(' ',
167 "M $x,$y", #start the path
168 "a$max,$max 0 0,$top $max,$toy", # arc up
169 "L $x2,$y2", # line across
170 "a$max,$max 0 0,$top $max,$toy2", # arc down
171 );
172 }
173 else {
174 0 $path = "M $x,$y a$radius,$radius 0 1,$top $distance,0";
175 }
176  
177 0 my $group = $self->svg->group(
178 id => "road$offsets{ $from }-$offsets{ $to }",
179 $self->arc_style( $from, $to )
180 );
181 0 $group->path( d => $path );
182 }
183  
184 =head2 message_radius
185  
186 The radius of the message circles. The most magic of all the magic
187 numbers.
188  
189 =cut
190  
191 0 ▶sub message_radius { 5 }
Subroutine message_radius: not called CC=1 SCAR=6.9
Pod documented
192  
193 =head2 message_style( $container )
194  
195 Returns the style hash for the message circle.
196  
197 =cut
198  
199 0 ▶sub message_style {
Subroutine message_style: not called CC=1 SCAR=6.9
Pod documented
200 0 my ($self, $message) = @_;
201  
202 return (
203 0 stroke => 'red',
204 fill => 'white',
205 'stroke-width' => $self->message_radius / 4,
206 );
207 }
208  
209 =head2 maximum_arc_height
210  
211 the maximum height of an arc. default is 17 message radii
212  
213 =cut
214  
215 0 ▶sub maximum_arc_height {
Subroutine maximum_arc_height: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217 0 return $self->message_radius * 17
218 }
219  
220 =head2 arc_style( $from, $to )
221  
222 Returns the style hash for the connecting arc,
223  
224 =cut
225  
226 0 ▶sub arc_style {
Subroutine arc_style: not called CC=1 SCAR=6.9
Pod documented
227 0 my ($self, $from, $to) = @_;
228  
229 return (
230 0 stroke => 'black',
231 fill => 'none',
232 'stroke-width' => $self->message_radius / 4,
233 );
234 }
235  
236 =head2 message_x( $container )
237  
238 returns the X co-ordinate for a message
239  
240 =cut
241  
242 0 ▶sub message_x {
Subroutine message_x: not called CC=1 SCAR=6.9
Pod documented
243 0 my ($self, $message) = @_;
244 0 return $self->message_offsets->{ $message } * $self->message_radius * 3;
245 }
246  
247 =head2 message_y
248  
249 returns the Y co-ordinate for a message (expected to be constant for
250 all messages)
251  
252 =cut
253  
254 0 ▶sub message_y {
Subroutine message_y: not called CC=1 SCAR=6.9
Pod documented
255 0 my $self = shift;
256 0 return $self->height / 2;
257 }
258  
259 =head2 thread_generation( $message )
260  
261 returns the thread generation of the container.
262  
263 =cut
264  
265 0 ▶sub thread_generation {
Subroutine thread_generation: not called CC=2 SCAR=17.9
Pod documented
266 0 my ($self, $container) = @_;
267  
268 0 my $count = 0;
269 0 ▶ while ($container->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->parent) 0 0 0
270 0 ++$count;
271 0 $container = $container->parent;
272 }
273  
274 0 return $count;
275 }
276  
277 =head2 date_of( $container )
278  
279 The date the message was sent, in epoch seconds
280  
281 =cut
282  
283 0 ▶sub date_of {
Subroutine date_of: not called CC=1 SCAR=6.9
Pod documented
284 0 my ($self, $container) = @_;
285 0 return str2time $container->header( 'date' );
286 }
287  
288 0 ▶sub javascript_chunk {
Subroutine javascript_chunk: not called CC=5 SCAR=34.0
Pod undocumented
289 0 my $self = shift;
290 0 my $js = <<'END';
291 /* lifted from http://roasp.com/tutorial/tutorial5.shtml */
292 var SVGDoc;
293 var groups = new Array();
294 var last_group;
295  
296 function init(e) {
297 SVGDoc = e.getTarget().getOwnerDocument();
298 END
299  
300 # just what groups do we manage?
301 0 my %offset = %{ $self->message_offsets };
302 0 for my $id (sort { $a <=> $b } values %offset) {
303 0 my $group = $id - 1;
304 0 my $message = $self->messages->[$group];
305 0 my @path = $id - 1;
306 0 ▶ while ($message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 0 0 0
307 0 ▶ last unless $message->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $message->parent 0 0 0
308 0 push @path, $offset{ $message->parent } - 1,
309 "'$offset{ $message->parent }-$offset{ $message }'";
310 0 $message = $message->parent;
311 }
312  
313 0 $js .= " append_group( ". join(', ', @path ) . " ); // road $group\n"
314 }
315  
316 0 ▶ if (my $m = $self->selected_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $self->selected_message) 0 0 0
317 0 my $group = $offset{ $m } - 1;
318 0 $js .= " set_group_color( $group, 'blue' ); // select group $group\n"
319 }
320  
321 0 $js .= <<'END';
322 }
323  
324 function append_group() {
325 var roads = new Array();
326 for (var i = 0; i < arguments.length; i++) {
327 var index = arguments[i];
328 var road = SVGDoc.getElementById("road" + index);
329 roads[roads.length] = road;
330 }
331 groups[groups.length] = roads;
332 }
333  
334 function set_group_color(group_index, color) {
335 if ( last_group != null ) {
336 _set_group_color(last_group, "black");
337 }
338 _set_group_color(group_index, color);
339 last_group = group_index;
340 }
341  
342 function _set_group_color(group_index, color) {
343 var roads = groups[group_index];
344  
345 for (var i = 0; i < roads.length; i++) {
346 var road = roads[i];
347  
348 //alert( "Setting " + road + " to " + color );
349 // set the color
350 road.setAttribute("stroke", color);
351 // pop to top - seems to blow errors on circles
352 // road.getParentNode.appendChild(road);
353 }
354 }
355 END
356  
357 0 return $js;
358 }
359  
360  
361 1 1;
362 __END__
↑ Summary