Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / main / ChartFrameloader.cxx
blobc03a3f6b619eb5b5df0022d45528cd356639b442
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 "ChartFrameloader.hxx"
21 #include <servicenames.hxx>
22 #include <MediaDescriptorHelper.hxx>
23 #include <unotools/mediadescriptor.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <com/sun/star/frame/XLoadable.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <tools/diagnose_ex.h>
29 namespace chart
32 using namespace ::com::sun::star;
34 ChartFrameLoader::ChartFrameLoader(
35 uno::Reference<uno::XComponentContext> const & xContext)
36 : m_bCancelRequired( false )
38 m_xCC = xContext;
39 m_oCancelFinished.reset();
42 ChartFrameLoader::~ChartFrameLoader()
46 bool ChartFrameLoader::impl_checkCancel()
48 if(m_bCancelRequired)
50 m_oCancelFinished.set();
51 return true;
53 return false;
56 // lang::XServiceInfo
58 OUString SAL_CALL ChartFrameLoader::getImplementationName()
60 return CHART_FRAMELOADER_SERVICE_IMPLEMENTATION_NAME;
63 sal_Bool SAL_CALL ChartFrameLoader::supportsService( const OUString& rServiceName )
65 return cppu::supportsService(this, rServiceName);
68 css::uno::Sequence< OUString > SAL_CALL ChartFrameLoader::getSupportedServiceNames()
70 return { CHART_FRAMELOADER_SERVICE_NAME };
73 // frame::XFrameLoader
75 sal_Bool SAL_CALL ChartFrameLoader::load( const uno::Sequence< beans::PropertyValue >& rMediaDescriptor, const uno::Reference<frame::XFrame >& xFrame )
77 //@todo ? need to add as terminate listener to desktop?
79 uno::Reference< frame::XModel > xModel;
80 bool bHaveLoadedModel = false;
82 utl::MediaDescriptor aMediaDescriptor(rMediaDescriptor);
84 utl::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( utl::MediaDescriptor::PROP_MODEL()));
85 if( aIt != aMediaDescriptor.end())
87 xModel.set( (*aIt).second.get< uno::Reference< frame::XModel > >());
88 bHaveLoadedModel = true;
92 //create and initialize the model
93 if( ! xModel.is())
95 //@todo?? load mechanism to cancel during loading of document
96 xModel.set(
97 m_xCC->getServiceManager()->createInstanceWithContext(
98 CHART_MODEL_SERVICE_IMPLEMENTATION_NAME, m_xCC )
99 , uno::UNO_QUERY );
101 if( impl_checkCancel() )
102 return false;
105 //create the controller(+XWindow)
106 uno::Reference< frame::XController > xController;
107 uno::Reference< awt::XWindow > xComponentWindow;
109 xController.set(
110 m_xCC->getServiceManager()->createInstanceWithContext(
111 CHART_CONTROLLER_SERVICE_IMPLEMENTATION_NAME,m_xCC )
112 , uno::UNO_QUERY );
114 //!!!it is a special characteristic of the example application
115 //that the controller simultaneously provides the XWindow controller functionality
116 xComponentWindow =
117 uno::Reference< awt::XWindow >( xController, uno::UNO_QUERY );
119 if( impl_checkCancel() )
120 return false;
123 //connect frame, controller and model one to each other:
124 if(xController.is()&&xModel.is())
126 xModel->connectController(xController);
127 xModel->setCurrentController(xController);
128 xController->attachModel(xModel);
129 if(xFrame.is())
130 xFrame->setComponent(xComponentWindow,xController);
131 //creates the view and menu
132 //for correct menu creation the initialized component must be already set into the frame
133 xController->attachFrame(xFrame);
136 // call initNew() or load() at XLoadable
137 if(!bHaveLoadedModel)
140 utl::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( utl::MediaDescriptor::PROP_URL()));
141 if( aIt != aMediaDescriptor.end())
143 OUString aURL( (*aIt).second.get< OUString >());
144 if( aURL.startsWith( "private:factory/schart" ) )
146 // create new file
147 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
148 xLoadable->initNew();
150 else
152 // use the URL as BaseURL, similar to what SfxBaseModel effectively does
153 if (!aURL.isEmpty())
155 aMediaDescriptor[utl::MediaDescriptor::PROP_DOCUMENTBASEURL()] <<= aURL;
157 aMediaDescriptor.addInputStream();
158 uno::Sequence< beans::PropertyValue > aCompleteMediaDescriptor;
159 aMediaDescriptor >> aCompleteMediaDescriptor;
160 apphelper::MediaDescriptorHelper aMDHelper( aCompleteMediaDescriptor );
162 // load file
163 // @todo: replace: aMediaDescriptorHelper.getReducedForModel()
164 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
165 xLoadable->load( aCompleteMediaDescriptor );
167 //resize standalone files to get correct size:
168 if( xComponentWindow.is() && aMDHelper.ISSET_FilterName && aMDHelper.FilterName == "StarChart 5.0" )
170 awt::Rectangle aRect( xComponentWindow->getPosSize() );
171 xComponentWindow->setPosSize( aRect.X, aRect.Y, aRect.Width, aRect.Height, 0 );
176 catch( const uno::Exception & )
178 DBG_UNHANDLED_EXCEPTION("chart2");
181 return true;
184 void SAL_CALL ChartFrameLoader::cancel()
186 m_oCancelFinished.reset();
187 m_bCancelRequired = true;
188 m_oCancelFinished.wait();
189 m_bCancelRequired = false;
192 } //namespace chart
194 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
195 com_sun_star_comp_chart2_ChartFrameLoader_get_implementation(css::uno::XComponentContext *context,
196 css::uno::Sequence<css::uno::Any> const &)
198 return cppu::acquire(new chart::ChartFrameLoader(context));
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */