Debian Codegraph @

chart2/source/controller/main/ChartFrameloader.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include "ChartFrameloader.hxx"
21 #include <MediaDescriptorHelper.hxx>
22 #include <ChartController.hxx>
23 #include <ChartModel.hxx>
24 #include <unotools/fcm.hxx>
25 #include <unotools/mediadescriptor.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <com/sun/star/frame/XLoadable.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <comphelper/diagnose_ex.hxx>
30
31 namespace chart
32 {
33
34 using namespace ::com::sun::star;
35
36 ChartFrameLoader::ChartFrameLoader(
37 uno::Reference<uno::XComponentContext> const & xContext)
38 : m_bCancelRequired( false )
39 {
40 m_xCC = xContext;
41 m_oCancelFinished.reset();
42 }
43
44 ChartFrameLoader::~ChartFrameLoader()
45 {
46 }
47
48 bool ChartFrameLoader::impl_checkCancel()
49 {
50 if(m_bCancelRequired)
51 {
52 m_oCancelFinished.set();
53 return true;
54 }
55 return false;
56 }
57
58 // lang::XServiceInfo
59
60 OUString SAL_CALL ChartFrameLoader::getImplementationName()
61 {
62 return u"com.sun.star.comp.chart2.ChartFrameLoader"_ustr;
63 }
64
65 sal_Bool SAL_CALL ChartFrameLoader::supportsService( const OUString& rServiceName )
66 {
67 return cppu::supportsService(this, rServiceName);
68 }
69
70 css::uno::Sequence< OUString > SAL_CALL ChartFrameLoader::getSupportedServiceNames()
71 {
72 return { u"com.sun.star.frame.SynchronousFrameLoader"_ustr };
73 }
74
75 // frame::XFrameLoader
76
77 sal_Bool SAL_CALL ChartFrameLoader::load( const uno::Sequence< beans::PropertyValue >& rMediaDescriptor, const uno::Reference<frame::XFrame >& xFrame )
78 {
79 //@todo ? need to add as terminate listener to desktop?
80
81 uno::Reference< frame::XModel > xModel;
82 bool bHaveLoadedModel = false;
83
84 utl::MediaDescriptor aMediaDescriptor(rMediaDescriptor);
85 {
86 utl::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( utl::MediaDescriptor::PROP_MODEL));
87 if( aIt != aMediaDescriptor.end())
88 {
89 xModel.set( (*aIt).second.get< uno::Reference< frame::XModel > >());
90 bHaveLoadedModel = true;
91 }
92 }
93
94 //create and initialize the model
95 if( ! xModel.is())
96 {
97 //@todo?? load mechanism to cancel during loading of document
98 xModel = new ChartModel(m_xCC);
99
100 if( impl_checkCancel() )
101 return false;
102 }
103
104 //create the controller(+XWindow)
105 rtl::Reference< ChartController > xController = new ChartController(m_xCC);
106
107 if( impl_checkCancel() )
108 return false;
109
110 //connect frame, controller and model one to each other:
111 if(xModel.is())
112 {
113 utl::ConnectFrameControllerModel(xFrame, xController, xModel);
114 }
115
116 // call initNew() or load() at XLoadable
117 if(bHaveLoadedModel)
118 return true;
119
120 try
121 {
122 utl::MediaDescriptor::const_iterator aIt( aMediaDescriptor.find( utl::MediaDescriptor::PROP_URL));
123 if( aIt != aMediaDescriptor.end())
124 {
125 OUString aURL( (*aIt).second.get< OUString >());
126 if( aURL.startsWith( "private:factory/schart" ) )
127 {
128 // create new file
129 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
130 xLoadable->initNew();
131 }
132 else
133 {
134 // use the URL as BaseURL, similar to what SfxBaseModel effectively does
135 if (!aURL.isEmpty())
136 {
137 aMediaDescriptor[utl::MediaDescriptor::PROP_DOCUMENTBASEURL] <<= aURL;
138 }
139 aMediaDescriptor.addInputStream();
140 uno::Sequence< beans::PropertyValue > aCompleteMediaDescriptor;
141 aMediaDescriptor >> aCompleteMediaDescriptor;
142 apphelper::MediaDescriptorHelper aMDHelper( aCompleteMediaDescriptor );
143
144 // load file
145 // @todo: replace: aMediaDescriptorHelper.getReducedForModel()
146 uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY_THROW );
147 xLoadable->load( aCompleteMediaDescriptor );
148
149 //resize standalone files to get correct size:
150 if( aMDHelper.ISSET_FilterName && aMDHelper.FilterName == "StarChart 5.0" )
151 {
152 uno::Reference<awt::XWindow> xComponentWindow = xController->getComponentWindow();
153 awt::Rectangle aRect( xComponentWindow->getPosSize() );
154 xComponentWindow->setPosSize( aRect.X, aRect.Y, aRect.Width, aRect.Height, 0 );
155 }
156 }
157 }
158 }
159 catch( const uno::Exception & )
160 {
161 DBG_UNHANDLED_EXCEPTION("chart2");
162 }
163
164 return true;
165 }
166
167 void SAL_CALL ChartFrameLoader::cancel()
168 {
169 m_oCancelFinished.reset();
170 m_bCancelRequired = true;
171 m_oCancelFinished.wait();
172 m_bCancelRequired = false;
173 }
174
175 } //namespace chart
176
177 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
178 com_sun_star_comp_chart2_ChartFrameLoader_get_implementation(css::uno::XComponentContext *context,
179 css::uno::Sequence<css::uno::Any> const &)
180 {
181 return cppu::acquire(new chart::ChartFrameLoader(context));
182 }
183
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */