2 * W.J. van der Laan 2011
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
13 #include <QApplication>
14 #include <QMessageBox>
18 #include <QTranslator>
19 #include <QSplashScreen>
20 #include <QLibraryInfo>
22 // Need a global reference for the notifications to find the GUI
24 QSplashScreen *splashref;
26 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
28 // Message from main thread
31 guiref->error(QString::fromStdString(caption),
32 QString::fromStdString(message));
36 QMessageBox::critical(0, QString::fromStdString(caption),
37 QString::fromStdString(message),
38 QMessageBox::Ok, QMessageBox::Ok);
43 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
45 bool modal = style & wxMODAL;
51 // Message from network thread
54 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
55 Q_ARG(QString, QString::fromStdString(caption)),
56 Q_ARG(QString, QString::fromStdString(message)),
61 printf("%s: %s\n", caption.c_str(), message.c_str());
62 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
67 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
71 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
75 // Call slot on GUI thread.
76 // If called from another thread, use a blocking QueuedConnection.
77 Qt::ConnectionType connectionType = Qt::DirectConnection;
78 if(QThread::currentThread() != QCoreApplication::instance()->thread())
80 connectionType = Qt::BlockingQueuedConnection;
83 QMetaObject::invokeMethod(guiref, "askFee", connectionType,
84 Q_ARG(qint64, nFeeRequired),
85 Q_ARG(bool*, &payFee));
90 void CalledSetStatusBar(const std::string& strText, int nField)
92 // Only used for built-in mining, which is disabled, simple ignore
95 void UIThreadCall(boost::function0<void> fn)
97 // Only used for built-in mining, which is disabled, simple ignore
100 void MainFrameRepaint()
103 QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
106 void InitMessage(const std::string &message)
110 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
111 QApplication::instance()->processEvents();
117 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
121 Translate string to current locale using Qt.
123 std::string _(const char* psz)
125 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
128 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
130 static void handleRunawayException(std::exception *e)
132 PrintExceptionContinue(e, "Runaway exception");
133 QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occured. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
137 int main(int argc, char *argv[])
139 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
140 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
142 Q_INIT_RESOURCE(bitcoin);
143 QApplication app(argc, argv);
145 // Load language files for system locale:
146 // - First load the translator for the base language, without territory
147 // - Then load the more specific locale translator
148 QString lang_territory = QLocale::system().name(); // "en_US"
149 QString lang = lang_territory;
150 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
151 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
153 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
154 if (!qtTranslatorBase.isEmpty())
155 app.installTranslator(&qtTranslatorBase);
157 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
158 if (!qtTranslator.isEmpty())
159 app.installTranslator(&qtTranslator);
161 translatorBase.load(":/translations/"+lang);
162 if (!translatorBase.isEmpty())
163 app.installTranslator(&translatorBase);
165 translator.load(":/translations/"+lang_territory);
166 if (!translator.isEmpty())
167 app.installTranslator(&translator);
169 app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
171 QSplashScreen splash(QPixmap(":/images/splash"), 0);
172 if (!GetBoolArg("-min"))
175 splash.setAutoFillBackground(true);
181 app.setQuitOnLastWindowClosed(false);
185 if(AppInit2(argc, argv))
188 // Put this in a block, so that BitcoinGUI is cleaned up properly before
189 // calling Shutdown() in case of exceptions.
192 splash.finish(&window);
193 OptionsModel optionsModel(pwalletMain);
194 ClientModel clientModel(&optionsModel);
195 WalletModel walletModel(pwalletMain, &optionsModel);
198 window.setClientModel(&clientModel);
199 window.setWalletModel(&walletModel);
201 // If -min option passed, start window minimized.
202 if(GetBoolArg("-min"))
204 window.showMinimized();
216 // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
223 } catch (std::exception& e) {
224 handleRunawayException(&e);
226 handleRunawayException(NULL);