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