12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
102722ceddSAndrew Rist  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
192722ceddSAndrew Rist  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "dp_misc.h"
29cdf0e10cSrcweir #include "dp_version.hxx"
30cdf0e10cSrcweir #include "dp_interact.h"
31cdf0e10cSrcweir #include "rtl/uri.hxx"
32cdf0e10cSrcweir #include "rtl/digest.h"
33cdf0e10cSrcweir #include "rtl/random.h"
34cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
35cdf0e10cSrcweir #include "unotools/bootstrap.hxx"
36cdf0e10cSrcweir #include "osl/file.hxx"
37cdf0e10cSrcweir #include "osl/pipe.hxx"
38cdf0e10cSrcweir #include "osl/security.hxx"
39cdf0e10cSrcweir #include "osl/thread.hxx"
40cdf0e10cSrcweir #include "osl/mutex.hxx"
41cdf0e10cSrcweir #include "com/sun/star/ucb/CommandAbortedException.hpp"
42cdf0e10cSrcweir #include "com/sun/star/task/XInteractionHandler.hpp"
43cdf0e10cSrcweir #include "com/sun/star/bridge/UnoUrlResolver.hpp"
44cdf0e10cSrcweir #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
45cdf0e10cSrcweir #include "com/sun/star/deployment/ExtensionManager.hpp"
46cdf0e10cSrcweir #include "com/sun/star/task/XRestartManager.hpp"
47cdf0e10cSrcweir #include "boost/scoped_array.hpp"
48cdf0e10cSrcweir #include "boost/shared_ptr.hpp"
49cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #ifdef WNT
52cdf0e10cSrcweir //#include "tools/prewin.h"
53cdf0e10cSrcweir #define UNICODE
54cdf0e10cSrcweir #define _UNICODE
55cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
56cdf0e10cSrcweir #include <Windows.h>
57cdf0e10cSrcweir //#include "tools/postwin.h"
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir 
60cdf0e10cSrcweir using namespace ::com::sun::star;
61cdf0e10cSrcweir using namespace ::com::sun::star::uno;
62cdf0e10cSrcweir using ::rtl::OUString;
63cdf0e10cSrcweir using ::rtl::OString;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #define SOFFICE1 "soffice.exe"
67cdf0e10cSrcweir #define SOFFICE2 "soffice.bin"
68cdf0e10cSrcweir #define SBASE "sbase.exe"
69cdf0e10cSrcweir #define SCALC "scalc.exe"
70cdf0e10cSrcweir #define SDRAW "sdraw.exe"
71cdf0e10cSrcweir #define SIMPRESS "simpress.exe"
72cdf0e10cSrcweir #define SWRITER "swriter.exe"
73cdf0e10cSrcweir 
74cdf0e10cSrcweir namespace dp_misc {
75cdf0e10cSrcweir namespace {
76cdf0e10cSrcweir 
77cdf0e10cSrcweir struct UnoRc : public rtl::StaticWithInit<
78cdf0e10cSrcweir     const boost::shared_ptr<rtl::Bootstrap>, UnoRc> {
operator ()dp_misc::__anon83038faa0111::UnoRc79cdf0e10cSrcweir     const boost::shared_ptr<rtl::Bootstrap> operator () () {
80cdf0e10cSrcweir         OUString unorc( RTL_CONSTASCII_USTRINGPARAM(
81cdf0e10cSrcweir                             "$OOO_BASE_DIR/program/" SAL_CONFIGFILE("uno")) );
82cdf0e10cSrcweir         ::rtl::Bootstrap::expandMacros( unorc );
83cdf0e10cSrcweir         ::boost::shared_ptr< ::rtl::Bootstrap > ret(
84cdf0e10cSrcweir             new ::rtl::Bootstrap( unorc ) );
85cdf0e10cSrcweir         OSL_ASSERT( ret->getHandle() != 0 );
86cdf0e10cSrcweir         return ret;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir };
89cdf0e10cSrcweir 
90cdf0e10cSrcweir struct OfficePipeId : public rtl::StaticWithInit<const OUString, OfficePipeId> {
91cdf0e10cSrcweir     const OUString operator () ();
92cdf0e10cSrcweir };
93cdf0e10cSrcweir 
operator ()()94cdf0e10cSrcweir const OUString OfficePipeId::operator () ()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     OUString userPath;
97cdf0e10cSrcweir 	::utl::Bootstrap::PathStatus aLocateResult =
98cdf0e10cSrcweir 	::utl::Bootstrap::locateUserInstallation( userPath );
99cdf0e10cSrcweir 	if (!(aLocateResult == ::utl::Bootstrap::PATH_EXISTS ||
100cdf0e10cSrcweir 		aLocateResult == ::utl::Bootstrap::PATH_VALID))
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir 		throw Exception(OUSTR("Extension Manager: Could not obtain path for UserInstallation."), 0);
103cdf0e10cSrcweir 	}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     rtlDigest digest = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
106*e2b21863SAriel Constenla-Haile     if (digest == NULL) {
107cdf0e10cSrcweir         throw RuntimeException(
108cdf0e10cSrcweir             OUSTR("cannot get digest rtl_Digest_AlgorithmMD5!"), 0 );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     sal_uInt8 const * data =
112cdf0e10cSrcweir         reinterpret_cast<sal_uInt8 const *>(userPath.getStr());
113cdf0e10cSrcweir     sal_Size size = (userPath.getLength() * sizeof (sal_Unicode));
114cdf0e10cSrcweir     sal_uInt32 md5_key_len = rtl_digest_queryLength( digest );
115cdf0e10cSrcweir     ::boost::scoped_array<sal_uInt8> md5_buf( new sal_uInt8 [ md5_key_len ] );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     rtl_digest_init( digest, data, static_cast<sal_uInt32>(size) );
118cdf0e10cSrcweir     rtl_digest_update( digest, data, static_cast<sal_uInt32>(size) );
119cdf0e10cSrcweir     rtl_digest_get( digest, md5_buf.get(), md5_key_len );
120cdf0e10cSrcweir     rtl_digest_destroy( digest );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     // create hex-value string from the MD5 value to keep
123cdf0e10cSrcweir     // the string size minimal
124cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
125cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("SingleOfficeIPC_") );
126cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < md5_key_len; ++i ) {
127cdf0e10cSrcweir         buf.append( static_cast<sal_Int32>(md5_buf[ i ]), 0x10 );
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir     return buf.makeStringAndClear();
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
existsOfficePipe()132cdf0e10cSrcweir bool existsOfficePipe()
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     OUString const & pipeId = OfficePipeId::get();
135cdf0e10cSrcweir     if (pipeId.getLength() == 0)
136cdf0e10cSrcweir         return false;
137cdf0e10cSrcweir     ::osl::Security sec;
138cdf0e10cSrcweir     ::osl::Pipe pipe( pipeId, osl_Pipe_OPEN, sec );
139cdf0e10cSrcweir     return pipe.is();
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir //Returns true if the Folder was more recently modified then
144cdf0e10cSrcweir //the lastsynchronized file. That is the repository needs to
145cdf0e10cSrcweir //be synchronized.
compareExtensionFolderWithLastSynchronizedFile(OUString const & folderURL,OUString const & fileURL)146cdf0e10cSrcweir bool compareExtensionFolderWithLastSynchronizedFile(
147cdf0e10cSrcweir     OUString const & folderURL, OUString const & fileURL)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir     bool bNeedsSync = false;
150cdf0e10cSrcweir     ::osl::DirectoryItem itemExtFolder;
151cdf0e10cSrcweir     ::osl::File::RC err1 =
152cdf0e10cSrcweir           ::osl::DirectoryItem::get(folderURL, itemExtFolder);
153cdf0e10cSrcweir     //If it does not exist, then there is nothing to be done
154cdf0e10cSrcweir     if (err1 == ::osl::File::E_NOENT)
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         return false;
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     else if (err1 != ::osl::File::E_None)
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         OSL_ENSURE(0, "Cannot access extension folder");
161cdf0e10cSrcweir         return true; //sync just in case
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     //If last synchronized does not exist, then OOo is started for the first time
165cdf0e10cSrcweir     ::osl::DirectoryItem itemFile;
166cdf0e10cSrcweir     ::osl::File::RC err2 = ::osl::DirectoryItem::get(fileURL, itemFile);
167cdf0e10cSrcweir     if (err2 == ::osl::File::E_NOENT)
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         return true;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir     else if (err2 != ::osl::File::E_None)
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         OSL_ENSURE(0, "Cannot access file lastsynchronized");
175cdf0e10cSrcweir         return true; //sync just in case
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     //compare the modification time of the extension folder and the last
179cdf0e10cSrcweir     //modified file
180cdf0e10cSrcweir     ::osl::FileStatus statFolder(FileStatusMask_ModifyTime);
181cdf0e10cSrcweir     ::osl::FileStatus statFile(FileStatusMask_ModifyTime);
182cdf0e10cSrcweir     if (itemExtFolder.getFileStatus(statFolder) == ::osl::File::E_None)
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         if (itemFile.getFileStatus(statFile) == ::osl::File::E_None)
185cdf0e10cSrcweir         {
186cdf0e10cSrcweir             TimeValue timeFolder = statFolder.getModifyTime();
187cdf0e10cSrcweir             TimeValue timeFile = statFile.getModifyTime();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             if (timeFile.Seconds < timeFolder.Seconds)
190cdf0e10cSrcweir                 bNeedsSync = true;
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir         else
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             OSL_ASSERT(0);
195cdf0e10cSrcweir             bNeedsSync = true;
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     else
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         OSL_ASSERT(0);
201cdf0e10cSrcweir         bNeedsSync = true;
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir     return bNeedsSync;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
needToSyncRepostitory(OUString const & name)206cdf0e10cSrcweir bool needToSyncRepostitory(OUString const & name)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     OUString folder;
209cdf0e10cSrcweir     OUString file;
210cdf0e10cSrcweir     if (name.equals(OUString(RTL_CONSTASCII_USTRINGPARAM("bundled"))))
211cdf0e10cSrcweir     {
212cdf0e10cSrcweir         folder = OUString(
213cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("$BUNDLED_EXTENSIONS"));
214cdf0e10cSrcweir         file = OUString (
215cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
216cdf0e10cSrcweir                 "$BUNDLED_EXTENSIONS_USER/lastsynchronized"));
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     else if (name.equals(OUString(RTL_CONSTASCII_USTRINGPARAM("shared"))))
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         folder = OUString(
221cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
222cdf0e10cSrcweir                 "$UNO_SHARED_PACKAGES_CACHE/uno_packages"));
223cdf0e10cSrcweir         file = OUString (
224cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
225cdf0e10cSrcweir                 "$SHARED_EXTENSIONS_USER/lastsynchronized"));
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir     else
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         OSL_ASSERT(0);
230cdf0e10cSrcweir         return true;
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir     ::rtl::Bootstrap::expandMacros(folder);
233cdf0e10cSrcweir     ::rtl::Bootstrap::expandMacros(file);
234cdf0e10cSrcweir     return compareExtensionFolderWithLastSynchronizedFile(
235cdf0e10cSrcweir         folder, file);
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir } // anon namespace
240cdf0e10cSrcweir 
241cdf0e10cSrcweir //==============================================================================
242cdf0e10cSrcweir 
243cdf0e10cSrcweir namespace {
encodeForRcFile(OUString const & str)244cdf0e10cSrcweir inline OUString encodeForRcFile( OUString const & str )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     // escape $\{} (=> rtl bootstrap files)
247cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
248cdf0e10cSrcweir     sal_Int32 pos = 0;
249cdf0e10cSrcweir     const sal_Int32 len = str.getLength();
250cdf0e10cSrcweir     for ( ; pos < len; ++pos ) {
251cdf0e10cSrcweir         sal_Unicode c = str[ pos ];
252cdf0e10cSrcweir         switch (c) {
253cdf0e10cSrcweir         case '$':
254cdf0e10cSrcweir         case '\\':
255cdf0e10cSrcweir         case '{':
256cdf0e10cSrcweir         case '}':
257cdf0e10cSrcweir             buf.append( static_cast<sal_Unicode>('\\') );
258cdf0e10cSrcweir             break;
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir         buf.append( c );
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir     return buf.makeStringAndClear();
263cdf0e10cSrcweir }
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir //==============================================================================
makeURL(OUString const & baseURL,OUString const & relPath_)267cdf0e10cSrcweir OUString makeURL( OUString const & baseURL, OUString const & relPath_ )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
270cdf0e10cSrcweir     if (baseURL.getLength() > 1 && baseURL[ baseURL.getLength() - 1 ] == '/')
271cdf0e10cSrcweir         buf.append( baseURL.copy( 0, baseURL.getLength() - 1 ) );
272cdf0e10cSrcweir     else
273cdf0e10cSrcweir         buf.append( baseURL );
274cdf0e10cSrcweir     OUString relPath(relPath_);
275cdf0e10cSrcweir     if (relPath.getLength() > 0 && relPath[ 0 ] == '/')
276cdf0e10cSrcweir         relPath = relPath.copy( 1 );
277cdf0e10cSrcweir     if (relPath.getLength() > 0)
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         buf.append( static_cast<sal_Unicode>('/') );
280cdf0e10cSrcweir         if (baseURL.matchAsciiL(
281cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.expand:") )) {
282cdf0e10cSrcweir             // encode for macro expansion: relPath is supposed to have no
283cdf0e10cSrcweir             // macros, so encode $, {} \ (bootstrap mimic)
284cdf0e10cSrcweir             relPath = encodeForRcFile(relPath);
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             // encode once more for vnd.sun.star.expand schema:
287cdf0e10cSrcweir             // vnd.sun.star.expand:$UNO_...
288cdf0e10cSrcweir             // will expand to file-url
289cdf0e10cSrcweir             relPath = ::rtl::Uri::encode( relPath, rtl_UriCharClassUric,
290cdf0e10cSrcweir                                           rtl_UriEncodeIgnoreEscapes,
291cdf0e10cSrcweir                                           RTL_TEXTENCODING_UTF8 );
292cdf0e10cSrcweir         }
293cdf0e10cSrcweir         buf.append( relPath );
294cdf0e10cSrcweir     }
295cdf0e10cSrcweir     return buf.makeStringAndClear();
296cdf0e10cSrcweir }
297cdf0e10cSrcweir 
makeURLAppendSysPathSegment(OUString const & baseURL,OUString const & relPath_)298cdf0e10cSrcweir OUString makeURLAppendSysPathSegment( OUString const & baseURL, OUString const & relPath_ )
299cdf0e10cSrcweir {
300cdf0e10cSrcweir     OUString segment = relPath_;
301cdf0e10cSrcweir     OSL_ASSERT(segment.indexOf(static_cast<sal_Unicode>('/')) == -1);
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     ::rtl::Uri::encode(
304cdf0e10cSrcweir         segment, rtl_UriCharClassPchar, rtl_UriEncodeIgnoreEscapes,
305cdf0e10cSrcweir         RTL_TEXTENCODING_UTF8);
306cdf0e10cSrcweir     return makeURL(baseURL, segment);
307cdf0e10cSrcweir }
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 
311cdf0e10cSrcweir //==============================================================================
expandUnoRcTerm(OUString const & term_)312cdf0e10cSrcweir OUString expandUnoRcTerm( OUString const & term_ )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir     OUString term(term_);
315cdf0e10cSrcweir     UnoRc::get()->expandMacrosFrom( term );
316cdf0e10cSrcweir     return term;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
makeRcTerm(OUString const & url)319cdf0e10cSrcweir OUString makeRcTerm( OUString const & url )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir     OSL_ASSERT( url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(
322cdf0e10cSrcweir                                      "vnd.sun.star.expand:") ) );
323cdf0e10cSrcweir     if (url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.expand:") )) {
324cdf0e10cSrcweir         // cut protocol:
325cdf0e10cSrcweir         OUString rcterm( url.copy( sizeof ("vnd.sun.star.expand:") - 1 ) );
326cdf0e10cSrcweir         // decode uric class chars:
327cdf0e10cSrcweir         rcterm = ::rtl::Uri::decode(
328cdf0e10cSrcweir             rcterm, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
329cdf0e10cSrcweir         return rcterm;
330cdf0e10cSrcweir     }
331cdf0e10cSrcweir     else
332cdf0e10cSrcweir         return url;
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir //==============================================================================
expandUnoRcUrl(OUString const & url)336cdf0e10cSrcweir OUString expandUnoRcUrl( OUString const & url )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir     if (url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.expand:") )) {
339cdf0e10cSrcweir         // cut protocol:
340cdf0e10cSrcweir         OUString rcurl( url.copy( sizeof ("vnd.sun.star.expand:") - 1 ) );
341cdf0e10cSrcweir         // decode uric class chars:
342cdf0e10cSrcweir         rcurl = ::rtl::Uri::decode(
343cdf0e10cSrcweir             rcurl, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
344cdf0e10cSrcweir         // expand macro string:
345cdf0e10cSrcweir         UnoRc::get()->expandMacrosFrom( rcurl );
346cdf0e10cSrcweir         return rcurl;
347cdf0e10cSrcweir     }
348cdf0e10cSrcweir     else {
349cdf0e10cSrcweir         return url;
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir //==============================================================================
office_is_running()354cdf0e10cSrcweir bool office_is_running()
355cdf0e10cSrcweir {
356cdf0e10cSrcweir     //We need to check if we run within the office process. Then we must not use the pipe, because
357cdf0e10cSrcweir     //this could cause a deadlock. This is actually a workaround for i82778
358cdf0e10cSrcweir     OUString sFile;
359cdf0e10cSrcweir     oslProcessError err = osl_getExecutableFile(& sFile.pData);
360cdf0e10cSrcweir     bool ret = false;
361cdf0e10cSrcweir     if (osl_Process_E_None == err)
362cdf0e10cSrcweir     {
363cdf0e10cSrcweir         sFile = sFile.copy(sFile.lastIndexOf('/') + 1);
364cdf0e10cSrcweir         if (
365cdf0e10cSrcweir #if defined UNIX
366cdf0e10cSrcweir             sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SOFFICE2)))
367cdf0e10cSrcweir #elif defined WNT || defined OS2
368cdf0e10cSrcweir             //osl_getExecutableFile should deliver "soffice.bin" on windows
369cdf0e10cSrcweir             //even if swriter.exe, scalc.exe etc. was started. This is a bug
370cdf0e10cSrcweir             //in osl_getExecutableFile
371cdf0e10cSrcweir             sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SOFFICE1)))
372cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SOFFICE2)))
373cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SBASE)))
374cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SCALC)))
375cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SDRAW)))
376cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SIMPRESS)))
377cdf0e10cSrcweir             || sFile.equals(OUString(RTL_CONSTASCII_USTRINGPARAM(SWRITER)))
378cdf0e10cSrcweir #else
379cdf0e10cSrcweir #error "Unsupported platform"
380cdf0e10cSrcweir #endif
381cdf0e10cSrcweir 
382cdf0e10cSrcweir             )
383cdf0e10cSrcweir             ret = true;
384cdf0e10cSrcweir         else
385cdf0e10cSrcweir             ret = existsOfficePipe();
386cdf0e10cSrcweir     }
387cdf0e10cSrcweir     else
388cdf0e10cSrcweir     {
389cdf0e10cSrcweir         OSL_ENSURE(0, "NOT osl_Process_E_None ");
390cdf0e10cSrcweir         //if osl_getExecutable file than we take the risk of creating a pipe
391cdf0e10cSrcweir         ret =  existsOfficePipe();
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir     return ret;
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir //==============================================================================
raiseProcess(OUString const & appURL,Sequence<OUString> const & args)397cdf0e10cSrcweir oslProcess raiseProcess(
398cdf0e10cSrcweir     OUString const & appURL, Sequence<OUString> const & args )
399cdf0e10cSrcweir {
400cdf0e10cSrcweir     ::osl::Security sec;
401cdf0e10cSrcweir     oslProcess hProcess = 0;
402cdf0e10cSrcweir     oslProcessError rc = osl_executeProcess(
403cdf0e10cSrcweir         appURL.pData,
404cdf0e10cSrcweir         reinterpret_cast<rtl_uString **>(
405cdf0e10cSrcweir             const_cast<OUString *>(args.getConstArray()) ),
406cdf0e10cSrcweir         args.getLength(),
407cdf0e10cSrcweir         osl_Process_DETACHED,
408cdf0e10cSrcweir         sec.getHandle(),
409cdf0e10cSrcweir         0, // => current working dir
410cdf0e10cSrcweir         0, 0, // => no env vars
411cdf0e10cSrcweir         &hProcess );
412cdf0e10cSrcweir 
413cdf0e10cSrcweir     switch (rc) {
414cdf0e10cSrcweir     case osl_Process_E_None:
415cdf0e10cSrcweir         break;
416cdf0e10cSrcweir     case osl_Process_E_NotFound:
417cdf0e10cSrcweir         throw RuntimeException( OUSTR("image not found!"), 0 );
418cdf0e10cSrcweir     case osl_Process_E_TimedOut:
419cdf0e10cSrcweir         throw RuntimeException( OUSTR("timout occured!"), 0 );
420cdf0e10cSrcweir     case osl_Process_E_NoPermission:
421cdf0e10cSrcweir         throw RuntimeException( OUSTR("permission denied!"), 0 );
422cdf0e10cSrcweir     case osl_Process_E_Unknown:
423cdf0e10cSrcweir         throw RuntimeException( OUSTR("unknown error!"), 0 );
424cdf0e10cSrcweir     case osl_Process_E_InvalidError:
425cdf0e10cSrcweir     default:
426cdf0e10cSrcweir         throw RuntimeException( OUSTR("unmapped error!"), 0 );
427cdf0e10cSrcweir     }
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     return hProcess;
430cdf0e10cSrcweir }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir //==============================================================================
generateRandomPipeId()433cdf0e10cSrcweir OUString generateRandomPipeId()
434cdf0e10cSrcweir {
435cdf0e10cSrcweir     // compute some good pipe id:
436cdf0e10cSrcweir     static rtlRandomPool s_hPool = rtl_random_createPool();
437cdf0e10cSrcweir     if (s_hPool == 0)
438cdf0e10cSrcweir         throw RuntimeException( OUSTR("cannot create random pool!?"), 0 );
439cdf0e10cSrcweir     sal_uInt8 bytes[ 32 ];
440cdf0e10cSrcweir     if (rtl_random_getBytes(
441cdf0e10cSrcweir             s_hPool, bytes, ARLEN(bytes) ) != rtl_Random_E_None) {
442cdf0e10cSrcweir         throw RuntimeException( OUSTR("random pool error!?"), 0 );
443cdf0e10cSrcweir     }
444cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
445cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < ARLEN(bytes); ++i ) {
446cdf0e10cSrcweir         buf.append( static_cast<sal_Int32>(bytes[ i ]), 0x10 );
447cdf0e10cSrcweir     }
448cdf0e10cSrcweir     return buf.makeStringAndClear();
449cdf0e10cSrcweir }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir //==============================================================================
resolveUnoURL(OUString const & connectString,Reference<XComponentContext> const & xLocalContext,AbortChannel * abortChannel)452cdf0e10cSrcweir Reference<XInterface> resolveUnoURL(
453cdf0e10cSrcweir     OUString const & connectString,
454cdf0e10cSrcweir     Reference<XComponentContext> const & xLocalContext,
455cdf0e10cSrcweir     AbortChannel * abortChannel )
456cdf0e10cSrcweir {
457cdf0e10cSrcweir     Reference<bridge::XUnoUrlResolver> xUnoUrlResolver(
458cdf0e10cSrcweir         bridge::UnoUrlResolver::create( xLocalContext ) );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     for (;;)
461cdf0e10cSrcweir     {
462cdf0e10cSrcweir         if (abortChannel != 0 && abortChannel->isAborted()) {
463cdf0e10cSrcweir             throw ucb::CommandAbortedException(
464cdf0e10cSrcweir                 OUSTR("abort!"), Reference<XInterface>() );
465cdf0e10cSrcweir         }
466cdf0e10cSrcweir         try {
467cdf0e10cSrcweir             return xUnoUrlResolver->resolve( connectString );
468cdf0e10cSrcweir         }
469cdf0e10cSrcweir         catch (connection::NoConnectException &) {
470cdf0e10cSrcweir             TimeValue tv = { 0 /* secs */, 500000000 /* nanosecs */ };
471cdf0e10cSrcweir             ::osl::Thread::wait( tv );
472cdf0e10cSrcweir         }
473cdf0e10cSrcweir     }
474cdf0e10cSrcweir }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir #ifdef WNT
writeConsoleWithStream(::rtl::OUString const & sText,HANDLE stream)477cdf0e10cSrcweir void writeConsoleWithStream(::rtl::OUString const & sText, HANDLE stream)
478cdf0e10cSrcweir {
479cdf0e10cSrcweir     DWORD nWrittenChars = 0;
480cdf0e10cSrcweir     WriteFile(stream, sText.getStr(),
481cdf0e10cSrcweir         sText.getLength() * 2, &nWrittenChars, NULL);
482cdf0e10cSrcweir }
483cdf0e10cSrcweir #else
writeConsoleWithStream(::rtl::OUString const & sText,FILE * stream)484cdf0e10cSrcweir void writeConsoleWithStream(::rtl::OUString const & sText, FILE * stream)
485cdf0e10cSrcweir {
486cdf0e10cSrcweir     OString s = OUStringToOString(sText, osl_getThreadTextEncoding());
487cdf0e10cSrcweir     fprintf(stream, "%s", s.getStr());
488cdf0e10cSrcweir     fflush(stream);
489cdf0e10cSrcweir }
490cdf0e10cSrcweir #endif
491cdf0e10cSrcweir 
492cdf0e10cSrcweir #ifdef WNT
writeConsoleWithStream(::rtl::OString const & sText,HANDLE stream)493cdf0e10cSrcweir void writeConsoleWithStream(::rtl::OString const & sText, HANDLE stream)
494cdf0e10cSrcweir {
495cdf0e10cSrcweir     writeConsoleWithStream(OStringToOUString(
496cdf0e10cSrcweir         sText, RTL_TEXTENCODING_UTF8), stream);
497cdf0e10cSrcweir }
498cdf0e10cSrcweir #else
writeConsoleWithStream(::rtl::OString const & sText,FILE * stream)499cdf0e10cSrcweir void writeConsoleWithStream(::rtl::OString const & sText, FILE * stream)
500cdf0e10cSrcweir {
501cdf0e10cSrcweir     fprintf(stream, "%s", sText.getStr());
502cdf0e10cSrcweir     fflush(stream);
503cdf0e10cSrcweir }
504cdf0e10cSrcweir #endif
505cdf0e10cSrcweir 
writeConsole(::rtl::OUString const & sText)506cdf0e10cSrcweir void writeConsole(::rtl::OUString const & sText)
507cdf0e10cSrcweir {
508cdf0e10cSrcweir #ifdef WNT
509cdf0e10cSrcweir     writeConsoleWithStream(sText, GetStdHandle(STD_OUTPUT_HANDLE));
510cdf0e10cSrcweir #else
511cdf0e10cSrcweir     writeConsoleWithStream(sText, stdout);
512cdf0e10cSrcweir #endif
513cdf0e10cSrcweir }
514cdf0e10cSrcweir 
writeConsole(::rtl::OString const & sText)515cdf0e10cSrcweir void writeConsole(::rtl::OString const & sText)
516cdf0e10cSrcweir {
517cdf0e10cSrcweir #ifdef WNT
518cdf0e10cSrcweir     writeConsoleWithStream(sText, GetStdHandle(STD_OUTPUT_HANDLE));
519cdf0e10cSrcweir #else
520cdf0e10cSrcweir     writeConsoleWithStream(sText, stdout);
521cdf0e10cSrcweir #endif
522cdf0e10cSrcweir }
523cdf0e10cSrcweir 
writeConsoleError(::rtl::OUString const & sText)524cdf0e10cSrcweir void writeConsoleError(::rtl::OUString const & sText)
525cdf0e10cSrcweir {
526cdf0e10cSrcweir #ifdef WNT
527cdf0e10cSrcweir     writeConsoleWithStream(sText, GetStdHandle(STD_ERROR_HANDLE));
528cdf0e10cSrcweir #else
529cdf0e10cSrcweir     writeConsoleWithStream(sText, stderr);
530cdf0e10cSrcweir #endif
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 
writeConsoleError(::rtl::OString const & sText)534cdf0e10cSrcweir void writeConsoleError(::rtl::OString const & sText)
535cdf0e10cSrcweir {
536cdf0e10cSrcweir #ifdef WNT
537cdf0e10cSrcweir     writeConsoleWithStream(sText, GetStdHandle(STD_ERROR_HANDLE));
538cdf0e10cSrcweir #else
539cdf0e10cSrcweir     writeConsoleWithStream(sText, stderr);
540cdf0e10cSrcweir #endif
541cdf0e10cSrcweir }
542cdf0e10cSrcweir 
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 
readConsole()545cdf0e10cSrcweir OUString readConsole()
546cdf0e10cSrcweir {
547cdf0e10cSrcweir #ifdef WNT
548cdf0e10cSrcweir     sal_Unicode aBuffer[1024];
549cdf0e10cSrcweir 	DWORD	dwRead = 0;
550cdf0e10cSrcweir     //unopkg.com feeds unopkg.exe with wchar_t|s
551cdf0e10cSrcweir     if (ReadFile( GetStdHandle(STD_INPUT_HANDLE), &aBuffer, sizeof(aBuffer), &dwRead, NULL ) )
552cdf0e10cSrcweir 	{
553cdf0e10cSrcweir         OSL_ASSERT((dwRead % 2) == 0);
554cdf0e10cSrcweir         OUString value( aBuffer, dwRead / 2);
555cdf0e10cSrcweir         return value.trim();
556cdf0e10cSrcweir 	}
557cdf0e10cSrcweir #else
558cdf0e10cSrcweir 	char buf[1024];
559cdf0e10cSrcweir 	rtl_zeroMemory(buf, 1024);
560cdf0e10cSrcweir 	// read one char less so that the last char in buf is always zero
561cdf0e10cSrcweir 	if (fgets(buf, 1024, stdin) != NULL)
562cdf0e10cSrcweir     {
563cdf0e10cSrcweir         OUString value = ::rtl::OStringToOUString(::rtl::OString(buf), osl_getThreadTextEncoding());
564cdf0e10cSrcweir         return value.trim();
565cdf0e10cSrcweir     }
566cdf0e10cSrcweir #endif
567cdf0e10cSrcweir     return OUString();
568cdf0e10cSrcweir }
569cdf0e10cSrcweir 
TRACE(::rtl::OUString const & sText)570cdf0e10cSrcweir void TRACE(::rtl::OUString const & sText)
571cdf0e10cSrcweir {
572cdf0e10cSrcweir     (void) sText;
573cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
574cdf0e10cSrcweir     writeConsole(sText);
575cdf0e10cSrcweir #endif
576cdf0e10cSrcweir }
577cdf0e10cSrcweir 
TRACE(::rtl::OString const & sText)578cdf0e10cSrcweir void TRACE(::rtl::OString const & sText)
579cdf0e10cSrcweir {
580cdf0e10cSrcweir     (void) sText;
581cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
582cdf0e10cSrcweir     writeConsole(sText);
583cdf0e10cSrcweir #endif
584cdf0e10cSrcweir }
585cdf0e10cSrcweir 
syncRepositories(Reference<ucb::XCommandEnvironment> const & xCmdEnv)586cdf0e10cSrcweir void syncRepositories(Reference<ucb::XCommandEnvironment> const & xCmdEnv)
587cdf0e10cSrcweir {
588cdf0e10cSrcweir     OUString sDisable;
589cdf0e10cSrcweir     ::rtl::Bootstrap::get( OUSTR( "DISABLE_EXTENSION_SYNCHRONIZATION" ), sDisable, OUString() );
590cdf0e10cSrcweir     if (sDisable.getLength() > 0)
591cdf0e10cSrcweir         return;
592cdf0e10cSrcweir 
593cdf0e10cSrcweir     Reference<deployment::XExtensionManager> xExtensionManager;
594cdf0e10cSrcweir     //synchronize shared before bundled otherewise there are
595cdf0e10cSrcweir     //more revoke and registration calls.
596cdf0e10cSrcweir     sal_Bool bModified = false;
597cdf0e10cSrcweir     if (needToSyncRepostitory(OUString(RTL_CONSTASCII_USTRINGPARAM("shared")))
598cdf0e10cSrcweir         || needToSyncRepostitory(OUString(RTL_CONSTASCII_USTRINGPARAM("bundled"))))
599cdf0e10cSrcweir     {
600cdf0e10cSrcweir         xExtensionManager =
601cdf0e10cSrcweir             deployment::ExtensionManager::get(
602cdf0e10cSrcweir                 comphelper_getProcessComponentContext());
603cdf0e10cSrcweir 
604cdf0e10cSrcweir         if (xExtensionManager.is())
605cdf0e10cSrcweir         {
606cdf0e10cSrcweir             bModified = xExtensionManager->synchronize(
607cdf0e10cSrcweir                 Reference<task::XAbortChannel>(), xCmdEnv);
608cdf0e10cSrcweir         }
609cdf0e10cSrcweir     }
610cdf0e10cSrcweir 
611cdf0e10cSrcweir     if (bModified)
612cdf0e10cSrcweir     {
613cdf0e10cSrcweir         Reference<task::XRestartManager> restarter(
614cdf0e10cSrcweir             comphelper_getProcessComponentContext()->getValueByName(
615cdf0e10cSrcweir                 OUSTR( "/singletons/com.sun.star.task.OfficeRestartManager") ), UNO_QUERY );
616cdf0e10cSrcweir         if (restarter.is())
617cdf0e10cSrcweir         {
618cdf0e10cSrcweir             restarter->requestRestart(xCmdEnv.is() == sal_True ? xCmdEnv->getInteractionHandler() :
619cdf0e10cSrcweir                                       Reference<task::XInteractionHandler>());
620cdf0e10cSrcweir         }
621cdf0e10cSrcweir      }
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir 
625cdf0e10cSrcweir 
626cdf0e10cSrcweir }
627