1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppuhelper/servicefactory.hxx>
21 #include <com/sun/star/lang/XTypeProvider.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/container/NoSuchElementException.hpp>
24 #include <com/sun/star/datatransfer/XMimeContentType.hpp>
25 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 #include <osl/diagnose.h>
37 #define TEST_CLIPBOARD
38 #define RDB_SYSPATH "d:\\projects\\src621\\dtrans\\wntmsci7\\bin\\applicat.rdb"
42 using namespace ::std
;
43 using namespace ::cppu
;
44 using namespace ::com::sun::star::datatransfer
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::container
;
49 void ShutdownServiceMgr( Reference
< XMultiServiceFactory
>& SrvMgr
)
51 // Cast factory to XComponent
52 Reference
< XComponent
> xComponent( SrvMgr
, UNO_QUERY
);
54 if ( !xComponent
.is() )
55 OSL_FAIL("Error shutting down");
57 // Dispose and clear factory
58 xComponent
->dispose();
62 sal_Bool
readCntTypesFromFileIntoVector( char* fname
, vector
< string
>& vecData
)
66 fstream
= fopen( fname
, "r+" );
70 // set pointer to file start
71 fseek( fstream
, 0, SEEK_SET
);
74 while ( fscanf( fstream
, "%1023[^\n]s", line
) != EOF
)
76 vecData
.push_back( line
);
85 sal_Bool
processCntTypesAndWriteResultIntoFile( char* fname
, vector
< string
>& vecData
, Reference
< XMimeContentTypeFactory
> cnttypeFactory
)
89 fstream
= fopen( fname
, "w" );
93 // set pointer to file start
94 fseek( fstream
, 0, SEEK_SET
);
96 for ( const auto& rData
: vecData
)
100 fprintf( fstream
, "Read: %s\n", rData
.c_str( ) );
102 Reference
< XMimeContentType
> xMCntTyp
= cnttypeFactory
->createMimeContentType( OUString::createFromAscii( rData
.c_str( ) ) );
104 fwprintf( fstream
, OUString("Type: %s\n"), xMCntTyp
->getMediaType( ).getStr( ) );
105 fwprintf( fstream
, OUString("Subtype: %s\n"), xMCntTyp
->getMediaSubtype( ).getStr( ) );
107 Sequence
< OUString
> seqParam
= xMCntTyp
->getParameters( );
108 sal_Int32 nParams
= seqParam
.getLength( );
110 for ( sal_Int32 i
= 0; i
< nParams
; i
++ )
112 fwprintf( fstream
, OUString("PName: %s\n"), seqParam
[i
].getStr( ) );
113 fwprintf( fstream
, OUString("PValue: %s\n"), xMCntTyp
->getParameterValue( seqParam
[i
] ).getStr( ) );
116 catch( IllegalArgumentException
& ex
)
118 fwprintf( fstream
, OUString("Read incorrect content type!\n\n") );
120 catch( NoSuchElementException
& )
122 fwprintf( fstream
, OUString("Value of parameter not available\n") );
126 fwprintf( fstream
, OUString("Unknown error!\n\n") );
129 fwprintf( fstream
, OUString("\n#############################################\n\n") );
139 int SAL_CALL
main( int nArgc
, char* argv
[] )
142 printf( "Start with: testcnttype input-file output-file\n" );
144 // get the global service-manager
146 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( RDB_SYSPATH
) );
148 // Print a message if an error occurred.
149 if ( !g_xFactory
.is( ) )
151 OSL_FAIL("Can't create RegistryServiceFactory");
155 vector
< string
> vecCntTypes
;
157 // open input-file and read the data
158 if ( !readCntTypesFromFileIntoVector( argv
[1], vecCntTypes
) )
160 printf( "Can't open input file" );
161 ShutdownServiceMgr( g_xFactory
);
164 Reference
< XMimeContentTypeFactory
>
165 xMCntTypeFactory( g_xFactory
->createInstance("com.sun.star.datatransfer.MimeContentTypeFactory"), UNO_QUERY
);
167 if ( !xMCntTypeFactory
.is( ) )
169 OSL_FAIL( "Error creating MimeContentTypeFactory Service" );
173 if ( !processCntTypesAndWriteResultIntoFile( argv
[2], vecCntTypes
, xMCntTypeFactory
) )
175 printf( "Can't open output file" );
176 ShutdownServiceMgr( g_xFactory
);
179 // shutdown the service manager
181 ShutdownServiceMgr( g_xFactory
);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */