Merge branch '0.5.x' into 0.6.0.x
[novacoin.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
9 #include "headers.h"
10 #include "init.h"
11 #include "qtipcserver.h"
12 #include "util.h"
13
14 #include <QApplication>
15 #include <QMessageBox>
16 #include <QThread>
17 #include <QTextCodec>
18 #include <QLocale>
19 #include <QTranslator>
20 #include <QSplashScreen>
21 #include <QLibraryInfo>
22
23 #include <boost/interprocess/ipc/message_queue.hpp>
24
25 // Need a global reference for the notifications to find the GUI
26 BitcoinGUI *guiref;
27 QSplashScreen *splashref;
28
29 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
30 {
31     // Message from AppInit2(), always in main thread before main window is constructed
32     QMessageBox::critical(0, QString::fromStdString(caption),
33         QString::fromStdString(message),
34         QMessageBox::Ok, QMessageBox::Ok);
35     return 4;
36 }
37
38 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
39 {
40     bool modal = style & wxMODAL;
41
42     if (modal)
43         while (!guiref)
44             Sleep(1000);
45
46     // Message from network thread
47     if(guiref)
48     {
49         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
50                                    Q_ARG(QString, QString::fromStdString(caption)),
51                                    Q_ARG(QString, QString::fromStdString(message)),
52                                    Q_ARG(bool, modal));
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     }
59     return 4;
60 }
61
62 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
63 {
64     if(!guiref)
65         return false;
66     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
67         return true;
68     bool payFee = false;
69
70     // Call slot on GUI thread.
71     // If called from another thread, use a blocking QueuedConnection.
72     Qt::ConnectionType connectionType = Qt::DirectConnection;
73     if(QThread::currentThread() != QCoreApplication::instance()->thread())
74     {
75         connectionType = Qt::BlockingQueuedConnection;
76     }
77
78     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
79                                Q_ARG(qint64, nFeeRequired),
80                                Q_ARG(bool*, &payFee));
81
82     return payFee;
83 }
84
85 void ThreadSafeHandleURI(const std::string& strURI)
86 {
87     if(!guiref)
88         return;
89
90     // Call slot on GUI thread.
91     // If called from another thread, use a blocking QueuedConnection.
92     Qt::ConnectionType connectionType = Qt::DirectConnection;
93     if(QThread::currentThread() != QCoreApplication::instance()->thread())
94     {
95         connectionType = Qt::BlockingQueuedConnection;
96     }
97     QMetaObject::invokeMethod(guiref, "handleURI", connectionType,
98                                Q_ARG(QString, QString::fromStdString(strURI)));
99 }
100
101 void CalledSetStatusBar(const std::string& strText, int nField)
102 {
103     // Only used for built-in mining, which is disabled, simple ignore
104 }
105
106 void UIThreadCall(boost::function0<void> fn)
107 {
108     // Only used for built-in mining, which is disabled, simple ignore
109 }
110
111 void MainFrameRepaint()
112 {
113     if(guiref)
114         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
115 }
116
117 void InitMessage(const std::string &message)
118 {
119     if(splashref)
120     {
121         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
122         QApplication::instance()->processEvents();
123     }
124 }
125
126 void QueueShutdown()
127 {
128     QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
129 }
130
131 /*
132    Translate string to current locale using Qt.
133  */
134 std::string _(const char* psz)
135 {
136     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
137 }
138
139 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
140  */
141 static void handleRunawayException(std::exception *e)
142 {
143     PrintExceptionContinue(e, "Runaway exception");
144     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));
145     exit(1);
146 }
147
148 #ifndef BITCOIN_QT_TEST
149 int main(int argc, char *argv[])
150 {
151 #if !defined(MAC_OSX) && !defined(WIN32)
152 // TODO: implement qtipcserver.cpp for Mac and Windows
153
154     // Do this early as we don't want to bother initializing if we are just calling IPC
155     for (int i = 1; i < argc; i++)
156     {
157         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
158         {
159             const char *strURI = argv[i];
160             try {
161                 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
162                 if(mq.try_send(strURI, strlen(strURI), 0))
163                     exit(0);
164                 else
165                     break;
166             }
167             catch (boost::interprocess::interprocess_exception &ex) {
168                 break;
169             }
170         }
171     }
172 #endif
173
174     // Internal string conversion is all UTF-8
175     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
176     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
177
178     Q_INIT_RESOURCE(bitcoin);
179     QApplication app(argc, argv);
180
181     // Command-line options take precedence:
182     ParseParameters(argc, argv);
183
184     // ... then bitcoin.conf:
185     if (!ReadConfigFile(mapArgs, mapMultiArgs))
186     {
187         fprintf(stderr, "Error: Specified directory does not exist\n");
188         return 1;
189     }
190
191     // Application identification (must be set before OptionsModel is initialized,
192     // as it is used to locate QSettings)
193     app.setOrganizationName("Bitcoin");
194     app.setOrganizationDomain("bitcoin.org");
195     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
196         app.setApplicationName("Bitcoin-Qt-testnet");
197     else
198         app.setApplicationName("Bitcoin-Qt");
199
200     // ... then GUI settings:
201     OptionsModel optionsModel;
202
203     // Get desired locale ("en_US") from command line or system locale
204     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
205     // Load language files for configured locale:
206     // - First load the translator for the base language, without territory
207     // - Then load the more specific locale translator
208     QString lang = lang_territory;
209
210     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
211     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
212
213     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
214     if (!qtTranslatorBase.isEmpty())
215         app.installTranslator(&qtTranslatorBase);
216
217     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
218     if (!qtTranslator.isEmpty())
219         app.installTranslator(&qtTranslator);
220
221     translatorBase.load(":/translations/"+lang);
222     if (!translatorBase.isEmpty())
223         app.installTranslator(&translatorBase);
224
225     translator.load(":/translations/"+lang_territory);
226     if (!translator.isEmpty())
227         app.installTranslator(&translator);
228
229     QSplashScreen splash(QPixmap(":/images/splash"), 0);
230     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
231     {
232         splash.show();
233         splash.setAutoFillBackground(true);
234         splashref = &splash;
235     }
236
237     app.processEvents();
238
239     app.setQuitOnLastWindowClosed(false);
240
241     try
242     {
243         if(AppInit2(argc, argv))
244         {
245             {
246                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
247                 // calling Shutdown() in case of exceptions.
248
249                 optionsModel.Upgrade(); // Must be done after AppInit2
250
251                 BitcoinGUI window;
252                 if (splashref)
253                     splash.finish(&window);
254
255                 ClientModel clientModel(&optionsModel);
256                 WalletModel walletModel(pwalletMain, &optionsModel);
257
258                 guiref = &window;
259                 window.setClientModel(&clientModel);
260                 window.setWalletModel(&walletModel);
261
262                 // If -min option passed, start window minimized.
263                 if(GetBoolArg("-min"))
264                 {
265                     window.showMinimized();
266                 }
267                 else
268                 {
269                     window.show();
270                 }
271
272                 // Place this here as guiref has to be defined if we dont want to lose URIs
273                 ipcInit();
274
275 #if !defined(MAC_OSX) && !defined(WIN32)
276 // TODO: implement qtipcserver.cpp for Mac and Windows
277
278                 // Check for URI in argv
279                 for (int i = 1; i < argc; i++)
280                 {
281                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
282                     {
283                         const char *strURI = argv[i];
284                         try {
285                             boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
286                             mq.try_send(strURI, strlen(strURI), 0);
287                         }
288                         catch (boost::interprocess::interprocess_exception &ex) {
289                         }
290                     }
291                 }
292 #endif
293                 app.exec();
294
295                 window.hide();
296                 guiref = 0;
297             }
298             // Shutdown the core and it's threads, but don't exit Bitcoin-Qt here
299             Shutdown(NULL);
300         }
301         else
302         {
303             return 1;
304         }
305     } catch (std::exception& e) {
306         handleRunawayException(&e);
307     } catch (...) {
308         handleRunawayException(NULL);
309     }
310     return 0;
311 }
312 #endif // BITCOIN_QT_TEST