Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifdef AIX
30 : : #define _LINUX_SOURCE_COMPAT
31 : : #include <sys/timer.h>
32 : : #undef _LINUX_SOURCE_COMPAT
33 : : #endif
34 : :
35 : : #include <com/sun/star/lang/DisposedException.hpp>
36 : : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
37 : : #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
38 : : #include <com/sun/star/lang/SystemDependent.hpp>
39 : : #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
40 : : #include <com/sun/star/awt/SystemDependentXWindow.hpp>
41 : : #include <com/sun/star/beans/NamedValue.hpp>
42 : : #include <com/sun/star/container/XNameAccess.hpp>
43 : : #include <comphelper/processfactory.hxx>
44 : : #include <cppuhelper/interfacecontainer.h>
45 : : #include <rtl/process.h>
46 : : #include <osl/diagnose.h>
47 : : #include <com/sun/star/uno/Any.hxx>
48 : : #include <osl/mutex.hxx>
49 : : #include <vcl/svapp.hxx>
50 : : #include <tools/urlobj.hxx>
51 : : #include <stdio.h>
52 : :
53 : : #include "vcl/window.hxx"
54 : : #include "unx/gtk/gtkframe.hxx"
55 : : #include "gtk/fpicker/SalGtkPicker.hxx"
56 : :
57 : : using namespace ::rtl;
58 : : using namespace ::com::sun::star;
59 : : using namespace ::com::sun::star::lang;
60 : : using namespace ::com::sun::star::uno;
61 : :
62 : 0 : rtl::OUString SalGtkPicker::uritounicode(const gchar* pIn)
63 : : {
64 : 0 : if (!pIn)
65 : 0 : return rtl::OUString();
66 : :
67 : 0 : rtl::OUString sURL( const_cast<const sal_Char *>(pIn), strlen(pIn),
68 : 0 : RTL_TEXTENCODING_UTF8 );
69 : :
70 : 0 : INetURLObject aURL(sURL);
71 : 0 : if (INET_PROT_FILE == aURL.GetProtocol())
72 : : {
73 : : // all the URLs are handled by office in UTF-8
74 : : // so the Gnome FP related URLs should be converted accordingly
75 : 0 : gchar *pEncodedFileName = g_filename_from_uri(pIn, NULL, NULL);
76 : 0 : if ( pEncodedFileName )
77 : : {
78 : 0 : rtl::OUString sEncoded(pEncodedFileName, strlen(pEncodedFileName),
79 : 0 : osl_getThreadTextEncoding());
80 : 0 : INetURLObject aCurrentURL(sEncoded, INetURLObject::FSYS_UNX);
81 : 0 : aCurrentURL.SetHost(aURL.GetHost());
82 : 0 : sURL = aCurrentURL.getExternalURL();
83 : : }
84 : : else
85 : : {
86 : 0 : OUString aNewURL = uno::Reference<uri::XExternalUriReferenceTranslator>(uno::Reference<XMultiComponentFactory>(comphelper::getProcessServiceFactory(), UNO_QUERY_THROW)->createInstanceWithContext(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uri.ExternalUriReferenceTranslator")), m_xContext), UNO_QUERY_THROW)->translateToInternal(sURL);
87 : 0 : if( !aNewURL.isEmpty() )
88 : 0 : sURL = aNewURL;
89 : : }
90 : : }
91 : 0 : return sURL;
92 : : }
93 : :
94 : 0 : rtl::OString SalGtkPicker::unicodetouri(const rtl::OUString &rURL)
95 : : {
96 : : // all the URLs are handled by office in UTF-8 ( and encoded with "%xx" codes based on UTF-8 )
97 : : // so the Gnome FP related URLs should be converted accordingly
98 : 0 : OString sURL = OUStringToOString(rURL, RTL_TEXTENCODING_UTF8);
99 : 0 : INetURLObject aURL(rURL);
100 : 0 : if (INET_PROT_FILE == aURL.GetProtocol())
101 : : {
102 : 0 : OUString aNewURL = uno::Reference<uri::XExternalUriReferenceTranslator>(uno::Reference<XMultiComponentFactory>(comphelper::getProcessServiceFactory(), UNO_QUERY_THROW)->createInstanceWithContext(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uri.ExternalUriReferenceTranslator")), m_xContext ), UNO_QUERY_THROW)->translateToExternal( rURL );
103 : :
104 : 0 : if( !aNewURL.isEmpty() )
105 : : {
106 : : // At this point the URL should contain ascii characters only actually
107 : 0 : sURL = OUStringToOString( aNewURL, osl_getThreadTextEncoding() );
108 : 0 : }
109 : : }
110 : 0 : return sURL;
111 : : }
112 : :
113 : : extern "C"
114 : : {
115 : 0 : static gboolean canceldialog(RunDialog *pDialog)
116 : : {
117 : 0 : GdkThreadLock lock;
118 : 0 : pDialog->cancel();
119 : 0 : return false;
120 : : }
121 : : }
122 : :
123 : 0 : RunDialog::RunDialog( GtkWidget *pDialog, uno::Reference< awt::XExtendedToolkit >& rToolkit,
124 : : uno::Reference< frame::XDesktop >& rDesktop ) :
125 : : cppu::WeakComponentImplHelper2< awt::XTopWindowListener, frame::XTerminateListener >( maLock ),
126 : 0 : mpDialog(pDialog), mxToolkit(rToolkit), mxDesktop(rDesktop)
127 : : {
128 : 0 : GtkWindow *pParent = NULL;
129 : :
130 : 0 : ::Window * pWindow = ::Application::GetActiveTopWindow();
131 : 0 : if( pWindow )
132 : : {
133 : 0 : GtkSalFrame *pFrame = dynamic_cast<GtkSalFrame *>( pWindow->ImplGetFrame() );
134 : 0 : if( pFrame )
135 : 0 : pParent = GTK_WINDOW( pFrame->getWindow() );
136 : : }
137 : 0 : if (pParent)
138 : 0 : gtk_window_set_transient_for( GTK_WINDOW( mpDialog ), pParent );
139 : 0 : }
140 : :
141 : 0 : RunDialog::~RunDialog()
142 : : {
143 : 0 : SolarMutexGuard g;
144 : :
145 : 0 : g_source_remove_by_user_data (this);
146 : 0 : }
147 : :
148 : 0 : void SAL_CALL RunDialog::windowOpened( const ::com::sun::star::lang::EventObject& )
149 : : throw (::com::sun::star::uno::RuntimeException)
150 : : {
151 : 0 : SolarMutexGuard g;
152 : :
153 : 0 : g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, (GSourceFunc)canceldialog, this, NULL);
154 : 0 : }
155 : :
156 : 0 : void SAL_CALL RunDialog::queryTermination( const ::com::sun::star::lang::EventObject& )
157 : : throw(::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException)
158 : : {
159 : 0 : }
160 : :
161 : 0 : void SAL_CALL RunDialog::notifyTermination( const ::com::sun::star::lang::EventObject& )
162 : : throw(::com::sun::star::uno::RuntimeException)
163 : : {
164 : 0 : SolarMutexGuard g;
165 : :
166 : 0 : g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, (GSourceFunc)canceldialog, this, NULL);
167 : 0 : }
168 : :
169 : 0 : void RunDialog::cancel()
170 : : {
171 : 0 : gtk_dialog_response( GTK_DIALOG( mpDialog ), GTK_RESPONSE_CANCEL );
172 : 0 : gtk_widget_hide( mpDialog );
173 : 0 : }
174 : :
175 : 0 : gint RunDialog::run()
176 : : {
177 : 0 : if (mxToolkit.is())
178 : 0 : mxToolkit->addTopWindowListener(this);
179 : :
180 : 0 : gint nStatus = gtk_dialog_run( GTK_DIALOG( mpDialog ) );
181 : :
182 : 0 : if (mxToolkit.is())
183 : 0 : mxToolkit->removeTopWindowListener(this);
184 : :
185 : 0 : if (nStatus != 1) //PLAY
186 : 0 : gtk_widget_hide( mpDialog );
187 : :
188 : 0 : return nStatus;
189 : : }
190 : :
191 : : // FIXME: this is pretty nasty ... we try to tell gtk+'s
192 : : // gettext the locale it should use via the environment
193 : 0 : void SalGtkPicker::setGtkLanguage()
194 : : {
195 : : static bool bSet = false;
196 : 0 : if (bSet)
197 : 0 : return;
198 : :
199 : 0 : ::com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
200 : 0 : rtl::OUStringBuffer aBuffer;
201 : 0 : aBuffer.append( aLocale.Language );
202 : 0 : aBuffer.appendAscii( "_" );
203 : 0 : aBuffer.append( aLocale.Country );
204 : 0 : aBuffer.appendAscii( ".UTF-8" );
205 : :
206 : 0 : if (aBuffer.getLength() > 8)
207 : : {
208 : 0 : rtl::OUString envVar( RTL_CONSTASCII_USTRINGPARAM( "LANGUAGE" ) );
209 : 0 : osl_setEnvironment( envVar.pData, aBuffer.makeStringAndClear().pData );
210 : : }
211 : 0 : bSet = true;
212 : : }
213 : :
214 : 0 : SalGtkPicker::SalGtkPicker( const uno::Reference<uno::XComponentContext>& xContext )
215 : 0 : : m_pDialog( 0 ), m_xContext( xContext )
216 : : {
217 : 0 : setGtkLanguage();
218 : 0 : }
219 : :
220 : 0 : SalGtkPicker::~SalGtkPicker()
221 : : {
222 : 0 : SolarMutexGuard g;
223 : :
224 : 0 : if (m_pDialog)
225 : : {
226 : 0 : gtk_widget_destroy(m_pDialog);
227 : 0 : }
228 : 0 : }
229 : :
230 : 0 : void SAL_CALL SalGtkPicker::implsetDisplayDirectory( const rtl::OUString& aDirectory )
231 : : throw( lang::IllegalArgumentException, uno::RuntimeException )
232 : : {
233 : : OSL_ASSERT( m_pDialog != NULL );
234 : :
235 : 0 : OString aTxt = unicodetouri(aDirectory);
236 : :
237 : 0 : if( aTxt.lastIndexOf('/') == aTxt.getLength() - 1 )
238 : 0 : aTxt = aTxt.copy( 0, aTxt.getLength() - 1 );
239 : :
240 : : OSL_TRACE( "setting path to %s", aTxt.getStr() );
241 : :
242 : 0 : gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ),
243 : 0 : aTxt.getStr() );
244 : 0 : }
245 : :
246 : 0 : rtl::OUString SAL_CALL SalGtkPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
247 : : {
248 : : OSL_ASSERT( m_pDialog != NULL );
249 : :
250 : : gchar* pCurrentFolder =
251 : 0 : gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) );
252 : 0 : ::rtl::OUString aCurrentFolderName = uritounicode(pCurrentFolder);
253 : 0 : g_free( pCurrentFolder );
254 : :
255 : 0 : return aCurrentFolderName;
256 : : }
257 : :
258 : 0 : void SAL_CALL SalGtkPicker::implsetTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
259 : : {
260 : : OSL_ASSERT( m_pDialog != NULL );
261 : :
262 : 0 : ::rtl::OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 );
263 : :
264 : 0 : gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() );
265 : 0 : }
266 : :
267 : 0 : uno::Reference< uno::XInterface > SalGtkPicker::createInstance( const rtl::OUString &rName )
268 : : {
269 : 0 : return m_xContext->getServiceManager()->createInstanceWithContext( rName, m_xContext );
270 : : }
271 : :
272 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|