kexi

KexiStartupFileDialog_win.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
24
25#include "KexiStartupFileDialog.h"
26#include <kexiutils/utils.h>
27
28#include <kiconloader.h>
29#include <kdebug.h>
30#include <tderecentdirs.h>
31
32#include <tqobjectlist.h>
33#include <tqlineedit.h>
34
35#include <win/win32_utils.h>
36
38class KexiStartupFileDialogBasePrivate
39{
40 public:
41 KexiStartupFileDialogBasePrivate()
42 {}
43 KFile::Mode mode;
44 TQString kde_filters;
45 TQStringList mimetypes;
46};
47
48KexiStartupFileDialogBase::KexiStartupFileDialogBase(
49 const TQString & dirName, const TQString & filter,
50 TQWidget * parent, const char * name, bool modal )
51 : TQFileDialog( realStartDir(dirName), filter, parent, name, modal )
52 , d(new KexiStartupFileDialogBasePrivate())
53{
54// TQString _dirName = dirName;
55 TQString _dirName = dirPath();
56 //make default 'My Documents' folder
57//TODO: store changes in the app's config file?
58 if (_dirName.isEmpty())
59 _dirName = TDEGlobalSettings::documentPath();
60
61 init(_dirName, filter, parent);
62
63 //find "OK" button
64 TQObjectList *l = queryList( "TQPushButton", "OK", false );
65 m_okBtn = dynamic_cast<TQPushButton*>(l->first());
66 delete l;
67 l = queryList( "TQLineEdit", "name/filter editor", false );
68 m_lineEdit = dynamic_cast<TQLineEdit*>(l->first());
69 delete l;
70
71 adjustSize();
72}
73
74KexiStartupFileDialogBase::~KexiStartupFileDialogBase()
75{
76}
77
78void KexiStartupFileDialogBase::init(const TQString& startDir, const TQString& filter, TQWidget* widget)
79{
80//TODO initStatic();
81//TODO d = new KFileDialogPrivate();
82
83//(js) d->boxLayout = 0;
84//TODO d->keepLocation = false;
85//TODO d->operationMode = Opening;
86 setMode(KFile::File | KFile::ExistingOnly); //(js) default: open action
87 setIcon( TDEGlobal::iconLoader()->loadIcon("document-open", TDEIcon::Desktop) );
88 setDir(TQDir(startDir));
89//TODO d->hasDefaultFilter = false;
90//TODO d->hasView = false;
91//(js) d->mainWidget = new TQWidget( this, "KFileDialog::mainWidget");
92//(js) setMainWidget( d->mainWidget );
93//(js) d->okButton = new KPushButton( KStdGuiItem::ok(), d->mainWidget );
94//(js) d->okButton->setDefault( true );
95//(js) d->cancelButton = new KPushButton(KStdGuiItem::cancel(), d->mainWidget);
96//(js) connect( d->okButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotOk() ));
97//(js) connect( d->cancelButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotCancel() ));
98//(js) d->customWidget = widget;
99//(js) d->autoSelectExtCheckBox = 0; // delayed loading
100//TODO d->autoSelectExtChecked = false;
101//(js) d->urlBar = 0; // delayed loading
102//TODO TDEConfig *config = TDEGlobal::config();
103//TODO TDEConfigGroupSaver cs( config, ConfigGroup );
104//TODO d->initializeSpeedbar = config->readBoolEntry( "Set speedbar defaults",
105//TODO true );
106//TODO d->completionLock = false;
107
108//TODO TQtMsgHandler oldHandler = qInstallMsgHandler( silenceTQToolBar );
109//TODO toolbar = 0; //(js)
110//(js) toolbar = new TDEToolBar( d->mainWidget, "KFileDialog::toolbar", true);
111//(js) toolbar->setFlat(true);
112//TODO qInstallMsgHandler( oldHandler );
113
114//(js) d->pathCombo = new KURLComboBox( KURLComboBox::Directories, true,
115//(js) toolbar, "path combo" );
116//(js) TQToolTip::add( d->pathCombo, i18n("Often used directories") );
117//(js) TQWhatsThis::add( d->pathCombo, "<qt>" + i18n("Commonly used locations are listed here. "
118//(js) "This includes standard locations, such as your home directory, as well as "
119//(js) "locations that have been visited recently.") + autocompletionWhatsThisText);
120/*
121 KURL u;
122 u.setPath( TQDir::rootDirPath() );
123 TQString text = i18n("Root Directory: %1").arg( u.path() );
124 d->pathCombo->addDefaultURL( u,
125 KMimeType::pixmapForURL( u, 0, TDEIcon::Small ),
126 text );
127
128 u.setPath( TQDir::homeDirPath() );
129 text = i18n("Home Directory: %1").arg( u.path( +1 ) );
130 d->pathCombo->addDefaultURL( u, KMimeType::pixmapForURL( u, 0, TDEIcon::Small ),
131 text );
132
133 KURL docPath;
134 docPath.setPath( TDEGlobalSettings::documentPath() );
135 if ( u.path(+1) != docPath.path(+1) ) {
136 text = i18n("Documents: %1").arg( docPath.path( +1 ) );
137 d->pathCombo->addDefaultURL( u,
138 KMimeType::pixmapForURL( u, 0, TDEIcon::Small ),
139 text );
140 }
141
142 u.setPath( TDEGlobalSettings::desktopPath() );
143 text = i18n("Desktop: %1").arg( u.path( +1 ) );
144 d->pathCombo->addDefaultURL( u,
145 KMimeType::pixmapForURL( u, 0, TDEIcon::Small ),
146 text );
147
148 u.setPath( "/tmp" );
149
150 d->url = getStartURL( startDir, d->fileClass );
151 d->selection = d->url.url();
152
153 // If local, check it exists. If not, go up until it exists.
154 if ( d->url.isLocalFile() )
155 {
156 if ( !TQFile::exists( d->url.path() ) )
157 {
158 d->url = d->url.upURL();
159 TQDir dir( d->url.path() );
160 while ( !dir.exists() )
161 {
162 d->url = d->url.upURL();
163 dir.setPath( d->url.path() );
164 }
165 }
166 }
167
168 ops = new KDirOperator(d->url, d->mainWidget, "KFileDialog::ops");
169 ops->setOnlyDoubleClickSelectsFiles( true );
170 connect(ops, TQ_SIGNAL(urlEntered(const KURL&)),
171 TQ_SLOT(urlEntered(const KURL&)));
172 connect(ops, TQ_SIGNAL(fileHighlighted(const KFileItem *)),
173 TQ_SLOT(fileHighlighted(const KFileItem *)));
174 connect(ops, TQ_SIGNAL(fileSelected(const KFileItem *)),
175 TQ_SLOT(fileSelected(const KFileItem *)));
176 connect(ops, TQ_SIGNAL(finishedLoading()),
177 TQ_SLOT(slotLoadingFinished()));
178
179 ops->setupMenu(KDirOperator::SortActions |
180 KDirOperator::FileActions |
181 KDirOperator::ViewActions);
182 TDEActionCollection *coll = ops->actionCollection();
183
184 // plug nav items into the toolbar
185 coll->action( "up" )->plug( toolbar );
186 coll->action( "up" )->setWhatsThis(i18n("<qt>Click this button to enter the parent directory.<p>"
187 "For instance, if the current location is file:/home/%1 clicking this "
188 "button will take you to file:/home.</qt>").arg(getlogin()));
189 coll->action( "back" )->plug( toolbar );
190 coll->action( "back" )->setWhatsThis(i18n("Click this button to move backwards one step in the browsing history."));
191 coll->action( "forward" )->plug( toolbar );
192 coll->action( "forward" )->setWhatsThis(i18n("Click this button to move forward one step in the browsing history."));
193 coll->action( "reload" )->plug( toolbar );
194 coll->action( "reload" )->setWhatsThis(i18n("Click this button to reload the contents of the current location."));
195 coll->action( "mkdir" )->setShortcut(Key_F10);
196 coll->action( "mkdir" )->plug( toolbar );
197 coll->action( "mkdir" )->setWhatsThis(i18n("Click this button to create a new directory."));
198
199 d->bookmarkHandler = new KFileBookmarkHandler( this );
200 toolbar->insertButton(TQString::fromLatin1("bookmark"),
201 (int)HOTLIST_BUTTON, true,
202 i18n("Bookmarks"));
203 toolbar->getButton(HOTLIST_BUTTON)->setPopup( d->bookmarkHandler->menu(),
204 true);
205 TQWhatsThis::add(toolbar->getButton(HOTLIST_BUTTON),
206 i18n("<qt>This button allows you to bookmark specific locations. "
207 "Click on this button to open the bookmark menu where you may add, "
208 "edit or select a bookmark.<p>"
209 "These bookmarks are specific to the file dialog, but otherwise operate "
210 "like bookmarks elsewhere in TDE.</qt>"));
211 connect( d->bookmarkHandler, TQ_SIGNAL( openURL( const TQString& )),
212 TQ_SLOT( enterURL( const TQString& )));
213
214 TDEToggleAction *showSidebarAction =
215 new TDEToggleAction(i18n("Show Quick Access Navigation Panel"), Key_F9, coll,"toggleSpeedbar");
216 connect( showSidebarAction, TQ_SIGNAL( toggled( bool ) ),
217 TQ_SLOT( toggleSpeedbar( bool )) );
218
219 TDEActionMenu *menu = new TDEActionMenu( i18n("Configure"), "configure", this, "extra menu" );
220 menu->setWhatsThis(i18n("<qt>This is the configuration menu for the file dialog. "
221 "Various options can be accessed from this menu including: <ul>"
222 "<li>how files are sorted in the list</li>"
223 "<li>types of view, including icon and list</li>"
224 "<li>showing of hidden files</li>"
225 "<li>the Quick Access navigation panel</li>"
226 "<li>file previews</li>"
227 "<li>separating directories from files</li></ul></qt>"));
228 menu->insert( coll->action( "sorting menu" ));
229 menu->insert( coll->action( "separator" ));
230 coll->action( "short view" )->setShortcut(Key_F6);
231 menu->insert( coll->action( "short view" ));
232 coll->action( "detailed view" )->setShortcut(Key_F7);
233 menu->insert( coll->action( "detailed view" ));
234 menu->insert( coll->action( "separator" ));
235 coll->action( "show hidden" )->setShortcut(Key_F8);
236 menu->insert( coll->action( "show hidden" ));
237 menu->insert( showSidebarAction );
238 coll->action( "preview" )->setShortcut(Key_F11);
239 menu->insert( coll->action( "preview" ));
240 coll->action( "separate dirs" )->setShortcut(Key_F12);
241 menu->insert( coll->action( "separate dirs" ));
242
243 menu->setDelayed( false );
244 connect( menu->popupMenu(), TQ_SIGNAL( aboutToShow() ),
245 ops, TQ_SLOT( updateSelectionDependentActions() ));
246 menu->plug( toolbar );
247*/
248 /*
249 * ugly little hack to have a 5 pixel space between the buttons
250 * and the combo box
251 */
252/* TQWidget *spacerWidget = new TQWidget(toolbar);
253//(js) spacerWidget->setMinimumWidth(spacingHint());
254//(js) spacerWidget->setMaximumWidth(spacingHint());
255 d->m_pathComboIndex = toolbar->insertWidget(-1, -1, spacerWidget);
256 toolbar->insertWidget(PATH_COMBO, 0, d->pathCombo);
257
258
259 toolbar->setItemAutoSized (PATH_COMBO);
260 toolbar->setIconText(TDEToolBar::IconOnly);
261 toolbar->setBarPos(TDEToolBar::Top);
262 toolbar->setMovingEnabled(false);
263 toolbar->adjustSize();
264
265 d->pathCombo->setCompletionObject( ops->dirCompletionObject(), false );
266
267 connect( d->pathCombo, TQ_SIGNAL( urlActivated( const KURL& )),
268 this, TQ_SLOT( enterURL( const KURL& ) ));
269 connect( d->pathCombo, TQ_SIGNAL( returnPressed( const TQString& )),
270 this, TQ_SLOT( enterURL( const TQString& ) ));
271 connect( d->pathCombo, TQ_SIGNAL(textChanged( const TQString& )),
272 TQ_SLOT( pathComboChanged( const TQString& ) ));
273 connect( d->pathCombo, TQ_SIGNAL( completion( const TQString& )),
274 TQ_SLOT( dirCompletion( const TQString& )));
275 connect( d->pathCombo, TQ_SIGNAL( textRotation(TDECompletionBase::KeyBindingType) ),
276 d->pathCombo, TQ_SLOT( rotateText(TDECompletionBase::KeyBindingType) ));
277
278 TQString whatsThisText;
279
280 // the Location label/edit
281 d->locationLabel = new TQLabel(i18n("&Location:"), d->mainWidget);
282 locationEdit = new KURLComboBox(KURLComboBox::Files, true,
283 d->mainWidget, "LocationEdit");
284 updateLocationWhatsThis ();
285 d->locationLabel->setBuddy(locationEdit);
286
287 // to get the completionbox-signals connected:
288 locationEdit->setHandleSignals( true );
289 (void) locationEdit->completionBox();
290
291 locationEdit->setFocus();
292// locationEdit->setCompletionObject( new KURLCompletion() );
293// locationEdit->setAutoDeleteCompletionObject( true );
294 locationEdit->setCompletionObject( ops->completionObject(), false );
295
296 connect( locationEdit, TQ_SIGNAL( returnPressed() ),
297 this, TQ_SLOT( slotOk()));
298 connect(locationEdit, TQ_SIGNAL( activated( const TQString& )),
299 this, TQ_SLOT( locationActivated( const TQString& ) ));
300 connect( locationEdit, TQ_SIGNAL( completion( const TQString& )),
301 TQ_SLOT( fileCompletion( const TQString& )));
302 connect( locationEdit, TQ_SIGNAL( textRotation(TDECompletionBase::KeyBindingType) ),
303 locationEdit, TQ_SLOT( rotateText(TDECompletionBase::KeyBindingType) ));
304
305 // the Filter label/edit
306 whatsThisText = i18n("<qt>This is the filter to apply to the file list. "
307 "File names that do not match the filter will not be shown.<p>"
308 "You may select from one of the preset filters in the "
309 "drop down menu, or you may enter a custom filter "
310 "directly into the text area.<p>"
311 "Wildcards such as * and ? are allowed.</qt>");
312 d->filterLabel = new TQLabel(i18n("&Filter:"), d->mainWidget);
313 TQWhatsThis::add(d->filterLabel, whatsThisText);
314 filterWidget = new KFileFilterCombo(d->mainWidget,
315 "KFileDialog::filterwidget");
316 TQWhatsThis::add(filterWidget, whatsThisText);
317 setFilter(filter);
318 d->filterLabel->setBuddy(filterWidget);
319 connect(filterWidget, TQ_SIGNAL(filterChanged()), TQ_SLOT(slotFilterChanged()));
320
321 // the Automatically Select Extension checkbox
322 // (the text, visibility etc. is set in updateAutoSelectExtension(), which is called by readConfig())
323 d->autoSelectExtCheckBox = new TQCheckBox (d->mainWidget);
324 connect(d->autoSelectExtCheckBox, TQ_SIGNAL(clicked()), TQ_SLOT(slotAutoSelectExtClicked()));
325
326 initGUI(); // activate GM
327
328 readRecentFiles( config );
329
330 adjustSize();
331
332 // we set the completionLock to avoid entering pathComboChanged() when
333 // inserting the list of URLs into the combo.
334 d->completionLock = true;
335 ops->setViewConfig( config, ConfigGroup );
336 readConfig( config, ConfigGroup );
337 setSelection(d->selection);
338 d->completionLock = false;
339 */
340}
341
342void KexiStartupFileDialogBase::clearFilter()
343{
344 d->kde_filters = "";//(js)
345 TQFileDialog::setFilter(""); //(js);
346//todo d->mimetypes.clear();
347//todo d->hasDefaultFilter = false;
348
349 updateAutoSelectExtension ();
350}
351
352KFile::Mode KexiStartupFileDialogBase::mode() const
353{
354 return d->mode;
355}
356
357void KexiStartupFileDialogBase::setMode( KFile::Mode m )
358{
359 //(js) translate mode for TQFileDialog
360 d->mode = m;
361 TQFileDialog::Mode qm = (TQFileDialog::Mode)0;
362 if (m & KFile::File) qm = Mode(qm | TQFileDialog::AnyFile);
363 else if (m & KFile::Directory) qm = Mode(qm | TQFileDialog::DirectoryOnly);
364 if (m & KFile::Files) qm = Mode(qm | TQFileDialog::ExistingFiles);
365 if (m & KFile::ExistingOnly) qm = Mode(qm | TQFileDialog::ExistingFile);
366
367 TQFileDialog::setMode( qm );
368/*(js) ops->setMode(m);
369 if ( ops->dirOnlyMode() ) {
370//(js) filterWidget->setDefaultFilter( i18n("*|All Directories") );
371 }
372 else {
373//(js) filterWidget->setDefaultFilter( i18n("*|All Files") );
374 }
375
376 updateAutoSelectExtension ();*/
377}
378
379void KexiStartupFileDialogBase::setMode( unsigned int m )
380{
381 setMode(static_cast<KFile::Mode>( m ));
382}
383
384void KexiStartupFileDialogBase::setOperationMode( KFileDialog::OperationMode mode )
385{
386// d->operationMode = mode;
387 // d->keepLocation = (mode == Saving);
388 if (mode == KFileDialog::Saving) {
389 setMode( KFile::File );
390 setIcon( TDEGlobal::iconLoader()->loadIcon("document-save", TDEIcon::Desktop) );
391 }
392//(js) filterWidget->setEditable( !d->hasDefaultFilter || mode != Saving );
393//(js) d->okButton->setGuiItem( (mode == Saving) ? KStdGuiItem::save() : KStdGuiItem::ok() );
394//TODO updateLocationWhatsThis ();
395 updateAutoSelectExtension ();
396}
397
398TQString KexiStartupFileDialogBase::currentFilter() const
399{
400 //(js)filterWidget->currentFilter();
401
402 //we need to convert TQt filter format to TDE format
403 //TQt format: "some text (*.first *.second)" or "All (*)"
404 //TDE format: "*.first *.second" or "*"
405 TQString f = selectedFilter();
406 if (f.find('(')!=-1)
407 f = f.mid(f.find('(')+1);
408 if (f.mid(f.find(')')!=-1))
409 f = f.left(f.find(')'));
410 return f;
411}
412
413void KexiStartupFileDialogBase::setFilter(const TQString& filter)
414{
415 d->kde_filters = filter;
416 int pos = d->kde_filters.find('/');
417
418 // Check for an un-escaped '/', if found
419 // interpret as a MIME filter.
420
421 if (pos > 0 && filter[pos - 1] != '\\') {
422 TQStringList filters = TQStringList::split( " ", d->kde_filters );
423 setMimeFilter( filters );
424 return;
425 }
426 TQFileDialog::setFilters( convertKFileDialogFilterToTQFileDialogFilter(filter) );
427 //</js>
428//(js) ops->clearFilter();
429//(js) filterWidget->setFilter(copy);
430//(js) ops->setNameFilter(filterWidget->currentFilter());
431//(js) d->hasDefaultFilter = false;
432//(js) filterWidget->setEditable( true );
433
434 updateAutoSelectExtension ();
435}
436
437void KexiStartupFileDialogBase::setMimeFilter( const TQStringList& mimeTypes,
438 const TQString& defaultType )
439{
440 d->mimetypes = mimeTypes;
441//(js) filterWidget->setMimeFilter( mimeTypes, defaultType );
442
443//(js) TQStringList types = TQStringList::split(" ", filterWidget->currentFilter());
444//(js) types.append( TQString::fromLatin1( "inode/directory" ));
445//(js) ops->clearFilter();
446//(js) ops->setMimeFilter( types );
447//(js) d->hasDefaultFilter = !defaultType.isEmpty();
448//(js) filterWidget->setEditable( !d->hasDefaultFilter ||
449//(js) d->operationMode != Saving );
450
451//TODO updateAutoSelectExtension ();
452}
453
454TQString KexiStartupFileDialogBase::realStartDir(const TQString& startDir)
455{
456 if (!startDir.startsWith(":"))
457 return startDir;
458 TQString recentDir; //dummy
459 TQString path( KFileDialog::getStartURL(startDir, recentDir).path() );
460 if (path.isEmpty())
461 return TQString();
462 TQFileInfo fi(path);
463 return fi.isDir() ? fi.absFilePath() : fi.dir(true).absPath();
464}
465
466void KexiStartupFileDialogBase::saveLastVisitedPath(const TQString& path)
467{
468 if (!m_lastVisitedPathsVariable.isEmpty()) {
469 //save last visited dir path
470// TQString dir = TQDir(path).absPath();
471 TQFileInfo fi(path);
472 TQString dir( fi.isDir() ? fi.absFilePath() : fi.dir(true).absPath() );
473 if (!dir.isEmpty())
474 TDERecentDirs::add(m_lastVisitedPathsVariable, dir);
475 }
476}
TQString m_lastVisitedPathsVariable
Used by win32;.
TQString realStartDir(const TQString &startDir)