1
/* This file is part of the KDE project
2
Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
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.
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.
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., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
20
#include "KexiStartupFileDialog.h"
22
#include <kexidb/driver.h>
23
#include <core/kexi.h>
26
#include <qobjectlist.h>
27
#include <qpushbutton.h>
28
#include <qapplication.h>
30
#include <kmessagebox.h>
33
#include <kmimetype.h>
35
#include <kurlcombobox.h>
37
KexiStartupFileDialog::KexiStartupFileDialog(
38
const QString& startDir, Mode mode,
39
QWidget *parent, const char *name)
40
: KexiStartupFileDialogBase(startDir, "", parent, name, 0)
41
, m_confirmOverwrites(true)
43
setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
47
reparent( parentWidget(), point );
50
layout()->setMargin(0);
51
setMinimumHeight(100);
52
setSizeGripEnabled ( FALSE );
54
//dirty hack to customize filedialog view:
56
QObjectList *l = queryList( "QPushButton" );
57
QObjectListIt it( *l );
59
while ( (obj = it.current()) != 0 ) {
61
static_cast<QPushButton*>(obj)->hide();
66
QObjectList *l = queryList("QWidget");
67
QObjectListIt it( *l );
69
while ( (obj = it.current()) != 0 ) {
71
static_cast<QPushButton*>(obj)->installEventFilter(this);
77
toggleSpeedbar(false);
78
setFocusProxy( locationEdit );//locationWidget() );
83
void KexiStartupFileDialog::setMode(KexiStartupFileDialog::Mode mode,
84
const QStringList &additionalMimeTypes)
91
QStringList allfilters;
93
if (m_mode == KexiStartupFileDialog::Opening
94
|| m_mode == KexiStartupFileDialog::SavingFileBasedDB) {
95
mime = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() );
97
filter += fileDialogFilterString(mime);
98
// filter += mime->patterns().join(" ") + "|" + mime->comment()
99
// + " ("+mime->patterns().join(" ")+")\n";
100
allfilters += mime->patterns().join(" ");
103
#ifdef KEXI_SERVER_SUPPORT
104
if (m_mode == KexiStartupFileDialog::Opening
105
|| m_mode == KexiStartupFileDialog::SavingServerBasedDB) {
106
mime = KMimeType::mimeType("application/x-kexiproject-shortcut");
108
// filter += mime->patterns().join(" ") + "|" + mime->comment()
109
// + " ("+mime->patterns().join(" ")+")\n";
110
filter += fileDialogFilterString(mime);
111
// allfilters += mime->patterns().join(" ");
115
foreach (QStringList::ConstIterator, it, additionalMimeTypes) {
116
// mime = KMimeType::mimeType(*it);
117
// filter += mime->patterns().join(" ") + "|" + mime->comment() + " ("
118
// + mime->patterns().join(" ")+")\n";
119
filter += fileDialogFilterString(*it);
122
filter += fileDialogFilterString("all/allfiles");
123
// mime = KMimeType::mimeType("all/allfiles");
125
// filter += QString(mime->patterns().isEmpty() ? "*" : mime->patterns().join(" "))
126
// + "|" + mime->comment()+ " (*)\n";
129
if (allfilters.count()>1) {//prepend "all supoported files" entry
130
filter = allfilters.join(" ")+"|" + i18n("All Kexi Files")+" ("+allfilters.join(" ")+")\n" + filter;
133
if (filter.right(1)=="\n")
134
filter.truncate(filter.length()-1);
137
if (m_mode == KexiStartupFileDialog::Opening) {
138
KexiStartupFileDialogBase::setMode( KFile::ExistingOnly | KFile::LocalOnly | KFile::File );
139
setOperationMode( KFileDialog::Opening );
141
KexiStartupFileDialogBase::setMode( KFile::LocalOnly | KFile::File );
142
setOperationMode( KFileDialog::Saving );
146
void KexiStartupFileDialog::show()
148
m_lastFileName = QString::null;
149
// m_lastUrl = KURL();
150
KexiStartupFileDialogBase::show();
153
//KURL KexiStartupFileDialog::currentURL()
154
QString KexiStartupFileDialog::currentFileName()
156
setResult( QDialog::Accepted ); // selectedURL tests for it
159
// QString path = selectedFile();
161
// kdDebug() << "selectedFile() == " << path << " '" << url().fileName() << "' " << m_lineEdit->text() << endl;
162
QString path = dir()->absPath();
163
if (!path.endsWith("/") && !path.endsWith("\\"))
165
path += m_lineEdit->text();
166
// QString path = QFileInfo(selectedFile()).dirPath(true) + "/" + m_lineEdit->text();
168
// QString path = locationEdit->currentText().stripWhiteSpace(); //url.path().stripWhiteSpace(); that does not work, if the full path is not in the location edit !!!!!
169
QString path=KexiStartupFileDialogBase::selectedURL().path();
170
kdDebug() << "prev selectedURL() == " << path <<endl;
171
kdDebug() << "locationEdit == " << locationEdit->currentText().stripWhiteSpace() <<endl;
172
//make sure user-entered path is acceped:
173
setSelection( locationEdit->currentText().stripWhiteSpace() );
175
path=KexiStartupFileDialogBase::selectedURL().path();
176
kdDebug() << "selectedURL() == " << path <<endl;
180
if (!currentFilter().isEmpty()) {
181
if (m_mode == KexiStartupFileDialog::SavingFileBasedDB) {
182
QString filter = QStringList::split(" ", currentFilter()).first().stripWhiteSpace();
183
kdDebug()<< " filter == " << filter <<endl;
184
QString ext = QFileInfo(path).extension(false);
185
if ( !filter.mid(2).isEmpty() && ext!=filter.mid(2) ) {
186
path+=(QString(".")+filter.mid(2));
187
kdDebug() << "KexiStartupFileDialog::checkURL(): append extension, " << path << endl;
188
setSelection( path );
192
kdDebug() << "KexiStartupFileDialog::currentFileName() == " << path <<endl;
194
// return KFileDialog::selectedURL();
197
//bool KexiStartupFileDialog::checkURL()
198
bool KexiStartupFileDialog::checkFileName()
200
// KURL url = currentURL();
201
// QString path = url.path().stripWhiteSpace();
202
QString path = currentFileName().stripWhiteSpace();
204
// if (url.fileName().stripWhiteSpace().isEmpty()) {
205
if (path.isEmpty()) {
206
KMessageBox::error( this, i18n( "Enter a filename" ));
210
kdDebug() << "KexiStartupFileDialog::checkURL() path: " << path << endl;
211
// kdDebug() << "KexiStartupFileDialog::checkURL() fname: " << url.fileName() << endl;
212
//todo if ( url.isLocalFile() ) {
214
if (mode() & KFile::ExistingOnly) {
215
if ( !fi.exists() ) {
216
KMessageBox::error( this, "<qt>"+i18n( "The file \"%1\" doesn't exist." )
217
.arg( QDir::convertSeparators(path) ) );
220
else if ((mode() & KFile::File) && (!fi.isFile() || !fi.isReadable())) {
221
KMessageBox::error( this, "<qt>"+i18n( "The file \"%1\" is not readable." )
222
.arg( QDir::convertSeparators(path) ) );
226
else if (m_confirmOverwrites && fi.exists()) {
227
if (KMessageBox::Yes!=KMessageBox::warningYesNo( this, i18n( "The file \"%1\" already exists.\n"
228
"Do you want to overwrite it?").arg( QDir::convertSeparators(path) ) )) {
236
void KexiStartupFileDialog::accept()
238
// locationEdit->setFocus();
239
// QKeyEvent ev(QEvent::KeyPress, Qt::Key_Enter, '\n', 0);
240
// QApplication::sendEvent(locationEdit, &ev);
241
// QApplication::postEvent(locationEdit, &ev);
243
// kdDebug() << "KexiStartupFileDialog::accept() m_lastUrl == " << m_lastUrl.path() << endl;
244
// if (m_lastUrl.path()==currentURL().path()) {//(js) to prevent more multiple kjob signals (i dont know why this is)
245
if (m_lastFileName==currentFileName()) {//(js) to prevent more multiple kjob signals (i dont know why this is)
247
m_lastFileName=QString::null;
248
kdDebug() << "m_lastFileName==currentFileName()" << endl;
253
// kdDebug() << "KexiStartupFileDialog::accept(): url = " << currentURL().path() << " " << endl;
254
kdDebug() << "KexiStartupFileDialog::accept(): path = " << currentFileName() << endl;
255
// if ( checkURL() ) {
256
if ( checkFileName() ) {
260
// m_lastUrl = KURL();
262
// m_lastUrl = currentURL();
263
m_lastFileName = currentFileName();
266
void KexiStartupFileDialog::reject()
268
kdDebug() << "KexiStartupFileDialog: reject!" << endl;
273
KURLComboBox *KexiStartupFileDialog::locationWidget() const
280
void KexiStartupFileDialog::setLocationText(const QString& fn)
287
// locationEdit->setCurrentText(fn);
288
// locationEdit->lineEdit()->setEdited( true );
293
void KexiStartupFileDialog::setFocus()
296
m_lineEdit->setFocus();
298
locationEdit->setFocus();
302
bool KexiStartupFileDialog::eventFilter ( QObject * watched, QEvent * e )
305
if (e->type()==QEvent::KeyPress && static_cast<QKeyEvent*>(e)->key()==Qt::Key_Escape
306
&& static_cast<QKeyEvent*>(e)->state()==Qt::NoButton) {
307
static_cast<QKeyEvent*>(e)->accept();
311
return KexiStartupFileDialogBase::eventFilter(watched,e);
314
#include "KexiStartupFileDialog.moc"