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