1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "hyperlinkcontext.hxx"
29 
30 #include <com/sun/star/xml/sax/XFastContextHandler.hpp>
31 
32 #include "oox/helper/propertymap.hxx"
33 #include "oox/core/relations.hxx"
34 #include "oox/core/xmlfilterbase.hxx"
35 #include "oox/drawingml/embeddedwavaudiofile.hxx"
36 
37 using ::rtl::OUString;
38 using namespace ::oox::core;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
41 
42 namespace oox {
43 namespace drawingml {
44 
45 HyperLinkContext::HyperLinkContext( ContextHandler& rParent,
46         const Reference< XFastAttributeList >& xAttributes, PropertyMap& aProperties )
47     : ContextHandler( rParent )
48     , maProperties(aProperties)
49 {
50 	OUString sURL, sHref;
51 	OUString aRelId = xAttributes->getOptionalValue( R_TOKEN( id ) );
52 	if ( aRelId.getLength() )
53 	{
54 		OSL_TRACE("OOX: URI rId %s", ::rtl::OUStringToOString (aRelId, RTL_TEXTENCODING_UTF8).pData->buffer);
55 		sHref = getRelations().getExternalTargetFromRelId( aRelId );
56 		if( sHref.getLength() > 0 )
57 		{
58 			OSL_TRACE("OOX: URI href %s", ::rtl::OUStringToOString (sHref, RTL_TEXTENCODING_UTF8).pData->buffer);
59 			sURL = getFilter().getAbsoluteUrl( sHref );
60 		}
61 	}
62 	OUString sTooltip = xAttributes->getOptionalValue( R_TOKEN( tooltip ) );
63 	if ( sTooltip.getLength() )
64 		maProperties[ PROP_Representation ] <<= sTooltip;
65 	OUString sFrame = xAttributes->getOptionalValue( R_TOKEN( tgtFrame ) );
66 	if( sFrame.getLength() )
67 		maProperties[ PROP_TargetFrame ] <<= sFrame;
68 	OUString aAction = xAttributes->getOptionalValue( XML_action );
69 	if ( aAction.getLength() )
70 	{
71 		// reserved values of the unrestricted string aAction:
72 		// ppaction://customshow?id=SHOW_ID				// custom presentation
73 		// ppaction://hlinkfile							// external file via r:id
74 		// ppaction://hlinkpres?slideindex=SLIDE_NUM	// external presentation via r:id
75 		// ppaction://hlinkshowjump?jump=endshow
76 		// ppaction://hlinkshowjump?jump=firstslide
77 		// ppaction://hlinkshowjump?jump=lastslide
78 		// ppaction://hlinkshowjump?jump=lastslideviewed
79 		// ppaction://hlinkshowjump?jump=nextslide
80 		// ppaction://hlinkshowjump?jump=previousslide
81 		// ppaction://hlinksldjump
82 		// ppaction://macro?name=MACRO_NAME
83 		// ppaction://program
84 
85 		const OUString sPPAction( CREATE_OUSTRING( "ppaction://" ) );
86 		if ( aAction.matchIgnoreAsciiCase( sPPAction, 0 ) )
87 		{
88 			OUString aPPAct( aAction.copy( sPPAction.getLength() ) );
89 			sal_Int32 nIndex = aPPAct.indexOf( '?', 0 );
90 			OUString aPPAction( nIndex > 0 ? aPPAct.copy( 0, nIndex ) : aPPAct );
91 
92 			const OUString sHlinkshowjump( CREATE_OUSTRING( "hlinkshowjump" ) );
93 			const OUString sHlinksldjump( CREATE_OUSTRING( "hlinksldjump" ) );
94 			if ( aPPAction.match( sHlinkshowjump ) )
95 			{
96 				const OUString sJump( CREATE_OUSTRING( "jump=" ) );
97 				if ( aPPAct.match( sJump, nIndex + 1 ) )
98 				{
99 					OUString aDestination( aPPAct.copy( nIndex + 1 + sJump.getLength() ) );
100 					sURL = sURL.concat( CREATE_OUSTRING( "#action?jump=" ) );
101 					sURL = sURL.concat( aDestination );
102 				}
103 			}
104 			else if ( aPPAction.match( sHlinksldjump ) )
105 			{
106 				sURL = OUString();
107 
108 				sal_Int32 nIndex2 = 0;
109 				while ( nIndex2 < sHref.getLength() )
110 				{
111 					sal_Unicode nChar = sHref[ nIndex2 ];
112 					if ( ( nChar >= '0' ) && ( nChar <= '9' ) )
113 						break;
114 					nIndex2++;
115 				}
116 				if ( nIndex2 && ( nIndex2 != sHref.getLength() ) )
117 				{
118 					sal_Int32 nLength = 1;
119 					while( ( nIndex2 + nLength ) < sHref.getLength() )
120 					{
121 						sal_Unicode nChar = sHref[ nIndex2 + nLength ];
122 						if ( ( nChar < '0' ) || ( nChar > '9' ) )
123 							break;
124 						nLength++;
125 					}
126 					sal_Int32 nPageNumber = sHref.copy( nIndex2, nLength ).toInt32();
127 					if ( nPageNumber )
128 					{
129 						const OUString sSlide( CREATE_OUSTRING( "slide" ) );
130 						const OUString sNotesSlide( CREATE_OUSTRING( "notesSlide" ) );
131 						const OUString aSlideType( sHref.copy( 0, nIndex2 ) );
132 						if ( aSlideType.match( sSlide ) )
133 							sURL = CREATE_OUSTRING( "#Slide " ).concat( rtl::OUString::valueOf( nPageNumber ) );
134 						else if ( aSlideType.match( sNotesSlide ) )
135 							sURL = CREATE_OUSTRING( "#Notes " ).concat( rtl::OUString::valueOf( nPageNumber ) );
136 //						else: todo for other types such as notesMaster or slideMaster as they can't be referenced easily
137 					}
138 				}
139 			}
140 		}
141 	}
142 	if ( sURL.getLength() )
143 		maProperties[ PROP_URL ] <<= sURL;
144 
145 	// TODO unhandled
146     // XML_invalidUrl
147     // XML_history
148     // XML_highlightClick
149     // XML_endSnd
150 }
151 
152 HyperLinkContext::~HyperLinkContext()
153 {
154 }
155 
156 Reference< XFastContextHandler > HyperLinkContext::createFastChildContext(
157         ::sal_Int32 aElement, const Reference< XFastAttributeList >& xAttribs ) throw (SAXException, RuntimeException)
158 {
159     Reference< XFastContextHandler > xRet;
160     switch( aElement )
161     {
162     case A_TOKEN( extLst ):
163         return xRet;
164     case A_TOKEN( snd ):
165         EmbeddedWAVAudioFile aAudio;
166         getEmbeddedWAVAudioFile( getRelations(), xAttribs, aAudio );
167         break;
168     }
169     if ( !xRet.is() )
170         xRet.set( this );
171     return xRet;
172 }
173 
174 } // namespace drawingml
175 } // namespace oox
176 
177