2 * W.J. van der Laan 2011-2012
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
9 #include "guiconstants.h"
13 #include "interface.h"
14 #include "qtipcserver.h"
17 #include <QApplication>
18 #include <QMessageBox>
19 #if QT_VERSION < 0x050000
23 #include <QTranslator>
24 #include <QSplashScreen>
25 #include <QLibraryInfo>
28 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
29 #define _BITCOIN_QT_PLUGINS_INCLUDED
32 Q_IMPORT_PLUGIN(qcncodecs)
33 Q_IMPORT_PLUGIN(qjpcodecs)
34 Q_IMPORT_PLUGIN(qtwcodecs)
35 Q_IMPORT_PLUGIN(qkrcodecs)
36 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
39 // Need a global reference for the notifications to find the GUI
40 static BitcoinGUI *guiref;
41 static QSplashScreen *splashref;
43 /** Set up translations */
44 static void initTranslations(QTranslator &qtTranslatorBase, QTranslator &qtTranslator, QTranslator &translatorBase, QTranslator &translator)
47 // Get desired locale (e.g. "de_DE")
48 // 1) System default language
49 QString lang_territory = QLocale::system().name();
50 // 2) Language from QSettings
51 QString lang_territory_qsettings = settings.value("language", "").toString();
52 if(!lang_territory_qsettings.isEmpty())
53 lang_territory = lang_territory_qsettings;
54 // 3) -lang command line argument
55 lang_territory = QString::fromStdString(GetArg("-lang", lang_territory.toStdString()));
56 // Convert to "de" only by truncating "_DE"
57 QString lang = lang_territory;
58 lang.truncate(lang_territory.lastIndexOf('_'));
59 // Load language files for configured locale:
60 // - First load the translator for the base language, without territory
61 // - Then load the more specific locale translator
63 if (qtTranslatorBase.load("qt_" + lang, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
64 QApplication::installTranslator(&qtTranslatorBase);
65 // Load e.g. qt_de_DE.qm
66 if (qtTranslator.load("qt_" + lang_territory, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
67 QApplication::installTranslator(&qtTranslator);
68 // Load e.g. bitcoin_de.qm (shortcut "de" needs to be defined in bitcoin.qrc)
69 if (translatorBase.load(lang, ":/translations/"))
70 QApplication::installTranslator(&translatorBase);
71 // Load e.g. bitcoin_de_DE.qm (shortcut "de_DE" needs to be defined in bitcoin.qrc)
72 if (translator.load(lang_territory, ":/translations/"))
73 QApplication::installTranslator(&translator);
76 static void ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
78 // Message from network thread
81 bool modal = (style & CClientUIInterface::MODAL);
82 // in case of modal message, use blocking connection to wait for user to click OK
83 QMetaObject::invokeMethod(guiref, "error",
84 modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
85 Q_ARG(QString, QString::fromStdString(caption)),
86 Q_ARG(QString, QString::fromStdString(message)),
91 printf("%s: %s\n", caption.c_str(), message.c_str());
92 fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
96 static bool ThreadSafeAskFee(int64_t nFeeRequired, const std::string& strCaption)
100 if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
104 QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
105 Q_ARG(qint64, nFeeRequired),
106 Q_ARG(bool*, &payFee));
111 static void ThreadSafeHandleURI(const std::string& strURI)
116 QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
117 Q_ARG(QString, QString::fromStdString(strURI)));
120 static void InitMessage(const std::string &message)
124 splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
125 QApplication::instance()->processEvents();
129 static void QueueShutdown()
131 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
135 Translate string to current locale using Qt.
137 static std::string Translate(const char* psz)
139 return QCoreApplication::translate("bitcoin-core", psz).toStdString();
142 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
144 static void handleRunawayException(std::exception *e)
146 PrintExceptionContinue(e, "Runaway exception");
147 QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. NovaCoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
151 #ifndef BITCOIN_QT_TEST
152 int main(int argc, char *argv[])
154 // Do this early as we don't want to bother initializing if we are just calling IPC
155 ipcScanRelay(argc, argv);
156 #if QT_VERSION < 0x050000
157 // Internal string conversion is all UTF-8
158 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
159 QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
161 Q_INIT_RESOURCE(bitcoin);
162 QApplication app(argc, argv);
164 // Application identification (must be set before OptionsModel is initialized,
165 // as it is used to locate QSettings)
166 app.setOrganizationName("NovaCoin");
167 app.setOrganizationDomain("novacoin.su");
168 if(GetBoolArg("-testnet")) // Separate UI settings for testnet
169 app.setApplicationName("NovaCoin-Qt-testnet");
171 app.setApplicationName("NovaCoin-Qt");
172 // Now that QSettings are accessible, initialize translations
173 QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
174 initTranslations(qtTranslatorBase, qtTranslator, translatorBase, translator);
176 // Command-line options take precedence:
177 ParseParameters(argc, argv);
179 // User language is set up: pick a data directory
180 Intro::pickDataDirectory();
182 // Install global event filter that makes sure that long tooltips can be word-wrapped
183 app.installEventFilter(new GUIUtil::ToolTipToRichTextFilter(TOOLTIP_WRAP_THRESHOLD, &app));
185 // ... then bitcoin.conf:
186 if (!boost::filesystem::is_directory(GetDataDir(false)))
188 QMessageBox::critical(0, "NovaCoin",
189 QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(mapArgs["-datadir"])));
192 ReadConfigFile(mapArgs, mapMultiArgs);
194 // ... then GUI settings:
195 OptionsModel optionsModel;
197 // Subscribe to global signals from core
198 uiInterface.ThreadSafeMessageBox.connect(ThreadSafeMessageBox);
199 uiInterface.ThreadSafeAskFee.connect(ThreadSafeAskFee);
200 uiInterface.ThreadSafeHandleURI.connect(ThreadSafeHandleURI);
201 uiInterface.InitMessage.connect(InitMessage);
202 uiInterface.QueueShutdown.connect(QueueShutdown);
203 uiInterface.Translate.connect(Translate);
205 // Show help message immediately after parsing command-line options (for "-lang") and setting locale,
206 // but before showing splash screen.
207 if (mapArgs.count("-?") || mapArgs.count("--help"))
209 GUIUtil::HelpMessageBox help;
214 QSplashScreen splash(QPixmap(":/images/splash"), 0);
215 if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
218 splash.setAutoFillBackground(true);
224 app.setQuitOnLastWindowClosed(false);
228 // Regenerate startup link, to fix links to old versions
229 if (GUIUtil::GetStartOnSystemStartup())
230 GUIUtil::SetStartOnSystemStartup(true);
237 // Put this in a block, so that the Model objects are cleaned up before
238 // calling Shutdown().
241 splash.finish(&window);
243 ClientModel clientModel(&optionsModel);
244 WalletModel walletModel(pwalletMain, &optionsModel);
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 don't want to lose URIs
265 window.setClientModel(0);
266 window.setWalletModel(0);
269 // Shutdown the core and its threads, but don't exit Bitcoin-Qt here
276 } catch (std::exception& e) {
277 handleRunawayException(&e);
279 handleRunawayException(NULL);
283 #endif // BITCOIN_QT_TEST