xref: /trunk/main/sfx2/source/appl/linkmgr2.cxx (revision b0c1db7e)
1d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5d119d52dSAndrew Rist  * distributed with this work for additional information
6d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10d119d52dSAndrew Rist  *
11d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12d119d52dSAndrew Rist  *
13d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17d119d52dSAndrew Rist  * specific language governing permissions and limitations
18d119d52dSAndrew Rist  * under the License.
19d119d52dSAndrew Rist  *
20d119d52dSAndrew Rist  *************************************************************/
21d119d52dSAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <sfx2/linkmgr.hxx>
26cdf0e10cSrcweir #include <com/sun/star/document/UpdateDocMode.hpp>
27cdf0e10cSrcweir #include <sfx2/objsh.hxx>
28cdf0e10cSrcweir #include <svl/urihelper.hxx>
29cdf0e10cSrcweir #include <sot/formats.hxx>
30cdf0e10cSrcweir #include <tools/urlobj.hxx>
31cdf0e10cSrcweir #include <sot/exchange.hxx>
32cdf0e10cSrcweir #include <tools/debug.hxx>
33cdf0e10cSrcweir #include <vcl/msgbox.hxx>
34cdf0e10cSrcweir #include <sfx2/lnkbase.hxx>
35cdf0e10cSrcweir #include <sfx2/app.hxx>
36cdf0e10cSrcweir #include <vcl/graph.hxx>
37cdf0e10cSrcweir #include <svl/stritem.hxx>
38cdf0e10cSrcweir #include <svl/eitem.hxx>
39cdf0e10cSrcweir #include <svl/intitem.hxx>
40cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
41cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
42cdf0e10cSrcweir #include <sfx2/request.hxx>
4345fd3b9aSArmin Le Grand #include <vcl/dibtools.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "fileobj.hxx"
46cdf0e10cSrcweir #include "impldde.hxx"
47cdf0e10cSrcweir #include "app.hrc"
48cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #define _SVSTDARR_STRINGSDTOR
51cdf0e10cSrcweir #include <svl/svstdarr.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir namespace sfx2
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 
56cdf0e10cSrcweir class SvxInternalLink : public sfx2::SvLinkSource
57cdf0e10cSrcweir {
58cdf0e10cSrcweir public:
59cdf0e10cSrcweir 	SvxInternalLink() {}
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     virtual sal_Bool Connect( sfx2::SvBaseLink* );
62cdf0e10cSrcweir };
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 
65cdf0e10cSrcweir SV_IMPL_PTRARR( SvBaseLinks, SvBaseLinkRefPtr )
66cdf0e10cSrcweir 
67cdf0e10cSrcweir LinkManager::LinkManager(SfxObjectShell* p)
68*b0c1db7eSPatricia Shanahan 	: pPersist(p),
69*b0c1db7eSPatricia Shanahan 	mUpdateAsked(sal_False),
70*b0c1db7eSPatricia Shanahan 	mAutoAskUpdateAllLinks(sal_False)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir LinkManager::~LinkManager()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	SvBaseLinkRef** ppRef = (SvBaseLinkRef**)aLinkTbl.GetData();
77cdf0e10cSrcweir 	for( sal_uInt16 n = aLinkTbl.Count(); n; --n, ++ppRef )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		if( (*ppRef)->Is() )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			(*(*ppRef))->Disconnect();
82cdf0e10cSrcweir             (*(*ppRef))->SetLinkManager( NULL );
83cdf0e10cSrcweir 		}
84cdf0e10cSrcweir 		delete *ppRef;
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir /************************************************************************
90cdf0e10cSrcweir |*    LinkManager::Remove()
91cdf0e10cSrcweir |*
925799d6dbSmarcus |*    Description
93cdf0e10cSrcweir *************************************************************************/
94cdf0e10cSrcweir 
95cdf0e10cSrcweir void LinkManager::Remove( SvBaseLink *pLink )
96cdf0e10cSrcweir {
975799d6dbSmarcus 	// do not insert links double
98cdf0e10cSrcweir 	int bFound = sal_False;
99cdf0e10cSrcweir 	SvBaseLinkRef** ppRef = (SvBaseLinkRef**)aLinkTbl.GetData();
100cdf0e10cSrcweir 	for( sal_uInt16 n = aLinkTbl.Count(); n; --n, ++ppRef )
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir 		if( pLink == *(*ppRef) )
103cdf0e10cSrcweir 		{
104cdf0e10cSrcweir 			(*(*ppRef))->Disconnect();
105cdf0e10cSrcweir             (*(*ppRef))->SetLinkManager( NULL );
106cdf0e10cSrcweir 			(*(*ppRef)).Clear();
107cdf0e10cSrcweir 			bFound = sal_True;
108cdf0e10cSrcweir 		}
109cdf0e10cSrcweir 
1105799d6dbSmarcus 		// if there are still some empty ones, get rid of them
111cdf0e10cSrcweir 		if( !(*ppRef)->Is() )
112cdf0e10cSrcweir 		{
113cdf0e10cSrcweir 			delete *ppRef;
114cdf0e10cSrcweir 			aLinkTbl.Remove( aLinkTbl.Count() - n, 1 );
115cdf0e10cSrcweir 			if( bFound )
116cdf0e10cSrcweir 				return ;
117cdf0e10cSrcweir 			--ppRef;
118cdf0e10cSrcweir 		}
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 
123cdf0e10cSrcweir void LinkManager::Remove( sal_uInt16 nPos, sal_uInt16 nCnt )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	if( nCnt && nPos < aLinkTbl.Count() )
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 		if( nPos + nCnt > aLinkTbl.Count() )
128cdf0e10cSrcweir 			nCnt = aLinkTbl.Count() - nPos;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		SvBaseLinkRef** ppRef = (SvBaseLinkRef**)aLinkTbl.GetData() + nPos;
131cdf0e10cSrcweir 		for( sal_uInt16 n = nCnt; n; --n, ++ppRef )
132cdf0e10cSrcweir 		{
133cdf0e10cSrcweir 			if( (*ppRef)->Is() )
134cdf0e10cSrcweir 			{
135cdf0e10cSrcweir 				(*(*ppRef))->Disconnect();
136cdf0e10cSrcweir                 (*(*ppRef))->SetLinkManager( NULL );
137cdf0e10cSrcweir 			}
138cdf0e10cSrcweir 			delete *ppRef;
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 		aLinkTbl.Remove( nPos, nCnt );
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 
145cdf0e10cSrcweir sal_Bool LinkManager::Insert( SvBaseLink* pLink )
146cdf0e10cSrcweir {
147*b0c1db7eSPatricia Shanahan 
1485799d6dbSmarcus 	// do not insert links double
149cdf0e10cSrcweir 	for( sal_uInt16 n = 0; n < aLinkTbl.Count(); ++n )
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 		SvBaseLinkRef* pTmp = aLinkTbl[ n ];
152cdf0e10cSrcweir 		if( !pTmp->Is() )
153cdf0e10cSrcweir 			aLinkTbl.DeleteAndDestroy( n-- );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		if( pLink == *pTmp )
156cdf0e10cSrcweir 			return sal_False;
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	SvBaseLinkRef* pTmp = new SvBaseLinkRef( pLink );
160cdf0e10cSrcweir     pLink->SetLinkManager( this );
161cdf0e10cSrcweir 	aLinkTbl.Insert( pTmp, aLinkTbl.Count() );
162*b0c1db7eSPatricia Shanahan 	Window *parent = GetPersist()->GetDialogParent();
163*b0c1db7eSPatricia Shanahan 	if (mAutoAskUpdateAllLinks)
164*b0c1db7eSPatricia Shanahan 	{
165*b0c1db7eSPatricia Shanahan 		SetUserAllowsLinkUpdate(pLink, GetUserAllowsLinkUpdate(parent));
166*b0c1db7eSPatricia Shanahan 	}
167*b0c1db7eSPatricia Shanahan 
168cdf0e10cSrcweir 	return sal_True;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
172cdf0e10cSrcweir sal_Bool LinkManager::InsertLink( SvBaseLink * pLink,
173cdf0e10cSrcweir 								sal_uInt16 nObjType,
174cdf0e10cSrcweir 								sal_uInt16 nUpdateMode,
175cdf0e10cSrcweir 								const String* pName )
176cdf0e10cSrcweir {
1775799d6dbSmarcus 	// in any case: do this first
178cdf0e10cSrcweir 	pLink->SetObjType( nObjType );
179cdf0e10cSrcweir 	if( pName )
180cdf0e10cSrcweir 		pLink->SetName( *pName );
181cdf0e10cSrcweir 	pLink->SetUpdateMode( nUpdateMode );
182cdf0e10cSrcweir 	return Insert( pLink );
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
186cdf0e10cSrcweir sal_Bool LinkManager::InsertDDELink( SvBaseLink * pLink,
187cdf0e10cSrcweir 									const String& rServer,
188cdf0e10cSrcweir 									const String& rTopic,
189cdf0e10cSrcweir 									const String& rItem )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	if( !( OBJECT_CLIENT_SO & pLink->GetObjType() ) )
192cdf0e10cSrcweir 		return sal_False;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	String sCmd;
195cdf0e10cSrcweir 	::sfx2::MakeLnkName( sCmd, &rServer, rTopic, rItem );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	pLink->SetObjType( OBJECT_CLIENT_DDE );
198cdf0e10cSrcweir 	pLink->SetName( sCmd );
199cdf0e10cSrcweir 	return Insert( pLink );
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 
203cdf0e10cSrcweir sal_Bool LinkManager::InsertDDELink( SvBaseLink * pLink )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	DBG_ASSERT( OBJECT_CLIENT_SO & pLink->GetObjType(), "no OBJECT_CLIENT_SO" );
206cdf0e10cSrcweir 	if( !( OBJECT_CLIENT_SO & pLink->GetObjType() ) )
207cdf0e10cSrcweir 		return sal_False;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 	if( pLink->GetObjType() == OBJECT_CLIENT_SO )
210cdf0e10cSrcweir 		pLink->SetObjType( OBJECT_CLIENT_DDE );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	return Insert( pLink );
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
2165799d6dbSmarcus // ask for the strings to be used in the dialog
217cdf0e10cSrcweir sal_Bool LinkManager::GetDisplayNames( const SvBaseLink * pLink,
218cdf0e10cSrcweir 										String* pType,
219cdf0e10cSrcweir 										String* pFile,
220cdf0e10cSrcweir 										String* pLinkStr,
221cdf0e10cSrcweir 										String* pFilter ) const
222cdf0e10cSrcweir {
223cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
224cdf0e10cSrcweir 	const String sLNm( pLink->GetLinkSourceName() );
225cdf0e10cSrcweir 	if( sLNm.Len() )
226cdf0e10cSrcweir 	{
227cdf0e10cSrcweir 		switch( pLink->GetObjType() )
228cdf0e10cSrcweir 		{
229cdf0e10cSrcweir 		    case OBJECT_CLIENT_FILE:
230cdf0e10cSrcweir 		    case OBJECT_CLIENT_GRF:
231cdf0e10cSrcweir 		    case OBJECT_CLIENT_OLE:
232cdf0e10cSrcweir 			    {
233cdf0e10cSrcweir 				    sal_uInt16 nPos = 0;
234cdf0e10cSrcweir 				    String sFile( sLNm.GetToken( 0, ::sfx2::cTokenSeperator, nPos ) );
235cdf0e10cSrcweir 				    String sRange( sLNm.GetToken( 0, ::sfx2::cTokenSeperator, nPos ) );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 				    if( pFile )
238cdf0e10cSrcweir 					    *pFile = sFile;
239cdf0e10cSrcweir 				    if( pLinkStr )
240cdf0e10cSrcweir 					    *pLinkStr = sRange;
241cdf0e10cSrcweir 				    if( pFilter )
242cdf0e10cSrcweir 					    *pFilter = sLNm.Copy( nPos );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 				    if( pType )
245cdf0e10cSrcweir 				    {
246cdf0e10cSrcweir 					    sal_uInt16 nObjType = pLink->GetObjType();
247cdf0e10cSrcweir 					    *pType = String( SfxResId(
248cdf0e10cSrcweir 								    ( OBJECT_CLIENT_FILE == nObjType || OBJECT_CLIENT_OLE == nObjType )
249cdf0e10cSrcweir 										    ? RID_SVXSTR_FILELINK
250cdf0e10cSrcweir 										    : RID_SVXSTR_GRAFIKLINK ));
251cdf0e10cSrcweir 				    }
252cdf0e10cSrcweir 				    bRet = sal_True;
253cdf0e10cSrcweir 			    }
254cdf0e10cSrcweir 			    break;
255cdf0e10cSrcweir 		    case OBJECT_CLIENT_DDE:
256cdf0e10cSrcweir 	            {
257cdf0e10cSrcweir 		            sal_uInt16 nTmp = 0;
258cdf0e10cSrcweir 		            String sCmd( sLNm );
259cdf0e10cSrcweir 		            String sServer( sCmd.GetToken( 0, cTokenSeperator, nTmp ) );
260cdf0e10cSrcweir 		            String sTopic( sCmd.GetToken( 0, cTokenSeperator, nTmp ) );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 		            if( pType )
263cdf0e10cSrcweir 			            *pType = sServer;
264cdf0e10cSrcweir 		            if( pFile )
265cdf0e10cSrcweir 			            *pFile = sTopic;
266cdf0e10cSrcweir 		            if( pLinkStr )
267cdf0e10cSrcweir 			            *pLinkStr = sCmd.Copy( nTmp );
268cdf0e10cSrcweir 		            bRet = sal_True;
269cdf0e10cSrcweir 		        }
270cdf0e10cSrcweir 		        break;
271cdf0e10cSrcweir 	        default:
272cdf0e10cSrcweir 	            break;
273cdf0e10cSrcweir 	    }
274cdf0e10cSrcweir 	}
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	return bRet;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
279*b0c1db7eSPatricia Shanahan void LinkManager::SetAutoAskUpdateAllLinks()
280*b0c1db7eSPatricia Shanahan {
281*b0c1db7eSPatricia Shanahan 	mAutoAskUpdateAllLinks = sal_True;
282*b0c1db7eSPatricia Shanahan }
283*b0c1db7eSPatricia Shanahan 
284*b0c1db7eSPatricia Shanahan sal_Bool LinkManager::GetUserAllowsLinkUpdate(Window *pParentWin)
285*b0c1db7eSPatricia Shanahan {
286*b0c1db7eSPatricia Shanahan 	if (!mUpdateAsked)
287*b0c1db7eSPatricia Shanahan 	{
288*b0c1db7eSPatricia Shanahan 		if (QueryBox(pParentWin, WB_YES_NO | WB_DEF_NO, SfxResId(STR_QUERY_UPDATE_LINKS)).Execute() == RET_YES)
289*b0c1db7eSPatricia Shanahan 			mAllowUpdate = sal_True;
290*b0c1db7eSPatricia Shanahan 		else
291*b0c1db7eSPatricia Shanahan 			mAllowUpdate = sal_False;
292*b0c1db7eSPatricia Shanahan 		mUpdateAsked = sal_True;
293*b0c1db7eSPatricia Shanahan 	}
294*b0c1db7eSPatricia Shanahan 	return mAllowUpdate;
295*b0c1db7eSPatricia Shanahan }
296*b0c1db7eSPatricia Shanahan 
297*b0c1db7eSPatricia Shanahan void LinkManager::SetUserAllowsLinkUpdate(SvBaseLink *pLink, sal_Bool allows)
298*b0c1db7eSPatricia Shanahan {
299*b0c1db7eSPatricia Shanahan 	SfxObjectShell* pShell = pLink->GetLinkManager()->GetPersist();
300*b0c1db7eSPatricia Shanahan 
301*b0c1db7eSPatricia Shanahan 	if (pShell)
302*b0c1db7eSPatricia Shanahan 	{
303*b0c1db7eSPatricia Shanahan 		comphelper::EmbeddedObjectContainer& rEmbeddedObjectContainer = pShell->getEmbeddedObjectContainer();
304*b0c1db7eSPatricia Shanahan 		rEmbeddedObjectContainer.setUserAllowsLinkUpdate(allows);
305*b0c1db7eSPatricia Shanahan 	}
306*b0c1db7eSPatricia Shanahan }
307*b0c1db7eSPatricia Shanahan 
308cdf0e10cSrcweir 
309cdf0e10cSrcweir void LinkManager::UpdateAllLinks(
310cdf0e10cSrcweir     sal_Bool bAskUpdate,
311cdf0e10cSrcweir     sal_Bool /*bCallErrHdl*/,
312cdf0e10cSrcweir     sal_Bool bUpdateGrfLinks,
313cdf0e10cSrcweir     Window* pParentWin )
314cdf0e10cSrcweir {
315cdf0e10cSrcweir 	SvStringsDtor aApps, aTopics, aItems;
316cdf0e10cSrcweir 	String sApp, sTopic, sItem;
317cdf0e10cSrcweir 
3185799d6dbSmarcus 	// first create a copy of the array, so that updated links to not interfere with ... in between!!
319cdf0e10cSrcweir 	SvPtrarr aTmpArr( 255, 50 );
320cdf0e10cSrcweir 	sal_uInt16 n;
321cdf0e10cSrcweir 	for( n = 0; n < aLinkTbl.Count(); ++n )
322cdf0e10cSrcweir 	{
323cdf0e10cSrcweir 		SvBaseLink* pLink = *aLinkTbl[ n ];
324cdf0e10cSrcweir 		if( !pLink )
325cdf0e10cSrcweir 		{
326cdf0e10cSrcweir 			Remove( n-- );
327cdf0e10cSrcweir 			continue;
328cdf0e10cSrcweir 		}
329cdf0e10cSrcweir 		aTmpArr.Insert( pLink, aTmpArr.Count() );
330cdf0e10cSrcweir 	}
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 	for( n = 0; n < aTmpArr.Count(); ++n )
333cdf0e10cSrcweir 	{
334cdf0e10cSrcweir 		SvBaseLink* pLink = (SvBaseLink*)aTmpArr[ n ];
335cdf0e10cSrcweir 
3365799d6dbSmarcus 		// first search the entry in the array
337cdf0e10cSrcweir 		sal_uInt16 nFndPos = USHRT_MAX;
338cdf0e10cSrcweir 		for( sal_uInt16 i = 0; i < aLinkTbl.Count(); ++i )
339cdf0e10cSrcweir 			if( pLink == *aLinkTbl[ i ] )
340cdf0e10cSrcweir 			{
341cdf0e10cSrcweir 				nFndPos = i;
342cdf0e10cSrcweir 				break;
343cdf0e10cSrcweir 			}
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 		if( USHRT_MAX == nFndPos )
3465799d6dbSmarcus 			continue;					// was not already existing!
347cdf0e10cSrcweir 
3485799d6dbSmarcus 		// do not update graphic links yet
349cdf0e10cSrcweir 		if( !pLink->IsVisible() ||
350cdf0e10cSrcweir 			( !bUpdateGrfLinks && OBJECT_CLIENT_GRF == pLink->GetObjType() ))
351cdf0e10cSrcweir 			continue;
352cdf0e10cSrcweir 
353*b0c1db7eSPatricia Shanahan 		sal_Bool allows = sal_True;
354*b0c1db7eSPatricia Shanahan 
355*b0c1db7eSPatricia Shanahan 		if (bAskUpdate)
356cdf0e10cSrcweir 		{
357*b0c1db7eSPatricia Shanahan 			allows = GetUserAllowsLinkUpdate(pParentWin);
358cdf0e10cSrcweir 		}
359cdf0e10cSrcweir 
360*b0c1db7eSPatricia Shanahan 		SetUserAllowsLinkUpdate(pLink, allows);
361*b0c1db7eSPatricia Shanahan 	    bAskUpdate = sal_False;		// one time is OK
362*b0c1db7eSPatricia Shanahan 
363*b0c1db7eSPatricia Shanahan 		if (allows)
364*b0c1db7eSPatricia Shanahan 			pLink->Update();
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir /************************************************************************
369cdf0e10cSrcweir |*    SvBaseLink::CreateObject()
370cdf0e10cSrcweir |*
3715799d6dbSmarcus |*    Description
372cdf0e10cSrcweir *************************************************************************/
373cdf0e10cSrcweir 
374cdf0e10cSrcweir SvLinkSourceRef LinkManager::CreateObj( SvBaseLink * pLink )
375cdf0e10cSrcweir {
376cdf0e10cSrcweir 	switch( pLink->GetObjType() )
377cdf0e10cSrcweir 	{
378cdf0e10cSrcweir 	    case OBJECT_CLIENT_FILE:
379cdf0e10cSrcweir 	    case OBJECT_CLIENT_GRF:
380cdf0e10cSrcweir 	    case OBJECT_CLIENT_OLE:
381cdf0e10cSrcweir 		    return new SvFileObject;
382cdf0e10cSrcweir 	    case OBJECT_INTERN:
383cdf0e10cSrcweir 		    return new SvxInternalLink;
384cdf0e10cSrcweir         case OBJECT_CLIENT_DDE:
385cdf0e10cSrcweir 		    return new SvDDEObject;
386cdf0e10cSrcweir 	    default:
387cdf0e10cSrcweir         	return SvLinkSourceRef();
388cdf0e10cSrcweir    	}
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir sal_Bool LinkManager::InsertServer( SvLinkSource* pObj )
392cdf0e10cSrcweir {
3935799d6dbSmarcus 	// do not insert double
394cdf0e10cSrcweir 	if( !pObj || USHRT_MAX != aServerTbl.GetPos( pObj ) )
395cdf0e10cSrcweir 		return sal_False;
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	aServerTbl.Insert( pObj, aServerTbl.Count() );
398cdf0e10cSrcweir 	return sal_True;
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 
402cdf0e10cSrcweir void LinkManager::RemoveServer( SvLinkSource* pObj )
403cdf0e10cSrcweir {
404cdf0e10cSrcweir 	sal_uInt16 nPos = aServerTbl.GetPos( pObj );
405cdf0e10cSrcweir 	if( USHRT_MAX != nPos )
406cdf0e10cSrcweir 		aServerTbl.Remove( nPos, 1 );
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 
410cdf0e10cSrcweir void MakeLnkName( String& rName, const String* pType, const String& rFile,
411cdf0e10cSrcweir 					const String& rLink, const String* pFilter )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir 	if( pType )
414cdf0e10cSrcweir 		(rName = *pType).EraseLeadingChars().EraseTrailingChars() += cTokenSeperator;
415cdf0e10cSrcweir 	else if( rName.Len() )
416cdf0e10cSrcweir 		rName.Erase();
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	((rName += rFile).EraseLeadingChars().EraseTrailingChars() +=
419cdf0e10cSrcweir 		cTokenSeperator ).EraseLeadingChars().EraseTrailingChars() += rLink;
420cdf0e10cSrcweir 	if( pFilter )
421cdf0e10cSrcweir 		((rName += cTokenSeperator ) += *pFilter).EraseLeadingChars().EraseTrailingChars();
422cdf0e10cSrcweir }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir sal_Bool LinkManager::InsertFileLink( sfx2::SvBaseLink& rLink,
425cdf0e10cSrcweir 									sal_uInt16 nFileType,
426cdf0e10cSrcweir 									const String& rFileNm,
427cdf0e10cSrcweir 									const String* pFilterNm,
428cdf0e10cSrcweir 									const String* pRange )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir 	if( !( OBJECT_CLIENT_SO & rLink.GetObjType() ))
431cdf0e10cSrcweir 		return sal_False;
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	String sCmd( rFileNm );
434cdf0e10cSrcweir 	sCmd += ::sfx2::cTokenSeperator;
435cdf0e10cSrcweir 	if( pRange )
436cdf0e10cSrcweir 		sCmd += *pRange;
437cdf0e10cSrcweir 	if( pFilterNm )
438cdf0e10cSrcweir 		( sCmd += ::sfx2::cTokenSeperator ) += *pFilterNm;
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	return InsertLink( &rLink, nFileType, sfx2::LINKUPDATE_ONCALL, &sCmd );
441cdf0e10cSrcweir }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir sal_Bool LinkManager::InsertFileLink( sfx2::SvBaseLink& rLink )
444cdf0e10cSrcweir {
445cdf0e10cSrcweir 	if( OBJECT_CLIENT_FILE == ( OBJECT_CLIENT_FILE & rLink.GetObjType() ))
446cdf0e10cSrcweir 		return InsertLink( &rLink, rLink.GetObjType(), sfx2::LINKUPDATE_ONCALL );
447cdf0e10cSrcweir 	return sal_False;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
4505799d6dbSmarcus // a transfer will be discontinued, therefore cancel all DownloadMedia
4515799d6dbSmarcus // (at the moment only interesting for the FileLinks!)
452cdf0e10cSrcweir void LinkManager::CancelTransfers()
453cdf0e10cSrcweir {
454cdf0e10cSrcweir 	SvFileObject* pFileObj;
455cdf0e10cSrcweir 	sfx2::SvBaseLink* pLnk;
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 	const sfx2::SvBaseLinks& rLnks = GetLinks();
458cdf0e10cSrcweir 	for( sal_uInt16 n = rLnks.Count(); n; )
459cdf0e10cSrcweir 		if( 0 != ( pLnk = &(*rLnks[ --n ])) &&
460cdf0e10cSrcweir 			OBJECT_CLIENT_FILE == (OBJECT_CLIENT_FILE & pLnk->GetObjType()) &&
461cdf0e10cSrcweir 			0 != ( pFileObj = (SvFileObject*)pLnk->GetObj() ) )
462cdf0e10cSrcweir //			0 != ( pFileObj = (SvFileObject*)SvFileObject::ClassFactory()->
463cdf0e10cSrcweir //									CastAndAddRef( pLnk->GetObj() )) )
464cdf0e10cSrcweir 			pFileObj->CancelTransfers();
465cdf0e10cSrcweir }
466cdf0e10cSrcweir 
4675799d6dbSmarcus 	// to send status information from the FileObject to the BaseLink, there is an own ClipboardId.
4685799d6dbSmarcus 	// The SvData object has then the respective information as string.
4695799d6dbSmarcus 	// Currently this will be used for FileObject in connection with JavaScript
4705799d6dbSmarcus 	// - that needs information about Load/Abort/Error
471cdf0e10cSrcweir sal_uIntPtr LinkManager::RegisterStatusInfoId()
472cdf0e10cSrcweir {
473cdf0e10cSrcweir 	static sal_uIntPtr nFormat = 0;
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	if( !nFormat )
476cdf0e10cSrcweir 	{
4775799d6dbSmarcus // how does the new interface look like?
478cdf0e10cSrcweir //		nFormat = Exchange::RegisterFormatName( "StatusInfo vom SvxInternalLink" );
479cdf0e10cSrcweir 		nFormat = SotExchange::RegisterFormatName(
480cdf0e10cSrcweir 					String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
481cdf0e10cSrcweir 								"StatusInfo vom SvxInternalLink" )));
482cdf0e10cSrcweir 	}
483cdf0e10cSrcweir 	return nFormat;
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir // ----------------------------------------------------------------------
487cdf0e10cSrcweir 
488cdf0e10cSrcweir sal_Bool LinkManager::GetGraphicFromAny( const String& rMimeType,
489cdf0e10cSrcweir 								const ::com::sun::star::uno::Any & rValue,
490cdf0e10cSrcweir 								Graphic& rGrf )
491cdf0e10cSrcweir {
492cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
493cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< sal_Int8 > aSeq;
494cdf0e10cSrcweir 	if( rValue.hasValue() && ( rValue >>= aSeq ) )
495cdf0e10cSrcweir 	{
496cdf0e10cSrcweir 		SvMemoryStream aMemStm( (void*)aSeq.getConstArray(), aSeq.getLength(),
497cdf0e10cSrcweir 								STREAM_READ );
498cdf0e10cSrcweir 		aMemStm.Seek( 0 );
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 		switch( SotExchange::GetFormatIdFromMimeType( rMimeType ) )
501cdf0e10cSrcweir 		{
502cdf0e10cSrcweir 		case SOT_FORMATSTR_ID_SVXB:
503cdf0e10cSrcweir 			{
504cdf0e10cSrcweir 				aMemStm >> rGrf;
505cdf0e10cSrcweir 				bRet = sal_True;
506cdf0e10cSrcweir 			}
507cdf0e10cSrcweir 			break;
508cdf0e10cSrcweir 		case FORMAT_GDIMETAFILE:
509cdf0e10cSrcweir 			{
510cdf0e10cSrcweir 				GDIMetaFile aMtf;
511cdf0e10cSrcweir 				aMtf.Read( aMemStm );
512cdf0e10cSrcweir 				rGrf = aMtf;
513cdf0e10cSrcweir 				bRet = sal_True;
514cdf0e10cSrcweir 			}
515cdf0e10cSrcweir 			break;
516cdf0e10cSrcweir 		case FORMAT_BITMAP:
517cdf0e10cSrcweir 			{
518cdf0e10cSrcweir 				Bitmap aBmp;
51945fd3b9aSArmin Le Grand                 ReadDIB(aBmp, aMemStm, true);
520cdf0e10cSrcweir 				rGrf = aBmp;
521cdf0e10cSrcweir 				bRet = sal_True;
522cdf0e10cSrcweir 			}
523cdf0e10cSrcweir 			break;
524cdf0e10cSrcweir 		}
525cdf0e10cSrcweir 	}
526cdf0e10cSrcweir 	return bRet;
527cdf0e10cSrcweir }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 
530cdf0e10cSrcweir // ----------------------------------------------------------------------
531cdf0e10cSrcweir String lcl_DDE_RelToAbs( const String& rTopic, const String& rBaseURL )
532cdf0e10cSrcweir {
533cdf0e10cSrcweir 	String sRet;
534cdf0e10cSrcweir 	INetURLObject aURL( rTopic );
535cdf0e10cSrcweir 	if( INET_PROT_NOT_VALID == aURL.GetProtocol() )
536cdf0e10cSrcweir         utl::LocalFileHelper::ConvertSystemPathToURL( rTopic, rBaseURL, sRet );
537cdf0e10cSrcweir 	if( !sRet.Len() )
538cdf0e10cSrcweir         sRet = URIHelper::SmartRel2Abs( INetURLObject(rBaseURL), rTopic, URIHelper::GetMaybeFileHdl(), true );
539cdf0e10cSrcweir 	return sRet;
540cdf0e10cSrcweir }
541cdf0e10cSrcweir 
542cdf0e10cSrcweir sal_Bool SvxInternalLink::Connect( sfx2::SvBaseLink* pLink )
543cdf0e10cSrcweir {
544cdf0e10cSrcweir 	SfxObjectShell* pFndShell = 0;
545cdf0e10cSrcweir 	sal_uInt16 nUpdateMode = com::sun::star::document::UpdateDocMode::NO_UPDATE;
546cdf0e10cSrcweir 	String sTopic, sItem, sReferer;
547cdf0e10cSrcweir 	if( pLink->GetLinkManager() &&
548cdf0e10cSrcweir 		pLink->GetLinkManager()->GetDisplayNames( pLink, 0, &sTopic, &sItem )
549cdf0e10cSrcweir 		&& sTopic.Len() )
550cdf0e10cSrcweir 	{
5515799d6dbSmarcus 		// for the moment run through the DocumentShells and search for the ones with names:
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 	    com::sun::star::lang::Locale aLocale;
554cdf0e10cSrcweir 	    MsLangId::convertLanguageToLocale( LANGUAGE_SYSTEM, aLocale );
555cdf0e10cSrcweir 		CharClass aCC( aLocale );
556cdf0e10cSrcweir 
557cdf0e10cSrcweir         String sNm( sTopic ), sTmp;
558cdf0e10cSrcweir 		aCC.toLower( sNm );
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 		TypeId aType( TYPE(SfxObjectShell) );
561cdf0e10cSrcweir 
562cdf0e10cSrcweir 		sal_Bool bFirst = sal_True;
563cdf0e10cSrcweir         SfxObjectShell* pShell = pLink->GetLinkManager()->GetPersist();
564cdf0e10cSrcweir         if( pShell && pShell->GetMedium() )
565cdf0e10cSrcweir 		{
566cdf0e10cSrcweir             sReferer = pShell->GetMedium()->GetBaseURL();
567cdf0e10cSrcweir 			SFX_ITEMSET_ARG( pShell->GetMedium()->GetItemSet(), pItem, SfxUInt16Item, SID_UPDATEDOCMODE, sal_False );
568cdf0e10cSrcweir 			if ( pItem )
569cdf0e10cSrcweir 				nUpdateMode = pItem->GetValue();
570cdf0e10cSrcweir 		}
571cdf0e10cSrcweir 
572cdf0e10cSrcweir         String sNmURL( lcl_DDE_RelToAbs( sTopic, sReferer ) );
573cdf0e10cSrcweir 		aCC.toLower( sNmURL );
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 		if ( !pShell )
576cdf0e10cSrcweir 		{
577cdf0e10cSrcweir 			bFirst = sal_False;
578cdf0e10cSrcweir             pShell = SfxObjectShell::GetFirst( &aType, sal_False );
579cdf0e10cSrcweir 		}
580cdf0e10cSrcweir 
581cdf0e10cSrcweir 		while( pShell )
582cdf0e10cSrcweir 		{
583cdf0e10cSrcweir 			if( !sTmp.Len() )
584cdf0e10cSrcweir 			{
585cdf0e10cSrcweir 				sTmp = pShell->GetTitle( SFX_TITLE_FULLNAME );
586cdf0e10cSrcweir                 sTmp = lcl_DDE_RelToAbs(sTmp, sReferer );
587cdf0e10cSrcweir 			}
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 
590cdf0e10cSrcweir 			aCC.toLower( sTmp );
5915799d6dbSmarcus 			if( sTmp == sNmURL )		// these we want to have
592cdf0e10cSrcweir 			{
593cdf0e10cSrcweir 				pFndShell = pShell;
594cdf0e10cSrcweir 				break;
595cdf0e10cSrcweir 			}
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 			if( bFirst )
598cdf0e10cSrcweir 			{
599cdf0e10cSrcweir 				bFirst = sal_False;
600cdf0e10cSrcweir                 pShell = SfxObjectShell::GetFirst( &aType, sal_False );
601cdf0e10cSrcweir 			}
602cdf0e10cSrcweir 			else
603cdf0e10cSrcweir                 pShell = SfxObjectShell::GetNext( *pShell, &aType, sal_False );
604cdf0e10cSrcweir 
605cdf0e10cSrcweir 			sTmp.Erase();
606cdf0e10cSrcweir 		}
607cdf0e10cSrcweir 	}
608cdf0e10cSrcweir 
609cdf0e10cSrcweir 	// empty topics are not allowed - which document is it
610cdf0e10cSrcweir 	if( !sTopic.Len() )
611cdf0e10cSrcweir 		return sal_False;
612cdf0e10cSrcweir 
613cdf0e10cSrcweir 	if( !pFndShell )
614cdf0e10cSrcweir 	{
6155799d6dbSmarcus 		// try to load the file:
616cdf0e10cSrcweir 		INetURLObject aURL( sTopic );
617cdf0e10cSrcweir 		INetProtocol eOld = aURL.GetProtocol();
618cdf0e10cSrcweir         aURL.SetURL( sTopic = lcl_DDE_RelToAbs( sTopic, sReferer ) );
619cdf0e10cSrcweir 		if( INET_PROT_NOT_VALID != eOld ||
620cdf0e10cSrcweir 			INET_PROT_HTTP != aURL.GetProtocol() )
621cdf0e10cSrcweir 		{
622cdf0e10cSrcweir 			SfxStringItem aName( SID_FILE_NAME, sTopic );
623cdf0e10cSrcweir             SfxBoolItem aMinimized(SID_MINIMIZED, sal_True);
624cdf0e10cSrcweir             SfxBoolItem aHidden(SID_HIDDEN, sal_True);
625cdf0e10cSrcweir             SfxStringItem aTarget( SID_TARGETNAME, String::CreateFromAscii("_blank") );
626cdf0e10cSrcweir 			SfxStringItem aReferer( SID_REFERER, sReferer );
627cdf0e10cSrcweir 			SfxUInt16Item aUpdate( SID_UPDATEDOCMODE, nUpdateMode );
628cdf0e10cSrcweir             SfxBoolItem aReadOnly(SID_DOC_READONLY, sal_True);
629cdf0e10cSrcweir 
630cdf0e10cSrcweir             // #i14200# (DDE-link crashes wordprocessor)
631cdf0e10cSrcweir             SfxAllItemSet aArgs( SFX_APP()->GetPool() );
632cdf0e10cSrcweir             aArgs.Put(aReferer);
633cdf0e10cSrcweir             aArgs.Put(aTarget);
634cdf0e10cSrcweir             aArgs.Put(aHidden);
635cdf0e10cSrcweir             aArgs.Put(aMinimized);
636cdf0e10cSrcweir             aArgs.Put(aName);
637cdf0e10cSrcweir 			aArgs.Put(aUpdate);
638cdf0e10cSrcweir 			aArgs.Put(aReadOnly);
639cdf0e10cSrcweir             pFndShell = SfxObjectShell::CreateAndLoadObject( aArgs );
640cdf0e10cSrcweir 		}
641cdf0e10cSrcweir 	}
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
644cdf0e10cSrcweir 	if( pFndShell )
645cdf0e10cSrcweir 	{
646cdf0e10cSrcweir 		sfx2::SvLinkSource* pNewSrc = pFndShell->DdeCreateLinkSource( sItem );
647cdf0e10cSrcweir 		if( pNewSrc )
648cdf0e10cSrcweir 		{
649cdf0e10cSrcweir 			bRet = sal_True;
650cdf0e10cSrcweir 
651cdf0e10cSrcweir 			::com::sun::star::datatransfer::DataFlavor aFl;
652cdf0e10cSrcweir 			SotExchange::GetFormatDataFlavor( pLink->GetContentType(), aFl );
653cdf0e10cSrcweir 
654cdf0e10cSrcweir 			pLink->SetObj( pNewSrc );
655cdf0e10cSrcweir 			pNewSrc->AddDataAdvise( pLink, aFl.MimeType,
656cdf0e10cSrcweir 								sfx2::LINKUPDATE_ONCALL == pLink->GetUpdateMode()
657cdf0e10cSrcweir 									? ADVISEMODE_ONLYONCE
658cdf0e10cSrcweir 									: 0 );
659cdf0e10cSrcweir 		}
660cdf0e10cSrcweir 	}
661cdf0e10cSrcweir 	return bRet;
662cdf0e10cSrcweir }
663cdf0e10cSrcweir 
664cdf0e10cSrcweir 
665cdf0e10cSrcweir }
666cdf0e10cSrcweir 
667