2 * W.J. van der Laan 2011-2012
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
12 #include "qtipcserver.h"
14 #include <QApplication>
15 #include <QMessageBox>
18 #include <QTranslator>
19 #include <QSplashScreen>
20 #include <QLibraryInfo>
22 #include <boost/interprocess/ipc/message_queue.hpp>
24 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
25 #define _BITCOIN_QT_PLUGINS_INCLUDED
28 Q_IMPORT_PLUGIN(qcncodecs)
29 Q_IMPORT_PLUGIN(qjpcodecs)
30 Q_IMPORT_PLUGIN(qtwcodecs)
31 Q_IMPORT_PLUGIN(qkrcodecs)
32 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
35 // Need a global reference for the notifications to find the GUI
36 static BitcoinGUI *guiref;
37 static QSplashScreen *splashref;
38 static WalletModel *walletmodel;
39 static ClientModel *clientmodel;
41 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
43 // Message from AppInit2(), always in main thread before main window is constructed
44 QMessageBox::critical(0, QString::fromStdString(caption),
45 QString::fromStdString(message),
46 QMessageBox::Ok, QMessageBox::Ok);
50 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
52 // Message from network thread
55 QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
56 Q_ARG(QString, QString::fromStdString(caption)),
57 Q_ARG(QString, QString::fromStdString(message)));
61 printf("%s: %s\n", caption.c_str(), message.c_str());
62 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
67 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
71 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
75 QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
76 Q_ARG(qint64, nFeeRequired),
77 Q_ARG(bool*, &payFee));
82 void ThreadSafeHandleURL(const std::string& strURL)
87 QMetaObject::invokeMethod(guiref, "handleURL", GUIUtil::blockingGUIThreadConnection(),
88 Q_ARG(QString, QString::fromStdString(strURL)));
91 void MainFrameRepaint()
94 QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
96 QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
99 void AddressBookRepaint()
102 QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
105 void InitMessage(const std::string &message)
109 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
110 QApplication::instance()->processEvents();
115 Translate string to current locale using Qt.
117 std::string _(const char* psz)
119 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
123 #define strncasecmp strnicmp
125 #ifndef BITCOIN_QT_TEST
126 int main(int argc, char *argv[])
128 #if !defined(MAC_OSX) && !defined(WIN32)
129 // TODO: implement qtipcserver.cpp for Mac and Windows
131 // Do this early as we don't want to bother initializing if we are just calling IPC
132 for (int i = 1; i < argc; i++)
134 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
136 const char *strURL = argv[i];
138 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
139 if(mq.try_send(strURL, strlen(strURL), 0))
144 catch (boost::interprocess::interprocess_exception &ex) {
151 // Internal string conversion is all UTF-8
152 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
153 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
155 Q_INIT_RESOURCE(bitcoin);
156 QApplication app(argc, argv);
158 // Command-line options take precedence:
159 ParseParameters(argc, argv);
161 // ... then bitcoin.conf:
162 if (!ReadConfigFile(mapArgs, mapMultiArgs))
164 fprintf(stderr, "Error: Specified directory does not exist\n");
168 // Application identification (must be set before OptionsModel is initialized,
169 // as it is used to locate QSettings)
170 app.setOrganizationName("Bitcoin");
171 app.setOrganizationDomain("bitcoin.org");
172 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
173 app.setApplicationName("Bitcoin-Qt-testnet");
175 app.setApplicationName("Bitcoin-Qt");
177 // ... then GUI settings:
178 OptionsModel optionsModel;
180 // Get desired locale ("en_US") from command line or system locale
181 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
182 // Load language files for configured locale:
183 // - First load the translator for the base language, without territory
184 // - Then load the more specific locale translator
185 QString lang = lang_territory;
187 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
188 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
190 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
191 if (!qtTranslatorBase.isEmpty())
192 app.installTranslator(&qtTranslatorBase);
194 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
195 if (!qtTranslator.isEmpty())
196 app.installTranslator(&qtTranslator);
198 translatorBase.load(":/translations/"+lang);
199 if (!translatorBase.isEmpty())
200 app.installTranslator(&translatorBase);
202 translator.load(":/translations/"+lang_territory);
203 if (!translator.isEmpty())
204 app.installTranslator(&translator);
206 QSplashScreen splash(QPixmap(":/images/splash"), 0);
207 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
210 splash.setAutoFillBackground(true);
216 app.setQuitOnLastWindowClosed(false);
220 if(AppInit2(argc, argv))
223 // Put this in a block, so that BitcoinGUI is cleaned up properly before
224 // calling Shutdown() in case of exceptions.
226 optionsModel.Upgrade(); // Must be done after AppInit2
230 splash.finish(&window);
232 ClientModel clientModel(&optionsModel);
233 clientmodel = &clientModel;
234 WalletModel walletModel(pwalletMain, &optionsModel);
235 walletmodel = &walletModel;
238 window.setClientModel(&clientModel);
239 window.setWalletModel(&walletModel);
241 // If -min option passed, start window minimized.
242 if(GetBoolArg("-min"))
244 window.showMinimized();
251 // Place this here as guiref has to be defined if we dont want to lose URLs
254 #if !defined(MAC_OSX) && !defined(WIN32)
255 // TODO: implement qtipcserver.cpp for Mac and Windows
257 // Check for URL in argv
258 for (int i = 1; i < argc; i++)
260 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
262 const char *strURL = argv[i];
264 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
265 mq.try_send(strURL, strlen(strURL), 0);
267 catch (boost::interprocess::interprocess_exception &ex) {
284 } catch (std::exception& e) {
285 PrintException(&e, "Runaway exception");
287 PrintException(NULL, "Runaway exception");
291 #endif // BITCOIN_QT_TEST