summaryrefslogtreecommitdiffstats
path: root/tdeio/tdefile/kurlrequester.cpp
blob: cca8c052939289c72a679772b5ff1d3e3dd7664a (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
/* This file is part of the KDE libraries
    Copyright (C) 1999,2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>

    library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2, as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/


#include <sys/stat.h>
#include <unistd.h>

#include <tqstring.h>
#include <tqtooltip.h>
#include <tqapplication.h>

#include <kaccel.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kdirselectdialog.h>
#include <tdefiledialog.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <klocale.h>
#include <kurlcompletion.h>
#include <kurldrag.h>
#include <kprotocolinfo.h>

#include "kurlrequester.h"


class KURLDragPushButton : public KPushButton
{
public:
    KURLDragPushButton( TQWidget *parent, const char *name=0 )
	: KPushButton( parent, name ) {
    	setDragEnabled( true );
    }
    ~KURLDragPushButton() {}

    void setURL( const KURL& url ) {
	m_urls.clear();
	m_urls.append( url );
    }

    /* not needed so far
    void setURLs( const KURL::List& urls ) {
	m_urls = urls;
    }
    const KURL::List& urls() const { return m_urls; }
    */

protected:
    virtual TQDragObject *dragObject() {
	if ( m_urls.isEmpty() )
	    return 0L;

	TQDragObject *drag = new KURLDrag( m_urls, this, "url drag" );
	return drag;
    }

private:
    KURL::List m_urls;

};


/*
*************************************************************************
*/

class KURLRequester::KURLRequesterPrivate
{
public:
    KURLRequesterPrivate() {
	edit = 0L;
	combo = 0L;
        fileDialogMode = KFile::File | KFile::ExistingOnly | KFile::LocalOnly;
    }

    void setText( const TQString& text ) {
	if ( combo )
	{
	    if (combo->editable())
	    {
               combo->setEditText( text );
            }
            else
            {
               combo->insertItem( text );
               combo->setCurrentItem( combo->count()-1 );
            }
        }
	else
	{
	    edit->setText( text );
	}
    }

    void connectSignals( TQObject *receiver ) {
	TQObject *sender;
	if ( combo )
	    sender = TQT_TQOBJECT(combo);
	else
	    sender = TQT_TQOBJECT(edit);

	connect( sender, TQT_SIGNAL( textChanged( const TQString& )),
		 receiver, TQT_SIGNAL( textChanged( const TQString& )));
	connect( sender, TQT_SIGNAL( returnPressed() ),
		 receiver, TQT_SIGNAL( returnPressed() ));
	connect( sender, TQT_SIGNAL( returnPressed( const TQString& ) ),
		 receiver, TQT_SIGNAL( returnPressed( const TQString& ) ));
    }

    void setCompletionObject( KCompletion *comp ) {
	if ( combo )
	    combo->setCompletionObject( comp );
	else
	    edit->setCompletionObject( comp );
    }

    /**
     * replaces ~user or $FOO, if necessary
     */
    TQString url() {
        TQString txt = combo ? combo->currentText() : edit->text();
        KURLCompletion *comp;
        if ( combo )
            comp = dynamic_cast<KURLCompletion*>(combo->completionObject());
        else
            comp = dynamic_cast<KURLCompletion*>(edit->completionObject());

        if ( comp )
            return comp->replacedPath( txt );
        else
            return txt;
    }

    KLineEdit *edit;
    KComboBox *combo;
    int fileDialogMode;
    TQString fileDialogFilter;
};



KURLRequester::KURLRequester( TQWidget *editWidget, TQWidget *parent,
			      const char *name )
  : TQHBox( parent, name )
{
    d = new KURLRequesterPrivate;

    // must have this as parent
    editWidget->reparent( this, 0, TQPoint(0,0) );
    d->edit = dynamic_cast<KLineEdit*>( editWidget );
    d->combo = dynamic_cast<KComboBox*>( editWidget );

    init();
}


KURLRequester::KURLRequester( TQWidget *parent, const char *name )
  : TQHBox( parent, name )
{
    d = new KURLRequesterPrivate;
    init();
}


KURLRequester::KURLRequester( const TQString& url, TQWidget *parent,
			      const char *name )
  : TQHBox( parent, name )
{
    d = new KURLRequesterPrivate;
    init();
    setKURL( KURL::fromPathOrURL( url ) );
}


KURLRequester::~KURLRequester()
{
    delete myCompletion;
    delete myFileDialog;
    delete d;
}


void KURLRequester::init()
{
    myFileDialog    = 0L;
    myShowLocalProt = false;

    if ( !d->combo && !d->edit )
	d->edit = new KLineEdit( this, "line edit" );

    myButton = new KURLDragPushButton( this, "tdefile button");
    TQIconSet iconSet = SmallIconSet(TQString::fromLatin1("fileopen"));
    TQPixmap pixMap = iconSet.pixmap( TQIconSet::Small, TQIconSet::Normal );
    myButton->setIconSet( iconSet );
    myButton->setFixedSize( pixMap.width()+8, pixMap.height()+8 );
    TQToolTip::add(myButton, i18n("Open file dialog"));

    connect( myButton, TQT_SIGNAL( pressed() ), TQT_SLOT( slotUpdateURL() ));

    setSpacing( KDialog::spacingHint() );

    TQWidget *widget = d->combo ? (TQWidget*) d->combo : (TQWidget*) d->edit;
    widget->installEventFilter( this );
    setFocusProxy( widget );

    d->connectSignals( TQT_TQOBJECT(this) );
    connect( myButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotOpenDialog() ));

    myCompletion = new KURLCompletion();
    d->setCompletionObject( myCompletion );

    KAccel *accel = new KAccel( this );
    accel->insert( KStdAccel::Open, TQT_TQOBJECT(this), TQT_SLOT( slotOpenDialog() ));
    accel->readSettings();
}


