LibreOffice Module desktop (master)  1
basicmigration.cxx
Go to the documentation of this file.
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"
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>
27 
28 
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 
32 
33 namespace migration
34 {
35 
36 
37  #define sSourceUserBasic "/user/basic"
38  #define sTargetUserBasic "/user/__basic_80"
39 
40 
41  // BasicMigration
42 
43 
45  {
46  }
47 
48 
50  {
51  }
52 
53 
54  TStringVectorPtr BasicMigration::getFiles( const OUString& rBaseURL ) const
55  {
56  TStringVectorPtr aResult( new TStringVector );
57  ::osl::Directory aDir( rBaseURL);
58 
59  if ( aDir.open() == ::osl::FileBase::E_None )
60  {
61  // iterate over directory content
62  TStringVector aSubDirs;
63  ::osl::DirectoryItem aItem;
64  while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
65  {
66  ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
67  if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
68  {
69  if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
70  aSubDirs.push_back( aFileStatus.getFileURL() );
71  else
72  aResult->push_back( aFileStatus.getFileURL() );
73  }
74  }
75 
76  // iterate recursive over subfolders
77  for (auto const& subDir : aSubDirs)
78  {
79  TStringVectorPtr aSubResult = getFiles(subDir);
80  aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
81  }
82  }
83 
84  return aResult;
85  }
86 
87 
89  {
90  ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
91  if ( aResult == ::osl::FileBase::E_NOENT )
92  {
93  INetURLObject aBaseURL( rDirURL );
94  aBaseURL.removeSegment();
95  checkAndCreateDirectory( aBaseURL );
97  }
98  }
99 
100 
102  {
103  OUString sTargetDir;
105  if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
106  {
107  sTargetDir += sTargetUserBasic;
108  TStringVectorPtr aFileList = getFiles( m_sSourceDir );
109  for (auto const& elem : *aFileList)
110  {
111  OUString sLocalName = elem.copy( 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 )
118  {
119  SAL_WARN( "desktop", "BasicMigration::copyFiles: cannot copy "
120  << elem << " to " << sTargetName );
121  }
122  }
123  }
124  else
125  {
126  OSL_FAIL( "BasicMigration::copyFiles: no user installation!" );
127  }
128  }
129 
130 
131  // XServiceInfo
132 
133 
135  {
136  return "com.sun.star.comp.desktop.migration.Basic";
137  }
138 
139 
140  sal_Bool BasicMigration::supportsService(OUString const & ServiceName)
141  {
142  return cppu::supportsService(this, ServiceName);
143  }
144 
145 
147  {
148  return { "com.sun.star.migration.Basic" };
149  }
150 
151 
152  // XInitialization
153 
154 
155  void BasicMigration::initialize( const Sequence< Any >& aArguments )
156  {
157  ::osl::MutexGuard aGuard( m_aMutex );
158 
159  const Any* pIter = aArguments.getConstArray();
160  const Any* pEnd = pIter + aArguments.getLength();
161  for ( ; pIter != pEnd ; ++pIter )
162  {
163  beans::NamedValue aValue;
164  *pIter >>= aValue;
165  if ( aValue.Name == "UserData" )
166  {
167  if ( !(aValue.Value >>= m_sSourceDir) )
168  {
169  OSL_FAIL( "BasicMigration::initialize: argument UserData has wrong type!" );
170  }
172  break;
173  }
174  }
175  }
176 
177 
178  // XJob
179 
180 
182  {
183  ::osl::MutexGuard aGuard( m_aMutex );
184 
185  copyFiles();
186 
187  return Any();
188  }
189 
190 
191 } // namespace migration
192 
193 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
195  css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
196 {
197  return cppu::acquire(new migration::BasicMigration());
198 }
199 
200 
201 
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Any SAL_CALL execute(const css::uno::Sequence< css::beans::NamedValue > &Arguments) override
static PathStatus locateUserInstallation(OUString &_rURL)
virtual ~BasicMigration() override
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * desktop_BasicMigration_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void checkAndCreateDirectory(INetURLObject const &rDirURL)
std::unique_ptr< TStringVector > TStringVectorPtr
Definition: misc.hxx:33
unsigned char sal_Bool
std::vector< OUString > TStringVector
Definition: misc.hxx:32
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
#define sSourceUserBasic
#define SAL_WARN(area, stream)
TStringVectorPtr getFiles(const OUString &rBaseURL) const
#define sTargetUserBasic
virtual OUString SAL_CALL getImplementationName() override
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual sal_Bool SAL_CALL supportsService(const OUString &rServiceName) override