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 "dp_misc.h"
25 #include "com/sun/star/uno/Exception.hpp"
26 #include "com/sun/star/lang/XComponent.hpp"
27 #include "com/sun/star/uno/XComponentContext.hpp"
28 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
29 #include "com/sun/star/deployment/XPackage.hpp"
30 #include "tools/resmgr.hxx"
31 #include "rtl/ustring.hxx"
32 #include "unotools/configmgr.hxx"
33 #include "ucbhelper/contentbroker.hxx"
34 
35 
36 #define APP_NAME "unopkg"
37 
38 namespace css = ::com::sun::star;
39 
40 namespace unopkg {
41 
toLocale(::rtl::OUString const & slang)42     inline ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
43     {
44         ::com::sun::star::lang::Locale locale;
45         sal_Int32 nIndex = 0;
46         locale.Language = slang.getToken( 0, '-', nIndex );
47         locale.Country = slang.getToken( 0, '-', nIndex );
48         locale.Variant = slang.getToken( 0, '-', nIndex );
49         return locale;
50     }
51 
52 
53 	struct OfficeLocale :
54 		public rtl::StaticWithInit<const css::lang::Locale, OfficeLocale> {
operator ()unopkg::OfficeLocale55 			const css::lang::Locale operator () () {
56 				::rtl::OUString slang;
57         if (! (::utl::ConfigManager::GetDirectConfigProperty(
58                    ::utl::ConfigManager::LOCALE ) >>= slang))
59             throw css::uno::RuntimeException( OUSTR("Cannot determine language!"), 0 );
60         if (slang.getLength() == 0)
61             slang = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
62 		return toLocale(slang);
63     }
64 };
65 
66 struct DeploymentResMgr :  public rtl::StaticWithInit< ResMgr *, DeploymentResMgr >
67 {
operator ()unopkg::DeploymentResMgr68 	ResMgr * operator () () {
69         return ResMgr::CreateResMgr( "deployment", OfficeLocale::get());
70 	}
71 };
72 
73 struct OptionInfo
74 {
75     char const * m_name;
76     sal_uInt32 m_name_length;
77     sal_Unicode m_short_option;
78     bool m_has_argument;
79 };
80 
81 struct LockFileException : public css::uno::Exception
82 {
LockFileExceptionunopkg::LockFileException83     LockFileException(::rtl::OUString const & sMessage) :
84         css::uno::Exception(sMessage, css::uno::Reference< css::uno::XInterface > ()) {}
85 };
86 
87 //==============================================================================
88 ::rtl::OUString toString( OptionInfo const * info );
89 
90 //==============================================================================
91 OptionInfo const * getOptionInfo(
92     OptionInfo const * list,
93     ::rtl::OUString const & opt, sal_Unicode copt = '\0' );
94 
95 //==============================================================================
96 bool isOption( OptionInfo const * option_info, sal_uInt32 * pIndex );
97 
98 //==============================================================================
99 bool readArgument(
100     ::rtl::OUString * pValue, OptionInfo const * option_info,
101     sal_uInt32 * pIndex );
102 
103 //==============================================================================
readOption(bool * flag,OptionInfo const * option_info,sal_uInt32 * pIndex)104 inline bool readOption(
105     bool * flag, OptionInfo const * option_info, sal_uInt32 * pIndex )
106 {
107     if (isOption( option_info, pIndex )) {
108         OSL_ASSERT( flag != 0 );
109         *flag = true;
110         return true;
111     }
112     return false;
113 }
114 //==============================================================================
115 
116 /** checks if an argument is a bootstrap variable. These start with -env:. For example
117     -env:UNO_JAVA_JFW_USER_DATA=file:///d:/user
118 */
119 bool isBootstrapVariable(sal_uInt32 * pIndex);
120 //==============================================================================
121 ::rtl::OUString const & getExecutableDir();
122 
123 //==============================================================================
124 ::rtl::OUString const & getProcessWorkingDir();
125 
126 //==============================================================================
127 ::rtl::OUString makeAbsoluteFileUrl(
128     ::rtl::OUString const & sys_path, ::rtl::OUString const & base_url,
129     bool throw_exc = true );
130 
131 //##############################################################################
132 
133 //==============================================================================
134 class DisposeGuard
135 {
136     css::uno::Reference<css::lang::XComponent> m_xComp;
137     bool m_bDeinitUCB;
138 public:
DisposeGuard()139     DisposeGuard(): m_bDeinitUCB(false) {}
~DisposeGuard()140     inline ~DisposeGuard()
141     {
142         if (m_bDeinitUCB)
143             ::ucbhelper::ContentBroker::deinitialize();
144 
145         if (m_xComp.is())
146             m_xComp->dispose();
147     }
148 
reset(css::uno::Reference<css::lang::XComponent> const & xComp)149     inline void reset(
150         css::uno::Reference<css::lang::XComponent> const & xComp )
151     {
152         m_xComp = xComp;
153     }
154 
setDeinitUCB()155     inline void setDeinitUCB()
156     {
157         m_bDeinitUCB = true;
158     }
159 
160 };
161 
162 //==============================================================================
163 css::uno::Reference<css::ucb::XCommandEnvironment> createCmdEnv(
164     css::uno::Reference<css::uno::XComponentContext> const & xContext,
165     ::rtl::OUString const & logFile,
166     bool option_force_overwrite,
167     bool option_verbose);
168 //==============================================================================
169 void printf_packages(
170     ::std::vector<
171     css::uno::Reference<css::deployment::XPackage> > const & allExtensions,
172     ::std::vector<bool> const & vecUnaccepted,
173     css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv,
174     sal_Int32 level = 0 );
175 
176 //##############################################################################
177 
178 //==============================================================================
179 css::uno::Reference<css::uno::XComponentContext> getUNO(
180     DisposeGuard & disposeGuard, bool verbose, bool shared, bool bGui,
181     css::uno::Reference<css::uno::XComponentContext> & out_LocalComponentContext);
182 
183 bool hasNoFolder(::rtl::OUString const & folderUrl);
184 
185 void removeFolder(::rtl::OUString const & folderUrl);
186 
187 }
188 
189 
190 
191