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