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 #include "translate.hxx"
29 
30 #include <list>
31 #if TEST_LAYOUT
32 #include <cstdio>
33 #include "tools/getprocessworkingdir.hxx"
34 #endif
35 
36 #include <unotools/bootstrap.hxx>
37 #include <unotools/localfilehelper.hxx>
38 #include <unotools/ucbhelper.hxx>
39 #include <vcl/svapp.hxx>
40 
41 #include "proplist.hxx"
42 
43 namespace layoutimpl
44 {
45 namespace css = ::com::sun::star;
46 using namespace css;
47 using ::rtl::OUString;
48 using ::utl::LocalFileHelper;
49 using ::utl::UCBContentHelper;
50 using ::utl::Bootstrap;
51 
52 static std::list<OUString>
53 getLocaleSubdirList( lang::Locale const& rLocale )
54 {
55     std::list<OUString> aSubdirs;
56     aSubdirs.push_front( OUString::createFromAscii( "." ) );
57     aSubdirs.push_front( OUString::createFromAscii( "en-US" ) );
58     if ( rLocale.Language.getLength() )
59         aSubdirs.push_front( rLocale.Language );
60     if ( rLocale.Country.getLength() )
61     {
62         OUString aLocaleCountry = rLocale.Language
63             + OUString::createFromAscii( "-" )
64             + rLocale.Country;
65         aSubdirs.push_front( aLocaleCountry );
66         if ( rLocale.Variant.getLength() )
67             aSubdirs.push_front( aLocaleCountry
68                                  + OUString::createFromAscii( "." )
69                                  + rLocale.Variant );
70     }
71     return aSubdirs;
72 }
73 
74 static bool
75 fileExists( String const& aFile )
76 {
77     String aUrl;
78     LocalFileHelper::ConvertPhysicalNameToURL( aFile, aUrl );
79     return UCBContentHelper::Exists( aUrl );
80 }
81 
82 static OUString
83 getFirstExisting( OUString const& aDir, std::list<OUString> const& aSubDirs,
84                   OUString const& aXMLName )
85 {
86     static OUString const aSlash = OUString::createFromAscii( "/" );
87     String aResult;
88     for ( std::list<OUString>::const_iterator i = aSubDirs.begin();
89           i != aSubDirs.end(); i++ )
90     {
91         String aFile = aDir + aSlash + *i + aSlash + aXMLName;
92         OSL_TRACE( "testing: %s", OUSTRING_CSTR( aFile ) );
93         if ( fileExists( aFile ) )
94             return aFile;
95     }
96     return OUString();
97 }
98 
99 /*  FIXME: IWBN to share code with impimagetree.cxx, also for reading
100   from zip files.  */
101 OUString
102 readRightTranslation( OUString const& aXMLName )
103 {
104     String aXMLFile;
105     std::list<OUString> aSubdirs
106         = getLocaleSubdirList( Application::GetSettings().GetUILocale() );
107 #if TEST_LAYOUT // read from cwd first
108     OUString aCurrentWorkingUrl;
109     tools::getProcessWorkingDir( &aCurrentWorkingUrl );
110     String aCurrentWorkingDir;
111     LocalFileHelper::ConvertURLToPhysicalName( aCurrentWorkingUrl, aCurrentWorkingDir );
112     aXMLFile = getFirstExisting( aCurrentWorkingDir, aSubdirs, aXMLName );
113     if ( aXMLFile.Len() )
114         ;
115     else
116 #endif /* TEST_LAYOUT */
117     {
118         OUString aShareUrl;
119         Bootstrap::locateSharedData( aShareUrl );
120         OUString aXMLUrl = aShareUrl + OUString::createFromAscii( "/layout" );
121         String aXMLDir;
122         LocalFileHelper::ConvertURLToPhysicalName( aXMLUrl, aXMLDir );
123         aXMLFile = getFirstExisting( aXMLDir, aSubdirs, aXMLName );
124     }
125 
126     OSL_TRACE( "FOUND:%s", OUSTRING_CSTR ( OUString (aXMLFile) ) );
127     return aXMLFile;
128 }
129 
130 } // namespace layoutimpl
131