2 * W.J. van der Laan 20011-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 main thread
33 guiref->error(QString::fromStdString(caption),
34 QString::fromStdString(message));
38 QMessageBox::critical(0, QString::fromStdString(caption),
39 QString::fromStdString(message),
40 QMessageBox::Ok, QMessageBox::Ok);
45 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
47 // Message from network thread
50 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
51 Q_ARG(QString, QString::fromStdString(caption)),
52 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 ThreadSafeHandleURL(const std::string& strURL)
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, "handleURL", connectionType,
98 Q_ARG(QString, QString::fromStdString(strURL)));
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 #ifndef BITCOIN_QT_TEST
135 int main(int argc, char *argv[])
137 // Do this early as we don't want to bother initializing if we are just calling IPC
138 for (int i = 1; i < argc; i++)
140 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
142 const char *strURL = argv[i];
144 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
145 if(mq.try_send(strURL, strlen(strURL), 0))
150 catch (boost::interprocess::interprocess_exception &ex) {
156 // Internal string conversion is all UTF-8
157 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
158 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
160 Q_INIT_RESOURCE(bitcoin);
161 QApplication app(argc, argv);
163 ParseParameters(argc, argv);
165 // Get desired locale ("en_US") from command line or system locale
166 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
167 // Load language files for configured locale:
168 // - First load the translator for the base language, without territory
169 // - Then load the more specific locale translator
170 QString lang = lang_territory;
172 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
173 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
175 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
176 if (!qtTranslatorBase.isEmpty())
177 app.installTranslator(&qtTranslatorBase);
179 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
180 if (!qtTranslator.isEmpty())
181 app.installTranslator(&qtTranslator);
183 translatorBase.load(":/translations/"+lang);
184 if (!translatorBase.isEmpty())
185 app.installTranslator(&translatorBase);
187 translator.load(":/translations/"+lang_territory);
188 if (!translator.isEmpty())
189 app.installTranslator(&translator);
191 app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
193 QSplashScreen splash(QPixmap(":/images/splash"), 0);
195 splash.setAutoFillBackground(true);
200 app.setQuitOnLastWindowClosed(false);
204 if(AppInit2(argc, argv))
207 // Put this in a block, so that BitcoinGUI is cleaned up properly before
208 // calling Shutdown() in case of exceptions.
210 splash.finish(&window);
211 OptionsModel optionsModel(pwalletMain);
212 ClientModel clientModel(&optionsModel);
213 WalletModel walletModel(pwalletMain, &optionsModel);
216 window.setClientModel(&clientModel);
217 window.setWalletModel(&walletModel);
219 // If -min option passed, start window minimized.
220 if(GetBoolArg("-min"))
222 window.showMinimized();
229 // Place this here as guiref has to be defined if we dont want to lose URLs
231 // Check for URL in argv
232 for (int i = 1; i < argc; i++)
234 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
236 const char *strURL = argv[i];
238 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
239 mq.try_send(strURL, strlen(strURL), 0);
241 catch (boost::interprocess::interprocess_exception &ex) {
256 } catch (std::exception& e) {
257 PrintException(&e, "Runaway exception");
259 PrintException(NULL, "Runaway exception");
263 #endif // BITCOIN_QT_TEST