1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "wordbookmigration.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <tools/urlobj.hxx>
23 #include <unotools/bootstrap.hxx>
24 #include <unotools/ucbstreamhelper.hxx>
25 #include <sal/log.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/file.hxx>
28 #include <com/sun/star/uno/XComponentContext.hpp>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
36 WordbookMigration::WordbookMigration()
41 WordbookMigration::~WordbookMigration()
46 TStringVectorPtr
WordbookMigration::getFiles( const OUString
& rBaseURL
) const
48 TStringVectorPtr
aResult( new TStringVector
);
49 ::osl::Directory
aDir( rBaseURL
);
51 if ( aDir
.open() == ::osl::FileBase::E_None
)
53 // iterate over directory content
54 TStringVector aSubDirs
;
55 ::osl::DirectoryItem aItem
;
56 while ( aDir
.getNextItem( aItem
) == ::osl::FileBase::E_None
)
58 ::osl::FileStatus
aFileStatus( osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
59 if ( aItem
.getFileStatus( aFileStatus
) == ::osl::FileBase::E_None
)
61 if ( aFileStatus
.getFileType() == ::osl::FileStatus::Directory
)
62 aSubDirs
.push_back( aFileStatus
.getFileURL() );
64 aResult
->push_back( aFileStatus
.getFileURL() );
68 // iterate recursive over subfolders
69 for (auto const& subDir
: aSubDirs
)
71 TStringVectorPtr aSubResult
= getFiles(subDir
);
72 aResult
->insert( aResult
->end(), aSubResult
->begin(), aSubResult
->end() );
80 void WordbookMigration::checkAndCreateDirectory( INetURLObject
const & rDirURL
)
82 ::osl::FileBase::RC aResult
= ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
83 if ( aResult
== ::osl::FileBase::E_NOENT
)
85 INetURLObject
aBaseURL( rDirURL
);
86 aBaseURL
.removeSegment();
87 checkAndCreateDirectory( aBaseURL
);
88 ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
92 #define MAX_HEADER_LENGTH 16
93 static bool IsUserWordbook( const OUString
& rFile
)
96 std::unique_ptr
<SvStream
> pStream
= ::utl::UcbStreamHelper::CreateStream( rFile
, StreamMode::STD_READ
);
97 if ( pStream
&& !pStream
->GetError() )
99 static const char* const pVerOOo7
= "OOoUserDict1";
100 sal_uInt64
const nSniffPos
= pStream
->Tell();
101 static std::size_t nVerOOo7Len
= sal::static_int_cast
< std::size_t >(strlen( pVerOOo7
));
102 char pMagicHeader
[MAX_HEADER_LENGTH
];
103 pMagicHeader
[ nVerOOo7Len
] = '\0';
104 if (pStream
->ReadBytes(static_cast<void *>(pMagicHeader
), nVerOOo7Len
) == nVerOOo7Len
)
106 if ( !strcmp(pMagicHeader
, pVerOOo7
) )
111 pStream
->Seek (nSniffPos
);
112 pStream
->ReadUInt16( nLen
);
113 if ( nLen
< MAX_HEADER_LENGTH
)
115 pStream
->ReadBytes(pMagicHeader
, nLen
);
116 pMagicHeader
[nLen
] = '\0';
117 if ( !strcmp(pMagicHeader
, "WBSWG2")
118 || !strcmp(pMagicHeader
, "WBSWG5")
119 || !strcmp(pMagicHeader
, "WBSWG6") )
130 void WordbookMigration::copyFiles()
133 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( sTargetDir
);
134 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
136 sTargetDir
+= "/user/wordbook";
137 TStringVectorPtr aFileList
= getFiles( m_sSourceDir
);
138 for (auto const& elem
: *aFileList
)
140 if (IsUserWordbook(elem
) )
142 std::u16string_view sSourceLocalName
= elem
.subView( m_sSourceDir
.getLength() );
143 OUString sTargetName
= sTargetDir
+ sSourceLocalName
;
144 INetURLObject
aURL( sTargetName
);
145 aURL
.removeSegment();
146 checkAndCreateDirectory( aURL
);
147 ::osl::FileBase::RC aResult
= ::osl::File::copy( elem
, sTargetName
);
148 if ( aResult
!= ::osl::FileBase::E_None
)
150 SAL_WARN( "desktop", "WordbookMigration::copyFiles: cannot copy "
151 << elem
<< " to " << sTargetName
);
158 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
166 OUString
WordbookMigration::getImplementationName()
168 return u
"com.sun.star.comp.desktop.migration.Wordbooks"_ustr
;
172 sal_Bool
WordbookMigration::supportsService(OUString
const & ServiceName
)
174 return cppu::supportsService(this, ServiceName
);
178 Sequence
< OUString
> WordbookMigration::getSupportedServiceNames()
180 return { u
"com.sun.star.migration.Wordbooks"_ustr
};
187 void WordbookMigration::initialize( const Sequence
< Any
>& aArguments
)
189 ::osl::MutexGuard
aGuard( m_aMutex
);
191 const Any
* pIter
= aArguments
.getConstArray();
192 const Any
* pEnd
= pIter
+ aArguments
.getLength();
193 for ( ; pIter
!= pEnd
; ++pIter
)
195 beans::NamedValue aValue
;
197 if ( aValue
.Name
== "UserData" )
199 if ( !(aValue
.Value
>>= m_sSourceDir
) )
201 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
203 m_sSourceDir
+= "/user/wordbook";
213 Any
WordbookMigration::execute( const Sequence
< beans::NamedValue
>& )
215 ::osl::MutexGuard
aGuard( m_aMutex
);
222 } // namespace migration
225 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
226 desktop_WordbookMigration_get_implementation(
227 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
229 return cppu::acquire(new migration::WordbookMigration());
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */