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_dtrans.hxx"
26 
27 
28 //_________________________________________________________________________________________________________________________
29 //	other includes
30 //_________________________________________________________________________________________________________________________
31 #include <cppuhelper/servicefactory.hxx>
32 #include <com/sun/star/lang/XTypeProvider.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/container/NoSuchElementException.hpp>
35 #include <com/sun/star/datatransfer/XMimeContentType.hpp>
36 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <rtl/ustring.hxx>
39 #include <sal/types.h>
40 #include <osl/diagnose.h>
41 
42 #include <stdio.h>
43 
44 #include <vector>
45 
46 //-------------------------------------------------------------
47 // my defines
48 //-------------------------------------------------------------
49 
50 #define TEST_CLIPBOARD
51 #define RDB_SYSPATH  "d:\\projects\\src621\\dtrans\\wntmsci7\\bin\\applicat.rdb"
52 
53 //------------------------------------------------------------
54 //	namesapces
55 //------------------------------------------------------------
56 
57 using namespace	::rtl;
58 using namespace ::std;
59 using namespace ::cppu;
60 using namespace ::com::sun::star::datatransfer;
61 using namespace ::com::sun::star::uno;
62 using namespace	::com::sun::star::lang;
63 using namespace ::com::sun::star::container;
64 
65 //------------------------------------------------------------
66 //	globales
67 //------------------------------------------------------------
68 
69 //################################################################
70 
71 /*
72 void CheckMimeContentType( const OUString& aCntType, const OUString& aType, const OUString& aSubtype, sal_Int32 nParams )
73 {
74 
75 	Reference< XMimeContentType > xMimeCntType = xMCntTypeFactory->createMimeContentType( aCntType );
76 
77 	OSL_ASSERT( aType    == xMimeCntType->getMediaType ( ) );
78 	OSL_ASSERT( aSubtype == xMimeCntType->getMediaSubtype ( ) );
79 
80 	try
81 	{
82 		Sequence< OUString > seqParam = xMimeCntType->getParameters( );
83 		OSL_ASSERT( seqParam.getLength( ) == nParams );
84 
85 		OUString param;
86 		OUString pvalue;
87 		for ( sal_Int32 i = 0; i < seqParam.getLength( ); i++ )
88 		{
89 			param  = seqParam[i];
90 			OSL_ASSERT( xMimeCntType->hasParameter( param ) );
91 
92 			pvalue = xMimeCntType->getParameterValue( param );
93 		}
94 
95 		pvalue = xMimeCntType->getParameterValue( OUString::createFromAscii( "aparam" ) );
96 	}
97 	catch( IllegalArgumentException& )
98 	{
99 		printf( "FAILED: Invalid Mime Contenttype detected\n" );
100 	}
101 	catch( NoSuchElementException& )
102 	{
103 
104 	}
105 }
106 */
107 
108 //----------------------------------------------------------------
109 //
110 //----------------------------------------------------------------
111 
ShutdownServiceMgr(Reference<XMultiServiceFactory> & SrvMgr)112 void ShutdownServiceMgr( Reference< XMultiServiceFactory >& SrvMgr )
113 {
114 	// Cast factory to XComponent
115 	Reference< XComponent > xComponent( SrvMgr, UNO_QUERY );
116 
117 	if ( !xComponent.is() )
118 		OSL_ENSURE(sal_False, "Error shuting down");
119 
120 	// Dispose and clear factory
121 	xComponent->dispose();
122 	SrvMgr.clear();
123 	SrvMgr = Reference< XMultiServiceFactory >();
124 }
125 
126 //----------------------------------------------------------------
127 //
128 //----------------------------------------------------------------
129 
readCntTypesFromFileIntoVector(char * fname,vector<string> & vecData)130 sal_Bool readCntTypesFromFileIntoVector( char* fname, vector< string >& vecData )
131 {
132 	FILE* fstream;
133 
134 	fstream = fopen( fname, "r+" );
135 	if ( !fstream )
136 		return sal_False;
137 
138 	// set pointer to file start
139 	fseek( fstream, 0L, SEEK_SET );
140 
141 	char line[1024];
142 	while ( fscanf( fstream, "%[^\n]s", line ) != EOF )
143 	{
144 		vecData.push_back( line );
145 		fgetc( fstream );
146 	}
147 
148 	fclose( fstream );
149 
150 	return sal_True;
151 }
152 
153 //----------------------------------------------------------------
154 //
155 //----------------------------------------------------------------
156 
processCntTypesAndWriteResultIntoFile(char * fname,vector<string> & vecData,Reference<XMimeContentTypeFactory> cnttypeFactory)157 sal_Bool processCntTypesAndWriteResultIntoFile( char* fname, vector< string >& vecData, Reference< XMimeContentTypeFactory > cnttypeFactory )
158 {
159 	FILE* fstream;
160 
161 	fstream = fopen( fname, "w" );
162 	if ( !fstream )
163 		return sal_False;
164 
165 	// set pointer to file start
166 	fseek( fstream, 0L, SEEK_SET );
167 
168 	vector< string >::iterator iter_end = vecData.end( );
169 	const char* pStr = NULL;
170 
171 	for ( vector< string >::iterator iter = vecData.begin( ); iter != iter_end; ++iter )
172 	{
173 		try
174 		{
175 			fprintf( fstream, "Gelesen: %s\n", iter->c_str( ) );
176 
177 			Reference< XMimeContentType > xMCntTyp = cnttypeFactory->createMimeContentType( OUString::createFromAscii( iter->c_str( ) ) );
178 
179 			fwprintf( fstream, OUString::createFromAscii( "Type: %s\n" ),  xMCntTyp->getMediaType( ).getStr( ) );
180 			fwprintf( fstream, OUString::createFromAscii( "Subtype: %s\n" ), xMCntTyp->getMediaSubtype( ).getStr( ) );
181 
182 			Sequence< OUString > seqParam = xMCntTyp->getParameters( );
183 			sal_Int32 nParams = seqParam.getLength( );
184 
185 			for ( sal_Int32 i = 0; i < nParams; i++ )
186 			{
187 				fwprintf( fstream, OUString::createFromAscii("PName: %s\n" ), seqParam[i].getStr( ) );
188 				fwprintf( fstream, OUString::createFromAscii("PValue: %s\n" ), xMCntTyp->getParameterValue( seqParam[i] ).getStr( ) );
189 			}
190 		}
191 		catch( IllegalArgumentException& ex )
192 		{
193 			fwprintf( fstream, OUString::createFromAscii( "Fehlerhafter Content-Type gelesen!!!\n\n" ) );
194 		}
195 		catch( NoSuchElementException& )
196 		{
197 			fwprintf( fstream, OUString::createFromAscii( "Parameterwert nicht vorhanden\n" ) );
198 		}
199 		catch( ... )
200 		{
201 			fwprintf( fstream, OUString::createFromAscii( "Unbekannter Fehler!!!\n\n" ) );
202 		}
203 
204 		fwprintf( fstream, OUString::createFromAscii( "\n#############################################\n\n" ) );
205 	}
206 
207 	fclose( fstream );
208 
209 	return sal_True;
210 }
211 
212 //----------------------------------------------------------------
213 //	main
214 //----------------------------------------------------------------
215 
main(int nArgc,char * argv[])216 int SAL_CALL main( int nArgc, char* argv[] )
217 {
218 	if ( nArgc != 3 )
219 		printf( "Start with: testcnttype input-file output-file\n" );
220 
221 	//-------------------------------------------------
222 	// get the global service-manager
223 	//-------------------------------------------------
224 	OUString rdbName = OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH ) );
225 	Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( rdbName ) );
226 
227 	// Print a message if an error occured.
228 	if ( !g_xFactory.is( ) )
229 	{
230 		OSL_ENSURE(sal_False, "Can't create RegistryServiceFactory");
231 		return(-1);
232 	}
233 
234 	vector< string > vecCntTypes;
235 
236 	// open input-file and read the data
237 	if ( !readCntTypesFromFileIntoVector( argv[1], vecCntTypes ) )
238 	{
239 		printf( "Can't open input file" );
240 		ShutdownServiceMgr( g_xFactory );
241 	}
242 
243 	Reference< XMimeContentTypeFactory >
244 		xMCntTypeFactory( g_xFactory->createInstance( OUString::createFromAscii( "com.sun.star.datatransfer.MimeContentTypeFactory" ) ), UNO_QUERY );
245 
246 	if ( !xMCntTypeFactory.is( ) )
247 	{
248 		OSL_ENSURE( sal_False, "Error creating MimeContentTypeFactory Service" );
249 		return(-1);
250 	}
251 
252 	if ( !processCntTypesAndWriteResultIntoFile( argv[2], vecCntTypes, xMCntTypeFactory ) )
253 	{
254 		printf( "Can't open output file" );
255 		ShutdownServiceMgr( g_xFactory );
256 	}
257 
258 	//--------------------------------------------------
259 	// shutdown the service manager
260 	//--------------------------------------------------
261 
262 	ShutdownServiceMgr( g_xFactory );
263 
264 	return 0;
265 }
266