2 * W.J. van der Laan 2011-2012
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
9 #include "guiconstants.h"
12 #include "ui_interface.h"
13 #include "qtipcserver.h"
15 #include <QApplication>
16 #include <QMessageBox>
19 #include <QTranslator>
20 #include <QSplashScreen>
21 #include <QLibraryInfo>
23 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
24 #define _BITCOIN_QT_PLUGINS_INCLUDED
27 Q_IMPORT_PLUGIN(qcncodecs)
28 Q_IMPORT_PLUGIN(qjpcodecs)
29 Q_IMPORT_PLUGIN(qtwcodecs)
30 Q_IMPORT_PLUGIN(qkrcodecs)
31 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
34 // Need a global reference for the notifications to find the GUI
35 static BitcoinGUI *guiref;
36 static QSplashScreen *splashref;
38 static void ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
40 // Message from network thread
43 bool modal = (style & CClientUIInterface::MODAL);
44 // in case of modal message, use blocking connection to wait for user to click OK
45 QMetaObject::invokeMethod(guiref, "error",
46 modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
47 Q_ARG(QString, QString::fromStdString(caption)),
48 Q_ARG(QString, QString::fromStdString(message)),
53 printf("%s: %s\n", caption.c_str(), message.c_str());
54 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
58 static bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption)
62 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
66 QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
67 Q_ARG(qint64, nFeeRequired),
68 Q_ARG(bool*, &payFee));
73 static void ThreadSafeHandleURI(const std::string& strURI)
78 QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
79 Q_ARG(QString, QString::fromStdString(strURI)));
82 static void InitMessage(const std::string &message)
86 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
87 QApplication::instance()->processEvents();
91 static void QueueShutdown()
93 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
97 Translate string to current locale using Qt.
99 static std::string Translate(const char* psz)
101 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
104 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
106 static void handleRunawayException(std::exception *e)
108 PrintExceptionContinue(e, "Runaway exception");
109 QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
113 #ifndef BITCOIN_QT_TEST
114 int main(int argc, char *argv[])
116 // Do this early as we don't want to bother initializing if we are just calling IPC
117 ipcScanRelay(argc, argv);
119 // Internal string conversion is all UTF-8
120 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
121 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
123 Q_INIT_RESOURCE(bitcoin);
124 QApplication app(argc, argv);
126 // Install global event filter that makes sure that long tooltips can be word-wrapped
127 app.installEventFilter(new GUIUtil::ToolTipToRichTextFilter(TOOLTIP_WRAP_THRESHOLD, &app));
129 // Command-line options take precedence:
130 ParseParameters(argc, argv);
132 // ... then bitcoin.conf:
133 if (!boost::filesystem::is_directory(GetDataDir(false)))
135 // This message can not be translated, as translation is not initialized yet
136 // (which not yet possible because lang=XX can be overridden in bitcoin.conf in the data directory)
137 QMessageBox::critical(0, "Bitcoin",
138 QString("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(mapArgs["-datadir"])));
141 ReadConfigFile(mapArgs, mapMultiArgs);
143 // Application identification (must be set before OptionsModel is initialized,
144 // as it is used to locate QSettings)
145 app.setOrganizationName("Bitcoin");
146 app.setOrganizationDomain("bitcoin.org");
147 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
148 app.setApplicationName("Bitcoin-Qt-testnet");
150 app.setApplicationName("Bitcoin-Qt");
152 // ... then GUI settings:
153 OptionsModel optionsModel;
155 // Get desired locale (e.g. "de_DE") from command line or use system locale
156 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
157 QString lang = lang_territory;
158 // Convert to "de" only by truncating "_DE"
159 lang.truncate(lang_territory.lastIndexOf('_'));
161 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
162 // Load language files for configured locale:
163 // - First load the translator for the base language, without territory
164 // - Then load the more specific locale translator
166 // Load e.g. qt_de.qm
167 if (qtTranslatorBase.load("qt_" + lang, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
168 app.installTranslator(&qtTranslatorBase);
170 // Load e.g. qt_de_DE.qm
171 if (qtTranslator.load("qt_" + lang_territory, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
172 app.installTranslator(&qtTranslator);
174 // Load e.g. bitcoin_de.qm (shortcut "de" needs to be defined in bitcoin.qrc)
175 if (translatorBase.load(lang, ":/translations/"))
176 app.installTranslator(&translatorBase);
178 // Load e.g. bitcoin_de_DE.qm (shortcut "de_DE" needs to be defined in bitcoin.qrc)
179 if (translator.load(lang_territory, ":/translations/"))
180 app.installTranslator(&translator);
182 // Subscribe to global signals from core
183 uiInterface.ThreadSafeMessageBox.connect(ThreadSafeMessageBox);
184 uiInterface.ThreadSafeAskFee.connect(ThreadSafeAskFee);
185 uiInterface.ThreadSafeHandleURI.connect(ThreadSafeHandleURI);
186 uiInterface.InitMessage.connect(InitMessage);
187 uiInterface.QueueShutdown.connect(QueueShutdown);
188 uiInterface.Translate.connect(Translate);
190 // Show help message immediately after parsing command-line options (for "-lang") and setting locale,
191 // but before showing splash screen.
192 if (mapArgs.count("-?") || mapArgs.count("--help"))
194 GUIUtil::HelpMessageBox help;
199 QSplashScreen splash(QPixmap(":/images/splash"), 0);
200 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
203 splash.setAutoFillBackground(true);
209 app.setQuitOnLastWindowClosed(false);
213 // Regenerate startup link, to fix links to old versions
214 if (GUIUtil::GetStartOnSystemStartup())
215 GUIUtil::SetStartOnSystemStartup(true);
222 // Put this in a block, so that the Model objects are cleaned up before
223 // calling Shutdown().
225 optionsModel.Upgrade(); // Must be done after AppInit2
228 splash.finish(&window);
230 ClientModel clientModel(&optionsModel);
231 WalletModel walletModel(pwalletMain, &optionsModel);
233 window.setClientModel(&clientModel);
234 window.setWalletModel(&walletModel);
236 // If -min option passed, start window minimized.
237 if(GetBoolArg("-min"))
239 window.showMinimized();
246 // Place this here as guiref has to be defined if we don't want to lose URIs
252 window.setClientModel(0);
253 window.setWalletModel(0);
256 // Shutdown the core and its threads, but don't exit Bitcoin-Qt here
263 } catch (std::exception& e) {
264 handleRunawayException(&e);
266 handleRunawayException(NULL);
270 #endif // BITCOIN_QT_TEST