Files
Sourcetrail/src/lib/Application.cpp
T
Eberhard Graether 30d222b7db logic: More fixes for release
* Fixed crash on undoing aggregation edge
* Avoid mutex locking in logging classes when logging is disabled
* Readded dark color scheme
* Fixed font size of type label in autocompletion list
* Fixed text drawing in autocompletion list
* Only give camelcase score when no noletter score was given
* Added MessageShowErrors to undo stack
* Select error line in table when undoing MessageShowErrors
* Notify user about new Plugin in description of From Visual Studio project setup
* Fixed row height of Project File Location label in project setup
2016-10-14 00:45:37 +02:00

343 lines
7.7 KiB
C++

#include "Application.h"
#include "utility/logging/logging.h"
#include "utility/logging/LogManager.h"
#include "utility/messaging/MessageQueue.h"
#include "utility/messaging/type/MessageStatus.h"
#include "utility/messaging/type/MessageShowStartScreen.h"
#include "utility/scheduling/TaskScheduler.h"
#include "utility/tracing.h"
#include "utility/UserPaths.h"
#include "utility/Version.h"
#include "component/view/DialogView.h"
#include "component/view/GraphViewStyle.h"
#include "component/controller/NetworkFactory.h"
#include "component/controller/IDECommunicationController.h"
#include "component/view/MainView.h"
#include "component/view/ViewFactory.h"
#include "data/StorageCache.h"
#include "LicenseChecker.h"
#include "settings/ApplicationSettings.h"
#include "settings/ColorScheme.h"
void Application::createInstance(
const Version& version, ViewFactory* viewFactory, NetworkFactory* networkFactory
){
Version::setApplicationVersion(version);
loadSettings();
TaskScheduler::getInstance();
MessageQueue::getInstance();
bool hasGui = (viewFactory != nullptr);
s_instance = std::shared_ptr<Application>(new Application(hasGui));
s_instance->m_storageCache = std::make_shared<StorageCache>();
if (viewFactory != nullptr)
{
s_instance->m_componentManager = ComponentManager::create(viewFactory, s_instance->m_storageCache.get());
s_instance->m_mainView = viewFactory->createMainView();
s_instance->m_mainView->setTitle("Coati");
s_instance->m_componentManager->setup(s_instance->m_mainView.get());
s_instance->m_mainView->loadLayout();
MessageShowStartScreen().dispatch();
}
if (networkFactory != nullptr)
{
s_instance->m_ideCommunicationController =
networkFactory->createIDECommunicationController(s_instance->m_storageCache.get());
s_instance->m_ideCommunicationController->startListening();
}
s_instance->startMessagingAndScheduling();
}
std::shared_ptr<Application> Application::getInstance()
{
return s_instance;
}
void Application::destroyInstance()
{
s_instance.reset();
}
void Application::loadSettings()
{
std::shared_ptr<ApplicationSettings> settings = ApplicationSettings::getInstance();
settings->load(FilePath(UserPaths::getAppSettingsPath()));
LogManager* logManager = LogManager::getInstance().get();
logManager->setLoggingEnabled(settings->getLoggingEnabled());
loadStyle(settings->getColorSchemePath());
}
void Application::loadStyle(const FilePath& colorSchemePath)
{
ColorScheme::getInstance()->load(colorSchemePath);
GraphViewStyle::loadStyleSettings();
}
std::shared_ptr<Application> Application::s_instance;
Application::Application(bool withGUI)
: m_hasGUI(withGUI)
, m_isInTrial(false)
{
LicenseChecker::createInstance();
}
Application::~Application()
{
MessageQueue::getInstance()->stopMessageLoop();
TaskScheduler::getInstance()->stopSchedulerLoop();
if (m_hasGUI)
{
m_mainView->saveLayout();
}
}
void Application::addProjectFactoryModule(std::shared_ptr<ProjectFactoryModule> module)
{
m_projectFactory.addModule(module);
}
const std::shared_ptr<Project> Application::getCurrentProject()
{
return m_project;
}
bool Application::hasGUI()
{
return m_hasGUI;
}
int Application::handleDialog(const std::string& message)
{
return getDialogView()->confirm(message);
}
int Application::handleDialog(const std::string& message, const std::vector<std::string>& options)
{
return getDialogView()->confirm(message, options);
}
void Application::setTitle(const std::string& title)
{
m_mainView->setTitle(title);
}
bool Application::isInTrial() const
{
return m_isInTrial;
}
void Application::createAndLoadProject(const FilePath& projectSettingsFilePath)
{
MessageStatus("Loading Project: " + projectSettingsFilePath.str(), false, true).dispatch();
try
{
updateRecentProjects(projectSettingsFilePath);
m_storageCache->clear();
m_project = m_projectFactory.createProject(projectSettingsFilePath, m_storageCache.get(), getDialogView());
if (m_project)
{
if (m_hasGUI)
{
setTitle("Coati - " + projectSettingsFilePath.fileName());
m_mainView->hideStartScreen();
m_componentManager->clearComponents();
m_componentManager->refreshViews();
}
}
else
{
LOG_ERROR_STREAM(<< "Failed to load project.");
MessageStatus("Failed to load project.", true).dispatch();
}
}
catch (...)
{
LOG_ERROR_STREAM(<< "Failed to load project.");
MessageStatus("Failed to load project.", true).dispatch();
}
}
void Application::refreshProject(bool force)
{
if (m_project)
{
bool indexing = m_project->refresh(force);
if (indexing)
{
m_storageCache->clear();
if (m_hasGUI)
{
m_componentManager->refreshViews();
}
}
}
}
void Application::handleMessage(MessageActivateWindow* message)
{
if (m_hasGUI)
{
m_mainView->activateWindow();
}
}
void Application::handleMessage(MessageEnteredLicense* message)
{
m_isInTrial = false;
}
void Application::handleMessage(MessageFinishedParsing* message)
{
logStorageStats();
if (m_hasGUI)
{
MessageRefresh().refreshUiOnly().dispatch();
}
}
void Application::handleMessage(MessageLoadProject* message)
{
TRACE("app load project");
loadSettings();
FilePath projectSettingsFilePath(message->projectSettingsFilePath);
if (projectSettingsFilePath.empty())
{
return;
}
if (m_project && projectSettingsFilePath == m_project->getProjectSettingsFilePath())
{
if (message->forceRefresh)
{
m_project->setStateSettingsUpdated();
refreshProject(false);
}
return;
}
createAndLoadProject(projectSettingsFilePath);
}
void Application::handleMessage(MessageRefresh* message)
{
TRACE("app refresh");
if (message->reloadSettings)
{
loadSettings();
}
if (message->uiOnly)
{
if (m_hasGUI)
{
m_componentManager->refreshViews();
}
}
else
{
refreshProject(message->all);
}
}
void Application::handleMessage(MessageSwitchColorScheme* message)
{
loadStyle(message->colorSchemePath);
MessageRefresh().refreshUiOnly().keepSettings().dispatch();
}
void Application::startMessagingAndScheduling()
{
TaskScheduler::getInstance()->startSchedulerLoopThreaded();
MessageQueue::getInstance()->setSendMessagesAsTasks(true);
MessageQueue::getInstance()->startMessageLoopThreaded();
}
void Application::updateRecentProjects(const FilePath& projectSettingsFilePath)
{
if (m_hasGUI)
{
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get();
std::vector<FilePath> recentProjects = appSettings->getRecentProjects();
if (recentProjects.size())
{
std::vector<FilePath>::iterator it = std::find(recentProjects.begin(), recentProjects.end(), projectSettingsFilePath);
if (it != recentProjects.end())
{
recentProjects.erase(it);
}
}
recentProjects.insert(recentProjects.begin(), projectSettingsFilePath);
while (recentProjects.size() > 7)
{
recentProjects.pop_back();
}
appSettings->setRecentProjects(recentProjects);
appSettings->save(UserPaths::getAppSettingsPath());
m_mainView->updateRecentProjectMenu();
}
}
DialogView* Application::getDialogView() const
{
if (m_componentManager)
{
return m_componentManager->getDialogView();
}
static DialogView dialogView(nullptr);
return &dialogView;
}
void Application::logStorageStats() const
{
if (!ApplicationSettings::getInstance()->getLoggingEnabled())
{
return;
}
std::stringstream ss;
StorageStats stats = m_storageCache->getStorageStats();
ss << "\nGraph:\n";
ss << "\t" << stats.nodeCount << " Nodes\n";
ss << "\t" << stats.edgeCount << " Edges\n";
ss << "\nCode:\n";
ss << "\t" << stats.fileCount << " Files\n";
ss << "\t" << stats.fileLOCCount << " Lines of Code\n";
ErrorCountInfo errorCount = m_storageCache->getErrorCount();
ss << "\nErrors:\n";
ss << "\t" << errorCount.total << " Errors\n";
ss << "\t" << errorCount.fatal << " Fatal Errors\n";
LOG_INFO(ss.str());
}