LCOV - code coverage report
Current view: top level - desktop/source/app - userinstall.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 56 69 81.2 %
Date: 2012-08-25 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 54 110 49.1 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include "sal/config.h"
      30                 :            : 
      31                 :            : #include "userinstall.hxx"
      32                 :            : #include "langselect.hxx"
      33                 :            : 
      34                 :            : #include <stdio.h>
      35                 :            : #include <rtl/ustring.hxx>
      36                 :            : #include <rtl/ustrbuf.hxx>
      37                 :            : #include <osl/file.hxx>
      38                 :            : #include <osl/mutex.hxx>
      39                 :            : #include <osl/process.h>
      40                 :            : #include <osl/diagnose.h>
      41                 :            : #include <osl/security.hxx>
      42                 :            : #include <rtl/ref.hxx>
      43                 :            : 
      44                 :            : #include <officecfg/Setup.hxx>
      45                 :            : #include <unotools/bootstrap.hxx>
      46                 :            : #include <svl/languageoptions.hxx>
      47                 :            : #include <unotools/syslocaleoptions.hxx>
      48                 :            : #include <comphelper/processfactory.hxx>
      49                 :            : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      50                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      51                 :            : #include <i18npool/mslangid.hxx>
      52                 :            : #include <com/sun/star/lang/XLocalizable.hpp>
      53                 :            : #include <com/sun/star/lang/Locale.hpp>
      54                 :            : 
      55                 :            : #include "app.hxx"
      56                 :            : 
      57                 :            : using rtl::OString;
      58                 :            : using rtl::OUString;
      59                 :            : using namespace osl;
      60                 :            : using namespace utl;
      61                 :            : using namespace com::sun::star::container;
      62                 :            : using namespace com::sun::star::uno;
      63                 :            : using namespace com::sun::star::lang;
      64                 :            : using namespace com::sun::star::beans;
      65                 :            : using namespace com::sun::star::util;
      66                 :            : 
      67                 :            : 
      68                 :            : namespace desktop {
      69                 :            : 
      70                 :            :     static UserInstall::UserInstallStatus create_user_install(OUString&);
      71                 :            : 
      72                 :        158 :     static bool is_user_install()
      73                 :            :     {
      74                 :            :         try
      75                 :            :         {
      76                 :            :             Reference< XMultiServiceFactory > theConfigProvider(
      77                 :            :                 com::sun::star::configuration::theDefaultProvider::get(
      78 [ +  - ][ +  - ]:        158 :                     comphelper::getProcessComponentContext() ) );
      79                 :            : 
      80                 :            :             // localize the provider to user selection
      81         [ +  - ]:        158 :             Reference< XLocalizable > localizable(theConfigProvider, UNO_QUERY_THROW);
      82         [ +  - ]:        158 :             OUString aUserLanguage = LanguageSelection::getLanguageString();
      83         [ +  - ]:        158 :             Locale aLocale = LanguageSelection::IsoStringToLocale(aUserLanguage);
      84 [ +  - ][ +  - ]:        158 :             localizable->setLocale(aLocale);
      85                 :            : 
      86                 :        158 :             return officecfg::Setup::Office::ooSetupInstCompleted::get();
      87                 :            :         }
      88         [ #  # ]:          0 :         catch (Exception const & e)
      89                 :            :         {
      90         [ #  # ]:          0 :             OString msg(OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
      91                 :          0 :             OSL_FAIL(msg.getStr());
      92                 :            :         }
      93                 :            : 
      94                 :          0 :         return false;
      95                 :            :     }
      96                 :            : 
      97                 :        158 :     UserInstall::UserInstallStatus UserInstall::finalize()
      98                 :            :     {
      99                 :        158 :         OUString aUserInstallPath;
     100                 :            :         utl::Bootstrap::PathStatus aLocateResult =
     101         [ +  - ]:        158 :             utl::Bootstrap::locateUserInstallation(aUserInstallPath);
     102                 :            : 
     103   [ -  +  -  - ]:        158 :         switch (aLocateResult) {
     104                 :            : 
     105                 :            :             case utl::Bootstrap::DATA_INVALID:
     106                 :            :             case utl::Bootstrap::DATA_MISSING:
     107                 :            :             case utl::Bootstrap::DATA_UNKNOWN:
     108                 :            :                 // cannot find a valid path or path is missing
     109                 :          0 :                 return E_Unknown;
     110                 :            : 
     111                 :            :             case utl::Bootstrap::PATH_EXISTS:
     112                 :            :             {
     113                 :            :                 // path exists, check if an installation lives there
     114 [ +  - ][ +  + ]:        158 :                 if ( is_user_install() )
     115                 :            :                 {
     116                 :         96 :                     return Ok;
     117                 :            :                 }
     118                 :            :                 // Note: fall-thru intended.
     119                 :            :             }
     120                 :            :             case utl::Bootstrap::PATH_VALID:
     121                 :            :                 // found a path but need to create user install
     122         [ +  - ]:         62 :                 return create_user_install(aUserInstallPath);
     123                 :            :             default:
     124                 :          0 :                 return E_Unknown;
     125                 :        158 :         }
     126                 :            :     }
     127                 :            : 
     128                 :       3348 :     static osl::FileBase::RC copy_recursive( const rtl::OUString& srcUnqPath, const rtl::OUString& dstUnqPath)
     129                 :            :     {
     130                 :            :         FileBase::RC err;
     131                 :       3348 :         DirectoryItem aDirItem;
     132         [ +  - ]:       3348 :         DirectoryItem::get(srcUnqPath, aDirItem);
     133                 :       3348 :         FileStatus aFileStatus(osl_FileStatus_Mask_All);
     134         [ +  - ]:       3348 :         aDirItem.getFileStatus(aFileStatus);
     135                 :            : 
     136 [ +  - ][ +  + ]:       3348 :         if( aFileStatus.getFileType() == FileStatus::Directory)
     137                 :            :         {
     138                 :            :             // create directory if not already there
     139         [ +  - ]:       1240 :             err = Directory::create( dstUnqPath );
     140         [ +  + ]:       1240 :             if (err == osl::FileBase::E_EXIST)
     141                 :         62 :                 err = osl::FileBase::E_None;
     142                 :            : 
     143                 :       1240 :             FileBase::RC next = err;
     144         [ +  - ]:       1240 :             if (err == osl::FileBase::E_None)
     145                 :            :             {
     146                 :            :                 // iterate through directory contents
     147                 :       1240 :                 Directory aDir( srcUnqPath );
     148         [ +  - ]:       1240 :                 aDir.open();
     149 [ +  - ][ +  - ]:       4526 :                 while (err ==  osl::FileBase::E_None &&
         [ +  + ][ +  + ]
     150                 :            :                     (next = aDir.getNextItem( aDirItem )) == osl::FileBase::E_None)
     151                 :            :                 {
     152         [ +  - ]:       3286 :                     aDirItem.getFileStatus(aFileStatus);
     153                 :            :                     // generate new src/dst pair and make recursive call
     154         [ +  - ]:       3286 :                     rtl::OUString newSrcUnqPath = aFileStatus.getFileURL();
     155                 :       3286 :                     rtl::OUString newDstUnqPath = dstUnqPath;
     156         [ +  - ]:       3286 :                     rtl::OUString itemname = aFileStatus.getFileName();
     157                 :            :                     // append trailing '/' if needed
     158         [ +  - ]:       3286 :                     if (newDstUnqPath.lastIndexOf(sal_Unicode('/')) != newDstUnqPath.getLength()-1)
     159                 :       3286 :                         newDstUnqPath += rtl::OUString("/");
     160                 :       3286 :                     newDstUnqPath += itemname;
     161                 :            :                     // recursion
     162         [ +  - ]:       3286 :                     err = copy_recursive(newSrcUnqPath, newDstUnqPath);
     163                 :       3286 :                 }
     164         [ +  - ]:       1240 :                 aDir.close();
     165                 :            : 
     166         [ -  + ]:       1240 :                 if ( err != osl::FileBase::E_None )
     167                 :          0 :                     return err;
     168         [ -  + ]:       1240 :                 if( next != FileBase::E_NOENT )
     169 [ +  - ][ +  - ]:       1240 :                     err = FileBase::E_INVAL;
     170                 :            :             }
     171                 :            :         }
     172                 :            :         else
     173                 :            :         {
     174                 :            :             // copy single file - foldback
     175         [ +  - ]:       2108 :             err = File::copy( srcUnqPath,dstUnqPath );
     176                 :            :         }
     177                 :            : 
     178                 :            : #ifdef ANDROID
     179                 :            :         fprintf (stderr, "copy_recursive '%s' to '%s' returns (%d)0x%x\n",
     180                 :            :                  rtl::OUStringToOString(srcUnqPath, RTL_TEXTENCODING_UTF8).getStr(),
     181                 :            :                  rtl::OUStringToOString(dstUnqPath, RTL_TEXTENCODING_UTF8).getStr(),
     182                 :            :                  (int)err, (int)err);
     183                 :            : #endif
     184         [ +  - ]:       3348 :         return err;
     185                 :            :     }
     186                 :            : 
     187                 :         62 :     static UserInstall::UserInstallStatus create_user_install(OUString& aUserPath)
     188                 :            :     {
     189                 :         62 :         OUString aBasePath;
     190 [ -  + ][ +  - ]:         62 :         if (utl::Bootstrap::locateBaseInstallation(aBasePath) != utl::Bootstrap::PATH_EXISTS)
     191                 :          0 :             return UserInstall::E_InvalidBaseinstall;
     192                 :            : 
     193                 :            :         // create the user directory
     194         [ +  - ]:         62 :         FileBase::RC rc = Directory::createPath(aUserPath);
     195 [ +  - ][ -  + ]:         62 :         if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST)) return UserInstall::E_Creation;
     196                 :            : 
     197                 :            : #ifdef UNIX
     198                 :            :         // Set safer permissions for the user directory by default:
     199         [ +  - ]:         62 :         File::setAttributes(aUserPath, osl_File_Attribute_OwnWrite| osl_File_Attribute_OwnRead| osl_File_Attribute_OwnExe);
     200                 :            : #endif
     201                 :            : 
     202                 :            : #ifndef ANDROID
     203                 :            :         // as of now osl_copyFile does not work on Android => don't do this.
     204                 :            : 
     205                 :            :         // Copy data from shared data directory of base installation:
     206                 :            :         rc = copy_recursive(
     207                 :            :             aBasePath + rtl::OUString("/presets"),
     208         [ +  - ]:         62 :             aUserPath + rtl::OUString("/user"));
     209 [ #  # ][ -  + ]:         62 :         if ((rc != FileBase::E_None) && (rc != FileBase::E_EXIST))
     210                 :            :         {
     211         [ #  # ]:          0 :             if ( rc == FileBase::E_NOSPC )
     212                 :          0 :                 return UserInstall::E_NoDiskSpace;
     213         [ #  # ]:          0 :             else if ( rc == FileBase::E_ACCES )
     214                 :          0 :                 return UserInstall::E_NoWriteAccess;
     215                 :            :             else
     216                 :          0 :                 return UserInstall::E_Creation;
     217                 :            :         }
     218                 :            : #endif
     219                 :            : 
     220                 :            :         boost::shared_ptr< comphelper::ConfigurationChanges > batch(
     221 [ +  - ][ +  - ]:         62 :             comphelper::ConfigurationChanges::create());
     222         [ +  - ]:         62 :         officecfg::Setup::Office::ooSetupInstCompleted::set(true, batch);
     223         [ +  - ]:         62 :         batch->commit();
     224                 :            : 
     225         [ +  - ]:         62 :         return UserInstall::Created;
     226                 :            :     }
     227                 :            : }
     228                 :            : 
     229                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10