Update UI through async calls MainFrameRepaint and AddressBookRepaint instead of...
[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 MyMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
42 {
43     // Message from AppInit2(), always in main thread before main window is constructed
44     QMessageBox::critical(0, QString::fromStdString(caption),
45         QString::fromStdString(message),
46         QMessageBox::Ok, QMessageBox::Ok);
47     return 4;
48 }
49
50 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
51 {
52     // Message from network thread
53     if(guiref)
54     {
55         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
56                                    Q_ARG(QString, QString::fromStdString(caption)),
57                                    Q_ARG(QString, QString::fromStdString(message)));
58     }
59     else
60     {
61         printf("%s: %s\n", caption.c_str(), message.c_str());
62         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
63     }
64     return 4;
65 }
66
67 bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent)
68 {
69     if(!guiref)
70         return false;
71     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
72         return true;
73     bool payFee = false;
74
75     QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
76                                Q_ARG(qint64, nFeeRequired),
77                                Q_ARG(bool*, &payFee));
78
79     return payFee;
80 }
81
82 void ThreadSafeHandleURL(const std::string& strURL)
83 {
84     if(!guiref)
85         return;
86
87     QMetaObject::invokeMethod(guiref, "handleURL", GUIUtil::blockingGUIThreadConnection(),
88                                Q_ARG(QString, QString::fromStdString(strURL)));
89 }
90
91 void CalledSetStatusBar(const std::string& strText, int nField)
92 {
93     // Only used for built-in mining, which is disabled, simple ignore
94 }
95
96 void UIThreadCall(boost::function0<void> fn)
97 {
98     // Only used for built-in mining, which is disabled, simple ignore
99 }
100
101 void MainFrameRepaint()
102 {
103     if(clientmodel)
104         QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
105     if(walletmodel)
106         QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
107 }
108
109 void AddressBookRepaint()
110 {
111     if(walletmodel)
112         QMetaObject::invokeMethod(walletmodel, "updateAddressList", Qt::QueuedConnection);
113 }
114
115 void InitMessage(const std::string &message)
116 {
117     if(splashref)
118     {
119         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
120         QApplication::instance()->processEvents();
121     }
122 }
123
124 /*
125    Translate string to current locale using Qt.
126  */
127 std::string _(const char* psz)
128 {
129     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
130 }
131
132 #ifdef WIN32
133 #define strncasecmp strnicmp
134 #endif
135 #ifndef BITCOIN_QT_TEST
136 int main(int argc, char *argv[])
137 {
138 #if !defined(MAC_OSX) && !defined(WIN32)
139 // TODO: implement qtipcserver.cpp for Mac and Windows
140
141     // Do this early as we don't want to bother initializing if we are just calling IPC
142     for (int i = 1; i < argc; i++)
143     {
144         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
145         {
146             const char *strURL = argv[i];
147             try {
148                 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
149                 if(mq.try_send(strURL, strlen(strURL), 0))
150                     exit(0);
151                 else
152                     break;
153             }
154             catch (boost::interprocess::interprocess_exception &ex) {
155                 break;
156             }
157         }
158     }
159 #endif
160
161     // Internal string conversion is all UTF-8
162     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
163     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
164
165     Q_INIT_RESOURCE(bitcoin);
166     QApplication app(argc, argv);
167
168     // Command-line options take precedence:
169     ParseParameters(argc, argv);
170
171     // ... then bitcoin.conf:
172     if (!ReadConfigFile(mapArgs, mapMultiArgs))
173     {
174         fprintf(stderr, "Error: Specified directory does not exist\n");
175         return 1;
176     }
177
178     // Application identification (must be set before OptionsModel is initialized,
179     // as it is used to locate QSettings)
180     app.setOrganizationName("Bitcoin");
181     app.setOrganizationDomain("bitcoin.org");
182     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
183         app.setApplicationName("Bitcoin-Qt-testnet");
184     else
185         app.setApplicationName("Bitcoin-Qt");
186
187     // ... then GUI settings:
188     OptionsModel optionsModel;
189
190     // Get desired locale ("en_US") from command line or system locale
191     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
192     // Load language files for configured locale:
193     // - First load the translator for the base language, without territory
194     // - Then load the more specific locale translator
195     QString lang = lang_territory;
196
197     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
198     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
199
200     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
201     if (!qtTranslatorBase.isEmpty())
202         app.installTranslator(&qtTranslatorBase);
203
204     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
205     if (!qtTranslator.isEmpty())
206         app.installTranslator(&qtTranslator);
207
208     translatorBase.load(":/translations/"+lang);
209     if (!translatorBase.isEmpty())
210         app.installTranslator(&translatorBase);
211
212     translator.load(":/translations/"+lang_territory);
213     if (!translator.isEmpty())
214         app.installTranslator(&translator);
215
216     QSplashScreen splash(QPixmap(":/images/splash"), 0);
217     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
218     {
219         splash.show();
220         splash.setAutoFillBackground(true);
221         splashref = &splash;
222     }
223
224     app.processEvents();
225
226     app.setQuitOnLastWindowClosed(false);
227
228     try
229     {
230         if(AppInit2(argc, argv))
231         {
232             {
233                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
234                 // calling Shutdown() in case of exceptions.
235
236                 optionsModel.Upgrade(); // Must be done after AppInit2
237
238                 BitcoinGUI window;
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                 guiref = &window;
248                 window.setClientModel(&clientModel);
249                 window.setWalletModel(&walletModel);
250
251                 // If -min option passed, start window minimized.
252                 if(GetBoolArg("-min"))
253                 {
254                     window.showMinimized();
255                 }
256                 else
257                 {
258                     window.show();
259                 }
260
261                 // Place this here as guiref has to be defined if we dont want to lose URLs
262                 ipcInit();
263
264 #if !defined(MAC_OSX) && !defined(WIN32)
265 // TODO: implement qtipcserver.cpp for Mac and Windows
266
267                 // Check for URL in argv
268                 for (int i = 1; i < argc; i++)
269                 {
270                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
271                     {
272                         const char *strURL = argv[i];
273                         try {
274                             boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
275                             mq.try_send(strURL, strlen(strURL), 0);
276                         }
277                         catch (boost::interprocess::interprocess_exception &ex) {
278                         }
279                     }
280                 }
281 #endif
282                 app.exec();
283
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         PrintException(&e, "Runaway exception");
296     } catch (...) {
297         PrintException(NULL, "Runaway exception");
298     }
299     return 0;
300 }
301 #endif // BITCOIN_QT_TEST