merge the formfield patch from ooo-build
[ooovba.git] / desktop / source / migration / services / wordbookmigration.cxx
blobc5a59964739ac8a72c3f02433ee21fed7947b50b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: wordbookmigration.cxx,v $
10 * $Revision: 1.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
33 #include "wordbookmigration.hxx"
34 #include <tools/urlobj.hxx>
35 #include <unotools/bootstrap.hxx>
36 #include <unotools/ucbstreamhelper.hxx>
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
42 //.........................................................................
43 namespace migration
45 //.........................................................................
48 static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
49 static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
50 static ::rtl::OUString sBaseName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/wordbook" ) );
51 static ::rtl::OUString sSuffix = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".dic" ) );
54 // =============================================================================
55 // component operations
56 // =============================================================================
58 ::rtl::OUString WordbookMigration_getImplementationName()
60 static ::rtl::OUString* pImplName = 0;
61 if ( !pImplName )
63 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
64 if ( !pImplName )
66 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Wordbooks" ) );
67 pImplName = &aImplName;
70 return *pImplName;
73 // -----------------------------------------------------------------------------
75 Sequence< ::rtl::OUString > WordbookMigration_getSupportedServiceNames()
77 static Sequence< ::rtl::OUString >* pNames = 0;
78 if ( !pNames )
80 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
81 if ( !pNames )
83 static Sequence< ::rtl::OUString > aNames(1);
84 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Wordbooks" ) );
85 pNames = &aNames;
88 return *pNames;
91 // =============================================================================
92 // WordbookMigration
93 // =============================================================================
95 WordbookMigration::WordbookMigration()
99 // -----------------------------------------------------------------------------
101 WordbookMigration::~WordbookMigration()
105 // -----------------------------------------------------------------------------
107 TStringVectorPtr WordbookMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
109 TStringVectorPtr aResult( new TStringVector );
110 ::osl::Directory aDir( rBaseURL);
112 if ( aDir.open() == ::osl::FileBase::E_None )
114 // iterate over directory content
115 TStringVector aSubDirs;
116 ::osl::DirectoryItem aItem;
117 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
119 ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
120 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
122 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
123 aSubDirs.push_back( aFileStatus.getFileURL() );
124 else
125 aResult->push_back( aFileStatus.getFileURL() );
129 // iterate recursive over subfolders
130 TStringVector::const_iterator aI = aSubDirs.begin();
131 while ( aI != aSubDirs.end() )
133 TStringVectorPtr aSubResult = getFiles( *aI );
134 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
135 ++aI;
139 return aResult;
142 // -----------------------------------------------------------------------------
144 ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
146 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
147 if ( aResult == ::osl::FileBase::E_NOENT )
149 INetURLObject aBaseURL( rDirURL );
150 aBaseURL.removeSegment();
151 checkAndCreateDirectory( aBaseURL );
152 return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
154 else
156 return aResult;
160 #define MAX_HEADER_LENGTH 16
161 bool IsUserWordbook( const ::rtl::OUString& rFile )
163 static const sal_Char* pVerStr2 = "WBSWG2";
164 static const sal_Char* pVerStr5 = "WBSWG5";
165 static const sal_Char* pVerStr6 = "WBSWG6";
166 static const sal_Char* pVerOOo7 = "OOoUserDict1";
168 bool bRet = false;
169 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( String(rFile), STREAM_STD_READ );
170 if ( pStream && !pStream->GetError() )
172 sal_Size nSniffPos = pStream->Tell();
173 static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
174 sal_Char pMagicHeader[MAX_HEADER_LENGTH];
175 pMagicHeader[ nVerOOo7Len ] = '\0';
176 if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
178 if ( !strcmp(pMagicHeader, pVerOOo7) )
179 bRet = true;
180 else
182 USHORT nLen;
183 pStream->Seek (nSniffPos);
184 *pStream >> nLen;
185 if ( nLen < MAX_HEADER_LENGTH )
187 pStream->Read(pMagicHeader, nLen);
188 pMagicHeader[nLen] = '\0';
189 if ( !strcmp(pMagicHeader, pVerStr2)
190 || !strcmp(pMagicHeader, pVerStr5)
191 || !strcmp(pMagicHeader, pVerStr6) )
192 bRet = true;
198 delete pStream;
199 return bRet;
203 // -----------------------------------------------------------------------------
205 void WordbookMigration::copyFiles()
207 ::rtl::OUString sTargetDir;
208 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
209 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
211 sTargetDir += sTargetSubDir;
212 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
213 TStringVector::const_iterator aI = aFileList->begin();
214 while ( aI != aFileList->end() )
216 if (IsUserWordbook(*aI) )
218 ::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
219 ::rtl::OUString sTargetName = sTargetDir + sSourceLocalName;
220 INetURLObject aURL( sTargetName );
221 aURL.removeSegment();
222 checkAndCreateDirectory( aURL );
223 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
224 if ( aResult != ::osl::FileBase::E_None )
226 ::rtl::OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
227 aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
228 + ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
229 OSL_ENSURE( sal_False, aMsg.getStr() );
232 ++aI;
235 else
237 OSL_ENSURE( sal_False, "WordbookMigration::copyFiles: no user installation!" );
241 // -----------------------------------------------------------------------------
242 // XServiceInfo
243 // -----------------------------------------------------------------------------
245 ::rtl::OUString WordbookMigration::getImplementationName() throw (RuntimeException)
247 return WordbookMigration_getImplementationName();
250 // -----------------------------------------------------------------------------
252 sal_Bool WordbookMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
254 Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
255 const ::rtl::OUString* pNames = aNames.getConstArray();
256 const ::rtl::OUString* pEnd = pNames + aNames.getLength();
257 for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
260 return pNames != pEnd;
263 // -----------------------------------------------------------------------------
265 Sequence< ::rtl::OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
267 return WordbookMigration_getSupportedServiceNames();
270 // -----------------------------------------------------------------------------
271 // XInitialization
272 // -----------------------------------------------------------------------------
274 void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
276 ::osl::MutexGuard aGuard( m_aMutex );
278 const Any* pIter = aArguments.getConstArray();
279 const Any* pEnd = pIter + aArguments.getLength();
280 for ( ; pIter != pEnd ; ++pIter )
282 beans::NamedValue aValue;
283 *pIter >>= aValue;
284 if ( aValue.Name.equalsAscii( "UserData" ) )
286 if ( !(aValue.Value >>= m_sSourceDir) )
288 OSL_ENSURE( false, "WordbookMigration::initialize: argument UserData has wrong type!" );
290 m_sSourceDir += sSourceSubDir;
291 break;
296 // -----------------------------------------------------------------------------
297 // XJob
298 // -----------------------------------------------------------------------------
300 Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
301 throw (lang::IllegalArgumentException, Exception, RuntimeException)
303 ::osl::MutexGuard aGuard( m_aMutex );
305 copyFiles();
307 return Any();
310 // =============================================================================
311 // component operations
312 // =============================================================================
314 Reference< XInterface > SAL_CALL WordbookMigration_create(
315 Reference< XComponentContext > const & )
316 SAL_THROW( () )
318 return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
321 // -----------------------------------------------------------------------------
323 //.........................................................................
324 } // namespace migration
325 //.........................................................................