Fix status bar not displaying Alerts.
[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     if(guiref)
95         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
96 }
97
98 void InitMessage(const std::string &message)
99 {
100     if(splashref)
101     {
102         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
103         QApplication::instance()->processEvents();
104     }
105 }
106
107 /*
108    Translate string to current locale using Qt.
109  */
110 std::string _(const char* psz)
111 {
112     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
113 }
114
115 int main(int argc, char *argv[])
116 {
117     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
118     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
119
120     Q_INIT_RESOURCE(bitcoin);
121     QApplication app(argc, argv);
122
123     // Load language files for system locale:
124     // - First load the translator for the base language, without territory
125     // - Then load the more specific locale translator
126     QString lang_territory = QLocale::system().name(); // "en_US"
127     QString lang = lang_territory;
128     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
129     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
130
131     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
132     if (!qtTranslatorBase.isEmpty())
133         app.installTranslator(&qtTranslatorBase);
134
135     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
136     if (!qtTranslator.isEmpty())
137         app.installTranslator(&qtTranslator);
138
139     translatorBase.load(":/translations/"+lang);
140     if (!translatorBase.isEmpty())
141         app.installTranslator(&translatorBase);
142
143     translator.load(":/translations/"+lang_territory);
144     if (!translator.isEmpty())
145         app.installTranslator(&translator);
146
147     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
148
149     QSplashScreen splash(QPixmap(":/images/splash"), 0);
150     splash.show();
151     splash.setAutoFillBackground(true);
152     splashref = &splash;
153
154     app.processEvents();
155
156     app.setQuitOnLastWindowClosed(false);
157
158     try
159     {
160         if(AppInit2(argc, argv))
161         {
162             {
163                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
164                 // calling Shutdown() in case of exceptions.
165                 BitcoinGUI window;
166                 splash.finish(&window);
167                 OptionsModel optionsModel(pwalletMain);
168                 ClientModel clientModel(&optionsModel);
169                 WalletModel walletModel(pwalletMain, &optionsModel);
170
171                 guiref = &window;
172                 window.setClientModel(&clientModel);
173                 window.setWalletModel(&walletModel);
174
175                 // If -min option passed, start window minimized.
176                 if(GetBoolArg("-min"))
177                 {
178                     window.showMinimized();
179                 }
180                 else
181                 {
182                     window.show();
183                 }
184
185                 app.exec();
186
187                 guiref = 0;
188             }
189             Shutdown(NULL);
190         }
191         else
192         {
193             return 1;
194         }
195     } catch (std::exception& e) {
196         PrintException(&e, "Runaway exception");
197     } catch (...) {
198         PrintException(NULL, "Runaway exception");
199     }
200     return 0;
201 }