Somewhat confident now, tested on GNOME+KDE, with all types of transactions. Next...
[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 "util.h"
7 #include "init.h"
8 #include "main.h"
9 #include "externui.h"
10
11 #include <QApplication>
12 #include <QMessageBox>
13 #include <QThread>
14
15 // Need a global reference for the notifications to find the GUI
16 BitcoinGUI *guiref;
17
18 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
19 {
20     // Message from main thread
21     if(guiref)
22     {
23         guiref->error(QString::fromStdString(caption),
24                       QString::fromStdString(message));
25     }
26     else
27     {
28         QMessageBox::critical(0, QString::fromStdString(caption),
29             QString::fromStdString(message),
30             QMessageBox::Ok, QMessageBox::Ok);
31     }
32     return 4;
33 }
34
35 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
36 {
37     // Message from network thread
38     if(guiref)
39     {
40         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
41                                    Q_ARG(QString, QString::fromStdString(caption)),
42                                    Q_ARG(QString, QString::fromStdString(message)));
43     }
44     else
45     {
46         printf("%s: %s\n", caption.c_str(), message.c_str());
47         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
48     }
49     return 4;
50 }
51
52 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
53 {
54     if(!guiref)
55         return false;
56     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
57         return true;
58     bool payFee = false;
59
60     /* Call slot on GUI thread.
61        If called from another thread, use a blocking QueuedConnection.
62      */
63     Qt::ConnectionType connectionType = Qt::DirectConnection;
64     if(QThread::currentThread() != QCoreApplication::instance()->thread())
65     {
66         connectionType = Qt::BlockingQueuedConnection;
67     }
68
69     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
70                                Q_ARG(qint64, nFeeRequired),
71                                Q_ARG(bool*, &payFee));
72
73     return payFee;
74 }
75
76 void CalledSetStatusBar(const std::string& strText, int nField)
77 {
78     // Only used for built-in mining, which is disabled, simple ignore
79 }
80
81 void UIThreadCall(boost::function0<void> fn)
82 {
83     // Only used for built-in mining, which is disabled, simple ignore
84 }
85
86 void MainFrameRepaint()
87 {
88 }
89
90 int main(int argc, char *argv[])
91 {
92     QApplication app(argc, argv);
93     app.setQuitOnLastWindowClosed(false);
94
95     try
96     {
97         if(AppInit2(argc, argv))
98         {
99             BitcoinGUI window;
100             ClientModel model;
101             guiref = &window;
102             window.setModel(&model);
103
104             window.show();
105
106             int retval = app.exec();
107
108             guiref = 0;
109             Shutdown(NULL);
110
111             return retval;
112         }
113         else
114         {
115             return 1;
116         }
117     } catch (std::exception& e) {
118         PrintException(&e, "Runaway exception");
119     } catch (...) {
120         PrintException(NULL, "Runaway exception");
121     }
122 }