1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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 
26 
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::uno;
29 
30 
31 namespace migration
32 {
33 
34 
35     #define sSourceUserBasic "/user/basic"
36     #define sTargetUserBasic "/user/__basic_80"
37 
38 
39     // component operations
40 
41 
BasicMigration_getImplementationName()42     OUString BasicMigration_getImplementationName()
43     {
44         return "com.sun.star.comp.desktop.migration.Basic";
45     }
46 
47 
BasicMigration_getSupportedServiceNames()48     Sequence< OUString > BasicMigration_getSupportedServiceNames()
49     {
50         Sequence< OUString > aNames { "com.sun.star.migration.Basic" };
51         return aNames;
52     }
53 
54 
55     // BasicMigration
56 
57 
BasicMigration()58     BasicMigration::BasicMigration()
59     {
60     }
61 
62 
~BasicMigration()63     BasicMigration::~BasicMigration()
64     {
65     }
66 
67 
getFiles(const OUString & rBaseURL) const68     TStringVectorPtr BasicMigration::getFiles( const OUString& rBaseURL ) const
69     {
70         TStringVectorPtr aResult( new TStringVector );
71         ::osl::Directory aDir( rBaseURL);
72 
73         if ( aDir.open() == ::osl::FileBase::E_None )
74         {
75             // iterate over directory content
76             TStringVector aSubDirs;
77             ::osl::DirectoryItem aItem;
78             while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
79             {
80                 ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
81                 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
82                 {
83                     if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
84                         aSubDirs.push_back( aFileStatus.getFileURL() );
85                     else
86                         aResult->push_back( aFileStatus.getFileURL() );
87                 }
88             }
89 
90             // iterate recursive over subfolders
91             for (auto const& subDir : aSubDirs)
92             {
93                 TStringVectorPtr aSubResult = getFiles(subDir);
94                 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
95             }
96         }
97 
98         return aResult;
99     }
100 
101 
checkAndCreateDirectory(INetURLObject const & rDirURL)102     void BasicMigration::checkAndCreateDirectory( INetURLObject const & rDirURL )
103     {
104         ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
105         if ( aResult == ::osl::FileBase::E_NOENT )
106         {
107             INetURLObject aBaseURL( rDirURL );
108             aBaseURL.removeSegment();
109             checkAndCreateDirectory( aBaseURL );
110             ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
111         }
112     }
113 
114 
copyFiles()115     void BasicMigration::copyFiles()
116     {
117         OUString sTargetDir;
118         ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
119         if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
120         {
121             sTargetDir += sTargetUserBasic;
122             TStringVectorPtr aFileList = getFiles( m_sSourceDir );
123             for (auto const& elem : *aFileList)
124             {
125                 OUString sLocalName = elem.copy( m_sSourceDir.getLength() );
126                 OUString sTargetName = sTargetDir + sLocalName;
127                 INetURLObject aURL( sTargetName );
128                 aURL.removeSegment();
129                 checkAndCreateDirectory( aURL );
130                 ::osl::FileBase::RC aResult = ::osl::File::copy( elem, sTargetName );
131                 if ( aResult != ::osl::FileBase::E_None )
132                 {
133                     SAL_WARN( "desktop", "BasicMigration::copyFiles: cannot copy "
134                                 << elem << " to " << sTargetName );
135                 }
136             }
137         }
138         else
139         {
140             OSL_FAIL( "BasicMigration::copyFiles: no user installation!" );
141         }
142     }
143 
144 
145     // XServiceInfo
146 
147 
getImplementationName()148     OUString BasicMigration::getImplementationName()
149     {
150         return BasicMigration_getImplementationName();
151     }
152 
153 
supportsService(OUString const & ServiceName)154     sal_Bool BasicMigration::supportsService(OUString const & ServiceName)
155     {
156         return cppu::supportsService(this, ServiceName);
157     }
158 
159 
getSupportedServiceNames()160     Sequence< OUString > BasicMigration::getSupportedServiceNames()
161     {
162         return BasicMigration_getSupportedServiceNames();
163     }
164 
165 
166     // XInitialization
167 
168 
initialize(const Sequence<Any> & aArguments)169     void BasicMigration::initialize( const Sequence< Any >& aArguments )
170     {
171         ::osl::MutexGuard aGuard( m_aMutex );
172 
173         const Any* pIter = aArguments.getConstArray();
174         const Any* pEnd = pIter + aArguments.getLength();
175         for ( ; pIter != pEnd ; ++pIter )
176         {
177             beans::NamedValue aValue;
178             *pIter >>= aValue;
179             if ( aValue.Name == "UserData" )
180             {
181                 if ( !(aValue.Value >>= m_sSourceDir) )
182                 {
183                     OSL_FAIL( "BasicMigration::initialize: argument UserData has wrong type!" );
184                 }
185                 m_sSourceDir += sSourceUserBasic;
186                 break;
187             }
188         }
189     }
190 
191 
192     // XJob
193 
194 
execute(const Sequence<beans::NamedValue> &)195     Any BasicMigration::execute( const Sequence< beans::NamedValue >& )
196     {
197         ::osl::MutexGuard aGuard( m_aMutex );
198 
199         copyFiles();
200 
201         return Any();
202     }
203 
204 
205     // component operations
206 
207 
BasicMigration_create(Reference<XComponentContext> const &)208     Reference< XInterface > BasicMigration_create(
209         Reference< XComponentContext > const & )
210     {
211         return static_cast< lang::XTypeProvider * >( new BasicMigration() );
212     }
213 
214 
215 }   // namespace migration
216 
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
219