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