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>
26 using namespace ::com::sun::star
;
27 using namespace ::com::sun::star::uno
;
30 //.........................................................................
33 //.........................................................................
36 static OUString
sSourceSubDir( "/user/wordbook" );
37 static OUString
sTargetSubDir( "/user/wordbook" );
40 // =============================================================================
41 // component operations
42 // =============================================================================
44 OUString
WordbookMigration_getImplementationName()
46 static OUString
* pImplName
= 0;
49 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
52 static OUString
aImplName( "com.sun.star.comp.desktop.migration.Wordbooks" );
53 pImplName
= &aImplName
;
59 // -----------------------------------------------------------------------------
61 Sequence
< OUString
> WordbookMigration_getSupportedServiceNames()
63 static Sequence
< OUString
>* pNames
= 0;
66 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
69 static Sequence
< OUString
> aNames(1);
70 aNames
.getArray()[0] = "com.sun.star.migration.Wordbooks";
77 // =============================================================================
79 // =============================================================================
81 WordbookMigration::WordbookMigration()
85 // -----------------------------------------------------------------------------
87 WordbookMigration::~WordbookMigration()
91 // -----------------------------------------------------------------------------
93 TStringVectorPtr
WordbookMigration::getFiles( const OUString
& rBaseURL
) const
95 TStringVectorPtr
aResult( new TStringVector
);
96 ::osl::Directory
aDir( rBaseURL
);
98 if ( aDir
.open() == ::osl::FileBase::E_None
)
100 // iterate over directory content
101 TStringVector aSubDirs
;
102 ::osl::DirectoryItem aItem
;
103 while ( aDir
.getNextItem( aItem
) == ::osl::FileBase::E_None
)
105 ::osl::FileStatus
aFileStatus( osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
106 if ( aItem
.getFileStatus( aFileStatus
) == ::osl::FileBase::E_None
)
108 if ( aFileStatus
.getFileType() == ::osl::FileStatus::Directory
)
109 aSubDirs
.push_back( aFileStatus
.getFileURL() );
111 aResult
->push_back( aFileStatus
.getFileURL() );
115 // iterate recursive over subfolders
116 TStringVector::const_iterator aI
= aSubDirs
.begin();
117 while ( aI
!= aSubDirs
.end() )
119 TStringVectorPtr aSubResult
= getFiles( *aI
);
120 aResult
->insert( aResult
->end(), aSubResult
->begin(), aSubResult
->end() );
128 // -----------------------------------------------------------------------------
130 ::osl::FileBase::RC
WordbookMigration::checkAndCreateDirectory( INetURLObject
& rDirURL
)
132 ::osl::FileBase::RC aResult
= ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DECODE_TO_IURI
) );
133 if ( aResult
== ::osl::FileBase::E_NOENT
)
135 INetURLObject
aBaseURL( rDirURL
);
136 aBaseURL
.removeSegment();
137 checkAndCreateDirectory( aBaseURL
);
138 return ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DECODE_TO_IURI
) );
146 #define MAX_HEADER_LENGTH 16
147 bool IsUserWordbook( const OUString
& rFile
)
149 static const sal_Char
* pVerStr2
= "WBSWG2";
150 static const sal_Char
* pVerStr5
= "WBSWG5";
151 static const sal_Char
* pVerStr6
= "WBSWG6";
152 static const sal_Char
* pVerOOo7
= "OOoUserDict1";
155 SvStream
* pStream
= ::utl::UcbStreamHelper::CreateStream( OUString(rFile
), STREAM_STD_READ
);
156 if ( pStream
&& !pStream
->GetError() )
158 sal_Size nSniffPos
= pStream
->Tell();
159 static sal_Size nVerOOo7Len
= sal::static_int_cast
< sal_Size
>(strlen( pVerOOo7
));
160 sal_Char pMagicHeader
[MAX_HEADER_LENGTH
];
161 pMagicHeader
[ nVerOOo7Len
] = '\0';
162 if ((pStream
->Read((void *) pMagicHeader
, nVerOOo7Len
) == nVerOOo7Len
))
164 if ( !strcmp(pMagicHeader
, pVerOOo7
) )
169 pStream
->Seek (nSniffPos
);
171 if ( nLen
< MAX_HEADER_LENGTH
)
173 pStream
->Read(pMagicHeader
, nLen
);
174 pMagicHeader
[nLen
] = '\0';
175 if ( !strcmp(pMagicHeader
, pVerStr2
)
176 || !strcmp(pMagicHeader
, pVerStr5
)
177 || !strcmp(pMagicHeader
, pVerStr6
) )
189 // -----------------------------------------------------------------------------
191 void WordbookMigration::copyFiles()
194 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( sTargetDir
);
195 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
197 sTargetDir
+= sTargetSubDir
;
198 TStringVectorPtr aFileList
= getFiles( m_sSourceDir
);
199 TStringVector::const_iterator aI
= aFileList
->begin();
200 while ( aI
!= aFileList
->end() )
202 if (IsUserWordbook(*aI
) )
204 OUString sSourceLocalName
= aI
->copy( m_sSourceDir
.getLength() );
205 OUString sTargetName
= sTargetDir
+ sSourceLocalName
;
206 INetURLObject
aURL( sTargetName
);
207 aURL
.removeSegment();
208 checkAndCreateDirectory( aURL
);
209 ::osl::FileBase::RC aResult
= ::osl::File::copy( *aI
, sTargetName
);
210 if ( aResult
!= ::osl::FileBase::E_None
)
212 OString
aMsg( "WordbookMigration::copyFiles: cannot copy " );
213 aMsg
+= OUStringToOString( *aI
, RTL_TEXTENCODING_UTF8
) + " to "
214 + OUStringToOString( sTargetName
, RTL_TEXTENCODING_UTF8
);
215 OSL_FAIL( aMsg
.getStr() );
223 OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
227 // -----------------------------------------------------------------------------
229 // -----------------------------------------------------------------------------
231 OUString
WordbookMigration::getImplementationName() throw (RuntimeException
)
233 return WordbookMigration_getImplementationName();
236 // -----------------------------------------------------------------------------
238 sal_Bool
WordbookMigration::supportsService(OUString
const & ServiceName
)
239 throw (css::uno::RuntimeException
)
241 return cppu::supportsService(this, ServiceName
);
244 // -----------------------------------------------------------------------------
246 Sequence
< OUString
> WordbookMigration::getSupportedServiceNames() throw (RuntimeException
)
248 return WordbookMigration_getSupportedServiceNames();
251 // -----------------------------------------------------------------------------
253 // -----------------------------------------------------------------------------
255 void WordbookMigration::initialize( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
)
257 ::osl::MutexGuard
aGuard( m_aMutex
);
259 const Any
* pIter
= aArguments
.getConstArray();
260 const Any
* pEnd
= pIter
+ aArguments
.getLength();
261 for ( ; pIter
!= pEnd
; ++pIter
)
263 beans::NamedValue aValue
;
265 if ( aValue
.Name
== "UserData" )
267 if ( !(aValue
.Value
>>= m_sSourceDir
) )
269 OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
271 m_sSourceDir
+= sSourceSubDir
;
277 // -----------------------------------------------------------------------------
279 // -----------------------------------------------------------------------------
281 Any
WordbookMigration::execute( const Sequence
< beans::NamedValue
>& )
282 throw (lang::IllegalArgumentException
, Exception
, RuntimeException
)
284 ::osl::MutexGuard
aGuard( m_aMutex
);
291 // =============================================================================
292 // component operations
293 // =============================================================================
295 Reference
< XInterface
> SAL_CALL
WordbookMigration_create(
296 Reference
< XComponentContext
> const & )
299 return static_cast< lang::XTypeProvider
* >( new WordbookMigration() );
302 // -----------------------------------------------------------------------------
304 //.........................................................................
305 } // namespace migration
306 //.........................................................................
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */