:- ensure_loaded(common).
cvs_id('$Id: short_views.pl,v 1.11 2004/10/09 04:44:17 sandro Exp $').
/*

  Various short (word/phrase/icon) and simple views.

*/



:- rdf_register_ns(foaf, 'http://xmlns.com/foaf/0.1/').

delang(lang(Tag, Text), span(lang=Tag, Text)).
delang(lang(_Tag, Text), Text).
delang(A,A) :- atom(A).

%%  good_label(+Q, +Focus, -Text)
%%
%%  Picks labels for the given focus in decreasing order of quality,
%%  on backtracking.   Fails if there is nothing "good".  cf label/3.

good_label(Q, Focus, Text) :-
	rdfq(Q, Focus, rdfs:label, literal(Text1)),
	delang(Text1, Text).
good_label(Q, Focus, Text) :-
	(   rdfq(Q, Focus, dc:title, literal(Text1))
	;   rdfq(Q, Focus, retr:documentTitle, literal(Text1))
        ;   rdfq(Q, Focus, retr:documentHeading, literal(Text1))
	),
	delang(Text1, Text2),
	atom(Text2),   % hack for now....
	concat_atom(['Page titled "', Text2, '"'], Text).
good_label(Q, Focus, Text) :-
	rdfq(Q, Focus, foaf:name, literal(Text1)),
	delang(Text1, Text).
good_label(Q, Focus, Text) :-
	( rdfq(Q, Focus, foaf:givenname, literal(N1))
	; rdfq(Q, Focus, foaf:firstName, literal(N1))
	),
	( rdfq(Q, Focus, foaf:family_name, literal(N2))
	; rdfq(Q, Focus, foaf:surname, literal(N2))
	),
	delang(N1, N1L),
	delang(N2, N2L),
	Text = [N1L, ' ', N2L].
good_label(Q, Focus, Text) :-
	rdfq(Q, Focus, foaf:nick, literal(Text1)),
	delang(Text1, Text).
good_label(_Q, Focus, Text) :-
	atom(Focus),	
	rdf_split_url(_, Text, Focus).
good_label(_Q, Focus, Text) :-
	atom(Focus),
	atom_concat('mailto:', Text, Focus).
% cater to TAP?
%   "MusicianLiquid_Sound_Device"  ?   nah.   they have labels.


%%  label(+Q, +Focus, -Text)
%%
%%  Picks labels for the given focus in decreasing order of quality,
%%  on backtracking.    Never fails; just uses a really really bad
%%  name if necessary.    cf: good_label/3.

label(Q, Focus, Text) :-
	good_label(Q, Focus, Text).
label(_Q, '', Text) :-
	Text = '(the null URI -- probably due to error)'.
label(Q, Focus, Text) :-
	rdfq(Q, Focus, rdf:type, Class),
	good_label(Q, Class, ClassName),
	concat_atom(['(an unnamed ', ClassName, ')'], Text).
label(_Q, _Focus, Text) :-
	Text = '(no name)'.


%%  word(+Q, +Focus, -HTree)
%%
%%  Unifies HTree with a short bit of HTML used to display Object.  Often
%%  something like: a(href=___,+label(___)).
%%
%%  Uses Q option:    truncate(+MaxChars)    this is the max-length of the
%%                                           *label*, not the HTree.
%%                    label_quality(good)    fail if you can't find a good
%%                                           label for this (don't give it
%%                                           a lame, made-up name).

word(_Q, literal(unixtime(T)), H) :-
	reltime(T, M),
	convert_time(T, TimeStr),
	string_to_atom(TimeStr, TimeAtom),
	H=[TimeAtom, ' ET (', M, ')'].
word(_Q, literal(Number), [int(Number)]) :-
	integer(Number).
word(Q, Focus, HTree) :-
	atom(Focus),
	(   member(link_predicate(LP), Q)
	->  call(LP, Focus, Link)
	;   Link = \self_uri(basic, Q, [focus=Focus])
	),
	(   memberchk(label_quality(good), Q)
	->  good_label(Q, Focus, Text)
	;   label(Q, Focus, Text)
	),
	Text \= '',
	(   memberchk(truncate(MaxChars), Q)
	->  truncated(Text, MaxChars, Text2)
	;   Text2 = Text
	),
	HTree=a(href=Link, Text2).
word(Q, Focus, HTree) :-
	short(Q, Focus, HTree, []), !.


link(Q, Focus, Label, H) :-
	atom(Focus),
	Link = \self_uri(basic, Q, [focus=Focus]), 
	H=a(href=Link, Label).

/* not used any more, since we discovered rdf_split_url 
last_word(URL, Word) :-
	parse_url(URL, Parts),
	(   member(fragment(Word), Parts)
	->  true
	;   member(path(Path), Parts),
	    concat_atom(Segments, '/', Path),
	    reverse(Segments, [Word | _])
	).
*/

/*
html_view('All Uses', page, Focus, Q,
	  +terms_used(Q, Focus, _FocusURI)).
*/

%	uri(Focus, FocusURI),
%	once(source(_, _, _, FocusURI)).


truncated(Text, Length, Truncated) :-
	atom(Text),
	(   sub_atom(Text, 0, Length, _, _)
	->  SubLength is Length - 3,  % len of '...'
	    sub_atom(Text, 0, SubLength, _, Text2),
	    concat_atom([Text2, '...'], Truncated)
	;   Text=Truncated
	).
/*    better to fail, until we get this working right...
truncated(lang(Lang, Text), Length, lang(Lang, Truncated)) :-
	%  @@@ uhhhh...   not really save to truncate utf-8 string, I suspect.  Ulp...
	atom(Text),
	(   sub_atom(Text, 0, Length, _, Text2)
	->  concat_atom([Text2, '...'], Truncated)
	;   Text=Truncated
	).
*/

shortWord(Q, Focus, HTree) :-
	word([truncate(18) | Q], Focus, HTree).



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%   short/4  is very very old code, pre-"HTree"
%%
%%   could/should be merged up into word/3.
%%
%%



%short(X, Y, _, _) :-
%	print_message(informational, short_trace(X, Y)),
%	fail.

short(_, literal(Text), C1, C2) :-
   C1=[ span(class='literal', Text) | C2 ].

short(_, Num, C1, C2) :-
	number(Num),
	atom_number(N, Num),
	C1=[ span(class='literal', N) | C2 ].

short(Q, unixtime(X)) -->
	{ % ideally, we'd pull a timezone out of the Q...
	  convert_time(X, TimeStr),
	  string_to_atom(TimeStr, TimeAtom),
	  get_time(Now),
	  Ago is Now-X },
	[ TimeAtom, ' (' ],
	short(Q, duration(Ago)),
	[ ' ago)' ].


short(_, duration(X)) -->
	[float(X,3), ' seconds'].

short(Q, URI, C1, C2) :-
   atom(URI),
   rdf_db:ns(URI, QQ),
   Link = \self_uri(basic, Q, [focus=URI]), 
   C1=[ a(href=Link, QQ) | C2 ].

% html_view(qname, word, ... )
/*
short(Q, URI, C1, C2) :-
	atom(URI),
	% try a little backtracking loop to see if there's
	% a shortname for some prefix part of this....
	concat_atom([Long, Local], URI),
	rdfq(Q, Long, sw:nsShortName, literal(Short), _),
	Link = \self_uri(basic, Q, [focus=URI]), 
	C1=[ a(href=Link, Short, ':', Local) | C2 ].
*/

% html_view(qname, word, ... )
short(Q, URI, C1, C2) :-
	atom(URI),	
%   concat_atom([N, X], URI),
%   ns(N, QQ),
   rdf_global_id(NS:Local, URI),
   Link = \self_uri(basic, Q, [focus=URI]), 
   C1=[ a(href=Link, NS, ':', Local) | C2 ].

short(Q, URI, C1, C2) :-
	   atom(URI),	
   Link = \self_uri(basic, Q, [focus=URI]), 
   Label=URI,
   C1=[ a(href=Link, Label) | C2 ].

short(Q, L=R) -->
	short(Q, L),
	[' = '],
	short(Q, R).
short(Q, [H|T]) -->
	short(Q, H),
	[', '],
	short(Q, T).
short(Q, [H|[]]) -->
	short(Q, H).
short(_Q, []) -->
	['[]'].
	
short(_, X) -->
	{ throw(cant_short(X)) }.
	
uri_escape(From, To) :-
   % ohhh, cheating, this is too agressive, but... that's okay!
   www_form_encode(From, To).

