summaryrefslogtreecommitdiff
path: root/vcl/unx/kde/fpicker/kdefilepicker.cxx
blob: 73210557980b5bed818f31485aadf17770ab485a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <cstddef>

#include <kdecommandthread.hxx>
#include <kdefilepicker.hxx>

#include <config_vclplug.h>

#if ENABLE_TDE

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqgrid.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqobjectlist.h>
#include <tqpushbutton.h>
#include <tqregexp.h>
#include <tqvbox.h>

#ifdef TQT_NO_EMIT
#define emit
#endif

#include <tdeversion.h>
#include <tdediroperator.h>
#include <tdefiledialog.h>
#include <tdefilefiltercombo.h>
#include <tdeio/netaccess.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdetempfile.h>

#else // ENABLE_TDE

#include <qcheckbox.h>
#include <qcombobox.h>
#include <qgrid.h>
#include <qhbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qobjectlist.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <qvbox.h>

#ifdef QT_NO_EMIT
#define emit
#endif

#include <kdeversion.h>
#include <kdiroperator.h>
#include <kfiledialog.h>
#include <kfilefiltercombo.h>
#include <kio/netaccess.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ktempfile.h>

#endif // ENABLE_TDE

#if ENABLE_TDE
#define QCheckBox_String    "TQCheckBox"
#define QComboBox_String    "TQComboBox"
#else // ENABLE_TDE
#define QCheckBox_String    "QCheckBox"
#define QComboBox_String    "QComboBox"
#endif // ENABLE_TDE

#include <algorithm>
#include <iostream>


// KDEFileDialog


KDEFileDialog::KDEFileDialog( const QString &startDir, const QString &filter,
        QWidget *parent, const char *name )
    : KFileDialog( startDir, filter, parent, name, true, m_pCustomWidget = new QVBox() ),
      m_pCombosAndButtons( new QHBox( m_pCustomWidget ) ),
      m_pLabels( new QVBox( m_pCombosAndButtons ) ),
      m_pComboBoxes( new QVBox( m_pCombosAndButtons ) ),
      m_pPushButtons( new QVBox( m_pCombosAndButtons ) ),
      m_pCheckBoxes( new QGrid( 2, m_pCustomWidget ) ),
      m_bIsSave( false ),
      m_bIsExecuting( false ),
      m_bCanNotifySelection( true )
{
#if ENABLE_TDE
    connect( this, SIGNAL( fileHighlighted( const TQString & ) ),
             this, SLOT( fileHighlightedCommand( const TQString & ) ) );
#else // ENABLE_TDE
    connect( this, SIGNAL( fileHighlighted( const QString & ) ),
             this, SLOT( fileHighlightedCommand( const QString & ) ) );
#endif // ENABLE_TDE

    connect( this, SIGNAL( selectionChanged() ),
             this, SLOT( selectionChangedCommand() ) );

    m_pCustomWidget->setSpacing( KDialog::spacingHint() );
    m_pCombosAndButtons->setSpacing( KDialog::spacingHint() );

    updateCustomWidgetLayout();
}

KDEFileDialog::~KDEFileDialog()
{
}

void KDEFileDialog::resizeEvent( QResizeEvent *pEvent )
{
    KFileDialog::resizeEvent( pEvent );

    updateCustomWidgetLayout();
}

void KDEFileDialog::showEvent( QShowEvent *pEvent )
{
    KFileDialog::showEvent( pEvent );

    updateCustomWidgetLayout();
}

void KDEFileDialog::updateCustomWidgetLayout()
{
    QPoint qReferencePoint = filterWidget->mapTo( this, QPoint( 0, 0 ) );
    QPoint qCustomPoint = m_pCustomWidget->mapTo( this, QPoint( 0, 0 ) );

    int nLeft = qReferencePoint.x() - qCustomPoint.x();
    int nRight = m_pCustomWidget->width() - filterWidget->width() - nLeft;

    nLeft -= KDialog::spacingHint();
    nRight -= KDialog::spacingHint();
    m_pLabels->setFixedWidth( ( nLeft > 0 )? nLeft: 80 );
    // FIXME The following call sets the width of m_pPushButtons all right,
    // but it also increases the width of m_pComboBoxes rapidly. Can we do
    // anything about it?
    m_pPushButtons->setFixedWidth( ( nRight > 0 )? nRight: 100 );
}

void KDEFileDialog::customEvent( QCustomEvent *pEvent )
{
    if ( pEvent && pEvent->type() == KDECommandEvent::TypeId )
    {
        KDECommandEvent *pCommandEvent = static_cast< KDECommandEvent* >( pEvent );
        QStringList *pStringList = pCommandEvent->stringList();

        int nListSize = -1;
        if ( pStringList )
            nListSize = pStringList->size();

        switch ( pCommandEvent->command() )
        {
            case KDECommandEvent::AppendControl:
                if ( nListSize >= 3 )
                {
                    appendControl( (*pStringList)[0], (*pStringList)[1], (*pStringList)[2] );
                }
                break;
            case KDECommandEvent::EnableControl:
                if ( nListSize >= 2 )
                {
                    enableControl( (*pStringList)[0], (*pStringList)[1] );
                }
                break;
            case KDECommandEvent::GetValue:
                if ( nListSize >= 2 )
                {
                    getValue( (*pStringList)[0], (*pStringList)[1] );
                }
                break;
            case KDECommandEvent::SetValue:
                if ( nListSize >= 2 )
                {
                    QStringList qStringList = (*pStringList);
                    qStringList.pop_front();
                    qStringList.pop_front();

                    setValue( (*pStringList)[0], (*pStringList)[1], qStringList );
                }
                break;
            case KDECommandEvent::AppendFilter:
                if ( nListSize >= 2 )
                {
                    appendFilter( (*pStringList)[0], (*pStringList)[1] );

                    // update the filters widget
                    setFilter( filters() );
                }
                break;
            case KDECommandEvent::AppendFilterGroup:
                if ( nListSize >= 1 )
                {
                    QStringList::const_iterator it = pStringList->begin();
                    ++it; // We ignore the filter group name

                    while ( it != pStringList->end() )
                    {
                        QString qTitle = *it;
                        ++it;
                        if ( it != pStringList->end() )
                        {
                            appendFilter( qTitle, (*it) );
                            ++it;
                        }
                    }

                    // update the filters widget
                    setFilter( filters() );
                }
                break;
            case KDECommandEvent::GetCurrentFilter:
                {
                    QString qCurrentFilter = filterWidget->currentText();
                    sendCommand( "currentFilter " + escapeString( qCurrentFilter ) );
                }
                break;
            case KDECommandEvent::SetCurrentFilter:
                if ( nListSize >= 1 )
                {
                    static_cast< KDEFileFilterComboHack* >( filterWidget )->setCurrentFilter( pStringList->front() );
                }
                break;
            case KDECommandEvent::GetDirectory:
                {
                    QString qDirectory = baseURL().url();
                    if ( qDirectory.startsWith( "file:/" ) && qDirectory.mid( 6, 1 ) != "/" )
                        qDirectory.replace( "file:/", "file:///" );
                    sendCommand( "currentDirectory " + escapeString( qDirectory ) );
                }
                break;
            case KDECommandEvent::SetDirectory:
                if ( nListSize >= 1 )
                {
                    setURL( pStringList->front() );
                }
                break;
            case KDECommandEvent::GetFiles:
                {
                    QString qString;
                    qString.reserve( 1024 );

                    qString.append( "files" );

                    if ( result() == QDialog::Accepted )
                    {
                        KURL::List qList( selectedURLs() );
                        for ( KURL::List::const_iterator it = qList.begin(); it != qList.end(); ++it )
                            appendURL( qString, (*it) );
                    }
                    else
                    {
                        // we have to return the selected files anyway
                        const KFileItemList *pItems = ops->selectedItems();
                        for ( KFileItemListIterator it( *pItems ); it.current(); ++it )
                            appendURL( qString, (*it)->url() );
                    }

                    sendCommand( qString );
                    setCanNotifySelection( true );
                }
                break;
            case KDECommandEvent::SetTitle:
                if ( nListSize >= 1 )
                {
                    setCaption( pStringList->front() );
                }
                break;
            case KDECommandEvent::SetType:
                if ( nListSize >= 1 )
                {
                    QString qType( pStringList->front() );
                    if ( qType == "open" )
                    {
                        setIsSave( false );
                        setCaption( i18n( "Open" ) );
                    }
                    else if ( qType == "save" )
                    {
                        setIsSave( true );
                        setCaption( i18n( "Save As" ) );
                    }
                }
                break;
            case KDECommandEvent::SetDefaultName:
                if ( nListSize >= 1 )
                {
                    setKeepLocation( true );
                    setSelection( pStringList->front() );
                }
                break;
            case KDECommandEvent::SetMultiSelection:
                if ( nListSize >= 1 )
                {
                    if ( pStringList->front() == "true" )
                        setMode( KFile::Files );
                    else
                        setMode( KFile::File );
                }
                break;
            case KDECommandEvent::Exec:
                {
                    filterWidget->setEditable( false );
                    setIsExecuting( true );
                    bool bCanExit = false;
                    do {
                        setCanNotifySelection( true );
                        exec();

                        KURL qLocalSelectedURL = mostLocalURL( selectedURL() );
                        QString qProtocol( qLocalSelectedURL.protocol() );

                        if ( isSave() && result() == QDialog::Accepted )
                        {
                            if ( qProtocol == "file" )
                            {
                                QString qFileName( addExtension( qLocalSelectedURL.path() ) );
                                bCanExit =
                                    !QFile::exists( qFileName ) ||
                                    ( KMessageBox::warningYesNo( 0,
                                                                 i18n( "A file named \"%1\" already exists. "
                                                                     "Are you sure you want to overwrite it?" ).arg( qFileName ),
                                                                 i18n( "Overwrite File?" ),
                                                                 i18n( "Overwrite" ), KStdGuiItem::cancel() ) == KMessageBox::Yes );
                            }
                            else if ( !isSupportedProtocol( qProtocol ) )
                            {
                                KMessageBox::sorry( 0,
                                        i18n( "Saving using protocol \"%1\" is not supported." ).arg( qProtocol ) );
                                bCanExit = false;
                            }
                            else
                                bCanExit = true;
                        }
                        else if ( !isSave() && result() == QDialog::Accepted && !isSupportedProtocol( qProtocol ) )
                        {
                            KMessageBox::information( 0,
                                    i18n( "Protocol \"%1\" is supported only partially. "
                                        "Local copy of the file will be created." ).arg( qProtocol ) );
                            bCanExit = true;
                        }
                        else
                            bCanExit = true;
                    } while ( !bCanExit );
                    setIsExecuting( false );

                    if ( result() == QDialog::Accepted )
                        sendCommand( "accept" );
                    else
                        sendCommand( "reject" );
                }
                break;
            default:
                break;
        }

        // FIXME Some cleanup of pEvent? delete something, etc.?
    }
}

