00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config-kfile.h"
00023
00024 #include "kencodingfiledialog.h"
00025 #include <kcombobox.h>
00026 #include <ktoolbar.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <kcharsets.h>
00030 #include <qtextcodec.h>
00031 #include <kdiroperator.h>
00032 #include <krecentdocument.h>
00033
00034 struct KEncodingFileDialogPrivate
00035 {
00036 KComboBox *encoding;
00037 };
00038
00039 KEncodingFileDialog::KEncodingFileDialog(const QString& startDir, const QString& encoding , const QString& filter,
00040 const QString& caption, KFileDialog::OperationMode type, QWidget *parent, const char* name, bool modal)
00041 : KFileDialog(startDir,filter,parent,name,modal), d(new KEncodingFileDialogPrivate)
00042 {
00043 setCaption(caption);
00044
00045 setOperationMode( type );
00046
00047 KToolBar *tb = toolBar();
00048 tb->insertSeparator();
00049 int index = tb->insertCombo(QStringList(), -1 , false , 0 , 0 , 0 );
00050 d->encoding = tb->getCombo( tb->idAt( index ) );
00051 if ( !d->encoding )
00052 return;
00053
00054 d->encoding->clear ();
00055 QString sEncoding = encoding;
00056 if (sEncoding.isEmpty())
00057 sEncoding = QString::fromLatin1(KGlobal::locale()->encoding());
00058
00059 QStringList encodings (KGlobal::charsets()->availableEncodingNames());
00060 int insert = 0;
00061 for (uint i=0; i < encodings.count(); i++)
00062 {
00063 bool found = false;
00064 QTextCodec *codecForEnc = KGlobal::charsets()->codecForName(encodings[i], found);
00065
00066 if (found)
00067 {
00068 d->encoding->insertItem (encodings[i]);
00069 if ( (codecForEnc->name() == sEncoding) || (encodings[i] == sEncoding) )
00070 {
00071 d->encoding->setCurrentItem(insert);
00072 }
00073
00074 insert++;
00075 }
00076 }
00077
00078
00079 }
00080
00081 KEncodingFileDialog::~KEncodingFileDialog()
00082 {
00083 delete d;
00084 }
00085
00086
00087 QString KEncodingFileDialog::selectedEncoding() const
00088 {
00089 if (d->encoding)
00090 return d->encoding->currentText();
00091 else
00092 return QString::null;
00093 }
00094
00095
00096 KEncodingFileDialog::Result KEncodingFileDialog::getOpenFileNameAndEncoding(const QString& encoding,
00097 const QString& startDir,
00098 const QString& filter,
00099 QWidget *parent, const QString& caption)
00100 {
00101 KEncodingFileDialog dlg(startDir, encoding,filter,caption.isNull() ? i18n("Open") : caption,Opening,parent,
00102 "filedialog", true);
00103
00104 dlg.setMode( KFile::File | KFile::LocalOnly );
00105 dlg.ops->clearHistory();
00106 dlg.exec();
00107
00108 Result res;
00109 res.fileNames<<dlg.selectedFile();
00110 res.encoding=dlg.selectedEncoding();
00111 return res;
00112 }
00113
00114 KEncodingFileDialog::Result KEncodingFileDialog::getOpenFileNamesAndEncoding(const QString& encoding,
00115 const QString& startDir,
00116 const QString& filter,
00117 QWidget *parent,
00118 const QString& caption)
00119 {
00120 KEncodingFileDialog dlg(startDir, encoding,filter,caption.isNull() ? i18n("Open") : caption,Opening,parent,
00121 "filedialog", true);
00122 dlg.setMode(KFile::Files | KFile::LocalOnly);
00123 dlg.ops->clearHistory();
00124 dlg.exec();
00125
00126 Result res;
00127 res.fileNames=dlg.selectedFiles();
00128 res.encoding=dlg.selectedEncoding();
00129 return res;
00130 }
00131
00132 KEncodingFileDialog::Result KEncodingFileDialog::getOpenURLAndEncoding(const QString& encoding, const QString& startDir,
00133 const QString& filter, QWidget *parent, const QString& caption)
00134 {
00135 KEncodingFileDialog dlg(startDir, encoding,filter,caption.isNull() ? i18n("Open") : caption,Opening,parent,
00136 "filedialog", true);
00137
00138 dlg.setMode( KFile::File );
00139 dlg.ops->clearHistory();
00140 dlg.exec();
00141
00142 Result res;
00143 res.URLs<<dlg.selectedURL();
00144 res.encoding=dlg.selectedEncoding();
00145 return res;
00146 }
00147
00148 KEncodingFileDialog::Result KEncodingFileDialog::getOpenURLsAndEncoding(const QString& encoding, const QString& startDir,
00149 const QString& filter,
00150 QWidget *parent,
00151 const QString& caption)
00152 {
00153 KEncodingFileDialog dlg(startDir, encoding,filter,caption.isNull() ? i18n("Open") : caption,Opening,parent,
00154 "filedialog", true);
00155
00156 dlg.setMode(KFile::Files);
00157 dlg.ops->clearHistory();
00158 dlg.exec();
00159
00160 Result res;
00161 res.URLs=dlg.selectedURLs();
00162 res.encoding=dlg.selectedEncoding();
00163 return res;
00164 }
00165
00166
00167 KEncodingFileDialog::Result KEncodingFileDialog::getSaveFileNameAndEncoding(const QString& encoding,
00168 const QString& dir,
00169 const QString& filter,
00170 QWidget *parent,
00171 const QString& caption)
00172 {
00173 bool specialDir = dir.at(0) == ':';
00174 KEncodingFileDialog dlg(specialDir?dir:QString::null, encoding,filter,caption.isNull() ? i18n("Save As") : caption,
00175 Saving,parent, "filedialog", true);
00176
00177 if ( !specialDir )
00178 dlg.setSelection( dir );
00179 dlg.exec();
00180
00181 QString filename = dlg.selectedFile();
00182 if (!filename.isEmpty())
00183 KRecentDocument::add(filename);
00184
00185 Result res;
00186 res.fileNames<<filename;
00187 res.encoding=dlg.selectedEncoding();
00188 return res;
00189 }
00190
00191
00192 KEncodingFileDialog::Result KEncodingFileDialog::getSaveURLAndEncoding(const QString& encoding,
00193 const QString& dir, const QString& filter,
00194 QWidget *parent, const QString& caption)
00195 {
00196 bool specialDir = dir.at(0) == ':';
00197 KEncodingFileDialog dlg(specialDir?dir:QString::null, encoding,filter,caption.isNull() ? i18n("Save As") :
00198 caption, Saving,parent, "filedialog", true);
00199
00200 if ( !specialDir )
00201 dlg.setSelection( dir );
00202
00203 dlg.exec();
00204
00205 KURL url = dlg.selectedURL();
00206 if (url.isValid())
00207 KRecentDocument::add( url );
00208
00209 Result res;
00210 res.URLs<<url;
00211 res.encoding=dlg.selectedEncoding();
00212 return res;
00213 }
00214
00215
00216
00217 void KEncodingFileDialog::virtual_hook( int id, void* data )
00218 {
00219 KFileDialog::virtual_hook( id, data );
00220 }
00221
00222
00223 #include "kencodingfiledialog.moc"