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_sdext.hxx"
26 
27 #include "pppoptimizer.hxx"
28 #include "impoptimizer.hxx"
29 #include <osl/file.hxx>
30 
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 
33 using namespace ::rtl;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::util;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::frame;
38 using namespace ::com::sun::star::beans;
39 
40 #define SERVICE_NAME "com.sun.star.comp.PPPOptimizer"
41 
42 // ----------------
43 // - PPPOptimizer -
44 // ----------------
45 
46 PPPOptimizer::PPPOptimizer( const Reference< XComponentContext > &rxMSF ) :
47     mxMSF( rxMSF )
48 {
49 }
50 
51 // -----------------------------------------------------------------------------
52 
53 PPPOptimizer::~PPPOptimizer()
54 {
55 }
56 
57 // -----------------------------------------------------------------------------
58 // XInitialization
59 // -----------------------------------------------------------------------------
60 
61 void SAL_CALL PPPOptimizer::initialize( const Sequence< Any >& aArguments )
62 	throw ( Exception, RuntimeException )
63 {
64 	if( aArguments.getLength() != 1 )
65 		throw IllegalArgumentException();
66 
67 	Reference< XFrame > xFrame;
68 	aArguments[ 0 ] >>= xFrame;
69 	if ( xFrame.is() )
70 		mxController = xFrame->getController();
71 }
72 
73 // -----------------------------------------------------------------------------
74 // XServiceInfo
75 // -----------------------------------------------------------------------------
76 
77 OUString SAL_CALL PPPOptimizer::getImplementationName()
78 	throw ( RuntimeException )
79 {
80 	return PPPOptimizer_getImplementationName();
81 }
82 
83 sal_Bool SAL_CALL PPPOptimizer::supportsService( const OUString& rServiceName )
84 	throw ( RuntimeException )
85 {
86     return rServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( SERVICE_NAME ) );
87 }
88 
89 Sequence< OUString > SAL_CALL PPPOptimizer::getSupportedServiceNames()
90 	throw ( RuntimeException )
91 {
92     return PPPOptimizer_getSupportedServiceNames();
93 }
94 
95 // -----------------------------------------------------------------------------
96 // XDispatchProvider
97 // -----------------------------------------------------------------------------
98 
99 Reference< com::sun::star::frame::XDispatch > SAL_CALL PPPOptimizer::queryDispatch(
100 	const URL& aURL, const ::rtl::OUString& /* aTargetFrameName */, sal_Int32 /* nSearchFlags */ ) throw( RuntimeException )
101 {
102 	Reference < XDispatch > xRet;
103     if ( aURL.Protocol.compareToAscii( "vnd.com.sun.star.comp.PPPOptimizer:" ) == 0 )
104     {
105 //		if ( aURL.Path.compareToAscii( "Function1" ) == 0 )
106 		xRet = this;
107     }
108 	return xRet;
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 Sequence< Reference< com::sun::star::frame::XDispatch > > SAL_CALL PPPOptimizer::queryDispatches(
114 	const Sequence< com::sun::star::frame::DispatchDescriptor >& aDescripts ) throw( RuntimeException )
115 {
116 	Sequence< Reference< com::sun::star::frame::XDispatch> > aReturn( aDescripts.getLength() );
117 	Reference< com::sun::star::frame::XDispatch>* pReturn = aReturn.getArray();
118 	const com::sun::star::frame::DispatchDescriptor* pDescripts = aDescripts.getConstArray();
119 	for (sal_Int16 i = 0; i < aDescripts.getLength(); ++i, ++pReturn, ++pDescripts )
120 	{
121 		*pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
122 	}
123 	return aReturn;
124 }
125 
126 // -----------------------------------------------------------------------------
127 // XDispatch
128 // -----------------------------------------------------------------------------
129 
130 void SAL_CALL PPPOptimizer::dispatch( const URL& rURL, const Sequence< PropertyValue >& lArguments )
131     throw( RuntimeException )
132 {
133 	if ( mxController.is() && ( rURL.Protocol.compareToAscii( "vnd.com.sun.star.comp.PPPOptimizer:" ) == 0 ) )
134 	{
135 		if ( rURL.Path.compareToAscii( "optimize" ) == 0 )
136 		{
137 			Reference< XModel > xModel( mxController->getModel() );
138 			if ( xModel.is() )
139 			{
140 				try
141 				{
142 					ImpOptimizer aOptimizer( mxMSF, xModel );
143 					aOptimizer.Optimize( lArguments );
144 				}
145 				catch( Exception& )
146 				{
147 				}
148 			}
149 		}
150 	}
151 }
152 
153 //===============================================
154 void SAL_CALL PPPOptimizer::addStatusListener( const Reference< XStatusListener >&, const URL& )
155 	throw( RuntimeException )
156 {
157     // TODO
158     OSL_ENSURE( sal_False, "PPPOptimizer::addStatusListener()\nNot implemented yet!" );
159 }
160 
161 //===============================================
162 void SAL_CALL PPPOptimizer::removeStatusListener( const Reference< XStatusListener >&, const URL& )
163     throw( RuntimeException )
164 {
165     // TODO
166     OSL_ENSURE( sal_False, "PPPOptimizer::removeStatusListener()\nNot implemented yet!" );
167 }
168 
169 // -----------------------------------------------------------------------------
170 // returning filesize, on error zero is returned
171 sal_Int64 PPPOptimizer::GetFileSize( const rtl::OUString& rURL )
172 {
173 	sal_Int64 nFileSize = 0;
174 	osl::DirectoryItem aItem;
175 	if ( osl::DirectoryItem::get( rURL, aItem ) == osl::FileBase::E_None )
176 	{
177 		osl::FileStatus aStatus( osl_FileStatus_Mask_FileSize );
178 		if ( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
179 		{
180 			nFileSize = aStatus.getFileSize();
181 		}
182 	}
183 	return nFileSize;
184 }
185 
186 // -----------------------------------------------------------------------------
187 
188 OUString PPPOptimizer_getImplementationName()
189 {
190 	return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.PPPOptimizerImp" ) );
191 }
192 
193 Sequence< OUString > PPPOptimizer_getSupportedServiceNames()
194 {
195 	Sequence < OUString > aRet(1);
196     OUString* pArray = aRet.getArray();
197     pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
198     return aRet;
199 }
200 
201 Reference< XInterface > PPPOptimizer_createInstance( const Reference< XComponentContext > & rSMgr )
202 	throw( Exception )
203 {
204 	return (cppu::OWeakObject*) new PPPOptimizer( rSMgr );
205 }
206