support RPC stop and encryptwallet with UI
[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 MainFrameRepaint()
92 {
93     if(clientmodel)
94         QMetaObject::invokeMethod(clientmodel, "update", Qt::QueuedConnection);
95     if(walletmodel)
96         QMetaObject::invokeMethod(walletmodel, "update", Qt::QueuedConnection);
97 }
98
99 void AddressBookRepaint()
100 {
101     if(walletmodel)
102         QMetaObject::invokeMethod(walletmodel, "updateAddressList", 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 void QueueShutdown()
115 {
116     QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
117 }
118
119 /*
120    Translate string to current locale using Qt.
121  */
122 std::string _(const char* psz)
123 {
124     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
125 }
126
127 #ifdef WIN32
128 #define strncasecmp strnicmp
129 #endif
130 #ifndef BITCOIN_QT_TEST
131 int main(int argc, char *argv[])
132 {
133 #if !defined(MAC_OSX) && !defined(WIN32)
134 // TODO: implement qtipcserver.cpp for Mac and Windows
135
136     // Do this early as we don't want to bother initializing if we are just calling IPC
137     for (int i = 1; i < argc; i++)
138     {
139         if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
140         {
141             const char *strURL = argv[i];
142             try {
143                 boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
144                 if(mq.try_send(strURL, strlen(strURL), 0))
145                     exit(0);
146                 else
147                     break;
148             }
149             catch (boost::interprocess::interprocess_exception &ex) {
150                 break;
151             }
152         }
153     }
154 #endif
155
156     // Internal string conversion is all UTF-8
157     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
158     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
159
160     Q_INIT_RESOURCE(bitcoin);
161     QApplication app(argc, argv);
162
163     // Command-line options take precedence:
164     ParseParameters(argc, argv);
165
166     // ... then bitcoin.conf:
167     if (!ReadConfigFile(mapArgs, mapMultiArgs))
168     {
169         fprintf(stderr, "Error: Specified directory does not exist\n");
170         return 1;
171     }
172
173     // Application identification (must be set before OptionsModel is initialized,
174     // as it is used to locate QSettings)
175     app.setOrganizationName("Bitcoin");
176     app.setOrganizationDomain("bitcoin.org");
177     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
178         app.setApplicationName("Bitcoin-Qt-testnet");
179     else
180         app.setApplicationName("Bitcoin-Qt");
181
182     // ... then GUI settings:
183     OptionsModel optionsModel;
184
185     // Get desired locale ("en_US") from command line or system locale
186     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
187     // Load language files for configured locale:
188     // - First load the translator for the base language, without territory
189     // - Then load the more specific locale translator
190     QString lang = lang_territory;
191
192     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
193     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
194
195     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
196     if (!qtTranslatorBase.isEmpty())
197         app.installTranslator(&qtTranslatorBase);
198
199     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
200     if (!qtTranslator.isEmpty())
201         app.installTranslator(&qtTranslator);
202
203     translatorBase.load(":/translations/"+lang);
204     if (!translatorBase.isEmpty())
205         app.installTranslator(&translatorBase);
206
207     translator.load(":/translations/"+lang_territory);
208     if (!translator.isEmpty())
209         app.installTranslator(&translator);
210
211     QSplashScreen splash(QPixmap(":/images/splash"), 0);
212     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
213     {
214         splash.show();
215         splash.setAutoFillBackground(true);
216         splashref = &splash;
217     }
218
219     app.processEvents();
220
221     app.setQuitOnLastWindowClosed(false);
222
223     try
224     {
225         if(AppInit2(argc, argv))
226         {
227             {
228                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
229                 // calling Shutdown() in case of exceptions.
230
231                 optionsModel.Upgrade(); // Must be done after AppInit2
232
233                 BitcoinGUI window;
234                 if (splashref)
235                     splash.finish(&window);
236
237                 ClientModel clientModel(&optionsModel);
238                 clientmodel = &clientModel;
239                 WalletModel walletModel(pwalletMain, &optionsModel);
240                 walletmodel = &walletModel;
241
242                 guiref = &window;
243                 window.setClientModel(&clientModel);
244                 window.setWalletModel(&walletModel);
245
246                 // If -min option passed, start window minimized.
247                 if(GetBoolArg("-min"))
248                 {
249                     window.showMinimized();
250                 }
251                 else
252                 {
253                     window.show();
254                 }
255
256                 // Place this here as guiref has to be defined if we dont want to lose URLs
257                 ipcInit();
258
259 #if !defined(MAC_OSX) && !defined(WIN32)
260 // TODO: implement qtipcserver.cpp for Mac and Windows
261
262                 // Check for URL in argv
263                 for (int i = 1; i < argc; i++)
264                 {
265                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
266                     {
267                         const char *strURL = argv[i];
268                         try {
269                             boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
270                             mq.try_send(strURL, strlen(strURL), 0);
271                         }
272                         catch (boost::interprocess::interprocess_exception &ex) {
273                         }
274                     }
275                 }
276 #endif
277                 app.exec();
278
279                 guiref = 0;
280                 clientmodel = 0;
281                 walletmodel = 0;
282             }
283             Shutdown(NULL);
284         }
285         else
286         {
287             return 1;
288         }
289     } catch (std::exception& e) {
290         PrintException(&e, "Runaway exception");
291     } catch (...) {
292         PrintException(NULL, "Runaway exception");
293     }
294     return 0;
295 }
296 #endif // BITCOIN_QT_TEST