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();
127 Translate string to current locale using Qt.
129 std::string _(const char* psz)
131 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
134 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
136 static void handleRunawayException(std::exception *e)
138 PrintExceptionContinue(e, "Runaway exception");
139 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));
143 #ifndef BITCOIN_QT_TEST
144 int main(int argc, char *argv[])
146 #if !defined(MAC_OSX) && !defined(WIN32)
147 // TODO: implement qtipcserver.cpp for Mac and Windows
149 // Do this early as we don't want to bother initializing if we are just calling IPC
150 for (int i = 1; i < argc; i++)
152 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
154 const char *strURI = argv[i];
156 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
157 if(mq.try_send(strURI, strlen(strURI), 0))
162 catch (boost::interprocess::interprocess_exception &ex) {
169 // Internal string conversion is all UTF-8
170 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
171 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
173 Q_INIT_RESOURCE(bitcoin);
174 QApplication app(argc, argv);
176 // Command-line options take precedence:
177 ParseParameters(argc, argv);
179 // ... then bitcoin.conf:
180 if (!ReadConfigFile(mapArgs, mapMultiArgs))
182 fprintf(stderr, "Error: Specified directory does not exist\n");
186 // Application identification (must be set before OptionsModel is initialized,
187 // as it is used to locate QSettings)
188 app.setOrganizationName("Bitcoin");
189 app.setOrganizationDomain("bitcoin.org");
190 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
191 app.setApplicationName("Bitcoin-Qt-testnet");
193 app.setApplicationName("Bitcoin-Qt");
195 // ... then GUI settings:
196 OptionsModel optionsModel;
198 // Get desired locale ("en_US") from command line or system locale
199 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
200 // Load language files for configured locale:
201 // - First load the translator for the base language, without territory
202 // - Then load the more specific locale translator
203 QString lang = lang_territory;
205 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
206 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
208 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
209 if (!qtTranslatorBase.isEmpty())
210 app.installTranslator(&qtTranslatorBase);
212 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
213 if (!qtTranslator.isEmpty())
214 app.installTranslator(&qtTranslator);
216 translatorBase.load(":/translations/"+lang);
217 if (!translatorBase.isEmpty())
218 app.installTranslator(&translatorBase);
220 translator.load(":/translations/"+lang_territory);
221 if (!translator.isEmpty())
222 app.installTranslator(&translator);
224 QSplashScreen splash(QPixmap(":/images/splash"), 0);
225 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
228 splash.setAutoFillBackground(true);
234 app.setQuitOnLastWindowClosed(false);
238 if(AppInit2(argc, argv))
241 // Put this in a block, so that BitcoinGUI is cleaned up properly before
242 // calling Shutdown() in case of exceptions.
244 optionsModel.Upgrade(); // Must be done after AppInit2
248 splash.finish(&window);
250 ClientModel clientModel(&optionsModel);
251 WalletModel walletModel(pwalletMain, &optionsModel);
254 window.setClientModel(&clientModel);
255 window.setWalletModel(&walletModel);
257 // If -min option passed, start window minimized.
258 if(GetBoolArg("-min"))
260 window.showMinimized();
267 // Place this here as guiref has to be defined if we dont want to lose URIs
270 #if !defined(MAC_OSX) && !defined(WIN32)
271 // TODO: implement qtipcserver.cpp for Mac and Windows
273 // Check for URI in argv
274 for (int i = 1; i < argc; i++)
276 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
278 const char *strURI = argv[i];
280 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
281 mq.try_send(strURI, strlen(strURI), 0);
283 catch (boost::interprocess::interprocess_exception &ex) {
298 } catch (std::exception& e) {
299 handleRunawayException(&e);
301 handleRunawayException(NULL);
305 #endif // BITCOIN_QT_TEST