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