GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / desktop / source / migration / services / wordbookmigration.cxx
blobb2131fd7c63ba278586961ad1fdefdee44367098
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "wordbookmigration.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <tools/urlobj.hxx>
23 #include <unotools/bootstrap.hxx>
24 #include <unotools/ucbstreamhelper.hxx>
26 using namespace ::com::sun::star;
27 using namespace ::com::sun::star::uno;
30 //.........................................................................
31 namespace migration
33 //.........................................................................
36 static OUString sSourceSubDir( "/user/wordbook" );
37 static OUString sTargetSubDir( "/user/wordbook" );
40 // =============================================================================
41 // component operations
42 // =============================================================================
44 OUString WordbookMigration_getImplementationName()
46 static OUString* pImplName = 0;
47 if ( !pImplName )
49 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
50 if ( !pImplName )
52 static OUString aImplName( "com.sun.star.comp.desktop.migration.Wordbooks" );
53 pImplName = &aImplName;
56 return *pImplName;
59 // -----------------------------------------------------------------------------
61 Sequence< OUString > WordbookMigration_getSupportedServiceNames()
63 static Sequence< OUString >* pNames = 0;
64 if ( !pNames )
66 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
67 if ( !pNames )
69 static Sequence< OUString > aNames(1);
70 aNames.getArray()[0] = "com.sun.star.migration.Wordbooks";
71 pNames = &aNames;
74 return *pNames;
77 // =============================================================================
78 // WordbookMigration
79 // =============================================================================
81 WordbookMigration::WordbookMigration()
85 // -----------------------------------------------------------------------------
87 WordbookMigration::~WordbookMigration()
91 // -----------------------------------------------------------------------------
93 TStringVectorPtr WordbookMigration::getFiles( const OUString& rBaseURL ) const
95 TStringVectorPtr aResult( new TStringVector );
96 ::osl::Directory aDir( rBaseURL);
98 if ( aDir.open() == ::osl::FileBase::E_None )
100 // iterate over directory content
101 TStringVector aSubDirs;
102 ::osl::DirectoryItem aItem;
103 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
105 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
106 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
108 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
109 aSubDirs.push_back( aFileStatus.getFileURL() );
110 else
111 aResult->push_back( aFileStatus.getFileURL() );
115 // iterate recursive over subfolders
116 TStringVector::const_iterator aI = aSubDirs.begin();
117 while ( aI != aSubDirs.end() )
119 TStringVectorPtr aSubResult = getFiles( *aI );
120 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
121 ++aI;
125 return aResult;
128 // -----------------------------------------------------------------------------
130 ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
132 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
133 if ( aResult == ::osl::FileBase::E_NOENT )
135 INetURLObject aBaseURL( rDirURL );
136 aBaseURL.removeSegment();
137 checkAndCreateDirectory( aBaseURL );
138 return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
140 else
142 return aResult;
146 #define MAX_HEADER_LENGTH 16
147 bool IsUserWordbook( const OUString& rFile )
149 static const sal_Char* pVerStr2 = "WBSWG2";
150 static const sal_Char* pVerStr5 = "WBSWG5";
151 static const sal_Char* pVerStr6 = "WBSWG6";
152 static const sal_Char* pVerOOo7 = "OOoUserDict1";
154 bool bRet = false;
155 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( OUString(rFile), STREAM_STD_READ );
156 if ( pStream && !pStream->GetError() )
158 sal_Size nSniffPos = pStream->Tell();
159 static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
160 sal_Char pMagicHeader[MAX_HEADER_LENGTH];
161 pMagicHeader[ nVerOOo7Len ] = '\0';
162 if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
164 if ( !strcmp(pMagicHeader, pVerOOo7) )
165 bRet = true;
166 else
168 sal_uInt16 nLen;
169 pStream->Seek (nSniffPos);
170 *pStream >> nLen;
171 if ( nLen < MAX_HEADER_LENGTH )
173 pStream->Read(pMagicHeader, nLen);
174 pMagicHeader[nLen] = '\0';
175 if ( !strcmp(pMagicHeader, pVerStr2)
176 || !strcmp(pMagicHeader, pVerStr5)
177 || !strcmp(pMagicHeader, pVerStr6) )
178 bRet = true;
184 delete pStream;
185 return bRet;
189 // -----------------------------------------------------------------------------
191 void WordbookMigration::copyFiles()
193 OUString sTargetDir;
194 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
195 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
197 sTargetDir += sTargetSubDir;
198 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
199 TStringVector::const_iterator aI = aFileList->begin();
200 while ( aI != aFileList->end() )
202 if (IsUserWordbook(*aI) )
204 OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
205 OUString sTargetName = sTargetDir + sSourceLocalName;
206 INetURLObject aURL( sTargetName );
207 aURL.removeSegment();
208 checkAndCreateDirectory( aURL );
209 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
210 if ( aResult != ::osl::FileBase::E_None )
212 OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
213 aMsg += OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
214 + OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
215 OSL_FAIL( aMsg.getStr() );
218 ++aI;
221 else
223 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
227 // -----------------------------------------------------------------------------
228 // XServiceInfo
229 // -----------------------------------------------------------------------------
231 OUString WordbookMigration::getImplementationName() throw (RuntimeException)
233 return WordbookMigration_getImplementationName();
236 // -----------------------------------------------------------------------------
238 sal_Bool WordbookMigration::supportsService(OUString const & ServiceName)
239 throw (css::uno::RuntimeException)
241 return cppu::supportsService(this, ServiceName);
244 // -----------------------------------------------------------------------------
246 Sequence< OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
248 return WordbookMigration_getSupportedServiceNames();
251 // -----------------------------------------------------------------------------
252 // XInitialization
253 // -----------------------------------------------------------------------------
255 void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
257 ::osl::MutexGuard aGuard( m_aMutex );
259 const Any* pIter = aArguments.getConstArray();
260 const Any* pEnd = pIter + aArguments.getLength();
261 for ( ; pIter != pEnd ; ++pIter )
263 beans::NamedValue aValue;
264 *pIter >>= aValue;
265 if ( aValue.Name == "UserData" )
267 if ( !(aValue.Value >>= m_sSourceDir) )
269 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
271 m_sSourceDir += sSourceSubDir;
272 break;
277 // -----------------------------------------------------------------------------
278 // XJob
279 // -----------------------------------------------------------------------------
281 Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
282 throw (lang::IllegalArgumentException, Exception, RuntimeException)
284 ::osl::MutexGuard aGuard( m_aMutex );
286 copyFiles();
288 return Any();
291 // =============================================================================
292 // component operations
293 // =============================================================================
295 Reference< XInterface > SAL_CALL WordbookMigration_create(
296 Reference< XComponentContext > const & )
297 SAL_THROW(())
299 return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
302 // -----------------------------------------------------------------------------
304 //.........................................................................
305 } // namespace migration
306 //.........................................................................
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */