2 * W.J. van der Laan 2011-2012
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
11 #include "qtipcserver.h"
14 #include <QApplication>
15 #include <QMessageBox>
19 #include <QTranslator>
20 #include <QSplashScreen>
21 #include <QLibraryInfo>
23 #include <boost/interprocess/ipc/message_queue.hpp>
25 // Need a global reference for the notifications to find the GUI
27 QSplashScreen *splashref;
29 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
31 // Message from AppInit2(), always in main thread before main window is constructed
32 QMessageBox::critical(0, QString::fromStdString(caption),
33 QString::fromStdString(message),
34 QMessageBox::Ok, QMessageBox::Ok);
38 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
40 bool modal = style & wxMODAL;
46 // Message from network thread
49 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
50 Q_ARG(QString, QString::fromStdString(caption)),
51 Q_ARG(QString, QString::fromStdString(message)),
56 printf("%s: %s\n", caption.c_str(), message.c_str());
57 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
62 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
66 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
70 // Call slot on GUI thread.
71 // If called from another thread, use a blocking QueuedConnection.
72 Qt::ConnectionType connectionType = Qt::DirectConnection;
73 if(QThread::currentThread() != QCoreApplication::instance()->thread())
75 connectionType = Qt::BlockingQueuedConnection;
78 QMetaObject::invokeMethod(guiref, "askFee", connectionType,
79 Q_ARG(qint64, nFeeRequired),
80 Q_ARG(bool*, &payFee));
85 void ThreadSafeHandleURI(const std::string& strURI)
90 // Call slot on GUI thread.
91 // If called from another thread, use a blocking QueuedConnection.
92 Qt::ConnectionType connectionType = Qt::DirectConnection;
93 if(QThread::currentThread() != QCoreApplication::instance()->thread())
95 connectionType = Qt::BlockingQueuedConnection;
97 QMetaObject::invokeMethod(guiref, "handleURI", connectionType,
98 Q_ARG(QString, QString::fromStdString(strURI)));
101 void CalledSetStatusBar(const std::string& strText, int nField)
103 // Only used for built-in mining, which is disabled, simple ignore
106 void UIThreadCall(boost::function0<void> fn)
108 // Only used for built-in mining, which is disabled, simple ignore
111 void MainFrameRepaint()
114 QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
117 void InitMessage(const std::string &message)
121 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
122 QApplication::instance()->processEvents();
128 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
132 Translate string to current locale using Qt.
134 std::string _(const char* psz)
136 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
139 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
141 static void handleRunawayException(std::exception *e)
143 PrintExceptionContinue(e, "Runaway exception");
144 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));
148 #ifndef BITCOIN_QT_TEST
149 int main(int argc, char *argv[])
151 #if !defined(MAC_OSX) && !defined(WIN32)
152 // TODO: implement qtipcserver.cpp for Mac and Windows
154 // Do this early as we don't want to bother initializing if we are just calling IPC
155 for (int i = 1; i < argc; i++)
157 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
159 const char *strURI = argv[i];
161 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
162 if(mq.try_send(strURI, strlen(strURI), 0))
167 catch (boost::interprocess::interprocess_exception &ex) {
174 // Internal string conversion is all UTF-8
175 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
176 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
178 Q_INIT_RESOURCE(bitcoin);
179 QApplication app(argc, argv);
181 // Command-line options take precedence:
182 ParseParameters(argc, argv);
184 // ... then bitcoin.conf:
185 if (!ReadConfigFile(mapArgs, mapMultiArgs))
187 fprintf(stderr, "Error: Specified directory does not exist\n");
191 // Application identification (must be set before OptionsModel is initialized,
192 // as it is used to locate QSettings)
193 app.setOrganizationName("Bitcoin");
194 app.setOrganizationDomain("bitcoin.org");
195 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
196 app.setApplicationName("Bitcoin-Qt-testnet");
198 app.setApplicationName("Bitcoin-Qt");
200 // ... then GUI settings:
201 OptionsModel optionsModel;
203 // Get desired locale ("en_US") from command line or system locale
204 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
205 // Load language files for configured locale:
206 // - First load the translator for the base language, without territory
207 // - Then load the more specific locale translator
208 QString lang = lang_territory;
210 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
211 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
213 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
214 if (!qtTranslatorBase.isEmpty())
215 app.installTranslator(&qtTranslatorBase);
217 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
218 if (!qtTranslator.isEmpty())
219 app.installTranslator(&qtTranslator);
221 translatorBase.load(":/translations/"+lang);
222 if (!translatorBase.isEmpty())
223 app.installTranslator(&translatorBase);
225 translator.load(":/translations/"+lang_territory);
226 if (!translator.isEmpty())
227 app.installTranslator(&translator);
229 QSplashScreen splash(QPixmap(":/images/splash"), 0);
230 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
233 splash.setAutoFillBackground(true);
239 app.setQuitOnLastWindowClosed(false);
243 if(AppInit2(argc, argv))
246 // Put this in a block, so that BitcoinGUI is cleaned up properly before
247 // calling Shutdown() in case of exceptions.
249 optionsModel.Upgrade(); // Must be done after AppInit2
253 splash.finish(&window);
255 ClientModel clientModel(&optionsModel);
256 WalletModel walletModel(pwalletMain, &optionsModel);
259 window.setClientModel(&clientModel);
260 window.setWalletModel(&walletModel);
262 // If -min option passed, start window minimized.
263 if(GetBoolArg("-min"))
265 window.showMinimized();
272 // Place this here as guiref has to be defined if we dont want to lose URIs
275 #if !defined(MAC_OSX) && !defined(WIN32)
276 // TODO: implement qtipcserver.cpp for Mac and Windows
278 // Check for URI in argv
279 for (int i = 1; i < argc; i++)
281 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
283 const char *strURI = argv[i];
285 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
286 mq.try_send(strURI, strlen(strURI), 0);
288 catch (boost::interprocess::interprocess_exception &ex) {
298 // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
305 } catch (std::exception& e) {
306 handleRunawayException(&e);
308 handleRunawayException(NULL);
312 #endif // BITCOIN_QT_TEST