1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <comphelper/processfactory.hxx>
#include <osl/file.hxx>
#include <osl/thread.h>
#include <unotools/ucbstreamhelper.hxx>
#include <ucbhelper/content.hxx>
#include <unotools/datetime.hxx>
#include <svx/svdotext.hxx>
#include <svx/svdmodel.hxx>
#include <editeng/editdata.hxx>
#include <sfx2/lnkbase.hxx>
#include <sfx2/linkmgr.hxx>
#include <tools/urlobj.hxx>
#include <tools/debug.hxx>
#include <tools/tenccvt.hxx>
#include <memory>
class ImpSdrObjTextLink: public ::sfx2::SvBaseLink
{
SdrTextObj* pSdrObj;
public:
explicit ImpSdrObjTextLink( SdrTextObj* pObj1 )
: ::sfx2::SvBaseLink( ::SfxLinkUpdateMode::ONCALL, SotClipboardFormatId::SIMPLE_FILE ),
pSdrObj( pObj1 )
{}
virtual void Closed() override;
virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
const OUString& rMimeType, const css::uno::Any & rValue ) override;
};
void ImpSdrObjTextLink::Closed()
{
if (pSdrObj )
{
// set pLink of the object to NULL, because we are destroying the link instance now
ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
if (pData!=nullptr) pData->mpLink = nullptr;
pSdrObj->ReleaseTextLink();
}
SvBaseLink::Closed();
}
::sfx2::SvBaseLink::UpdateResult ImpSdrObjTextLink::DataChanged(
const OUString& /*rMimeType*/, const css::uno::Any & /*rValue */)
{
bool bForceReload = false;
SdrModel* pModel(pSdrObj ? &pSdrObj->getSdrModelFromSdrObject() : nullptr);
sfx2::LinkManager* pLinkManager(pModel ? pModel->GetLinkManager() : nullptr);
if( pLinkManager )
{
ImpSdrObjTextLinkUserData* pData=pSdrObj->GetLinkUserData();
if( pData )
{
OUString aFile;
OUString aFilter;
sfx2::LinkManager::GetDisplayNames( this, nullptr,&aFile, nullptr, &aFilter );
if( pData->maFileName != aFile ||
pData->maFilterName != aFilter )
{
pData->maFileName = aFile;
pData->maFilterName = aFilter;
pSdrObj->SetChanged();
bForceReload = true;
}
}
}
if (pSdrObj )
pSdrObj->ReloadLinkedText( bForceReload );
return SUCCESS;
}
ImpSdrObjTextLinkUserData::ImpSdrObjTextLinkUserData():
SdrObjUserData(SdrInventor::Default,SDRUSERDATA_OBJTEXTLINK),
maFileDate0( DateTime::EMPTY ),
meCharSet(RTL_TEXTENCODING_DONTKNOW)
{
}
ImpSdrObjTextLinkUserData::~ImpSdrObjTextLinkUserData()
{
}
std::unique_ptr<SdrObjUserData> ImpSdrObjTextLinkUserData::Clone(SdrObject* ) const
{
ImpSdrObjTextLinkUserData* pData = new ImpSdrObjTextLinkUserData;
pData->maFileName = maFileName;
pData->maFilterName = maFilterName;
pData->maFileDate0 = maFileDate0;
pData->meCharSet = meCharSet;
pData->mpLink = nullptr;
return std::unique_ptr<SdrObjUserData>(pData);
}
void SdrTextObj::SetTextLink(const OUString& rFileName, const OUString& rFilterName)
{
rtl_TextEncoding eCharSet = osl_getThreadTextEncoding();
ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
if (pData!=nullptr) {
ReleaseTextLink();
}
pData=new ImpSdrObjTextLinkUserData;
pData->maFileName = rFileName;
pData->maFilterName = rFilterName;
pData->meCharSet = eCharSet;
AppendUserData(std::unique_ptr<SdrObjUserData>(pData));
ImpRegisterLink();
}
void SdrTextObj::ReleaseTextLink()
{
ImpDeregisterLink();
sal_uInt16 nCount=GetUserDataCount();
for (sal_uInt16 nNum=nCount; nNum>0;) {
nNum--;
SdrObjUserData* pData=GetUserData(nNum);
if (pData->GetInventor()==SdrInventor::Default && pData->GetId()==SDRUSERDATA_OBJTEXTLINK) {
DeleteUserData(nNum);
}
}
}
bool SdrTextObj::ReloadLinkedText( bool bForceLoad)
{
ImpSdrObjTextLinkUserData* pData = GetLinkUserData();
bool bRet = true;
if( pData )
{
DateTime aFileDT( DateTime::EMPTY );
bool bExists = true;
try
{
INetURLObject aURL( pData->maFileName );
DBG_ASSERT( aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
::ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
css::uno::Any aAny( aCnt.getPropertyValue(u"DateModified"_ustr) );
css::util::DateTime aDateTime;
aAny >>= aDateTime;
::utl::typeConvert( aDateTime, aFileDT );
}
catch( ... )
{
bExists = false;
}
if( bExists )
{
bool bLoad = false;
if( bForceLoad )
bLoad = true;
else
bLoad = ( aFileDT > pData->maFileDate0 );
if( bLoad )
{
bRet = LoadText( pData->maFileName, pData->meCharSet );
}
pData->maFileDate0 = aFileDT;
}
}
return bRet;
}
bool SdrTextObj::LoadText(const OUString& rFileName, rtl_TextEncoding eCharSet)
{
INetURLObject aFileURL( rFileName );
bool bRet = false;
if( aFileURL.GetProtocol() == INetProtocol::NotValid )
{
OUString aFileURLStr;
if( osl::FileBase::getFileURLFromSystemPath( rFileName, aFileURLStr ) == osl::FileBase::E_None )
aFileURL = INetURLObject( aFileURLStr );
else
aFileURL.SetSmartURL( rFileName );
}
DBG_ASSERT( aFileURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( aFileURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
if( pIStm )
{
pIStm->SetStreamCharSet(GetSOLoadTextEncoding(eCharSet));
char cRTF[5];
cRTF[4] = 0;
pIStm->ReadBytes(cRTF, 5);
bool bRTF = cRTF[0] == '{' && cRTF[1] == '\\' && cRTF[2] == 'r' && cRTF[3] == 't' && cRTF[4] == 'f';
pIStm->Seek(0);
if( !pIStm->GetError() )
{
SetText( *pIStm, aFileURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), bRTF ? EETextFormat::Rtf : EETextFormat::Text );
bRet = true;
}
}
return bRet;
}
ImpSdrObjTextLinkUserData* SdrTextObj::GetLinkUserData() const
{
sal_uInt16 nCount=GetUserDataCount();
for (sal_uInt16 nNum=nCount; nNum>0;) {
nNum--;
SdrObjUserData * pData=GetUserData(nNum);
if (pData->GetInventor() == SdrInventor::Default
&& pData->GetId() == SDRUSERDATA_OBJTEXTLINK)
{
return static_cast<ImpSdrObjTextLinkUserData *>(pData);
}
}
return nullptr;
}
void SdrTextObj::ImpRegisterLink()
{
ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
sfx2::LinkManager* pLinkManager(getSdrModelFromSdrObject().GetLinkManager());
if (pLinkManager!=nullptr && pData!=nullptr && pData->mpLink==nullptr) { // don't register twice
pData->mpLink = new ImpSdrObjTextLink(this);
pLinkManager->InsertFileLink(*pData->mpLink,sfx2::SvBaseLinkObjectType::ClientFile,pData->maFileName,
!pData->maFilterName.isEmpty() ?
&pData->maFilterName : nullptr);
}
}
void SdrTextObj::ImpDeregisterLink()
{
ImpSdrObjTextLinkUserData* pData=GetLinkUserData();
if (!pData)
return;
sfx2::LinkManager* pLinkManager(getSdrModelFromSdrObject().GetLinkManager());
if (pLinkManager!=nullptr && pData->mpLink!=nullptr) { // don't register twice
// when doing Remove, *pLink is deleted implicitly
pLinkManager->Remove( pData->mpLink.get() );
pData->mpLink=nullptr;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|