Line data Source code
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"
21 : #include <cppuhelper/supportsservice.hxx>
22 : #include <tools/string.hxx>
23 : #include <tools/urlobj.hxx>
24 : #include <unotools/bootstrap.hxx>
25 : #include <unotools/ucbstreamhelper.hxx>
26 :
27 : using namespace ::com::sun::star;
28 : using namespace ::com::sun::star::uno;
29 :
30 :
31 : //.........................................................................
32 : namespace migration
33 : {
34 : //.........................................................................
35 :
36 :
37 0 : static OUString sSourceSubDir( "/user/wordbook" );
38 0 : static OUString sTargetSubDir( "/user/wordbook" );
39 :
40 :
41 : // =============================================================================
42 : // component operations
43 : // =============================================================================
44 :
45 0 : OUString WordbookMigration_getImplementationName()
46 : {
47 : static OUString* pImplName = 0;
48 0 : if ( !pImplName )
49 : {
50 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
51 0 : if ( !pImplName )
52 : {
53 0 : static OUString aImplName( "com.sun.star.comp.desktop.migration.Wordbooks" );
54 0 : pImplName = &aImplName;
55 0 : }
56 : }
57 0 : return *pImplName;
58 : }
59 :
60 : // -----------------------------------------------------------------------------
61 :
62 0 : Sequence< OUString > WordbookMigration_getSupportedServiceNames()
63 : {
64 : static Sequence< OUString >* pNames = 0;
65 0 : if ( !pNames )
66 : {
67 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
68 0 : if ( !pNames )
69 : {
70 0 : static Sequence< OUString > aNames(1);
71 0 : aNames.getArray()[0] = OUString( "com.sun.star.migration.Wordbooks" );
72 0 : pNames = &aNames;
73 0 : }
74 : }
75 0 : return *pNames;
76 : }
77 :
78 : // =============================================================================
79 : // WordbookMigration
80 : // =============================================================================
81 :
82 0 : WordbookMigration::WordbookMigration()
83 : {
84 0 : }
85 :
86 : // -----------------------------------------------------------------------------
87 :
88 0 : WordbookMigration::~WordbookMigration()
89 : {
90 0 : }
91 :
92 : // -----------------------------------------------------------------------------
93 :
94 0 : TStringVectorPtr WordbookMigration::getFiles( const OUString& rBaseURL ) const
95 : {
96 0 : TStringVectorPtr aResult( new TStringVector );
97 0 : ::osl::Directory aDir( rBaseURL);
98 :
99 0 : if ( aDir.open() == ::osl::FileBase::E_None )
100 : {
101 : // iterate over directory content
102 0 : TStringVector aSubDirs;
103 0 : ::osl::DirectoryItem aItem;
104 0 : while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
105 : {
106 0 : ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
107 0 : if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
108 : {
109 0 : if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
110 0 : aSubDirs.push_back( aFileStatus.getFileURL() );
111 : else
112 0 : aResult->push_back( aFileStatus.getFileURL() );
113 : }
114 0 : }
115 :
116 : // iterate recursive over subfolders
117 0 : TStringVector::const_iterator aI = aSubDirs.begin();
118 0 : while ( aI != aSubDirs.end() )
119 : {
120 0 : TStringVectorPtr aSubResult = getFiles( *aI );
121 0 : aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
122 0 : ++aI;
123 0 : }
124 : }
125 :
126 0 : return aResult;
127 : }
128 :
129 : // -----------------------------------------------------------------------------
130 :
131 0 : ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
132 : {
133 0 : ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
134 0 : if ( aResult == ::osl::FileBase::E_NOENT )
135 : {
136 0 : INetURLObject aBaseURL( rDirURL );
137 0 : aBaseURL.removeSegment();
138 0 : checkAndCreateDirectory( aBaseURL );
139 0 : return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
140 : }
141 : else
142 : {
143 0 : return aResult;
144 : }
145 : }
146 :
147 : #define MAX_HEADER_LENGTH 16
148 0 : bool IsUserWordbook( const OUString& rFile )
149 : {
150 : static const sal_Char* pVerStr2 = "WBSWG2";
151 : static const sal_Char* pVerStr5 = "WBSWG5";
152 : static const sal_Char* pVerStr6 = "WBSWG6";
153 : static const sal_Char* pVerOOo7 = "OOoUserDict1";
154 :
155 0 : bool bRet = false;
156 0 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( String(rFile), STREAM_STD_READ );
157 0 : if ( pStream && !pStream->GetError() )
158 : {
159 0 : sal_Size nSniffPos = pStream->Tell();
160 0 : static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
161 : sal_Char pMagicHeader[MAX_HEADER_LENGTH];
162 0 : pMagicHeader[ nVerOOo7Len ] = '\0';
163 0 : if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
164 : {
165 0 : if ( !strcmp(pMagicHeader, pVerOOo7) )
166 0 : bRet = true;
167 : else
168 : {
169 : sal_uInt16 nLen;
170 0 : pStream->Seek (nSniffPos);
171 0 : *pStream >> nLen;
172 0 : if ( nLen < MAX_HEADER_LENGTH )
173 : {
174 0 : pStream->Read(pMagicHeader, nLen);
175 0 : pMagicHeader[nLen] = '\0';
176 0 : if ( !strcmp(pMagicHeader, pVerStr2)
177 0 : || !strcmp(pMagicHeader, pVerStr5)
178 0 : || !strcmp(pMagicHeader, pVerStr6) )
179 0 : bRet = true;
180 : }
181 : }
182 : }
183 : }
184 :
185 0 : delete pStream;
186 0 : return bRet;
187 : }
188 :
189 :
190 : // -----------------------------------------------------------------------------
191 :
192 0 : void WordbookMigration::copyFiles()
193 : {
194 0 : OUString sTargetDir;
195 0 : ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
196 0 : if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
197 : {
198 0 : sTargetDir += sTargetSubDir;
199 0 : TStringVectorPtr aFileList = getFiles( m_sSourceDir );
200 0 : TStringVector::const_iterator aI = aFileList->begin();
201 0 : while ( aI != aFileList->end() )
202 : {
203 0 : if (IsUserWordbook(*aI) )
204 : {
205 0 : OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
206 0 : OUString sTargetName = sTargetDir + sSourceLocalName;
207 0 : INetURLObject aURL( sTargetName );
208 0 : aURL.removeSegment();
209 0 : checkAndCreateDirectory( aURL );
210 0 : ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
211 0 : if ( aResult != ::osl::FileBase::E_None )
212 : {
213 0 : OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
214 0 : aMsg += OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
215 0 : + OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
216 0 : OSL_FAIL( aMsg.getStr() );
217 0 : }
218 : }
219 0 : ++aI;
220 0 : }
221 : }
222 : else
223 : {
224 : OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
225 0 : }
226 0 : }
227 :
228 : // -----------------------------------------------------------------------------
229 : // XServiceInfo
230 : // -----------------------------------------------------------------------------
231 :
232 0 : OUString WordbookMigration::getImplementationName() throw (RuntimeException)
233 : {
234 0 : return WordbookMigration_getImplementationName();
235 : }
236 :
237 : // -----------------------------------------------------------------------------
238 :
239 0 : sal_Bool WordbookMigration::supportsService(OUString const & ServiceName)
240 : throw (css::uno::RuntimeException)
241 : {
242 0 : return cppu::supportsService(this, ServiceName);
243 : }
244 :
245 : // -----------------------------------------------------------------------------
246 :
247 0 : Sequence< OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
248 : {
249 0 : return WordbookMigration_getSupportedServiceNames();
250 : }
251 :
252 : // -----------------------------------------------------------------------------
253 : // XInitialization
254 : // -----------------------------------------------------------------------------
255 :
256 0 : void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
257 : {
258 0 : ::osl::MutexGuard aGuard( m_aMutex );
259 :
260 0 : const Any* pIter = aArguments.getConstArray();
261 0 : const Any* pEnd = pIter + aArguments.getLength();
262 0 : for ( ; pIter != pEnd ; ++pIter )
263 : {
264 0 : beans::NamedValue aValue;
265 0 : *pIter >>= aValue;
266 0 : if ( aValue.Name == "UserData" )
267 : {
268 0 : if ( !(aValue.Value >>= m_sSourceDir) )
269 : {
270 : OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
271 : }
272 0 : m_sSourceDir += sSourceSubDir;
273 0 : break;
274 : }
275 0 : }
276 0 : }
277 :
278 : // -----------------------------------------------------------------------------
279 : // XJob
280 : // -----------------------------------------------------------------------------
281 :
282 0 : Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
283 : throw (lang::IllegalArgumentException, Exception, RuntimeException)
284 : {
285 0 : ::osl::MutexGuard aGuard( m_aMutex );
286 :
287 0 : copyFiles();
288 :
289 0 : return Any();
290 : }
291 :
292 : // =============================================================================
293 : // component operations
294 : // =============================================================================
295 :
296 0 : Reference< XInterface > SAL_CALL WordbookMigration_create(
297 : Reference< XComponentContext > const & )
298 : SAL_THROW(())
299 : {
300 0 : return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
301 : }
302 :
303 : // -----------------------------------------------------------------------------
304 :
305 : //.........................................................................
306 0 : } // namespace migration
307 : //.........................................................................
308 :
309 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|