Revert "выбор папки для базы блоков из GUI"
[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 #include "guiconstants.h"
10
11 #include "init.h"
12 #include "ui_interface.h"
13 #include "qtipcserver.h"
14
15 #include <QApplication>
16 #include <QMessageBox>
17 #if QT_VERSION < 0x050000
18 #include <QTextCodec>
19 #endif
20 #include <QLocale>
21 #include <QTranslator>
22 #include <QSplashScreen>
23 #include <QLibraryInfo>
24
25 #if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
26 #define _BITCOIN_QT_PLUGINS_INCLUDED
27 #define __INSURE__
28 #include <QtPlugin>
29 Q_IMPORT_PLUGIN(qcncodecs)
30 Q_IMPORT_PLUGIN(qjpcodecs)
31 Q_IMPORT_PLUGIN(qtwcodecs)
32 Q_IMPORT_PLUGIN(qkrcodecs)
33 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
34 #endif
35
36 // Need a global reference for the notifications to find the GUI
37 static BitcoinGUI *guiref;
38 static QSplashScreen *splashref;
39
40 static void ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style)
41 {
42     // Message from network thread
43     if(guiref)
44     {
45         bool modal = (style & CClientUIInterface::MODAL);
46         // in case of modal message, use blocking connection to wait for user to click OK
47         QMetaObject::invokeMethod(guiref, "error",
48                                    modal ? GUIUtil::blockingGUIThreadConnection() : Qt::QueuedConnection,
49                                    Q_ARG(QString, QString::fromStdString(caption)),
50                                    Q_ARG(QString, QString::fromStdString(message)),
51                                    Q_ARG(bool, modal));
52     }
53     else
54     {
55         printf("%s: %s\n", caption.c_str(), message.c_str());
56         fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str());
57     }
58 }
59
60 static bool ThreadSafeAskFee(int64_t nFeeRequired, const std::string& strCaption)
61 {
62     if(!guiref)
63         return false;
64     if(nFeeRequired < MIN_TX_FEE || nFeeRequired <= nTransactionFee || fDaemon)
65         return true;
66     bool payFee = false;
67
68     QMetaObject::invokeMethod(guiref, "askFee", GUIUtil::blockingGUIThreadConnection(),
69                                Q_ARG(qint64, nFeeRequired),
70                                Q_ARG(bool*, &payFee));
71
72     return payFee;
73 }
74
75 static void ThreadSafeHandleURI(const std::string& strURI)
76 {
77     if(!guiref)
78         return;
79
80     QMetaObject::invokeMethod(guiref, "handleURI", GUIUtil::blockingGUIThreadConnection(),
81                                Q_ARG(QString, QString::fromStdString(strURI)));
82 }
83
84 static void InitMessage(const std::string &message)
85 {
86     if(splashref)
87     {
88         splashref->showMessage(QString::fromStdString(message), Qt::AlignBottom|Qt::AlignHCenter, QColor(255,255,200));
89         QApplication::instance()->processEvents();
90     }
91 }
92
93 static void QueueShutdown()
94 {
95     QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
96 }
97
98 /*
99    Translate string to current locale using Qt.
100  */
101 static std::string Translate(const char* psz)
102 {
103     return QCoreApplication::translate("bitcoin-core", psz).toStdString();
104 }
105
106 /* Handle runaway exceptions. Shows a message box with the problem and quits the program.
107  */
108 static void handleRunawayException(std::exception *e)
109 {
110     PrintExceptionContinue(e, "Runaway exception");
111     QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. NovaCoin can no longer continue safely and will quit.") + QString("\n\n") + QString::fromStdString(strMiscWarning));
112     exit(1);
113 }
114
115 #ifndef BITCOIN_QT_TEST
116 int main(int argc, char *argv[])
117 {
118     // Do this early as we don't want to bother initializing if we are just calling IPC
119     ipcScanRelay(argc, argv);
120 #if QT_VERSION < 0x050000
121     // Internal string conversion is all UTF-8
122     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
123     QTextCodec::setCodecForCStrings(QTextCodec::codecForTr());
124 #endif
125     Q_INIT_RESOURCE(bitcoin);
126     QApplication app(argc, argv);
127
128     // Install global event filter that makes sure that long tooltips can be word-wrapped
129     app.installEventFilter(new GUIUtil::ToolTipToRichTextFilter(TOOLTIP_WRAP_THRESHOLD, &app));
130
131     // Command-line options take precedence:
132     ParseParameters(argc, argv);
133
134     // ... then bitcoin.conf:
135     if (!boost::filesystem::is_directory(GetDataDir(false)))
136     {
137         // This message can not be translated, as translation is not initialized yet
138         // (which not yet possible because lang=XX can be overridden in bitcoin.conf in the data directory)
139         QMessageBox::critical(0, "NovaCoin",
140                               QString("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(mapArgs["-datadir"])));
141         return 1;
142     }
143     ReadConfigFile(mapArgs, mapMultiArgs);
144
145     // Application identification (must be set before OptionsModel is initialized,
146     // as it is used to locate QSettings)
147     app.setOrganizationName("NovaCoin");
148     app.setOrganizationDomain("novacoin.su");
149     if(GetBoolArg("-testnet")) // Separate UI settings for testnet
150         app.setApplicationName("NovaCoin-Qt-testnet");
151     else
152         app.setApplicationName("NovaCoin-Qt");
153
154     // ... then GUI settings:
155     OptionsModel optionsModel;
156
157     // Get desired locale (e.g. "de_DE") from command line or use system locale
158     QString lang_territory = QString::fromStdString(GetArg("-lang", QLocale::system().name().toStdString()));
159     QString lang = lang_territory;
160     // Convert to "de" only by truncating "_DE"
161     lang.truncate(lang_territory.lastIndexOf('_'));
162
163     QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;
164     // Load language files for configured locale:
165     // - First load the translator for the base language, without territory
166     // - Then load the more specific locale translator
167
168     // Load e.g. qt_de.qm
169     if (qtTranslatorBase.load("qt_" + lang, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
170         app.installTranslator(&qtTranslatorBase);
171
172     // Load e.g. qt_de_DE.qm
173     if (qtTranslator.load("qt_" + lang_territory, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
174         app.installTranslator(&qtTranslator);
175
176     // Load e.g. bitcoin_de.qm (shortcut "de" needs to be defined in bitcoin.qrc)
177     if (translatorBase.load(lang, ":/translations/"))
178         app.installTranslator(&translatorBase);
179
180     // Load e.g. bitcoin_de_DE.qm (shortcut "de_DE" needs to be defined in bitcoin.qrc)
181     if (translator.load(lang_territory, ":/translations/"))
182         app.installTranslator(&translator);
183
184     // Subscribe to global signals from core
185     uiInterface.ThreadSafeMessageBox.connect(ThreadSafeMessageBox);
186     uiInterface.ThreadSafeAskFee.connect(ThreadSafeAskFee);
187     uiInterface.ThreadSafeHandleURI.connect(ThreadSafeHandleURI);
188     uiInterface.InitMessage.connect(InitMessage);
189     uiInterface.QueueShutdown.connect(QueueShutdown);
190     uiInterface.Translate.connect(Translate);
191
192     // Show help message immediately after parsing command-line options (for "-lang") and setting locale,
193     // but before showing splash screen.
194     if (mapArgs.count("-?") || mapArgs.count("--help"))
195     {
196         GUIUtil::HelpMessageBox help;
197         help.showOrPrint();
198         return 1;
199     }
200
201     QSplashScreen splash(QPixmap(":/images/splash"), 0);
202     if (GetBoolArg("-splash", true) && !GetBoolArg("-min"))
203     {
204         splash.show();
205         splash.setAutoFillBackground(true);
206         splashref = &splash;
207     }
208
209     app.processEvents();
210
211     app.setQuitOnLastWindowClosed(false);
212
213     try
214     {
215         // Regenerate startup link, to fix links to old versions
216         if (GUIUtil::GetStartOnSystemStartup())
217             GUIUtil::SetStartOnSystemStartup(true);
218
219         BitcoinGUI window;
220         guiref = &window;
221         if(AppInit2())
222         {
223             {
224                 // Put this in a block, so that the Model objects are cleaned up before
225                 // calling Shutdown().
226
227                 if (splashref)
228                     splash.finish(&window);
229
230                 ClientModel clientModel(&optionsModel);
231                 WalletModel walletModel(pwalletMain, &optionsModel);
232
233                 window.setClientModel(&clientModel);
234                 window.setWalletModel(&walletModel);
235
236                 // If -min option passed, start window minimized.
237                 if(GetBoolArg("-min"))
238                 {
239                     window.showMinimized();
240                 }
241                 else
242                 {
243                     window.show();
244                 }
245
246                 // Place this here as guiref has to be defined if we don't want to lose URIs
247                 ipcInit(argc, argv);
248
249                 app.exec();
250
251                 window.hide();
252                 window.setClientModel(0);
253                 window.setWalletModel(0);
254                 guiref = 0;
255             }
256             // Shutdown the core and its threads, but don't exit Bitcoin-Qt here
257             Shutdown(NULL);
258         }
259         else
260         {
261             return 1;
262         }
263     } catch (std::exception& e) {
264         handleRunawayException(&e);
265     } catch (...) {
266         handleRunawayException(NULL);
267     }
268     return 0;
269 }
270 #endif // BITCOIN_QT_TEST