void KURLRequester::setURL( const TQString& url )
{
    if ( myShowLocalProt )
    {
        d->setText( url );
    }
    else
    {
        // ### This code is broken (e.g. for paths with '#')
        if ( url.startsWith("file://") )
            d->setText( url.mid( 7 ) );
        else if ( url.startsWith("file:") )
            d->setText( url.mid( 5 ) );
        else
            d->setText( url );
    }
}

void KURLRequester::setKURL( const KURL& url )
{
    if ( myShowLocalProt )
        d->setText( url.url() );
    else
        d->setText( url.pathOrURL() );
}

void KURLRequester::setCaption( const TQString& caption )
{
   TQWidget::setCaption( caption );
   if (myFileDialog)
      myFileDialog->setCaption( caption );
}

TQString KURLRequester::url() const
{
    return d->url();
}

void KURLRequester::slotOpenDialog()
{
    KURL newurl;
    if ( (d->fileDialogMode & KFile::Directory) && !(d->fileDialogMode & KFile::File) ||
         /* catch possible fileDialog()->setMode( KFile::Directory ) changes */
         (myFileDialog && ( (myFileDialog->mode() & KFile::Directory) &&
         (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0 ) ) )
    {
        newurl = KDirSelectDialog::selectDirectory(url(), d->fileDialogMode & KFile::LocalOnly);
        if ( !newurl.isValid() )
        {
            return;
        }
    }
    else
    {
      emit openFileDialog( this );

      KFileDialog *dlg = fileDialog();
      if ( !d->url().isEmpty() ) {
          KURL u( url() );
          // If we won't be able to list it (e.g. http), then don't try :)
          if ( KProtocolInfo::supportsListing( u ) )
              dlg->setSelection( u.url() );
      }

      if ( dlg->exec() != TQDialog::Accepted )
      {
          return;
      }

      newurl = dlg->selectedURL();
    }

    setKURL( newurl );
    emit urlSelected( d->url() );
}

void KURLRequester::setMode(uint mode)
{
    Q_ASSERT( (mode & KFile::Files) == 0 );
    d->fileDialogMode = mode;
    if ( (mode & KFile::Directory) && !(mode & KFile::File) )
        myCompletion->setMode( KURLCompletion::DirCompletion );

    if (myFileDialog)
       myFileDialog->setMode( d->fileDialogMode );
}

unsigned int KURLRequester::mode() const
{
    return d->fileDialogMode;
}

void KURLRequester::setFilter(const TQString &filter)
{
    d->fileDialogFilter = filter;
    if (myFileDialog)
       myFileDialog->setFilter( d->fileDialogFilter );
}

TQString KURLRequester::filter( ) const
{
    return d->fileDialogFilter;
}


KFileDialog * KURLRequester::fileDialog() const
{
    if ( !myFileDialog ) {
        TQWidget *p = parentWidget();
        myFileDialog = new KFileDialog( TQString::null, d->fileDialogFilter, p,
                                        "file dialog", true );

        myFileDialog->setMode( d->fileDialogMode );
        myFileDialog->setCaption( caption() );
    }

    return myFileDialog;
}


void KURLRequester::setShowLocalProtocol( bool b )
{
    if ( myShowLocalProt == b )
	return;

    myShowLocalProt = b;
    setKURL( url() );
}

void KURLRequester::clear()
{
    d->setText( TQString::null );
}

KLineEdit * KURLRequester::lineEdit() const
{
    return d->edit;
}

KComboBox * KURLRequester::comboBox() const
{
    return d->combo;
}

void KURLRequester::slotUpdateURL()
{
    // bin compat, myButton is declared as QPushButton
    KURL u;
    u = KURL( KURL( TQDir::currentDirPath() + '/' ), url() );
    (static_cast<KURLDragPushButton *>( myButton ))->setURL( u );
}

bool KURLRequester::eventFilter( TQObject *obj, TQEvent *ev )
{
    if ( ( TQT_BASE_OBJECT(d->edit) == TQT_BASE_OBJECT(obj) ) || ( TQT_BASE_OBJECT(d->combo) == TQT_BASE_OBJECT(obj) ) )
    {
        if (( ev->type() == TQEvent::FocusIn ) || ( ev->type() == TQEvent::FocusOut ))
            // Forward focusin/focusout events to the urlrequester; needed by file form element in tdehtml
            TQApplication::sendEvent( this, ev );
    }
    return TQWidget::eventFilter( obj, ev );
}

KPushButton * KURLRequester::button() const
{
    return myButton;
}

KEditListBox::CustomEditor KURLRequester::customEditor()
{
    setSizePolicy(TQSizePolicy( TQSizePolicy::Preferred,
                               TQSizePolicy::Fixed));

    KLineEdit *edit = d->edit;
    if ( !edit && d->combo )
        edit = dynamic_cast<KLineEdit*>( d->combo->lineEdit() );

#ifndef NDEBUG
    if ( !edit )
        kdWarning() << "KURLRequester's lineedit is not a KLineEdit!??\n";
#endif

    KEditListBox::CustomEditor editor( this, edit );
    return editor;
}

void KURLRequester::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

KURLComboRequester::KURLComboRequester( TQWidget *parent,
			      const char *name )
  : KURLRequester( new KComboBox(false), parent, name)
{
}

#include "kurlrequester.moc"