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 <svx/svdomedia.hxx>
31 : :
32 : : #include <rtl/oustringostreaminserter.hxx>
33 : : #include <osl/file.hxx>
34 : :
35 : : #include <com/sun/star/document/XStorageBasedDocument.hpp>
36 : : #include <com/sun/star/embed/XStorage.hpp>
37 : :
38 : : #include <ucbhelper/content.hxx>
39 : :
40 : : #include <comphelper/storagehelper.hxx>
41 : :
42 : : #include <vcl/svapp.hxx>
43 : :
44 : : #include <svx/svdmodel.hxx>
45 : : #include "svx/svdglob.hxx"
46 : : #include "svx/svdstr.hrc"
47 : : #include <svx/sdr/contact/viewcontactofsdrmediaobj.hxx>
48 : : #include <avmedia/mediawindow.hxx>
49 : :
50 : :
51 : :
52 : : using namespace ::com::sun::star;
53 : :
54 : : // ---------------
55 : : // - SdrMediaObj -
56 : : // ---------------
57 : :
58 : : // Note: the temp file is read only, until it is deleted!
59 : : // It may be shared between multiple documents in case of copy/paste,
60 : : // hence the shared_ptr.
61 : : struct MediaTempFile
62 : : {
63 : : ::rtl::OUString const m_TempFileURL;
64 : 0 : MediaTempFile(::rtl::OUString const& rURL) : m_TempFileURL(rURL) {}
65 : 0 : ~MediaTempFile()
66 : 0 : {
67 [ # # ]: 0 : ::osl::File::remove(m_TempFileURL);
68 : 0 : }
69 : : };
70 : :
71 [ # # ][ # # ]: 0 : struct SdrMediaObj::Impl
[ # # ]
72 : : {
73 : : ::avmedia::MediaItem m_MediaProperties;
74 : : ::boost::scoped_ptr<Graphic> m_pGraphic;
75 : : ::boost::shared_ptr<MediaTempFile> m_pTempFile;
76 : : };
77 : :
78 [ # # ][ # # ]: 4 : TYPEINIT1( SdrMediaObj, SdrRectObj );
79 : :
80 : : // ------------------------------------------------------------------------------
81 : :
82 : 0 : SdrMediaObj::SdrMediaObj()
83 : : : SdrRectObj()
84 [ # # ][ # # ]: 0 : , m_pImpl( new Impl() )
85 : : {
86 : 0 : }
87 : :
88 : : // ------------------------------------------------------------------------------
89 : :
90 : 0 : SdrMediaObj::SdrMediaObj( const Rectangle& rRect )
91 : : : SdrRectObj( rRect )
92 [ # # ][ # # ]: 0 : , m_pImpl( new Impl() )
93 : : {
94 : 0 : }
95 : :
96 : : // ------------------------------------------------------------------------------
97 : :
98 [ # # ]: 0 : SdrMediaObj::~SdrMediaObj()
99 : : {
100 [ # # ]: 0 : }
101 : :
102 : : // ------------------------------------------------------------------------------
103 : :
104 : 0 : bool SdrMediaObj::HasTextEdit() const
105 : : {
106 : 0 : return false;
107 : : }
108 : :
109 : : // ------------------------------------------------------------------------------
110 : :
111 : 0 : sdr::contact::ViewContact* SdrMediaObj::CreateObjectSpecificViewContact()
112 : : {
113 [ # # ]: 0 : return new ::sdr::contact::ViewContactOfSdrMediaObj( *this );
114 : : }
115 : :
116 : : // ------------------------------------------------------------------------------
117 : :
118 : 0 : void SdrMediaObj::TakeObjInfo( SdrObjTransformInfoRec& rInfo ) const
119 : : {
120 : 0 : rInfo.bSelectAllowed = true;
121 : 0 : rInfo.bMoveAllowed = true;
122 : 0 : rInfo.bResizeFreeAllowed = true;
123 : 0 : rInfo.bResizePropAllowed = true;
124 : 0 : rInfo.bRotateFreeAllowed = false;
125 : 0 : rInfo.bRotate90Allowed = false;
126 : 0 : rInfo.bMirrorFreeAllowed = false;
127 : 0 : rInfo.bMirror45Allowed = false;
128 : 0 : rInfo.bMirror90Allowed = false;
129 : 0 : rInfo.bTransparenceAllowed = false;
130 : 0 : rInfo.bGradientAllowed = false;
131 : 0 : rInfo.bShearAllowed = false;
132 : 0 : rInfo.bEdgeRadiusAllowed = false;
133 : 0 : rInfo.bNoOrthoDesired = false;
134 : 0 : rInfo.bNoContortion = false;
135 : 0 : rInfo.bCanConvToPath = false;
136 : 0 : rInfo.bCanConvToPoly = false;
137 : 0 : rInfo.bCanConvToContour = false;
138 : 0 : rInfo.bCanConvToPathLineToArea = false;
139 : 0 : rInfo.bCanConvToPolyLineToArea = false;
140 : 0 : }
141 : :
142 : : // ------------------------------------------------------------------------------
143 : :
144 : 0 : sal_uInt16 SdrMediaObj::GetObjIdentifier() const
145 : : {
146 : 0 : return sal_uInt16( OBJ_MEDIA );
147 : : }
148 : :
149 : : // ------------------------------------------------------------------------------
150 : :
151 : 0 : void SdrMediaObj::TakeObjNameSingul(XubString& rName) const
152 : : {
153 [ # # ][ # # ]: 0 : rName=ImpGetResStr(STR_ObjNameSingulMEDIA);
[ # # ]
154 : :
155 [ # # ][ # # ]: 0 : String aName( GetName() );
156 : :
157 [ # # ]: 0 : if(aName.Len())
158 : : {
159 [ # # ]: 0 : rName += sal_Unicode(' ');
160 [ # # ]: 0 : rName += sal_Unicode('\'');
161 [ # # ]: 0 : rName += aName;
162 [ # # ]: 0 : rName += sal_Unicode('\'');
163 [ # # ]: 0 : }
164 : 0 : }
165 : :
166 : : // ------------------------------------------------------------------------------
167 : :
168 : 0 : void SdrMediaObj::TakeObjNamePlural(XubString& rName) const
169 : : {
170 [ # # ]: 0 : rName=ImpGetResStr(STR_ObjNamePluralMEDIA);
171 : 0 : }
172 : :
173 : : // ------------------------------------------------------------------------------
174 : :
175 : 0 : SdrMediaObj* SdrMediaObj::Clone() const
176 : : {
177 : 0 : return CloneHelper< SdrMediaObj >();
178 : : }
179 : :
180 : 0 : SdrMediaObj& SdrMediaObj::operator=(const SdrMediaObj& rObj)
181 : : {
182 [ # # ]: 0 : if( this == &rObj )
183 : 0 : return *this;
184 : 0 : SdrRectObj::operator=( rObj );
185 : :
186 : 0 : m_pImpl->m_pTempFile = rObj.m_pImpl->m_pTempFile; // before props
187 : 0 : setMediaProperties( rObj.getMediaProperties() );
188 : 0 : setGraphic( rObj.m_pImpl->m_pGraphic.get() );
189 : 0 : return *this;
190 : : }
191 : :
192 : : // ------------------------------------------------------------------------------
193 : :
194 : 0 : void SdrMediaObj::AdjustToMaxRect( const Rectangle& rMaxRect, bool bShrinkOnly /* = false */ )
195 : : {
196 [ # # ][ # # ]: 0 : Size aSize( Application::GetDefaultDevice()->PixelToLogic( getPreferredSize(), MAP_100TH_MM ) );
[ # # ][ # # ]
[ # # ]
197 [ # # ]: 0 : Size aMaxSize( rMaxRect.GetSize() );
198 : :
199 [ # # ][ # # ]: 0 : if( aSize.Height() != 0 && aSize.Width() != 0 )
[ # # ]
200 : : {
201 : 0 : Point aPos( rMaxRect.TopLeft() );
202 : :
203 : : // if graphic is too large, fit it to the page
204 [ # # ][ # # : 0 : if ( (!bShrinkOnly ||
# # # # #
# # # ]
205 : 0 : ( aSize.Height() > aMaxSize.Height() ) ||
206 : 0 : ( aSize.Width() > aMaxSize.Width() ) )&&
207 : 0 : aSize.Height() && aMaxSize.Height() )
208 : : {
209 : 0 : float fGrfWH = (float)aSize.Width() /
210 : 0 : (float)aSize.Height();
211 : 0 : float fWinWH = (float)aMaxSize.Width() /
212 : 0 : (float)aMaxSize.Height();
213 : :
214 : : // scale graphic to page size
215 [ # # ]: 0 : if ( fGrfWH < fWinWH )
216 : : {
217 : 0 : aSize.Width() = (long)(aMaxSize.Height() * fGrfWH);
218 : 0 : aSize.Height()= aMaxSize.Height();
219 : : }
220 [ # # ]: 0 : else if ( fGrfWH > 0.F )
221 : : {
222 : 0 : aSize.Width() = aMaxSize.Width();
223 : 0 : aSize.Height()= (long)(aMaxSize.Width() / fGrfWH);
224 : : }
225 : :
226 [ # # ]: 0 : aPos = rMaxRect.Center();
227 : : }
228 : :
229 [ # # ]: 0 : if( bShrinkOnly )
230 : 0 : aPos = aRect.TopLeft();
231 : :
232 : 0 : aPos.X() -= aSize.Width() / 2;
233 : 0 : aPos.Y() -= aSize.Height() / 2;
234 [ # # ][ # # ]: 0 : SetLogicRect( Rectangle( aPos, aSize ) );
235 : : }
236 : 0 : }
237 : :
238 : : // ------------------------------------------------------------------------------
239 : :
240 : 0 : void SdrMediaObj::setURL( const ::rtl::OUString& rURL)
241 : : {
242 [ # # ]: 0 : ::avmedia::MediaItem aURLItem;
243 : :
244 [ # # ]: 0 : aURLItem.setURL( rURL, 0 );
245 [ # # ][ # # ]: 0 : setMediaProperties( aURLItem );
246 : 0 : }
247 : :
248 : : // ------------------------------------------------------------------------------
249 : :
250 : 0 : const ::rtl::OUString& SdrMediaObj::getURL() const
251 : : {
252 : 0 : return m_pImpl->m_MediaProperties.getURL();
253 : : }
254 : :
255 : : // ------------------------------------------------------------------------------
256 : :
257 : 0 : void SdrMediaObj::setMediaProperties( const ::avmedia::MediaItem& rState )
258 : : {
259 : 0 : mediaPropertiesChanged( rState );
260 : 0 : static_cast< ::sdr::contact::ViewContactOfSdrMediaObj& >( GetViewContact() ).executeMediaItem( getMediaProperties() );
261 : 0 : }
262 : :
263 : : // ------------------------------------------------------------------------------
264 : :
265 : 0 : const ::avmedia::MediaItem& SdrMediaObj::getMediaProperties() const
266 : : {
267 : 0 : return m_pImpl->m_MediaProperties;
268 : : }
269 : :
270 : : // ------------------------------------------------------------------------------
271 : :
272 : 0 : Size SdrMediaObj::getPreferredSize() const
273 : : {
274 : 0 : return static_cast< ::sdr::contact::ViewContactOfSdrMediaObj& >( GetViewContact() ).getPreferredSize();
275 : : }
276 : :
277 : : // ------------------------------------------------------------------------------
278 : :
279 : 0 : void SdrMediaObj::setGraphic( const Graphic* pGraphic )
280 : : {
281 [ # # ][ # # ]: 0 : m_pImpl->m_pGraphic.reset( pGraphic ? new Graphic( *pGraphic ) : NULL );
282 : 0 : }
283 : :
284 : : // ------------------------------------------------------------------------------
285 : 0 : uno::Reference<io::XInputStream> SdrMediaObj::GetInputStream()
286 : : {
287 [ # # ]: 0 : if (!m_pImpl->m_pTempFile)
288 : : {
289 : : SAL_WARN("svx", "this is only intended for embedded media");
290 [ # # ]: 0 : return 0;
291 : : }
292 : 0 : ucbhelper::Content tempFile(m_pImpl->m_pTempFile->m_TempFileURL,
293 [ # # ]: 0 : uno::Reference<ucb::XCommandEnvironment>());
294 [ # # ][ # # ]: 0 : return tempFile.openStream();
295 : : }
296 : :
297 : : /// copy a stream from XStorage to temp file
298 : 0 : bool lcl_HandlePackageURL(
299 : : ::rtl::OUString const & rURL,
300 : : SdrModel *const pModel,
301 : : ::rtl::OUString & o_rTempFileURL)
302 : : {
303 [ # # ]: 0 : if (!pModel)
304 : : {
305 : : SAL_WARN("svx", "no model");
306 : 0 : return false;
307 : : }
308 [ # # ]: 0 : ::comphelper::LifecycleProxy sourceProxy;
309 : 0 : uno::Reference<io::XInputStream> xInStream;
310 : : try {
311 [ # # ]: 0 : xInStream = pModel->GetDocumentStream(rURL, sourceProxy);
[ # # # ]
[ # # ]
312 : : }
313 [ # # ]: 0 : catch (container::NoSuchElementException const&)
314 : : {
315 : : SAL_INFO("svx", "not found: '" << ::rtl::OUString(rURL) << "'");
316 : 0 : return false;
317 : : }
318 [ # # ]: 0 : catch (uno::Exception const& e)
319 : : {
320 : : SAL_WARN("svx", "exception: '" << e.Message << "'");
321 : 0 : return false;
322 : : }
323 [ # # ]: 0 : if (!xInStream.is())
324 : : {
325 : : SAL_WARN("svx", "no stream?");
326 : 0 : return false;
327 : : }
328 : :
329 : 0 : ::rtl::OUString tempFileURL;
330 : : ::osl::FileBase::RC const err =
331 [ # # ]: 0 : ::osl::FileBase::createTempFile(0, 0, & tempFileURL);
332 [ # # ]: 0 : if (::osl::FileBase::E_None != err)
333 : : {
334 : : SAL_INFO("svx", "cannot create temp file");
335 : 0 : return false;
336 : : }
337 : :
338 : : try
339 : : {
340 : : ::ucbhelper::Content tempContent(tempFileURL,
341 [ # # ]: 0 : uno::Reference<ucb::XCommandEnvironment>());
342 [ # # # # ]: 0 : tempContent.writeStream(xInStream, true); // copy stream to file
[ # # ]
343 : : }
344 [ # # ]: 0 : catch (uno::Exception const& e)
345 : : {
346 : : SAL_WARN("svx", "exception: '" << e.Message << "'");
347 : 0 : return false;
348 : : }
349 : 0 : o_rTempFileURL = tempFileURL;
350 [ # # ]: 0 : return true;
351 : : }
352 : :
353 : : static char const s_PkgScheme[] = "vnd.sun.star.Package:";
354 : :
355 : 0 : void SdrMediaObj::mediaPropertiesChanged( const ::avmedia::MediaItem& rNewProperties )
356 : : {
357 : 0 : const sal_uInt32 nMaskSet = rNewProperties.getMaskSet();
358 : :
359 : : // use only a subset of MediaItem properties for own own properties
360 [ # # ]: 0 : if( ( AVMEDIA_SETMASK_URL & nMaskSet ) &&
[ # # # # ]
361 : 0 : ( rNewProperties.getURL() != getURL() ))
362 : : {
363 [ # # ]: 0 : setGraphic();
364 [ # # ]: 0 : ::rtl::OUString const url(rNewProperties.getURL());
365 [ # # ]: 0 : if ((0 == rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
366 : : url.getStr(), url.getLength(),
367 : 0 : s_PkgScheme, SAL_N_ELEMENTS(s_PkgScheme) - 1)))
368 : : {
369 [ # # # # ]: 0 : if ( !m_pImpl->m_pTempFile
[ # # ]
370 : 0 : || (m_pImpl->m_pTempFile->m_TempFileURL !=
371 [ # # ]: 0 : rNewProperties.getTempURL()))
372 : : {
373 : 0 : ::rtl::OUString tempFileURL;
374 : : bool const bSuccess = lcl_HandlePackageURL(
375 [ # # ][ # # ]: 0 : url, GetModel(), tempFileURL);
376 [ # # ]: 0 : if (bSuccess)
377 : : {
378 [ # # ][ # # ]: 0 : m_pImpl->m_pTempFile.reset(new MediaTempFile(tempFileURL));
379 [ # # ]: 0 : m_pImpl->m_MediaProperties.setURL(url, & tempFileURL);
380 : : }
381 : : else // this case is for Clone via operator=
382 : : {
383 [ # # ]: 0 : m_pImpl->m_pTempFile.reset();
384 [ # # ]: 0 : m_pImpl->m_MediaProperties.setURL(::rtl::OUString(), 0);
385 : 0 : }
386 : : }
387 : : else
388 : : {
389 : 0 : m_pImpl->m_MediaProperties.setURL(url,
390 [ # # # # ]: 0 : &rNewProperties.getTempURL());
391 : : }
392 : : }
393 : : else
394 : : {
395 [ # # ]: 0 : m_pImpl->m_pTempFile.reset();
396 [ # # ]: 0 : m_pImpl->m_MediaProperties.setURL(url, 0);
397 : 0 : }
398 : : }
399 : :
400 [ # # ]: 0 : if( AVMEDIA_SETMASK_LOOP & nMaskSet )
401 : 0 : m_pImpl->m_MediaProperties.setLoop( rNewProperties.isLoop() );
402 : :
403 [ # # ]: 0 : if( AVMEDIA_SETMASK_MUTE & nMaskSet )
404 : 0 : m_pImpl->m_MediaProperties.setMute( rNewProperties.isMute() );
405 : :
406 [ # # ]: 0 : if( AVMEDIA_SETMASK_VOLUMEDB & nMaskSet )
407 : 0 : m_pImpl->m_MediaProperties.setVolumeDB( rNewProperties.getVolumeDB() );
408 : :
409 [ # # ]: 0 : if( AVMEDIA_SETMASK_ZOOM & nMaskSet )
410 : 0 : m_pImpl->m_MediaProperties.setZoom( rNewProperties.getZoom() );
411 : 0 : }
412 : :
413 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|