File indexing completed on 2024-12-22 04:18:18

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "datasourceselectordialog.h"
0014 
0015 #include "datasource.h"
0016 #include "datasourcepluginmanager.h"
0017 
0018 #include <QMessageBox>
0019 #include <QDebug>
0020 
0021 namespace Kst {
0022 
0023 DataSourceSelectorDialog::DataSourceSelectorDialog(QString &file, QWidget *parent)
0024   : QFileDialog(parent) {
0025 
0026   setFileMode(QFileDialog::Directory);
0027   selectFile(file);
0028   currentChanged(file);
0029 
0030   connect(this, SIGNAL(currentChanged(QString)), this, SLOT(currentChanged(QString)));
0031 }
0032 
0033 
0034 DataSourceSelectorDialog::~DataSourceSelectorDialog() {
0035 }
0036 
0037 
0038 QString DataSourceSelectorDialog::selectedDataSource() {
0039   return selectedFiles().first();
0040 }
0041 
0042 
0043 void DataSourceSelectorDialog::currentChanged(const QString &current) {
0044   //qDebug() << "currentChanged" << current;
0045   if (current.isEmpty()) {
0046     setFileMode(QFileDialog::Directory);
0047   } else {
0048     QFileInfo fileInfo(current);
0049     if (fileInfo.isDir()) {
0050       //qDebug() << "Directory Selected - valid?" << DataSourcePluginManager::validSource(current);
0051       if (DataSourcePluginManager::validSource(current)) {
0052         setFileMode(QFileDialog::Directory);
0053       } else {
0054         setFileMode(QFileDialog::ExistingFile);
0055       }
0056     } else if (fileInfo.exists()) {
0057       //qDebug() << "File Selected - valid?" << DataSourcePluginManager::validSource(current);
0058       if (DataSourcePluginManager::validSource(current)) {
0059         setFileMode(QFileDialog::ExistingFile);
0060       } else {
0061         setFileMode(QFileDialog::Directory);
0062       }
0063     }
0064   }
0065   QStringList filters;
0066   filters << "Any files (*)";
0067   setNameFilters(filters);
0068 }
0069 
0070 
0071 void DataSourceSelectorDialog::accept() {
0072   QStringList files = selectedFiles();
0073   if (files.isEmpty()) {
0074     return;
0075   }
0076 
0077   for (int i = 0; i < files.count(); ++i) {
0078       QFileInfo info(files.at(i));
0079       if (!info.exists()) {
0080 #ifndef QT_NO_MESSAGEBOX
0081   QString message = tr("%1\nFile not found.\nPlease verify the "
0082                       "correct file name was given.");
0083   QMessageBox::warning(this, windowTitle(), message.arg(info.fileName()));
0084 #endif // QT_NO_MESSAGEBOX
0085           return;
0086       }
0087       if (info.isDir()) {
0088           if (info.fileName().isEmpty() || (info.filePath() == directory().path())) {
0089             QDialog::accept();
0090             return;
0091           }
0092       }
0093   }
0094 
0095   QFileDialog::accept();
0096 }
0097 
0098 }
0099 
0100 // vim: ts=2 sw=2 et