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