~ubuntu-branches/ubuntu/breezy/koffice/breezy-security

« back to all changes in this revision

Viewing changes to kexi/main/startup/KexiStartupFileDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-10-11 14:49:50 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051011144950-lwpngbifzp8nk0ds
Tags: 1:1.4.1-0ubuntu7
* SECURITY UPDATE: fix heap based buffer overflow in the RTF importer of KWord
* Opening specially crafted RTF files in KWord can cause
  execution of abitrary code.
* Add kubuntu_01_rtfimport_heap_overflow.diff
* References:
  CAN-2005-2971
  CESA-2005-005
  http://www.koffice.org/security/advisory-20051011-1.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "KexiStartupFileDialog.h"
 
21
 
 
22
#include <kexidb/driver.h>
 
23
#include <core/kexi.h>
 
24
 
 
25
#include <qlayout.h>
 
26
#include <qobjectlist.h>
 
27
#include <qpushbutton.h>
 
28
#include <qapplication.h>
 
29
 
 
30
#include <kmessagebox.h>
 
31
#include <klocale.h>
 
32
#include <kdebug.h>
 
33
#include <kmimetype.h>
 
34
#include <kfile.h>
 
35
#include <kurlcombobox.h>
 
36
 
 
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)
 
42
{
 
43
        setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
 
44
        setMode( mode );
 
45
        
 
46
        QPoint point( 0, 0 );
 
47
        reparent( parentWidget(), point );
 
48
 
 
49
        if (layout())
 
50
                layout()->setMargin(0);
 
51
        setMinimumHeight(100);
 
52
        setSizeGripEnabled ( FALSE );
 
53
 
 
54
        //dirty hack to customize filedialog view:
 
55
        {
 
56
                QObjectList *l = queryList( "QPushButton" );
 
57
                QObjectListIt it( *l );
 
58
                QObject *obj;
 
59
                while ( (obj = it.current()) != 0 ) {
 
60
                        ++it;
 
61
                        static_cast<QPushButton*>(obj)->hide();
 
62
                }
 
63
                delete l;
 
64
        }
 
65
        {
 
66
                QObjectList *l = queryList("QWidget");
 
67
                QObjectListIt it( *l );
 
68
                QObject *obj;
 
69
                while ( (obj = it.current()) != 0 ) {
 
70
                        ++it;
 
71
                        static_cast<QPushButton*>(obj)->installEventFilter(this);
 
72
                }
 
73
                delete l;
 
74
        }       
 
75
        
 
76
#ifndef Q_WS_WIN
 
77
        toggleSpeedbar(false);
 
78
        setFocusProxy( locationEdit );//locationWidget() );
 
79
#endif
 
80
 
 
81
}
 
82
        
 
83
void KexiStartupFileDialog::setMode(KexiStartupFileDialog::Mode mode, 
 
84
        const QStringList &additionalMimeTypes)
 
85
{
 
86
        m_mode = mode;
 
87
        clearFilter();
 
88
        
 
89
        QString filter;
 
90
        KMimeType::Ptr mime;
 
91
        QStringList allfilters;
 
92
        
 
93
        if (m_mode == KexiStartupFileDialog::Opening 
 
94
                || m_mode == KexiStartupFileDialog::SavingFileBasedDB) {
 
95
                mime = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() );
 
96
                if (mime) {
 
97
                        filter += fileDialogFilterString(mime);
 
98
//                      filter += mime->patterns().join(" ") + "|" + mime->comment() 
 
99
//                                      + " ("+mime->patterns().join(" ")+")\n";
 
100
                        allfilters += mime->patterns().join(" ");
 
101
                }
 
102
        }
 
103
#ifdef KEXI_SERVER_SUPPORT
 
104
        if (m_mode == KexiStartupFileDialog::Opening 
 
105
                || m_mode == KexiStartupFileDialog::SavingServerBasedDB) {
 
106
                mime = KMimeType::mimeType("application/x-kexiproject-shortcut");
 
107
                if (mime) {
 
108
//                      filter += mime->patterns().join(" ") + "|" + mime->comment() 
 
109
//                              + " ("+mime->patterns().join(" ")+")\n";
 
110
                        filter += fileDialogFilterString(mime);
 
111
//                      allfilters += mime->patterns().join(" ");
 
112
                }
 
113
        }
 
114
#endif
 
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);
 
120
        }
 
121
 
 
122
        filter += fileDialogFilterString("all/allfiles");
 
123
//      mime = KMimeType::mimeType("all/allfiles");
 
124
//      if (mime) {
 
125
//              filter += QString(mime->patterns().isEmpty() ? "*" : mime->patterns().join(" ")) 
 
126
//                      + "|" + mime->comment()+ " (*)\n";
 
127
//      }
 
128
        
 
129
        if (allfilters.count()>1) {//prepend "all supoported files" entry
 
130
                filter = allfilters.join(" ")+"|" + i18n("All Kexi Files")+" ("+allfilters.join(" ")+")\n" + filter;
 
131
        }
 
132
        
 
133
        if (filter.right(1)=="\n")
 
134
                filter.truncate(filter.length()-1);
 
135
        setFilter(filter);
 
136
        
 
137
        if (m_mode == KexiStartupFileDialog::Opening) {
 
138
                KexiStartupFileDialogBase::setMode( KFile::ExistingOnly | KFile::LocalOnly | KFile::File );
 
139
                setOperationMode( KFileDialog::Opening );
 
140
        } else {
 
141
                KexiStartupFileDialogBase::setMode( KFile::LocalOnly | KFile::File );
 
142
                setOperationMode( KFileDialog::Saving );
 
143
        }
 
144
}
 
