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