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  // BasicMigration
41 
42 
44  {
45  }
46 
47 
49  {
50  }
51 
52 
53  TStringVectorPtr BasicMigration::getFiles( const OUString& rBaseURL ) const
54  {
55  TStringVectorPtr aResult( new TStringVector );
56  ::osl::Directory aDir( rBaseURL);
57 
58  if ( aDir.open() == ::osl::FileBase::E_None )
59  {
60  // iterate over directory content
61  TStringVector aSubDirs;
62  ::osl::DirectoryItem aItem;
63  while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
64  {
65  ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
66  if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
67  {
68  if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
69  aSubDirs.push_back( aFileStatus.getFileURL() );
70  else
71  aResult->push_back( aFileStatus.getFileURL() );
72  }
73  }
74 
75  // iterate recursive over subfolders
76  for (auto const& subDir : aSubDirs)
77  {
78  TStringVectorPtr aSubResult = getFiles(subDir);
79  aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
80  }
81  }
82 
83  return aResult;
84  }
85 
86 
88  {
89  ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
90  if ( aResult == ::osl::FileBase::E_NOENT )
91  {
92  INetURLObject aBaseURL( rDirURL );
93  aBaseURL.removeSegment();
94  checkAndCreateDirectory( aBaseURL );
96  }
97  }
98 
99 
101  {
102  OUString sTargetDir;
104  if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
105  {
106  sTargetDir += sTargetUserBasic;
107  TStringVectorPtr aFileList = getFiles( m_sSourceDir );
108  for (auto const& elem : *aFileList)
109  {
110  OUString sLocalName = elem.copy( m_sSourceDir.getLength() );
111  OUString sTargetName = sTargetDir + sLocalName;
112  INetURLObject aURL( sTargetName );
113  aURL.removeSegment();
114  checkAndCreateDirectory( aURL );
115  ::osl::FileBase::RC aResult = ::osl::File::copy( elem, sTargetName );
116  if ( aResult != ::osl::FileBase::E_None )
117  {
118  SAL_WARN( "desktop", "BasicMigration::copyFiles: cannot copy "
119  << elem << " to " << sTargetName );
120  }
121  }
122  }
123  else
124  {
125  OSL_FAIL( "BasicMigration::copyFiles: no user installation!" );
126  }
127  }
128 
129 
130  // XServiceInfo
131 
132 
134  {
135  return "com.sun.star.comp.desktop.migration.Basic";
136  }
137 
138 
139  sal_Bool BasicMigration::supportsService(OUString const & ServiceName)
140  {
141  return cppu::supportsService(this, ServiceName);
142  }
143 
144 
146  {
147  return { "com.sun.star.migration.Basic" };
148  }
149 
150 
151  // XInitialization
152 
153 
154  void BasicMigration::initialize( const Sequence< Any >& aArguments )
155  {
156  ::osl::MutexGuard aGuard( m_aMutex );
157 
158  const Any* pIter = aArguments.getConstArray();
159  const Any* pEnd = pIter + aArguments.getLength();
160  for ( ; pIter != pEnd ; ++pIter )
161  {
162  beans::NamedValue aValue;
163  *pIter >>= aValue;
164  if ( aValue.Name == "UserData" )
165  {
166  if ( !(aValue.Value >>= m_sSourceDir) )
167  {
168  OSL_FAIL( "BasicMigration::initialize: argument UserData has wrong type!" );
169  }
171  break;
172  }
173  }
174  }
175 
176 
177  // XJob
178 
179 
181  {
182  ::osl::MutexGuard aGuard( m_aMutex );
183 
184  copyFiles();
185 
186  return Any();
187  }
188 
189 
190 } // namespace migration
191 
192 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
194  css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
195 {
196  return cppu::acquire(new migration::BasicMigration());
197 }
198 
199 
200 
201 /* 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: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