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 bool modal = style & wxMODAL;
45 // Message from network thread
48 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
49 Q_ARG(QString, QString::fromStdString(caption)),
50 Q_ARG(QString, QString::fromStdString(message)),
55 printf("%s: %s\n", caption.c_str(), message.c_str());
56 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
61 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
65 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
69 // Call slot on GUI thread.
70 // If called from another thread, use a blocking QueuedConnection.
71 Qt::ConnectionType connectionType = Qt::DirectConnection;
72 if(QThread::currentThread() != QCoreApplication::instance()->thread())
74 connectionType = Qt::BlockingQueuedConnection;
77 QMetaObject::invokeMethod(guiref, "askFee", connectionType,
78 Q_ARG(qint64, nFeeRequired),
79 Q_ARG(bool*, &payFee));
84 void ThreadSafeHandleURI(const std::string& strURI)
89 // Call slot on GUI thread.
90 // If called from another thread, use a blocking QueuedConnection.
91 Qt::ConnectionType connectionType = Qt::DirectConnection;
92 if(QThread::currentThread() != QCoreApplication::instance()->thread())
94 connectionType = Qt::BlockingQueuedConnection;
96 QMetaObject::invokeMethod(guiref, "handleURI", connectionType,
97 Q_ARG(QString, QString::fromStdString(strURI)));
100 void CalledSetStatusBar(const std::string& strText, int nField)
102 // Only used for built-in mining, which is disabled, simple ignore
105 void UIThreadCall(boost::function0<void> fn)
107 // Only used for built-in mining, which is disabled, simple ignore
110 void MainFrameRepaint()
113 QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
116 void InitMessage(const std::string &message)
120 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
121 QApplication::instance()->processEvents();
126 Translate string to current locale using Qt.
128 std::string _(const char* psz)
130 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
133 #ifndef BITCOIN_QT_TEST
134 int main(int argc, char *argv[])
136 #if !defined(MAC_OSX) && !defined(WIN32)
137 // TODO: implement qtipcserver.cpp for Mac and Windows
139 // Do this early as we don't want to bother initializing if we are just calling IPC
140 for (int i = 1; i < argc; i++)
142 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
144 const char *strURI = argv[i];
146 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
147 if(mq.try_send(strURI, strlen(strURI), 0))
152 catch (boost::interprocess::interprocess_exception &ex) {
159 // Internal string conversion is all UTF-8
160 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
161 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
163 Q_INIT_RESOURCE(bitcoin);
164 QApplication app(argc, argv);
166 // Command-line options take precedence:
167 ParseParameters(argc, argv);
169 // ... then bitcoin.conf:
170 if (!ReadConfigFile(mapArgs, mapMultiArgs))
172 fprintf(stderr, "Error: Specified directory does not exist\n");
176 // Application identification (must be set before OptionsModel is initialized,
177 // as it is used to locate QSettings)
178 app.setOrganizationName("Bitcoin");
179 app.setOrganizationDomain("bitcoin.org");
180 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
181 app.setApplicationName("Bitcoin-Qt-testnet");
183 app.setApplicationName("Bitcoin-Qt");
185 // ... then GUI settings:
186 OptionsModel optionsModel;
188 // Get desired locale ("en_US") from command line or system locale
189 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
190 // Load language files for configured locale:
191 // - First load the translator for the base language, without territory
192 // - Then load the more specific locale translator
193 QString lang = lang_territory;
195 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
196 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
198 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
199 if (!qtTranslatorBase.isEmpty())
200 app.installTranslator(&qtTranslatorBase);
202 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
203 if (!qtTranslator.isEmpty())
204 app.installTranslator(&qtTranslator);
206 translatorBase.load(":/translations/"+lang);
207 if (!translatorBase.isEmpty())
208 app.installTranslator(&translatorBase);
210 translator.load(":/translations/"+lang_territory);
211 if (!translator.isEmpty())
212 app.installTranslator(&translator);
214 QSplashScreen splash(QPixmap(":/images/splash"), 0);
215 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
218 splash.setAutoFillBackground(true);
224 app.setQuitOnLastWindowClosed(false);
228 if(AppInit2(argc, argv))
231 // Put this in a block, so that BitcoinGUI is cleaned up properly before
232 // calling Shutdown() in case of exceptions.
234 optionsModel.Upgrade(); // Must be done after AppInit2
238 splash.finish(&window);
240 ClientModel clientModel(&optionsModel);
241 WalletModel walletModel(pwalletMain, &optionsModel);
244 window.setClientModel(&clientModel);
245 window.setWalletModel(&walletModel);
247 // If -min option passed, start window minimized.
248 if(GetBoolArg("-min"))
250 window.showMinimized();
257 // Place this here as guiref has to be defined if we dont want to lose URIs
260 #if !defined(MAC_OSX) && !defined(WIN32)
261 // TODO: implement qtipcserver.cpp for Mac and Windows
263 // Check for URI in argv
264 for (int i = 1; i < argc; i++)
266 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
268 const char *strURI = argv[i];
270 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
271 mq.try_send(strURI, strlen(strURI), 0);
273 catch (boost::interprocess::interprocess_exception &ex) {
288 } catch (std::exception& e) {
289 PrintException(&e, "Runaway exception");
291 PrintException(NULL, "Runaway exception");
295 #endif // BITCOIN_QT_TEST