#include <stdio.h>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>

using namespace std;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge;
using namespace com::sun::star::io;
using namespace com::sun::star::beans;
using namespace com::sun::star::frame;
using namespace rtl;
using namespace cppu;


Reference< XMultiServiceFactory > ooConnect(){
   // create the initial component context
   Reference< XComponentContext > rComponentContext = 
				defaultBootstrap_InitialComponentContext();
 
   // retrieve the servicemanager from the context
   Reference< XMultiComponentFactory > rServiceManager = 
				rComponentContext->getServiceManager();
 
   // instantiate a sample service with the servicemanager.
   Reference< XInterface > rInstance =  rServiceManager->createInstanceWithContext(
         OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),rComponentContext );
 
   // Query for the XUnoUrlResolver interface
   Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY );
   if( ! rResolver.is() ){
      printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
      return NULL;
   }
   try {
      // resolve the uno-url
      rInstance = rResolver->resolve( OUString::createFromAscii(
         "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" ) );
 
      if( ! rInstance.is() ){
         printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" );
         return NULL;
      }
 
      // query for the simpler XMultiServiceFactory interface, sufficient for scripting
      Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY);
 
      if( ! rOfficeServiceManager.is() ){
            printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
            return NULL;
        }       
        return rOfficeServiceManager;
   }
   catch( Exception &e ){
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "Error: %s\n", o.pData->buffer );
      return NULL;
   }
   return NULL;
}

int main( ) {
//retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager;
    rOfficeServiceManager = ooConnect();
    if( rOfficeServiceManager.is() ){
        printf( "Connected sucessfully to the office\n" );
    }
 
//get the desktop service using createInstance returns an XInterface type
    Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
    OUString::createFromAscii( "com.sun.star.frame.Desktop" ));
 
//query for the XComponentLoader interface
    Reference< XComponentLoader > rComponentLoader (Desktop, UNO_QUERY);
    if( rComponentLoader.is() ){
        	printf( "XComponentloader successfully instanciated\n" );
    	}
 
//get an instance of the spreadsheet
	Sequence < ::com::sun::star::beans::PropertyValue > pvin(1);
	pvin[0].Name =OUString::createFromAscii("FilterName"); 
	pvin[0].Value = Any(OUString::createFromAscii("MS PowerPoint 97"));

    Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL(
	OUString::createFromAscii("file:///home/smoutou/ppt.ppt"),
//	OUString::createFromAscii("file:///home/smoutou/ta.html"),
//	OUString::createFromAscii("file:///home/smoutou/bak.doc"),
//	OUString::createFromAscii("file:///home/smoutou/cpp.pdf"),
        OUString::createFromAscii("_default"),
        0,
		pvin);
//    Sequence < ::com::sun::star::beans::PropertyValue >());

	if(!xcomponent.is())
		printf("xcomponent bad");	
	else 
		printf("xcomponent load ok\n");

	Reference< XStorable > rcomponentStore (xcomponent, UNO_QUERY);
	if( !rcomponentStore.is() ){
		printf( "XStorable Not successfully instanciated\n" );
	}else{
		printf( "XStorable successfully instanciated\n" );

		printf("start to save file\n");
		printf("readonly:%d\n", rcomponentStore->isReadonly());
		printf("haslocation:%d\n", rcomponentStore->hasLocation());
		try{
			Sequence < ::com::sun::star::beans::PropertyValue > pv(1);
			pv[0].Name =OUString::createFromAscii("FilterName"); 
			pv[0].Value = Any(OUString::createFromAscii("HTML (Impress)"));
			rcomponentStore->storeAsURL(OUString::createFromAscii("file:///home/smoutou/ppt.ppt.html"),
			//rcomponentStore->storeToURL(OUString::createFromAscii("file:///home/smoutou/ppt.ppt.html"),
			pv);
			//Sequence < ::com::sun::star::beans::PropertyValue >());
		}
		catch(Exception e)
		{
			printf("found a error\n");
			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
			printf( "An error occurred: %s\n", o.pData->buffer );
			xcomponent->dispose();
			return 1;
		}
	}
	xcomponent->dispose();
    return 0;
}
