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