1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/diagnose.h>
26 #include <osl/file.hxx>
27 #include <com/sun/star/uno/XComponentContext.hpp>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
38 #define sSourceUserBasic "/user/basic"
39 #define sTargetUserBasic "/user/__basic_80"
45 BasicMigration::BasicMigration()
50 BasicMigration::~BasicMigration()
55 TStringVectorPtr
BasicMigration::getFiles( const OUString
& rBaseURL
) const
57 TStringVectorPtr
aResult( new TStringVector
);
58 ::osl::Directory
aDir( rBaseURL
);
60 if ( aDir
.open() == ::osl::FileBase::E_None
)
62 // iterate over directory content
63 TStringVector aSubDirs
;
64 ::osl::DirectoryItem aItem
;
65 while ( aDir
.getNextItem( aItem
) == ::osl::FileBase::E_None
)
67 ::osl::FileStatus
aFileStatus( osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
68 if ( aItem
.getFileStatus( aFileStatus
) == ::osl::FileBase::E_None
)
70 if ( aFileStatus
.getFileType() == ::osl::FileStatus::Directory
)
71 aSubDirs
.push_back( aFileStatus
.getFileURL() );
73 aResult
->push_back( aFileStatus
.getFileURL() );
77 // iterate recursive over subfolders
78 for (auto const& subDir
: aSubDirs
)
80 TStringVectorPtr aSubResult
= getFiles(subDir
);
81 aResult
->insert( aResult
->end(), aSubResult
->begin(), aSubResult
->end() );
89 void BasicMigration::checkAndCreateDirectory( INetURLObject
const & rDirURL
)
91 ::osl::FileBase::RC aResult
= ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
92 if ( aResult
== ::osl::FileBase::E_NOENT
)
94 INetURLObject
aBaseURL( rDirURL
);
95 aBaseURL
.removeSegment();
96 checkAndCreateDirectory( aBaseURL
);
97 ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
102 void BasicMigration::copyFiles()
105 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( sTargetDir
);
106 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
108 sTargetDir
+= sTargetUserBasic
;
109 TStringVectorPtr aFileList
= getFiles( m_sSourceDir
);
110 for (auto const& elem
: *aFileList
)
112 std::u16string_view sLocalName
= elem
.subView( m_sSourceDir
.getLength() );
113 OUString sTargetName
= sTargetDir
+ sLocalName
;
114 INetURLObject
aURL( sTargetName
);
115 aURL
.removeSegment();
116 checkAndCreateDirectory( aURL
);
117 ::osl::FileBase::RC aResult
= ::osl::File::copy( elem
, sTargetName
);
118 if ( aResult
!= ::osl::FileBase::E_None
)
120 SAL_WARN( "desktop", "BasicMigration::copyFiles: cannot copy "
121 << elem
<< " to " << sTargetName
);
127 OSL_FAIL( "BasicMigration::copyFiles: no user installation!" );
135 OUString
BasicMigration::getImplementationName()
137 return u
"com.sun.star.comp.desktop.migration.Basic"_ustr
;
141 sal_Bool
BasicMigration::supportsService(OUString
const & ServiceName
)
143 return cppu::supportsService(this, ServiceName
);
147 Sequence
< OUString
> BasicMigration::getSupportedServiceNames()
149 return { u
"com.sun.star.migration.Basic"_ustr
};
156 void BasicMigration::initialize( const Sequence
< Any
>& aArguments
)
158 ::osl::MutexGuard
aGuard( m_aMutex
);
160 const Any
* pIter
= aArguments
.getConstArray();
161 const Any
* pEnd
= pIter
+ aArguments
.getLength();
162 for ( ; pIter
!= pEnd
; ++pIter
)
164 beans::NamedValue aValue
;
166 if ( aValue
.Name
== "UserData" )
168 if ( !(aValue
.Value
>>= m_sSourceDir
) )
170 OSL_FAIL( "BasicMigration::initialize: argument UserData has wrong type!" );
172 m_sSourceDir
+= sSourceUserBasic
;
182 Any
BasicMigration::execute( const Sequence
< beans::NamedValue
>& )
184 ::osl::MutexGuard
aGuard( m_aMutex
);
192 } // namespace migration
194 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
195 desktop_BasicMigration_get_implementation(
196 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
198 return cppu::acquire(new migration::BasicMigration());
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */