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 "ui_interface.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 ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
43 // Message from network thread
46 bool modal = (style & wxMODAL);
47 // in case of modal message, use blocking connection to wait for user to click OK
48 QMetaObject::invokeMethod(guiref, "error",
49 modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
50 Q_ARG(QString, QString::fromStdString(caption)),
51 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)
66 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
70 QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
71 Q_ARG(qint64, nFeeRequired),
72 Q_ARG(bool*, &payFee));
77 void ThreadSafeHandleURI(const std::string& strURI)
82 QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
83 Q_ARG(QString, QString::fromStdString(strURI)));
86 void MainFrameRepaint()
89 QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
91 QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
94 void AddressBookRepaint()
97 QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
100 void InitMessage(const std::string &message)
104 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
105 QApplication::instance()->processEvents();
111 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
115 Translate string to current locale using Qt.
117 std::string _(const char* psz)
119 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
122 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
124 static void handleRunawayException(std::exception *e)
126 PrintExceptionContinue(e, "Runaway exception");
127 QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occured. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
132 #define strncasecmp strnicmp
134 #ifndef BITCOIN_QT_TEST
135 int main(int argc, char *argv[])
137 #if !defined(MAC_OSX) && !defined(WIN32)
138 // TODO: implement qtipcserver.cpp for Mac and Windows
140 // Do this early as we don't want to bother initializing if we are just calling IPC
141 for (int i = 1; i < argc; i++)
143 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
145 const char *strURI = argv[i];
147 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
148 if(mq.try_send(strURI, strlen(strURI), 0))
153 catch (boost::interprocess::interprocess_exception &ex) {
160 // Internal string conversion is all UTF-8
161 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
162 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
164 Q_INIT_RESOURCE(bitcoin);
165 QApplication app(argc, argv);
167 // Command-line options take precedence:
168 ParseParameters(argc, argv);
170 // ... then bitcoin.conf:
171 if (!boost::filesystem::is_directory(GetDataDir(false)))
173 fprintf(stderr, "Error: Specified directory does not exist\n");
176 ReadConfigFile(mapArgs, mapMultiArgs);
178 // Application identification (must be set before OptionsModel is initialized,
179 // as it is used to locate QSettings)
180 app.setOrganizationName("Bitcoin");
181 app.setOrganizationDomain("bitcoin.org");
182 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
183 app.setApplicationName("Bitcoin-Qt-testnet");
185 app.setApplicationName("Bitcoin-Qt");
187 // ... then GUI settings:
188 OptionsModel optionsModel;
190 // Get desired locale ("en_US") from command line or system locale
191 QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
192 // Load language files for configured locale:
193 // - First load the translator for the base language, without territory
194 // - Then load the more specific locale translator
195 QString lang = lang_territory;
197 lang.truncate(lang_territory.lastIndexOf('_')); // "en"
198 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
200 qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
201 if (!qtTranslatorBase.isEmpty())
202 app.installTranslator(&qtTranslatorBase);
204 qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
205 if (!qtTranslator.isEmpty())
206 app.installTranslator(&qtTranslator);
208 translatorBase.load(":/translations/"+lang);
209 if (!translatorBase.isEmpty())
210 app.installTranslator(&translatorBase);
212 translator.load(":/translations/"+lang_territory);
213 if (!translator.isEmpty())
214 app.installTranslator(&translator);
216 QSplashScreen splash(QPixmap(":/images/splash"), 0);
217 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
220 splash.setAutoFillBackground(true);
226 app.setQuitOnLastWindowClosed(false);
232 if(AppInit2(argc, argv))
235 // Put this in a block, so that the Model objects are cleaned up before
236 // calling Shutdown().
238 optionsModel.Upgrade(); // Must be done after AppInit2
241 splash.finish(&window);
243 ClientModel clientModel(&optionsModel);
244 clientmodel = &clientModel;
245 WalletModel walletModel(pwalletMain, &optionsModel);
246 walletmodel = &walletModel;
248 window.setClientModel(&clientModel);
249 window.setWalletModel(&walletModel);
251 // If -min option passed, start window minimized.
252 if(GetBoolArg("-min"))
254 window.showMinimized();
261 // Place this here as guiref has to be defined if we dont want to lose URIs
264 #if !defined(MAC_OSX) && !defined(WIN32)
265 // TODO: implement qtipcserver.cpp for Mac and Windows
267 // Check for URI in argv
268 for (int i = 1; i < argc; i++)
270 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
272 const char *strURI = argv[i];
274 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
275 mq.try_send(strURI, strlen(strURI), 0);
277 catch (boost::interprocess::interprocess_exception &ex) {
285 window.setClientModel(0);
286 window.setWalletModel(0);
291 // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
298 } catch (std::exception& e) {
299 handleRunawayException(&e);
301 handleRunawayException(NULL);
305 #endif // BITCOIN_QT_TEST