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