xref: /aoo41x/main/desktop/source/app/userinstall.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "userinstall.hxx"
33*cdf0e10cSrcweir #include "langselect.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <stdio.h>
36*cdf0e10cSrcweir #include <rtl/ustring.hxx>
37*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
38*cdf0e10cSrcweir #include <osl/file.hxx>
39*cdf0e10cSrcweir #include <osl/mutex.hxx>
40*cdf0e10cSrcweir #include <osl/process.h>
41*cdf0e10cSrcweir #include <osl/diagnose.h>
42*cdf0e10cSrcweir #include <vos/security.hxx>
43*cdf0e10cSrcweir #include <vos/ref.hxx>
44*cdf0e10cSrcweir #include <vos/process.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #ifndef _TOOLS_RESMGR_HXX_
47*cdf0e10cSrcweir #include <tools/resmgr.hxx>
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
50*cdf0e10cSrcweir #include <svl/languageoptions.hxx>
51*cdf0e10cSrcweir #ifndef _SVTOOLS_SYSLOCALEOPTIONSOPTIONS_HXX
52*cdf0e10cSrcweir #include <unotools/syslocaleoptions.hxx>
53*cdf0e10cSrcweir #endif
54*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
55*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
58*cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
59*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
60*cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/beans/XHierarchicalPropertySet.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
64*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/lang/XLocalizable.hpp>
66*cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir #include "app.hxx"
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir using namespace rtl;
71*cdf0e10cSrcweir using namespace osl;
72*cdf0e10cSrcweir using namespace utl;
73*cdf0e10cSrcweir using namespace com::sun::star::container;
74*cdf0e10cSrcweir using namespace com::sun::star::uno;
75*cdf0e10cSrcweir using namespace com::sun::star::lang;
76*cdf0e10cSrcweir using namespace com::sun::star::beans;
77*cdf0e10cSrcweir using namespace com::sun::star::util;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir namespace desktop {
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     static UserInstall::UserInstallError create_user_install(OUString&);
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     static bool is_user_install()
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         try
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             OUString sConfigSrvc(
89*cdf0e10cSrcweir                  RTL_CONSTASCII_USTRINGPARAM(
90*cdf0e10cSrcweir                     "com.sun.star.configuration.ConfigurationProvider" ) );
91*cdf0e10cSrcweir             OUString sAccessSrvc(
92*cdf0e10cSrcweir                  RTL_CONSTASCII_USTRINGPARAM(
93*cdf0e10cSrcweir                     "com.sun.star.configuration.ConfigurationAccess" ) );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir             // get configuration provider
96*cdf0e10cSrcweir             Reference< XMultiServiceFactory > theMSF
97*cdf0e10cSrcweir                 = comphelper::getProcessServiceFactory();
98*cdf0e10cSrcweir             Reference< XMultiServiceFactory > theConfigProvider
99*cdf0e10cSrcweir                 = Reference< XMultiServiceFactory >(
100*cdf0e10cSrcweir                     theMSF->createInstance(sConfigSrvc), UNO_QUERY_THROW);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             // localize the provider to user selection
103*cdf0e10cSrcweir //            Reference< XLocalizable > localizable(theConfigProvider, UNO_QUERY_THROW);
104*cdf0e10cSrcweir //            LanguageType aUserLanguageType = LanguageSelection::getLanguageType();
105*cdf0e10cSrcweir //            Locale aLocale( MsLangId::convertLanguageToIsoString(aUserLanguageType));
106*cdf0e10cSrcweir //            localizable->setLocale(aLocale);
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             Reference< XLocalizable > localizable(theConfigProvider, UNO_QUERY_THROW);
109*cdf0e10cSrcweir             OUString aUserLanguage = LanguageSelection::getLanguageString();
110*cdf0e10cSrcweir             Locale aLocale = LanguageSelection::IsoStringToLocale(aUserLanguage);
111*cdf0e10cSrcweir             localizable->setLocale(aLocale);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             Sequence< Any > theArgs(1);
114*cdf0e10cSrcweir             NamedValue v;
115*cdf0e10cSrcweir             v.Name = OUString::createFromAscii("NodePath");
116*cdf0e10cSrcweir             v.Value = makeAny(OUString::createFromAscii("org.openoffice.Setup"));
117*cdf0e10cSrcweir             theArgs[0] <<= v;
118*cdf0e10cSrcweir             Reference< XHierarchicalNameAccess> hnacc(
119*cdf0e10cSrcweir                 theConfigProvider->createInstanceWithArguments(
120*cdf0e10cSrcweir                     sAccessSrvc, theArgs), UNO_QUERY_THROW);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             try
123*cdf0e10cSrcweir             {
124*cdf0e10cSrcweir                 sal_Bool bValue = sal_False;
125*cdf0e10cSrcweir                 hnacc->getByHierarchicalName(
126*cdf0e10cSrcweir                         OUString( RTL_CONSTASCII_USTRINGPARAM(
127*cdf0e10cSrcweir                             "Office/ooSetupInstCompleted" ) ) ) >>= bValue;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir                 return bValue ? true : false;
130*cdf0e10cSrcweir             }
131*cdf0e10cSrcweir             catch ( NoSuchElementException const & )
132*cdf0e10cSrcweir             {
133*cdf0e10cSrcweir                 // just return false in this case.
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir         catch (Exception const & e)
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             OString msg(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
139*cdf0e10cSrcweir             OSL_ENSURE(sal_False, msg.getStr());
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         return false;
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     UserInstall::UserInstallError UserInstall::finalize()
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir         OUString aUserInstallPath;
148*cdf0e10cSrcweir         utl::Bootstrap::PathStatus aLocateResult =
149*cdf0e10cSrcweir             utl::Bootstrap::locateUserInstallation(aUserInstallPath);
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         switch (aLocateResult) {
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir             case utl::Bootstrap::DATA_INVALID:
154*cdf0e10cSrcweir             case utl::Bootstrap::DATA_MISSING:
155*cdf0e10cSrcweir             case utl::Bootstrap::DATA_UNKNOWN:
156*cdf0e10cSrcweir                 // cannot find a valid path or path is missing
157*cdf0e10cSrcweir                 return E_Unknown;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir             case utl::Bootstrap::PATH_EXISTS:
160*cdf0e10cSrcweir             {
161*cdf0e10cSrcweir                 // path exists, check if an installation lives there
162*cdf0e10cSrcweir                 if ( is_user_install() )
163*cdf0e10cSrcweir                 {
164*cdf0e10cSrcweir                     return E_None;
165*cdf0e10cSrcweir                 }
166*cdf0e10cSrcweir                 // Note: fall-thru intended.
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir             case utl::Bootstrap::PATH_VALID:
169*cdf0e10cSrcweir                 // found a path but need to create user install
170*cdf0e10cSrcweir                 return create_user_install(aUserInstallPath);
171*cdf0e10cSrcweir             default:
172*cdf0e10cSrcweir                 return E_Unknown;
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     static osl::FileBase::RC copy_recursive( const rtl::OUString& srcUnqPath, const rtl::OUString& dstUnqPath)
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         FileBase::RC err;
180*cdf0e10cSrcweir         DirectoryItem aDirItem;
181*cdf0e10cSrcweir         DirectoryItem::get(srcUnqPath, aDirItem);
182*cdf0e10cSrcweir         FileStatus aFileStatus(FileStatusMask_All);
183*cdf0e10cSrcweir         aDirItem.getFileStatus(aFileStatus);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         if( aFileStatus.getFileType() == FileStatus::Directory)
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             // create directory if not already there
188*cdf0e10cSrcweir             err = Directory::create( dstUnqPath );
189*cdf0e10cSrcweir             if (err == osl::FileBase::E_EXIST)
190*cdf0e10cSrcweir                 err = osl::FileBase::E_None;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             FileBase::RC next = err;
193*cdf0e10cSrcweir             if (err == osl::FileBase::E_None)
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                 // iterate through directory contents
196*cdf0e10cSrcweir                 Directory aDir( srcUnqPath );
197*cdf0e10cSrcweir                 aDir.open();
198*cdf0e10cSrcweir                 while (err ==  osl::FileBase::E_None &&
199*cdf0e10cSrcweir                     (next = aDir.getNextItem( aDirItem )) == osl::FileBase::E_None)
200*cdf0e10cSrcweir                 {
201*cdf0e10cSrcweir                     aDirItem.getFileStatus(aFileStatus);
202*cdf0e10cSrcweir                     // generate new src/dst pair and make recursive call
203*cdf0e10cSrcweir                     rtl::OUString newSrcUnqPath = aFileStatus.getFileURL();
204*cdf0e10cSrcweir                     rtl::OUString newDstUnqPath = dstUnqPath;
205*cdf0e10cSrcweir                     rtl::OUString itemname = aFileStatus.getFileName();
206*cdf0e10cSrcweir                     // append trailing '/' if needed
207*cdf0e10cSrcweir                     if (newDstUnqPath.lastIndexOf(sal_Unicode('/')) != newDstUnqPath.getLength()-1)
208*cdf0e10cSrcweir                         newDstUnqPath += rtl::OUString::createFromAscii("/");
209*cdf0e10cSrcweir                     newDstUnqPath += itemname;
210*cdf0e10cSrcweir                     // recursion
211*cdf0e10cSrcweir                     err = copy_recursive(newSrcUnqPath, newDstUnqPath);
212*cdf0e10cSrcweir                 }
213*cdf0e10cSrcweir                 aDir.close();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir                 if ( err != osl::FileBase::E_None )
216*cdf0e10cSrcweir                     return err;
217*cdf0e10cSrcweir                 if( next != FileBase::E_NOENT )
218*cdf0e10cSrcweir                     err = FileBase::E_INVAL;
219*cdf0e10cSrcweir             }
220*cdf0e10cSrcweir         }
221*cdf0e10cSrcweir         else
222*cdf0e10cSrcweir         {
223*cdf0e10cSrcweir             // copy single file - foldback
224*cdf0e10cSrcweir             err = File::copy( srcUnqPath,dstUnqPath );
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir         return err;
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     static const char *pszSrcList[] = {
230*cdf0e10cSrcweir         "/presets",
231*cdf0e10cSrcweir         NULL
232*cdf0e10cSrcweir     };
233*cdf0e10cSrcweir     static const char *pszDstList[] = {
234*cdf0e10cSrcweir         "/user",
235*cdf0e10cSrcweir         NULL
236*cdf0e10cSrcweir     };
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     static UserInstall::UserInstallError create_user_install(OUString& aUserPath)
240*cdf0e10cSrcweir     {
241*cdf0e10cSrcweir         OUString aBasePath;
242*cdf0e10cSrcweir         if (utl::Bootstrap::locateBaseInstallation(aBasePath) != utl::Bootstrap::PATH_EXISTS)
243*cdf0e10cSrcweir             return UserInstall::E_InvalidBaseinstall;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         // create the user directory
246*cdf0e10cSrcweir         FileBase::RC rc = Directory::createPath(aUserPath);
247*cdf0e10cSrcweir         if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST)) return UserInstall::E_Creation;
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir             // copy data from shared data directory of base installation
250*cdf0e10cSrcweir         for (sal_Int32 i=0; pszSrcList[i]!=NULL && pszDstList[i]!=NULL; i++)
251*cdf0e10cSrcweir         {
252*cdf0e10cSrcweir             rc = copy_recursive(
253*cdf0e10cSrcweir                     aBasePath + OUString::createFromAscii(pszSrcList[i]),
254*cdf0e10cSrcweir                     aUserPath + OUString::createFromAscii(pszDstList[i]));
255*cdf0e10cSrcweir             if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST))
256*cdf0e10cSrcweir             {
257*cdf0e10cSrcweir                 if ( rc == FileBase::E_NOSPC )
258*cdf0e10cSrcweir                     return UserInstall::E_NoDiskSpace;
259*cdf0e10cSrcweir                 else if ( rc == FileBase::E_ACCES )
260*cdf0e10cSrcweir                     return UserInstall::E_NoWriteAccess;
261*cdf0e10cSrcweir                 else
262*cdf0e10cSrcweir                     return UserInstall::E_Creation;
263*cdf0e10cSrcweir             }
264*cdf0e10cSrcweir         }
265*cdf0e10cSrcweir         try
266*cdf0e10cSrcweir         {
267*cdf0e10cSrcweir             OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
268*cdf0e10cSrcweir             OUString sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess");
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir             // get configuration provider
271*cdf0e10cSrcweir             Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
272*cdf0e10cSrcweir             Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory >(
273*cdf0e10cSrcweir                 theMSF->createInstance(sConfigSrvc), UNO_QUERY_THROW);
274*cdf0e10cSrcweir             Sequence< Any > theArgs(1);
275*cdf0e10cSrcweir             NamedValue v(OUString::createFromAscii("NodePath"), makeAny(OUString::createFromAscii("org.openoffice.Setup")));
276*cdf0e10cSrcweir             //v.Name = OUString::createFromAscii("NodePath");
277*cdf0e10cSrcweir             //v.Value = makeAny(OUString::createFromAscii("org.openoffice.Setup"));
278*cdf0e10cSrcweir             theArgs[0] <<= v;
279*cdf0e10cSrcweir             Reference< XHierarchicalPropertySet> hpset(
280*cdf0e10cSrcweir                 theConfigProvider->createInstanceWithArguments(sAccessSrvc, theArgs), UNO_QUERY_THROW);
281*cdf0e10cSrcweir             hpset->setHierarchicalPropertyValue(OUString::createFromAscii("Office/ooSetupInstCompleted"), makeAny(sal_True));
282*cdf0e10cSrcweir             Reference< XChangesBatch >(hpset, UNO_QUERY_THROW)->commitChanges();
283*cdf0e10cSrcweir         }
284*cdf0e10cSrcweir         catch ( PropertyVetoException& )
285*cdf0e10cSrcweir         {
286*cdf0e10cSrcweir             // we are not allowed to change this
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir         catch (Exception& e)
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             OString aMsg("create_user_install(): ");
291*cdf0e10cSrcweir             aMsg += OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
292*cdf0e10cSrcweir             OSL_ENSURE(sal_False, aMsg.getStr());
293*cdf0e10cSrcweir             return UserInstall::E_Creation;
294*cdf0e10cSrcweir         }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         return UserInstall::E_None;
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 
302