xref: /AOO41X/main/desktop/source/migration/services/wordbookmigration.cxx (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir #include "wordbookmigration.hxx"
27cdf0e10cSrcweir #include <tools/urlobj.hxx>
28cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
29cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::com::sun::star;
32cdf0e10cSrcweir using namespace ::com::sun::star::uno;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //.........................................................................
36cdf0e10cSrcweir namespace migration
37cdf0e10cSrcweir {
38cdf0e10cSrcweir //.........................................................................
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     static ::rtl::OUString sSourceSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
42cdf0e10cSrcweir     static ::rtl::OUString sTargetSubDir = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/wordbook" ) );
43cdf0e10cSrcweir     static ::rtl::OUString sBaseName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/wordbook" ) );
44cdf0e10cSrcweir     static ::rtl::OUString sSuffix = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".dic" ) );
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     // =============================================================================
48cdf0e10cSrcweir     // component operations
49cdf0e10cSrcweir     // =============================================================================
50cdf0e10cSrcweir 
WordbookMigration_getImplementationName()51cdf0e10cSrcweir     ::rtl::OUString WordbookMigration_getImplementationName()
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         static ::rtl::OUString* pImplName = 0;
54cdf0e10cSrcweir 	    if ( !pImplName )
55cdf0e10cSrcweir 	    {
56cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
57cdf0e10cSrcweir             if ( !pImplName )
58cdf0e10cSrcweir 		    {
59cdf0e10cSrcweir                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Wordbooks" ) );
60cdf0e10cSrcweir 			    pImplName = &aImplName;
61cdf0e10cSrcweir 		    }
62cdf0e10cSrcweir 	    }
63cdf0e10cSrcweir 	    return *pImplName;
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     // -----------------------------------------------------------------------------
67cdf0e10cSrcweir 
WordbookMigration_getSupportedServiceNames()68cdf0e10cSrcweir     Sequence< ::rtl::OUString > WordbookMigration_getSupportedServiceNames()
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         static Sequence< ::rtl::OUString >* pNames = 0;
71cdf0e10cSrcweir 	    if ( !pNames )
72cdf0e10cSrcweir 	    {
73cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
74cdf0e10cSrcweir 		    if ( !pNames )
75cdf0e10cSrcweir 		    {
76cdf0e10cSrcweir                 static Sequence< ::rtl::OUString > aNames(1);
77cdf0e10cSrcweir                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Wordbooks" ) );
78cdf0e10cSrcweir                 pNames = &aNames;
79cdf0e10cSrcweir 		    }
80cdf0e10cSrcweir 	    }
81cdf0e10cSrcweir 	    return *pNames;
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // =============================================================================
85cdf0e10cSrcweir     // WordbookMigration
86cdf0e10cSrcweir     // =============================================================================
87cdf0e10cSrcweir 
WordbookMigration()88cdf0e10cSrcweir     WordbookMigration::WordbookMigration()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     // -----------------------------------------------------------------------------
93cdf0e10cSrcweir 
~WordbookMigration()94cdf0e10cSrcweir     WordbookMigration::~WordbookMigration()
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     // -----------------------------------------------------------------------------
99cdf0e10cSrcweir 
getFiles(const::rtl::OUString & rBaseURL) const100cdf0e10cSrcweir     TStringVectorPtr WordbookMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         TStringVectorPtr aResult( new TStringVector );
103cdf0e10cSrcweir         ::osl::Directory aDir( rBaseURL);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         if ( aDir.open() == ::osl::FileBase::E_None )
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             // iterate over directory content
108cdf0e10cSrcweir             TStringVector aSubDirs;
109cdf0e10cSrcweir             ::osl::DirectoryItem aItem;
110cdf0e10cSrcweir             while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
113cdf0e10cSrcweir                 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
114cdf0e10cSrcweir                 {
115cdf0e10cSrcweir                     if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
116cdf0e10cSrcweir                         aSubDirs.push_back( aFileStatus.getFileURL() );
117cdf0e10cSrcweir                     else
118cdf0e10cSrcweir                         aResult->push_back( aFileStatus.getFileURL() );
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             // iterate recursive over subfolders
123cdf0e10cSrcweir             TStringVector::const_iterator aI = aSubDirs.begin();
124cdf0e10cSrcweir             while ( aI != aSubDirs.end() )
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 TStringVectorPtr aSubResult = getFiles( *aI );
127cdf0e10cSrcweir                 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
128cdf0e10cSrcweir                 ++aI;
129cdf0e10cSrcweir             }
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         return aResult;
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     // -----------------------------------------------------------------------------
136cdf0e10cSrcweir 
checkAndCreateDirectory(INetURLObject & rDirURL)137cdf0e10cSrcweir     ::osl::FileBase::RC WordbookMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
140cdf0e10cSrcweir         if ( aResult == ::osl::FileBase::E_NOENT )
141cdf0e10cSrcweir         {
142cdf0e10cSrcweir             INetURLObject aBaseURL( rDirURL );
143cdf0e10cSrcweir             aBaseURL.removeSegment();
144cdf0e10cSrcweir             checkAndCreateDirectory( aBaseURL );
145cdf0e10cSrcweir             return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir         else
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             return aResult;
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir #define MAX_HEADER_LENGTH 16
IsUserWordbook(const::rtl::OUString & rFile)154cdf0e10cSrcweir bool IsUserWordbook( const ::rtl::OUString& rFile )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir 	static const sal_Char*		pVerStr2	= "WBSWG2";
157cdf0e10cSrcweir 	static const sal_Char*		pVerStr5	= "WBSWG5";
158cdf0e10cSrcweir 	static const sal_Char*		pVerStr6	= "WBSWG6";
159cdf0e10cSrcweir 	static const sal_Char*      pVerOOo7    = "OOoUserDict1";
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     bool bRet = false;
162cdf0e10cSrcweir 	SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( String(rFile), STREAM_STD_READ );
163cdf0e10cSrcweir 	if ( pStream && !pStream->GetError() )
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 	    sal_Size nSniffPos = pStream->Tell();
166cdf0e10cSrcweir 		static sal_Size nVerOOo7Len = sal::static_int_cast< sal_Size >(strlen( pVerOOo7 ));
167cdf0e10cSrcweir 		sal_Char pMagicHeader[MAX_HEADER_LENGTH];
168cdf0e10cSrcweir 		pMagicHeader[ nVerOOo7Len ] = '\0';
169cdf0e10cSrcweir 		if ((pStream->Read((void *) pMagicHeader, nVerOOo7Len) == nVerOOo7Len))
170cdf0e10cSrcweir 		{
171cdf0e10cSrcweir 			if ( !strcmp(pMagicHeader, pVerOOo7) )
172cdf0e10cSrcweir 				bRet = true;
173cdf0e10cSrcweir 			else
174cdf0e10cSrcweir 			{
175cdf0e10cSrcweir 		        sal_uInt16 nLen;
176cdf0e10cSrcweir 		        pStream->Seek (nSniffPos);
177cdf0e10cSrcweir 		        *pStream >> nLen;
178cdf0e10cSrcweir 				if ( nLen < MAX_HEADER_LENGTH )
179cdf0e10cSrcweir 				{
180cdf0e10cSrcweir 			       pStream->Read(pMagicHeader, nLen);
181cdf0e10cSrcweir 			       pMagicHeader[nLen] = '\0';
182cdf0e10cSrcweir 					if ( !strcmp(pMagicHeader, pVerStr2)
183cdf0e10cSrcweir 					 ||  !strcmp(pMagicHeader, pVerStr5)
184cdf0e10cSrcweir 			         ||  !strcmp(pMagicHeader, pVerStr6) )
185cdf0e10cSrcweir 					bRet = true;
186cdf0e10cSrcweir 				}
187cdf0e10cSrcweir 			}
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	delete pStream;
192cdf0e10cSrcweir 	return bRet;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     // -----------------------------------------------------------------------------
197cdf0e10cSrcweir 
copyFiles()198cdf0e10cSrcweir     void WordbookMigration::copyFiles()
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         ::rtl::OUString sTargetDir;
201cdf0e10cSrcweir         ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
202cdf0e10cSrcweir         if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir             sTargetDir += sTargetSubDir;
205cdf0e10cSrcweir             TStringVectorPtr aFileList = getFiles( m_sSourceDir );
206cdf0e10cSrcweir             TStringVector::const_iterator aI = aFileList->begin();
207cdf0e10cSrcweir             while ( aI != aFileList->end() )
208cdf0e10cSrcweir             {
209cdf0e10cSrcweir 				if (IsUserWordbook(*aI) )
210cdf0e10cSrcweir 				{
211cdf0e10cSrcweir 					::rtl::OUString sSourceLocalName = aI->copy( m_sSourceDir.getLength() );
212cdf0e10cSrcweir 					::rtl::OUString sTargetName = sTargetDir + sSourceLocalName;
213cdf0e10cSrcweir 					INetURLObject aURL( sTargetName );
214cdf0e10cSrcweir 					aURL.removeSegment();
215cdf0e10cSrcweir 					checkAndCreateDirectory( aURL );
216cdf0e10cSrcweir 					::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
217cdf0e10cSrcweir 					if ( aResult != ::osl::FileBase::E_None )
218cdf0e10cSrcweir 					{
219cdf0e10cSrcweir 						::rtl::OString aMsg( "WordbookMigration::copyFiles: cannot copy " );
220cdf0e10cSrcweir 						aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
221cdf0e10cSrcweir 							 +  ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
222cdf0e10cSrcweir 						OSL_ENSURE( sal_False, aMsg.getStr() );
223cdf0e10cSrcweir 					}
224cdf0e10cSrcweir 				}
225cdf0e10cSrcweir 				++aI;
226cdf0e10cSrcweir             }
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         else
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir             OSL_ENSURE( sal_False, "WordbookMigration::copyFiles: no user installation!" );
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     // -----------------------------------------------------------------------------
235cdf0e10cSrcweir     // XServiceInfo
236cdf0e10cSrcweir     // -----------------------------------------------------------------------------
237cdf0e10cSrcweir 
getImplementationName()238cdf0e10cSrcweir     ::rtl::OUString WordbookMigration::getImplementationName() throw (RuntimeException)
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         return WordbookMigration_getImplementationName();
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     // -----------------------------------------------------------------------------
244cdf0e10cSrcweir 
supportsService(const::rtl::OUString & rServiceName)245cdf0e10cSrcweir     sal_Bool WordbookMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
246cdf0e10cSrcweir     {
247cdf0e10cSrcweir 	    Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
248cdf0e10cSrcweir 	    const ::rtl::OUString* pNames = aNames.getConstArray();
249cdf0e10cSrcweir 	    const ::rtl::OUString* pEnd = pNames + aNames.getLength();
250cdf0e10cSrcweir 	    for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
251cdf0e10cSrcweir 		    ;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	    return pNames != pEnd;
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     // -----------------------------------------------------------------------------
257cdf0e10cSrcweir 
getSupportedServiceNames()258cdf0e10cSrcweir     Sequence< ::rtl::OUString > WordbookMigration::getSupportedServiceNames() throw (RuntimeException)
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir         return WordbookMigration_getSupportedServiceNames();
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     // -----------------------------------------------------------------------------
264cdf0e10cSrcweir     // XInitialization
265cdf0e10cSrcweir     // -----------------------------------------------------------------------------
266cdf0e10cSrcweir 
initialize(const Sequence<Any> & aArguments)267cdf0e10cSrcweir     void WordbookMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir         const Any* pIter = aArguments.getConstArray();
272cdf0e10cSrcweir         const Any* pEnd = pIter + aArguments.getLength();
273cdf0e10cSrcweir         for ( ; pIter != pEnd ; ++pIter )
274cdf0e10cSrcweir         {
275cdf0e10cSrcweir             beans::NamedValue aValue;
276cdf0e10cSrcweir             *pIter >>= aValue;
277cdf0e10cSrcweir             if ( aValue.Name.equalsAscii( "UserData" ) )
278cdf0e10cSrcweir             {
279cdf0e10cSrcweir                 if ( !(aValue.Value >>= m_sSourceDir) )
280cdf0e10cSrcweir                 {
281cdf0e10cSrcweir                     OSL_ENSURE( false, "WordbookMigration::initialize: argument UserData has wrong type!" );
282cdf0e10cSrcweir                 }
283cdf0e10cSrcweir                 m_sSourceDir += sSourceSubDir;
284cdf0e10cSrcweir                 break;
285cdf0e10cSrcweir             }
286cdf0e10cSrcweir         }
287cdf0e10cSrcweir     }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     // -----------------------------------------------------------------------------
290cdf0e10cSrcweir     // XJob
291cdf0e10cSrcweir     // -----------------------------------------------------------------------------
292cdf0e10cSrcweir 
execute(const Sequence<beans::NamedValue> &)293cdf0e10cSrcweir     Any WordbookMigration::execute( const Sequence< beans::NamedValue >& )
294cdf0e10cSrcweir         throw (lang::IllegalArgumentException, Exception, RuntimeException)
295cdf0e10cSrcweir     {
296cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
297cdf0e10cSrcweir 
298cdf0e10cSrcweir         copyFiles();
299cdf0e10cSrcweir 
300cdf0e10cSrcweir         return Any();
301cdf0e10cSrcweir     }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     // =============================================================================
304cdf0e10cSrcweir     // component operations
305cdf0e10cSrcweir     // =============================================================================
306cdf0e10cSrcweir 
WordbookMigration_create(Reference<XComponentContext> const &)307cdf0e10cSrcweir     Reference< XInterface > SAL_CALL WordbookMigration_create(
308cdf0e10cSrcweir         Reference< XComponentContext > const & )
309cdf0e10cSrcweir         SAL_THROW( () )
310cdf0e10cSrcweir     {
311cdf0e10cSrcweir         return static_cast< lang::XTypeProvider * >( new WordbookMigration() );
312cdf0e10cSrcweir     }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     // -----------------------------------------------------------------------------
315cdf0e10cSrcweir 
316cdf0e10cSrcweir //.........................................................................
317cdf0e10cSrcweir }	// namespace migration
318cdf0e10cSrcweir //.........................................................................
319