bump product version to 4.1.6.2
[LibreOffice.git] / desktop / source / app / userinstall.cxx
bloba901a761b04e4a3c0d2ab1307db5c28a0f9f0580
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include "sal/config.h"
24 #include "userinstall.hxx"
25 #include "langselect.hxx"
27 #include <stdio.h>
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/file.hxx>
31 #include <osl/mutex.hxx>
32 #include <osl/process.h>
33 #include <osl/diagnose.h>
34 #include <osl/security.hxx>
35 #include <rtl/ref.hxx>
37 #include <officecfg/Setup.hxx>
38 #include <unotools/bootstrap.hxx>
39 #include <svl/languageoptions.hxx>
40 #include <unotools/syslocaleoptions.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <com/sun/star/configuration/theDefaultProvider.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <i18nlangtag/mslangid.hxx>
45 #include <com/sun/star/lang/XLocalizable.hpp>
46 #include <com/sun/star/lang/Locale.hpp>
48 #include "app.hxx"
50 using namespace osl;
51 using namespace utl;
52 using namespace com::sun::star::container;
53 using namespace com::sun::star::uno;
54 using namespace com::sun::star::lang;
55 using namespace com::sun::star::beans;
56 using namespace com::sun::star::util;
59 namespace desktop {
61 static UserInstall::UserInstallStatus create_user_install(OUString&);
63 static bool is_user_install()
65 try
67 Reference< XMultiServiceFactory > theConfigProvider(
68 com::sun::star::configuration::theDefaultProvider::get(
69 comphelper::getProcessComponentContext() ) );
71 // localize the provider to user selection
72 Reference< XLocalizable > localizable(theConfigProvider, UNO_QUERY_THROW);
73 OUString aUserLanguage = LanguageSelection::getLanguageString();
74 LanguageTag aLanguageTag(aUserLanguage);
75 localizable->setLocale(aLanguageTag.getLocale( false));
77 return officecfg::Setup::Office::ooSetupInstCompleted::get();
79 catch (Exception const & e)
81 OString msg(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
82 OSL_FAIL(msg.getStr());
85 return false;
88 UserInstall::UserInstallStatus UserInstall::finalize()
90 OUString aUserInstallPath;
91 utl::Bootstrap::PathStatus aLocateResult =
92 utl::Bootstrap::locateUserInstallation(aUserInstallPath);
94 switch (aLocateResult) {
96 case utl::Bootstrap::DATA_INVALID:
97 case utl::Bootstrap::DATA_MISSING:
98 case utl::Bootstrap::DATA_UNKNOWN:
99 // cannot find a valid path or path is missing
100 return E_Unknown;
102 case utl::Bootstrap::PATH_EXISTS:
104 // path exists, check if an installation lives there
105 if ( is_user_install() )
107 return Ok;
109 // Note: fall-thru intended.
111 case utl::Bootstrap::PATH_VALID:
112 // found a path but need to create user install
113 return create_user_install(aUserInstallPath);
114 default:
115 return E_Unknown;
119 #if HAVE_FEATURE_DESKTOP
120 static osl::FileBase::RC copy_recursive( const OUString& srcUnqPath, const OUString& dstUnqPath)
122 FileBase::RC err;
123 DirectoryItem aDirItem;
124 DirectoryItem::get(srcUnqPath, aDirItem);
125 FileStatus aFileStatus(osl_FileStatus_Mask_All);
126 aDirItem.getFileStatus(aFileStatus);
128 if( aFileStatus.getFileType() == FileStatus::Directory)
130 // create directory if not already there
131 err = Directory::create( dstUnqPath );
132 if (err == osl::FileBase::E_EXIST)
133 err = osl::FileBase::E_None;
135 FileBase::RC next = err;
136 if (err == osl::FileBase::E_None)
138 // iterate through directory contents
139 Directory aDir( srcUnqPath );
140 aDir.open();
141 while (err == osl::FileBase::E_None &&
142 (next = aDir.getNextItem( aDirItem )) == osl::FileBase::E_None)
144 aDirItem.getFileStatus(aFileStatus);
145 // generate new src/dst pair and make recursive call
146 OUString newSrcUnqPath = aFileStatus.getFileURL();
147 OUString newDstUnqPath = dstUnqPath;
148 OUString itemname = aFileStatus.getFileName();
149 // append trailing '/' if needed
150 if (newDstUnqPath.lastIndexOf(sal_Unicode('/')) != newDstUnqPath.getLength()-1)
151 newDstUnqPath += OUString("/");
152 newDstUnqPath += itemname;
153 // recursion
154 err = copy_recursive(newSrcUnqPath, newDstUnqPath);
156 aDir.close();
158 if ( err != osl::FileBase::E_None )
159 return err;
160 if( next != FileBase::E_NOENT )
161 err = FileBase::E_INVAL;
164 else
166 // copy single file - foldback
167 err = File::copy( srcUnqPath,dstUnqPath );
170 return err;
172 #endif
174 static UserInstall::UserInstallStatus create_user_install(OUString& aUserPath)
176 OUString aBasePath;
177 if (utl::Bootstrap::locateBaseInstallation(aBasePath) != utl::Bootstrap::PATH_EXISTS)
178 return UserInstall::E_InvalidBaseinstall;
180 // create the user directory
181 FileBase::RC rc = Directory::createPath(aUserPath);
182 if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST)) return UserInstall::E_Creation;
184 #if defined(UNIX) && !defined(ANDROID) && !defined(IOS)
185 // Set safer permissions for the user directory by default:
186 File::setAttributes(aUserPath, osl_File_Attribute_OwnWrite| osl_File_Attribute_OwnRead| osl_File_Attribute_OwnExe);
187 #endif
189 #if !defined(ANDROID) && !defined(IOS)
190 // as of now osl_copyFile does not work on Android => don't do this.
192 // Copy data from shared data directory of base installation:
193 rc = copy_recursive(
194 aBasePath + OUString("/presets"),
195 aUserPath + OUString("/user"));
196 if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST))
198 if ( rc == FileBase::E_NOSPC )
199 return UserInstall::E_NoDiskSpace;
200 else if ( rc == FileBase::E_ACCES )
201 return UserInstall::E_NoWriteAccess;
202 else
203 return UserInstall::E_Creation;
205 #endif
207 boost::shared_ptr< comphelper::ConfigurationChanges > batch(
208 comphelper::ConfigurationChanges::create());
209 officecfg::Setup::Office::ooSetupInstCompleted::set(true, batch);
210 batch->commit();
212 return UserInstall::Created;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */