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