Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "wordbookmigration.hxx"
30 : : #include <tools/urlobj.hxx>
31 : : #include <unotools/bootstrap.hxx>
32 : : #include <unotools/ucbstreamhelper.hxx>
33 : :
34 : : using namespace ::com::sun::star;
35 : : using namespace ::com::sun::star::uno;
36 : :
37 : :
38 : : //.........................................................................
39 : : namespace migration
40 : : {
41 : : //.........................................................................
42 : :
43 : :
44 : 0 : static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
45 : 0 : static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
46 : :
47 : :
48 : : // =============================================================================
49 : : // component operations
50 : : // =============================================================================
51 : :
52 : 0 : ::rtl::OUString WordbookMigration_getImplementationName()
53 : : {
54 : : static ::rtl::OUString* pImplName = 0;
55 : 0 : if ( !pImplName )
56 : : {
57 : 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
58 : 0 : if ( !pImplName )
59 : : {
60 : 0 : static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Wordbooks" ) );
61 : 0 : pImplName = &aImplName;
62 : 0 : }
63 : : }
64 : 0 : return *pImplName;
65 : : }
66 : :
67 : : // -----------------------------------------------------------------------------
68 : :
69 : 0 : Sequence< ::rtl::OUString > WordbookMigration_getSupportedServiceNames()
70 : : {
71 : : static Sequence< ::rtl::OUString >* pNames = 0;
72 : 0 : if ( !pNames )
73 : : {
74 : 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
75 : 0 : if ( !pNames )
76 : : {
77 : 0 : static Sequence< ::rtl::OUString > aNames(1);
78 : 0 : aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Wordbooks" ) );
79 : 0 : pNames = &aNames;
80 : 0 : }
81 : : }
82 : 0 : return *pNames;
83 : : }
84 : :
85 : : // =============================================================================
86 : : // WordbookMigration
87 : : // =============================================================================
88 : :
89 : 0 : WordbookMigration::WordbookMigration()
90 : : {
91 : 0 : }
92 : :
93 : : // -----------------------------------------------------------------------------
94 : :
95 : 0 : WordbookMigration::~WordbookMigration()
96 : : {
97 : 0 : }
98 : :
99 : : // -----------------------------------------------------------------------------
100 : :
101 : 0 : TStringVectorPtr WordbookMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
102 : : {
103 : 0 : TStringVectorPtr aResult( new TStringVector );
104 : 0 : ::osl::Directory aDir( rBaseURL);
105 : :
106 : 0 : if ( aDir.open() == ::osl::FileBase::E_None )
107 : : {
108 : : // iterate over directory content
109 : 0 : TStringVector aSubDirs;
110 : 0 : ::osl::DirectoryItem aItem;
111 : 0 : while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
112 : : {
113 : 0 : ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL );
114 : 0 : if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
115 : : {
116 : 0 : if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
117 : 0 : aSubDirs.push_back( aFileStatus.getFileURL() );
118 : : else
119 : 0 : aResult->push_back( aFileStatus.getFileURL() );
120 : : }
121 : 0 : }
122 : :
123 : : // iterate recursive over subfolders
124 : 0 : TStringVector::const_iterator aI = aSubDirs.begin();
125 : 0 : while ( aI != aSubDirs.end() )
126 : : {
127 : 0 : TStringVectorPtr aSubResult = getFiles( *aI );
128 : 0 : aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
129 : 0 : ++aI;
130 : 0 : }
131 : : }
132 : :
133 : 0 : return aResult;
134 : : }
135 : :
136 : : // -----------------------------------------------------------------------------
137 : :
138 : 0 : ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
139 : : {
140 : 0 : ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
141 : 0 : if ( aResult == ::osl::FileBase::E_NOENT )
142 : : {
143 : 0 : INetURLObject aBaseURL( rDirURL );
144 : 0 : aBaseURL.removeSegment();
145 : 0 : checkAndCreateDirectory( aBaseURL );
146 : 0 : return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
147 : : }
148 : : else
149 : : {
150 : 0 : return aResult;
151 : : }
152 : : }
153 : :
154 : : #define MAX_HEADER_LENGTH 16
155 : 0 : bool IsUserWordbook( const ::rtl::OUString& rFile )
156 : : {
157 : : static const sal_Char* pVerStr2 = "WBSWG2";
158 : : static const sal_Char* pVerStr5 = "WBSWG5";
159 : : static const sal_Char* pVerStr6 = "WBSWG6";
160 : : static const sal_Char* pVerOOo7 = "OOoUserDict1";
161 : :
162 : 0 : bool bRet = false;
163 : 0 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( String(rFile), STREAM_STD_READ );
164 : 0 : if ( pStream && !pStream->GetError() )
165 : : {
166 : 0 : sal_Size nSniffPos = pStream->Tell();
167 : 0 : static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
168 : : sal_Char pMagicHeader[MAX_HEADER_LENGTH];
169 : 0 : pMagicHeader[ nVerOOo7Len ] = '\0';
170 : 0 : if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
171 : : {
172 : 0 : if ( !strcmp(pMagicHeader, pVerOOo7) )
173 : 0 : bRet = true;
174 : : else
175 : : {
176 : : sal_uInt16 nLen;
177 : 0 : pStream->Seek (nSniffPos);
178 : 0 : *pStream >> nLen;
179 : 0 : if ( nLen < MAX_HEADER_LENGTH )
180 : : {
181 : 0 : pStream->Read(pMagicHeader, nLen);
182 : 0 : pMagicHeader[nLen] = '\0';
183 : 0 : if ( !strcmp(pMagicHeader, pVerStr2)
184 : 0 : || !strcmp(pMagicHeader, pVerStr5)
185 : 0 : || !strcmp(pMagicHeader, pVerStr6) )
186 : 0 : bRet = true;
187 : : }
188 : : }
189 : : }
190 : : }
191 : :
192 : 0 : delete pStream;
193 : 0 : return bRet;
194 : : }
195 : :
196 : :
197 : : // -----------------------------------------------------------------------------
198 : :
199 : 0 : void WordbookMigration::copyFiles()
200 : : {
201 : 0 : ::rtl::OUString sTargetDir;
202 : 0 : ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
203 : 0 : if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
204 : : {
205 : 0 : sTargetDir += sTargetSubDir;
206 : 0 : TStringVectorPtr aFileList = getFiles( m_sSourceDir );
207 : 0 : TStringVector::const_iterator aI = aFileList->begin();
208 : 0 : while ( aI != aFileList->end() )
209 : : {
210 : 0 : if (IsUserWordbook(*aI) )
211 : : {
212 : 0 : ::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
213 : 0 : ::rtl::OUString sTargetName = sTargetDir + sSourceLocalName;
214 : 0 : INetURLObject aURL( sTargetName );
215 : 0 : aURL.removeSegment();
216 : 0 : checkAndCreateDirectory( aURL );
217 : 0 : ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
218 : 0 : if ( aResult != ::osl::FileBase::E_None )
219 : : {
220 : 0 : ::rtl::OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
221 : 0 : aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
222 : 0 : + ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
223 : 0 : OSL_FAIL( aMsg.getStr() );
224 : 0 : }
225 : : }
226 : 0 : ++aI;
227 : 0 : }
228 : : }
229 : : else
230 : : {
231 : : OSL_FAIL( "WordbookMigration::copyFiles: no user installation!" );
232 : 0 : }
233 : 0 : }
234 : :
235 : : // -----------------------------------------------------------------------------
236 : : // XServiceInfo
237 : : // -----------------------------------------------------------------------------
238 : :
239 : 0 : ::rtl::OUString WordbookMigration::getImplementationName() throw (RuntimeException)
240 : : {
241 : 0 : return WordbookMigration_getImplementationName();
242 : : }
243 : :
244 : : // -----------------------------------------------------------------------------
245 : :
246 : 0 : sal_Bool WordbookMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
247 : : {
248 : 0 : Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
249 : 0 : const ::rtl::OUString* pNames = aNames.getConstArray();
250 : 0 : const ::rtl::OUString* pEnd = pNames + aNames.getLength();
251 : 0 : for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
252 : : ;
253 : :
254 : 0 : return pNames != pEnd;
255 : : }
256 : :
257 : : // -----------------------------------------------------------------------------
258 : :
259 : 0 : Sequence< ::rtl::OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
260 : : {
261 : 0 : return WordbookMigration_getSupportedServiceNames();
262 : : }
263 : :
264 : : // -----------------------------------------------------------------------------
265 : : // XInitialization
266 : : // -----------------------------------------------------------------------------
267 : :
268 : 0 : void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
269 : : {
270 : 0 : ::osl::MutexGuard aGuard( m_aMutex );
271 : :
272 : 0 : const Any* pIter = aArguments.getConstArray();
273 : 0 : const Any* pEnd = pIter + aArguments.getLength();
274 : 0 : for ( ; pIter != pEnd ; ++pIter )
275 : : {
276 : 0 : beans::NamedValue aValue;
277 : 0 : *pIter >>= aValue;
278 : 0 : if ( aValue.Name == "UserData" )
279 : : {
280 : 0 : if ( !(aValue.Value >>= m_sSourceDir) )
281 : : {
282 : : OSL_FAIL( "WordbookMigration::initialize: argument UserData has wrong type!" );
283 : : }
284 : 0 : m_sSourceDir += sSourceSubDir;
285 : : break;
286 : : }
287 : 0 : }
288 : 0 : }
289 : :
290 : : // -----------------------------------------------------------------------------
291 : : // XJob
292 : : // -----------------------------------------------------------------------------
293 : :
294 : 0 : Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
295 : : throw (lang::IllegalArgumentException, Exception, RuntimeException)
296 : : {
297 : 0 : ::osl::MutexGuard aGuard( m_aMutex );
298 : :
299 : 0 : copyFiles();
300 : :
301 : 0 : return Any();
302 : : }
303 : :
304 : : // =============================================================================
305 : : // component operations
306 : : // =============================================================================
307 : :
308 : 0 : Reference< XInterface > SAL_CALL WordbookMigration_create(
309 : : Reference< XComponentContext > const & )
310 : : SAL_THROW(())
311 : : {
312 : 0 : return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
313 : : }
314 : :
315 : : // -----------------------------------------------------------------------------
316 : :
317 : : //.........................................................................
318 : 0 : } // namespace migration
319 : : //.........................................................................
320 : :
321 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|