1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "ExternalToolUtils.h"
23 
24 #include <QFile>
25 #include <QFileInfo>
26 #include <QMessageBox>
27 
28 #include <U2Core/AppContext.h>
29 #include <U2Core/DataPathRegistry.h>
30 #include <U2Core/ExternalToolRegistry.h>
31 #include <U2Core/QObjectScopedPointer.h>
32 #include <U2Core/Settings.h>
33 #include <U2Core/U2SafePoints.h>
34 
35 #include <U2Gui/AppSettingsGUI.h>
36 
37 #include "ExternalToolSupportSettingsController.h"
38 
39 namespace U2 {
40 
41 const QString ExternalToolUtils::CISTROME_DATA_DIR = "CISTROME_DATA_DIR";
42 
checkExtToolsPath(const QStringList & ids)43 void ExternalToolUtils::checkExtToolsPath(const QStringList &ids) {
44     QStringList missingTools;
45     foreach (const QString &id, ids) {
46         ExternalTool *tool = AppContext::getExternalToolRegistry()->getById(id);
47         SAFE_POINT(nullptr != tool, QString("External tool with ID '%1' not found in the registry").arg(id), );
48         if (tool->getPath().isEmpty()) {
49             missingTools << tool->getName();
50         }
51     }
52     if (!missingTools.isEmpty()) {
53         QString mergedNames = missingTools.join(", ");
54 
55         QObjectScopedPointer<QMessageBox> msgBox = new QMessageBox;
56         msgBox->setWindowTitle("BLAST: " + QString(mergedNames));
57         msgBox->setText(tr("Paths for the following tools are not selected: %1.").arg(mergedNames));
58         msgBox->setInformativeText(tr("Do you want to select it now?"));
59         msgBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
60         msgBox->setDefaultButton(QMessageBox::Yes);
61         const int ret = msgBox->exec();
62         CHECK(!msgBox.isNull(), );
63 
64         switch (ret) {
65             case QMessageBox::Yes:
66                 AppContext::getAppSettingsGUI()->showSettingsDialog(ExternalToolSupportSettingsPageId);
67                 break;
68             case QMessageBox::No:
69                 return;
70             default:
71                 assert(false);
72                 break;
73         }
74     }
75 }
76 
addDefaultCistromeDirToSettings()77 void ExternalToolUtils::addDefaultCistromeDirToSettings() {
78     QString cistromeDefaultPath;
79     QString customDataDir = qgetenv("UGENE_DATA_PATH");
80     if (!customDataDir.isEmpty()) {
81         cistromeDefaultPath = QFileInfo(customDataDir + "/cistrome").absoluteFilePath();
82     } else {
83         cistromeDefaultPath = QFileInfo(QString(PATH_PREFIX_DATA) + QString(":") + "cistrome").absoluteFilePath();
84     }
85 
86     bool defaultExists = QFile::exists(cistromeDefaultPath);
87     QString savedValue = AppContext::getSettings()->getValue(CISTROME_DATA_DIR).toString();
88 
89     bool addNew = savedValue.isEmpty() && defaultExists;
90     bool removeOld = !savedValue.isEmpty() && !QFile::exists(savedValue);
91     bool replaceOld = removeOld && defaultExists;
92 
93     if (addNew || replaceOld) {
94         AppContext::getSettings()->setValue(CISTROME_DATA_DIR, cistromeDefaultPath);
95     } else if (removeOld) {
96         AppContext::getSettings()->remove(CISTROME_DATA_DIR);
97     }
98 }
99 
addCistromeDataPath(const QString & dataName,const QString & dirName,bool entriesAreFolders)100 void ExternalToolUtils::addCistromeDataPath(const QString &dataName, const QString &dirName, bool entriesAreFolders) {
101     U2DataPathRegistry *dpr = AppContext::getDataPathRegistry();
102     CHECK(nullptr != dpr, );
103 
104     const QString dataPath = AppContext::getSettings()->getValue(CISTROME_DATA_DIR).toString() + QDir::separator() + dirName;
105     U2DataPath *dp = new U2DataPath(dataName, dataPath, "", U2DataPath::CutFileExtension | (entriesAreFolders ? U2DataPath::AddOnlyFolders : U2DataPath::None));
106     bool ok = dpr->registerEntry(dp);
107     if (!ok) {
108         delete dp;
109     }
110 }
111 
112 }    // namespace U2
113