Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <unotools/ucbstreamhelper.hxx>
31 : : #include <unotools/localfilehelper.hxx>
32 : : #include <ucbhelper/content.hxx>
33 : : #include <ucbhelper/contentbroker.hxx>
34 : : #include <unotools/datetime.hxx>
35 : : #include <svx/svdotext.hxx>
36 : : #include "svx/svditext.hxx"
37 : : #include <svx/svdmodel.hxx>
38 : : #include <editeng/editdata.hxx>
39 : : #include <sfx2/lnkbase.hxx>
40 : : #include <sfx2/linkmgr.hxx>
41 : : #include <tools/urlobj.hxx>
42 : : #include <svl/urihelper.hxx>
43 : : #include <tools/tenccvt.hxx>
44 : :
45 : :
46 : : class ImpSdrObjTextLink: public ::sfx2::SvBaseLink
47 : : {
48 : : SdrTextObj* pSdrObj;
49 : :
50 : : public:
51 : 0 : ImpSdrObjTextLink( SdrTextObj* pObj1 )
52 : : : ::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL, FORMAT_FILE ),
53 : 0 : pSdrObj( pObj1 )
54 : 0 : {}
55 : : virtual ~ImpSdrObjTextLink();
56 : :
57 : : virtual void Closed();
58 : : virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
59 : : const String& rMimeType, const ::com::sun::star::uno::Any & rValue );
60 : :
61 : 0 : sal_Bool Connect() { return 0 != SvBaseLink::GetRealObject(); }
62 : : };
63 : :
64 : 0 : ImpSdrObjTextLink::~ImpSdrObjTextLink()
65 : : {
66 [ # # ]: 0 : }
67 : :
68 : 0 : void ImpSdrObjTextLink::Closed()
69 : : {
70 [ # # ]: 0 : if (pSdrObj )
71 : : {
72 : : // set pLink of the object to NULL, because we are destroying the link instance now
73 : 0 : ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
74 [ # # ]: 0 : if (pData!=NULL) pData->pLink=NULL;
75 : 0 : pSdrObj->ReleaseTextLink();
76 : : }
77 : 0 : SvBaseLink::Closed();
78 : 0 : }
79 : :
80 : :
81 : 0 : ::sfx2::SvBaseLink::UpdateResult ImpSdrObjTextLink::DataChanged(
82 : : const String& /*rMimeType*/, const ::com::sun::star::uno::Any & /*rValue */)
83 : : {
84 : 0 : bool bForceReload = false;
85 [ # # ]: 0 : SdrModel* pModel = pSdrObj ? pSdrObj->GetModel() : 0;
86 [ # # ]: 0 : sfx2::LinkManager* pLinkManager= pModel ? pModel->GetLinkManager() : 0;
87 [ # # ]: 0 : if( pLinkManager )
88 : : {
89 : 0 : ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
90 [ # # ]: 0 : if( pData )
91 : : {
92 [ # # ]: 0 : String aFile;
93 [ # # ]: 0 : String aFilter;
94 [ # # ]: 0 : pLinkManager->GetDisplayNames( this, 0,&aFile, 0, &aFilter );
95 : :
96 [ # # ][ # # ]: 0 : if( !pData->aFileName.Equals( aFile ) ||
[ # # ][ # # ]
97 [ # # ]: 0 : !pData->aFilterName.Equals( aFilter ))
98 : : {
99 [ # # ]: 0 : pData->aFileName = aFile;
100 [ # # ]: 0 : pData->aFilterName = aFilter;
101 [ # # ]: 0 : pSdrObj->SetChanged();
102 : 0 : bForceReload = true;
103 [ # # ][ # # ]: 0 : }
104 : : }
105 : : }
106 [ # # ]: 0 : if (pSdrObj )
107 : 0 : pSdrObj->ReloadLinkedText( bForceReload );
108 : :
109 : 0 : return SUCCESS;
110 : : }
111 : :
112 : :
113 [ # # ][ # # ]: 0 : TYPEINIT1(ImpSdrObjTextLinkUserData,SdrObjUserData);
114 : :
115 : 0 : ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData(SdrTextObj* pObj1):
116 : : SdrObjUserData(SdrInventor,SDRUSERDATA_OBJTEXTLINK,0),
117 : : pObj(pObj1),
118 : : aFileDate0( DateTime::EMPTY ),
119 : : pLink(NULL),
120 [ # # ][ # # ]: 0 : eCharSet(RTL_TEXTENCODING_DONTKNOW)
121 : : {
122 : 0 : }
123 : :
124 [ # # ][ # # ]: 0 : ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
125 : : {
126 [ # # ][ # # ]: 0 : delete pLink;
127 [ # # ]: 0 : }
128 : :
129 : 0 : SdrObjUserData* ImpSdrObjTextLinkUserData::Clone(SdrObject* pObj1) const
130 : : {
131 [ # # ]: 0 : ImpSdrObjTextLinkUserData* pData=new ImpSdrObjTextLinkUserData((SdrTextObj*)pObj1);
132 : 0 : pData->aFileName =aFileName;
133 : 0 : pData->aFilterName=aFilterName;
134 : 0 : pData->aFileDate0 =aFileDate0;
135 : 0 : pData->eCharSet =eCharSet;
136 : 0 : pData->pLink=NULL;
137 : 0 : return pData;
138 : : }
139 : :
140 : :
141 : 0 : void SdrTextObj::SetTextLink(const String& rFileName, const String& rFilterName, rtl_TextEncoding eCharSet)
142 : : {
143 [ # # ]: 0 : if(eCharSet == RTL_TEXTENCODING_DONTKNOW)
144 : 0 : eCharSet = osl_getThreadTextEncoding();
145 : :
146 : 0 : ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
147 [ # # ]: 0 : if (pData!=NULL) {
148 : 0 : ReleaseTextLink();
149 : : }
150 [ # # ]: 0 : pData=new ImpSdrObjTextLinkUserData(this);
151 : 0 : pData->aFileName=rFileName;
152 : 0 : pData->aFilterName=rFilterName;
153 : 0 : pData->eCharSet=eCharSet;
154 : 0 : AppendUserData(pData);
155 : 0 : ImpLinkAnmeldung();
156 : 0 : }
157 : :
158 : 0 : void SdrTextObj::ReleaseTextLink()
159 : : {
160 : 0 : ImpLinkAbmeldung();
161 : 0 : sal_uInt16 nAnz=GetUserDataCount();
162 [ # # ]: 0 : for (sal_uInt16 nNum=nAnz; nNum>0;) {
163 : 0 : nNum--;
164 : 0 : SdrObjUserData* pData=GetUserData(nNum);
165 [ # # ][ # # ]: 0 : if (pData->GetInventor()==SdrInventor && pData->GetId()==SDRUSERDATA_OBJTEXTLINK) {
[ # # ]
166 : 0 : DeleteUserData(nNum);
167 : : }
168 : : }
169 : 0 : }
170 : :
171 : 0 : bool SdrTextObj::ReloadLinkedText( bool bForceLoad)
172 : : {
173 : 0 : ImpSdrObjTextLinkUserData* pData = GetLinkUserData();
174 : 0 : bool bRet = true;
175 : :
176 [ # # ]: 0 : if( pData )
177 : : {
178 [ # # ]: 0 : ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
179 : 0 : DateTime aFileDT( DateTime::EMPTY );
180 : 0 : sal_Bool bExists = sal_False, bLoad = sal_False;
181 : :
182 [ # # ]: 0 : if( pBroker )
183 : : {
184 : 0 : bExists = sal_True;
185 : :
186 : : try
187 : : {
188 [ # # ][ # # ]: 0 : INetURLObject aURL( pData->aFileName );
189 : : DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
190 : :
191 [ # # ][ # # ]: 0 : ::ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >() );
192 [ # # ][ # # ]: 0 : ::com::sun::star::uno::Any aAny( aCnt.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DateModified" ) ) ) );
193 : 0 : ::com::sun::star::util::DateTime aDateTime;
194 : :
195 [ # # ]: 0 : aAny >>= aDateTime;
196 [ # # ][ # # ]: 0 : ::utl::typeConvert( aDateTime, aFileDT );
[ # # ]
197 : : }
198 [ # # ]: 0 : catch( ... )
199 : : {
200 : 0 : bExists = sal_False;
201 : : }
202 : : }
203 : :
204 [ # # ]: 0 : if( bExists )
205 : : {
206 [ # # ]: 0 : if( bForceLoad )
207 : 0 : bLoad = sal_True;
208 : : else
209 [ # # ]: 0 : bLoad = ( aFileDT > pData->aFileDate0 );
210 : :
211 [ # # ]: 0 : if( bLoad )
212 : : {
213 [ # # ]: 0 : bRet = LoadText( pData->aFileName, pData->aFilterName, pData->eCharSet );
214 : : }
215 : :
216 [ # # ]: 0 : pData->aFileDate0 = aFileDT;
217 : : }
218 : : }
219 : :
220 : 0 : return bRet;
221 : : }
222 : :
223 : 0 : bool SdrTextObj::LoadText(const String& rFileName, const String& /*rFilterName*/, rtl_TextEncoding eCharSet)
224 : : {
225 [ # # ][ # # ]: 0 : INetURLObject aFileURL( rFileName );
226 : 0 : sal_Bool bRet = sal_False;
227 : :
228 [ # # ]: 0 : if( aFileURL.GetProtocol() == INET_PROT_NOT_VALID )
229 : : {
230 : 0 : rtl::OUString aFileURLStr;
231 : :
232 [ # # ][ # # ]: 0 : if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFileName, aFileURLStr ) )
[ # # ]
233 [ # # ][ # # ]: 0 : aFileURL = INetURLObject( aFileURLStr );
[ # # ]
234 : : else
235 [ # # ][ # # ]: 0 : aFileURL.SetSmartURL( rFileName );
236 : : }
237 : :
238 : : DBG_ASSERT( aFileURL.GetProtocol() != INET_PROT_NOT_VALID, "invalid URL" );
239 : :
240 [ # # ][ # # ]: 0 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
[ # # ][ # # ]
241 : :
242 [ # # ]: 0 : if( pIStm )
243 : : {
244 [ # # ]: 0 : pIStm->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet));
245 : :
246 : : char cRTF[5];
247 : 0 : cRTF[4] = 0;
248 [ # # ]: 0 : pIStm->Read(cRTF, 5);
249 : :
250 [ # # ][ # # ]: 0 : sal_Bool bRTF = cRTF[0] == '{' && cRTF[1] == '\\' && cRTF[2] == 'r' && cRTF[3] == 't' && cRTF[4] == 'f';
[ # # ][ # # ]
[ # # ]
251 : :
252 [ # # ]: 0 : pIStm->Seek(0);
253 : :
254 [ # # ]: 0 : if( !pIStm->GetError() )
255 : : {
256 [ # # ][ # # ]: 0 : SetText( *pIStm, aFileURL.GetMainURL( INetURLObject::NO_DECODE ), sal::static_int_cast< sal_uInt16 >( bRTF ? EE_FORMAT_RTF : EE_FORMAT_TEXT ) );
[ # # ][ # # ]
[ # # ]
257 : 0 : bRet = sal_True;
258 : : }
259 : :
260 [ # # ][ # # ]: 0 : delete pIStm;
261 : : }
262 : :
263 [ # # ]: 0 : return bRet;
264 : : }
265 : :
266 : 77238 : ImpSdrObjTextLinkUserData* SdrTextObj::GetLinkUserData() const
267 : : {
268 : 77238 : ImpSdrObjTextLinkUserData* pData=NULL;
269 : 77238 : sal_uInt16 nAnz=GetUserDataCount();
270 [ + + ][ + - ]: 79333 : for (sal_uInt16 nNum=nAnz; nNum>0 && pData==NULL;) {
[ + + ]
271 : 2095 : nNum--;
272 : 2095 : pData=(ImpSdrObjTextLinkUserData*)GetUserData(nNum);
273 [ # # ][ + - ]: 2095 : if (pData->GetInventor()!=SdrInventor || pData->GetId()!=SDRUSERDATA_OBJTEXTLINK) {
[ - + ]
274 : 2095 : pData=NULL;
275 : : }
276 : : }
277 : 77238 : return pData;
278 : : }
279 : :
280 : 0 : void SdrTextObj::ImpLinkAnmeldung()
281 : : {
282 : 0 : ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
283 [ # # ]: 0 : sfx2::LinkManager* pLinkManager=pModel!=NULL ? pModel->GetLinkManager() : NULL;
284 [ # # ][ # # ]: 0 : if (pLinkManager!=NULL && pData!=NULL && pData->pLink==NULL) { // don't register twice
[ # # ]
285 [ # # ]: 0 : pData->pLink=new ImpSdrObjTextLink(this);
286 : : #ifdef GCC
287 : : pLinkManager->InsertFileLink(*pData->pLink,OBJECT_CLIENT_FILE,pData->aFileName,
288 : 0 : pData->aFilterName.Len() ?
289 : : &pData->aFilterName : (const String *)NULL,
290 [ # # ]: 0 : (const String *)NULL);
291 : : #else
292 : : pLinkManager->InsertFileLink(*pData->pLink,OBJECT_CLIENT_FILE,pData->aFileName,
293 : : pData->aFilterName.Len() ? &pData->aFilterName : NULL,NULL);
294 : : #endif
295 : 0 : pData->pLink->Connect();
296 : : }
297 : 0 : }
298 : :
299 : 57567 : void SdrTextObj::ImpLinkAbmeldung()
300 : : {
301 : 57567 : ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
302 [ + + ]: 57567 : sfx2::LinkManager* pLinkManager=pModel!=NULL ? pModel->GetLinkManager() : NULL;
303 [ + + ][ - + ]: 57567 : if (pLinkManager!=NULL && pData!=NULL && pData->pLink!=NULL) { // don't register twice
[ # # ]
304 : : // when doing Remove, *pLink is deleted implicitly
305 : 0 : pLinkManager->Remove( pData->pLink );
306 : 0 : pData->pLink=NULL;
307 : : }
308 : 57567 : }
309 : :
310 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|