Hide UI immediately after leaving the main loop.
[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 #include "util.h"
12
13 #include <QApplication>
14 #include <QMessageBox>
15 #include <QThread>
16 #include <QTextCodec>
17 #include <QLocale>
18 #include <QTranslator>
19 #include <QSplashScreen>
20 #include <QLibraryInfo>
21
22 // Need a global reference for the notifications to find the GUI
23 BitcoinGUI *guiref;
24 QSplashScreen *splashref;
25
26 int MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
27 {
28     // Message from main thread
29     if(guiref)
30     {
31         guiref->error(QString::fromStdString(caption),
32                       QString::fromStdString(message));
33     }
34     else
35     {
36         QMessageBox::critical(0, QString::fromStdString(caption),
37             QString::fromStdString(message),
38             QMessageBox::Ok, QMessageBox::Ok);
39     }
40     return 4;
41 }
42
43 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
44 {
45     bool modal = style & wxMODAL;
46
47     if (modal)
48         while (!guiref)
49             Sleep(1000);
50
51     // Message from network thread
52     if(guiref)
53     {
54         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
55                                    Q_ARG(QString, QString::fromStdString(caption)),
56                                    Q_ARG(QString, QString::fromStdString(message)),
57                                    Q_ARG(bool, modal));
58     }
59     else
60     {
61         printf("%s: %s\n", caption.c_str(), message.c_str());
62         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
63     }
64     return 4;
65 }
66
67 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
68 {
69     if(!guiref)
70         return false;
71     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
72         return true;
73     bool payFee = false;
74
75     // Call slot on GUI thread.
76     // If called from another thread, use a blocking QueuedConnection.
77     Qt::ConnectionType connectionType = Qt::DirectConnection;
78     if(QThread::currentThread() != QCoreApplication::instance()->thread())
79     {
80         connectionType = Qt::BlockingQueuedConnection;
81     }
82
83     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
84                                Q_ARG(qint64, nFeeRequired),
85                                Q_ARG(bool*, &payFee));
86
87     return payFee;
88 }
89
90 void CalledSetStatusBar(const std::string& strText, int nField)
91 {
92     // Only used for built-in mining, which is disabled, simple ignore
93 }
94
95 void UIThreadCall(boost::function0<void> fn)
96 {
97     // Only used for built-in mining, which is disabled, simple ignore
98 }
99
100 void MainFrameRepaint()
101 {
102     if(guiref)
103         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
104 }
105
106 void InitMessage(const std::string &message)
107 {
108     if(splashref)
109     {
110         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
111         QApplication::instance()->processEvents();
112     }
113 }
114
115 /*
116    Translate string to current locale using Qt.
117  */
118 std::string _(const char* psz)
119 {
120     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
121 }
122
123 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
124  */
125 static void handleRunawayException(std::exception *e)
126 {
127     PrintExceptionContinue(e, "Runaway exception");
128     QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occured. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
129     exit(1);
130 }
131
132 int main(int argc, char *argv[])
133 {
134     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
135     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
136
137     Q_INIT_RESOURCE(bitcoin);
138     QApplication app(argc, argv);
139
140     // Load language files for system locale:
141     // - First load the translator for the base language, without territory
142     // - Then load the more specific locale translator
143     QString lang_territory = QLocale::system().name(); // "en_US"
144     QString lang = lang_territory;
145     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
146     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
147
148     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
149     if (!qtTranslatorBase.isEmpty())
150         app.installTranslator(&qtTranslatorBase);
151
152     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
153     if (!qtTranslator.isEmpty())
154         app.installTranslator(&qtTranslator);
155
156     translatorBase.load(":/translations/"+lang);
157     if (!translatorBase.isEmpty())
158         app.installTranslator(&translatorBase);
159
160     translator.load(":/translations/"+lang_territory);
161     if (!translator.isEmpty())
162         app.installTranslator(&translator);
163
164     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
165
166     QSplashScreen splash(QPixmap(":/images/splash"), 0);
167     if (!GetBoolArg("-min"))
168     {
169         splash.show();
170         splash.setAutoFillBackground(true);
171         splashref = &splash;
172     }
173
174     app.processEvents();
175
176     app.setQuitOnLastWindowClosed(false);
177
178     try
179     {
180         if(AppInit2(argc, argv))
181         {
182             {
183                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
184                 // calling Shutdown() in case of exceptions.
185                 BitcoinGUI window;
186                 if (splashref)
187                     splash.finish(&window);
188                 OptionsModel optionsModel(pwalletMain);
189                 ClientModel clientModel(&optionsModel);
190                 WalletModel walletModel(pwalletMain, &optionsModel);
191
192                 guiref = &window;
193                 window.setClientModel(&clientModel);
194                 window.setWalletModel(&walletModel);
195
196                 // If -min option passed, start window minimized.
197                 if(GetBoolArg("-min"))
198                 {
199                     window.showMinimized();
200                 }
201                 else
202                 {
203                     window.show();
204                 }
205
206                 app.exec();
207
208                 window.hide();
209                 guiref = 0;
210             }
211             Shutdown(NULL);
212         }
213         else
214         {
215             return 1;
216         }
217     } catch (std::exception& e) {
218         handleRunawayException(&e);
219     } catch (...) {
220         handleRunawayException(NULL);
221     }
222     return 0;
223 }