2 * W.J. van der Laan 2011
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 int main(int argc, char *argv[])
136 // Do this early as we don't want to bother initializing if we are just calling IPC
137 for (int i = 1; i < argc; i++)
139 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
141 const char *strURL = argv[i];
143 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
144 if(mq.try_send(strURL, strlen(strURL), 0))
149 catch (boost::interprocess::interprocess_exception &ex) {
155 // Internal string conversion is all UTF-8
156 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
157 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
159 Q_INIT_RESOURCE(bitcoin);
160 QApplication app(argc, argv);
162 ParseParameters(argc, argv);
164 // Load language files for system locale:
165 // - First load the translator for the base language, without territory
166 // - Then load the more specific locale translator
167 QString lang_territory = QLocale::system().name(); // "en_US"
168 QString lang = lang_territory;
169 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
170 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
172 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
173 if (!qtTranslatorBase.isEmpty())
174 app.installTranslator(&qtTranslatorBase);
176 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
177 if (!qtTranslator.isEmpty())
178 app.installTranslator(&qtTranslator);
180 translatorBase.load(":/translations/"+lang);
181 if (!translatorBase.isEmpty())
182 app.installTranslator(&translatorBase);
184 translator.load(":/translations/"+lang_territory);
185 if (!translator.isEmpty())
186 app.installTranslator(&translator);
188 app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
190 QSplashScreen splash(QPixmap(":/images/splash"), 0);
192 splash.setAutoFillBackground(true);
197 app.setQuitOnLastWindowClosed(false);
201 if(AppInit2(argc, argv))
204 // Put this in a block, so that BitcoinGUI is cleaned up properly before
205 // calling Shutdown() in case of exceptions.
207 splash.finish(&window);
208 OptionsModel optionsModel(pwalletMain);
209 ClientModel clientModel(&optionsModel);
210 WalletModel walletModel(pwalletMain, &optionsModel);
213 window.setClientModel(&clientModel);
214 window.setWalletModel(&walletModel);
216 // If -min option passed, start window minimized.
217 if(GetBoolArg("-min"))
219 window.showMinimized();
226 // Place this here as guiref has to be defined if we dont want to lose URLs
228 // Check for URL in argv
229 for (int i = 1; i < argc; i++)
231 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
233 const char *strURL = argv[i];
235 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
236 mq.try_send(strURL, strlen(strURL), 0);
238 catch (boost::interprocess::interprocess_exception &ex) {
253 } catch (std::exception& e) {
254 PrintException(&e, "Runaway exception");
256 PrintException(NULL, "Runaway exception");