Pull request #21: windows fixes/cleanup by Matoking
[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("bitcoin_"+locale);
124     app.installTranslator(&translator);
125
126     QSplashScreen splash(QPixmap(":/images/splash"), 0);
127     splash.show();
128     splash.setAutoFillBackground(true);
129     splashref = &splash;
130
131     app.processEvents();
132
133     app.setQuitOnLastWindowClosed(false);
134
135     try
136     {
137         if(AppInit2(argc, argv))
138         {
139             {
140                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
141                 // calling Shutdown().
142                 BitcoinGUI window;
143                 splash.finish(&window);
144                 OptionsModel optionsModel(pwalletMain);
145                 ClientModel clientModel(&optionsModel);
146                 WalletModel walletModel(pwalletMain, &optionsModel);
147
148                 guiref = &window;
149                 window.setClientModel(&clientModel);
150                 window.setWalletModel(&walletModel);
151
152                 window.show();
153
154                 app.exec();
155
156                 guiref = 0;
157             }
158             Shutdown(NULL);
159         }
160         else
161         {
162             return 1;
163         }
164     } catch (std::exception& e) {
165         PrintException(&e, "Runaway exception");
166     } catch (...) {
167         PrintException(NULL, "Runaway exception");
168     }
169     return 0;
170 }