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