xref: /aoo41x/main/sc/source/ui/vba/testvba/testvba.cxx (revision cdf0e10c)
1 #include "cppuhelper/bootstrap.hxx"
2 
3 #include <com/sun/star/beans/Property.hpp>
4 #include <com/sun/star/beans/XPropertySet.hpp>
5 #include <com/sun/star/beans/XPropertySetInfo.hpp>
6 #include <com/sun/star/container/XNameAccess.hpp>
7 #include <com/sun/star/container/XNameContainer.hpp>
8 #include <com/sun/star/frame/XComponentLoader.hpp>
9 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
10 #include <com/sun/star/sheet/XSpreadsheet.hpp>
11 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
12 #include <com/sun/star/util/XCloseable.hpp>
13 #include <com/sun/star/uno/XComponentContext.hpp>
14 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
15 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
16 #include <com/sun/star/document/XTypeDetection.hpp>
17 
18 #include <tools/urlobj.hxx>
19 #include <osl/file.hxx>
20 
21 #include <memory>
22 #include <iostream>
23 
24 using namespace ::com::sun::star;
25 using namespace ::com::sun::star::sheet;
26 
27 using ::com::sun::star::beans::Property;
28 using ::com::sun::star::beans::PropertyValue;
29 using ::com::sun::star::beans::XPropertySet;
30 using ::com::sun::star::beans::XPropertySetInfo;
31 using ::com::sun::star::container::XNameContainer;
32 using ::com::sun::star::lang::XComponent;
33 using ::com::sun::star::lang::XMultiComponentFactory;
34 using ::com::sun::star::frame::XComponentLoader;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::uno::UNO_QUERY;
38 using ::com::sun::star::uno::UNO_QUERY_THROW;
39 using ::com::sun::star::uno::XComponentContext;
40 using ::com::sun::star::uno::XInterface;
41 using ::com::sun::star::ucb::XSimpleFileAccess;
42 using ::com::sun::star::document::XTypeDetection;
43 using ::rtl::OUString;
44 
45 using ::std::auto_ptr;
46 
47 const OUString EXTN = rtl::OUString::createFromAscii(".xls");
48 
49 OUString convertToURL( const OUString& rPath )
50 {
51         rtl::OUString aURL;
52         INetURLObject aObj;
53         aObj.SetURL( rPath );
54         bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
55         if ( bIsURL )
56                 aURL = rPath;
57         else
58 		{
59                 osl::FileBase::getFileURLFromSystemPath( rPath, aURL );
60 				if ( aURL.equals( rPath ) )
61 					throw uno::RuntimeException( rtl::OUString::createFromAscii( "could'nt convert " ).concat( rPath ).concat( rtl::OUString::createFromAscii( " to a URL, is it a fully qualified path name? " ) ), Reference< uno::XInterface >() );
62 		}
63 		return aURL;
64 }
65 
66 OUString ascii(const sal_Char* cstr)
67 {
68 	return OUString::createFromAscii(cstr);
69 }
70 
71 const sal_Char* getStr(const OUString& ou)
72 {
73     return OUStringToOString(ou, RTL_TEXTENCODING_UTF8).getStr();
74 }
75 
76 
77 int usage( const char* pName )
78 {
79 	std::cerr << "usage: " << pName << "<path to testdocument dir> <output_directory>" << std::endl;
80         return 1;
81 
82 }
83 
84 class TestVBA
85 {
86 private:
87 	Reference< XComponentContext >  mxContext;
88 	Reference< XMultiComponentFactory > mxMCF;
89 	Reference< XComponentLoader > mxCompLoader;
90 	Reference< XSimpleFileAccess > mxSFA;
91 	rtl::OUString msOutDirPath;
92 protected:
93 public:
94 	TestVBA( const Reference< XComponentContext >&  _xContext,
95 		const Reference< XMultiComponentFactory >& _xMCF,
96 		const Reference< XComponentLoader >& _xCompLoader,
97 		const rtl::OUString& _outDirPath ) : mxContext( _xContext ), mxMCF( _xMCF ),
98 mxCompLoader( _xCompLoader ), msOutDirPath( convertToURL( _outDirPath  ) )
99 	{
100 		mxSFA.set( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ), mxContext), uno::UNO_QUERY_THROW );
101 	}
102 
103 	rtl::OUString getLogLocation() throw (  beans::UnknownPropertyException,  lang::IllegalArgumentException, lang::WrappedTargetException,  uno::Exception )
104 	{
105 		rtl::OUString sLogLocation;
106 		Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
107 		pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
108 		sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( rtl::OUString::createFromAscii( "HelperAPI-test.log" ) );
109 		return sLogLocation;
110 	}
111 	rtl::OUString getLogLocationWithName( OUString fileName ) throw (  beans::UnknownPropertyException,  lang::IllegalArgumentException, lang::WrappedTargetException,  uno::Exception )
112     {
113         printf("%s\n", getenv("HOME") );
114     	printf("file name %s\n", rtl::OUStringToOString( fileName, RTL_TEXTENCODING_UTF8 ).getStr() );
115 		//rtl::OUString sLogLocation( rtl::OUString::createFromAscii( getenv("HOME") ) );
116 		rtl::OUString sLogLocation;
117 		Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
118 		pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
119 		sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( fileName.copy ( 0, fileName.lastIndexOf( EXTN )  ) + rtl::OUString::createFromAscii( ".log" ) );
120 		return sLogLocation;
121     }
122 
123 	void init()
124 	{
125 		// blow away previous logs?
126 	}
127 
128 	void proccessDocument( const rtl::OUString& sUrl )
129 	{
130 			if ( !mxSFA->isFolder( sUrl ) && sUrl.endsWithIgnoreAsciiCaseAsciiL( ".xls", 4 ) )
131 
132 			{
133 				try
134 				{
135 					OSL_TRACE( "processing %s",  rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
136 					printf( "processing %s\n",  rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
137 					// Loading the wanted document
138 					Sequence< PropertyValue > propertyValues(1);
139 					propertyValues[0].Name = rtl::OUString::createFromAscii( "Hidden" );
140 					propertyValues[0].Value <<= sal_False;
141 
142                     rtl::OUString sfileUrl = convertToURL( sUrl );
143 					printf( "try to get xDoc %s\n", rtl::OUStringToOString( sfileUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
144 					Reference< uno::XInterface > xDoc =
145 						mxCompLoader->loadComponentFromURL( sfileUrl, rtl::OUString::createFromAscii( "_blank" ), 0, propertyValues);
146 					printf( "got xDoc\n" );
147 
148 					OUString logFileURL = convertToURL( getLogLocation() );
149 					try
150 					{
151 						Reference< script::provider::XScriptProviderSupplier > xSupplier( xDoc, uno::UNO_QUERY_THROW ) ;
152 						if ( mxSFA->exists( logFileURL ) )
153 							mxSFA->kill( logFileURL );
154 
155 						printf("try to get the ScriptProvider\n");
156 						Reference< script::provider::XScriptProvider > xProv = xSupplier->getScriptProvider();
157 						printf("get the ScriptProvider\n");
158 						printf("try to get the Script\n");
159                         Reference< script::provider::XScript > xScript;
160 						try
161                         {
162 						    xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.TestMacros.Main?language=Basic&location=document" ));
163                         } catch ( uno::Exception& e )
164                         {
165                             try
166                             {
167 						        xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.testMacro.Main?language=Basic&location=document" ));
168                             } catch ( uno::Exception& e2 )
169                             {
170 						        xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.testMain.Main?language=Basic&location=document" ));
171                             }
172                         }
173 						OSL_TRACE("Got script for doc %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
174 						printf("get the Script\n");
175 						Sequence< uno::Any > aArgs;
176 						Sequence< sal_Int16 > aOutArgsIndex;
177 						Sequence< uno::Any > aOutArgs;
178 
179 						xScript->invoke(aArgs, aOutArgsIndex, aOutArgs);
180 
181 						OUString fileName = sUrl.copy ( sUrl.lastIndexOf( '/' ) );
182 						OUString newLocation = msOutDirPath + fileName.copy ( 0, fileName.lastIndexOf( EXTN )  ) + rtl::OUString::createFromAscii( ".log" );
183                         try
184                         {
185     						printf("move log file\n");
186 	    					mxSFA->move( logFileURL, newLocation );
187 		    				OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
188 			    			printf("moved to new location\n");
189                         }
190                         catch ( uno::Exception& e )
191                         {
192                             logFileURL = convertToURL( getLogLocationWithName( fileName ) );
193     						printf("move log file from %s\n", rtl::OUStringToOString( logFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
194 	    					mxSFA->move( logFileURL, newLocation );
195 		    				OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
196 			    			printf("moved to new location\n");
197                         }
198 
199 					}
200 					catch ( uno::Exception& e )
201 					{
202 						std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
203 					}
204 
205 					// interface is supported, otherwise use XComponent.dispose
206 					Reference< util::XCloseable > xCloseable ( xDoc, uno::UNO_QUERY );
207 
208 					if ( xCloseable.is() )
209 					{
210 			    		printf("try to close\n");
211                         // will close application. and only run a test case for 3.0
212                         // maybe it is a bug. yes, it is a bug
213                         // if only one frame and model, click a button which related will colse.
214                         // will make a crash. It related with window listener.
215                         // so, for run all test cases, it should not close the document at this moment.
216 						xCloseable->close(sal_False);
217 			    		printf("closed\n");
218 					}
219 					else
220 					{
221 			    		printf("try to dispose\n");
222 						Reference< XComponent > xComp( xDoc, uno::UNO_QUERY_THROW );
223                         // same as close.
224 						xComp->dispose();
225 			    		printf("disposed\n");
226 					}
227 				}
228 				catch( uno::Exception& e )
229 				{
230 					std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
231 				}
232 
233 			}
234 		printf("complete processing %s\n", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
235 	}
236 
237 	void traverse( const rtl::OUString& sFileDirectory )
238 	{
239 		rtl::OUString sFileDirectoryURL = convertToURL( sFileDirectory );
240 		if ( !mxSFA->isFolder( sFileDirectoryURL) )
241 		{
242 			throw lang::IllegalArgumentException( rtl::OUString::createFromAscii( "not a directory: ").concat( sFileDirectoryURL ), Reference<uno::XInterface>(), 1 );
243 		}
244 		// Getting all files and directories in the current directory
245 		Sequence<OUString> entries = mxSFA->getFolderContents( sFileDirectoryURL, sal_False );
246 
247 		// Iterating for each file and directory
248         printf( "Entries %d\n", (int)entries.getLength() );
249 		for ( sal_Int32 i = 0; i < entries.getLength(); ++i )
250 		{
251             proccessDocument( entries[ i ] );
252 		}
253 	}
254 };
255 
256 void tryDispose( Reference< uno::XInterface > xIF, const char* sComp )
257 {
258 	Reference< lang::XComponent > xComponent( xIF, uno::UNO_QUERY );
259 	if ( xComponent.is() )
260 	{
261 		try
262 		{
263 			xComponent->dispose();
264 		}
265 		catch( uno::Exception& e )
266 		{
267 			std::cerr << "tryDispose caught exception " <<rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << " while disposing " <<  sComp << std::endl;
268 		}
269 	}
270 }
271 int main( int argv, char** argc )
272 {
273 	if ( !( argv > 2 ) )
274 		return usage( argc[0] );
275 	try
276 	{
277 
278 		OSL_TRACE("Attempting to bootstrap normal");
279 		Reference<XComponentContext> xCC = ::cppu::bootstrap();
280 		Reference<XMultiComponentFactory> xFactory = xCC->getServiceManager();
281 		OSL_TRACE("got servicemanager");
282         std::cout << "got servicemanager" << std::endl;
283 		Reference<XInterface> desktop = xFactory->createInstanceWithContext(
284 		ascii("com.sun.star.frame.Desktop"), xCC);
285 		OSL_TRACE("got desktop");
286         std::cout << "got desktop" << std::endl;
287 		Reference<frame::XComponentLoader> xLoader(desktop, UNO_QUERY_THROW);
288 		TestVBA* dTest = new TestVBA( xCC, xFactory, xLoader, ascii( argc[ 2 ] ) );
289         if ( argv == 4 )
290         {
291             std::cout << "before process" << std::endl;
292             dTest->proccessDocument( ascii( argc[ 3 ] ) );
293             std::cout << "after process" << std::endl;
294         }
295         else
296         {
297 		    dTest->traverse( ascii( argc[ 1 ] ) );
298         }
299 		delete dTest;
300 //		tryDispose( xLoader, "desktop" );
301 //		tryDispose( xCC, "remote context" );
302 
303 	}
304 	catch( uno::Exception& e )
305 	{
306 		std::cerr << "Caught Exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
307 	}
308 
309 }
310