1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "ChartFrameloader.hxx"
27 #include "servicenames.hxx"
28 #include "MediaDescriptorHelper.hxx"
29 #include "macros.hxx"
30 #include <comphelper/mediadescriptor.hxx>
31 #include <com/sun/star/document/XImporter.hpp>
32 #include <com/sun/star/document/XFilter.hpp>
33 #include <com/sun/star/frame/XLoadable.hpp>
34 
35 //.............................................................................
36 namespace chart
37 {
38 //.............................................................................
39 
40 using namespace ::com::sun::star;
41 
ChartFrameLoader(uno::Reference<uno::XComponentContext> const & xContext)42 ChartFrameLoader::ChartFrameLoader(
43 		uno::Reference<uno::XComponentContext> const & xContext)
44 		: m_bCancelRequired( sal_False )
45 {
46 	m_xCC = xContext;
47 	m_oCancelFinished.reset();
48 }
49 
~ChartFrameLoader()50 ChartFrameLoader::~ChartFrameLoader()
51 {
52 }
53 
54 	sal_Bool ChartFrameLoader
impl_checkCancel()55 ::impl_checkCancel()
56 {
57 	if(m_bCancelRequired)
58 	{
59 		m_oCancelFinished.set();
60 		return sal_True;
61 	}
62 	return sal_False;
63 }
64 
65 //-----------------------------------------------------------------
66 // lang::XServiceInfo
67 //-----------------------------------------------------------------
68 
APPHELPER_XSERVICEINFO_IMPL(ChartFrameLoader,CHART_FRAMELOADER_SERVICE_IMPLEMENTATION_NAME)69 APPHELPER_XSERVICEINFO_IMPL(ChartFrameLoader,CHART_FRAMELOADER_SERVICE_IMPLEMENTATION_NAME)
70 
71 	uno::Sequence< rtl::OUString > ChartFrameLoader
72 ::getSupportedServiceNames_Static()
73 {
74 	uno::Sequence< rtl::OUString > aSNS( 1 );
75 	aSNS.getArray()[ 0 ] = CHART_FRAMELOADER_SERVICE_NAME;
76 	return aSNS;
77 }
78 
79 //-----------------------------------------------------------------
80 // frame::XFrameLoader
81 //-----------------------------------------------------------------
82 
83 	sal_Bool SAL_CALL ChartFrameLoader
load(const uno::Sequence<beans::PropertyValue> & rMediaDescriptor,const uno::Reference<frame::XFrame> & xFrame)84 ::load( const uno::Sequence< beans::PropertyValue >& rMediaDescriptor
85 		, const uno::Reference<frame::XFrame >& xFrame )
86 		throw (uno::RuntimeException)
87 {
88 	//@todo ? need to add as terminate listener to desktop?
89 
90 	uno::Reference< frame::XModel >			xModel;
91     bool bHaveLoadedModel = false;
92 
93 	comphelper::MediaDescriptor aMediaDescriptor(rMediaDescriptor);
94     {
95         comphelper::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( aMediaDescriptor.PROP_MODEL()));
96         if( aIt != aMediaDescriptor.end())
97         {
98             xModel.set( (*aIt).second.get< uno::Reference< frame::XModel > >());
99             bHaveLoadedModel = true;
100         }
101     }
102 
103 	//create and initialize the model
104     if( ! xModel.is())
105 	{
106 		//@todo?? load mechanism to cancel during loading of document
107 		xModel.set(
108 				m_xCC->getServiceManager()->createInstanceWithContext(
109 				CHART_MODEL_SERVICE_IMPLEMENTATION_NAME, m_xCC )
110 				, uno::UNO_QUERY );
111 
112 		if( impl_checkCancel() )
113 			return sal_False;
114     }
115 
116 	//create the controller(+XWindow)
117 	uno::Reference< frame::XController >	xController = NULL;
118 	uno::Reference< awt::XWindow >			xComponentWindow = NULL;
119 	{
120 		xController = uno::Reference< frame::XController >(
121 			m_xCC->getServiceManager()->createInstanceWithContext(
122 			CHART_CONTROLLER_SERVICE_IMPLEMENTATION_NAME,m_xCC )
123 			, uno::UNO_QUERY );
124 
125 		//!!!it is a special characteristic of the example application
126 		//that the controller simultaniously provides the XWindow controller functionality
127 		xComponentWindow =
128 					  uno::Reference< awt::XWindow >( xController, uno::UNO_QUERY );
129 
130 		if( impl_checkCancel() )
131 			return sal_False;
132 	}
133 
134 
135 	//connect frame, controller and model one to each other:
136 	if(xController.is()&&xModel.is())
137 	{
138         xModel->connectController(xController);
139         xModel->setCurrentController(xController);
140         xController->attachModel(xModel);
141         if(xFrame.is())
142 		    xFrame->setComponent(xComponentWindow,xController);
143         //creates the view and menu
144         //for correct menu creation the initialized component must be already set into the frame
145 		xController->attachFrame(xFrame);
146 	}
147 
148     // call initNew() or load() at XLoadable
149     if(!bHaveLoadedModel)
150     try
151     {
152         comphelper::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( aMediaDescriptor.PROP_URL()));
153         if( aIt != aMediaDescriptor.end())
154         {
155             ::rtl::OUString aURL( (*aIt).second.get< ::rtl::OUString >());
156             if( aURL.matchAsciiL(
157                 RTL_CONSTASCII_STRINGPARAM( "private:factory/schart" )))
158             {
159                 // create new file
160                 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
161                 xLoadable->initNew();
162             }
163             else
164             {
165                 aMediaDescriptor.addInputStream();
166                 uno::Sequence< beans::PropertyValue > aCompleteMediaDescriptor;
167                 aMediaDescriptor >> aCompleteMediaDescriptor;
168                 apphelper::MediaDescriptorHelper aMDHelper( aCompleteMediaDescriptor );
169 
170                 // load file
171                 // @todo: replace: aMediaDescriptorHelper.getReducedForModel()
172                 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
173                 xLoadable->load( aCompleteMediaDescriptor );
174 
175                 //resize standalone files to get correct size:
176                 if( xComponentWindow.is() && aMDHelper.ISSET_FilterName && aMDHelper.FilterName.equals( C2U("StarChart 5.0")) )
177                 {
178                     awt::Rectangle aRect( xComponentWindow->getPosSize() );
179                     sal_Int16 nFlags=0;
180                     xComponentWindow->setPosSize( aRect.X, aRect.Y, aRect.Width, aRect.Height, nFlags );
181                 }
182             }
183         }
184     }
185     catch( uno::Exception & ex )
186     {
187         ASSERT_EXCEPTION( ex );
188     }
189 
190     return sal_True;
191 }
192 
193 	void SAL_CALL ChartFrameLoader
cancel()194 ::cancel() throw (uno::RuntimeException)
195 {
196 	m_oCancelFinished.reset();
197 	m_bCancelRequired = sal_True;
198 	m_oCancelFinished.wait();
199 	m_bCancelRequired = sal_False;
200 }
201 
202 //.............................................................................
203 } //namespace chart
204 //.............................................................................
205