]> Git Repo - VerusCoin.git/blob - src/qt/bitcoin.cpp
Merge branch 'refactor_times' of git://github.com/luke-jr/bitcoin
[VerusCoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 2011-2012
3  */
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
8 #include "guiutil.h"
9 #include "guiconstants.h"
10
11 #include "init.h"
12 #include "ui_interface.h"
13 #include "qtipcserver.h"
14
15 #include <QApplication>
16 #include <QMessageBox>
17 #include <QTextCodec>
18 #include <QLocale>
19 #include <QTranslator>
20 #include <QSplashScreen>
21 #include <QLibraryInfo>
22
23 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
24 #define _BITCOIN_QT_PLUGINS_INCLUDED
25 #define __INSURE__
26 #include <QtPlugin>
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)
32 #endif
33
34 // Need a global reference for the notifications to find the GUI
35 static BitcoinGUI *guiref;
36 static QSplashScreen *splashref;
37
38 static void ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
39 {
40     // Message from network thread
41     if(guiref)
42     {
43         bool modal = (style & CClientUIInterface::MODAL);
44         // in case of modal message, use blocking connection to wait for user to click OK
45         QMetaObject::invokeMethod(guiref, "error",
46                                    modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
47                                    Q_ARG(QString, QString::fromStdString(caption)),
48                                    Q_ARG(QString, QString::fromStdString(message)),
49                                    Q_ARG(bool, modal));
50     }
51     else
52     {
53         printf("%s: %s\n", caption.c_str(), message.c_str());
54         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
55     }
56 }
57
58 static bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption)
59 {
60     if(!guiref)
61         return false;
62     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
63         return true;
64     bool payFee = false;
65
66     QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
67                                Q_ARG(qint64, nFeeRequired),
68                                Q_ARG(bool*, &payFee));
69
70     return payFee;
71 }
72
73 static void ThreadSafeHandleURI(const std::string& strURI)
74 {
75     if(!guiref)
76         return;
77
78     QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
79                                Q_ARG(QString, QString::fromStdString(strURI)));
80 }
81
82 static void InitMessage(const std::string &message)
83 {
84     if(splashref)
85     {
86         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
87         QApplication::instance()->processEvents();
88     }
89 }
90
91 static void QueueShutdown()
92 {
93     QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
94 }
95
96 /*
97    Translate string to current locale using Qt.
98  */
99 static std::string Translate(const char* psz)
100 {
101     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
102 }
103
104 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
105  */
106 static void handleRunawayException(std::exception *e)
107 {
108     PrintExceptionContinue(e, "Runaway exception");
109     QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
110     exit(1);
111 }
112
113 #ifndef BITCOIN_QT_TEST
114 int main(int argc, char *argv[])
115 {
116     // Do this early as we don't want to bother initializing if we are just calling IPC
117     ipcScanRelay(argc, argv);
118
119     // Internal string conversion is all UTF-8
120     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
121     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
122
123     Q_INIT_RESOURCE(bitcoin);
124     QApplication app(argc, argv);
125
126     // Install global event filter that makes sure that long tooltips can be word-wrapped
127     app.installEventFilter(new GUIUtil::ToolTipToRichTextFilter(TOOLTIP_WRAP_THRESHOLD, &app));
128
129     // Command-line options take precedence:
130     ParseParameters(argc, argv);
131
132     // ... then bitcoin.conf:
133     if (!boost::filesystem::is_directory(GetDataDir(false)))
134     {
135         fprintf(stderr, "Error: Specified directory does not exist\n");
136         return 1;
137     }
138     ReadConfigFile(mapArgs, mapMultiArgs);
139
140     // Application identification (must be set before OptionsModel is initialized,
141     // as it is used to locate QSettings)
142     app.setOrganizationName("Bitcoin");
143     app.setOrganizationDomain("bitcoin.org");
144     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
145         app.setApplicationName("Bitcoin-Qt-testnet");
146     else
147         app.setApplicationName("Bitcoin-Qt");
148
149     // ... then GUI settings:
150     OptionsModel optionsModel;
151
152     // Get desired locale (e.g. "de_DE") from command line or use system locale
153     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
154     QString lang = lang_territory;
155     // Convert to "de" only by truncating "_DE"
156     lang.truncate(lang_territory.lastIndexOf('_'));
157
158     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
159     // Load language files for configured locale:
160     // - First load the translator for the base language, without territory
161     // - Then load the more specific locale translator
162
163     // Load e.g. qt_de.qm
164     if (qtTranslatorBase.load("qt_" + lang, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
165         app.installTranslator(&qtTranslatorBase);
166
167     // Load e.g. qt_de_DE.qm
168     if (qtTranslator.load("qt_" + lang_territory, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
169         app.installTranslator(&qtTranslator);
170
171     // Load e.g. bitcoin_de.qm (shortcut "de" needs to be defined in bitcoin.qrc)
172     if (translatorBase.load(lang, ":/translations/"))
173         app.installTranslator(&translatorBase);
174
175     // Load e.g. bitcoin_de_DE.qm (shortcut "de_DE" needs to be defined in bitcoin.qrc)
176     if (translator.load(lang_territory, ":/translations/"))
177         app.installTranslator(&translator);
178
179     // Subscribe to global signals from core
180     uiInterface.ThreadSafeMessageBox.connect(ThreadSafeMessageBox);
181     uiInterface.ThreadSafeAskFee.connect(ThreadSafeAskFee);
182     uiInterface.ThreadSafeHandleURI.connect(ThreadSafeHandleURI);
183     uiInterface.InitMessage.connect(InitMessage);
184     uiInterface.QueueShutdown.connect(QueueShutdown);
185     uiInterface.Translate.connect(Translate);
186
187     // Show help message immediately after parsing command-line options (for "-lang") and setting locale,
188     // but before showing splash screen.
189     if (mapArgs.count("-?") || mapArgs.count("--help"))
190     {
191         GUIUtil::HelpMessageBox help;
192         help.showOrPrint();
193         return 1;
194     }
195
196     QSplashScreen splash(QPixmap(":/images/splash"), 0);
197     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
198     {
199         splash.show();
200         splash.setAutoFillBackground(true);
201         splashref = &splash;
202     }
203
204     app.processEvents();
205
206     app.setQuitOnLastWindowClosed(false);
207
208     try
209     {
210         // Regenerate startup link, to fix links to old versions
211         if (GUIUtil::GetStartOnSystemStartup())
212             GUIUtil::SetStartOnSystemStartup(true);
213
214         BitcoinGUI window;
215         guiref = &window;
216         if(AppInit2())
217         {
218             {
219                 // Put this in a block, so that the Model objects are cleaned up before
220                 // calling Shutdown().
221
222                 optionsModel.Upgrade(); // Must be done after AppInit2
223
224                 if (splashref)
225                     splash.finish(&window);
226
227                 ClientModel clientModel(&optionsModel);
228                 WalletModel walletModel(pwalletMain, &optionsModel);
229
230                 window.setClientModel(&clientModel);
231                 window.setWalletModel(&walletModel);
232
233                 // If -min option passed, start window minimized.
234                 if(GetBoolArg("-min"))
235                 {
236                     window.showMinimized();
237                 }
238                 else
239                 {
240                     window.show();
241                 }
242
243                 // Place this here as guiref has to be defined if we don't want to lose URIs
244                 ipcInit(argc, argv);
245
246                 app.exec();
247
248                 window.hide();
249                 window.setClientModel(0);
250                 window.setWalletModel(0);
251                 guiref = 0;
252             }
253             // Shutdown the core and its threads, but don't exit Bitcoin-Qt here
254             Shutdown(NULL);
255         }
256         else
257         {
258             return 1;
259         }
260     } catch (std::exception& e) {
261         handleRunawayException(&e);
262     } catch (...) {
263         handleRunawayException(NULL);
264     }
265     return 0;
266 }
267 #endif // BITCOIN_QT_TEST
This page took 0.041856 seconds and 4 git commands to generate.