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();
116 Translate string to current locale using Qt.
118 std::string _(const char* psz)
120 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
123 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
125 static void handleRunawayException(std::exception *e)
127 PrintExceptionContinue(e, "Runaway exception");
128 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));
132 int main(int argc, char *argv[])
134 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
135 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
137 Q_INIT_RESOURCE(bitcoin);
138 QApplication app(argc, argv);
140 // Load language files for system locale:
141 // - First load the translator for the base language, without territory
142 // - Then load the more specific locale translator
143 QString lang_territory = QLocale::system().name(); // "en_US"
144 QString lang = lang_territory;
145 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
146 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
148 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
149 if (!qtTranslatorBase.isEmpty())
150 app.installTranslator(&qtTranslatorBase);
152 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
153 if (!qtTranslator.isEmpty())
154 app.installTranslator(&qtTranslator);
156 translatorBase.load(":/translations/"+lang);
157 if (!translatorBase.isEmpty())
158 app.installTranslator(&translatorBase);
160 translator.load(":/translations/"+lang_territory);
161 if (!translator.isEmpty())
162 app.installTranslator(&translator);
164 app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
166 QSplashScreen splash(QPixmap(":/images/splash"), 0);
167 if (!GetBoolArg("-min"))
170 splash.setAutoFillBackground(true);
176 app.setQuitOnLastWindowClosed(false);
180 if(AppInit2(argc, argv))
183 // Put this in a block, so that BitcoinGUI is cleaned up properly before
184 // calling Shutdown() in case of exceptions.
187 splash.finish(&window);
188 OptionsModel optionsModel(pwalletMain);
189 ClientModel clientModel(&optionsModel);
190 WalletModel walletModel(pwalletMain, &optionsModel);
193 window.setClientModel(&clientModel);
194 window.setWalletModel(&walletModel);
196 // If -min option passed, start window minimized.
197 if(GetBoolArg("-min"))
199 window.showMinimized();
216 } catch (std::exception& e) {
217 handleRunawayException(&e);
219 handleRunawayException(NULL);