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_desktop.hxx"
30 
31 #include "dp_misc.h"
32 #include "dp_platform.hxx"
33 #include "rtl/ustring.hxx"
34 #include "rtl/ustrbuf.hxx"
35 #include "rtl/instance.hxx"
36 #include "rtl/bootstrap.hxx"
37 
38 #define PLATFORM_ALL                "all"
39 #define PLATFORM_WIN_X86            "windows_x86"
40 #define PLATFORM_LINUX_X86          "linux_x86"
41 #define PLATFORM_LINUX_X86_64       "linux_x86_64"
42 #define PLATFORM_KFREEBSD_X86       "kfreebsd_x86"
43 #define PLATFORM_KFREEBSD_X86_64    "kfreebsd_x86_64"
44 #define PLATFORM_LINUX_SPARC        "linux_sparc"
45 #define PLATFORM_LINUX_POWERPC      "linux_powerpc"
46 #define PLATFORM_LINUX_POWERPC64    "linux_powerpc64"
47 #define PLATFORM_LINUX_ARM_EABI     "linux_arm_eabi"
48 #define PLATFORM_LINUX_ARM_OABI     "linux_arm_oabi"
49 #define PLATFORM_LINUX_MIPS_EL      "linux_mips_el"
50 #define PLATFORM_LINUX_MIPS_EB      "linux_mips_eb"
51 #define PLATFORM_LINUX_IA64         "linux_ia64"
52 #define PLATFORM_LINUX_M68K         "linux_m68k"
53 #define PLATFORM_LINUX_S390         "linux_s390"
54 #define PLATFORM_LINUX_S390x        "linux_s390x"
55 #define PLATFORM_LINUX_HPPA         "linux_hppa"
56 #define PLATFORM_LINUX_ALPHA        "linux_alpha"
57 
58 
59 
60 #define PLATFORM_SOLARIS_SPARC      "solaris_sparc"
61 #define PLATFORM_SOLARIS_SPARC64    "solaris_sparc64"
62 #define PLATFORM_SOLARIS_X86        "solaris_x86"
63 #define PLATFORM_FREEBSD_X86        "freebsd_x86"
64 #define PLATFORM_FREEBSD_X86_64     "freebsd_x86_64"
65 #define PLATFORM_MACOSX_X86         "macosx_x86"
66 #define PLATFORM_MACOSX_PPC         "macosx_powerpc"
67 #define PLATFORM_OS2_X86            "os2_x86"
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
78 using ::rtl::OUString;
79 namespace css = ::com::sun::star;
80 
81 namespace dp_misc
82 {
83 namespace
84 {
85     struct StrOperatingSystem :
86         public rtl::StaticWithInit<const OUString, StrOperatingSystem> {
87             const OUString operator () () {
88                 OUString os( RTL_CONSTASCII_USTRINGPARAM("$_OS") );
89                 ::rtl::Bootstrap::expandMacros( os );
90                 return os;
91             }
92     };
93 
94     struct StrCPU :
95         public rtl::StaticWithInit<const OUString, StrCPU> {
96             const OUString operator () () {
97                 OUString arch( RTL_CONSTASCII_USTRINGPARAM("$_ARCH") );
98                 ::rtl::Bootstrap::expandMacros( arch );
99                 return arch;
100             }
101     };
102 
103 
104     struct StrPlatform : public rtl::StaticWithInit<
105         const OUString, StrPlatform> {
106             const OUString operator () () {
107                 ::rtl::OUStringBuffer buf;
108                 buf.append( StrOperatingSystem::get() );
109                 buf.append( static_cast<sal_Unicode>('_') );
110                 OUString arch( RTL_CONSTASCII_USTRINGPARAM("$_ARCH") );
111                 ::rtl::Bootstrap::expandMacros( arch );
112                 buf.append( arch );
113                 return buf.makeStringAndClear();
114             }
115     };
116 
117     bool checkOSandCPU(OUString const & os, OUString const & cpu)
118     {
119         return os.equals(StrOperatingSystem::get())
120             && cpu.equals(StrCPU::get());
121     }
122 
123     bool isValidPlatform(OUString const &  token )
124     {
125         bool ret = false;
126         if (token.equals(OUSTR(PLATFORM_ALL)))
127             ret = true;
128         else if (token.equals(OUSTR(PLATFORM_WIN_X86)))
129             ret = checkOSandCPU(OUSTR("Windows"), OUSTR("x86"));
130         else if (token.equals(OUSTR(PLATFORM_LINUX_X86)))
131             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("x86"));
132         else if (token.equals(OUSTR(PLATFORM_LINUX_X86_64)))
133             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("X86_64"));
134         else if (token.equals(OUSTR(PLATFORM_KFREEBSD_X86)))
135             ret = checkOSandCPU(OUSTR("kFreeBSD"), OUSTR("x86"));
136         else if (token.equals(OUSTR(PLATFORM_KFREEBSD_X86_64)))
137             ret = checkOSandCPU(OUSTR("kFreeBSD"), OUSTR("X86_64"));
138         else if (token.equals(OUSTR(PLATFORM_LINUX_SPARC)))
139             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("SPARC"));
140         else if (token.equals(OUSTR(PLATFORM_LINUX_POWERPC)))
141             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("PowerPC"));
142         else if (token.equals(OUSTR(PLATFORM_LINUX_POWERPC64)))
143             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("PowerPC_64"));
144         else if (token.equals(OUSTR(PLATFORM_LINUX_ARM_EABI)))
145             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("ARM_EABI"));
146         else if (token.equals(OUSTR(PLATFORM_LINUX_ARM_OABI)))
147             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("ARM_OABI"));
148         else if (token.equals(OUSTR(PLATFORM_LINUX_MIPS_EL)))
149             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("MIPS_EL"));
150         else if (token.equals(OUSTR(PLATFORM_LINUX_MIPS_EB)))
151             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("MIPS_EB"));
152         else if (token.equals(OUSTR(PLATFORM_LINUX_IA64)))
153             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("IA64"));
154         else if (token.equals(OUSTR(PLATFORM_LINUX_M68K)))
155             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("M68K"));
156         else if (token.equals(OUSTR(PLATFORM_LINUX_S390)))
157             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("S390"));
158         else if (token.equals(OUSTR(PLATFORM_LINUX_S390x)))
159             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("S390x"));
160         else if (token.equals(OUSTR(PLATFORM_LINUX_HPPA)))
161             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("HPPA"));
162         else if (token.equals(OUSTR(PLATFORM_LINUX_ALPHA)))
163             ret = checkOSandCPU(OUSTR("Linux"), OUSTR("ALPHA"));
164         else if (token.equals(OUSTR(PLATFORM_SOLARIS_SPARC)))
165             ret = checkOSandCPU(OUSTR("Solaris"), OUSTR("SPARC"));
166         else if (token.equals(OUSTR(PLATFORM_SOLARIS_SPARC64)))
167             ret = checkOSandCPU(OUSTR("Solaris"), OUSTR("SPARC64"));
168         else if (token.equals(OUSTR(PLATFORM_SOLARIS_X86)))
169             ret = checkOSandCPU(OUSTR("Solaris"), OUSTR("x86"));
170         else if (token.equals(OUSTR(PLATFORM_FREEBSD_X86)))
171             ret = checkOSandCPU(OUSTR("FreeBSD"), OUSTR("x86"));
172         else if (token.equals(OUSTR(PLATFORM_FREEBSD_X86_64)))
173             ret = checkOSandCPU(OUSTR("FreeBSD"), OUSTR("X86_64"));
174         else if (token.equals(OUSTR(PLATFORM_MACOSX_X86)))
175             ret = checkOSandCPU(OUSTR("MacOSX"), OUSTR("x86"));
176         else if (token.equals(OUSTR(PLATFORM_MACOSX_PPC)))
177             ret = checkOSandCPU(OUSTR("MacOSX"), OUSTR("PowerPC"));
178         else if (token.equals(OUSTR(PLATFORM_OS2_X86)))
179             ret = checkOSandCPU(OUSTR("OS2"), OUSTR("x86"));
180         else
181         {
182             OSL_ENSURE(0, "Extension Manager: The extension supports an unknown platform. "
183             "Check the platform element in the descripion.xml");
184             ret = false;
185         }
186         return ret;
187     }
188 
189 } // anon namespace
190 //=============================================================================
191 
192 OUString const & getPlatformString()
193 {
194     return StrPlatform::get();
195 }
196 
197 bool platform_fits( OUString const & platform_string )
198 {
199     sal_Int32 index = 0;
200     for (;;)
201     {
202         const OUString token(
203             platform_string.getToken( 0, ',', index ).trim() );
204         // check if this platform:
205         if (token.equalsIgnoreAsciiCase( StrPlatform::get() ) ||
206             (token.indexOf( '_' ) < 0 && /* check OS part only */
207              token.equalsIgnoreAsciiCase( StrOperatingSystem::get() )))
208         {
209             return true;
210         }
211         if (index < 0)
212             break;
213     }
214     return false;
215 }
216 
217 bool hasValidPlatform( css::uno::Sequence<OUString> const & platformStrings)
218 {
219     bool ret = false;
220     for (sal_Int32 i  = 0; i < platformStrings.getLength(); i++)
221     {
222         if (isValidPlatform(platformStrings[i]))
223         {
224             ret = true;
225             break;
226         }
227     }
228     return ret;
229 }
230 
231 }
232 
233