void KDEFileDialog::appendControl( const QString &rId, const QString &rType, const QString &rTitle )
{
    QString qLabel( rTitle );
    qLabel.replace( '~', '&' );

    if ( rType == "checkbox" )
    {
        QCheckBox *pCheckBox = new QCheckBox( qLabel, m_pCheckBoxes, rId.utf8() );

        pCheckBox->setEnabled( true );
        pCheckBox->setChecked( false );
    }
    else if ( rType == "listbox" )
    {
        QLabel *pComboLabel = new QLabel( qLabel, m_pLabels );
        QComboBox *pComboBox = new QComboBox( m_pComboBoxes, rId.utf8() );

        pComboLabel->setBuddy( pComboBox );
        pComboBox->setEnabled( true );
    }
    else if ( rType == "pushbutton" )
    {
        QPushButton *pPushButton = new QPushButton( qLabel, m_pPushButtons, rId.utf8() );
        pPushButton->setEnabled( true );
    }
}

QWidget* KDEFileDialog::findControl( const QString &rId ) const
{
    QObjectList *pList = m_pCustomWidget->queryList();
    QCString qName( rId.utf8() );
    QObjectList::const_iterator it = pList->begin();

    for ( ; it != pList->end() && qName != (*it)->name(); ++it )
        ;

    QWidget *pWidget = NULL;
    if ( it != pList->end() )
        pWidget = static_cast< QWidget* >( *it );

    delete pList;

    return pWidget;
}

void KDEFileDialog::enableControl( const QString &rId, const QString &rValue )
{
    QWidget *pWidget = findControl( rId );

    if ( pWidget )
        pWidget->setEnabled( rValue.lower() == "true" );
}

void KDEFileDialog::getValue( const QString &rId, const QString &rAction )
{
    QWidget *pWidget = findControl( rId );
    QString qString;
    qString.reserve( 1024 );
    qString.append( "value" );

    if ( pWidget )
    {
        QCString qClassName = pWidget->className();
        if ( qClassName == QCheckBox_String )
        {
            QCheckBox *pCheckBox = static_cast< QCheckBox* >( pWidget );

            if ( pCheckBox->isChecked() )
                qString.append( " bool true" );
            else
                qString.append( " bool false" );
        }
        else if ( qClassName == QComboBox_String )
        {
            QComboBox *pComboBox = static_cast< QComboBox* >( pWidget );
            if ( rAction == "getItems" )
            {
                qString.append( " stringList" );
                for ( int nIdx = 0; nIdx < pComboBox->count(); ++nIdx )
                {
                    qString.append( ' ' );
                    appendEscaped( qString, pComboBox->text( nIdx ) );
                }
            }
            else if ( rAction == "getSelectedItem" )
            {
                qString.append( " string " );
                appendEscaped( qString, pComboBox->currentText() );
            }
            else if ( rAction == "getSelectedItemIndex" )
            {
                qString.append( " int " );
                qString.append( QString().setNum( pComboBox->currentItem() ) );
            }
            // TODO getHelpURL
        }
        // TODO push button
    }

    sendCommand( qString );
}

void KDEFileDialog::setValue( const QString &rId, const QString &rAction, const QStringList &rValue )
{
    QWidget *pWidget = findControl( rId );

    if ( pWidget )
    {
        QCString qClassName = pWidget->className();
        if ( qClassName == QCheckBox_String )
        {
            QCheckBox *pCheckBox = static_cast< QCheckBox* >( pWidget );

            bool bValue = ( !rValue.isEmpty() ) && ( rValue.front().lower() == "true" );
            pCheckBox->setChecked( bValue );
        }
        else if ( qClassName == QComboBox_String )
        {
            QComboBox *pComboBox = static_cast< QComboBox* >( pWidget );
            if ( rAction == "addItem" )
            {
                if ( !rValue.isEmpty() )
                    pComboBox->insertItem( rValue.front() );
            }
            else if ( rAction == "addItems" )
            {
                pComboBox->insertStringList( rValue );
            }
            else if ( rAction == "deleteItem" )
            {
                if ( !rValue.isEmpty() )
                    pComboBox->removeItem( rValue.front().toInt() );
            }
            else if ( rAction == "deleteItems" )
            {
                pComboBox->clear();
            }
            else if ( rAction == "setSelectedItem" )
            {
                if ( !rValue.isEmpty() )
                    pComboBox->setCurrentItem( rValue.front().toInt() );
            }
            // FIXME setHelpURL is ignored
        }
        // TODO push button
    }
}

void KDEFileDialog::appendFilter( const QString &rTitle, const QString &rFilter )
{
    // Filters are separated by ';'
    QString qFilter( rFilter );
    qFilter.replace( QChar( ';' ), QChar( ' ' ) ).replace( "*.*", "*" );

    // Workaround for too wide <All formats> (*.bmp;...) entry
    QString qTitle( rTitle );
    qTitle.replace( QRegExp( "^<([^>]*)> \\(.*" ), "<\\1>" );

    m_aFilters.push_back( qMakePair( qTitle, qFilter ) );
}

QString KDEFileDialog::filters() const
{
    QString qString, qTmp;
    bool bFirstFilter = true;

    for ( FilterList::const_iterator it = m_aFilters.begin(); it != m_aFilters.end(); ++it )
    {
        if ( bFirstFilter )
            bFirstFilter = false;
        else
            qString.append( '\n' );

        qString.append( (*it).second );
        qString.append( '|' );

        qTmp = (*it).first;
        qString.append( qTmp.replace( '/', "\\/" ) );
    }

    return qString;
}

QString KDEFileDialog::addExtension( const QString &rFileName ) const
{
    if ( !isSave() )
        return rFileName;

    QString qExtension;

    QWidget *pExtensionWidget = findControl( "100" ); // CHECKBOX_AUTOEXTENSION
    QCheckBox *pExtensionCB = pExtensionWidget? static_cast< QCheckBox* >( pExtensionWidget->qt_cast( QCheckBox_String ) ): NULL;
    if ( pExtensionCB && pExtensionCB->isChecked() )
    {
        // FIXME: qFilter can be a MIME; we ignore it now...
        QStringList qFilterList = QStringList::split( " ", currentFilter() );
        for ( QStringList::const_iterator it = qFilterList.begin();
              qExtension.isEmpty() && it != qFilterList.end();
              ++it )
        {
            int nUnwanted = (*it).findRev( '*' );
            if ( nUnwanted < 0 )
                nUnwanted = (*it).findRev( '?' );
            else
                nUnwanted = ::std::max( nUnwanted, (*it).find( '?', nUnwanted ) );

            int nIdx = (*it).find( '.', ::std::max( nUnwanted, 0 ) );
            if ( nIdx >= 0 )
                qExtension = (*it).mid( nIdx ).lower();
        }
    }

    if ( qExtension.isEmpty() || qExtension == "." || rFileName.endsWith( qExtension ) )
        return rFileName;
    else
        return rFileName + qExtension;
}

bool KDEFileDialog::isSupportedProtocol( const QString &rProtocol ) const
{
    // TODO Get this information directly from OOo
    const char * pOOoProtocols[] = { "", "smb", "ftp", "http", "file", "mailto",
        "vnd.sun.star.webdav", "news", "private", "vnd.sun.star.help",
        "https", "slot", "macro", "javascript", "imap", "pop3", "data",
        "cid", "out", "vnd.sun.star.wfs", "vnd.sun.star.hier", "vim",
        ".uno", ".component", "vnd.sun.star.pkg", "ldap", "db",
        "vnd.sun.star.cmd", "vnd.sun.star.script",
        "telnet",
        NULL };

    for ( const char **pIndex = pOOoProtocols; *pIndex != NULL; ++pIndex )
    {
        if ( rProtocol == *pIndex )
            return true;
    }

    // TODO gnome-vfs bits here

    return false;
}

KURL KDEFileDialog::mostLocalURL( const KURL &rURL ) const
{
#if KDE_IS_VERSION(3,5,0)
    KURL qMostLocalURL( KIO::NetAccess::mostLocalURL( rURL, const_cast<KDEFileDialog*>( this ) ) );
    if ( qMostLocalURL.isLocalFile() )
        return qMostLocalURL;
    else
    {
        // Terrible hack to get even non-existing media:// files right
        qMostLocalURL.cd( ".." );
        KURL qMostLocalPath( KIO::NetAccess::mostLocalURL( qMostLocalURL, const_cast<KDEFileDialog*>( this ) ) );
        if ( qMostLocalPath.isLocalFile() )
        {
            qMostLocalPath.addPath( rURL.fileName() );
            return qMostLocalPath;
        }
    }
#endif

    return rURL;
}

