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>
17 #include <QTranslator>
18 #include <QSplashScreen>
19 #include <QLibraryInfo>
21 #include <boost/interprocess/ipc/message_queue.hpp>
23 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
24 #define _BITCOIN_QT_PLUGINS_INCLUDED
27 Q_IMPORT_PLUGIN(qcncodecs)
28 Q_IMPORT_PLUGIN(qjpcodecs)
29 Q_IMPORT_PLUGIN(qtwcodecs)
30 Q_IMPORT_PLUGIN(qkrcodecs)
31 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
34 // Need a global reference for the notifications to find the GUI
35 static BitcoinGUI *guiref;
36 static QSplashScreen *splashref;
37 static WalletModel *walletmodel;
38 static ClientModel *clientmodel;
40 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
42 // Message from network thread
45 bool modal = (style & wxMODAL);
46 // in case of modal message, use blocking connection to wait for user to click OK
47 QMetaObject::invokeMethod(guiref, "error",
48 modal ? GUIUtil::blockingGUIThreadConnection() : 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)
65 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
69 QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
70 Q_ARG(qint64, nFeeRequired),
71 Q_ARG(bool*, &payFee));
76 void ThreadSafeHandleURI(const std::string& strURI)
81 QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
82 Q_ARG(QString, QString::fromStdString(strURI)));
85 void MainFrameRepaint()
88 QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
90 QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
93 void AddressBookRepaint()
96 QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
99 void InitMessage(const std::string &message)
103 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
104 QApplication::instance()->processEvents();
110 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
114 Translate string to current locale using Qt.
116 std::string _(const char* psz)
118 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
121 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
123 static void handleRunawayException(std::exception *e)
125 PrintExceptionContinue(e, "Runaway exception");
126 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));
131 #define strncasecmp strnicmp
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);
230 if(AppInit2(argc, argv))
233 // Put this in a block, so that the Model objects are cleaned up before
234 // calling Shutdown().
236 optionsModel.Upgrade(); // Must be done after AppInit2
239 splash.finish(&window);
241 ClientModel clientModel(&optionsModel);
242 clientmodel = &clientModel;
243 WalletModel walletModel(pwalletMain, &optionsModel);
244 walletmodel = &walletModel;
246 window.setClientModel(&clientModel);
247 window.setWalletModel(&walletModel);
249 // If -min option passed, start window minimized.
250 if(GetBoolArg("-min"))
252 window.showMinimized();
259 // Place this here as guiref has to be defined if we dont want to lose URIs
262 #if !defined(MAC_OSX) && !defined(WIN32)
263 // TODO: implement qtipcserver.cpp for Mac and Windows
265 // Check for URI in argv
266 for (int i = 1; i < argc; i++)
268 if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
270 const char *strURI = argv[i];
272 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
273 mq.try_send(strURI, strlen(strURI), 0);
275 catch (boost::interprocess::interprocess_exception &ex) {
282 window.setClientModel(0);
283 window.setWalletModel(0);
294 } catch (std::exception& e) {
295 handleRunawayException(&e);
297 handleRunawayException(NULL);
301 #endif // BITCOIN_QT_TEST