lib

KoFileDialog.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002-2005 David Faure <faure@kde.org>
3 Copyright (C) 2002-2004 Clarence Dang <dang@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KoFileDialog.h"
22#include "KoDocument.h"
23#include <tdefilefiltercombo.h>
24#include <tdelocale.h>
25#include <tdediroperator.h>
26#include <kdebug.h>
27
28KoFileDialog::KoFileDialog(const TQString& startDir, const TQString& filter,
29 TQWidget *parent, const char *name,
30 bool modal)
31 : KFileDialog( startDir, filter, parent, name, modal )
32{
33 connect( filterWidget, TQ_SIGNAL( activated( int) ),
34 this, TQ_SLOT( slotChangedfilter( int ) ) );
35}
36
37void KoFileDialog::slotChangedfilter( int index )
38{
39 // Switch to "directory selection" mode for SaveAsDirectoryStore,
40 // switch back to "file selection" mode otherwise.
41 KFile::Mode newMode = KFile::File;
42 if ( index >= 1 && index <= (int)m_specialFormats.count()
43 && m_specialFormats[index-1] == KoDocument::SaveAsDirectoryStore ) {
44 newMode = KFile::Directory;
45 }
46 if ( newMode != mode() )
47 {
48 ops->setMode( newMode );
49 updateAutoSelectExtension();
50 }
51}
52
53void KoFileDialog::setSpecialMimeFilter( TQStringList& mimeFilter,
54 const TQString& currentFormat, const int specialOutputFlag,
55 const TQString& nativeFormat,
56 int supportedSpecialFormats )
57{
58 Q_ASSERT( !mimeFilter.isEmpty() );
59 Q_ASSERT( mimeFilter[0] == nativeFormat );
60
61 bool addUncompressed = supportedSpecialFormats & KoDocument::SaveAsDirectoryStore;
62 bool addFlatXML = supportedSpecialFormats & KoDocument::SaveAsFlatXML;
63
64 int idxSpecialOutputFlag = 0;
65 int numSpecialEntries = 0;
66 if ( addUncompressed ) {
67 ++numSpecialEntries;
68 m_specialFormats.append( KoDocument::SaveAsDirectoryStore );
69 if ( specialOutputFlag == KoDocument::SaveAsDirectoryStore )
70 idxSpecialOutputFlag = numSpecialEntries;
71 }
72 if ( addFlatXML ) {
73 ++numSpecialEntries;
74 m_specialFormats.append( KoDocument::SaveAsFlatXML );
75 if ( specialOutputFlag == KoDocument::SaveAsFlatXML )
76 idxSpecialOutputFlag = numSpecialEntries;
77 }
78
79 // Insert numSpecialEntries entries with native mimetypes, for the special entries.
80 TQStringList::Iterator mimeFilterIt = mimeFilter.at( 1 );
81 mimeFilter.insert( mimeFilterIt /* before 1 -> after 0 */, numSpecialEntries, nativeFormat );
82
83 // Fill in filter combo
84 // Note: if currentFormat doesn't exist in mimeFilter, filterWidget
85 // will default to the first item (native format)
86 setMimeFilter( mimeFilter, currentFormat.isEmpty() ? nativeFormat : currentFormat );
87
88 // To get a different description in the combo, we need to change its entries afterwards
89 KMimeType::Ptr type = KMimeType::mimeType( nativeFormat );
90 int idx = 1; // 0 is the native format
91
92 if ( addUncompressed )
93 filterWidget->changeItem( i18n("%1 (Uncompressed XML Files)").arg( type->comment() ), idx++ );
94 if ( addFlatXML )
95 filterWidget->changeItem( i18n("%1 (Flat XML File)").arg( type->comment() ), idx++ );
96 // if you add an entry here, update numSpecialEntries above and specialEntrySelected() below
97
98 // For native format...
99 if (currentFormat == nativeFormat || currentFormat.isEmpty())
100 {
101 // KFileFilterCombo selected the _last_ "native mimetype" entry, select the correct one
102 filterWidget->setCurrentItem( idxSpecialOutputFlag );
103 slotChangedfilter( filterWidget->currentItem() );
104 }
105 // [Mainly KWord] Tell MS Office users that they can save in RTF!
106 int i = 0;
107 for (mimeFilterIt = mimeFilter.begin (); mimeFilterIt != mimeFilter.end (); ++mimeFilterIt, i++)
108 {
109 KMimeType::Ptr mime = KMimeType::mimeType (*mimeFilterIt);
110 TQString compatString = mime->property ("X-TDE-CompatibleApplication").toString ();
111 if (!compatString.isEmpty ())
112 filterWidget->changeItem (i18n ("%1 (%2 Compatible)").arg (mime->comment ()).arg (compatString), i);
113 }
114}
115
116int KoFileDialog::specialEntrySelected()
117{
118 int i = filterWidget->currentItem();
119 // Item 0 is the native format, the following ones are the special formats
120 if ( i >= 1 && i <= (int)m_specialFormats.count() )
121 return m_specialFormats[i-1];
122 return 0;
123}
124
125#include "KoFileDialog.moc"
const char * name(StdAction id)