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