Commit 6c7c8285 authored by Victor Yacovlev's avatar Victor Yacovlev

PluginManager: Start program in QApplication event loop to proper

Splashscreen handling + Removed qDebug() in PluginManager
parent 8bc0429a
......@@ -81,7 +81,10 @@ class Application
{
public:
inline explicit Application(int & argc, char **argv, bool gui)
: QApplication(argc, argv, gui) {}
: QApplication(argc, argv, gui)
, timerId_(-1)
, splashScreen_(nullptr)
{}
inline bool notify(QObject * receiver, QEvent * event) {
bool result = false;
try {
......@@ -94,6 +97,117 @@ public:
}
return result;
}
inline void setSplashScreen(QSplashScreen * s) {
splashScreen_ = s;
}
inline void initialize() {
bool gui = true;
#ifdef Q_WS_X11
gui = gui && getenv("DISPLAY")!=0;
#endif
const QString sharePath = QDir(applicationDirPath()+SHARE_PATH).canonicalPath();
QDir translationsDir(sharePath+"/translations");
QStringList ts_files = translationsDir.entryList(QStringList() << "*_"+getLanguage()+".qm");
foreach (QString tsname, ts_files) {
tsname = tsname.mid(0, tsname.length()-3);
QTranslator * tr = new QTranslator(this);
if (tr->load(tsname, sharePath+"/translations")) {
installTranslator(tr);
}
}
setProperty("sharePath", sharePath);
QSettings::setDefaultFormat(QSettings::IniFormat);
addLibraryPath(PLUGINS_PATH);
ExtensionSystem::PluginManager * manager = new ExtensionSystem::PluginManager;
QObject::connect (this, SIGNAL(aboutToQuit()), manager, SLOT(shutdown()));
manager->setPluginPath(PLUGINS_PATH);
manager->setSharePath(SHARE_PATH);
QString error;
#ifdef CONFIGURATION_TEMPLATE
const QString defaultTemplate = CONFIGURATION_TEMPLATE;
#else
#error No default configuration passed to GCC
#endif
QString templ = defaultTemplate;
for (int i=1; i<argc(); i++) {
QString arg = QString::fromLocal8Bit(argv()[i]);
if (arg.startsWith("[") && arg.endsWith("]")) {
templ = arg.mid(1, arg.length()-2);
}
}
error = manager->loadPluginsByTemplate(templ);
if (!gui && manager->isGuiRequired()) {
if (splashScreen_)
splashScreen_->finish(0);
showErrorMessage("Requires X11 session to run this configuration");
exit(1);
}
qInstallMsgHandler(manager->isGuiRequired()
? GuiMessageOutput
: ConsoleMessageOutput);
if (!error.isEmpty()) {
if (splashScreen_)
splashScreen_->finish(0);
showErrorMessage(error);
exit(1);
}
error = manager->initializePlugins();
if (!error.isEmpty()) {
if (splashScreen_)
splashScreen_->finish(0);
showErrorMessage(error);
exit(property("returnCode").isValid()
? property("returnCode").toInt() : 1);
}
// GUI requirement may be changed as result of plugins initialization,
// so check it again
if (!gui && manager->isGuiRequired()) {
showErrorMessage("Requires X11 session to run this configuration");
exit(property("returnCode").isValid()
? property("returnCode").toInt() : 1);
}
if (splashScreen_)
splashScreen_->finish(0);
error = manager->start();
if (!error.isEmpty()) {
if (splashScreen_)
splashScreen_->finish(0);
showErrorMessage(error);
exit(property("returnCode").isValid()
? property("returnCode").toInt() : 1);
}
if (!manager->isGuiRequired()) {
quit();
}
}
inline void timerEvent(QTimerEvent * event) {
killTimer(timerId_);
event->accept();
initialize();
}
inline int main() {
timerId_ = startTimer(250);
int ret = exec();
if (ret == 0) {
return property("returnCode").isValid()
? property("returnCode").toInt() : 0;
}
else {
return ret;
}
}
private:
int timerId_;
QSplashScreen * splashScreen_;
};
int main(int argc, char **argv)
......@@ -102,7 +216,7 @@ int main(int argc, char **argv)
#ifdef Q_WS_X11
gui = gui && getenv("DISPLAY")!=0;
#endif
QApplication * app = new Application(argc, argv, gui);
Application * app = new Application(argc, argv, gui);
QLocale russian = QLocale("ru_RU");
QLocale::setDefault(russian);
#ifdef Q_OS_WIN32
......@@ -139,11 +253,8 @@ int main(int argc, char **argv)
const QString sharePath = QDir(app->applicationDirPath()+SHARE_PATH).canonicalPath();
bool allowDebugMessages = false;
#ifdef SPLASHSCREEN
if (gui) {
allowDebugMessages = true;
QString imgPath = sharePath+QString("/")+SPLASHSCREEN;
splashScreen = new QSplashScreen();
QImage img(imgPath);
......@@ -155,8 +266,8 @@ int main(int argc, char **argv)
f.setPixelSize(12);
p.setFont(f);
QString v = qApp->applicationVersion();
if (app->property("svnRev").isValid()) {
v += " (rev. "+app->property("svnRev").toString()+")";
if (app->property("gitHash").isValid()) {
v += " (GIT "+app->property("gitHash").toString()+")";
}
int tw = QFontMetrics(f).width(v);
int th = QFontMetrics(f).height();
......@@ -167,112 +278,10 @@ int main(int argc, char **argv)
QPixmap px = QPixmap::fromImage(img);
splashScreen->setPixmap(px);
splashScreen->show();
qApp->processEvents();
app->setSplashScreen(splashScreen);
}
#endif
if (allowDebugMessages) {
qInstallMsgHandler(GuiMessageOutput);
}
else {
qInstallMsgHandler(ConsoleMessageOutput);
}
QDir translationsDir(sharePath+"/translations");
QStringList ts_files = translationsDir.entryList(QStringList() << "*_"+getLanguage()+".qm");
foreach (QString tsname, ts_files) {
tsname = tsname.mid(0, tsname.length()-3);
QTranslator * tr = new QTranslator(app);
if (tr->load(tsname, sharePath+"/translations")) {
// qDebug() << "Loaded " << tsname;
app->installTranslator(tr);
}
}
app->setProperty("sharePath", sharePath);
QSettings::setDefaultFormat(QSettings::IniFormat);
// const QString settingsPath = QDir(app->applicationDirPath()+QString(SHARE_PATH)+"/default_settings").canonicalPath();
// QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, settingsPath);
app->addLibraryPath(PLUGINS_PATH);
ExtensionSystem::PluginManager * manager = new ExtensionSystem::PluginManager;
QObject::connect (app, SIGNAL(aboutToQuit()), manager, SLOT(shutdown()));
manager->setPluginPath(PLUGINS_PATH);
manager->setSharePath(SHARE_PATH);
QString error;
#ifdef CONFIGURATION_TEMPLATE
const QString defaultTemplate = CONFIGURATION_TEMPLATE;
#else
#error No default configuration passed to GCC
#endif
QString templ = defaultTemplate;
for (int i=1; i<argc; i++) {
QString arg = QString::fromLocal8Bit(argv[i]);
if (arg.startsWith("[") && arg.endsWith("]")) {
templ = arg.mid(1, arg.length()-2);
}
}
error = manager->loadPluginsByTemplate(templ);
if (!gui && manager->isGuiRequired()) {
showErrorMessage("Requires X11 session to run this configuration");
delete manager;
delete app;
return 1;
}
if (!error.isEmpty()) {
showErrorMessage(error);
delete manager;
delete app;
return 1;
}
error = manager->initializePlugins();
if (!error.isEmpty()) {
showErrorMessage(error);
delete manager;
delete app;
return app->property("returnCode").isValid()
? app->property("returnCode").toInt() : 1;
}
// GUI requirement may be changed as result of plugins initialization,
// so check it again
if (!gui && manager->isGuiRequired()) {
showErrorMessage("Requires X11 session to run this configuration");
delete manager;
delete app;
return app->property("returnCode").isValid()
? app->property("returnCode").toInt() : 1;
}
error = manager->start();
if (!error.isEmpty()) {
showErrorMessage(error);
delete manager;
delete app;
return app->property("returnCode").isValid()
? app->property("returnCode").toInt() : 1;
}
int ret = 0;
if (splashScreen) {
splashScreen->finish(0);
}
if (manager->isGuiRequired()) {
ret = app->exec();
if (ret == 0) {
return app->property("returnCode").isValid()
? app->property("returnCode").toInt() : 0;
}
}
else {
manager->shutdown();
return app->property("returnCode").isValid()
? app->property("returnCode").toInt() : 0;
}
// delete manager;
// delete app;
if (splashScreen) {
// delete splashScreen;
}
return ret;
return app->main();
}
......@@ -298,7 +298,7 @@ QString PluginManagerPrivate::loadPlugins()
qPrintable(test);
for (int i=0; i<specs.size(); i++) {
QPluginLoader loader(specs[i].libraryFileName);
qDebug()<<specs[i].libraryFileName;
// qDebug()<<specs[i].libraryFileName;
if (!loader.load()) {
return QString("Can't load module %1: %2")
.arg(specs[i].name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment