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 #if ! defined INCLUDED_DP_MISC_H
25 #define INCLUDED_DP_MISC_H
26 
27 #include "rtl/ustrbuf.hxx"
28 #include "rtl/instance.hxx"
29 #include "osl/mutex.hxx"
30 #include "osl/process.h"
31 #include "com/sun/star/uno/XComponentContext.hpp"
32 #include "com/sun/star/lang/XComponent.hpp"
33 #include "com/sun/star/lang/DisposedException.hpp"
34 #include "com/sun/star/deployment/XPackageRegistry.hpp"
35 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
36 #include "com/sun/star/awt/XWindow.hpp"
37 #include "dp_misc_api.hxx"
38 
39 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
40 #define ARLEN(x) (sizeof (x) / sizeof *(x))
41 
42 namespace dp_misc {
43 
44 const sal_Char CR = 0x0d;
45 const sal_Char LF = 0x0a;
46 
47 //==============================================================================
48 class MutexHolder
49 {
50     mutable ::osl::Mutex m_mutex;
51 protected:
getMutex() const52     inline ::osl::Mutex & getMutex() const { return m_mutex; }
53 };
54 
55 //==============================================================================
try_dispose(::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> const & x)56 inline void try_dispose( ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> const & x )
57 {
58     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent> xComp( x, ::com::sun::star::uno::UNO_QUERY );
59     if (xComp.is())
60         xComp->dispose();
61 }
62 
63 //##############################################################################
64 
65 //==============================================================================
66 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
67 ::rtl::OUString expandUnoRcTerm( ::rtl::OUString const & term );
68 
69 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
70 ::rtl::OUString makeRcTerm( ::rtl::OUString const & url );
71 
72 //==============================================================================
73 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
74 ::rtl::OUString expandUnoRcUrl( ::rtl::OUString const & url );
75 
76 //==============================================================================
77 
78 /** appends a relative path to a url.
79 
80     The relative path must already be correctly encoded for use in an URL.
81     If the URL starts with vnd.sun.star.expand then the relative path will
82     be again encoded for use in an "expand" URL.
83  */
84 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC ::rtl::OUString makeURL(
85     ::rtl::OUString const & baseURL, ::rtl::OUString const & relPath );
86 
87 
88 /** appends a relative path to a url.
89 
90     This is the same as makeURL, but the relative Path must me a segment
91     of an system path.
92  */
93 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC ::rtl::OUString makeURLAppendSysPathSegment(
94     ::rtl::OUString const & baseURL, ::rtl::OUString const & relPath );
95 
96 //==============================================================================
97 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC ::rtl::OUString generateRandomPipeId();
98 
99 class AbortChannel;
100 //==============================================================================
101 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
102 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> resolveUnoURL(
103     ::rtl::OUString const & connectString,
104     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> const & xLocalContext,
105     AbortChannel * abortChannel = 0 );
106 
107 //==============================================================================
108 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC bool office_is_running();
109 
110 //==============================================================================
111 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
112 oslProcess raiseProcess( ::rtl::OUString const & appURL,
113                          ::com::sun::star::uno::Sequence< ::rtl::OUString > const & args );
114 
115 //==============================================================================
116 
117 /** writes the argument string to the console.
118     On Linux/Unix/etc. it converts the UTF16 string to an ANSI string using
119     osl_getThreadTextEncoding() as target encoding. On Windows it uses WriteFile
120     with the standard out stream. unopkg.com reads the data and prints them out using
121     WriteConsoleW.
122 */
123 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
124 void writeConsole(::rtl::OUString const & sText);
125 
126 /** writes the argument string to the console.
127     On Linux/Unix/etc. the string is passed into fprintf without any conversion.
128     On Windows the string is converted to UTF16 assuming the argument is UTF8
129     encoded. The UTF16 string is written to stdout with WriteFile. unopkg.com
130     reads the data and prints them out using WriteConsoleW.
131 */
132 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
133 void writeConsole(::rtl::OString const & sText);
134 
135 /** writes the argument to the console using the error stream.
136     Otherwise the same as writeConsole.
137 */
138 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
139 void writeConsoleError(::rtl::OUString const & sText);
140 
141 
142 /** writes the argument to the console using the error stream.
143     Otherwise the same as writeConsole.
144 */
145 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
146 void writeConsoleError(::rtl::OString const & sText);
147 
148 
149 /** reads from the console.
150     On Linux/Unix/etc. it uses fgets to read char values and converts them to OUString
151     using osl_getThreadTextEncoding as target encoding. The returned string has a maximum
152     size of 1024 and does NOT include leading and trailing white space(applied OUString::trim())
153 */
154 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
155 ::rtl::OUString readConsole();
156 
157 /** print the text to the console in a debug build.
158     The argument is forwarded to writeConsole. The function does not add new line.
159     The code is only executed if  OSL_DEBUG_LEVEL > 1
160 */
161 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
162 void TRACE(::rtl::OUString const & sText);
163 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
164 void TRACE(::rtl::OString const & sText);
165 
166 /** registers or revokes shared or bundled extensions which have been
167     recently added or removed.
168 */
169 DESKTOP_DEPLOYMENTMISC_DLLPUBLIC
170 void syncRepositories(::com::sun::star::uno::Reference<
171                       ::com::sun::star::ucb::XCommandEnvironment> const & xCmdEnv);
172 
173 }
174 
175 #endif
176