lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / desktop / source / migration / services / wordbookmigration.cxx
blobecc1afd567f3758bfad04173a483e9b398c514c1
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>
25 #include <sal/log.hxx>
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::uno;
31 namespace migration
33 // component operations
36 OUString WordbookMigration_getImplementationName()
38 return "com.sun.star.comp.desktop.migration.Wordbooks";
42 Sequence< OUString > WordbookMigration_getSupportedServiceNames()
44 return { "com.sun.star.migration.Wordbooks" };
48 // WordbookMigration
51 WordbookMigration::WordbookMigration()
56 WordbookMigration::~WordbookMigration()
61 TStringVectorPtr WordbookMigration::getFiles( const OUString& rBaseURL ) const
63 TStringVectorPtr aResult( new TStringVector );
64 ::osl::Directory aDir( rBaseURL);
66 if ( aDir.open() == ::osl::FileBase::E_None )
68 // iterate over directory content
69 TStringVector aSubDirs;
70 ::osl::DirectoryItem aItem;
71 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
73 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
74 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
76 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
77 aSubDirs.push_back( aFileStatus.getFileURL() );
78 else
79 aResult->push_back( aFileStatus.getFileURL() );
83 // iterate recursive over subfolders
84 for (auto const& subDir : aSubDirs)
86 TStringVectorPtr aSubResult = getFiles(subDir);
87 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
91 return aResult;
95 void WordbookMigration::checkAndCreateDirectory( INetURLObject const & rDirURL )
97 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
98 if ( aResult == ::osl::FileBase::E_NOENT )
100 INetURLObject aBaseURL( rDirURL );
101 aBaseURL.removeSegment();
102 checkAndCreateDirectory( aBaseURL );
103 ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
107 #define MAX_HEADER_LENGTH 16
108 static bool IsUserWordbook( const OUString& rFile )
110 bool bRet = false;
111 std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( rFile, StreamMode::STD_READ );
112 if ( pStream && !pStream->GetError() )
114 static const sal_Char* const pVerOOo7 = "OOoUserDict1";
115 sal_uInt64 const nSniffPos = pStream->Tell();
116 static std::size_t nVerOOo7Len = sal::static_int_cast< std::size_t >(strlen( pVerOOo7 ));
117 sal_Char pMagicHeader[MAX_HEADER_LENGTH];
118 pMagicHeader[ nVerOOo7Len ] = '\0';
119 if (pStream->ReadBytes(static_cast<void *>(pMagicHeader), nVerOOo7Len) == nVerOOo7Len)
121 if ( !strcmp(pMagicHeader, pVerOOo7) )
122 bRet = true;
123 else
125 sal_uInt16 nLen;
126 pStream->Seek (nSniffPos);
127 pStream->ReadUInt16( nLen );
128 if ( nLen < MAX_HEADER_LENGTH )
130 pStream->ReadBytes(pMagicHeader, nLen);
131 pMagicHeader[nLen] = '\0';
132 if ( !strcmp(pMagicHeader, "WBSWG2")
133 || !strcmp(pMagicHeader, "WBSWG5")
134 || !strcmp(pMagicHeader, "WBSWG6") )
135 bRet = true;
141 return bRet;
145 void WordbookMigration::copyFiles()
147 OUString sTargetDir;
148 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
149 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
151 sTargetDir += "/user/wordbook";
152 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
153 for (auto const& elem : *aFileList)
155 if (IsUserWordbook(elem) )
157 OUString sSourceLocalName = elem.copy( m_sSourceDir.getLength() );
158 OUString sTargetName = sTargetDir + sSourceLocalName;
159 INetURLObject aURL( sTargetName );
160 aURL.removeSegment();
161 checkAndCreateDirectory( aURL );
162 ::osl::FileBase::RC aResult = ::osl::File::copy( elem, sTargetName );
163 if ( aResult != ::osl::FileBase::E_None )
165 SAL_WARN( "desktop", "WordbookMigration::copyFiles: cannot copy "
166 << elem << " to " << sTargetName);
171 else
173 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
178 // XServiceInfo
181 OUString WordbookMigration::getImplementationName()
183 return WordbookMigration_getImplementationName();
187 sal_Bool WordbookMigration::supportsService(OUString const & ServiceName)
189 return cppu::supportsService(this, ServiceName);
193 Sequence< OUString > WordbookMigration::getSupportedServiceNames()
195 return WordbookMigration_getSupportedServiceNames();
199 // XInitialization
202 void WordbookMigration::initialize( const Sequence< Any >& aArguments )
204 ::osl::MutexGuard aGuard( m_aMutex );
206 const Any* pIter = aArguments.getConstArray();
207 const Any* pEnd = pIter + aArguments.getLength();
208 for ( ; pIter != pEnd ; ++pIter )
210 beans::NamedValue aValue;
211 *pIter >>= aValue;
212 if ( aValue.Name == "UserData" )
214 if ( !(aValue.Value >>= m_sSourceDir) )
216 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
218 m_sSourceDir += "/user/wordbook";
219 break;
225 // XJob
228 Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
230 ::osl::MutexGuard aGuard( m_aMutex );
232 copyFiles();
234 return Any();
238 // component operations
241 Reference< XInterface > WordbookMigration_create(
242 Reference< XComponentContext > const & )
244 return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
248 } // namespace migration
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */