Allow setting UI language from commandline (implements #678)
[novacoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 20011-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 main thread
31     if(guiref)
32     {
33         guiref->error(QString::fromStdString(caption),
34                       QString::fromStdString(message));
35     }
36     else
37     {
38         QMessageBox::critical(0, QString::fromStdString(caption),
39             QString::fromStdString(message),
40             QMessageBox::Ok, QMessageBox::Ok);
41     }
42     return 4;
43 }
44
45 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
46 {
47     // Message from network thread
48     if(guiref)
49     {
50         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
51                                    Q_ARG(QString, QString::fromStdString(caption)),
52                                    Q_ARG(QString, QString::fromStdString(message)));
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 ThreadSafeHandleURL(const std::string& strURL)
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, "handleURL", connectionType,
98                                Q_ARG(QString, QString::fromStdString(strURL)));
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 #ifndef BITCOIN_QT_TEST
135 int main(int argc, char *argv[])
136 {
137     // Do this early as we don't want to bother initializing if we are just calling IPC
138     for (int i = 1; i < argc; i++)
139     {
140         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
141         {
142             const char *strURL = argv[i];
143             try {
144                 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
145                 if(mq.try_send(strURL, strlen(strURL), 0))
146                     exit(0);
147                 else
148                     break;
149             }
150             catch (boost::interprocess::interprocess_exception &ex) {
151                 break;
152             }
153         }
154     }
155
156     // Internal string conversion is all UTF-8
157     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
158     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
159
160     Q_INIT_RESOURCE(bitcoin);
161     QApplication app(argc, argv);
162
163     ParseParameters(argc, argv);
164
165     // Get desired locale ("en_US") from command line or system locale
166     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
167     // Load language files for configured locale:
168     // - First load the translator for the base language, without territory
169     // - Then load the more specific locale translator
170     QString lang = lang_territory;
171
172     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
173     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
174
175     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
176     if (!qtTranslatorBase.isEmpty())
177         app.installTranslator(&qtTranslatorBase);
178
179     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
180     if (!qtTranslator.isEmpty())
181         app.installTranslator(&qtTranslator);
182
183     translatorBase.load(":/translations/"+lang);
184     if (!translatorBase.isEmpty())
185         app.installTranslator(&translatorBase);
186
187     translator.load(":/translations/"+lang_territory);
188     if (!translator.isEmpty())
189         app.installTranslator(&translator);
190
191     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
192
193     QSplashScreen splash(QPixmap(":/images/splash"), 0);
194     splash.show();
195     splash.setAutoFillBackground(true);
196     splashref = &splash;
197
198     app.processEvents();
199
200     app.setQuitOnLastWindowClosed(false);
201
202     try
203     {
204         if(AppInit2(argc, argv))
205         {
206             {
207                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
208                 // calling Shutdown() in case of exceptions.
209                 BitcoinGUI window;
210                 splash.finish(&window);
211                 OptionsModel optionsModel(pwalletMain);
212                 ClientModel clientModel(&optionsModel);
213                 WalletModel walletModel(pwalletMain, &optionsModel);
214
215                 guiref = &window;
216                 window.setClientModel(&clientModel);
217                 window.setWalletModel(&walletModel);
218
219                 // If -min option passed, start window minimized.
220                 if(GetBoolArg("-min"))
221                 {
222                     window.showMinimized();
223                 }
224                 else
225                 {
226                     window.show();
227                 }
228
229                 // Place this here as guiref has to be defined if we dont want to lose URLs
230                 ipcInit();
231                 // Check for URL in argv
232                 for (int i = 1; i < argc; i++)
233                 {
234                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
235                     {
236                         const char *strURL = argv[i];
237                         try {
238                             boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
239                             mq.try_send(strURL, strlen(strURL), 0);
240                         }
241                         catch (boost::interprocess::interprocess_exception &ex) {
242                         }
243                     }
244                 }
245
246                 app.exec();
247
248                 guiref = 0;
249             }
250             Shutdown(NULL);
251         }
252         else
253         {
254             return 1;
255         }
256     } catch (std::exception& e) {
257         PrintException(&e, "Runaway exception");
258     } catch (...) {
259         PrintException(NULL, "Runaway exception");
260     }
261     return 0;
262 }
263 #endif // BITCOIN_QT_TEST