Use a messagebox to display the error when -server is provided without providing...
[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     bool modal = style & wxMODAL;
45
46     if (modal)
47         while (!guiref)
48             sleep(1);
49
50     // Message from network thread
51     if(guiref)
52     {
53         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
54                                    Q_ARG(QString, QString::fromStdString(caption)),
55                                    Q_ARG(QString, QString::fromStdString(message)),
56                                    Q_ARG(bool, modal));
57     }
58     else
59     {
60         printf("%s: %s\n", caption.c_str(), message.c_str());
61         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
62     }
63     return 4;
64 }
65
66 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
67 {
68     if(!guiref)
69         return false;
70     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
71         return true;
72     bool payFee = false;
73
74     // Call slot on GUI thread.
75     // If called from another thread, use a blocking QueuedConnection.
76     Qt::ConnectionType connectionType = Qt::DirectConnection;
77     if(QThread::currentThread() != QCoreApplication::instance()->thread())
78     {
79         connectionType = Qt::BlockingQueuedConnection;
80     }
81
82     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
83                                Q_ARG(qint64, nFeeRequired),
84                                Q_ARG(bool*, &payFee));
85
86     return payFee;
87 }
88
89 void CalledSetStatusBar(const std::string& strText, int nField)
90 {
91     // Only used for built-in mining, which is disabled, simple ignore
92 }
93
94 void UIThreadCall(boost::function0<void> fn)
95 {
96     // Only used for built-in mining, which is disabled, simple ignore
97 }
98
99 void MainFrameRepaint()
100 {
101     if(guiref)
102         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
103 }
104
105 void InitMessage(const std::string &message)
106 {
107     if(splashref)
108     {
109         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
110         QApplication::instance()->processEvents();
111     }
112 }
113
114 /*
115    Translate string to current locale using Qt.
116  */
117 std::string _(const char* psz)
118 {
119     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
120 }
121
122 int main(int argc, char *argv[])
123 {
124     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
125     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
126
127     Q_INIT_RESOURCE(bitcoin);
128     QApplication app(argc, argv);
129
130     // Load language files for system locale:
131     // - First load the translator for the base language, without territory
132     // - Then load the more specific locale translator
133     QString lang_territory = QLocale::system().name(); // "en_US"
134     QString lang = lang_territory;
135     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
136     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
137
138     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
139     if (!qtTranslatorBase.isEmpty())
140         app.installTranslator(&qtTranslatorBase);
141
142     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
143     if (!qtTranslator.isEmpty())
144         app.installTranslator(&qtTranslator);
145
146     translatorBase.load(":/translations/"+lang);
147     if (!translatorBase.isEmpty())
148         app.installTranslator(&translatorBase);
149
150     translator.load(":/translations/"+lang_territory);
151     if (!translator.isEmpty())
152         app.installTranslator(&translator);
153
154     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
155
156     QSplashScreen splash(QPixmap(":/images/splash"), 0);
157     if (!GetBoolArg("-min"))
158     {
159         splash.show();
160         splash.setAutoFillBackground(true);
161         splashref = &splash;
162     }
163
164     app.processEvents();
165
166     app.setQuitOnLastWindowClosed(false);
167
168     try
169     {
170         if(AppInit2(argc, argv))
171         {
172             {
173                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
174                 // calling Shutdown().
175                 BitcoinGUI window;
176                 if (splashref)
177                     splash.finish(&window);
178                 OptionsModel optionsModel(pwalletMain);
179                 ClientModel clientModel(&optionsModel);
180                 WalletModel walletModel(pwalletMain, &optionsModel);
181
182                 guiref = &window;
183                 window.setClientModel(&clientModel);
184                 window.setWalletModel(&walletModel);
185
186                 window.show();
187
188                 app.exec();
189
190                 guiref = 0;
191             }
192             Shutdown(NULL);
193         }
194         else
195         {
196             return 1;
197         }
198     } catch (std::exception& e) {
199         PrintException(&e, "Runaway exception");
200     } catch (...) {
201         PrintException(NULL, "Runaway exception");
202     }
203     return 0;
204 }