1c142477cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c142477cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c142477cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c142477cSAndrew Rist  * distributed with this work for additional information
6c142477cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c142477cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c142477cSAndrew Rist  * "License"); you may not use this file except in compliance
9c142477cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10c142477cSAndrew Rist  *
11c142477cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12c142477cSAndrew Rist  *
13c142477cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c142477cSAndrew Rist  * software distributed under the License is distributed on an
15c142477cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c142477cSAndrew Rist  * KIND, either express or implied.  See the License for the
17c142477cSAndrew Rist  * specific language governing permissions and limitations
18c142477cSAndrew Rist  * under the License.
19c142477cSAndrew Rist  *
20c142477cSAndrew Rist  *************************************************************/
21c142477cSAndrew Rist 
22c142477cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sdext.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "pppoptimizer.hxx"
28cdf0e10cSrcweir #include "impoptimizer.hxx"
29cdf0e10cSrcweir #include <osl/file.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
32cdf0e10cSrcweir 
33*597a4c59SAriel Constenla-Haile #define SERVICE_NAME "com.sun.star.comp.PPPOptimizer"
34*597a4c59SAriel Constenla-Haile 
35*597a4c59SAriel Constenla-Haile 
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir using namespace ::com::sun::star::util;
38cdf0e10cSrcweir using namespace ::com::sun::star::lang;
39cdf0e10cSrcweir using namespace ::com::sun::star::frame;
40cdf0e10cSrcweir using namespace ::com::sun::star::beans;
41cdf0e10cSrcweir 
42*597a4c59SAriel Constenla-Haile using ::rtl::OUString;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ----------------
45cdf0e10cSrcweir // - PPPOptimizer -
46cdf0e10cSrcweir // ----------------
47cdf0e10cSrcweir 
48*597a4c59SAriel Constenla-Haile PPPOptimizer::PPPOptimizer( const Reference< XComponentContext > &rxContext ) :
49*597a4c59SAriel Constenla-Haile     mxContext( rxContext )
50cdf0e10cSrcweir {
51*597a4c59SAriel Constenla-Haile     OSL_TRACE("PPPOptimizer::PPPOptimizer");
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // -----------------------------------------------------------------------------
55cdf0e10cSrcweir 
56cdf0e10cSrcweir PPPOptimizer::~PPPOptimizer()
57cdf0e10cSrcweir {
58*597a4c59SAriel Constenla-Haile     OSL_TRACE("PPPOptimizer::~PPPOptimizer");
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir // -----------------------------------------------------------------------------
62cdf0e10cSrcweir // XInitialization
63cdf0e10cSrcweir // -----------------------------------------------------------------------------
64cdf0e10cSrcweir 
65cdf0e10cSrcweir void SAL_CALL PPPOptimizer::initialize( const Sequence< Any >& aArguments )
66cdf0e10cSrcweir 	throw ( Exception, RuntimeException )
67cdf0e10cSrcweir {
68*597a4c59SAriel Constenla-Haile     OSL_TRACE("PPPOptimizer::initialize");
69cdf0e10cSrcweir 	if( aArguments.getLength() != 1 )
70cdf0e10cSrcweir 		throw IllegalArgumentException();
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	Reference< XFrame > xFrame;
73cdf0e10cSrcweir 	aArguments[ 0 ] >>= xFrame;
74cdf0e10cSrcweir 	if ( xFrame.is() )
75cdf0e10cSrcweir 		mxController = xFrame->getController();
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir // -----------------------------------------------------------------------------
79cdf0e10cSrcweir // XServiceInfo
80cdf0e10cSrcweir // -----------------------------------------------------------------------------
81cdf0e10cSrcweir 
82cdf0e10cSrcweir OUString SAL_CALL PPPOptimizer::getImplementationName()
83cdf0e10cSrcweir 	throw ( RuntimeException )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	return PPPOptimizer_getImplementationName();
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir sal_Bool SAL_CALL PPPOptimizer::supportsService( const OUString& rServiceName )
89cdf0e10cSrcweir 	throw ( RuntimeException )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     return rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( SERVICE_NAME ) );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir Sequence< OUString > SAL_CALL PPPOptimizer::getSupportedServiceNames()
95cdf0e10cSrcweir 	throw ( RuntimeException )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     return PPPOptimizer_getSupportedServiceNames();
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // -----------------------------------------------------------------------------
101cdf0e10cSrcweir // XDispatchProvider
102cdf0e10cSrcweir // -----------------------------------------------------------------------------
103cdf0e10cSrcweir 
104cdf0e10cSrcweir Reference< com::sun::star::frame::XDispatch > SAL_CALL PPPOptimizer::queryDispatch(
105cdf0e10cSrcweir 	const URL& aURL, const ::rtl::OUString& /* aTargetFrameName */, sal_Int32 /* nSearchFlags */ ) throw( RuntimeException )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	Reference < XDispatch > xRet;
108cdf0e10cSrcweir     if ( aURL.Protocol.compareToAscii( "vnd.com.sun.star.comp.PPPOptimizer:" ) == 0 )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir //		if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
111cdf0e10cSrcweir 		xRet = this;
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 	return xRet;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //------------------------------------------------------------------------------
117cdf0e10cSrcweir 
118cdf0e10cSrcweir Sequence< Reference< com::sun::star::frame::XDispatch > > SAL_CALL PPPOptimizer::queryDispatches(
119cdf0e10cSrcweir 	const Sequence< com::sun::star::frame::DispatchDescriptor >& aDescripts ) throw( RuntimeException )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	Sequence< Reference< com::sun::star::frame::XDispatch> > aReturn( aDescripts.getLength() );
122cdf0e10cSrcweir 	Reference< com::sun::star::frame::XDispatch>* pReturn = aReturn.getArray();
123cdf0e10cSrcweir 	const com::sun::star::frame::DispatchDescriptor* pDescripts = aDescripts.getConstArray();
124cdf0e10cSrcweir 	for (sal_Int16 i = 0; i < aDescripts.getLength(); ++i, ++pReturn, ++pDescripts )
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		*pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir 	return aReturn;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir // -----------------------------------------------------------------------------
132cdf0e10cSrcweir // XDispatch
133cdf0e10cSrcweir // -----------------------------------------------------------------------------
134cdf0e10cSrcweir 
135cdf0e10cSrcweir void SAL_CALL PPPOptimizer::dispatch( const URL& rURL, const Sequence< PropertyValue >& lArguments )
136cdf0e10cSrcweir     throw( RuntimeException )
137cdf0e10cSrcweir {
138*597a4c59SAriel Constenla-Haile     OSL_TRACE("PPPOptimizer::dispatch");
139cdf0e10cSrcweir 	if ( mxController.is() && ( rURL.Protocol.compareToAscii( "vnd.com.sun.star.comp.PPPOptimizer:" ) == 0 ) )
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		if ( rURL.Path.compareToAscii( "optimize" ) == 0 )
142cdf0e10cSrcweir 		{
143cdf0e10cSrcweir 			Reference< XModel > xModel( mxController->getModel() );
144cdf0e10cSrcweir 			if ( xModel.is() )
145cdf0e10cSrcweir 			{
146cdf0e10cSrcweir 				try
147cdf0e10cSrcweir 				{
148*597a4c59SAriel Constenla-Haile 					ImpOptimizer aOptimizer( mxContext, xModel );
149cdf0e10cSrcweir 					aOptimizer.Optimize( lArguments );
150cdf0e10cSrcweir 				}
151cdf0e10cSrcweir 				catch( Exception& )
152cdf0e10cSrcweir 				{
153cdf0e10cSrcweir 				}
154cdf0e10cSrcweir 			}
155cdf0e10cSrcweir 		}
156cdf0e10cSrcweir 	}
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir //===============================================
160cdf0e10cSrcweir void SAL_CALL PPPOptimizer::addStatusListener( const Reference< XStatusListener >&, const URL& )
161cdf0e10cSrcweir 	throw( RuntimeException )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     // TODO
164cdf0e10cSrcweir     OSL_ENSURE( sal_False, "PPPOptimizer::addStatusListener()\nNot implemented yet!" );
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir //===============================================
168cdf0e10cSrcweir void SAL_CALL PPPOptimizer::removeStatusListener( const Reference< XStatusListener >&, const URL& )
169cdf0e10cSrcweir     throw( RuntimeException )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir     // TODO
172cdf0e10cSrcweir     OSL_ENSURE( sal_False, "PPPOptimizer::removeStatusListener()\nNot implemented yet!" );
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir // -----------------------------------------------------------------------------
176cdf0e10cSrcweir // returning filesize, on error zero is returned
177cdf0e10cSrcweir sal_Int64 PPPOptimizer::GetFileSize( const rtl::OUString& rURL )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	sal_Int64 nFileSize = 0;
180cdf0e10cSrcweir 	osl::DirectoryItem aItem;
181cdf0e10cSrcweir 	if ( osl::DirectoryItem::get( rURL, aItem ) == osl::FileBase::E_None )
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		osl::FileStatus aStatus( osl_FileStatus_Mask_FileSize );
184cdf0e10cSrcweir 		if ( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			nFileSize = aStatus.getFileSize();
187cdf0e10cSrcweir 		}
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 	return nFileSize;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir // -----------------------------------------------------------------------------
193cdf0e10cSrcweir 
194cdf0e10cSrcweir OUString PPPOptimizer_getImplementationName()
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.PPPOptimizerImp" ) );
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir Sequence< OUString > PPPOptimizer_getSupportedServiceNames()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	Sequence < OUString > aRet(1);
202cdf0e10cSrcweir     OUString* pArray = aRet.getArray();
203cdf0e10cSrcweir     pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
204cdf0e10cSrcweir     return aRet;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir Reference< XInterface > PPPOptimizer_createInstance( const Reference< XComponentContext > & rSMgr )
208cdf0e10cSrcweir 	throw( Exception )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	return (cppu::OWeakObject*) new PPPOptimizer( rSMgr );
211cdf0e10cSrcweir }
212