windows build fixes
[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
8 #include "headers.h"
9 #include "init.h"
10
11 #include <QApplication>
12 #include <QMessageBox>
13 #include <QThread>
14 #include <QLocale>
15 #include <QTranslator>
16
17 // Need a global reference for the notifications to find the GUI
18 BitcoinGUI *guiref;
19
20 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
21 {
22     // Message from main thread
23     if(guiref)
24     {
25         guiref->error(QString::fromStdString(caption),
26                       QString::fromStdString(message));
27     }
28     else
29     {
30         QMessageBox::critical(0, QString::fromStdString(caption),
31             QString::fromStdString(message),
32             QMessageBox::Ok, QMessageBox::Ok);
33     }
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     // Message from network thread
40     if(guiref)
41     {
42         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
43                                    Q_ARG(QString, QString::fromStdString(caption)),
44                                    Q_ARG(QString, QString::fromStdString(message)));
45     }
46     else
47     {
48         printf("%s: %s\n", caption.c_str(), message.c_str());
49         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
50     }
51     return 4;
52 }
53
54 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
55 {
56     if(!guiref)
57         return false;
58     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
59         return true;
60     bool payFee = false;
61
62     // Call slot on GUI thread.
63     // If called from another thread, use a blocking QueuedConnection.
64     Qt::ConnectionType connectionType = Qt::DirectConnection;
65     if(QThread::currentThread() != QCoreApplication::instance()->thread())
66     {
67         connectionType = Qt::BlockingQueuedConnection;
68     }
69
70     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
71                                Q_ARG(qint64, nFeeRequired),
72                                Q_ARG(bool*, &payFee));
73
74     return payFee;
75 }
76
77 void CalledSetStatusBar(const std::string& strText, int nField)
78 {
79     // Only used for built-in mining, which is disabled, simple ignore
80 }
81
82 void UIThreadCall(boost::function0<void> fn)
83 {
84     // Only used for built-in mining, which is disabled, simple ignore
85 }
86
87 void MainFrameRepaint()
88 {
89 }
90
91 /*
92    Translate string to current locale using Qt.
93  */
94 std::string _(const char* psz)
95 {
96     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
97 }
98
99 int main(int argc, char *argv[])
100 {
101     Q_INIT_RESOURCE(bitcoin);
102     QApplication app(argc, argv);
103
104     // Load language file for system locale
105     QString locale = QLocale::system().name();
106     QTranslator translator;
107     translator.load("bitcoin_"+locale);
108     app.installTranslator(&translator);
109
110     app.setQuitOnLastWindowClosed(false);
111
112     try
113     {
114         if(AppInit2(argc, argv))
115         {
116             {
117                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
118                 // calling shutdown.
119                 BitcoinGUI window;
120                 ClientModel clientModel(pwalletMain);
121                 WalletModel walletModel(pwalletMain);
122                 guiref = &window;
123                 window.setClientModel(&clientModel);
124                 window.setWalletModel(&walletModel);
125
126                 window.show();
127
128                 app.exec();
129
130                 guiref = 0;
131             }
132             Shutdown(NULL);
133         }
134         else
135         {
136             return 1;
137         }
138     } catch (std::exception& e) {
139         PrintException(&e, "Runaway exception");
140     } catch (...) {
141         PrintException(NULL, "Runaway exception");
142     }
143     return 0;
144 }