Update ooo320-m1
[ooovba.git] / desktop / source / migration / services / autocorrmigration.cxx
blobb480a241d06fa05ad9814b76012f95c8cbb60626
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: autocorrmigration.cxx,v $
10 * $Revision: 1.7 $
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 "autocorrmigration.hxx"
34 #include <i18npool/mslangid.hxx>
35 #include <tools/urlobj.hxx>
36 #include <unotools/bootstrap.hxx>
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
43 //.........................................................................
44 namespace migration
46 //.........................................................................
49 static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/autocorr" ) );
50 static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/autocorr" ) );
51 static ::rtl::OUString sBaseName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/acor" ) );
52 static ::rtl::OUString sSuffix = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".dat" ) );
55 // =============================================================================
56 // component operations
57 // =============================================================================
59 ::rtl::OUString AutocorrectionMigration_getImplementationName()
61 static ::rtl::OUString* pImplName = 0;
62 if ( !pImplName )
64 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
65 if ( !pImplName )
67 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Autocorrection" ) );
68 pImplName = &aImplName;
71 return *pImplName;
74 // -----------------------------------------------------------------------------
76 Sequence< ::rtl::OUString > AutocorrectionMigration_getSupportedServiceNames()
78 static Sequence< ::rtl::OUString >* pNames = 0;
79 if ( !pNames )
81 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
82 if ( !pNames )
84 static Sequence< ::rtl::OUString > aNames(1);
85 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Autocorrection" ) );
86 pNames = &aNames;
89 return *pNames;
92 // =============================================================================
93 // AutocorrectionMigration
94 // =============================================================================
96 AutocorrectionMigration::AutocorrectionMigration()
100 // -----------------------------------------------------------------------------
102 AutocorrectionMigration::~AutocorrectionMigration()
106 // -----------------------------------------------------------------------------
108 TStringVectorPtr AutocorrectionMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
110 TStringVectorPtr aResult( new TStringVector );
111 ::osl::Directory aDir( rBaseURL);
113 if ( aDir.open() == ::osl::FileBase::E_None )
115 // iterate over directory content
116 TStringVector aSubDirs;
117 ::osl::DirectoryItem aItem;
118 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
120 ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
121 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
123 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
124 aSubDirs.push_back( aFileStatus.getFileURL() );
125 else
126 aResult->push_back( aFileStatus.getFileURL() );
130 // iterate recursive over subfolders
131 TStringVector::const_iterator aI = aSubDirs.begin();
132 while ( aI != aSubDirs.end() )
134 TStringVectorPtr aSubResult = getFiles( *aI );
135 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
136 ++aI;
140 return aResult;
143 // -----------------------------------------------------------------------------
145 ::osl::FileBase::RC AutocorrectionMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
147 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
148 if ( aResult == ::osl::FileBase::E_NOENT )
150 INetURLObject aBaseURL( rDirURL );
151 aBaseURL.removeSegment();
152 checkAndCreateDirectory( aBaseURL );
153 return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
155 else
157 return aResult;
161 // -----------------------------------------------------------------------------
163 void AutocorrectionMigration::copyFiles()
165 ::rtl::OUString sTargetDir;
166 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
167 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
169 sTargetDir += sTargetSubDir;
170 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
171 TStringVector::const_iterator aI = aFileList->begin();
172 while ( aI != aFileList->end() )
174 ::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
175 sal_Int32 nStart = sBaseName.getLength();
176 sal_Int32 nEnd = sSourceLocalName.lastIndexOf ( sSuffix );
177 ::rtl::OUString sLanguageType = sSourceLocalName.copy( nStart, nEnd - nStart );
178 ::rtl::OUString sIsoName = MsLangId::convertLanguageToIsoString( (LanguageType) sLanguageType.toInt32() );
179 ::rtl::OUString sTargetLocalName = sBaseName;
180 sTargetLocalName += ::rtl::OUString::createFromAscii( "_" );
181 sTargetLocalName += sIsoName;
182 sTargetLocalName += sSuffix;
183 ::rtl::OUString sTargetName = sTargetDir + sTargetLocalName;
184 INetURLObject aURL( sTargetName );
185 aURL.removeSegment();
186 checkAndCreateDirectory( aURL );
187 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
188 if ( aResult != ::osl::FileBase::E_None )
190 ::rtl::OString aMsg( "AutocorrectionMigration::copyFiles: cannot copy " );
191 aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
192 + ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
193 OSL_ENSURE( sal_False, aMsg.getStr() );
195 ++aI;
198 else
200 OSL_ENSURE( sal_False, "AutocorrectionMigration::copyFiles: no user installation!" );
204 // -----------------------------------------------------------------------------
205 // XServiceInfo
206 // -----------------------------------------------------------------------------
208 ::rtl::OUString AutocorrectionMigration::getImplementationName() throw (RuntimeException)
210 return AutocorrectionMigration_getImplementationName();
213 // -----------------------------------------------------------------------------
215 sal_Bool AutocorrectionMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
217 Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
218 const ::rtl::OUString* pNames = aNames.getConstArray();
219 const ::rtl::OUString* pEnd = pNames + aNames.getLength();
220 for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
223 return pNames != pEnd;
226 // -----------------------------------------------------------------------------
228 Sequence< ::rtl::OUString > AutocorrectionMigration::getSupportedServiceNames() throw (RuntimeException)
230 return AutocorrectionMigration_getSupportedServiceNames();
233 // -----------------------------------------------------------------------------
234 // XInitialization
235 // -----------------------------------------------------------------------------
237 void AutocorrectionMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
239 ::osl::MutexGuard aGuard( m_aMutex );
241 const Any* pIter = aArguments.getConstArray();
242 const Any* pEnd = pIter + aArguments.getLength();
243 for ( ; pIter != pEnd ; ++pIter )
245 beans::NamedValue aValue;
246 *pIter >>= aValue;
247 if ( aValue.Name.equalsAscii( "UserData" ) )
249 if ( !(aValue.Value >>= m_sSourceDir) )
251 OSL_ENSURE( false, "AutocorrectionMigration::initialize: argument UserData has wrong type!" );
253 m_sSourceDir += sSourceSubDir;
254 break;
259 // -----------------------------------------------------------------------------
260 // XJob
261 // -----------------------------------------------------------------------------
263 Any AutocorrectionMigration::execute( const Sequence< beans::NamedValue >& )
264 throw (lang::IllegalArgumentException, Exception, RuntimeException)
266 ::osl::MutexGuard aGuard( m_aMutex );
268 copyFiles();
270 return Any();
273 // =============================================================================
274 // component operations
275 // =============================================================================
277 Reference< XInterface > SAL_CALL AutocorrectionMigration_create(
278 Reference< XComponentContext > const & )
279 SAL_THROW( () )
281 return static_cast< lang::XTypeProvider * >( new AutocorrectionMigration() );
284 // -----------------------------------------------------------------------------
286 //.........................................................................
287 } // namespace migration
288 //.........................................................................