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