Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / svdraw / svdotxln.cxx
blobb4e386f83e56b7f53f3a5f8fff28c7317c4a4cfd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include <comphelper/processfactory.hxx>
23 #include <osl/thread.h>
24 #include <unotools/ucbstreamhelper.hxx>
25 #include <unotools/localfilehelper.hxx>
26 #include <ucbhelper/content.hxx>
27 #include <unotools/datetime.hxx>
28 #include <svx/svdotext.hxx>
29 #include <svx/svdmodel.hxx>
30 #include <editeng/editdata.hxx>
31 #include <sfx2/lnkbase.hxx>
32 #include <sfx2/linkmgr.hxx>
33 #include <tools/urlobj.hxx>
34 #include <svl/urihelper.hxx>
35 #include <tools/tenccvt.hxx>
38 class ImpSdrObjTextLink: public ::sfx2::SvBaseLink
40 SdrTextObj* pSdrObj;
42 public:
43 ImpSdrObjTextLink( SdrTextObj* pObj1 )
44 : ::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL, FORMAT_FILE ),
45 pSdrObj( pObj1 )
47 virtual ~ImpSdrObjTextLink();
49 virtual void Closed() SAL_OVERRIDE;
50 virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
51 const OUString& rMimeType, const ::com::sun::star::uno::Any & rValue ) SAL_OVERRIDE;
54 ImpSdrObjTextLink::~ImpSdrObjTextLink()
58 void ImpSdrObjTextLink::Closed()
60 if (pSdrObj )
62 // set pLink of the object to NULL, because we are destroying the link instance now
63 ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
64 if (pData!=NULL) pData->pLink=NULL;
65 pSdrObj->ReleaseTextLink();
67 SvBaseLink::Closed();
71 ::sfx2::SvBaseLink::UpdateResult ImpSdrObjTextLink::DataChanged(
72 const OUString& /*rMimeType*/, const ::com::sun::star::uno::Any & /*rValue */)
74 bool bForceReload = false;
75 SdrModel* pModel = pSdrObj ? pSdrObj->GetModel() : 0;
76 sfx2::LinkManager* pLinkManager= pModel ? pModel->GetLinkManager() : 0;
77 if( pLinkManager )
79 ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
80 if( pData )
82 OUString aFile;
83 OUString aFilter;
84 pLinkManager->GetDisplayNames( this, 0,&aFile, 0, &aFilter );
86 if( pData->aFileName != aFile ||
87 pData->aFilterName != aFilter )
89 pData->aFileName = aFile;
90 pData->aFilterName = aFilter;
91 pSdrObj->SetChanged();
92 bForceReload = true;
96 if (pSdrObj )
97 pSdrObj->ReloadLinkedText( bForceReload );
99 return SUCCESS;
103 TYPEINIT1(ImpSdrObjTextLinkUserData,SdrObjUserData);
105 ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData(SdrTextObj* pObj1):
106 SdrObjUserData(SdrInventor,SDRUSERDATA_OBJTEXTLINK,0),
107 pObj(pObj1),
108 aFileDate0( DateTime::EMPTY ),
109 pLink(NULL),
110 eCharSet(RTL_TEXTENCODING_DONTKNOW)
114 ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
116 delete pLink;
119 SdrObjUserData* ImpSdrObjTextLinkUserData::Clone(SdrObject* pObj1) const
121 ImpSdrObjTextLinkUserData* pData=new ImpSdrObjTextLinkUserData((SdrTextObj*)pObj1);
122 pData->aFileName =aFileName;
123 pData->aFilterName=aFilterName;
124 pData->aFileDate0 =aFileDate0;
125 pData->eCharSet =eCharSet;
126 pData->pLink=NULL;
127 return pData;
131 void SdrTextObj::SetTextLink(const OUString& rFileName, const OUString& rFilterName, rtl_TextEncoding eCharSet)
133 if(eCharSet == RTL_TEXTENCODING_DONTKNOW)
134 eCharSet = osl_getThreadTextEncoding();
136 ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
137 if (pData!=NULL) {
138 ReleaseTextLink();
140 pData=new ImpSdrObjTextLinkUserData(this);
141 pData->aFileName=rFileName;
142 pData->aFilterName=rFilterName;
143 pData->eCharSet=eCharSet;
144 AppendUserData(pData);
145 ImpLinkAnmeldung();
148 void SdrTextObj::ReleaseTextLink()
150 ImpLinkAbmeldung();
151 sal_uInt16 nAnz=GetUserDataCount();
152 for (sal_uInt16 nNum=nAnz; nNum>0;) {
153 nNum--;
154 SdrObjUserData* pData=GetUserData(nNum);
155 if (pData->GetInventor()==SdrInventor && pData->GetId()==SDRUSERDATA_OBJTEXTLINK) {
156 DeleteUserData(nNum);
161 bool SdrTextObj::ReloadLinkedText( bool bForceLoad)
163 ImpSdrObjTextLinkUserData* pData = GetLinkUserData();
164 bool bRet = true;
166 if( pData )
168 DateTime aFileDT( DateTime::EMPTY );
169 bool bExists = true;
173 INetURLObject aURL( pData->aFileName );
174 DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
176 ::ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
177 ::com::sun::star::uno::Any aAny( aCnt.getPropertyValue("DateModified") );
178 ::com::sun::star::util::DateTime aDateTime;
180 aAny >>= aDateTime;
181 ::utl::typeConvert( aDateTime, aFileDT );
183 catch( ... )
185 bExists = false;
188 if( bExists )
190 bool bLoad = false;
191 if( bForceLoad )
192 bLoad = true;
193 else
194 bLoad = ( aFileDT > pData->aFileDate0 );
196 if( bLoad )
198 bRet = LoadText( pData->aFileName, pData->aFilterName, pData->eCharSet );
201 pData->aFileDate0 = aFileDT;
205 return bRet;
208 bool SdrTextObj::LoadText(const OUString& rFileName, const OUString& /*rFilterName*/, rtl_TextEncoding eCharSet)
210 INetURLObject aFileURL( rFileName );
211 bool bRet = false;
213 if( aFileURL.GetProtocol() == INET_PROT_NOT_VALID )
215 OUString aFileURLStr;
217 if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFileName, aFileURLStr ) )
218 aFileURL = INetURLObject( aFileURLStr );
219 else
220 aFileURL.SetSmartURL( rFileName );
223 DBG_ASSERT( aFileURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
225 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
227 if( pIStm )
229 pIStm->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet));
231 char cRTF[5];
232 cRTF[4] = 0;
233 pIStm->Read(cRTF, 5);
235 bool bRTF = cRTF[0] == '{' && cRTF[1] == '\\' && cRTF[2] == 'r' && cRTF[3] == 't' && cRTF[4] == 'f';
237 pIStm->Seek(0);
239 if( !pIStm->GetError() )
241 SetText( *pIStm, aFileURL.GetMainURL( INetURLObject::NO_DECODE ), sal::static_int_cast< sal_uInt16 >( bRTF ? EE_FORMAT_RTF : EE_FORMAT_TEXT ) );
242 bRet = true;
245 delete pIStm;
248 return bRet;
251 ImpSdrObjTextLinkUserData* SdrTextObj::GetLinkUserData() const
253 ImpSdrObjTextLinkUserData* pData=NULL;
254 sal_uInt16 nAnz=GetUserDataCount();
255 for (sal_uInt16 nNum=nAnz; nNum>0 && pData==NULL;) {
256 nNum--;
257 pData=(ImpSdrObjTextLinkUserData*)GetUserData(nNum);
258 if (pData->GetInventor()!=SdrInventor || pData->GetId()!=SDRUSERDATA_OBJTEXTLINK) {
259 pData=NULL;
262 return pData;
265 void SdrTextObj::ImpLinkAnmeldung()
267 ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
268 sfx2::LinkManager* pLinkManager=pModel!=NULL ? pModel->GetLinkManager() : NULL;
269 if (pLinkManager!=NULL && pData!=NULL && pData->pLink==NULL) { // don't register twice
270 pData->pLink = new ImpSdrObjTextLink(this);
271 pLinkManager->InsertFileLink(*pData->pLink,OBJECT_CLIENT_FILE,pData->aFileName,
272 !pData->aFilterName.isEmpty() ?
273 &pData->aFilterName : NULL,
274 NULL);
278 void SdrTextObj::ImpLinkAbmeldung()
280 ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
281 sfx2::LinkManager* pLinkManager=pModel!=NULL ? pModel->GetLinkManager() : NULL;
282 if (pLinkManager!=NULL && pData!=NULL && pData->pLink!=NULL) { // don't register twice
283 // when doing Remove, *pLink is deleted implicitly
284 pLinkManager->Remove( pData->pLink );
285 pData->pLink=NULL;
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */