1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2722ceddSAndrew Rist  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19*2722ceddSAndrew Rist  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir #include "autocorrmigration.hxx"
27cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
28cdf0e10cSrcweir #include <tools/urlobj.hxx>
29cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir using namespace ::com::sun::star::uno;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //.........................................................................
37cdf0e10cSrcweir namespace migration
38cdf0e10cSrcweir {
39cdf0e10cSrcweir //.........................................................................
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir     static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/autocorr" ) );
43cdf0e10cSrcweir     static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/autocorr" ) );
44cdf0e10cSrcweir     static ::rtl::OUString sBaseName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/acor" ) );
45cdf0e10cSrcweir     static ::rtl::OUString sSuffix = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".dat" ) );
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     // =============================================================================
49cdf0e10cSrcweir     // component operations
50cdf0e10cSrcweir     // =============================================================================
51cdf0e10cSrcweir 
AutocorrectionMigration_getImplementationName()52cdf0e10cSrcweir     ::rtl::OUString AutocorrectionMigration_getImplementationName()
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         static ::rtl::OUString* pImplName = 0;
55cdf0e10cSrcweir 	    if ( !pImplName )
56cdf0e10cSrcweir 	    {
57cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
58cdf0e10cSrcweir             if ( !pImplName )
59cdf0e10cSrcweir 		    {
60cdf0e10cSrcweir                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Autocorrection" ) );
61cdf0e10cSrcweir 			    pImplName = &aImplName;
62cdf0e10cSrcweir 		    }
63cdf0e10cSrcweir 	    }
64cdf0e10cSrcweir 	    return *pImplName;
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     // -----------------------------------------------------------------------------
68cdf0e10cSrcweir 
AutocorrectionMigration_getSupportedServiceNames()69cdf0e10cSrcweir     Sequence< ::rtl::OUString > AutocorrectionMigration_getSupportedServiceNames()
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         static Sequence< ::rtl::OUString >* pNames = 0;
72cdf0e10cSrcweir 	    if ( !pNames )
73cdf0e10cSrcweir 	    {
74cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
75cdf0e10cSrcweir 		    if ( !pNames )
76cdf0e10cSrcweir 		    {
77cdf0e10cSrcweir                 static Sequence< ::rtl::OUString > aNames(1);
78cdf0e10cSrcweir                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Autocorrection" ) );
79cdf0e10cSrcweir                 pNames = &aNames;
80cdf0e10cSrcweir 		    }
81cdf0e10cSrcweir 	    }
82cdf0e10cSrcweir 	    return *pNames;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     // =============================================================================
86cdf0e10cSrcweir     // AutocorrectionMigration
87cdf0e10cSrcweir     // =============================================================================
88cdf0e10cSrcweir 
AutocorrectionMigration()89cdf0e10cSrcweir     AutocorrectionMigration::AutocorrectionMigration()
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // -----------------------------------------------------------------------------
94cdf0e10cSrcweir 
~AutocorrectionMigration()95cdf0e10cSrcweir     AutocorrectionMigration::~AutocorrectionMigration()
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     // -----------------------------------------------------------------------------
100cdf0e10cSrcweir 
getFiles(const::rtl::OUString & rBaseURL) const101cdf0e10cSrcweir     TStringVectorPtr AutocorrectionMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         TStringVectorPtr aResult( new TStringVector );
104cdf0e10cSrcweir         ::osl::Directory aDir( rBaseURL);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         if ( aDir.open() == ::osl::FileBase::E_None )
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             // iterate over directory content
109cdf0e10cSrcweir             TStringVector aSubDirs;
110cdf0e10cSrcweir             ::osl::DirectoryItem aItem;
111cdf0e10cSrcweir             while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
112cdf0e10cSrcweir             {
113cdf0e10cSrcweir                 ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
114cdf0e10cSrcweir                 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
115cdf0e10cSrcweir                 {
116cdf0e10cSrcweir                     if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
117cdf0e10cSrcweir                         aSubDirs.push_back( aFileStatus.getFileURL() );
118cdf0e10cSrcweir                     else
119cdf0e10cSrcweir                         aResult->push_back( aFileStatus.getFileURL() );
120cdf0e10cSrcweir                 }
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir             // iterate recursive over subfolders
124cdf0e10cSrcweir             TStringVector::const_iterator aI = aSubDirs.begin();
125cdf0e10cSrcweir             while ( aI != aSubDirs.end() )
126cdf0e10cSrcweir             {
127cdf0e10cSrcweir                 TStringVectorPtr aSubResult = getFiles( *aI );
128cdf0e10cSrcweir                 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
129cdf0e10cSrcweir                 ++aI;
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         return aResult;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     // -----------------------------------------------------------------------------
137cdf0e10cSrcweir 
checkAndCreateDirectory(INetURLObject & rDirURL)138cdf0e10cSrcweir     ::osl::FileBase::RC AutocorrectionMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
141cdf0e10cSrcweir         if ( aResult == ::osl::FileBase::E_NOENT )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             INetURLObject aBaseURL( rDirURL );
144cdf0e10cSrcweir             aBaseURL.removeSegment();
145cdf0e10cSrcweir             checkAndCreateDirectory( aBaseURL );
146cdf0e10cSrcweir             return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir         else
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             return aResult;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     // -----------------------------------------------------------------------------
155cdf0e10cSrcweir 
copyFiles()156cdf0e10cSrcweir     void AutocorrectionMigration::copyFiles()
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         ::rtl::OUString sTargetDir;
159cdf0e10cSrcweir         ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
160cdf0e10cSrcweir         if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             sTargetDir += sTargetSubDir;
163cdf0e10cSrcweir             TStringVectorPtr aFileList = getFiles( m_sSourceDir );
164cdf0e10cSrcweir             TStringVector::const_iterator aI = aFileList->begin();
165cdf0e10cSrcweir             while ( aI != aFileList->end() )
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 ::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
168cdf0e10cSrcweir                 sal_Int32 nStart = sBaseName.getLength();
169cdf0e10cSrcweir                 sal_Int32 nEnd = sSourceLocalName.lastIndexOf ( sSuffix );
170cdf0e10cSrcweir                 ::rtl::OUString sLanguageType = sSourceLocalName.copy( nStart, nEnd - nStart );
171cdf0e10cSrcweir                 ::rtl::OUString sIsoName = MsLangId::convertLanguageToIsoString( (LanguageType) sLanguageType.toInt32() );
172cdf0e10cSrcweir                 ::rtl::OUString sTargetLocalName = sBaseName;
173cdf0e10cSrcweir                 sTargetLocalName += ::rtl::OUString::createFromAscii( "_" );
174cdf0e10cSrcweir                 sTargetLocalName += sIsoName;
175cdf0e10cSrcweir                 sTargetLocalName += sSuffix;
176cdf0e10cSrcweir                 ::rtl::OUString sTargetName = sTargetDir + sTargetLocalName;
177cdf0e10cSrcweir                 INetURLObject aURL( sTargetName );
178cdf0e10cSrcweir                 aURL.removeSegment();
179cdf0e10cSrcweir                 checkAndCreateDirectory( aURL );
180cdf0e10cSrcweir                 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
181cdf0e10cSrcweir                 if ( aResult != ::osl::FileBase::E_None )
182cdf0e10cSrcweir                 {
183cdf0e10cSrcweir                     ::rtl::OString aMsg( "AutocorrectionMigration::copyFiles: cannot copy " );
184cdf0e10cSrcweir                     aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
185cdf0e10cSrcweir                          +  ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
186cdf0e10cSrcweir                     OSL_ENSURE( sal_False, aMsg.getStr() );
187cdf0e10cSrcweir                 }
188cdf0e10cSrcweir                 ++aI;
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         else
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             OSL_ENSURE( sal_False, "AutocorrectionMigration::copyFiles: no user installation!" );
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     // -----------------------------------------------------------------------------
198cdf0e10cSrcweir     // XServiceInfo
199cdf0e10cSrcweir     // -----------------------------------------------------------------------------
200cdf0e10cSrcweir 
getImplementationName()201cdf0e10cSrcweir     ::rtl::OUString AutocorrectionMigration::getImplementationName() throw (RuntimeException)
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         return AutocorrectionMigration_getImplementationName();
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     // -----------------------------------------------------------------------------
207cdf0e10cSrcweir 
supportsService(const::rtl::OUString & rServiceName)208cdf0e10cSrcweir     sal_Bool AutocorrectionMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir 	    Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
211cdf0e10cSrcweir 	    const ::rtl::OUString* pNames = aNames.getConstArray();
212cdf0e10cSrcweir 	    const ::rtl::OUString* pEnd = pNames + aNames.getLength();
213cdf0e10cSrcweir 	    for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
214cdf0e10cSrcweir 		    ;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	    return pNames != pEnd;
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     // -----------------------------------------------------------------------------
220cdf0e10cSrcweir 
getSupportedServiceNames()221cdf0e10cSrcweir     Sequence< ::rtl::OUString > AutocorrectionMigration::getSupportedServiceNames() throw (RuntimeException)
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         return AutocorrectionMigration_getSupportedServiceNames();
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     // -----------------------------------------------------------------------------
227cdf0e10cSrcweir     // XInitialization
228cdf0e10cSrcweir     // -----------------------------------------------------------------------------
229cdf0e10cSrcweir 
initialize(const Sequence<Any> & aArguments)230cdf0e10cSrcweir     void AutocorrectionMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         const Any* pIter = aArguments.getConstArray();
235cdf0e10cSrcweir         const Any* pEnd = pIter + aArguments.getLength();
236cdf0e10cSrcweir         for ( ; pIter != pEnd ; ++pIter )
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             beans::NamedValue aValue;
239cdf0e10cSrcweir             *pIter >>= aValue;
240cdf0e10cSrcweir             if ( aValue.Name.equalsAscii( "UserData" ) )
241cdf0e10cSrcweir             {
242cdf0e10cSrcweir                 if ( !(aValue.Value >>= m_sSourceDir) )
243cdf0e10cSrcweir                 {
244cdf0e10cSrcweir                     OSL_ENSURE( false, "AutocorrectionMigration::initialize: argument UserData has wrong type!" );
245cdf0e10cSrcweir                 }
246cdf0e10cSrcweir                 m_sSourceDir += sSourceSubDir;
247cdf0e10cSrcweir                 break;
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     // -----------------------------------------------------------------------------
253cdf0e10cSrcweir     // XJob
254cdf0e10cSrcweir     // -----------------------------------------------------------------------------
255cdf0e10cSrcweir 
execute(const Sequence<beans::NamedValue> &)256cdf0e10cSrcweir     Any AutocorrectionMigration::execute( const Sequence< beans::NamedValue >& )
257cdf0e10cSrcweir         throw (lang::IllegalArgumentException, Exception, RuntimeException)
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         copyFiles();
262cdf0e10cSrcweir 
263cdf0e10cSrcweir         return Any();
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     // =============================================================================
267cdf0e10cSrcweir     // component operations
268cdf0e10cSrcweir     // =============================================================================
269cdf0e10cSrcweir 
AutocorrectionMigration_create(Reference<XComponentContext> const &)270cdf0e10cSrcweir     Reference< XInterface > SAL_CALL AutocorrectionMigration_create(
271cdf0e10cSrcweir         Reference< XComponentContext > const & )
272cdf0e10cSrcweir         SAL_THROW( () )
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         return static_cast< lang::XTypeProvider * >( new AutocorrectionMigration() );
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     // -----------------------------------------------------------------------------
278cdf0e10cSrcweir 
279cdf0e10cSrcweir //.........................................................................
280cdf0e10cSrcweir }	// namespace migration
281cdf0e10cSrcweir //.........................................................................
282