diff --git a/avogadro/mainwindow.cpp b/avogadro/mainwindow.cpp index e1d349aa..4c2a2f13 100644 --- a/avogadro/mainwindow.cpp +++ b/avogadro/mainwindow.cpp @@ -45,9 +45,11 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -437,7 +439,16 @@ void MainWindow::closeEvent(QCloseEvent* e) if (event->mimeData()->hasUrls()) { // TODO: check for ZIP, TAR, PY scripts (plugins) foreach(const QUrl& url, event->mimeData()->urls() ) { - openFile(url.toLocalFile()); + if (url.isLocalFile()) { + QString fileName = url.toLocalFile(); + QFileInfo info(fileName); + QString extension = info.completeSuffix(); // e.g. .tar.gz or .pdb.gz + + if (extension == "py") + addScript(fileName); + else + openFile(fileName); + } } event->acceptProposedAction(); } @@ -633,6 +644,57 @@ void MainWindow::importFile() } } +bool MainWindow::addScript(const QString& filePath) +{ + if (filePath.isEmpty()) { + return false; + } + + // Ask the user what type of script this is + // TODO: add some sort of warning? + QStringList types; + types << tr("Commands") << tr("Input Generators") << tr("File Formats"); + + bool ok; + QString item = QInputDialog::getItem(this, tr("Install Plugin Script"), + tr("Script Type:"), types, 0, false, &ok); + + if (!ok || item.isEmpty()) + return false; + + QString typePath; + + int index = types.indexOf(item); + // don't translate these + switch(index){ + case 0: // commands + typePath = "commands"; + break; + case 1: + typePath = "inputGenerators"; + break; + case 2: + typePath = "formatScripts"; + break; + default: + typePath = "other"; + } + + QStringList stdPaths = + QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation); + + QFileInfo info(filePath); + + QString destinationPath(stdPaths[0] + '/' + typePath + '/' + info.fileName()); + qDebug() << " copying " << filePath << " to " << destinationPath; + QFile::remove(destinationPath); // silently fail if there's nothing to remove + QFile::copy(filePath, destinationPath); + + // TODO: Ask that type of plugin script to reload? + + return true; +} + bool MainWindow::openFile(const QString& fileName, Io::FileFormat* reader) { if (fileName.isEmpty()) { diff --git a/avogadro/mainwindow.h b/avogadro/mainwindow.h index 43c419db..36a1dbf9 100644 --- a/avogadro/mainwindow.h +++ b/avogadro/mainwindow.h @@ -96,6 +96,11 @@ public slots: */ bool openFile(const QString& fileName, Io::FileFormat* reader = nullptr); + /** + * Move @a fileName as a plugin script (i.e. put it in the correct dir) + */ + bool addScript(const QString& fileName); + #ifdef QTTESTING void playTest(const QString& fileName, bool exit = true); #endif