Update ooo320-m1
[ooovba.git] / desktop / source / deployment / migration / dp_migration.cxx
blob5225d0cc4c62c09da32e22962c2751edb8c6fbf6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_migration.cxx,v $
10 * $Revision: 1.10 $
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"
34 #include "dp_misc.h"
35 #include "dp_ucb.h"
36 #include "cppuhelper/implbase1.hxx"
37 #include "cppuhelper/implbase2.hxx"
38 #include "cppuhelper/exc_hlp.hxx"
39 #include "ucbhelper/content.hxx"
40 #include "comphelper/anytostring.hxx"
41 #include "comphelper/servicedecl.hxx"
42 #include "com/sun/star/lang/WrappedTargetException.hpp"
43 #include "com/sun/star/task/XJob.hpp"
44 #include "com/sun/star/task/XInteractionAbort.hpp"
45 #include "com/sun/star/task/XInteractionApprove.hpp"
46 #include "com/sun/star/sdbc/XResultSet.hpp"
47 #include "com/sun/star/sdbc/XRow.hpp"
48 #include "com/sun/star/ucb/XContentAccess.hpp"
49 #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::ucb;
54 using namespace ::com::sun::star::uno;
55 using namespace ::dp_misc;
56 using ::rtl::OUString;
58 namespace dp_migration {
60 class MigrationImpl : public ::cppu::WeakImplHelper1<task::XJob>
62 struct CommandEnvironmentImpl
63 : public ::cppu::WeakImplHelper2< XCommandEnvironment,
64 task::XInteractionHandler >
66 // XCommandEnvironment
67 virtual Reference<task::XInteractionHandler> SAL_CALL
68 getInteractionHandler() throw (RuntimeException);
69 virtual Reference<XProgressHandler> SAL_CALL getProgressHandler()
70 throw (RuntimeException);
71 // XInteractionHandler
72 virtual void SAL_CALL handle(
73 Reference<task::XInteractionRequest> const & xRequest )
74 throw (RuntimeException);
77 const Reference<XComponentContext> m_xContext;
78 OUString m_userData;
80 protected:
81 virtual ~MigrationImpl();
82 public:
83 MigrationImpl( Sequence<Any> const & args,
84 Reference<XComponentContext> const & xComponentContext );
86 // XJob
87 virtual Any SAL_CALL execute( Sequence<beans::NamedValue> const & args )
88 throw (lang::IllegalArgumentException, Exception, RuntimeException);
91 MigrationImpl::~MigrationImpl()
95 MigrationImpl::MigrationImpl(
96 Sequence<Any> const & args, Reference<XComponentContext> const & xContext )
97 : m_xContext(xContext)
99 for ( sal_Int32 pos = args.getLength(); pos--; )
101 const beans::NamedValue nv(args[pos].get<beans::NamedValue>());
102 if (nv.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("UserData") ))
103 m_userData = nv.Value.get<OUString>();
105 if (m_userData.getLength() == 0)
106 throw lang::IllegalArgumentException( OUSTR("missing UserData!"), 0,
107 static_cast<sal_Int16>(-1) );
110 // XJob
111 Any MigrationImpl::execute( Sequence<beans::NamedValue> const & )
112 throw (lang::IllegalArgumentException, Exception, RuntimeException)
114 const Reference<deployment::XPackageManager> xManager(
115 deployment::thePackageManagerFactory::get(
116 m_xContext )->getPackageManager( OUSTR("user") ) );
117 ::ucbhelper::Content packagesDir;
118 if (create_ucb_content( &packagesDir,
119 makeURL( m_userData, OUSTR("user/uno_packages") ),
120 Reference<XCommandEnvironment>(),
121 false /* no throw */ ))
123 const Reference<XCommandEnvironment> xCmdEnv(
124 new CommandEnvironmentImpl );
125 OUString const & strTitle = StrTitle::get();
126 const Reference<sdbc::XResultSet> xResultSet(
127 packagesDir.createCursor( Sequence<OUString>( &strTitle, 1 ),
128 ::ucbhelper::INCLUDE_DOCUMENTS_ONLY ) );
129 while (xResultSet->next())
131 Reference<sdbc::XRow> xRow( xResultSet, UNO_QUERY_THROW );
132 const OUString title( xRow->getString( 1 /* Title */ ) );
133 // exclude stampIt, not migratable to OOo 2.0:
134 if (title.matchIgnoreAsciiCaseAsciiL(
135 RTL_CONSTASCII_STRINGPARAM("SSICONCT.") ))
136 continue;
137 const OUString sourceURL( Reference<XContentAccess>(
138 xResultSet, UNO_QUERY_THROW )
139 ->queryContentIdentifierString() );
140 try {
141 xManager->addPackage(
142 sourceURL, OUString() /* detect media-type */,
143 Reference<task::XAbortChannel>(), xCmdEnv );
145 catch (RuntimeException &) {
146 throw;
148 catch (Exception &) {
149 OSL_ENSURE( 0, ::rtl::OUStringToOString(
150 ::comphelper::anyToString(
151 ::cppu::getCaughtException() ),
152 RTL_TEXTENCODING_UTF8 ).getStr() );
156 return Any();
159 // XCommandEnvironment
160 Reference<task::XInteractionHandler>
161 MigrationImpl::CommandEnvironmentImpl::getInteractionHandler()
162 throw (RuntimeException)
164 return this;
167 Reference<XProgressHandler>
168 MigrationImpl::CommandEnvironmentImpl::getProgressHandler()
169 throw (RuntimeException)
171 return Reference<XProgressHandler>();
174 // XInteractionHandler
175 void MigrationImpl::CommandEnvironmentImpl::handle(
176 Reference<task::XInteractionRequest> const & xRequest )
177 throw (RuntimeException)
179 Any request( xRequest->getRequest() );
180 OSL_ASSERT( request.getValueTypeClass() == TypeClass_EXCEPTION );
181 #if OSL_DEBUG_LEVEL > 1
182 OSL_TRACE( "[dp_migration.cxx] incoming request:\n%s\n",
183 ::rtl::OUStringToOString( ::comphelper::anyToString(request),
184 RTL_TEXTENCODING_UTF8 ).getStr() );
185 #endif
187 // selections:
188 bool approve = false;
189 bool abort = false;
191 lang::WrappedTargetException wtExc;
192 if (request >>= wtExc) {
193 OSL_ENSURE( 0, ::rtl::OUStringToOString(
194 ::comphelper::anyToString(wtExc.TargetException),
195 RTL_TEXTENCODING_UTF8 ).getStr() );
197 // ignore intermediate errors of legacy packages, i.e.
198 // former pkgchk behaviour:
199 const Reference<deployment::XPackage> xPackage(
200 wtExc.Context, UNO_QUERY );
201 OSL_ASSERT( xPackage.is() );
202 if (xPackage.is()) {
203 const Reference<deployment::XPackageTypeInfo> xPackageType(
204 xPackage->getPackageType() );
205 OSL_ASSERT( xPackageType.is() );
206 if (xPackageType.is()) {
207 approve = (xPackage->isBundle() &&
208 xPackageType->getMediaType().matchAsciiL(
209 RTL_CONSTASCII_STRINGPARAM(
210 "application/"
211 "vnd.sun.star.legacy-package-bundle") ));
214 abort = !approve;
216 else
217 return; // unknown request => no selection at all
219 // select:
220 const Sequence< Reference<task::XInteractionContinuation> > conts(
221 xRequest->getContinuations() );
222 for ( sal_Int32 pos = 0; pos < conts.getLength(); ++pos )
224 if (approve) {
225 const Reference<task::XInteractionApprove> xInteractionApprove(
226 conts[ pos ], UNO_QUERY );
227 if (xInteractionApprove.is()) {
228 xInteractionApprove->select();
229 // don't query again for ongoing continuations:
230 approve = false;
233 else if (abort) {
234 const Reference<task::XInteractionAbort> xInteractionAbort(
235 conts[ pos ], UNO_QUERY );
236 if (xInteractionAbort.is()) {
237 xInteractionAbort->select();
238 // don't query again for ongoing continuations:
239 abort = false;
245 namespace sdecl = comphelper::service_decl;
246 sdecl::class_<MigrationImpl, sdecl::with_args<true> > serviceMI;
247 extern sdecl::ServiceDecl const serviceDecl(
248 serviceMI,
249 // a private one (config entry):
250 "com.sun.star.comp.deployment.migration.Migration_2_0",
251 "com.sun.star.comp.deployment.migration.Migration_2_0" );
253 } // namespace dp_migration