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"
13 #include <QApplication>
14 #include <QMessageBox>
18 #include <QTranslator>
19 #include <QSplashScreen>
20 #include <QLibraryInfo>
22 #include <boost/interprocess/ipc/message_queue.hpp>
24 // Need a global reference for the notifications to find the GUI
26 QSplashScreen *splashref;
28 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
30 // Message from AppInit2(), always in main thread before main window is constructed
31 QMessageBox::critical(0, QString::fromStdString(caption),
32 QString::fromStdString(message),
33 QMessageBox::Ok, QMessageBox::Ok);
37 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
39 // Message from network thread
42 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
43 Q_ARG(QString, QString::fromStdString(caption)),
44 Q_ARG(QString, QString::fromStdString(message)));
48 printf("%s: %s\n", caption.c_str(), message.c_str());
49 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
54 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
58 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
62 // Call slot on GUI thread.
63 // If called from another thread, use a blocking QueuedConnection.
64 Qt::ConnectionType connectionType = Qt::DirectConnection;
65 if(QThread::currentThread() != QCoreApplication::instance()->thread())
67 connectionType = Qt::BlockingQueuedConnection;
70 QMetaObject::invokeMethod(guiref, "askFee", connectionType,
71 Q_ARG(qint64, nFeeRequired),
72 Q_ARG(bool*, &payFee));
77 void ThreadSafeHandleURL(const std::string& strURL)
82 // Call slot on GUI thread.
83 // If called from another thread, use a blocking QueuedConnection.
84 Qt::ConnectionType connectionType = Qt::DirectConnection;
85 if(QThread::currentThread() != QCoreApplication::instance()->thread())
87 connectionType = Qt::BlockingQueuedConnection;
89 QMetaObject::invokeMethod(guiref, "handleURL", connectionType,
90 Q_ARG(QString, QString::fromStdString(strURL)));
93 void CalledSetStatusBar(const std::string& strText, int nField)
95 // Only used for built-in mining, which is disabled, simple ignore
98 void UIThreadCall(boost::function0<void> fn)
100 // Only used for built-in mining, which is disabled, simple ignore
103 void MainFrameRepaint()
106 QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
109 void InitMessage(const std::string &message)
113 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
114 QApplication::instance()->processEvents();
119 Translate string to current locale using Qt.
121 std::string _(const char* psz)
123 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
126 #ifndef BITCOIN_QT_TEST
127 int main(int argc, char *argv[])
129 #if !defined(MAC_OSX) && !defined(WIN32)
130 // TODO: implement qtipcserver.cpp for Mac and Windows
132 // Do this early as we don't want to bother initializing if we are just calling IPC
133 for (int i = 1; i < argc; i++)
135 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
137 const char *strURL = argv[i];
139 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
140 if(mq.try_send(strURL, strlen(strURL), 0))
145 catch (boost::interprocess::interprocess_exception &ex) {
152 // Internal string conversion is all UTF-8
153 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
154 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
156 Q_INIT_RESOURCE(bitcoin);
157 QApplication app(argc, argv);
159 // Command-line options take precedence:
160 ParseParameters(argc, argv);
162 // ... then bitcoin.conf:
163 if (!ReadConfigFile(mapArgs, mapMultiArgs))
165 fprintf(stderr, "Error: Specified directory does not exist\n");
169 // Application identification (must be set before OptionsModel is initialized,
170 // as it is used to locate QSettings)
171 app.setOrganizationName("Bitcoin");
172 app.setOrganizationDomain("bitcoin.org");
173 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
174 app.setApplicationName("Bitcoin-Qt-testnet");
176 app.setApplicationName("Bitcoin-Qt");
178 // ... then GUI settings:
179 OptionsModel optionsModel;
181 // Get desired locale ("en_US") from command line or system locale
182 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
183 // Load language files for configured locale:
184 // - First load the translator for the base language, without territory
185 // - Then load the more specific locale translator
186 QString lang = lang_territory;
188 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
189 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
191 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
192 if (!qtTranslatorBase.isEmpty())
193 app.installTranslator(&qtTranslatorBase);
195 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
196 if (!qtTranslator.isEmpty())
197 app.installTranslator(&qtTranslator);
199 translatorBase.load(":/translations/"+lang);
200 if (!translatorBase.isEmpty())
201 app.installTranslator(&translatorBase);
203 translator.load(":/translations/"+lang_territory);
204 if (!translator.isEmpty())
205 app.installTranslator(&translator);
207 QSplashScreen splash(QPixmap(":/images/splash"), 0);
208 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
211 splash.setAutoFillBackground(true);
217 app.setQuitOnLastWindowClosed(false);
221 if(AppInit2(argc, argv))
224 // Put this in a block, so that BitcoinGUI is cleaned up properly before
225 // calling Shutdown() in case of exceptions.
227 optionsModel.Upgrade(); // Must be done after AppInit2
231 splash.finish(&window);
233 ClientModel clientModel(&optionsModel);
234 WalletModel walletModel(pwalletMain, &optionsModel);
237 window.setClientModel(&clientModel);
238 window.setWalletModel(&walletModel);
240 // If -min option passed, start window minimized.
241 if(GetBoolArg("-min"))
243 window.showMinimized();
250 // Place this here as guiref has to be defined if we dont want to lose URLs
253 #if !defined(MAC_OSX) && !defined(WIN32)
254 // TODO: implement qtipcserver.cpp for Mac and Windows
256 // Check for URL in argv
257 for (int i = 1; i < argc; i++)
259 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
261 const char *strURL = argv[i];
263 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
264 mq.try_send(strURL, strlen(strURL), 0);
266 catch (boost::interprocess::interprocess_exception &ex) {
281 } catch (std::exception& e) {
282 PrintException(&e, "Runaway exception");
284 PrintException(NULL, "Runaway exception");
288 #endif // BITCOIN_QT_TEST