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 <QApplication>
23 #include <QMessageBox>
24 
25 #include <U2Algorithm/OpenCLGpuRegistry.h>
26 
27 #include <U2Core/CMDLineCoreOptions.h>
28 #include <U2Core/CMDLineRegistry.h>
29 #include <U2Core/ConsoleShutdownTask.h>
30 #include <U2Core/FileAndDirectoryUtils.h>
31 #include <U2Core/ResourceTracker.h>
32 #include <U2Core/Timer.h>
33 #include <U2Core/UserApplicationsSettings.h>
34 #include <U2Core/Version.h>
35 
36 #include <U2Lang/LocalDomain.h>
37 
38 // U2Private
39 #include <AppContextImpl.h>
40 #include <AppSettingsImpl.h>
41 #include <DocumentFormatRegistryImpl.h>
42 #include <IOAdapterRegistryImpl.h>
43 #include <PluginSupportImpl.h>
44 #include <ServiceRegistryImpl.h>
45 #include <SettingsImpl.h>
46 #include <TaskSchedulerImpl.h>
47 //#include <crash_handler/CrashHandler.h>
48 
49 #define TR_SETTINGS_ROOT QString("test_runner/")
50 
51 using namespace U2;
52 
registerCoreServices()53 static void registerCoreServices() {
54     ServiceRegistry *sr = AppContext::getServiceRegistry();
55     TaskScheduler *ts = AppContext::getTaskScheduler();
56     Q_UNUSED(sr);
57     Q_UNUSED(ts);
58     // unlike ugene's UI Main.cpp we don't create PluginViewerImpl, ProjectViewImpl
59     //    ts->registerTopLevelTask(sr->registerServiceTask(new ScriptRegistryService()));
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char **argv) {
63     bool useGui = true;
64 #if defined(Q_OS_UNIX)
65     useGui = (getenv("DISPLAY") != 0);
66     if (!useGui && argc == 1) {
67         printf("Use \"ugeneui\" to start Unipro UGENE graphical interface or \"ugenecl\" to use the command-line interface.");
68         return 1;
69     }
70 #endif
71 
72 #if 0
73     CrashHandler::setupHandler();
74     CrashHandler::setSendCrashReports(false);
75 #endif
76 
77     if (!Version::checkBuildAndRuntimeVersions()) {
78         return -1;
79     }
80 
81     GTIMER(c1, t1, "main()->QApp::exec");
82 
83     QApplication app(argc, argv);
84 
85     // User lauches the program manually
86     if (argc == 1) {
87         if (useGui) {
88             QMessageBox msgBox;
89             msgBox.setWindowTitle("Information");
90             msgBox.setText("Use \"ugeneui\" to start Unipro graphical interface \nor \"ugenecl\" to use the command-line interface.");
91             msgBox.exec();
92         } else {
93             printf("Use \"ugeneui\" to start Unipro UGENE graphical interface or \"ugenecl\" to use the command-line interface.");
94         }
95         return 1;
96     }
97 
98     AppContextImpl *appContext = AppContextImpl::getApplicationContext();
99     appContext->setWorkingDirectoryPath(QCoreApplication::applicationDirPath());
100 
101     appContext->setGUIMode(true);
102 
103     QCoreApplication::addLibraryPath(AppContext::getWorkingDirectoryPath());
104     // parse all cmdline arguments
105     CMDLineRegistry *cmdLineRegistry = new CMDLineRegistry(app.arguments());
106     appContext->setCMDLineRegistry(cmdLineRegistry);
107 
108     // 1 create settings
109     SettingsImpl *globalSettings = new SettingsImpl(QSettings::SystemScope);
110     appContext->setGlobalSettings(globalSettings);
111 
112     SettingsImpl *settings = new SettingsImpl(QSettings::UserScope);
113     appContext->setSettings(settings);
114 
115     AppSettings *appSettings = new AppSettingsImpl();
116     appContext->setAppSettings(appSettings);
117 
118     UserAppsSettings *userAppSettings = AppContext::getAppSettings()->getUserAppsSettings();
119 
120     if (cmdLineRegistry->hasParameter(CMDLineCoreOptions::DOWNLOAD_DIR)) {
121         userAppSettings->setDownloadDirPath(FileAndDirectoryUtils::getAbsolutePath(cmdLineRegistry->getParameterValue(CMDLineCoreOptions::DOWNLOAD_DIR)));
122     }
123     if (cmdLineRegistry->hasParameter(CMDLineCoreOptions::CUSTOM_TOOLS_CONFIG_DIR)) {
124         userAppSettings->setCustomToolsConfigsDirPath(FileAndDirectoryUtils::getAbsolutePath(cmdLineRegistry->getParameterValue(CMDLineCoreOptions::CUSTOM_TOOLS_CONFIG_DIR)));
125     }
126     if (cmdLineRegistry->hasParameter(CMDLineCoreOptions::TMP_DIR)) {
127         userAppSettings->setUserTemporaryDirPath(FileAndDirectoryUtils::getAbsolutePath(cmdLineRegistry->getParameterValue(CMDLineCoreOptions::TMP_DIR)));
128     }
129     if (cmdLineRegistry->hasParameter(CMDLineCoreOptions::DEFAULT_DATA_DIR)) {
130         userAppSettings->setDefaultDataDirPath(FileAndDirectoryUtils::getAbsolutePath(cmdLineRegistry->getParameterValue(CMDLineCoreOptions::DEFAULT_DATA_DIR)));
131     }
132     if (cmdLineRegistry->hasParameter(CMDLineCoreOptions::FILE_STORAGE_DIR)) {
133         userAppSettings->setFileStorageDir(FileAndDirectoryUtils::getAbsolutePath(cmdLineRegistry->getParameterValue(CMDLineCoreOptions::FILE_STORAGE_DIR)));
134     }
135     // 2 create functional components of ugene
136 
137     ResourceTracker *resTrack = new ResourceTracker();
138     appContext->setResourceTracker(resTrack);
139 
140     TaskSchedulerImpl *ts = new TaskSchedulerImpl(appSettings->getAppResourcePool());
141     appContext->setTaskScheduler(ts);
142 
143     PluginSupportImpl *psp = new PluginSupportImpl();
144     appContext->setPluginSupport(psp);
145 
146     ServiceRegistryImpl *sreg = new ServiceRegistryImpl();
147     appContext->setServiceRegistry(sreg);
148 
149 #ifdef OPENCL_SUPPORT
150     OpenCLGpuRegistry *oclgr = new OpenCLGpuRegistry();
151     appContext->setOpenCLGpuRegistry(oclgr);
152 #endif
153 
154     registerCoreServices();
155 
156     // 3 run QT
157     t1.stop();
158     ConsoleShutdownTask watchQuit(&app);
159     int rc = app.exec();
160     // int rc = 0;
161     // 4 deallocate resources
162     // Workflow::WorkflowEnv::shutdown();
163 
164     delete psp;
165     appContext->setPluginSupport(NULL);
166 
167     delete ts;
168     appContext->setTaskScheduler(NULL);
169 
170     delete resTrack;
171     appContext->setResourceTracker(NULL);
172 
173     appContext->setAppSettings(NULL);
174     delete appSettings;
175 
176     delete settings;
177     appContext->setSettings(NULL);
178 
179     delete globalSettings;
180     appContext->setGlobalSettings(NULL);
181 
182     delete cmdLineRegistry;
183     appContext->setCMDLineRegistry(NULL);
184 
185     //CrashHandler::shutdown();
186 
187     return rc;
188 }
189