Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[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 #include <QLibraryInfo>
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 qtTranslator;
124     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + locale);
125     if (!qtTranslator.isEmpty())
126         app.installTranslator(&qtTranslator);
127     QTranslator translator;
128     translator.load(":/translations/"+locale);
129     if (!translator.isEmpty())
130         app.installTranslator(&translator);
131
132     app.setApplicationName(QApplication::translate("main", "Bitcoin Qt"));
133
134     QSplashScreen splash(QPixmap(":/images/splash"), 0);
135     splash.show();
136     splash.setAutoFillBackground(true);
137     splashref = &splash;
138
139     app.processEvents();
140
141     app.setQuitOnLastWindowClosed(false);
142
143     try
144     {
145         if(AppInit2(argc, argv))
146         {
147             {
148                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
149                 // calling Shutdown().
150                 BitcoinGUI window;
151                 splash.finish(&window);
152                 OptionsModel optionsModel(pwalletMain);
153                 ClientModel clientModel(&optionsModel);
154                 WalletModel walletModel(pwalletMain, &optionsModel);
155
156                 guiref = &window;
157                 window.setClientModel(&clientModel);
158                 window.setWalletModel(&walletModel);
159
160                 window.show();
161
162                 app.exec();
163
164                 guiref = 0;
165             }
166             Shutdown(NULL);
167         }
168         else
169         {
170             return 1;
171         }
172     } catch (std::exception& e) {
173         PrintException(&e, "Runaway exception");
174     } catch (...) {
175         PrintException(NULL, "Runaway exception");
176     }
177     return 0;
178 }