QString KDEFileDialog::localCopy( const QString &rFileName ) const
{
    // 106 == MIB enum for UTF-8
    KURL qLocalURL = mostLocalURL( KURL( rFileName, 106 ) );
    if ( qLocalURL.isLocalFile() )
        return qLocalURL.url();

    int nExtensionPos = rFileName.findRev( '/' );
    if ( nExtensionPos >= 0 )
        nExtensionPos = rFileName.find( '.', nExtensionPos );
    else
        nExtensionPos = rFileName.find( '.' );

    KTempFile qTempFile( QString::null, ( nExtensionPos < 0 )? QString(): rFileName.mid( nExtensionPos ) );
    KURL qDestURL;
    qDestURL.setPath( qTempFile.name() );

    if ( !KIO::NetAccess::file_copy( rFileName, qDestURL, 0600, true, false, NULL ) )
    {
        KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
        return QString::null;
    }

    return qDestURL.url();
}

#if ENABLE_TDE
void KDEFileDialog::fileHighlightedCommand( const TQString & )
#else // ENABLE_TDE
void KDEFileDialog::fileHighlightedCommand( const QString & )
#endif // ENABLE_TDE
{
    if ( canNotifySelection() )
    {
    sendCommand( "fileSelectionChanged" );
    setCanNotifySelection( false );
    }
}

void KDEFileDialog::selectionChangedCommand()
{
    if ( canNotifySelection() )
    {
    sendCommand( "fileSelectionChanged" );
    setCanNotifySelection( false );
    }
}

void KDEFileDialog::sendCommand( const QString &rCommand )
{
#if OSL_DEBUG_LEVEL > 1
    ::std::cerr << "kdefilepicker sent: " << rCommand.latin1() << ::std::endl;
#endif

    //m_aOutputStream << rCommand << endl;
    ::std::cout << rCommand.utf8() << ::std::endl;
}

void KDEFileDialog::appendURL( QString &rBuffer, const KURL &rURL )
{
    // From Martin Kretzschmar:
    // file:///path/to/test%E0.odt is not a valid URL from OOo's point of
    // view. (?Most modern parts of?) OOo assume(s) that the URL contains only
    // ASCII characters (which test%E0.odt does) and is UTF-8 after unescaping
    // (which file:///path/test%E0.odt is not).
    // Cf. the comment in sal/inc/osl/file.h.
    // 106 == MIB enum for UTF-8
    QString qUrlStr = addExtension( rURL.url( 0, 106 ) );

    if ( !isExecuting() && !isSupportedProtocol( rURL.protocol() ) )
        qUrlStr = localCopy( qUrlStr );

    if ( qUrlStr.startsWith( "file:/" ) && qUrlStr.mid( 6, 1 ) != "/" )
        qUrlStr.replace( "file:/", "file:///" );

    rBuffer.append( " " );
    if ( !qUrlStr.isEmpty() )
        appendEscaped( rBuffer, qUrlStr );
}

void KDEFileDialog::appendEscaped( QString &rBuffer, const QString &rString )
{
    const QChar *pUnicode = rString.unicode();
    const QChar *pEnd     = pUnicode + rString.length();

    rBuffer.append( '"' );
    for ( ; pUnicode != pEnd; ++pUnicode )
    {
        if ( *pUnicode == '\\' )
            rBuffer.append( "\\\\" );
        else if ( *pUnicode == '"' )
            rBuffer.append( "\\\"" );
        else if ( *pUnicode == '\n' )
            rBuffer.append( "\\\n" );
        else
            rBuffer.append( *pUnicode );
    }
    rBuffer.append( '"' );
}

QString KDEFileDialog::escapeString( const QString &rString )
{
    QString qString;
    qString.reserve( 2*rString.length() + 2 ); // every char escaped + quotes

    appendEscaped( qString, rString );

    return qString;
}


void KDEFileFilterComboHack::setCurrentFilter( const QString& filter )
{
    setCurrentText( filter );
    filterChanged();

    // Workaround for 'Filter name (*.blah)' vs. 'Filter name'
    if ( currentText() != text( currentItem() ) )
    {
        int nItem = 0;
        for ( ; nItem < count() && !text( nItem ).startsWith( filter ); ++nItem );

        if ( nItem < count() )
            setCurrentItem( nItem );
        else
            setCurrentItem( 0 );

        filterChanged();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */