1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <documentlinkmgr.hxx>
21 #include <datastream.hxx>
22 #include <ddelink.hxx>
24 #include <scresid.hxx>
26 #include <sfx2/linkmgr.hxx>
27 #include <vcl/layout.hxx>
29 #include <boost/noncopyable.hpp>
30 #include <boost/scoped_ptr.hpp>
34 struct DocumentLinkManagerImpl
: boost::noncopyable
37 SfxObjectShell
* mpShell
;
38 boost::scoped_ptr
<DataStream
> mpDataStream
;
39 boost::scoped_ptr
<sfx2::LinkManager
> mpLinkManager
;
41 DocumentLinkManagerImpl( ScDocument
& rDoc
, SfxObjectShell
* pShell
) :
42 mrDoc(rDoc
), mpShell(pShell
), mpDataStream(NULL
), mpLinkManager(NULL
) {}
44 ~DocumentLinkManagerImpl()
49 sfx2::SvLinkSources aTemp
= mpLinkManager
->GetServers();
50 for (sfx2::SvLinkSources::const_iterator it
= aTemp
.begin(); it
!= aTemp
.end(); ++it
)
53 if (!mpLinkManager
->GetLinks().empty())
54 mpLinkManager
->Remove(0, mpLinkManager
->GetLinks().size());
59 DocumentLinkManager::DocumentLinkManager( ScDocument
& rDoc
, SfxObjectShell
* pShell
) :
60 mpImpl(new DocumentLinkManagerImpl(rDoc
, pShell
)) {}
62 DocumentLinkManager::~DocumentLinkManager()
67 void DocumentLinkManager::setDataStream( DataStream
* p
)
69 mpImpl
->mpDataStream
.reset(p
);
72 DataStream
* DocumentLinkManager::getDataStream()
74 return mpImpl
->mpDataStream
.get();
77 const DataStream
* DocumentLinkManager::getDataStream() const
79 return mpImpl
->mpDataStream
.get();
82 sfx2::LinkManager
* DocumentLinkManager::getLinkManager( bool bCreate
)
84 if (!mpImpl
->mpLinkManager
&& bCreate
&& mpImpl
->mpShell
)
85 mpImpl
->mpLinkManager
.reset(new sfx2::LinkManager(mpImpl
->mpShell
));
86 return mpImpl
->mpLinkManager
.get();
89 const sfx2::LinkManager
* DocumentLinkManager::getExistingLinkManager() const
91 return mpImpl
->mpLinkManager
.get();
94 bool DocumentLinkManager::idleCheckLinks()
96 if (!mpImpl
->mpLinkManager
)
99 bool bAnyLeft
= false;
100 const sfx2::SvBaseLinks
& rLinks
= mpImpl
->mpLinkManager
->GetLinks();
101 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
103 sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
104 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
105 if (!pDdeLink
|| !pDdeLink
->NeedsUpdate())
108 pDdeLink
->TryUpdate();
109 if (pDdeLink
->NeedsUpdate()) // Was not successful?
116 bool DocumentLinkManager::hasDdeLinks() const
118 if (!mpImpl
->mpLinkManager
)
121 const sfx2::SvBaseLinks
& rLinks
= mpImpl
->mpLinkManager
->GetLinks();
122 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
124 sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
125 if (dynamic_cast<ScDdeLink
*>(pBase
))
132 bool DocumentLinkManager::updateDdeLinks( vcl::Window
* pWin
)
134 if (!mpImpl
->mpLinkManager
)
137 sfx2::LinkManager
* pMgr
= mpImpl
->mpLinkManager
.get();
138 const sfx2::SvBaseLinks
& rLinks
= pMgr
->GetLinks();
140 // If the update takes longer, reset all values so that nothing
141 // old (wrong) is left behind
143 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
145 sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
146 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
150 if (pDdeLink
->Update())
154 // Update failed. Notify the user.
155 OUString aFile
= pDdeLink
->GetTopic();
156 OUString aElem
= pDdeLink
->GetItem();
157 OUString aType
= pDdeLink
->GetAppl();
160 aBuf
.append(OUString(ScResId(SCSTR_DDEDOC_NOT_LOADED
)));
161 aBuf
.appendAscii("\n\n");
162 aBuf
.appendAscii("Source : ");
164 aBuf
.appendAscii("\nElement : ");
166 aBuf
.appendAscii("\nType : ");
168 ScopedVclPtrInstance
< MessageDialog
> aBox(pWin
, aBuf
.makeStringAndClear());
173 pMgr
->CloseCachedComps();
178 bool DocumentLinkManager::updateDdeLink( const OUString
& rAppl
, const OUString
& rTopic
, const OUString
& rItem
)
180 if (!mpImpl
->mpLinkManager
)
183 sfx2::LinkManager
* pMgr
= mpImpl
->mpLinkManager
.get();
184 const sfx2::SvBaseLinks
& rLinks
= pMgr
->GetLinks();
187 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
189 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
190 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
194 if ( OUString(pDdeLink
->GetAppl()) == rAppl
&&
195 OUString(pDdeLink
->GetTopic()) == rTopic
&&
196 OUString(pDdeLink
->GetItem()) == rItem
)
198 pDdeLink
->TryUpdate();
199 bFound
= true; // Could be multiple (Mode), so continue searching
206 size_t DocumentLinkManager::getDdeLinkCount() const
208 if (!mpImpl
->mpLinkManager
)
211 size_t nDdeCount
= 0;
212 const sfx2::SvBaseLinks
& rLinks
= mpImpl
->mpLinkManager
->GetLinks();
213 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
215 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
216 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
226 void DocumentLinkManager::disconnectDdeLinks()
228 if (!mpImpl
->mpLinkManager
)
231 const sfx2::SvBaseLinks
& rLinks
= mpImpl
->mpLinkManager
->GetLinks();
232 for (size_t i
= 0, n
= rLinks
.size(); i
< n
; ++i
)
234 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
235 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
237 pDdeLink
->Disconnect();
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */