LibreOffice Module desktop (master)  1
wordbookmigration.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 "wordbookmigration.hxx"
22 #include <tools/urlobj.hxx>
23 #include <unotools/bootstrap.hxx>
25 #include <sal/log.hxx>
26 #include <osl/file.hxx>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 
32 
33 namespace migration
34 {
36  {
37  }
38 
39 
41  {
42  }
43 
44 
45  TStringVectorPtr WordbookMigration::getFiles( const OUString& rBaseURL ) const
46  {
47  TStringVectorPtr aResult( new TStringVector );
48  ::osl::Directory aDir( rBaseURL);
49 
50  if ( aDir.open() == ::osl::FileBase::E_None )
51  {
52  // iterate over directory content
53  TStringVector aSubDirs;
54  ::osl::DirectoryItem aItem;
55  while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
56  {
57  ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
58  if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
59  {
60  if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
61  aSubDirs.push_back( aFileStatus.getFileURL() );
62  else
63  aResult->push_back( aFileStatus.getFileURL() );
64  }
65  }
66 
67  // iterate recursive over subfolders
68  for (auto const& subDir : aSubDirs)
69  {
70  TStringVectorPtr aSubResult = getFiles(subDir);
71  aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
72  }
73  }
74 
75  return aResult;
76  }
77 
78 
80  {
81  ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri ) );
82  if ( aResult == ::osl::FileBase::E_NOENT )
83  {
84  INetURLObject aBaseURL( rDirURL );
85  aBaseURL.removeSegment();
86  checkAndCreateDirectory( aBaseURL );
88  }
89  }
90 
91 #define MAX_HEADER_LENGTH 16
92 static bool IsUserWordbook( const OUString& rFile )
93 {
94  bool bRet = false;
95  std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( rFile, StreamMode::STD_READ );
96  if ( pStream && !pStream->GetError() )
97  {
98  static const char* const pVerOOo7 = "OOoUserDict1";
99  sal_uInt64 const nSniffPos = pStream->Tell();
100  static std::size_t nVerOOo7Len = sal::static_int_cast< std::size_t >(strlen( pVerOOo7 ));
101  char pMagicHeader[MAX_HEADER_LENGTH];
102  pMagicHeader[ nVerOOo7Len ] = '\0';
103  if (pStream->ReadBytes(static_cast<void *>(pMagicHeader), nVerOOo7Len) == nVerOOo7Len)
104  {
105  if ( !strcmp(pMagicHeader, pVerOOo7) )
106  bRet = true;
107  else
108  {
109  sal_uInt16 nLen;
110  pStream->Seek (nSniffPos);
111  pStream->ReadUInt16( nLen );
112  if ( nLen < MAX_HEADER_LENGTH )
113  {
114  pStream->ReadBytes(pMagicHeader, nLen);
115  pMagicHeader[nLen] = '\0';
116  if ( !strcmp(pMagicHeader, "WBSWG2")
117  || !strcmp(pMagicHeader, "WBSWG5")
118  || !strcmp(pMagicHeader, "WBSWG6") )
119  bRet = true;
120  }
121  }
122  }
123  }
124 
125  return bRet;
126 }
127 
128 
130  {
131  OUString sTargetDir;
133  if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
134  {
135  sTargetDir += "/user/wordbook";
136  TStringVectorPtr aFileList = getFiles( m_sSourceDir );
137  for (auto const& elem : *aFileList)
138  {
139  if (IsUserWordbook(elem) )
140  {
141  OUString sSourceLocalName = elem.copy( m_sSourceDir.getLength() );
142  OUString sTargetName = sTargetDir + sSourceLocalName;
143  INetURLObject aURL( sTargetName );
144  aURL.removeSegment();
145  checkAndCreateDirectory( aURL );
146  ::osl::FileBase::RC aResult = ::osl::File::copy( elem, sTargetName );
147  if ( aResult != ::osl::FileBase::E_None )
148  {
149  SAL_WARN( "desktop", "WordbookMigration::copyFiles: cannot copy "
150  << elem << " to " << sTargetName);
151  }
152  }
153  }
154  }
155  else
156  {
157  OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
158  }
159  }
160 
161 
162  // XServiceInfo
163 
164 
166  {
167  return "com.sun.star.comp.desktop.migration.Wordbooks";
168  }
169 
170 
171  sal_Bool WordbookMigration::supportsService(OUString const & ServiceName)
172  {
173  return cppu::supportsService(this, ServiceName);
174  }
175 
176 
178  {
179  return { "com.sun.star.migration.Wordbooks" };
180  }
181 
182 
183  // XInitialization
184 
185 
186  void WordbookMigration::initialize( const Sequence< Any >& aArguments )
187  {
188  ::osl::MutexGuard aGuard( m_aMutex );
189 
190  const Any* pIter = aArguments.getConstArray();
191  const Any* pEnd = pIter + aArguments.getLength();
192  for ( ; pIter != pEnd ; ++pIter )
193  {
194  beans::NamedValue aValue;
195  *pIter >>= aValue;
196  if ( aValue.Name == "UserData" )
197  {
198  if ( !(aValue.Value >>= m_sSourceDir) )
199  {
200  OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
201  }
202  m_sSourceDir += "/user/wordbook";
203  break;
204  }
205  }
206  }
207 
208 
209  // XJob
210 
211 
213  {
214  ::osl::MutexGuard aGuard( m_aMutex );
215 
216  copyFiles();
217 
218  return Any();
219  }
220 
221 } // namespace migration
222 
223 
224 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
226  css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
227 {
228  return cppu::acquire(new migration::WordbookMigration());
229 }
230 
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
static PathStatus locateUserInstallation(OUString &_rURL)
virtual css::uno::Any SAL_CALL execute(const css::uno::Sequence< css::beans::NamedValue > &Arguments) 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_WordbookMigration_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
virtual ~WordbookMigration() override
const char *const pVerOOo7
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual sal_Bool SAL_CALL supportsService(const OUString &rServiceName) override
std::unique_ptr< TStringVector > TStringVectorPtr
Definition: misc.hxx:33
virtual OUString SAL_CALL getImplementationName() override
unsigned char sal_Bool
#define MAX_HEADER_LENGTH
void checkAndCreateDirectory(INetURLObject const &rDirURL)
TStringVectorPtr getFiles(const OUString &rBaseURL) const
std::vector< OUString > TStringVector
Definition: misc.hxx:32
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
#define SAL_WARN(area, stream)
static bool IsUserWordbook(const OUString &rFile)
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)