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