Bump version to 6.0-36
[LibreOffice.git] / desktop / source / migration / services / wordbookmigration.cxx
blob1830c65a362c660ebfa251a0d4d2bdda6e1fe763
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 namespace migration
32 // component operations
35 OUString WordbookMigration_getImplementationName()
37 return OUString( "com.sun.star.comp.desktop.migration.Wordbooks" );
41 Sequence< OUString > WordbookMigration_getSupportedServiceNames()
43 return Sequence< OUString > { "com.sun.star.migration.Wordbooks" };
47 // WordbookMigration
50 WordbookMigration::WordbookMigration()
55 WordbookMigration::~WordbookMigration()
60 TStringVectorPtr WordbookMigration::getFiles( const OUString& rBaseURL ) const
62 TStringVectorPtr aResult( new TStringVector );
63 ::osl::Directory aDir( rBaseURL);
65 if ( aDir.open() == ::osl::FileBase::E_None )
67 // iterate over directory content
68 TStringVector aSubDirs;
69 ::osl::DirectoryItem aItem;
70 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
72 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
73 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
75 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
76 aSubDirs.push_back( aFileStatus.getFileURL() );
77 else
78 aResult->push_back( aFileStatus.getFileURL() );
82 // iterate recursive over subfolders
83 TStringVector::const_iterator aI = aSubDirs.begin();
84 while ( aI != aSubDirs.end() )
86 TStringVectorPtr aSubResult = getFiles( *aI );
87 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
88 ++aI;
92 return aResult;
96 void WordbookMigration::checkAndCreateDirectory( INetURLObject const & rDirURL )
98 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
99 if ( aResult == ::osl::FileBase::E_NOENT )
101 INetURLObject aBaseURL( rDirURL );
102 aBaseURL.removeSegment();
103 checkAndCreateDirectory( aBaseURL );
104 ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
108 #define MAX_HEADER_LENGTH 16
109 bool IsUserWordbook( const OUString& rFile )
111 bool bRet = false;
112 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( rFile, StreamMode::STD_READ );
113 if ( pStream && !pStream->GetError() )
115 static const sal_Char* const pVerOOo7 = "OOoUserDict1";
116 sal_uInt64 const nSniffPos = pStream->Tell();
117 static std::size_t nVerOOo7Len = sal::static_int_cast< std::size_t >(strlen( pVerOOo7 ));
118 sal_Char pMagicHeader[MAX_HEADER_LENGTH];
119 pMagicHeader[ nVerOOo7Len ] = '\0';
120 if (pStream->ReadBytes(static_cast<void *>(pMagicHeader), nVerOOo7Len) == nVerOOo7Len)
122 if ( !strcmp(pMagicHeader, pVerOOo7) )
123 bRet = true;
124 else
126 sal_uInt16 nLen;
127 pStream->Seek (nSniffPos);
128 pStream->ReadUInt16( nLen );
129 if ( nLen < MAX_HEADER_LENGTH )
131 pStream->ReadBytes(pMagicHeader, nLen);
132 pMagicHeader[nLen] = '\0';
133 if ( !strcmp(pMagicHeader, "WBSWG2")
134 || !strcmp(pMagicHeader, "WBSWG5")
135 || !strcmp(pMagicHeader, "WBSWG6") )
136 bRet = true;
142 delete pStream;
143 return bRet;
147 void WordbookMigration::copyFiles()
149 OUString sTargetDir;
150 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
151 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
153 sTargetDir += "/user/wordbook";
154 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
155 TStringVector::const_iterator aI = aFileList->begin();
156 while ( aI != aFileList->end() )
158 if (IsUserWordbook(*aI) )
160 OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
161 OUString sTargetName = sTargetDir + sSourceLocalName;
162 INetURLObject aURL( sTargetName );
163 aURL.removeSegment();
164 checkAndCreateDirectory( aURL );
165 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
166 if ( aResult != ::osl::FileBase::E_None )
168 SAL_WARN( "desktop", "WordbookMigration::copyFiles: cannot copy "
169 << *aI << " to " << sTargetName);
172 ++aI;
175 else
177 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
182 // XServiceInfo
185 OUString WordbookMigration::getImplementationName()
187 return WordbookMigration_getImplementationName();
191 sal_Bool WordbookMigration::supportsService(OUString const & ServiceName)
193 return cppu::supportsService(this, ServiceName);
197 Sequence< OUString > WordbookMigration::getSupportedServiceNames()
199 return WordbookMigration_getSupportedServiceNames();
203 // XInitialization
206 void WordbookMigration::initialize( const Sequence< Any >& aArguments )
208 ::osl::MutexGuard aGuard( m_aMutex );
210 const Any* pIter = aArguments.getConstArray();
211 const Any* pEnd = pIter + aArguments.getLength();
212 for ( ; pIter != pEnd ; ++pIter )
214 beans::NamedValue aValue;
215 *pIter >>= aValue;
216 if ( aValue.Name == "UserData" )
218 if ( !(aValue.Value >>= m_sSourceDir) )
220 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
222 m_sSourceDir += "/user/wordbook";
223 break;
229 // XJob
232 Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
234 ::osl::MutexGuard aGuard( m_aMutex );
236 copyFiles();
238 return Any();
242 // component operations
245 Reference< XInterface > SAL_CALL WordbookMigration_create(
246 Reference< XComponentContext > const & )
248 return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
252 } // namespace migration
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */