1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #define _LINUX_SOURCE_COMPAT
22 #include <sys/timer.h>
23 #undef _LINUX_SOURCE_COMPAT
26 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
27 #include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp>
28 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <rtl/process.h>
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include <tools/urlobj.hxx>
38 #include "vcl/window.hxx"
39 #include "unx/gtk/gtkframe.hxx"
40 #include "gtk/fpicker/SalGtkPicker.hxx"
42 using namespace ::rtl
;
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::uno
;
47 OUString
SalGtkPicker::uritounicode(const gchar
* pIn
)
52 OUString
sURL( const_cast<const sal_Char
*>(pIn
), strlen(pIn
),
53 RTL_TEXTENCODING_UTF8
);
55 INetURLObject
aURL(sURL
);
56 if (INetProtocol::File
== aURL
.GetProtocol())
58 // all the URLs are handled by office in UTF-8
59 // so the Gnome FP related URLs should be converted accordingly
60 gchar
*pEncodedFileName
= g_filename_from_uri(pIn
, NULL
, NULL
);
61 if ( pEncodedFileName
)
63 OUString
sEncoded(pEncodedFileName
, strlen(pEncodedFileName
),
64 osl_getThreadTextEncoding());
65 g_free (pEncodedFileName
);
66 INetURLObject
aCurrentURL(sEncoded
, INetURLObject::FSYS_UNX
);
67 aCurrentURL
.SetHost(aURL
.GetHost());
68 sURL
= aCurrentURL
.getExternalURL();
72 OUString aNewURL
= uri::ExternalUriReferenceTranslator::create( m_xContext
)->translateToInternal(sURL
);
73 if( !aNewURL
.isEmpty() )
80 OString
SalGtkPicker::unicodetouri(const OUString
&rURL
)
82 // all the URLs are handled by office in UTF-8 ( and encoded with "%xx" codes based on UTF-8 )
83 // so the Gnome FP related URLs should be converted accordingly
84 OString sURL
= OUStringToOString(rURL
, RTL_TEXTENCODING_UTF8
);
85 INetURLObject
aURL(rURL
);
86 if (INetProtocol::File
== aURL
.GetProtocol())
88 OUString aNewURL
= uri::ExternalUriReferenceTranslator::create( m_xContext
)->translateToInternal(rURL
);
90 if( !aNewURL
.isEmpty() )
92 // At this point the URL should contain ascii characters only actually
93 sURL
= OUStringToOString( aNewURL
, osl_getThreadTextEncoding() );
101 static gboolean
canceldialog(RunDialog
*pDialog
)
109 GtkWindow
* RunDialog::GetTransientFor()
111 GtkWindow
*pParent
= NULL
;
113 vcl::Window
* pWindow
= ::Application::GetActiveTopWindow();
116 GtkSalFrame
*pFrame
= dynamic_cast<GtkSalFrame
*>( pWindow
->ImplGetFrame() );
118 pParent
= GTK_WINDOW( pFrame
->getWindow() );
124 RunDialog::RunDialog( GtkWidget
*pDialog
, uno::Reference
< awt::XExtendedToolkit
>& rToolkit
,
125 uno::Reference
< frame::XDesktop
>& rDesktop
) :
126 cppu::WeakComponentImplHelper2
< awt::XTopWindowListener
, frame::XTerminateListener
>( maLock
),
127 mpDialog(pDialog
), mxToolkit(rToolkit
), mxDesktop(rDesktop
)
131 RunDialog::~RunDialog()
135 g_source_remove_by_user_data (this);
138 void SAL_CALL
RunDialog::windowOpened(const css::lang::EventObject
& e
)
139 throw (css::uno::RuntimeException
, std::exception
)
143 //Don't popdown dialogs if a tooltip appears elsewhere, that's ok, but do pop down
144 //if another dialog/frame is launched.
145 css::uno::Reference
<css::accessibility::XAccessible
> xAccessible(e
.Source
, css::uno::UNO_QUERY
);
146 if (xAccessible
.is())
148 css::uno::Reference
<css::accessibility::XAccessibleContext
> xContext(xAccessible
->getAccessibleContext());
149 if (xContext
.is() && xContext
->getAccessibleRole() == css::accessibility::AccessibleRole::TOOL_TIP
)
155 g_timeout_add_full(G_PRIORITY_HIGH_IDLE
, 0, reinterpret_cast<GSourceFunc
>(canceldialog
), this, NULL
);
158 void SAL_CALL
RunDialog::queryTermination( const ::com::sun::star::lang::EventObject
& )
159 throw(::com::sun::star::frame::TerminationVetoException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
163 void SAL_CALL
RunDialog::notifyTermination( const ::com::sun::star::lang::EventObject
& )
164 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
168 g_timeout_add_full(G_PRIORITY_HIGH_IDLE
, 0, reinterpret_cast<GSourceFunc
>(canceldialog
), this, NULL
);
171 void RunDialog::cancel()
173 gtk_dialog_response( GTK_DIALOG( mpDialog
), GTK_RESPONSE_CANCEL
);
174 gtk_widget_hide( mpDialog
);
177 gint
RunDialog::run()
180 mxToolkit
->addTopWindowListener(this);
182 gint nStatus
= gtk_dialog_run( GTK_DIALOG( mpDialog
) );
185 mxToolkit
->removeTopWindowListener(this);
187 if (nStatus
!= 1) //PLAY
188 gtk_widget_hide( mpDialog
);
193 SalGtkPicker::SalGtkPicker( const uno::Reference
<uno::XComponentContext
>& xContext
)
194 : m_pDialog( 0 ), m_xContext( xContext
)
198 SalGtkPicker::~SalGtkPicker()
204 gtk_widget_destroy(m_pDialog
);
208 void SAL_CALL
SalGtkPicker::implsetDisplayDirectory( const OUString
& aDirectory
)
209 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
211 OSL_ASSERT( m_pDialog
!= NULL
);
213 OString aTxt
= unicodetouri(aDirectory
);
214 if( aTxt
.isEmpty() ){
215 aTxt
= unicodetouri(OUString("file:///."));
218 if( aTxt
.endsWith("/") )
219 aTxt
= aTxt
.copy( 0, aTxt
.getLength() - 1 );
221 OSL_TRACE( "setting path to %s", aTxt
.getStr() );
223 gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog
),
227 OUString SAL_CALL
SalGtkPicker::implgetDisplayDirectory() throw( uno::RuntimeException
)
229 OSL_ASSERT( m_pDialog
!= NULL
);
231 gchar
* pCurrentFolder
=
232 gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog
) );
233 OUString aCurrentFolderName
= uritounicode(pCurrentFolder
);
234 g_free( pCurrentFolder
);
236 return aCurrentFolderName
;
239 void SAL_CALL
SalGtkPicker::implsetTitle( const OUString
& aTitle
) throw( uno::RuntimeException
)
241 OSL_ASSERT( m_pDialog
!= NULL
);
243 OString aWindowTitle
= OUStringToOString( aTitle
, RTL_TEXTENCODING_UTF8
);
245 gtk_window_set_title( GTK_WINDOW( m_pDialog
), aWindowTitle
.getStr() );
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */