cba4e851b4bfd7563e9361d365659636573ad9e9
[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     ParseParameters(argc, argv);
124
125     // Load language files for system locale:
126     // - First load the translator for the base language, without territory
127     // - Then load the more specific locale translator
128     QString lang_territory = QLocale::system().name(); // "en_US"
129     QString lang = lang_territory;
130     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
131     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
132
133     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
134     if (!qtTranslatorBase.isEmpty())
135         app.installTranslator(&qtTranslatorBase);
136
137     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
138     if (!qtTranslator.isEmpty())
139         app.installTranslator(&qtTranslator);
140
141     translatorBase.load(":/translations/"+lang);
142     if (!translatorBase.isEmpty())
143         app.installTranslator(&translatorBase);
144
145     translator.load(":/translations/"+lang_territory);
146     if (!translator.isEmpty())
147         app.installTranslator(&translator);
148
149     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
150
151     QSplashScreen splash(QPixmap(":/images/splash"), 0);
152     splash.show();
153     splash.setAutoFillBackground(true);
154     splashref = &splash;
155
156     app.processEvents();
157
158     app.setQuitOnLastWindowClosed(false);
159
160     try
161     {
162         if(AppInit2(argc, argv))
163         {
164             {
165                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
166                 // calling Shutdown() in case of exceptions.
167                 BitcoinGUI window;
168                 splash.finish(&window);
169                 OptionsModel optionsModel(pwalletMain);
170                 ClientModel clientModel(&optionsModel);
171                 WalletModel walletModel(pwalletMain, &optionsModel);
172
173                 guiref = &window;
174                 window.setClientModel(&clientModel);
175                 window.setWalletModel(&walletModel);
176
177                 // If -min option passed, start window minimized.
178                 if(GetBoolArg("-min"))
179                 {
180                     window.showMinimized();
181                 }
182                 else
183                 {
184                     window.show();
185                 }
186
187                 app.exec();
188
189                 guiref = 0;
190             }
191             Shutdown(NULL);
192         }
193         else
194         {
195             return 1;
196         }
197     } catch (std::exception& e) {
198         PrintException(&e, "Runaway exception");
199     } catch (...) {
200         PrintException(NULL, "Runaway exception");
201     }
202     return 0;
203 }