1 /* This file is part of the KDE libraries
2     Copyright (C) 1997, 1998 Richard Moore <rich@kde.org>
3                   1998 Stephan Kulow <coolo@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 <unistd.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <QDir>
25 #include <QApplication>
26 #include <QLayout>
27 #include <QMutableStringListIterator>
28 #include <QWidget>
29 
30 #include <kfiledialog.h>
31 #include <kmessagebox.h>
32 #if 0 // SPLIT-TODO
33 #include <kconfig.h>
34 #include <kurlbar.h>
35 #include <kdiroperator.h>
36 #endif
37 #include <kfile.h>
38 #include <QDebug>
39 #include <kicondialog.h>
40 
41 #include "kfstest.h"
42 #include <kconfiggroup.h>
43 
44 #include <QMutableStringListIterator>
45 #include <kfiledialog.h>
46 #include <kmessagebox.h>
47 #include <QTimer>
48 
KFDTest(const QUrl & startDir,QObject * parent,const char * name)49 KFDTest::KFDTest(const QUrl &startDir, QObject *parent, const char *name)
50     : QObject(parent),
51       m_startDir(startDir)
52 {
53     setObjectName(name);
54     QTimer::singleShot(1000, this, SLOT(doit()));
55 }
56 
doit()57 void KFDTest::doit()
58 {
59     KFileDialog *dlg = new KFileDialog(m_startDir, QString(), nullptr);
60     dlg->setMode(KFile::File);
61     dlg->setOperationMode(KFileDialog::Saving);
62     QStringList filter;
63     filter << "application/octet-stream" << "text/plain";
64     dlg->setMimeFilter(filter, "application/octet-stream");
65 
66     if (dlg->exec() == QDialog::Accepted) {
67         KMessageBox::information(nullptr, QString::fromLatin1("You selected the file: %1").arg(dlg->selectedUrl().toString()));
68     }
69 
70 //     qApp->quit();
71 }
72 
main(int argc,char ** argv)73 int main(int argc, char **argv)
74 {
75     //KCmdLineOptions options;
76     //options.add("+[cmd]");
77     //options.add("+[url]");
78 
79     QApplication a(argc, argv);
80     a.setQuitOnLastWindowClosed(false);
81 
82     QString name1;
83     QStringList names;
84 
85     QString argv1;
86     QUrl startDir;
87     if (argc > 1) {
88         argv1 = QLatin1String(argv[1]);
89     }
90     if (argc > 2) {
91         startDir = QUrl::fromUserInput(argv[2]);
92     }
93 
94 #if 0 // SPLIT-TODO
95     if (argv1 == QLatin1String("diroperator")) {
96         KDirOperator *op = new KDirOperator(startDir, 0);
97         KConfigGroup grp(KSharedConfig::openConfig(), "TestGroup");
98         op->setViewConfig(grp);
99         op->setView(KFile::Simple);
100         op->show();
101         a.exec();
102     } else
103 #endif
104         if (argv1 == QLatin1String("localonly")) {
105             QString name = KFileDialog::getOpenFileName(startDir);
106             qDebug("filename=%s", name.toLatin1().constData());
107         } else if (argv1 == QLatin1String("oneurl")) {
108             QUrl url = KFileDialog::getOpenUrl(startDir);
109             qDebug() << "url=" << url;
110         } else if (argv1 == QLatin1String("multipleurls")) {
111             QList<QUrl> urls = KFileDialog::getOpenUrls(startDir);
112             qDebug() << "urls=" << QUrl::toStringList(urls);
113         } else if (argv1 == QLatin1String("existingDirectoryUrl")) {
114             QUrl url = KFileDialog::getExistingDirectoryUrl();
115             qDebug("URL=%s", url.toString().toLatin1().constData());
116             name1 = url.toString();
117         }
118 
119         else if (argv1 == QLatin1String("preview")) {
120             QUrl u =  KFileDialog::getImageOpenUrl();
121             qDebug("filename=%s", u.toString().toLatin1().constData());
122         }
123 
124         else if (argv1 == QLatin1String("preselect")) {
125             names = KFileDialog::getOpenFileNames(QUrl::fromLocalFile("/etc/passwd"));
126             QStringList::Iterator it = names.begin();
127             while (it != names.end()) {
128                 qDebug("selected file: %s", (*it).toLatin1().constData());
129                 ++it;
130             }
131         }
132 
133         else if (argv1 == QLatin1String("dirs")) {
134             name1 = KFileDialog::getExistingDirectory();
135         }
136 
137         else if (argv1 == QLatin1String("heap")) {
138             KFileDialog *dlg = new KFileDialog(startDir, QString(), nullptr);
139             dlg->setMode(KFile::File);
140             dlg->setOperationMode(KFileDialog::Saving);
141             QStringList filter;
142             filter << "application/octet-stream" << "text/plain";
143             dlg->setMimeFilter(filter, "application/octet-stream");
144 #if 0 // SPLIT-TODO
145             KUrlBar *urlBar = dlg->speedBar();
146             if (urlBar) {
147                 urlBar->insertDynamicItem(QUrl("ftp://ftp.kde.org"),
148                                           QLatin1String("KDE FTP Server"));
149             }
150 #endif
151             if (dlg->exec() == QDialog::Accepted) {
152                 name1 = dlg->selectedUrl().toString();
153             }
154         }
155 
156         else if (argv1 == QLatin1String("eventloop")) {
157             new KFDTest(startDir);
158             return a.exec();
159         }
160 
161         else if (argv1 == QLatin1String("save")) {
162             QUrl u = KFileDialog::getSaveUrl(startDir);
163 //        QString(QDir::homePath() + QLatin1String("/testfile")),
164 //        QString(), 0L);
165             name1 = u.toString();
166         }
167 
168         else if (argv1 == QLatin1String("icon")) {
169             KIconDialog dlg;
170             QString icon = dlg.getIcon();
171             qDebug() << icon;
172         }
173 
174 //     else if ( argv1 == QLatin1String("dirselect") ) {
175 //         QUrl url = QUrl::fromLocalFile("/");
176 //         QUrl selected = KDirSelectDialog::selectDirectory( url );
177 //         name1 = selected.toString();
178 //         qDebug("*** selected: %s", selected.toString().toLatin1().constData());
179 //     }
180 
181         else {
182             KFileDialog dlg(startDir,
183                             QString::fromLatin1("*|All Files\n"
184                                                 "*.lo *.o *.la|All libtool Files"), nullptr);
185 //    dlg.setFilter( "*.kdevelop" );
186             dlg.setMode(KFile::Files |
187                         KFile::Directory |
188                         KFile::ExistingOnly |
189                         KFile::LocalOnly);
190 //        QStringList filter;
191 //        filter << "text/plain" << "text/html" << "image/png";
192 //        dlg.setMimeFilter( filter );
193 //        KMimeType::List types;
194 //        types.append( KMimeType::mimeType( "text/plain" ) );
195 //        types.append( KMimeType::mimeType( "text/html" ) );
196 //        dlg.setFilterMimeType( "Filetypes:", types, types.first() );
197             if (dlg.exec() == QDialog::Accepted) {
198                 const QList<QUrl> list = dlg.selectedUrls();
199                 QList<QUrl>::ConstIterator it = list.constBegin();
200                 qDebug("*** selectedUrls(): ");
201                 while (it != list.constEnd()) {
202                     name1 = (*it).toString();
203                     qDebug("  -> %s", name1.toLatin1().constData());
204                     ++it;
205                 }
206                 qDebug("*** selectedFile: %s", dlg.selectedFile().toLatin1().constData());
207                 qDebug("*** selectedUrl: %s", dlg.selectedUrl().toString().toLatin1().constData());
208                 qDebug("*** selectedFiles: ");
209                 QStringList l = dlg.selectedFiles();
210                 QStringList::Iterator it2 = l.begin();
211                 while (it2 != l.end()) {
212                     qDebug("  -> %s", (*it2).toLatin1().constData());
213                     ++it2;
214                 }
215             }
216         }
217 
218     if (!(name1.isNull()))
219         KMessageBox::information(nullptr, QLatin1String("You selected the file ") + name1,
220                                  QLatin1String("Your Choice"));
221     return 0;
222 }
223