xref: /trunk/main/tools/bootstrp/inimgr.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 #if !defined( UNX )
31 #include <direct.h>
32 #else
33 #include <sys/stat.h>
34 #endif
35 #include <stdlib.h>
36 #include <stdio.h>
37 
38 
39 #include "bootstrp/inimgr.hxx"
40 #include "bootstrp/appdef.hxx"
41 
42 /****************************************************************************/
43 IniManager::IniManager( ByteString &rDir, ByteString &rLocalDir )
44 /****************************************************************************/
45             : bUpdate( sal_True )
46 {
47     sLocalPath = ByteString( getenv( "LOCALINI" ));
48     if ( !sLocalPath.Len())
49         sLocalPath = rLocalDir;
50 
51     sGlobalDir = rDir;
52 #if !defined( UNX ) && !defined( OS2 )
53     mkdir(( char * ) sLocalPath.GetBuffer());
54 #else
55     mkdir( sLocalPath.GetBuffer() ,00777 );
56 #endif
57 }
58 
59 /****************************************************************************/
60 IniManager::IniManager( ByteString &rDir )
61 /****************************************************************************/
62             : bUpdate( sal_True )
63 {
64     sLocalPath = GetLocalIni();
65     sGlobalDir = rDir;
66 #if !defined( UNX ) && !defined( OS2 )
67     mkdir(( char * ) sLocalPath.GetBuffer());
68 #else
69     mkdir( sLocalPath.GetBuffer() ,00777 );
70 #endif
71 }
72 
73 /****************************************************************************/
74 IniManager::IniManager()
75 /****************************************************************************/
76             : bUpdate( sal_True )
77 {
78     sLocalPath = GetLocalIni();
79 
80 #if !defined( UNX ) && !defined( OS2 )
81     mkdir(( char * ) sLocalPath.GetBuffer());
82 #else
83     mkdir( sLocalPath.GetBuffer(), 00777 );
84 #endif
85 
86     sGlobalDir = GetGlobalIni();
87 }
88 
89 /****************************************************************************/
90 ByteString IniManager::ToLocal( ByteString &rPath )
91 /****************************************************************************/
92 {
93     ByteString sTmp( rPath );
94 #if !defined( UNX )
95     ByteString sUnc( _INI_UNC );
96     sUnc.ToUpperAscii();
97     ByteString sOldUnc( _INI_UNC_OLD );
98     sOldUnc.ToUpperAscii();
99     sTmp.ToUpperAscii();
100 
101     sTmp.SearchAndReplace( sUnc, _INI_DRV );
102     sTmp.SearchAndReplace( sOldUnc, _INI_DRV );
103     sTmp.ToUpperAscii();
104 
105     ByteString sIni( sGlobalDir );
106     sIni.ToUpperAscii();
107 
108     sTmp.SearchAndReplace( sIni, sLocalPath );
109 
110     while ( sTmp.SearchAndReplace( "\\\\", "\\" ) != STRING_NOTFOUND ) ;
111 #else
112     sTmp.SearchAndReplace( sGlobalDir, sLocalPath );
113 
114     ByteString sOldGlobalDir( GetIniRootOld() );
115     sTmp.SearchAndReplace( sOldGlobalDir, sLocalPath );
116 
117     while ( sTmp.SearchAndReplace( "//", "/" ) != STRING_NOTFOUND ) ;
118 #endif
119 
120     return sTmp;
121 }
122 
123 /****************************************************************************/
124 ByteString IniManager::GetLocalIni()
125 /****************************************************************************/
126 {
127     ByteString sLocalPath = ByteString( getenv( "LOCALINI" ));
128 
129     if ( !sLocalPath.Len()) {
130 #ifdef UNX
131         ByteString sLocal( getenv( "HOME" ));
132         sLocal += ByteString( "/localini" );
133 #else
134         ByteString sLocal( getenv( "TMP" ));
135         sLocal += ByteString( "\\localini" );
136 #endif
137 
138         sLocalPath = sLocal;
139     }
140 
141     return sLocalPath;
142 }
143 
144 /****************************************************************************/
145 ByteString IniManager::GetGlobalIni()
146 /****************************************************************************/
147 {
148     ByteString sGlobalPath = ByteString( GetEnv( "GLOBALINI" ));
149 
150     if ( !sGlobalPath.Len())
151         sGlobalPath = ByteString( _INIROOT );
152 
153     return sGlobalPath;
154 }
155 
156 /****************************************************************************/
157 void IniManager::ForceUpdate()
158 /****************************************************************************/
159 {
160     UniString sUniGlobalDir( sGlobalDir, gsl_getSystemTextEncoding());
161     DirEntry aPath( UniString( sGlobalDir, gsl_getSystemTextEncoding()));
162     Dir aDir( aPath, FSYS_KIND_DIR | FSYS_KIND_FILE);
163 
164 #ifndef UNX
165     sLocalPath.EraseTrailingChars( '\\' );
166     sLocalPath += "\\";
167 #else
168     sLocalPath.EraseTrailingChars( '/' );
169     sLocalPath += "/";
170 #endif
171 
172     for ( sal_uInt16 i=0; i < aDir.Count(); i++ ) {
173         ByteString sEntry( aDir[i].GetName(), gsl_getSystemTextEncoding());
174         if (( sEntry != "." ) &&
175             ( sEntry != ".." ))
176         {
177             if ( !FileStat( aDir[i] ).IsKind( FSYS_KIND_DIR )) {
178                 ByteString sSrc( aDir[i].GetFull(), gsl_getSystemTextEncoding());
179                 ByteString sDestination( sLocalPath );
180                 sDestination += sEntry;
181 
182                 UniString sUniDestination( sDestination, gsl_getSystemTextEncoding());
183                 DirEntry aDestEntry( sUniDestination );
184                 FileStat aDestStat( aDestEntry );
185                 FileStat aSrcStat( aDir[i] );
186 
187                 if (( !aDestEntry.Exists() ) ||
188                     ( aSrcStat.IsYounger( aDestStat )))
189                 {
190                     FileCopier aFileCopier( aDir[ i ], aDestEntry );
191                     aFileCopier.Execute();
192 
193                     while ( !aDestEntry.Exists())
194                         aFileCopier.Execute();
195                 }
196             }
197         }
198     }
199 }
200 
201 /****************************************************************************/
202 void IniManager::Update()
203 /****************************************************************************/
204 {
205     if ( bUpdate )
206     {
207         ForceUpdate();
208         bUpdate = sal_False;
209     }
210 }
211