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 /*
127    Translate string to current locale using Qt.
128  */
129 std::string _(const char* psz)
130 {
131     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
132 }
133
134 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
135  */
136 static void handleRunawayException(std::exception *e)
137 {
138     PrintExceptionContinue(e, "Runaway exception");
139     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));
140     exit(1);
141 }
142
143 #ifndef BITCOIN_QT_TEST
144 int main(int argc, char *argv[])
145 {
146 #if !defined(MAC_OSX) && !defined(WIN32)
147 // TODO: implement qtipcserver.cpp for Mac and Windows
148
149     // Do this early as we don't want to bother initializing if we are just calling IPC
150     for (int i = 1; i < argc; i++)
151     {
152         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
153         {
154             const char *strURI = argv[i];
155             try {
156                 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
157                 if(mq.try_send(strURI, strlen(strURI), 0))
158                     exit(0);
159                 else
160                     break;
161             }
162             catch (boost::interprocess::interprocess_exception &ex) {
163                 break;
164             }
165         }
166     }
167 #endif
168
169     // Internal string conversion is all UTF-8
170     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
171     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
172
173     Q_INIT_RESOURCE(bitcoin);
174     QApplication app(argc, argv);
175
176     // Command-line options take precedence:
177     ParseParameters(argc, argv);
178
179     // ... then bitcoin.conf:
180     if (!ReadConfigFile(mapArgs, mapMultiArgs))
181     {
182         fprintf(stderr, "Error: Specified directory does not exist\n");
183         return 1;
184     }
185
186     // Application identification (must be set before OptionsModel is initialized,
187     // as it is used to locate QSettings)
188     app.setOrganizationName("Bitcoin");
189     app.setOrganizationDomain("bitcoin.org");
190     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
191         app.setApplicationName("Bitcoin-Qt-testnet");
192     else
193         app.setApplicationName("Bitcoin-Qt");
194
195     // ... then GUI settings:
196     OptionsModel optionsModel;
197
198     // Get desired locale ("en_US") from command line or system locale
199     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
200     // Load language files for configured locale:
201     // - First load the translator for the base language, without territory
202     // - Then load the more specific locale translator
203     QString lang = lang_territory;
204
205     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
206     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
207
208     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
209     if (!qtTranslatorBase.isEmpty())
210         app.installTranslator(&qtTranslatorBase);
211
212     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
213     if (!qtTranslator.isEmpty())
214         app.installTranslator(&qtTranslator);
215
216     translatorBase.load(":/translations/"+lang);
217     if (!translatorBase.isEmpty())
218         app.installTranslator(&translatorBase);
219
220     translator.load(":/translations/"+lang_territory);
221     if (!translator.isEmpty())
222         app.installTranslator(&translator);
223
224     QSplashScreen splash(QPixmap(":/images/splash"), 0);
225     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
226     {
227         splash.show();
228         splash.setAutoFillBackground(true);
229         splashref = &splash;
230     }
231
232     app.processEvents();
233
234     app.setQuitOnLastWindowClosed(false);
235
236     try
237     {
238         if(AppInit2(argc, argv))
239         {
240             {
241                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
242                 // calling Shutdown() in case of exceptions.
243
244                 optionsModel.Upgrade(); // Must be done after AppInit2
245
246                 BitcoinGUI window;
247                 if (splashref)
248                     splash.finish(&window);
249
250                 ClientModel clientModel(&optionsModel);
251                 WalletModel walletModel(pwalletMain, &optionsModel);
252
253                 guiref = &window;
254                 window.setClientModel(&clientModel);
255                 window.setWalletModel(&walletModel);
256
257                 // If -min option passed, start window minimized.
258                 if(GetBoolArg("-min"))
259                 {
260                     window.showMinimized();
261                 }
262                 else
263                 {
264                     window.show();
265                 }
266
267                 // Place this here as guiref has to be defined if we dont want to lose URIs
268                 ipcInit();
269
270 #if !defined(MAC_OSX) && !defined(WIN32)
271 // TODO: implement qtipcserver.cpp for Mac and Windows
272
273                 // Check for URI in argv
274                 for (int i = 1; i < argc; i++)
275                 {
276                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
277                     {
278                         const char *strURI = argv[i];
279                         try {
280                             boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
281                             mq.try_send(strURI, strlen(strURI), 0);
282                         }
283                         catch (boost::interprocess::interprocess_exception &ex) {
284                         }
285                     }
286                 }
287 #endif
288                 app.exec();
289
290                 guiref = 0;
291             }
292             Shutdown(NULL);
293         }
294         else
295         {
296             return 1;
297         }
298     } catch (std::exception& e) {
299         handleRunawayException(&e);
300     } catch (...) {
301         handleRunawayException(NULL);
302     }
303     return 0;
304 }
305 #endif // BITCOIN_QT_TEST