1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_desktop.hxx"
30 #include "basicmigration.hxx"
31 #include <tools/urlobj.hxx>
32 #include <unotools/bootstrap.hxx>
33 
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 
38 
39 //.........................................................................
40 namespace migration
41 {
42 //.........................................................................
43 
44 
45     static ::rtl::OUString sSourceUserBasic = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/basic" ) );
46     static ::rtl::OUString sTargetUserBasic = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/__basic_80" ) );
47 
48 
49     // =============================================================================
50     // component operations
51     // =============================================================================
52 
53     ::rtl::OUString BasicMigration_getImplementationName()
54     {
55         static ::rtl::OUString* pImplName = 0;
56 	    if ( !pImplName )
57 	    {
58             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
59             if ( !pImplName )
60 		    {
61                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Basic" ) );
62 			    pImplName = &aImplName;
63 		    }
64 	    }
65 	    return *pImplName;
66     }
67 
68     // -----------------------------------------------------------------------------
69 
70     Sequence< ::rtl::OUString > BasicMigration_getSupportedServiceNames()
71     {
72         static Sequence< ::rtl::OUString >* pNames = 0;
73 	    if ( !pNames )
74 	    {
75             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
76 		    if ( !pNames )
77 		    {
78                 static Sequence< ::rtl::OUString > aNames(1);
79                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Basic" ) );
80                 pNames = &aNames;
81 		    }
82 	    }
83 	    return *pNames;
84     }
85 
86     // =============================================================================
87     // BasicMigration
88     // =============================================================================
89 
90     BasicMigration::BasicMigration()
91     {
92     }
93 
94     // -----------------------------------------------------------------------------
95 
96     BasicMigration::~BasicMigration()
97     {
98     }
99 
100     // -----------------------------------------------------------------------------
101 
102     TStringVectorPtr BasicMigration::getFiles( const ::rtl::OUString& rBaseURL ) const
103     {
104         TStringVectorPtr aResult( new TStringVector );
105         ::osl::Directory aDir( rBaseURL);
106 
107         if ( aDir.open() == ::osl::FileBase::E_None )
108         {
109             // iterate over directory content
110             TStringVector aSubDirs;
111             ::osl::DirectoryItem aItem;
112             while ( aDir.getNextItem( aItem ) == ::osl::FileBase::E_None )
113             {
114                 ::osl::FileStatus aFileStatus( FileStatusMask_Type | FileStatusMask_FileURL );
115                 if ( aItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None )
116                 {
117                     if ( aFileStatus.getFileType() == ::osl::FileStatus::Directory )
118                         aSubDirs.push_back( aFileStatus.getFileURL() );
119                     else
120                         aResult->push_back( aFileStatus.getFileURL() );
121                 }
122             }
123 
124             // iterate recursive over subfolders
125             TStringVector::const_iterator aI = aSubDirs.begin();
126             while ( aI != aSubDirs.end() )
127             {
128                 TStringVectorPtr aSubResult = getFiles( *aI );
129                 aResult->insert( aResult->end(), aSubResult->begin(), aSubResult->end() );
130                 ++aI;
131             }
132         }
133 
134         return aResult;
135     }
136 
137     // -----------------------------------------------------------------------------
138 
139     ::osl::FileBase::RC BasicMigration::checkAndCreateDirectory( INetURLObject& rDirURL )
140     {
141         ::osl::FileBase::RC aResult = ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
142         if ( aResult == ::osl::FileBase::E_NOENT )
143         {
144             INetURLObject aBaseURL( rDirURL );
145             aBaseURL.removeSegment();
146             checkAndCreateDirectory( aBaseURL );
147             return ::osl::Directory::create( rDirURL.GetMainURL( INetURLObject::DECODE_TO_IURI ) );
148         }
149         else
150         {
151             return aResult;
152         }
153     }
154 
155     // -----------------------------------------------------------------------------
156 
157     void BasicMigration::copyFiles()
158     {
159         ::rtl::OUString sTargetDir;
160         ::utl::Bootstrap::PathStatus aStatus = ::utl::Bootstrap::locateUserInstallation( sTargetDir );
161         if ( aStatus == ::utl::Bootstrap::PATH_EXISTS )
162         {
163             sTargetDir += sTargetUserBasic;
164             TStringVectorPtr aFileList = getFiles( m_sSourceDir );
165             TStringVector::const_iterator aI = aFileList->begin();
166             while ( aI != aFileList->end() )
167             {
168                 ::rtl::OUString sLocalName = aI->copy( m_sSourceDir.getLength() );
169                 ::rtl::OUString sTargetName = sTargetDir + sLocalName;
170                 INetURLObject aURL( sTargetName );
171                 aURL.removeSegment();
172                 checkAndCreateDirectory( aURL );
173                 ::osl::FileBase::RC aResult = ::osl::File::copy( *aI, sTargetName );
174                 if ( aResult != ::osl::FileBase::E_None )
175                 {
176                     ::rtl::OString aMsg( "BasicMigration::copyFiles: cannot copy " );
177                     aMsg += ::rtl::OUStringToOString( *aI, RTL_TEXTENCODING_UTF8 ) + " to "
178                          +  ::rtl::OUStringToOString( sTargetName, RTL_TEXTENCODING_UTF8 );
179                     OSL_ENSURE( sal_False, aMsg.getStr() );
180                 }
181                 ++aI;
182             }
183         }
184         else
185         {
186             OSL_ENSURE( sal_False, "BasicMigration::copyFiles: no user installation!" );
187         }
188     }
189 
190     // -----------------------------------------------------------------------------
191     // XServiceInfo
192     // -----------------------------------------------------------------------------
193 
194     ::rtl::OUString BasicMigration::getImplementationName() throw (RuntimeException)
195     {
196         return BasicMigration_getImplementationName();
197     }
198 
199     // -----------------------------------------------------------------------------
200 
201     sal_Bool BasicMigration::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
202     {
203 	    Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
204 	    const ::rtl::OUString* pNames = aNames.getConstArray();
205 	    const ::rtl::OUString* pEnd = pNames + aNames.getLength();
206 	    for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
207 		    ;
208 
209 	    return pNames != pEnd;
210     }
211 
212     // -----------------------------------------------------------------------------
213 
214     Sequence< ::rtl::OUString > BasicMigration::getSupportedServiceNames() throw (RuntimeException)
215     {
216         return BasicMigration_getSupportedServiceNames();
217     }
218 
219     // -----------------------------------------------------------------------------
220     // XInitialization
221     // -----------------------------------------------------------------------------
222 
223     void BasicMigration::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
224     {
225         ::osl::MutexGuard aGuard( m_aMutex );
226 
227         const Any* pIter = aArguments.getConstArray();
228         const Any* pEnd = pIter + aArguments.getLength();
229         for ( ; pIter != pEnd ; ++pIter )
230         {
231             beans::NamedValue aValue;
232             *pIter >>= aValue;
233             if ( aValue.Name.equalsAscii( "UserData" ) )
234             {
235                 if ( !(aValue.Value >>= m_sSourceDir) )
236                 {
237                     OSL_ENSURE( false, "BasicMigration::initialize: argument UserData has wrong type!" );
238                 }
239                 m_sSourceDir += sSourceUserBasic;
240                 break;
241             }
242         }
243     }
244 
245     // -----------------------------------------------------------------------------
246     // XJob
247     // -----------------------------------------------------------------------------
248 
249     Any BasicMigration::execute( const Sequence< beans::NamedValue >& )
250         throw (lang::IllegalArgumentException, Exception, RuntimeException)
251     {
252         ::osl::MutexGuard aGuard( m_aMutex );
253 
254         copyFiles();
255 
256         return Any();
257     }
258 
259     // =============================================================================
260     // component operations
261     // =============================================================================
262 
263     Reference< XInterface > SAL_CALL BasicMigration_create(
264         Reference< XComponentContext > const & )
265         SAL_THROW( () )
266     {
267         return static_cast< lang::XTypeProvider * >( new BasicMigration() );
268     }
269 
270     // -----------------------------------------------------------------------------
271 
272 //.........................................................................
273 }	// namespace migration
274 //.........................................................................
275