1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_fpicker.hxx"
30 
31 //------------------------------------------------------------------------
32 // includes
33 //------------------------------------------------------------------------
34 #include <com/sun/star/lang/DisposedException.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
37 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
39 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
40 #include <cppuhelper/interfacecontainer.h>
41 #include <osl/diagnose.h>
42 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
43 #include <com/sun/star/uno/Any.hxx>
44 #include <FPServiceInfo.hxx>
45 #include <vos/mutex.hxx>
46 #include <vcl/svapp.hxx>
47 #include "SalGtkFolderPicker.hxx"
48 
49 #include <tools/urlobj.hxx>
50 
51 #include <iostream>
52 #include "resourceprovider.hxx"
53 #ifndef _SV_RC_H
54 #include <tools/rc.hxx>
55 #endif
56 
57 //------------------------------------------------------------------------
58 // namespace directives
59 //------------------------------------------------------------------------
60 
61 using namespace ::rtl;
62 using namespace ::com::sun::star;
63 using namespace ::com::sun::star::ui::dialogs;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::uno;
66 
67 //------------------------------------------------------------------------
68 // helper functions
69 //------------------------------------------------------------------------
70 
71 namespace
72 {
73 	// controling event notifications
74 	uno::Sequence<rtl::OUString> SAL_CALL FolderPicker_getSupportedServiceNames()
75 	{
76         uno::Sequence<rtl::OUString> aRet(2);
77         aRet[0] = rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.SystemFolderPicker" );
78         aRet[1] = rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.GtkFolderPicker" );
79 		return aRet;
80 	}
81 }
82 
83 //-----------------------------------------------------------------------------------------
84 // constructor
85 //-----------------------------------------------------------------------------------------
86 SalGtkFolderPicker::SalGtkFolderPicker( const uno::Reference<lang::XMultiServiceFactory>& xServiceMgr ) :
87 	SalGtkPicker(xServiceMgr),
88 	m_xServiceMgr(xServiceMgr)
89 {
90     CResourceProvider aResProvider;
91 
92     GdkThreadLock aLock;
93 
94     m_pDialog = gtk_file_chooser_dialog_new(
95     	OUStringToOString( aResProvider.getResString( FOLDERPICKER_TITLE ), RTL_TEXTENCODING_UTF8 ).getStr(),
96 		NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
97 		GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, (char *)NULL );
98 
99     gtk_dialog_set_default_response( GTK_DIALOG (m_pDialog), GTK_RESPONSE_ACCEPT );
100     gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( m_pDialog ), sal_False );
101     gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( m_pDialog ), sal_False );
102 }
103 
104 // -------------------------------------------------
105 // XEventListener
106 // -------------------------------------------------
107 
108 void SAL_CALL SalGtkFolderPicker::disposing( const lang::EventObject& )
109 	throw( uno::RuntimeException )
110 {
111 }
112 
113 void SAL_CALL SalGtkFolderPicker::setDisplayDirectory( const rtl::OUString& aDirectory )
114 	throw( lang::IllegalArgumentException, uno::RuntimeException )
115 {
116     OSL_ASSERT( m_pDialog != NULL );
117 
118     OString aTxt = unicodetouri( aDirectory );
119 
120     if( aTxt.lastIndexOf('/') == aTxt.getLength() - 1 )
121         aTxt = aTxt.copy( 0, aTxt.getLength() - 1 );
122 
123     OSL_TRACE( "setting path to %s\n", aTxt.getStr() );
124 
125     GdkThreadLock aLock;
126 
127     gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ),
128         aTxt.getStr() );
129 }
130 
131 rtl::OUString SAL_CALL SalGtkFolderPicker::getDisplayDirectory() throw( uno::RuntimeException )
132 {
133     OSL_ASSERT( m_pDialog != NULL );
134 
135     GdkThreadLock aLock;
136 
137     gchar* pCurrentFolder =
138         gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( m_pDialog ) );
139     ::rtl::OUString aCurrentFolderName = uritounicode(pCurrentFolder);
140     g_free( pCurrentFolder );
141 
142     return aCurrentFolderName;
143 }
144 
145 rtl::OUString SAL_CALL SalGtkFolderPicker::getDirectory() throw( uno::RuntimeException )
146 {
147 	return getDisplayDirectory();
148 }
149 
150 void SAL_CALL SalGtkFolderPicker::setDescription( const rtl::OUString& rDescription )
151 	throw( uno::RuntimeException )
152 {
153 	::rtl::OString aDescription = OUStringToOString( rDescription, RTL_TEXTENCODING_UTF8 );
154 }
155 
156 
157 
158 //-----------------------------------------------------------------------------------------
159 // XExecutableDialog functions
160 //-----------------------------------------------------------------------------------------
161 
162 void SAL_CALL SalGtkFolderPicker::setTitle( const rtl::OUString& aTitle ) throw( uno::RuntimeException )
163 {
164     OSL_ASSERT( m_pDialog != NULL );
165 
166     ::rtl::OString aWindowTitle = OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 );
167 
168     GdkThreadLock aLock;
169     gtk_window_set_title( GTK_WINDOW( m_pDialog ), aWindowTitle.getStr() );
170 }
171 
172 sal_Int16 SAL_CALL SalGtkFolderPicker::execute() throw( uno::RuntimeException )
173 {
174 	OSL_TRACE( "1: HERE WE ARE\n");
175 	OSL_ASSERT( m_pDialog != NULL );
176 
177 	sal_Int16 retVal = 0;
178 
179     uno::Reference< awt::XExtendedToolkit > xToolkit(
180         m_xServiceMgr->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.awt.Toolkit") ), uno::UNO_QUERY);
181 
182     RunDialog* pRunDialog = new RunDialog(m_pDialog, xToolkit);
183     uno::Reference < awt::XTopWindowListener > xLifeCycle(pRunDialog);
184     gint nStatus = pRunDialog->run();
185 	switch( nStatus )
186 	{
187 		case GTK_RESPONSE_ACCEPT:
188 			retVal = ExecutableDialogResults::OK;
189 			break;
190 		case GTK_RESPONSE_CANCEL:
191 			retVal = ExecutableDialogResults::CANCEL;
192 			break;
193 		default:
194 			retVal = 0;
195 			break;
196 	}
197 
198 	return retVal;
199 }
200 
201 //------------------------------------------------------------------------------------
202 // XCancellable
203 //------------------------------------------------------------------------------------
204 
205 void SAL_CALL SalGtkFolderPicker::cancel() throw( uno::RuntimeException )
206 {
207 	OSL_ASSERT( m_pDialog != NULL );
208 
209 	// TODO m_pImpl->cancel();
210 }
211 
212 // -------------------------------------------------
213 // XServiceInfo
214 // -------------------------------------------------
215 
216 rtl::OUString SAL_CALL SalGtkFolderPicker::getImplementationName()
217 	throw( uno::RuntimeException )
218 {
219 	return rtl::OUString::createFromAscii( FOLDER_PICKER_IMPL_NAME );
220 }
221 
222 // -------------------------------------------------
223 //	XServiceInfo
224 // -------------------------------------------------
225 
226 sal_Bool SAL_CALL SalGtkFolderPicker::supportsService( const rtl::OUString& ServiceName )
227 	throw( uno::RuntimeException )
228 {
229 	uno::Sequence <rtl::OUString> SupportedServicesNames = FolderPicker_getSupportedServiceNames();
230 
231 	for( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
232 		if( SupportedServicesNames[n].compareTo( ServiceName ) == 0)
233 			return sal_True;
234 
235 	return sal_False;
236 }
237 
238 // -------------------------------------------------
239 //	XServiceInfo
240 // -------------------------------------------------
241 
242 uno::Sequence<rtl::OUString> SAL_CALL SalGtkFolderPicker::getSupportedServiceNames()
243 	throw( uno::RuntimeException )
244 {
245 	return FolderPicker_getSupportedServiceNames();
246 }
247