1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basicmigration.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
33 #include "basicmigration.hxx"
34 #include <tools/urlobj.hxx>
35 #include <unotools/bootstrap.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
42 //.........................................................................
45 //.........................................................................
48 static ::rtl::OUString sSourceUserBasic
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/basic" ) );
49 static ::rtl::OUString sTargetUserBasic
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/__basic_80" ) );
52 // =============================================================================
53 // component operations
54 // =============================================================================
56 ::rtl::OUString
BasicMigration_getImplementationName()
58 static ::rtl::OUString
* pImplName
= 0;
61 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
64 static ::rtl::OUString
aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.desktop.migration.Basic" ) );
65 pImplName
= &aImplName
;
71 // -----------------------------------------------------------------------------
73 Sequence
< ::rtl::OUString
> BasicMigration_getSupportedServiceNames()
75 static Sequence
< ::rtl::OUString
>* pNames
= 0;
78 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
81 static Sequence
< ::rtl::OUString
> aNames(1);
82 aNames
.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.migration.Basic" ) );
89 // =============================================================================
91 // =============================================================================
93 BasicMigration::BasicMigration()
97 // -----------------------------------------------------------------------------
99 BasicMigration::~BasicMigration()
103 // -----------------------------------------------------------------------------
105 TStringVectorPtr
BasicMigration::getFiles( const ::rtl::OUString
& rBaseURL
) const
107 TStringVectorPtr
aResult( new TStringVector
);
108 ::osl::Directory
aDir( rBaseURL
);
110 if ( aDir
.open() == ::osl::FileBase::E_None
)
112 // iterate over directory content
113 TStringVector aSubDirs
;
114 ::osl::DirectoryItem aItem
;
115 while ( aDir
.getNextItem( aItem
) == ::osl::FileBase::E_None
)
117 ::osl::FileStatus
aFileStatus( FileStatusMask_Type
| FileStatusMask_FileURL
);
118 if ( aItem
.getFileStatus( aFileStatus
) == ::osl::FileBase::E_None
)
120 if ( aFileStatus
.getFileType() == ::osl::FileStatus::Directory
)
121 aSubDirs
.push_back( aFileStatus
.getFileURL() );
123 aResult
->push_back( aFileStatus
.getFileURL() );
127 // iterate recursive over subfolders
128 TStringVector::const_iterator aI
= aSubDirs
.begin();
129 while ( aI
!= aSubDirs
.end() )
131 TStringVectorPtr aSubResult
= getFiles( *aI
);
132 aResult
->insert( aResult
->end(), aSubResult
->begin(), aSubResult
->end() );
140 // -----------------------------------------------------------------------------
142 ::osl::FileBase::RC
BasicMigration::checkAndCreateDirectory( INetURLObject
& rDirURL
)
144 ::osl::FileBase::RC aResult
= ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DECODE_TO_IURI
) );
145 if ( aResult
== ::osl::FileBase::E_NOENT
)
147 INetURLObject
aBaseURL( rDirURL
);
148 aBaseURL
.removeSegment();
149 checkAndCreateDirectory( aBaseURL
);
150 return ::osl::Directory::create( rDirURL
.GetMainURL( INetURLObject::DECODE_TO_IURI
) );
158 // -----------------------------------------------------------------------------
160 void BasicMigration::copyFiles()
162 ::rtl::OUString sTargetDir
;
163 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( sTargetDir
);
164 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
166 sTargetDir
+= sTargetUserBasic
;
167 TStringVectorPtr aFileList
= getFiles( m_sSourceDir
);
168 TStringVector::const_iterator aI
= aFileList
->begin();
169 while ( aI
!= aFileList
->end() )
171 ::rtl::OUString sLocalName
= aI
->copy( m_sSourceDir
.getLength() );
172 ::rtl::OUString sTargetName
= sTargetDir
+ sLocalName
;
173 INetURLObject
aURL( sTargetName
);
174 aURL
.removeSegment();
175 checkAndCreateDirectory( aURL
);
176 ::osl::FileBase::RC aResult
= ::osl::File::copy( *aI
, sTargetName
);
177 if ( aResult
!= ::osl::FileBase::E_None
)
179 ::rtl::OString
aMsg( "BasicMigration::copyFiles: cannot copy " );
180 aMsg
+= ::rtl::OUStringToOString( *aI
, RTL_TEXTENCODING_UTF8
) + " to "
181 + ::rtl::OUStringToOString( sTargetName
, RTL_TEXTENCODING_UTF8
);
182 OSL_ENSURE( sal_False
, aMsg
.getStr() );
189 OSL_ENSURE( sal_False
, "BasicMigration::copyFiles: no user installation!" );
193 // -----------------------------------------------------------------------------
195 // -----------------------------------------------------------------------------
197 ::rtl::OUString
BasicMigration::getImplementationName() throw (RuntimeException
)
199 return BasicMigration_getImplementationName();
202 // -----------------------------------------------------------------------------
204 sal_Bool
BasicMigration::supportsService( const ::rtl::OUString
& rServiceName
) throw (RuntimeException
)
206 Sequence
< ::rtl::OUString
> aNames( getSupportedServiceNames() );
207 const ::rtl::OUString
* pNames
= aNames
.getConstArray();
208 const ::rtl::OUString
* pEnd
= pNames
+ aNames
.getLength();
209 for ( ; pNames
!= pEnd
&& !pNames
->equals( rServiceName
); ++pNames
)
212 return pNames
!= pEnd
;
215 // -----------------------------------------------------------------------------
217 Sequence
< ::rtl::OUString
> BasicMigration::getSupportedServiceNames() throw (RuntimeException
)
219 return BasicMigration_getSupportedServiceNames();
222 // -----------------------------------------------------------------------------
224 // -----------------------------------------------------------------------------
226 void BasicMigration::initialize( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
)
228 ::osl::MutexGuard
aGuard( m_aMutex
);
230 const Any
* pIter
= aArguments
.getConstArray();
231 const Any
* pEnd
= pIter
+ aArguments
.getLength();
232 for ( ; pIter
!= pEnd
; ++pIter
)
234 beans::NamedValue aValue
;
236 if ( aValue
.Name
.equalsAscii( "UserData" ) )
238 if ( !(aValue
.Value
>>= m_sSourceDir
) )
240 OSL_ENSURE( false, "BasicMigration::initialize: argument UserData has wrong type!" );
242 m_sSourceDir
+= sSourceUserBasic
;
248 // -----------------------------------------------------------------------------
250 // -----------------------------------------------------------------------------
252 Any
BasicMigration::execute( const Sequence
< beans::NamedValue
>& )
253 throw (lang::IllegalArgumentException
, Exception
, RuntimeException
)
255 ::osl::MutexGuard
aGuard( m_aMutex
);
262 // =============================================================================
263 // component operations
264 // =============================================================================
266 Reference
< XInterface
> SAL_CALL
BasicMigration_create(
267 Reference
< XComponentContext
> const & )
270 return static_cast< lang::XTypeProvider
* >( new BasicMigration() );
273 // -----------------------------------------------------------------------------
275 //.........................................................................
276 } // namespace migration
277 //.........................................................................