Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifdef AIX
21 : #define _LINUX_SOURCE_COMPAT
22 : #include <sys/timer.h>
23 : #undef _LINUX_SOURCE_COMPAT
24 : #endif
25 :
26 : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
27 : #include <com/sun/star/uri/ExternalUriReferenceTranslator.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <rtl/process.h>
30 : #include <osl/diagnose.h>
31 : #include <osl/mutex.hxx>
32 : #include <vcl/svapp.hxx>
33 : #include <tools/urlobj.hxx>
34 : #include <stdio.h>
35 :
36 : #include "vcl/window.hxx"
37 : #include "unx/gtk/gtkframe.hxx"
38 : #include "gtk/fpicker/SalGtkPicker.hxx"
39 :
40 : using namespace ::rtl;
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::uno;
44 :
45 0 : OUString SalGtkPicker::uritounicode(const gchar* pIn)
46 : {
47 0 : if (!pIn)
48 0 : return OUString();
49 :
50 0 : OUString sURL( const_cast<const sal_Char *>(pIn), strlen(pIn),
51 0 : RTL_TEXTENCODING_UTF8 );
52 :
53 0 : INetURLObject aURL(sURL);
54 0 : if (INET_PROT_FILE == aURL.GetProtocol())
55 : {
56 : // all the URLs are handled by office in UTF-8
57 : // so the Gnome FP related URLs should be converted accordingly
58 0 : gchar *pEncodedFileName = g_filename_from_uri(pIn, NULL, NULL);
59 0 : if ( pEncodedFileName )
60 : {
61 0 : OUString sEncoded(pEncodedFileName, strlen(pEncodedFileName),
62 0 : osl_getThreadTextEncoding());
63 0 : g_free (pEncodedFileName);
64 0 : INetURLObject aCurrentURL(sEncoded, INetURLObject::FSYS_UNX);
65 0 : aCurrentURL.SetHost(aURL.GetHost());
66 0 : sURL = aCurrentURL.getExternalURL();
67 : }
68 : else
69 : {
70 0 : OUString aNewURL = uri::ExternalUriReferenceTranslator::create( m_xContext )->translateToInternal(sURL);
71 0 : if( !aNewURL.isEmpty() )
72 0 : sURL = aNewURL;
73 : }
74 : }
75 0 : return sURL;
76 : }
77 :
78 0 : OString SalGtkPicker::unicodetouri(const OUString &rURL)
79 : {
80 : // all the URLs are handled by office in UTF-8 ( and encoded with "%xx" codes based on UTF-8 )
81 : // so the Gnome FP related URLs should be converted accordingly
82 0 : OString sURL = OUStringToOString(rURL, RTL_TEXTENCODING_UTF8);
83 0 : INetURLObject aURL(rURL);
84 0 : if (INET_PROT_FILE == aURL.GetProtocol())
85 : {
86 0 : OUString aNewURL = uri::ExternalUriReferenceTranslator::create( m_xContext )->translateToInternal(rURL);
87 :
88 0 : if( !aNewURL.isEmpty() )
89 : {
90 : // At this point the URL should contain ascii characters only actually
91 0 : sURL = OUStringToOString( aNewURL, osl_getThreadTextEncoding() );
92 0 : }
93 : }
94 0 : return sURL;
95 : }
96 :
97 : extern "C"
98 : {
99 0 : static gboolean canceldialog(RunDialog *pDialog)
100 : {
101 0 : SolarMutexGuard g;
102 0 : pDialog->cancel();
103 0 : return false;
104 : }
105 : }
106 :
107 0 : RunDialog::RunDialog( GtkWidget *pDialog, uno::Reference< awt::XExtendedToolkit >& rToolkit,
108 : uno::Reference< frame::XDesktop >& rDesktop ) :
109 : cppu::WeakComponentImplHelper2< awt::XTopWindowListener, frame::XTerminateListener >( maLock ),
110 0 : mpDialog(pDialog), mxToolkit(rToolkit), mxDesktop(rDesktop)
111 : {
112 0 : GtkWindow *pParent = NULL;
113 :
114 0 : vcl::Window * pWindow = ::Application::GetActiveTopWindow();
115 0 : if( pWindow )
116 : {
117 0 : GtkSalFrame *pFrame = dynamic_cast<GtkSalFrame *>( pWindow->ImplGetFrame() );
118 0 : if( pFrame )
119 0 : pParent = GTK_WINDOW( pFrame->getWindow() );
120 : }
121 0 : if (pParent)
122 0 : gtk_window_set_transient_for( GTK_WINDOW( mpDialog ), pParent );
123 0 : }
124 :
125 0 : RunDialog::~RunDialog()
126 : {
127 0 : SolarMutexGuard g;
128 :
129 0 : g_source_remove_by_user_data (this);
130 0 : }
131 :
132 0 : void SAL_CALL RunDialog::windowOpened( const ::com::sun::star::lang::EventObject& )
133 : throw (::com::sun::star::uno::RuntimeException, std::exception)
134 : {
135 0 : SolarMutexGuard g;
136 :
137 0 : g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, (GSourceFunc)canceldialog, this, NULL);
138 0 : }
139 :
140 0 : void SAL_CALL RunDialog::queryTermination( const ::com::sun::star::lang::EventObject& )
141 : throw(::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException, std::exception)
142 : {
143 0 : }
144 :
145 0 : void SAL_CALL RunDialog::notifyTermination( const ::com::sun::star::lang::EventObject& )
146 : throw(::com::sun::star::uno::RuntimeException, std::exception)
147 : {
148 0 : SolarMutexGuard g;
149 :
150 0 : g_timeout_add_full(G_PRIORITY_HIGH_IDLE, 0, (GSourceFunc)canceldialog, this, NULL);
151 0 : }
152 :
153 0 : void RunDialog::cancel()
154 : {
155 0 : gtk_dialog_response( GTK_DIALOG( mpDialog ), GTK_RESPONSE_CANCEL );
156 0 : gtk_widget_hide( mpDialog );
157 0 : }
158 :
159 0 : gint RunDialog::run()
160 : {
161 0 : if (mxToolkit.is())
162 0 : mxToolkit->addTopWindowListener(this);
163 :
164 0 : gint nStatus = gtk_dialog_run( GTK_DIALOG( mpDialog ) );
165 :
166 0 : if (mxToolkit.is())
167 0 : mxToolkit->removeTopWindowListener(this);
168 :
169 0 : if (nStatus != 1) //PLAY
170 0 : gtk_widget_hide( mpDialog );
171 :
172 0 : return nStatus;
173 : }
174 :
175 0 : SalGtkPicker::SalGtkPicker( const uno::Reference<uno::XComponentContext>& xContext )
176 0 : : m_pDialog( 0 ), m_xContext( xContext )
177 : {
178 0 : }
179 :
180 0 : SalGtkPicker::~SalGtkPicker()
181 : {
182 0 : SolarMutexGuard g;
183 :
184 0 : if (m_pDialog)
185 : {
186 0 : gtk_widget_destroy(m_pDialog);
187 0 : }
188 0 : }
189 :
190 0 : void SAL_CALL SalGtkPicker::implsetDisplayDirectory( const OUString& aDirectory )
191 : throw( lang::IllegalArgumentException, uno::RuntimeException )
192 : {
193 : OSL_ASSERT( m_pDialog != NULL );
194 :
195 0 : OString aTxt = unicodetouri(aDirectory);
196 0 : if( aTxt.isEmpty() ){
197 0 : aTxt = unicodetouri(OUString("file:///."));
198 : }
199 :
200 0 : if( aTxt.endsWith("/") )
201 0 : aTxt = aTxt.copy( 0, aTxt.getLength() - 1 );
202 :
203 : OSL_TRACE( "setting path to %s", aTxt.getStr() );
204 :
205 0 : gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ),
206 0 : aTxt.getStr() );
207 0 : }
208 :
209 0 : OUString SAL_CALL SalGtkPicker::implgetDisplayDirectory() throw( uno::RuntimeException )
210 : {
211 : OSL_ASSERT( m_pDialog != NULL );
212 :
213 : gchar* pCurrentFolder =
214 0 : gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) );
215 0 : OUString aCurrentFolderName = uritounicode(pCurrentFolder);
216 0 : g_free( pCurrentFolder );
217 :
218 0 : return aCurrentFolderName;
219 : }
220 :
221 0 : void SAL_CALL SalGtkPicker::implsetTitle( const OUString& aTitle ) throw( uno::RuntimeException )
222 : {
223 : OSL_ASSERT( m_pDialog != NULL );
224 :
225 0 : OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 );
226 :
227 0 : gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() );
228 0 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|