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