Add support for opening bitcoin: URIs directly.
[novacoin.git] / src / qt / bitcoin.cpp
1 /*
2  * W.J. van der Laan 2011
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 main thread
31     if(guiref)
32     {
33         guiref->error(QString::fromStdString(caption),
34                       QString::fromStdString(message));
35     }
36     else
37     {
38         QMessageBox::critical(0, QString::fromStdString(caption),
39             QString::fromStdString(message),
40             QMessageBox::Ok, QMessageBox::Ok);
41     }
42     return 4;
43 }
44
45 int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style, wxWindow* parent, int x, int y)
46 {
47     // Message from network thread
48     if(guiref)
49     {
50         QMetaObject::invokeMethod(guiref, "error", Qt::QueuedConnection,
51                                    Q_ARG(QString, QString::fromStdString(caption)),
52                                    Q_ARG(QString, QString::fromStdString(message)));
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, wxWindow* parent)
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     // Call slot on GUI thread.
71     // If called from another thread, use a blocking QueuedConnection.
72     Qt::ConnectionType connectionType = Qt::DirectConnection;
73     if(QThread::currentThread() != QCoreApplication::instance()->thread())
74     {
75         connectionType = Qt::BlockingQueuedConnection;
76     }
77
78     QMetaObject::invokeMethod(guiref, "askFee", connectionType,
79                                Q_ARG(qint64, nFeeRequired),
80                                Q_ARG(bool*, &payFee));
81
82     return payFee;
83 }
84
85 void ThreadSafeHandleURL(const std::string& strURL)
86 {
87     if(!guiref)
88         return;
89
90     // Call slot on GUI thread.
91     // If called from another thread, use a blocking QueuedConnection.
92     Qt::ConnectionType connectionType = Qt::DirectConnection;
93     if(QThread::currentThread() != QCoreApplication::instance()->thread())
94     {
95         connectionType = Qt::BlockingQueuedConnection;
96     }
97     QMetaObject::invokeMethod(guiref, "handleURL", connectionType,
98                                Q_ARG(QString, QString::fromStdString(strURL)));
99 }
100
101 void CalledSetStatusBar(const std::string& strText, int nField)
102 {
103     // Only used for built-in mining, which is disabled, simple ignore
104 }
105
106 void UIThreadCall(boost::function0<void> fn)
107 {
108     // Only used for built-in mining, which is disabled, simple ignore
109 }
110
111 void MainFrameRepaint()
112 {
113     if(guiref)
114         QMetaObject::invokeMethod(guiref, "refreshStatusBar", Qt::QueuedConnection);
115 }
116
117 void InitMessage(const std::string &message)
118 {
119     if(splashref)
120     {
121         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
122         QApplication::instance()->processEvents();
123     }
124 }
125
126 /*
127    Translate string to current locale using Qt.
128  */
129 std::string _(const char* psz)
130 {
131     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
132 }
133
134 int main(int argc, char *argv[])
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
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     ParseParameters(argc, argv);
163
164     // Load language files for system locale:
165     // - First load the translator for the base language, without territory
166     // - Then load the more specific locale translator
167     QString lang_territory = QLocale::system().name(); // "en_US"
168     QString lang = lang_territory;
169     lang.truncate(lang_territory.lastIndexOf('_')); // "en"
170     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
171
172     qtTranslatorBase.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang);
173     if (!qtTranslatorBase.isEmpty())
174         app.installTranslator(&qtTranslatorBase);
175
176     qtTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + lang_territory);
177     if (!qtTranslator.isEmpty())
178         app.installTranslator(&qtTranslator);
179
180     translatorBase.load(":/translations/"+lang);
181     if (!translatorBase.isEmpty())
182         app.installTranslator(&translatorBase);
183
184     translator.load(":/translations/"+lang_territory);
185     if (!translator.isEmpty())
186         app.installTranslator(&translator);
187
188     app.setApplicationName(QApplication::translate("main", "Bitcoin-Qt"));
189
190     QSplashScreen splash(QPixmap(":/images/splash"), 0);
191     splash.show();
192     splash.setAutoFillBackground(true);
193     splashref = &splash;
194
195     app.processEvents();
196
197     app.setQuitOnLastWindowClosed(false);
198
199     try
200     {
201         if(AppInit2(argc, argv))
202         {
203             {
204                 // Put this in a block, so that BitcoinGUI is cleaned up properly before
205                 // calling Shutdown() in case of exceptions.
206                 BitcoinGUI window;
207                 splash.finish(&window);
208                 OptionsModel optionsModel(pwalletMain);
209                 ClientModel clientModel(&optionsModel);
210                 WalletModel walletModel(pwalletMain, &optionsModel);
211
212                 guiref = &window;
213                 window.setClientModel(&clientModel);
214                 window.setWalletModel(&walletModel);
215
216                 // If -min option passed, start window minimized.
217                 if(GetBoolArg("-min"))
218                 {
219                     window.showMinimized();
220                 }
221                 else
222                 {
223                     window.show();
224                 }
225
226                 // Place this here as guiref has to be defined if we dont want to lose URLs
227                 ipcInit();
228                 // Check for URL in argv
229                 for (int i = 1; i < argc; i++)
230                 {
231                     if (strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0)
232                     {
233                         const char *strURL = argv[i];
234                         try {
235                             boost::interprocess::message_queue mq(boost::interprocess::open_only, "BitcoinURL");
236                             mq.try_send(strURL, strlen(strURL), 0);
237                         }
238                         catch (boost::interprocess::interprocess_exception &ex) {
239                         }
240                     }
241                 }
242
243                 app.exec();
244
245                 guiref = 0;
246             }
247             Shutdown(NULL);
248         }
249         else
250         {
251             return 1;
252         }
253     } catch (std::exception& e) {
254         PrintException(&e, "Runaway exception");
255     } catch (...) {
256         PrintException(NULL, "Runaway exception");
257     }
258     return 0;
259 }