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/file.hxx>
27 #include <com/sun/star/uno/XComponentContext.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::uno
;
35 WordbookMigration::WordbookMigration()
40 WordbookMigration::~WordbookMigration()
45 TStringVectorPtr
WordbookMigration::getFiles( const OUString
& rBaseURL
) const
47 TStringVectorPtr
aResult( new TStringVector
);
48 ::osl::Directory
aDir( rBaseURL
);
50 if ( aDir
.open() == ::osl::FileBase::E_None
)
52 // iterate over directory content
53 TStringVector aSubDirs
;
54 ::osl::DirectoryItem aItem
;
55 while ( aDir
.getNextItem( aItem
) == ::osl::FileBase::E_None
)
57 ::osl::FileStatus
aFileStatus( osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
58 if ( aItem
.getFileStatus( aFileStatus
) == ::osl::FileBase::E_None
)
60 if ( aFileStatus
.getFileType() == ::osl::FileStatus::Directory
)
61 aSubDirs
.push_back( aFileStatus
.getFileURL() );
63 aResult
->push_back( aFileStatus
.getFileURL() );
67 // iterate recursive over subfolders
68 for (auto const& subDir
: aSubDirs
)
70 TStringVectorPtr aSubResult
= getFiles(subDir
);
71 aResult
->insert( aResult
->end(), aSubResult
->begin(), aSubResult
->end() );
79 void WordbookMigration::checkAndCreateDirectory( INetURLObject
const & rDirURL
)
81 ::osl::FileBase::RC aResult
= ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
82 if ( aResult
== ::osl::FileBase::E_NOENT
)
84 INetURLObject
aBaseURL( rDirURL
);
85 aBaseURL
.removeSegment();
86 checkAndCreateDirectory( aBaseURL
);
87 ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
) );
91 #define MAX_HEADER_LENGTH 16
92 static bool IsUserWordbook( const OUString
& rFile
)
95 std::unique_ptr
<SvStream
> pStream
= ::utl::UcbStreamHelper::CreateStream( rFile
, StreamMode::STD_READ
);
96 if ( pStream
&& !pStream
->GetError() )
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
)
105 if ( !strcmp(pMagicHeader
, pVerOOo7
) )
110 pStream
->Seek (nSniffPos
);
111 pStream
->ReadUInt16( nLen
);
112 if ( nLen
< MAX_HEADER_LENGTH
)
114 pStream
->ReadBytes(pMagicHeader
, nLen
);
115 pMagicHeader
[nLen
] = '\0';
116 if ( !strcmp(pMagicHeader
, "WBSWG2")
117 || !strcmp(pMagicHeader
, "WBSWG5")
118 || !strcmp(pMagicHeader
, "WBSWG6") )
129 void WordbookMigration::copyFiles()
132 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( sTargetDir
);
133 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
135 sTargetDir
+= "/user/wordbook";
136 TStringVectorPtr aFileList
= getFiles( m_sSourceDir
);
137 for (auto const& elem
: *aFileList
)
139 if (IsUserWordbook(elem
) )
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
)
149 SAL_WARN( "desktop", "WordbookMigration::copyFiles: cannot copy "
150 << elem
<< " to " << sTargetName
);
157 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
165 OUString
WordbookMigration::getImplementationName()
167 return "com.sun.star.comp.desktop.migration.Wordbooks";
171 sal_Bool
WordbookMigration::supportsService(OUString
const & ServiceName
)
173 return cppu::supportsService(this, ServiceName
);
177 Sequence
< OUString
> WordbookMigration::getSupportedServiceNames()
179 return { "com.sun.star.migration.Wordbooks" };
186 void WordbookMigration::initialize( const Sequence
< Any
>& aArguments
)
188 ::osl::MutexGuard
aGuard( m_aMutex
);
190 const Any
* pIter
= aArguments
.getConstArray();
191 const Any
* pEnd
= pIter
+ aArguments
.getLength();
192 for ( ; pIter
!= pEnd
; ++pIter
)
194 beans::NamedValue aValue
;
196 if ( aValue
.Name
== "UserData" )
198 if ( !(aValue
.Value
>>= m_sSourceDir
) )
200 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
202 m_sSourceDir
+= "/user/wordbook";
212 Any
WordbookMigration::execute( const Sequence
< beans::NamedValue
>& )
214 ::osl::MutexGuard
aGuard( m_aMutex
);
221 } // namespace migration
224 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
225 desktop_WordbookMigration_get_implementation(
226 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
228 return cppu::acquire(new migration::WordbookMigration());
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */