1133f122e733d35020dfb08d59b774a084742285
[novacoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 2011
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 "util.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 // Need a global reference for the notifications to find the GUI
23 BitcoinGUI *guiref;
24 QSplashScreen *splashref;
25
26 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
27 {
28     // Message from main thread
29     if(guiref)
30     {
31         guiref->error(QString::fromStdString(caption),
32                       QString::fromStdString(message));
33     }
34     else
35     {
36         QMessageBox::critical(0, QString::fromStdString(caption),
37             QString::fromStdString(message),
38             QMessageBox::Ok, QMessageBox::Ok);
39     }
40     return 4;
41 }
42
43 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
44 {
45     bool modal = style & wxMODAL;
46
47     if (modal)
48         while (!guiref)
49             Sleep(1000);
50
51     // Message from network thread
52     if(guiref)
53     {
54         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
55                                    Q_ARG(QString, QString::fromStdString(caption)),
56                                    Q_ARG(QString, QString::fromStdString(message)),
57                                    Q_ARG(bool, modal));
58     }
59     else
60     {
61         printf("%s: %s\n", caption.c_str(), message.c_str());
62         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
63     }
64     return 4;
65 }
66
67 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
68 {
69     if(!guiref)
70         return false;
71     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
72         return true;
73     bool payFee = false;
74
75     // Call slot on GUI thread.
76     // If called from another thread, use a blocking QueuedConnection.
77     Qt::ConnectionType connectionType = Qt::DirectConnection;
78     if(QThread::currentThread() != QCoreApplication::instance()->thread())
79     {
80         connectionType = Qt::BlockingQueuedConnection;
81     }
82
83     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
84                                Q_ARG(qint64, nFeeRequired),
85                                Q_ARG(bool*, &payFee));
86
87     return payFee;
88 }
89
90 void CalledSetStatusBar(const std::string& strText, int nField)
91 {
92     // Only used for built-in mining, which is disabled, simple ignore
93 }
94
95 void UIThreadCall(boost::function0<void> fn)
96 {
97     // Only used for built-in mining, which is disabled, simple ignore
98 }
99
100 void MainFrameRepaint()
101 {
102     if(guiref)
103         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
104 }
105
106 void InitMessage(const std::string &message)
107 {
108     if(splashref)
109     {
110         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
111         QApplication::instance()->processEvents();
112     }
113 }
114
115 /*
116    Translate string to current locale using Qt.
117  */
118 std::string _(const char* psz)
119 {
120     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
121 }
122
123 int main(int argc, char *argv[])
124 {
125     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
126     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
127
128     Q_INIT_RESOURCE(bitcoin);
129     QApplication app(argc, argv);
130
131     // Load language files for system locale:
132     // - First load the translator for the base language, without territory
133     // - Then load the more specific locale translator
134     QString lang_territory = QLocale::system().name(); // "en_US"
135     QString lang = lang_territory;
136     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
137     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
138
139     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
140     if (!qtTranslatorBase.isEmpty())
141         app.installTranslator(&qtTranslatorBase);
142
143     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
144     if (!qtTranslator.isEmpty())
145         app.installTranslator(&qtTranslator);
146
147     translatorBase.load(":/translations/"+lang);
148     if (!translatorBase.isEmpty())
149         app.installTranslator(&translatorBase);
150
151     translator.load(":/translations/"+lang_territory);
152     if (!translator.isEmpty())
153         app.installTranslator(&translator);
154
155     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
156
157     QSplashScreen splash(QPixmap(":/images/splash"), 0);
158     if (!GetBoolArg("-min"))
159     {
160         splash.show();
161         splash.setAutoFillBackground(true);
162         splashref = &splash;
163     }
164
165     app.processEvents();
166
167     app.setQuitOnLastWindowClosed(false);
168
169     try
170     {
171         if(AppInit2(argc, argv))
172         {
173             {
174                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
175                 // calling Shutdown().
176                 BitcoinGUI window;
177                 if (splashref)
178                     splash.finish(&window);
179                 OptionsModel optionsModel(pwalletMain);
180                 ClientModel clientModel(&optionsModel);
181                 WalletModel walletModel(pwalletMain, &optionsModel);
182
183                 guiref = &window;
184                 window.setClientModel(&clientModel);
185                 window.setWalletModel(&walletModel);
186
187                 window.show();
188
189                 app.exec();
190
191                 guiref = 0;
192             }
193             Shutdown(NULL);
194         }
195         else
196         {
197             return 1;
198         }
199     } catch (std::exception& e) {
200         PrintException(&e, "Runaway exception");
201     } catch (...) {
202         PrintException(NULL, "Runaway exception");
203     }
204     return 0;
205 }