Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / migration / services / basicmigration.cxx
blob94e8677de786552f4c9c72aeefec13ce962102e7
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 "basicmigration.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <tools/urlobj.hxx>
23 #include <unotools/bootstrap.hxx>
24 #include <sal/log.hxx>
25 #include <osl/file.hxx>
26 #include <com/sun/star/uno/XComponentContext.hpp>
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
33 namespace migration
37 #define sSourceUserBasic "/user/basic"
38 #define sTargetUserBasic "/user/__basic_80"
41 // BasicMigration
44 BasicMigration::BasicMigration()
49 BasicMigration::~BasicMigration()
54 TStringVectorPtr BasicMigration::getFiles( const OUString& rBaseURL ) const
56 TStringVectorPtr aResult( new TStringVector );
57 ::osl::Directory aDir( rBaseURL);
59 if ( aDir.open() == ::osl::FileBase::E_None )
61 // iterate over directory content
62 TStringVector aSubDirs;
63 ::osl::DirectoryItem aItem;
64 while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
66 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
67 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
69 if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
70 aSubDirs.push_back( aFileStatus.getFileURL() );
71 else
72 aResult->push_back( aFileStatus.getFileURL() );
76 // iterate recursive over subfolders
77 for (auto const& subDir : aSubDirs)
79 TStringVectorPtr aSubResult = getFiles(subDir);
80 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
84 return aResult;
88 void BasicMigration::checkAndCreateDirectory( INetURLObject const & rDirURL )
90 ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
91 if ( aResult == ::osl::FileBase::E_NOENT )
93 INetURLObject aBaseURL( rDirURL );
94 aBaseURL.removeSegment();
95 checkAndCreateDirectory( aBaseURL );
96 ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
101 void BasicMigration::copyFiles()
103 OUString sTargetDir;
104 ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
105 if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
107 sTargetDir += sTargetUserBasic;
108 TStringVectorPtr aFileList = getFiles( m_sSourceDir );
109 for (auto const& elem : *aFileList)
111 std::u16string_view sLocalName = elem.subView( m_sSourceDir.getLength() );
112 OUString sTargetName = sTargetDir + sLocalName;
113 INetURLObject aURL( sTargetName );
114 aURL.removeSegment();
115 checkAndCreateDirectory( aURL );
116 ::osl::FileBase::RC aResult = ::osl::File::copy( elem, sTargetName );
117 if ( aResult != ::osl::FileBase::E_None )
119 SAL_WARN( "desktop", "BasicMigration::copyFiles: cannot copy "
120 << elem << " to " << sTargetName );
124 else
126 OSL_FAIL( "BasicMigration::copyFiles: no user installation!" );
131 // XServiceInfo
134 OUString BasicMigration::getImplementationName()
136 return "com.sun.star.comp.desktop.migration.Basic";
140 sal_Bool BasicMigration::supportsService(OUString const & ServiceName)
142 return cppu::supportsService(this, ServiceName);
146 Sequence< OUString > BasicMigration::getSupportedServiceNames()
148 return { "com.sun.star.migration.Basic" };
152 // XInitialization
155 void BasicMigration::initialize( const Sequence< Any >& aArguments )
157 ::osl::MutexGuard aGuard( m_aMutex );
159 const Any* pIter = aArguments.getConstArray();
160 const Any* pEnd = pIter + aArguments.getLength();
161 for ( ; pIter != pEnd ; ++pIter )
163 beans::NamedValue aValue;
164 *pIter >>= aValue;
165 if ( aValue.Name == "UserData" )
167 if ( !(aValue.Value >>= m_sSourceDir) )
169 OSL_FAIL( "BasicMigration::initialize: argument UserData has wrong type!" );
171 m_sSourceDir += sSourceUserBasic;
172 break;
178 // XJob
181 Any BasicMigration::execute( const Sequence< beans::NamedValue >& )
183 ::osl::MutexGuard aGuard( m_aMutex );
185 copyFiles();
187 return Any();
191 } // namespace migration
193 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
194 desktop_BasicMigration_get_implementation(
195 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
197 return cppu::acquire(new migration::BasicMigration());
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */