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