LCOV - code coverage report
Current view: top level - libreoffice/desktop/source/migration/services - wordbookmigration.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 126 0.0 %
Date: 2012-12-27 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "wordbookmigration.hxx"
      21             : #include <tools/urlobj.hxx>
      22             : #include <unotools/bootstrap.hxx>
      23             : #include <unotools/ucbstreamhelper.hxx>
      24             : 
      25             : using namespace ::com::sun::star;
      26             : using namespace ::com::sun::star::uno;
      27             : 
      28             : 
      29             : //.........................................................................
      30             : namespace migration
      31             : {
      32             : //.........................................................................
      33             : 
      34             : 
      35           0 :     static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
      36           0 :     static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
      37             : 
      38             : 
      39             :     // =============================================================================
      40             :     // component operations
      41             :     // =============================================================================
      42             : 
      43           0 :     ::rtl::OUString WordbookMigration_getImplementationName()
      44             :     {
      45             :         static ::rtl::OUString* pImplName = 0;
      46           0 :         if ( !pImplName )
      47             :         {
      48           0 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      49           0 :             if ( !pImplName )
      50             :             {
      51           0 :                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Wordbooks" ) );
      52           0 :                 pImplName = &aImplName;
      53           0 :             }
      54             :         }
      55           0 :         return *pImplName;
      56             :     }
      57             : 
      58             :     // -----------------------------------------------------------------------------
      59             : 
      60           0 :     Sequence< ::rtl::OUString > WordbookMigration_getSupportedServiceNames()
      61             :     {
      62             :         static Sequence< ::rtl::OUString >* pNames = 0;
      63           0 :         if ( !pNames )
      64             :         {
      65           0 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
      66           0 :             if ( !pNames )
      67             :             {
      68           0 :                 static Sequence< ::rtl::OUString > aNames(1);
      69           0 :                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Wordbooks" ) );
      70           0 :                 pNames = &aNames;
      71           0 :             }
      72             :         }
      73           0 :         return *pNames;
      74             :     }
      75             : 
      76             :     // =============================================================================
      77             :     // WordbookMigration
      78             :     // =============================================================================
      79             : 
      80           0 :     WordbookMigration::WordbookMigration()
      81             :     {
      82           0 :     }
      83             : 
      84             :     // -----------------------------------------------------------------------------
      85             : 
      86           0 :     WordbookMigration::~WordbookMigration()
      87             :     {
      88           0 :     }
      89             : 
      90             :     // -----------------------------------------------------------------------------
      91             : 
      92           0 :     TStringVectorPtr WordbookMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
      93             :     {
      94           0 :         TStringVectorPtr aResult( new TStringVector );
      95           0 :         ::osl::Directory aDir( rBaseURL);
      96             : 
      97           0 :         if ( aDir.open() == ::osl::FileBase::E_None )
      98             :         {
      99             :             // iterate over directory content
     100           0 :             TStringVector aSubDirs;
     101           0 :             ::osl::DirectoryItem aItem;
     102           0 :             while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
     103             :             {
     104           0 :                 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
     105           0 :                 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
     106             :                 {
     107           0 :                     if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
     108           0 :                         aSubDirs.push_back( aFileStatus.getFileURL() );
     109             :                     else
     110           0 :                         aResult->push_back( aFileStatus.getFileURL() );
     111             :                 }
     112           0 :             }
     113             : 
     114             :             // iterate recursive over subfolders
     115           0 :             TStringVector::const_iterator aI = aSubDirs.begin();
     116           0 :             while ( aI != aSubDirs.end() )
     117             :             {
     118           0 :                 TStringVectorPtr aSubResult = getFiles( *aI );
     119           0 :                 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
     120           0 :                 ++aI;
     121           0 :             }
     122             :         }
     123             : 
     124           0 :         return aResult;
     125             :     }
     126             : 
     127             :     // -----------------------------------------------------------------------------
     128             : 
     129           0 :     ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
     130             :     {
     131           0 :         ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
     132           0 :         if ( aResult == ::osl::FileBase::E_NOENT )
     133             :         {
     134           0 :             INetURLObject aBaseURL( rDirURL );
     135           0 :             aBaseURL.removeSegment();
     136           0 :             checkAndCreateDirectory( aBaseURL );
     137           0 :             return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
     138             :         }
     139             :         else
     140             :         {
     141           0 :             return aResult;
     142             :         }
     143             :     }
     144             : 
     145             : #define MAX_HEADER_LENGTH 16
     146           0 : bool IsUserWordbook( const ::rtl::OUString& rFile )
     147             : {
     148             :     static const sal_Char*      pVerStr2    = "WBSWG2";
     149             :     static const sal_Char*      pVerStr5    = "WBSWG5";
     150             :     static const sal_Char*      pVerStr6    = "WBSWG6";
     151             :     static const sal_Char*      pVerOOo7    = "OOoUserDict1";
     152             : 
     153           0 :     bool bRet = false;
     154           0 :     SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( String(rFile), STREAM_STD_READ );
     155           0 :     if ( pStream && !pStream->GetError() )
     156             :     {
     157           0 :         sal_Size nSniffPos = pStream->Tell();
     158           0 :         static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
     159             :         sal_Char pMagicHeader[MAX_HEADER_LENGTH];
     160           0 :         pMagicHeader[ nVerOOo7Len ] = '\0';
     161           0 :         if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
     162             :         {
     163           0 :             if ( !strcmp(pMagicHeader, pVerOOo7) )
     164           0 :                 bRet = true;
     165             :             else
     166             :             {
     167             :                 sal_uInt16 nLen;
     168           0 :                 pStream->Seek (nSniffPos);
     169           0 :                 *pStream >> nLen;
     170           0 :                 if ( nLen < MAX_HEADER_LENGTH )
     171             :                 {
     172           0 :                    pStream->Read(pMagicHeader, nLen);
     173           0 :                    pMagicHeader[nLen] = '\0';
     174           0 :                     if ( !strcmp(pMagicHeader, pVerStr2)
     175           0 :                      ||  !strcmp(pMagicHeader, pVerStr5)
     176           0 :                      ||  !strcmp(pMagicHeader, pVerStr6) )
     177           0 :                     bRet = true;
     178             :                 }
     179             :             }
     180             :         }
     181             :     }
     182             : 
     183           0 :     delete pStream;
     184           0 :     return bRet;
     185             : }
     186             : 
     187             : 
     188             :     // -----------------------------------------------------------------------------
     189             : 
     190           0 :     void WordbookMigration::copyFiles()
     191             :     {
     192           0 :         ::rtl::OUString sTargetDir;
     193           0 :         ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
     194           0 :         if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
     195             :         {
     196           0 :             sTargetDir += sTargetSubDir;
     197           0 :             TStringVectorPtr aFileList = getFiles( m_sSourceDir );
     198           0 :             TStringVector::const_iterator aI = aFileList->begin();
     199           0 :             while ( aI != aFileList->end() )
     200             :             {
     201           0 :                 if (IsUserWordbook(*aI) )
     202             :                 {
     203           0 :                     ::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
     204           0 :                     ::rtl::OUString sTargetName = sTargetDir + sSourceLocalName;
     205           0 :                     INetURLObject aURL( sTargetName );
     206           0 :                     aURL.removeSegment();
     207           0 :                     checkAndCreateDirectory( aURL );
     208           0 :                     ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
     209           0 :                     if ( aResult != ::osl::FileBase::E_None )
     210             :                     {
     211           0 :                         ::rtl::OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
     212           0 :                         aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
     213           0 :                              +  ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
     214           0 :                         OSL_FAIL( aMsg.getStr() );
     215           0 :                     }
     216             :                 }
     217           0 :                 ++aI;
     218           0 :             }
     219             :         }
     220             :         else
     221             :         {
     222             :             OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
     223           0 :         }
     224           0 :     }
     225             : 
     226             :     // -----------------------------------------------------------------------------
     227             :     // XServiceInfo
     228             :     // -----------------------------------------------------------------------------
     229             : 
     230           0 :     ::rtl::OUString WordbookMigration::getImplementationName() throw (RuntimeException)
     231             :     {
     232           0 :         return WordbookMigration_getImplementationName();
     233             :     }
     234             : 
     235             :     // -----------------------------------------------------------------------------
     236             : 
     237           0 :     sal_Bool WordbookMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
     238             :     {
     239           0 :         Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
     240           0 :         const ::rtl::OUString* pNames = aNames.getConstArray();
     241           0 :         const ::rtl::OUString* pEnd = pNames + aNames.getLength();
     242           0 :         for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
     243             :             ;
     244             : 
     245           0 :         return pNames != pEnd;
     246             :     }
     247             : 
     248             :     // -----------------------------------------------------------------------------
     249             : 
     250           0 :     Sequence< ::rtl::OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
     251             :     {
     252           0 :         return WordbookMigration_getSupportedServiceNames();
     253             :     }
     254             : 
     255             :     // -----------------------------------------------------------------------------
     256             :     // XInitialization
     257             :     // -----------------------------------------------------------------------------
     258             : 
     259           0 :     void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
     260             :     {
     261           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     262             : 
     263           0 :         const Any* pIter = aArguments.getConstArray();
     264           0 :         const Any* pEnd = pIter + aArguments.getLength();
     265           0 :         for ( ; pIter != pEnd ; ++pIter )
     266             :         {
     267           0 :             beans::NamedValue aValue;
     268           0 :             *pIter >>= aValue;
     269           0 :             if ( aValue.Name == "UserData" )
     270             :             {
     271           0 :                 if ( !(aValue.Value >>= m_sSourceDir) )
     272             :                 {
     273             :                     OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
     274             :                 }
     275           0 :                 m_sSourceDir += sSourceSubDir;
     276             :                 break;
     277             :             }
     278           0 :         }
     279           0 :     }
     280             : 
     281             :     // -----------------------------------------------------------------------------
     282             :     // XJob
     283             :     // -----------------------------------------------------------------------------
     284             : 
     285           0 :     Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
     286             :         throw (lang::IllegalArgumentException, Exception, RuntimeException)
     287             :     {
     288           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     289             : 
     290           0 :         copyFiles();
     291             : 
     292           0 :         return Any();
     293             :     }
     294             : 
     295             :     // =============================================================================
     296             :     // component operations
     297             :     // =============================================================================
     298             : 
     299           0 :     Reference< XInterface > SAL_CALL WordbookMigration_create(
     300             :         Reference< XComponentContext > const & )
     301             :         SAL_THROW(())
     302             :     {
     303           0 :         return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
     304             :     }
     305             : 
     306             :     // -----------------------------------------------------------------------------
     307             : 
     308             : //.........................................................................
     309           0 : }   // namespace migration
     310             : //.........................................................................
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10