145
 
 
146
void KexiStartupFileDialog::show()
 
147
{
 
148
        m_lastFileName = QString::null;
 
149
//      m_lastUrl = KURL();
 
150
        KexiStartupFileDialogBase::show();
 
151
}
 
152
 
 
153
//KURL KexiStartupFileDialog::currentURL()
 
154
QString KexiStartupFileDialog::currentFileName()
 
155
{
 
156
        setResult( QDialog::Accepted ); // selectedURL tests for it
 
157
        
 
158
#ifdef Q_WS_WIN
 
159
//      QString path = selectedFile();
 
160
        //js @todo
 
161
//      kdDebug() << "selectedFile() == " << path << " '" << url().fileName() << "' " << m_lineEdit->text() << endl;
 
162
        QString path = dir()->absPath();
 
163
        if (!path.endsWith("/") && !path.endsWith("\\"))
 
164
                path.append("/");
 
165
  path += m_lineEdit->text();
 
166
//      QString path = QFileInfo(selectedFile()).dirPath(true) + "/" + m_lineEdit->text();
 
167
#else
 
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() );
 
174
        
 
175
        path=KexiStartupFileDialogBase::selectedURL().path();
 
176
        kdDebug() << "selectedURL() == " << path <<endl;
 
177
        
 
178
#endif
 
179
        
 
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 );
 
189
                        }
 
190
                }
 
191
        }
 
192
        kdDebug() << "KexiStartupFileDialog::currentFileName() == " << path <<endl;
 
193
        return path;
 
194
//      return KFileDialog::selectedURL();
 
195
}
 
196
 
 
197
//bool KexiStartupFileDialog::checkURL()
 
198
bool KexiStartupFileDialog::checkFileName()
 
199
{
 
200
//      KURL url = currentURL();
 
201
//      QString path = url.path().stripWhiteSpace();
 
202
        QString path = currentFileName().stripWhiteSpace();
 
203
        
 
204
//      if (url.fileName().stripWhiteSpace().isEmpty()) {
 
205
        if (path.isEmpty()) {
 
206
                KMessageBox::error( this, i18n( "Enter a filename" ));
 
207
                return false;
 
208
        }
 
209
        
 
210
        kdDebug() << "KexiStartupFileDialog::checkURL() path: " << path  << endl;
 
211
//      kdDebug() << "KexiStartupFileDialog::checkURL() fname: " << url.fileName() << endl;
 
212
//todo  if ( url.isLocalFile() ) {
 
213
                QFileInfo fi(path);
 
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) ) );
 
218
                                return false;
 
219
                        }
 
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) ) );
 
223
                                return false;
 
224
                        }
 
225
                }
 
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) ) )) {
 
229
                                return false;
 
230
                        }
 
231
                }
 
232
//      }
 
233
        return true;
 
234
}
 
235
 
 
236
void KexiStartupFileDialog::accept()
 
237
{
 
238
//      locationEdit->setFocus();
 
239
//      QKeyEvent ev(QEvent::KeyPress, Qt::Key_Enter, '\n', 0);
 
240
//      QApplication::sendEvent(locationEdit, &ev);
 
241
//      QApplication::postEvent(locationEdit, &ev);
 
242
        
 
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)
 
246
//              m_lastUrl=KURL();
 
247
                m_lastFileName=QString::null;
 
248
                kdDebug() << "m_lastFileName==currentFileName()" << endl;
 
249
#ifdef Q_WS_WIN
 
250
                return;
 
251
#endif
 
252
        }
 
253
//      kdDebug() << "KexiStartupFileDialog::accept(): url = " << currentURL().path() << " " << endl;
 
254
        kdDebug() << "KexiStartupFileDialog::accept(): path = " << currentFileName() << endl;
 
255
//      if ( checkURL() ) {
 
256
        if ( checkFileName() ) {
 
257
                emit accepted();
 
258
        }
 
259
//      else {
 
260
//              m_lastUrl = KURL();
 
261
//      }
 
262
//      m_lastUrl = currentURL();
 
263
        m_lastFileName = currentFileName();
 
264
}
 
265
 
 
266
void KexiStartupFileDialog::reject()
 
267
{
 
268
        kdDebug() << "KexiStartupFileDialog: reject!" << endl;
 
269
        emit rejected();
 
270
}
 
271
 
 
272
/*#ifndef Q_WS_WIN
 
273
KURLComboBox *KexiStartupFileDialog::locationWidget() const
 
274
{
 
275
        return locationEdit;
 
276
}
 
277
#endif
 
278
*/
 
279
 
 
280
void KexiStartupFileDialog::setLocationText(const QString& fn)
 
281
{
 
282
#ifdef Q_WS_WIN
 
283
        //js @todo
 
284
        setSelection(fn);
 
285
#else
 
286
        setSelection(fn);
 
287
//      locationEdit->setCurrentText(fn);
 
288
//      locationEdit->lineEdit()->setEdited( true );
 
289
//      setSelection(fn);
 
290
#endif
 
291
}
 
292
 
 
293
void KexiStartupFileDialog::setFocus()
 
294
{
 
295
#ifdef Q_WS_WIN
 
296
        m_lineEdit->setFocus();
 
297
#else
 
298
        locationEdit->setFocus();
 
299
#endif
 
300
}
 
301
 
 
302
bool KexiStartupFileDialog::eventFilter ( QObject * watched, QEvent * e )
 
303
{
 
304
        //filter-out ESC key
 
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();
 
308
                emit rejected();
 
309
                return true;
 
310
        }
 
311
        return KexiStartupFileDialogBase::eventFilter(watched,e);
 
312
 
313
 
 
314
#include "KexiStartupFileDialog.moc"
 
315