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