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