Remove headers.h
[novacoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 2011-2012
3  */
4 #include "bitcoingui.h"
5 #include "clientmodel.h"
6 #include "walletmodel.h"
7 #include "optionsmodel.h"
8 #include "guiutil.h"
9
10 #include "init.h"
11 #include "qtipcserver.h"
12
13 #include <QApplication>
14 #include <QMessageBox>
15 #include <QTextCodec>
16 #include <QLocale>
17 #include <QTranslator>
18 #include <QSplashScreen>
19 #include <QLibraryInfo>
20
21 #include <boost/interprocess/ipc/message_queue.hpp>
22
23 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
24 #define _BITCOIN_QT_PLUGINS_INCLUDED
25 #define __INSURE__
26 #include <QtPlugin>
27 Q_IMPORT_PLUGIN(qcncodecs)
28 Q_IMPORT_PLUGIN(qjpcodecs)
29 Q_IMPORT_PLUGIN(qtwcodecs)
30 Q_IMPORT_PLUGIN(qkrcodecs)
31 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
32 #endif
33
34 // Need a global reference for the notifications to find the GUI
35 static BitcoinGUI *guiref;
36 static QSplashScreen *splashref;
37 static WalletModel *walletmodel;
38 static ClientModel *clientmodel;
39
40 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
41 {
42     // Message from network thread
43     if(guiref)
44     {
45         bool modal = (style & wxMODAL);
46         // in case of modal message, use blocking connection to wait for user to click OK
47         QMetaObject::invokeMethod(guiref, "error",
48                                    modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
49                                    Q_ARG(QString, QString::fromStdString(caption)),
50                                    Q_ARG(QString, QString::fromStdString(message)),
51                                    Q_ARG(bool, modal));
52     }
53     else
54     {
55         printf("%s: %s\n", caption.c_str(), message.c_str());
56         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
57     }
58     return 4;
59 }
60
61 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption)
62 {
63     if(!guiref)
64         return false;
65     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
66         return true;
67     bool payFee = false;
68
69     QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
70                                Q_ARG(qint64, nFeeRequired),
71                                Q_ARG(bool*, &payFee));
72
73     return payFee;
74 }
75
76 void ThreadSafeHandleURI(const std::string& strURI)
77 {
78     if(!guiref)
79         return;
80
81     QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
82                                Q_ARG(QString, QString::fromStdString(strURI)));
83 }
84
85 void MainFrameRepaint()
86 {
87     if(clientmodel)
88         QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
89     if(walletmodel)
90         QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
91 }
92
93 void AddressBookRepaint()
94 {
95     if(walletmodel)
96         QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
97 }
98
99 void InitMessage(const std::string &message)
100 {
101     if(splashref)
102     {
103         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
104         QApplication::instance()->processEvents();
105     }
106 }
107
108 void QueueShutdown()
109 {
110     QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
111 }
112
113 /*
114    Translate string to current locale using Qt.
115  */
116 std::string _(const char* psz)
117 {
118     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
119 }
120
121 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
122  */
123 static void handleRunawayException(std::exception *e)
124 {
125     PrintExceptionContinue(e, "Runaway exception");
126     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));
127     exit(1);
128 }
129
130 #ifdef WIN32
131 #define strncasecmp strnicmp
132 #endif
133 #ifndef BITCOIN_QT_TEST
134 int main(int argc, char *argv[])
135 {
136 #if !defined(MAC_OSX) && !defined(WIN32)
137 // TODO: implement qtipcserver.cpp for Mac and Windows
138
139     // Do this early as we don't want to bother initializing if we are just calling IPC
140     for (int i = 1; i < argc; i++)
141     {
142         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
143         {
144             const char *strURI = argv[i];
145             try {
146                 boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
147                 if(mq.try_send(strURI, strlen(strURI), 0))
148                     exit(0);
149                 else
150                     break;
151             }
152             catch (boost::interprocess::interprocess_exception &ex) {
153                 break;
154             }
155         }
156     }
157 #endif
158
159     // Internal string conversion is all UTF-8
160     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
161     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
162
163     Q_INIT_RESOURCE(bitcoin);
164     QApplication app(argc, argv);
165
166     // Command-line options take precedence:
167     ParseParameters(argc, argv);
168
169     // ... then bitcoin.conf:
170     if (!ReadConfigFile(mapArgs, mapMultiArgs))
171     {
172         fprintf(stderr, "Error: Specified directory does not exist\n");
173         return 1;
174     }
175
176     // Application identification (must be set before OptionsModel is initialized,
177     // as it is used to locate QSettings)
178     app.setOrganizationName("Bitcoin");
179     app.setOrganizationDomain("bitcoin.org");
180     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
181         app.setApplicationName("Bitcoin-Qt-testnet");
182     else
183         app.setApplicationName("Bitcoin-Qt");
184
185     // ... then GUI settings:
186     OptionsModel optionsModel;
187
188     // Get desired locale ("en_US") from command line or system locale
189     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
190     // Load language files for configured locale:
191     // - First load the translator for the base language, without territory
192     // - Then load the more specific locale translator
193     QString lang = lang_territory;
194
195     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
196     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
197
198     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
199     if (!qtTranslatorBase.isEmpty())
200         app.installTranslator(&qtTranslatorBase);
201
202     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
203     if (!qtTranslator.isEmpty())
204         app.installTranslator(&qtTranslator);
205
206     translatorBase.load(":/translations/"+lang);
207     if (!translatorBase.isEmpty())
208         app.installTranslator(&translatorBase);
209
210     translator.load(":/translations/"+lang_territory);
211     if (!translator.isEmpty())
212         app.installTranslator(&translator);
213
214     QSplashScreen splash(QPixmap(":/images/splash"), 0);
215     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
216     {
217         splash.show();
218         splash.setAutoFillBackground(true);
219         splashref = &splash;
220     }
221
222     app.processEvents();
223
224     app.setQuitOnLastWindowClosed(false);
225
226     try
227     {
228         BitcoinGUI window;
229         guiref = &window;
230         if(AppInit2(argc, argv))
231         {
232             {
233                 // Put this in a block, so that the Model objects are cleaned up before
234                 // calling Shutdown().
235
236                 optionsModel.Upgrade(); // Must be done after AppInit2
237
238                 if (splashref)
239                     splash.finish(&window);
240
241                 ClientModel clientModel(&optionsModel);
242                 clientmodel = &clientModel;
243                 WalletModel walletModel(pwalletMain, &optionsModel);
244                 walletmodel = &walletModel;
245
246                 window.setClientModel(&clientModel);
247                 window.setWalletModel(&walletModel);
248
249                 // If -min option passed, start window minimized.
250                 if(GetBoolArg("-min"))
251                 {
252                     window.showMinimized();
253                 }
254                 else
255                 {
256                     window.show();
257                 }
258
259                 // Place this here as guiref has to be defined if we dont want to lose URIs
260                 ipcInit();
261
262 #if !defined(MAC_OSX) && !defined(WIN32)
263 // TODO: implement qtipcserver.cpp for Mac and Windows
264
265                 // Check for URI in argv
266                 for (int i = 1; i < argc; i++)
267                 {
268                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
269                     {
270                         const char *strURI = argv[i];
271                         try {
272                             boost::interprocess::message_queue mq(boost::interprocess::open_only, BITCOINURI_QUEUE_NAME);
273                             mq.try_send(strURI, strlen(strURI), 0);
274                         }
275                         catch (boost::interprocess::interprocess_exception &ex) {
276                         }
277                     }
278                 }
279 #endif
280                 app.exec();
281
282                 window.setClientModel(0);
283                 window.setWalletModel(0);
284                 guiref = 0;
285                 clientmodel = 0;
286                 walletmodel = 0;
287             }
288             Shutdown(NULL);
289         }
290         else
291         {
292             return 1;
293         }
294     } catch (std::exception& e) {
295         handleRunawayException(&e);
296     } catch (...) {
297         handleRunawayException(NULL);
298     }
299     return 0;
300 }
301 #endif // BITCOIN_QT_TEST