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