1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SalGtkPicker.cxx,v $
10 * $Revision: 1.17.32.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_fpicker.hxx"
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <com/sun/star/lang/DisposedException.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
40 #include <com/sun/star/lang/SystemDependent.hpp>
41 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
42 #include <com/sun/star/awt/SystemDependentXWindow.hpp>
43 #include <com/sun/star/beans/NamedValue.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <cppuhelper/interfacecontainer.h>
46 #include <rtl/process.h>
47 #include <osl/diagnose.h>
48 #include <com/sun/star/uno/Any.hxx>
49 #include <FPServiceInfo.hxx>
50 #include <vos/mutex.hxx>
51 #include <vcl/svapp.hxx>
52 #include "SalGtkPicker.hxx"
53 #include <tools/urlobj.hxx>
56 //------------------------------------------------------------------------
57 // namespace directives
58 //------------------------------------------------------------------------
60 using namespace ::rtl
;
61 using namespace ::com::sun::star
;
62 using namespace ::com::sun::star::lang
;
63 using namespace ::com::sun::star::uno
;
65 rtl::OUString
SalGtkPicker::uritounicode(const gchar
* pIn
)
68 return rtl::OUString();
70 rtl::OUString
sURL( const_cast<const sal_Char
*>(pIn
), strlen(pIn
),
71 RTL_TEXTENCODING_UTF8
);
73 INetURLObject
aURL(sURL
);
74 if (INET_PROT_FILE
== aURL
.GetProtocol())
76 gchar
*pEncodedFileName
= g_filename_from_uri(pIn
, NULL
, NULL
);
77 if ( pEncodedFileName
)
79 rtl::OUString
sEncoded(pEncodedFileName
, strlen(pEncodedFileName
),
80 osl_getThreadTextEncoding());
81 INetURLObject
aCurrentURL(sEncoded
, INetURLObject::FSYS_UNX
);
82 aCurrentURL
.SetHost(aURL
.GetHost());
83 sURL
= aCurrentURL
.getExternalURL();
87 OUString aNewURL
= Reference
<uri::XExternalUriReferenceTranslator
>(Reference
<XMultiServiceFactory
>(comphelper::getProcessServiceFactory(), UNO_QUERY_THROW
)->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uri.ExternalUriReferenceTranslator"))), UNO_QUERY_THROW
)->translateToInternal(sURL
);
88 if( aNewURL
.getLength() )
95 rtl::OString
SalGtkPicker::unicodetouri(const rtl::OUString
&rURL
)
97 OString sURL
= OUStringToOString(rURL
, RTL_TEXTENCODING_UTF8
);
98 INetURLObject
aURL(rURL
);
99 if (INET_PROT_FILE
== aURL
.GetProtocol())
101 rtl::OUString sOUURL
= aURL
.getExternalURL(INetURLObject::DECODE_WITH_CHARSET
, osl_getThreadTextEncoding());
102 sURL
= OUStringToOString( sOUURL
, osl_getThreadTextEncoding());
107 gboolean
canceldialog(RunDialog
*pDialog
)
116 extern GdkDisplay
* gdk_x11_lookup_xdisplay (void*xdisplay
);
119 RunDialog::RunDialog( GtkWidget
*pDialog
, uno::Reference
< awt::XExtendedToolkit
>& rToolkit
) :
120 cppu::WeakComponentImplHelper1
< awt::XTopWindowListener
>( maLock
),
121 mpDialog(pDialog
), mpCreatedParent(NULL
), mxToolkit(rToolkit
)
123 awt::SystemDependentXWindow aWindowHandle
;
127 uno::Reference
< awt::XTopWindow
> xWindow(mxToolkit
->getActiveTopWindow());
130 uno::Reference
< awt::XSystemDependentWindowPeer
> xSystemDepParent(xWindow
, uno::UNO_QUERY
);
131 if (xSystemDepParent
.is())
134 sal_Int8 processID
[16];
136 rtl_getGlobalProcessId( (sal_uInt8
*)processID
);
137 uno::Sequence
<sal_Int8
> processIdSeq(processID
, 16);
138 uno::Any anyHandle
= xSystemDepParent
->getWindowHandle(processIdSeq
, SystemDependent::SYSTEM_XWINDOW
);
140 anyHandle
>>= aWindowHandle
;
145 GdkDisplay
*pDisplay
= aWindowHandle
.DisplayPointer
? gdk_x11_lookup_xdisplay(reinterpret_cast<void*>(static_cast<sal_IntPtr
>(aWindowHandle
.DisplayPointer
))) : NULL
;
146 GdkWindow
* pParent
= pDisplay
? gdk_window_lookup_for_display(pDisplay
, aWindowHandle
.WindowHandle
) : NULL
;
147 if (!pParent
&& pDisplay
)
148 pParent
= mpCreatedParent
= gdk_window_foreign_new_for_display( pDisplay
, aWindowHandle
.WindowHandle
);
151 gtk_widget_realize(mpDialog
);
152 gdk_window_set_transient_for(mpDialog
->window
, pParent
);
157 RunDialog::~RunDialog()
160 gdk_window_destroy (mpCreatedParent
);
163 void SAL_CALL
RunDialog::windowOpened( const ::com::sun::star::lang::EventObject
& )
164 throw (::com::sun::star::uno::RuntimeException
)
166 g_timeout_add_full(G_PRIORITY_HIGH_IDLE
, 0, (GSourceFunc
)canceldialog
, this, NULL
);
169 void RunDialog::cancel()
171 gtk_dialog_response( GTK_DIALOG( mpDialog
), GTK_RESPONSE_CANCEL
);
172 gtk_widget_hide( mpDialog
);
175 gint
RunDialog::run()
178 mxToolkit
->addTopWindowListener(this);
180 gint nStatus
= gtk_dialog_run( GTK_DIALOG( mpDialog
) );
183 mxToolkit
->removeTopWindowListener(this);
185 if (nStatus
!= 1) //PLAY
186 gtk_widget_hide( mpDialog
);
191 SalGtkPicker::~SalGtkPicker()
194 gtk_widget_destroy(m_pDialog
);
197 void SAL_CALL
SalGtkPicker::implsetDisplayDirectory( const rtl::OUString
& aDirectory
)
198 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
200 OSL_ASSERT( m_pDialog
!= NULL
);
201 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
203 OString aTxt
= unicodetouri(aDirectory
);
205 if( aTxt
.lastIndexOf('/') == aTxt
.getLength() - 1 )
206 aTxt
= aTxt
.copy( 0, aTxt
.getLength() - 1 );
208 OSL_TRACE( "setting path to %s\n", aTxt
.getStr() );
210 gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog
),
214 rtl::OUString SAL_CALL
SalGtkPicker::implgetDisplayDirectory() throw( uno::RuntimeException
)
216 OSL_ASSERT( m_pDialog
!= NULL
);
217 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
219 gchar
* pCurrentFolder
=
220 gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog
) );
221 ::rtl::OUString aCurrentFolderName
= uritounicode(pCurrentFolder
);
222 g_free( pCurrentFolder
);
224 return aCurrentFolderName
;
227 void SAL_CALL
SalGtkPicker::implsetTitle( const rtl::OUString
& aTitle
) throw( uno::RuntimeException
)
229 OSL_ASSERT( m_pDialog
!= NULL
);
230 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
232 ::rtl::OString aWindowTitle
= OUStringToOString( aTitle
, RTL_TEXTENCODING_UTF8
);
233 gtk_window_set_title( GTK_WINDOW( m_pDialog
), aWindowTitle
.getStr() );