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