91881c349a596704b9e2110278fb93005eb68556
[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 <QApplication>
13 #include <QMessageBox>
14 #include <QThread>
15 #include <QTextCodec>
16 #include <QLocale>
17 #include <QTranslator>
18 #include <QSplashScreen>
19
20 // Need a global reference for the notifications to find the GUI
21 BitcoinGUI *guiref;
22 QSplashScreen *splashref;
23
24 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
25 {
26     // Message from main thread
27     if(guiref)
28     {
29         guiref->error(QString::fromStdString(caption),
30                       QString::fromStdString(message));
31     }
32     else
33     {
34         QMessageBox::critical(0, QString::fromStdString(caption),
35             QString::fromStdString(message),
36             QMessageBox::Ok, QMessageBox::Ok);
37     }
38     return 4;
39 }
40
41 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
42 {
43     // Message from network thread
44     if(guiref)
45     {
46         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
47                                    Q_ARG(QString, QString::fromStdString(caption)),
48                                    Q_ARG(QString, QString::fromStdString(message)));
49     }
50     else
51     {
52         printf("%s: %s\n", caption.c_str(), message.c_str());
53         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
54     }
55     return 4;
56 }
57
58 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
59 {
60     if(!guiref)
61         return false;
62     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
63         return true;
64     bool payFee = false;
65
66     // Call slot on GUI thread.
67     // If called from another thread, use a blocking QueuedConnection.
68     Qt::ConnectionType connectionType = Qt::DirectConnection;
69     if(QThread::currentThread() != QCoreApplication::instance()->thread())
70     {
71         connectionType = Qt::BlockingQueuedConnection;
72     }
73
74     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
75                                Q_ARG(qint64, nFeeRequired),
76                                Q_ARG(bool*, &payFee));
77
78     return payFee;
79 }
80
81 void CalledSetStatusBar(const std::string& strText, int nField)
82 {
83     // Only used for built-in mining, which is disabled, simple ignore
84 }
85
86 void UIThreadCall(boost::function0<void> fn)
87 {
88     // Only used for built-in mining, which is disabled, simple ignore
89 }
90
91 void MainFrameRepaint()
92 {
93 }
94
95 void InitMessage(const std::string &message)
96 {
97     if(splashref)
98     {
99         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
100         QApplication::instance()->processEvents();
101     }
102 }
103
104 /*
105    Translate string to current locale using Qt.
106  */
107 std::string _(const char* psz)
108 {
109     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
110 }
111
112 int main(int argc, char *argv[])
113 {
114     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
115     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
116
117     Q_INIT_RESOURCE(bitcoin);
118     QApplication app(argc, argv);
119
120     // Load language file for system locale
121     QString locale = QLocale::system().name();
122     QTranslator translator;
123     translator.load(":/translations/"+locale);
124     if (!translator.isEmpty())
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                 window.show();
154
155                 app.exec();
156
157                 guiref = 0;
158             }
159             Shutdown(NULL);
160         }
161         else
162         {
163             return 1;
164         }
165     } catch (std::exception& e) {
166         PrintException(&e, "Runaway exception");
167     } catch (...) {
168         PrintException(NULL, "Runaway exception");
169     }
170     return 0;
171 }