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