1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_fpicker.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
28*b1cdbd2cSJim Jagielski // includes
29*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
30*b1cdbd2cSJim Jagielski #include <com/sun/star/lang/DisposedException.hpp>
31*b1cdbd2cSJim Jagielski #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32*b1cdbd2cSJim Jagielski #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
33*b1cdbd2cSJim Jagielski #include <com/sun/star/lang/SystemDependent.hpp>
34*b1cdbd2cSJim Jagielski #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
35*b1cdbd2cSJim Jagielski #include <com/sun/star/awt/SystemDependentXWindow.hpp>
36*b1cdbd2cSJim Jagielski #include <com/sun/star/beans/NamedValue.hpp>
37*b1cdbd2cSJim Jagielski #include <com/sun/star/container/XNameAccess.hpp>
38*b1cdbd2cSJim Jagielski #include <comphelper/processfactory.hxx>
39*b1cdbd2cSJim Jagielski #include <cppuhelper/interfacecontainer.h>
40*b1cdbd2cSJim Jagielski #include <rtl/process.h>
41*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
42*b1cdbd2cSJim Jagielski #include <com/sun/star/uno/Any.hxx>
43*b1cdbd2cSJim Jagielski #include <FPServiceInfo.hxx>
44*b1cdbd2cSJim Jagielski #include <vos/mutex.hxx>
45*b1cdbd2cSJim Jagielski #include <vcl/svapp.hxx>
46*b1cdbd2cSJim Jagielski #include "SalGtkPicker.hxx"
47*b1cdbd2cSJim Jagielski #include <tools/urlobj.hxx>
48*b1cdbd2cSJim Jagielski #include <stdio.h>
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
51*b1cdbd2cSJim Jagielski // namespace directives
52*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski using namespace ::rtl;
55*b1cdbd2cSJim Jagielski using namespace ::com::sun::star;
56*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::lang;
57*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::uno;
58*b1cdbd2cSJim Jagielski
uritounicode(const gchar * pIn)59*b1cdbd2cSJim Jagielski rtl::OUString SalGtkPicker::uritounicode(const gchar* pIn)
60*b1cdbd2cSJim Jagielski {
61*b1cdbd2cSJim Jagielski if (!pIn)
62*b1cdbd2cSJim Jagielski return rtl::OUString();
63*b1cdbd2cSJim Jagielski
64*b1cdbd2cSJim Jagielski rtl::OUString sURL( const_cast<const sal_Char *>(pIn), strlen(pIn),
65*b1cdbd2cSJim Jagielski RTL_TEXTENCODING_UTF8 );
66*b1cdbd2cSJim Jagielski
67*b1cdbd2cSJim Jagielski INetURLObject aURL(sURL);
68*b1cdbd2cSJim Jagielski if (INET_PROT_FILE == aURL.GetProtocol())
69*b1cdbd2cSJim Jagielski {
70*b1cdbd2cSJim Jagielski // all the URLs are handled by office in UTF-8
71*b1cdbd2cSJim Jagielski // so the Gnome FP related URLs should be converted accordingly
72*b1cdbd2cSJim Jagielski gchar *pEncodedFileName = g_filename_from_uri(pIn, NULL, NULL);
73*b1cdbd2cSJim Jagielski if ( pEncodedFileName )
74*b1cdbd2cSJim Jagielski {
75*b1cdbd2cSJim Jagielski rtl::OUString sEncoded(pEncodedFileName, strlen(pEncodedFileName),
76*b1cdbd2cSJim Jagielski osl_getThreadTextEncoding());
77*b1cdbd2cSJim Jagielski INetURLObject aCurrentURL(sEncoded, INetURLObject::FSYS_UNX);
78*b1cdbd2cSJim Jagielski aCurrentURL.SetHost(aURL.GetHost());
79*b1cdbd2cSJim Jagielski sURL = aCurrentURL.getExternalURL();
80*b1cdbd2cSJim Jagielski }
81*b1cdbd2cSJim Jagielski else
82*b1cdbd2cSJim Jagielski {
83*b1cdbd2cSJim Jagielski 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);
84*b1cdbd2cSJim Jagielski if( aNewURL.getLength() )
85*b1cdbd2cSJim Jagielski sURL = aNewURL;
86*b1cdbd2cSJim Jagielski }
87*b1cdbd2cSJim Jagielski }
88*b1cdbd2cSJim Jagielski return sURL;
89*b1cdbd2cSJim Jagielski }
90*b1cdbd2cSJim Jagielski
unicodetouri(const rtl::OUString & rURL)91*b1cdbd2cSJim Jagielski rtl::OString SalGtkPicker::unicodetouri(const rtl::OUString &rURL)
92*b1cdbd2cSJim Jagielski {
93*b1cdbd2cSJim Jagielski // all the URLs are handled by office in UTF-8 ( and encoded with "%xx" codes based on UTF-8 )
94*b1cdbd2cSJim Jagielski // so the Gnome FP related URLs should be converted accordingly
95*b1cdbd2cSJim Jagielski OString sURL = OUStringToOString(rURL, RTL_TEXTENCODING_UTF8);
96*b1cdbd2cSJim Jagielski INetURLObject aURL(rURL);
97*b1cdbd2cSJim Jagielski if (INET_PROT_FILE == aURL.GetProtocol())
98*b1cdbd2cSJim Jagielski {
99*b1cdbd2cSJim Jagielski 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)->translateToExternal( rURL );
100*b1cdbd2cSJim Jagielski
101*b1cdbd2cSJim Jagielski if( aNewURL.getLength() )
102*b1cdbd2cSJim Jagielski {
103*b1cdbd2cSJim Jagielski // At this point the URL should contain ascii characters only actually
104*b1cdbd2cSJim Jagielski sURL = OUStringToOString( aNewURL, osl_getThreadTextEncoding() );
105*b1cdbd2cSJim Jagielski }
106*b1cdbd2cSJim Jagielski }
107*b1cdbd2cSJim Jagielski return sURL;
108*b1cdbd2cSJim Jagielski }
109*b1cdbd2cSJim Jagielski
canceldialog(RunDialog * pDialog)110*b1cdbd2cSJim Jagielski gboolean canceldialog(RunDialog *pDialog)
111*b1cdbd2cSJim Jagielski {
112*b1cdbd2cSJim Jagielski pDialog->cancel();
113*b1cdbd2cSJim Jagielski return false;
114*b1cdbd2cSJim Jagielski }
115*b1cdbd2cSJim Jagielski
116*b1cdbd2cSJim Jagielski extern "C"
117*b1cdbd2cSJim Jagielski {
118*b1cdbd2cSJim Jagielski struct Display;
119*b1cdbd2cSJim Jagielski extern GdkDisplay* gdk_x11_lookup_xdisplay (void*xdisplay);
120*b1cdbd2cSJim Jagielski }
121*b1cdbd2cSJim Jagielski
RunDialog(GtkWidget * pDialog,uno::Reference<awt::XExtendedToolkit> & rToolkit)122*b1cdbd2cSJim Jagielski RunDialog::RunDialog( GtkWidget *pDialog, uno::Reference< awt::XExtendedToolkit >& rToolkit ) :
123*b1cdbd2cSJim Jagielski cppu::WeakComponentImplHelper1< awt::XTopWindowListener >( maLock ),
124*b1cdbd2cSJim Jagielski mpDialog(pDialog), mpCreatedParent(NULL), mxToolkit(rToolkit)
125*b1cdbd2cSJim Jagielski {
126*b1cdbd2cSJim Jagielski awt::SystemDependentXWindow aWindowHandle;
127*b1cdbd2cSJim Jagielski
128*b1cdbd2cSJim Jagielski if (mxToolkit.is())
129*b1cdbd2cSJim Jagielski {
130*b1cdbd2cSJim Jagielski uno::Reference< awt::XTopWindow > xWindow(mxToolkit->getActiveTopWindow());
131*b1cdbd2cSJim Jagielski if (xWindow.is())
132*b1cdbd2cSJim Jagielski {
133*b1cdbd2cSJim Jagielski uno::Reference< awt::XSystemDependentWindowPeer > xSystemDepParent(xWindow, uno::UNO_QUERY);
134*b1cdbd2cSJim Jagielski if (xSystemDepParent.is())
135*b1cdbd2cSJim Jagielski {
136*b1cdbd2cSJim Jagielski
137*b1cdbd2cSJim Jagielski sal_Int8 processID[16];
138*b1cdbd2cSJim Jagielski
139*b1cdbd2cSJim Jagielski rtl_getGlobalProcessId( (sal_uInt8*)processID );
140*b1cdbd2cSJim Jagielski uno::Sequence<sal_Int8> processIdSeq(processID, 16);
141*b1cdbd2cSJim Jagielski uno::Any anyHandle = xSystemDepParent->getWindowHandle(processIdSeq, SystemDependent::SYSTEM_XWINDOW);
142*b1cdbd2cSJim Jagielski
143*b1cdbd2cSJim Jagielski anyHandle >>= aWindowHandle;
144*b1cdbd2cSJim Jagielski }
145*b1cdbd2cSJim Jagielski }
146*b1cdbd2cSJim Jagielski }
147*b1cdbd2cSJim Jagielski
148*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski GdkDisplay *pDisplay = aWindowHandle.DisplayPointer ? gdk_x11_lookup_xdisplay(reinterpret_cast<void*>(static_cast<sal_IntPtr>(aWindowHandle.DisplayPointer))) : NULL;
151*b1cdbd2cSJim Jagielski GdkWindow* pParent = pDisplay ? gdk_window_lookup_for_display(pDisplay, aWindowHandle.WindowHandle) : NULL;
152*b1cdbd2cSJim Jagielski if (!pParent && pDisplay)
153*b1cdbd2cSJim Jagielski pParent = mpCreatedParent = gdk_window_foreign_new_for_display( pDisplay, aWindowHandle.WindowHandle);
154*b1cdbd2cSJim Jagielski if (pParent)
155*b1cdbd2cSJim Jagielski {
156*b1cdbd2cSJim Jagielski gtk_widget_realize(mpDialog);
157*b1cdbd2cSJim Jagielski gdk_window_set_transient_for(mpDialog->window, pParent);
158*b1cdbd2cSJim Jagielski }
159*b1cdbd2cSJim Jagielski }
160*b1cdbd2cSJim Jagielski
161*b1cdbd2cSJim Jagielski
~RunDialog()162*b1cdbd2cSJim Jagielski RunDialog::~RunDialog()
163*b1cdbd2cSJim Jagielski {
164*b1cdbd2cSJim Jagielski if (mpCreatedParent)
165*b1cdbd2cSJim Jagielski {
166*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
167*b1cdbd2cSJim Jagielski gdk_window_destroy (mpCreatedParent);
168*b1cdbd2cSJim Jagielski }
169*b1cdbd2cSJim Jagielski }
170*b1cdbd2cSJim Jagielski
windowOpened(const::com::sun::star::lang::EventObject &)171*b1cdbd2cSJim Jagielski void SAL_CALL RunDialog::windowOpened( const ::com::sun::star::lang::EventObject& )
172*b1cdbd2cSJim Jagielski throw (::com::sun::star::uno::RuntimeException)
173*b1cdbd2cSJim Jagielski {
174*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
175*b1cdbd2cSJim Jagielski g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, (GSourceFunc)canceldialog, this, NULL);
176*b1cdbd2cSJim Jagielski }
177*b1cdbd2cSJim Jagielski
cancel()178*b1cdbd2cSJim Jagielski void RunDialog::cancel()
179*b1cdbd2cSJim Jagielski {
180*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
181*b1cdbd2cSJim Jagielski gtk_dialog_response( GTK_DIALOG( mpDialog ), GTK_RESPONSE_CANCEL );
182*b1cdbd2cSJim Jagielski gtk_widget_hide( mpDialog );
183*b1cdbd2cSJim Jagielski }
184*b1cdbd2cSJim Jagielski
run()185*b1cdbd2cSJim Jagielski gint RunDialog::run()
186*b1cdbd2cSJim Jagielski {
187*b1cdbd2cSJim Jagielski if (mxToolkit.is())
188*b1cdbd2cSJim Jagielski mxToolkit->addTopWindowListener(this);
189*b1cdbd2cSJim Jagielski
190*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
191*b1cdbd2cSJim Jagielski gint nStatus = gtk_dialog_run( GTK_DIALOG( mpDialog ) );
192*b1cdbd2cSJim Jagielski
193*b1cdbd2cSJim Jagielski if (mxToolkit.is())
194*b1cdbd2cSJim Jagielski mxToolkit->removeTopWindowListener(this);
195*b1cdbd2cSJim Jagielski
196*b1cdbd2cSJim Jagielski if (nStatus != 1) //PLAY
197*b1cdbd2cSJim Jagielski gtk_widget_hide( mpDialog );
198*b1cdbd2cSJim Jagielski
199*b1cdbd2cSJim Jagielski return nStatus;
200*b1cdbd2cSJim Jagielski }
201*b1cdbd2cSJim Jagielski
lcl_setGTKLanguage(const uno::Reference<lang::XMultiServiceFactory> & xServiceMgr)202*b1cdbd2cSJim Jagielski static void lcl_setGTKLanguage(const uno::Reference<lang::XMultiServiceFactory>& xServiceMgr)
203*b1cdbd2cSJim Jagielski {
204*b1cdbd2cSJim Jagielski static bool bSet = false;
205*b1cdbd2cSJim Jagielski if (bSet)
206*b1cdbd2cSJim Jagielski return;
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski OUString sUILocale;
209*b1cdbd2cSJim Jagielski try
210*b1cdbd2cSJim Jagielski {
211*b1cdbd2cSJim Jagielski uno::Reference<lang::XMultiServiceFactory> xConfigMgr =
212*b1cdbd2cSJim Jagielski uno::Reference<lang::XMultiServiceFactory>(xServiceMgr->createInstance(
213*b1cdbd2cSJim Jagielski OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
214*b1cdbd2cSJim Jagielski UNO_QUERY_THROW );
215*b1cdbd2cSJim Jagielski
216*b1cdbd2cSJim Jagielski Sequence< Any > theArgs(1);
217*b1cdbd2cSJim Jagielski theArgs[ 0 ] <<= OUString::createFromAscii("org.openoffice.Office.Linguistic/General");
218*b1cdbd2cSJim Jagielski
219*b1cdbd2cSJim Jagielski uno::Reference< container::XNameAccess > xNameAccess =
220*b1cdbd2cSJim Jagielski uno::Reference< container::XNameAccess >(xConfigMgr->createInstanceWithArguments(
221*b1cdbd2cSJim Jagielski OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"), theArgs ),
222*b1cdbd2cSJim Jagielski UNO_QUERY_THROW );
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski if (xNameAccess.is())
225*b1cdbd2cSJim Jagielski xNameAccess->getByName(OUString::createFromAscii("UILocale")) >>= sUILocale;
226*b1cdbd2cSJim Jagielski } catch (...) {}
227*b1cdbd2cSJim Jagielski
228*b1cdbd2cSJim Jagielski if (sUILocale.getLength())
229*b1cdbd2cSJim Jagielski {
230*b1cdbd2cSJim Jagielski sUILocale = sUILocale.replace('-', '_');
231*b1cdbd2cSJim Jagielski rtl::OUString envVar(RTL_CONSTASCII_USTRINGPARAM("LANGUAGE"));
232*b1cdbd2cSJim Jagielski osl_setEnvironment(envVar.pData, sUILocale.pData);
233*b1cdbd2cSJim Jagielski }
234*b1cdbd2cSJim Jagielski bSet = true;
235*b1cdbd2cSJim Jagielski }
236*b1cdbd2cSJim Jagielski
SalGtkPicker(const uno::Reference<lang::XMultiServiceFactory> & xServiceMgr)237*b1cdbd2cSJim Jagielski SalGtkPicker::SalGtkPicker(const uno::Reference<lang::XMultiServiceFactory>& xServiceMgr) : m_pDialog(0)
238*b1cdbd2cSJim Jagielski {
239*b1cdbd2cSJim Jagielski lcl_setGTKLanguage(xServiceMgr);
240*b1cdbd2cSJim Jagielski }
241*b1cdbd2cSJim Jagielski
~SalGtkPicker()242*b1cdbd2cSJim Jagielski SalGtkPicker::~SalGtkPicker()
243*b1cdbd2cSJim Jagielski {
244*b1cdbd2cSJim Jagielski if (m_pDialog)
245*b1cdbd2cSJim Jagielski {
246*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
247*b1cdbd2cSJim Jagielski gtk_widget_destroy(m_pDialog);
248*b1cdbd2cSJim Jagielski }
249*b1cdbd2cSJim Jagielski }
250*b1cdbd2cSJim Jagielski
implsetDisplayDirectory(const rtl::OUString & aDirectory)251*b1cdbd2cSJim Jagielski void SAL_CALL SalGtkPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
252*b1cdbd2cSJim Jagielski throw( lang::IllegalArgumentException, uno::RuntimeException )
253*b1cdbd2cSJim Jagielski {
254*b1cdbd2cSJim Jagielski OSL_ASSERT( m_pDialog != NULL );
255*b1cdbd2cSJim Jagielski
256*b1cdbd2cSJim Jagielski OString aTxt = unicodetouri(aDirectory);
257*b1cdbd2cSJim Jagielski
258*b1cdbd2cSJim Jagielski if( aTxt.lastIndexOf('/') == aTxt.getLength() - 1 )
259*b1cdbd2cSJim Jagielski aTxt = aTxt.copy( 0, aTxt.getLength() - 1 );
260*b1cdbd2cSJim Jagielski
261*b1cdbd2cSJim Jagielski OSL_TRACE( "setting path to %s\n", aTxt.getStr() );
262*b1cdbd2cSJim Jagielski
263*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
264*b1cdbd2cSJim Jagielski gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ),
265*b1cdbd2cSJim Jagielski aTxt.getStr() );
266*b1cdbd2cSJim Jagielski }
267*b1cdbd2cSJim Jagielski
implgetDisplayDirectory()268*b1cdbd2cSJim Jagielski rtl::OUString SAL_CALL SalGtkPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
269*b1cdbd2cSJim Jagielski {
270*b1cdbd2cSJim Jagielski OSL_ASSERT( m_pDialog != NULL );
271*b1cdbd2cSJim Jagielski
272*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
273*b1cdbd2cSJim Jagielski
274*b1cdbd2cSJim Jagielski gchar* pCurrentFolder =
275*b1cdbd2cSJim Jagielski gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) );
276*b1cdbd2cSJim Jagielski ::rtl::OUString aCurrentFolderName = uritounicode(pCurrentFolder);
277*b1cdbd2cSJim Jagielski g_free( pCurrentFolder );
278*b1cdbd2cSJim Jagielski
279*b1cdbd2cSJim Jagielski return aCurrentFolderName;
280*b1cdbd2cSJim Jagielski }
281*b1cdbd2cSJim Jagielski
implsetTitle(const rtl::OUString & aTitle)282*b1cdbd2cSJim Jagielski void SAL_CALL SalGtkPicker::implsetTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
283*b1cdbd2cSJim Jagielski {
284*b1cdbd2cSJim Jagielski OSL_ASSERT( m_pDialog != NULL );
285*b1cdbd2cSJim Jagielski
286*b1cdbd2cSJim Jagielski ::rtl::OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 );
287*b1cdbd2cSJim Jagielski
288*b1cdbd2cSJim Jagielski GdkThreadLock aLock;
289*b1cdbd2cSJim Jagielski gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() );
290*b1cdbd2cSJim Jagielski }
291