1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 //////////////////////////////////////////////////////////////////////////
25 // includes
26 //////////////////////////////////////////////////////////////////////////
27 
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <cppuhelper/interfacecontainer.h>
31 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
33 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
34 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
35 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
36 
37 #include <svtools/svtools.hrc>
38 
39 #include <vos/mutex.hxx>
40 
41 #include <vcl/svapp.hxx>
42 #include <vcl/sysdata.hxx>
43 #include <vcl/syswin.hxx>
44 
45 #include "osl/file.h"
46 
47 #include "KDE4FilePicker.hxx"
48 #include "FPServiceInfo.hxx"
49 
50 /* ********* Hack, but needed because of conflicting types... */
51 #define Region QtXRegion
52 
53 //kde has an enum that uses this...OO does too
54 #undef SETTINGS_MOUSE
55 
56 #include <kfiledialog.h>
57 #include <kwindowsystem.h>
58 #include <kapplication.h>
59 #include <kfilefiltercombo.h>
60 
61 #include <QWidget>
62 #include <QCheckBox>
63 #include <QGridLayout>
64 
65 #undef Region
66 
67 using namespace ::com::sun::star;
68 
69 using namespace ::com::sun::star::ui::dialogs;
70 using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
71 
72 using namespace ::com::sun::star;
73 using namespace ::com::sun::star::ui::dialogs;
74 using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
75 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
76 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
77 using namespace ::com::sun::star::lang;
78 using namespace ::com::sun::star::beans;
79 using namespace ::com::sun::star::uno;
80 
81 //////////////////////////////////////////////////////////////////////////
82 // helper functions
83 //////////////////////////////////////////////////////////////////////////
84 
85 #include <QDebug>
86 
87 namespace
88 {
89     // controling event notifications
90     const bool STARTUP_SUSPENDED = true;
91     const bool STARTUP_ALIVE     = false;
92 
FilePicker_getSupportedServiceNames()93     uno::Sequence<rtl::OUString> SAL_CALL FilePicker_getSupportedServiceNames()
94     {
95         uno::Sequence<rtl::OUString> aRet(3);
96         aRet[0] = rtl::OUString::createFromAscii("com.sun.star.ui.dialogs.FilePicker");
97         aRet[1] = rtl::OUString::createFromAscii("com.sun.star.ui.dialogs.SystemFilePicker");
98         aRet[2] = rtl::OUString::createFromAscii("com.sun.star.ui.dialogs.KDE4FilePicker");
99         return aRet;
100     }
101 }
102 
toOUString(const QString & s)103 rtl::OUString toOUString(const QString& s)
104 {
105     // QString stores UTF16, just like OUString
106 	return rtl::OUString(reinterpret_cast<const sal_Unicode*>(s.data()), s.length());
107 }
108 
toQString(const rtl::OUString & s)109 QString toQString(const rtl::OUString& s)
110 {
111 	return QString::fromUtf16(s.getStr(), s.getLength());
112 }
113 
114 //////////////////////////////////////////////////////////////////////////
115 // KDE4FilePicker
116 //////////////////////////////////////////////////////////////////////////
117 
KDE4FilePicker(const uno::Reference<lang::XMultiServiceFactory> & xServiceMgr)118 KDE4FilePicker::KDE4FilePicker( const uno::Reference<lang::XMultiServiceFactory>& xServiceMgr )
119     : cppu::WeakComponentImplHelper8<
120           XFilterManager,
121           XFilterGroupManager,
122           XFilePickerControlAccess,
123           XFilePickerNotifier,
124 // TODO   XFilePreview,
125           lang::XInitialization,
126           util::XCancellable,
127           lang::XEventListener,
128           lang::XServiceInfo>( _helperMutex ),
129           m_xServiceMgr( xServiceMgr ),
130 		  _resMgr( CREATEVERSIONRESMGR( fps_office ) )
131 {
132 	_extraControls = new QWidget();
133 	_layout = new QGridLayout(_extraControls);
134 
135 	_dialog = new KFileDialog(KUrl("~"), QString(""), 0, _extraControls);
136 	_dialog->setMode(KFile::File | KFile::LocalOnly);
137 
138 	//default mode
139 	_dialog->setOperationMode(KFileDialog::Opening);
140 }
141 
~KDE4FilePicker()142 KDE4FilePicker::~KDE4FilePicker()
143 {
144 	delete _resMgr;
145 	delete _dialog;
146 }
147 
addFilePickerListener(const uno::Reference<XFilePickerListener> & xListener)148 void SAL_CALL KDE4FilePicker::addFilePickerListener( const uno::Reference<XFilePickerListener>& xListener )
149 	throw( uno::RuntimeException )
150 {
151     ::vos::OGuard aGuard( Application::GetSolarMutex() );
152     m_xListener = xListener;
153 }
154 
removeFilePickerListener(const uno::Reference<XFilePickerListener> &)155 void SAL_CALL KDE4FilePicker::removeFilePickerListener( const uno::Reference<XFilePickerListener>& )
156 	throw( uno::RuntimeException )
157 {
158     ::vos::OGuard aGuard( Application::GetSolarMutex() );
159     m_xListener.clear();
160 }
161 
setTitle(const rtl::OUString & title)162 void SAL_CALL KDE4FilePicker::setTitle( const rtl::OUString &title )
163     throw( uno::RuntimeException )
164 {
165 	_dialog->setCaption(toQString(title));
166 }
167 
execute()168 sal_Int16 SAL_CALL KDE4FilePicker::execute()
169     throw( uno::RuntimeException )
170 {
171 	//get the window id of the main OO window to set it for the dialog as a parent
172 	Window *pParentWin = Application::GetDefDialogParent();
173 	if ( pParentWin )
174 	{
175 		const SystemEnvData* pSysData = ((SystemWindow *)pParentWin)->GetSystemData();
176 		if ( pSysData )
177 		{
178 			KWindowSystem::setMainWindow( _dialog, pSysData->aWindow); // unx only
179 		}
180 	}
181 
182 	_dialog->clearFilter();
183 	_dialog->setFilter(_filter);
184     _dialog->filterWidget()->setEditable(false);
185 
186 	//block and wait for user input
187 	if (_dialog->exec() == KFileDialog::Accepted)
188 		return ExecutableDialogResults::OK;
189 
190 	return ExecutableDialogResults::CANCEL;
191 }
192 
setMultiSelectionMode(sal_Bool multiSelect)193 void SAL_CALL KDE4FilePicker::setMultiSelectionMode( sal_Bool multiSelect )
194     throw( uno::RuntimeException )
195 {
196 	if (multiSelect)
197 		_dialog->setMode(KFile::Files | KFile::LocalOnly);
198 	else
199 		_dialog->setMode(KFile::File | KFile::LocalOnly);
200 }
201 
setDefaultName(const::rtl::OUString & name)202 void SAL_CALL KDE4FilePicker::setDefaultName( const ::rtl::OUString &name )
203     throw( uno::RuntimeException )
204 {
205 	const QString url = toQString(name);
206 	_dialog->setSelection(url);
207 }
208 
setDisplayDirectory(const rtl::OUString & dir)209 void SAL_CALL KDE4FilePicker::setDisplayDirectory( const rtl::OUString &dir )
210     throw( uno::RuntimeException )
211 {
212 	const QString url = toQString(dir);
213 	_dialog->setUrl(KUrl(url));
214 }
215 
getDisplayDirectory()216 rtl::OUString SAL_CALL KDE4FilePicker::getDisplayDirectory()
217     throw( uno::RuntimeException )
218 {
219 	QString dir = _dialog->baseUrl().url();
220 	return toOUString(dir);
221 }
222 
getFiles()223 uno::Sequence< ::rtl::OUString > SAL_CALL KDE4FilePicker::getFiles()
224     throw( uno::RuntimeException )
225 {
226 	QStringList rawFiles = _dialog->selectedFiles();
227 	QStringList files;
228 
229 	// check if we need to add an extension
230 	QString extension = "";
231 	if ( _dialog->operationMode() == KFileDialog::Saving )
232 	{
233 		QCheckBox *cb = dynamic_cast<QCheckBox*> (
234 			_customWidgets[ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION ]);
235 
236 		if (cb && cb->isChecked())
237 		{
238 			extension = _dialog->currentFilter(); // assuming filter value is like this *.ext
239 			extension.replace("*","");
240 		}
241 	}
242 
243 	// Workaround for the double click selection KDE4 bug
244 	// kde file picker returns the file and directories for selectedFiles()
245 	// when a file is double clicked
246 	// make a true list of files
247 	const QString dir = KUrl(rawFiles[0]).directory();
248 
249 	bool singleFile = true;
250 	if (rawFiles.size() > 1)
251 	{
252 		singleFile = false;
253 		//for multi file sequences, oo expects the first param to be the directory
254 		//can't treat all cases like multi file because in some instances (inserting image)
255 		//oo WANTS only one entry in the final list
256 		files.append(dir);
257 	}
258 
259 	for (sal_uInt16 i = 0; i < rawFiles.size(); ++i)
260 	{
261 		// if the raw file is not the base directory (see above kde bug)
262 		// we add the file to list of avail files
263 		if ((dir + "/") != ( rawFiles[i]))
264 		{
265 			QString filename = KUrl(rawFiles[i]).fileName();
266 
267 			if (singleFile)
268 				filename.prepend(dir + "/");
269 
270 			//prevent extension append if we already have one
271 			if (filename.endsWith(extension))
272 				files.append(filename);
273 			else
274 				files.append(filename + extension);
275 		}
276 	}
277 
278 	// add all files and leading directory to outgoing OO sequence
279 	uno::Sequence< ::rtl::OUString > seq(files.size());
280 	for (int i = 0; i < files.size(); ++i)
281     {
282         rtl::OUString aFile(toOUString(files[i])), aURL;
283         osl_getFileURLFromSystemPath(aFile.pData, &aURL.pData );
284 		seq[i] = aURL;
285     }
286 
287 	return seq;
288 }
289 
appendFilter(const::rtl::OUString & title,const::rtl::OUString & filter)290 void SAL_CALL KDE4FilePicker::appendFilter( const ::rtl::OUString &title, const ::rtl::OUString &filter )
291     throw( lang::IllegalArgumentException, uno::RuntimeException )
292 {
293 	QString t = toQString(title);
294 	QString f = toQString(filter);
295 
296 	if (!_filter.isNull())
297 		_filter.append("\n");
298 
299 	//add to hash map for reverse lookup in getCurrentFilter
300 	_filters.insert(f, t);
301 
302 	// '/' meed to be escaped to else they are assumed to be mime types by kfiledialog
303 	//see the docs
304 	t.replace("/", "\\/");
305 
306 	// openoffice gives us filters separated by ';' qt dialogs just want space separated
307 	f.replace(";", " ");
308 
309 	_filter.append(QString("%1|%2").arg(f).arg(t));
310 }
311 
setCurrentFilter(const rtl::OUString & title)312 void SAL_CALL KDE4FilePicker::setCurrentFilter( const rtl::OUString &title )
313     throw( lang::IllegalArgumentException, uno::RuntimeException )
314 {
315 	QString t = toQString(title);
316 	t.replace("/", "\\/");
317 	_dialog->filterWidget()->setCurrentFilter(t);
318 }
319 
getCurrentFilter()320 rtl::OUString SAL_CALL KDE4FilePicker::getCurrentFilter()
321     throw( uno::RuntimeException )
322 {
323 	QString filter = _filters[_dialog->currentFilter()];
324 
325 	//default if not found
326 	if (filter.isNull())
327 		filter = "ODF Text Document (.odt)";
328 
329 	return toOUString(filter);
330 }
331 
appendFilterGroup(const rtl::OUString &,const uno::Sequence<beans::StringPair> & filters)332 void SAL_CALL KDE4FilePicker::appendFilterGroup( const rtl::OUString& , const uno::Sequence<beans::StringPair>& filters)
333     throw( lang::IllegalArgumentException, uno::RuntimeException )
334 {
335 	if (!_filter.isNull())
336 		_filter.append(QString("\n"));
337 
338 	const sal_uInt16 length = filters.getLength();
339 	for (sal_uInt16 i = 0; i < length; ++i)
340 	{
341 		beans::StringPair aPair = filters[i];
342 
343 		_filter.append(QString("%1|%2").arg(
344 			toQString(aPair.Second).replace(";", " ")).arg(
345 			toQString(aPair.First).replace("/","\\/")));
346 
347 		if (i != length - 1)
348 			_filter.append('\n');
349 	}
350 }
351 
setValue(sal_Int16 controlId,sal_Int16,const uno::Any & value)352 void SAL_CALL KDE4FilePicker::setValue( sal_Int16 controlId, sal_Int16, const uno::Any &value )
353     throw( uno::RuntimeException )
354 {
355 	QWidget* widget = _customWidgets[controlId];
356 
357 	if (widget)
358 	{
359 		switch (controlId)
360 		{
361 			case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
362 			case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
363 			case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
364 			case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
365 			case ExtendedFilePickerElementIds::CHECKBOX_LINK:
366 			case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
367 			case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
368 			{
369 				QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
370 				cb->setChecked(value.getValue());
371 				break;
372 			}
373 			case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
374 			case ExtendedFilePickerElementIds::LISTBOX_VERSION:
375 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
376 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
377 			case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
378 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
379 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
380 			case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
381 				break;
382 		}
383 	}
384 }
385 
getValue(sal_Int16 controlId,sal_Int16)386 uno::Any SAL_CALL KDE4FilePicker::getValue( sal_Int16 controlId, sal_Int16 )
387     throw( uno::RuntimeException )
388 {
389 	uno::Any res(false);
390 
391 	QWidget* widget = _customWidgets[controlId];
392 
393 	if (widget)
394 	{
395 		switch (controlId)
396 		{
397 			case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
398 			case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
399 			case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
400 			case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
401 			case ExtendedFilePickerElementIds::CHECKBOX_LINK:
402 			case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
403 			case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
404 			{
405 				QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
406 				res = uno::Any(cb->isChecked());
407 				break;
408 			}
409 			case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
410 			case ExtendedFilePickerElementIds::LISTBOX_VERSION:
411 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
412 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
413 			case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
414 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
415 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
416 			case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
417 				break;
418 		}
419 	}
420 
421 	return res;
422 }
423 
enableControl(sal_Int16 controlId,sal_Bool enable)424 void SAL_CALL KDE4FilePicker::enableControl( sal_Int16 controlId, sal_Bool enable )
425     throw( uno::RuntimeException )
426 {
427 	QWidget* widget = _customWidgets[controlId];
428 
429 	if (widget)
430 	{
431 		widget->setEnabled(enable);
432 	}
433 }
434 
setLabel(sal_Int16 controlId,const::rtl::OUString & label)435 void SAL_CALL KDE4FilePicker::setLabel( sal_Int16 controlId, const ::rtl::OUString &label )
436     throw( uno::RuntimeException )
437 {
438 	QWidget* widget = _customWidgets[controlId];
439 
440 	if (widget)
441 	{
442 		switch (controlId)
443 		{
444 			case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
445 			case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
446 			case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
447 			case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
448 			case ExtendedFilePickerElementIds::CHECKBOX_LINK:
449 			case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
450 			case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
451 			{
452 				QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
453 				cb->setText(toQString(label));
454 				break;
455 			}
456 			case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
457 			case ExtendedFilePickerElementIds::LISTBOX_VERSION:
458 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
459 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
460 			case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
461 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
462 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
463 			case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
464 				break;
465 		}
466 	}
467 }
468 
getLabel(sal_Int16 controlId)469 rtl::OUString SAL_CALL KDE4FilePicker::getLabel(sal_Int16 controlId)
470     throw ( uno::RuntimeException )
471 {
472 	QWidget* widget = _customWidgets[controlId];
473 	QString label;
474 
475 	if (widget)
476 	{
477 		switch (controlId)
478 		{
479 			case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
480 			case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
481 			case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
482 			case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
483 			case ExtendedFilePickerElementIds::CHECKBOX_LINK:
484 			case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
485 			case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
486 			{
487 				QCheckBox* cb = dynamic_cast<QCheckBox*>(widget);
488 				label = cb->text();
489 				break;
490 			}
491 			case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
492 			case ExtendedFilePickerElementIds::LISTBOX_VERSION:
493 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
494 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
495 			case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
496 			case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
497 			case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
498 			case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
499 				break;
500 		}
501 	}
502 	return toOUString(label);
503 }
504 
addCustomControl(sal_Int16 controlId)505 void KDE4FilePicker::addCustomControl(sal_Int16 controlId)
506 {
507 	QWidget* widget = 0;
508 	sal_Int32 resId = -1;
509 
510 	switch (controlId)
511 	{
512 		case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
513 			resId = STR_SVT_FILEPICKER_AUTO_EXTENSION;
514 			break;
515 		case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
516 			resId = STR_SVT_FILEPICKER_PASSWORD;
517 			break;
518 		case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
519 			resId = STR_SVT_FILEPICKER_FILTER_OPTIONS;
520 			break;
521 		case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
522 			resId = STR_SVT_FILEPICKER_READONLY;
523 			break;
524 		case ExtendedFilePickerElementIds::CHECKBOX_LINK:
525 			resId = STR_SVT_FILEPICKER_INSERT_AS_LINK;
526 			break;
527 		case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
528 			resId = STR_SVT_FILEPICKER_SHOW_PREVIEW;
529 			break;
530 		case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
531 			resId = STR_SVT_FILEPICKER_SELECTION;
532 			break;
533 		case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
534 			resId = STR_SVT_FILEPICKER_PLAY;
535 			break;
536 		case ExtendedFilePickerElementIds::LISTBOX_VERSION:
537 			resId = STR_SVT_FILEPICKER_VERSION;
538 			break;
539 		case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
540 			resId = STR_SVT_FILEPICKER_TEMPLATES;
541 			break;
542 		case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
543 			resId = STR_SVT_FILEPICKER_IMAGE_TEMPLATE;
544 			break;
545 		case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
546 		case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
547 		case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
548 		case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
549 			break;
550 	}
551 
552 	switch (controlId)
553 	{
554 		case ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION:
555 		case ExtendedFilePickerElementIds::CHECKBOX_PASSWORD:
556 		case ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS:
557 		case ExtendedFilePickerElementIds::CHECKBOX_READONLY:
558 		case ExtendedFilePickerElementIds::CHECKBOX_LINK:
559 		case ExtendedFilePickerElementIds::CHECKBOX_PREVIEW:
560 		case ExtendedFilePickerElementIds::CHECKBOX_SELECTION:
561 		{
562 			QString label;
563 
564 			if (_resMgr && resId != -1)
565 			{
566 				rtl::OUString s = String(ResId( resId, *_resMgr ));
567 				label = toQString(s);
568 				label.replace("~", "&");
569 			}
570 
571 			widget = new QCheckBox(label, _extraControls);
572 
573 			break;
574 		}
575 		case ExtendedFilePickerElementIds::PUSHBUTTON_PLAY:
576 		case ExtendedFilePickerElementIds::LISTBOX_VERSION:
577 		case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE:
578 		case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE:
579 		case ExtendedFilePickerElementIds::LISTBOX_VERSION_LABEL:
580 		case ExtendedFilePickerElementIds::LISTBOX_TEMPLATE_LABEL:
581 		case ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE_LABEL:
582 		case ExtendedFilePickerElementIds::LISTBOX_FILTER_SELECTOR:
583 			break;
584 	}
585 
586 	if (widget)
587 	{
588 		_layout->addWidget(widget);
589 		_customWidgets.insert(controlId, widget);
590 	}
591 }
592 
initialize(const uno::Sequence<uno::Any> & args)593 void SAL_CALL KDE4FilePicker::initialize( const uno::Sequence<uno::Any> &args )
594     throw( uno::Exception, uno::RuntimeException )
595 {
596 	_filter.clear();
597 	_filters.clear();
598 
599     // parameter checking
600     uno::Any arg;
601     if (args.getLength() == 0)
602 	{
603         throw lang::IllegalArgumentException(
604                 rtl::OUString::createFromAscii( "no arguments" ),
605                 static_cast< XFilePicker* >( this ), 1 );
606 	}
607 
608     arg = args[0];
609 
610     if (( arg.getValueType() != ::getCppuType((sal_Int16*)0)) &&
611 		( arg.getValueType() != ::getCppuType((sal_Int8*)0)))
612 	{
613         throw lang::IllegalArgumentException(
614                 rtl::OUString::createFromAscii( "invalid argument type" ),
615                 static_cast< XFilePicker* >( this ), 1 );
616 	}
617 
618     sal_Int16 templateId = -1;
619     arg >>= templateId;
620 
621 	//default is opening
622 	KFileDialog::OperationMode operationMode = KFileDialog::Opening;
623 
624     switch ( templateId )
625     {
626         case FILEOPEN_SIMPLE:
627             break;
628 
629         case FILESAVE_SIMPLE:
630             operationMode = KFileDialog::Saving;
631             break;
632 
633         case FILESAVE_AUTOEXTENSION:
634             operationMode = KFileDialog::Saving;
635 			//addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION );
636             break;
637 
638         case FILESAVE_AUTOEXTENSION_PASSWORD:
639 		{
640             operationMode = KFileDialog::Saving;
641 			//addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION );
642             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD );
643             break;
644 		}
645         case FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS:
646 		{
647 			operationMode = KFileDialog::Saving;
648 			addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION );
649             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD );
650             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_FILTEROPTIONS );
651             break;
652 		}
653         case FILESAVE_AUTOEXTENSION_SELECTION:
654             operationMode = KFileDialog::Saving;
655             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION );
656             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_SELECTION );
657             break;
658 
659         case FILESAVE_AUTOEXTENSION_TEMPLATE:
660             operationMode = KFileDialog::Saving;
661             addCustomControl( ExtendedFilePickerElementIds::LISTBOX_TEMPLATE );
662             break;
663 
664         case FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE:
665             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_LINK );
666             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_PREVIEW );
667             addCustomControl( ExtendedFilePickerElementIds::LISTBOX_IMAGE_TEMPLATE );
668             break;
669 
670         case FILEOPEN_PLAY:
671             addCustomControl( ExtendedFilePickerElementIds::PUSHBUTTON_PLAY );
672             break;
673 
674         case FILEOPEN_READONLY_VERSION:
675             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_READONLY );
676             addCustomControl( ExtendedFilePickerElementIds::LISTBOX_VERSION );
677             break;
678 
679         case FILEOPEN_LINK_PREVIEW:
680             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_LINK );
681             addCustomControl( ExtendedFilePickerElementIds::CHECKBOX_PREVIEW );
682             break;
683 
684         default:
685             throw lang::IllegalArgumentException(
686                     rtl::OUString::createFromAscii( "Unknown template" ),
687                     static_cast< XFilePicker* >( this ),
688                     1 );
689     }
690 
691 	_dialog->setOperationMode(operationMode);
692     _dialog->setConfirmOverwrite(true);
693 }
694 
cancel()695 void SAL_CALL KDE4FilePicker::cancel()
696     throw ( uno::RuntimeException )
697 {
698 
699 }
700 
disposing(const lang::EventObject & rEvent)701 void SAL_CALL KDE4FilePicker::disposing( const lang::EventObject &rEvent )
702     throw( uno::RuntimeException )
703 {
704     uno::Reference<XFilePickerListener> xFilePickerListener( rEvent.Source, uno::UNO_QUERY );
705 
706     if ( xFilePickerListener.is() )
707 	{
708         removeFilePickerListener( xFilePickerListener );
709 	}
710 }
711 
getImplementationName()712 rtl::OUString SAL_CALL KDE4FilePicker::getImplementationName()
713     throw( uno::RuntimeException )
714 {
715     return rtl::OUString::createFromAscii( FILE_PICKER_IMPL_NAME );
716 }
717 
supportsService(const rtl::OUString & ServiceName)718 sal_Bool SAL_CALL KDE4FilePicker::supportsService( const rtl::OUString& ServiceName )
719     throw( uno::RuntimeException )
720 {
721     uno::Sequence< ::rtl::OUString > SupportedServicesNames = FilePicker_getSupportedServiceNames();
722 
723     for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
724     {
725         if ( SupportedServicesNames[n].compareTo( ServiceName ) == 0 )
726             return sal_True;
727     }
728 
729     return sal_False;
730 }
731 
getSupportedServiceNames()732 uno::Sequence< ::rtl::OUString > SAL_CALL KDE4FilePicker::getSupportedServiceNames()
733     throw( uno::RuntimeException )
734 {
735     return FilePicker_getSupportedServiceNames();
736 }
737