interface_ui.cpp raw
1 // Copyright (c) 2010-2022 The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <node/interface_ui.h>
6
7 #include <util/string.h>
8 #include <util/translation.h>
9
10 #include <boost/signals2/optional_last_value.hpp>
11 #include <boost/signals2/signal.hpp>
12
13 using util::MakeUnorderedList;
14
15 CClientUIInterface uiInterface;
16
17 struct UISignals {
18 boost::signals2::signal<CClientUIInterface::ThreadSafeMessageBoxSig, boost::signals2::optional_last_value<bool>> ThreadSafeMessageBox;
19 boost::signals2::signal<CClientUIInterface::ThreadSafeQuestionSig, boost::signals2::optional_last_value<bool>> ThreadSafeQuestion;
20 boost::signals2::signal<CClientUIInterface::InitMessageSig> InitMessage;
21 boost::signals2::signal<CClientUIInterface::InitWalletSig> InitWallet;
22 boost::signals2::signal<CClientUIInterface::NotifyNumConnectionsChangedSig> NotifyNumConnectionsChanged;
23 boost::signals2::signal<CClientUIInterface::NotifyNetworkActiveChangedSig> NotifyNetworkActiveChanged;
24 boost::signals2::signal<CClientUIInterface::NotifyNetworkLocalChangedSig> NotifyNetworkLocalChanged;
25 boost::signals2::signal<CClientUIInterface::NotifyAlertChangedSig> NotifyAlertChanged;
26 boost::signals2::signal<CClientUIInterface::ShowProgressSig> ShowProgress;
27 boost::signals2::signal<CClientUIInterface::NotifyBlockTipSig> NotifyBlockTip;
28 boost::signals2::signal<CClientUIInterface::NotifyHeaderTipSig> NotifyHeaderTip;
29 boost::signals2::signal<CClientUIInterface::BannedListChangedSig> BannedListChanged;
30 };
31 static UISignals g_ui_signals;
32
33 #define ADD_SIGNALS_IMPL_WRAPPER(signal_name) \
34 boost::signals2::connection CClientUIInterface::signal_name##_connect(std::function<signal_name##Sig> fn) \
35 { \
36 return g_ui_signals.signal_name.connect(fn); \
37 }
38
39 ADD_SIGNALS_IMPL_WRAPPER(ThreadSafeMessageBox);
40 ADD_SIGNALS_IMPL_WRAPPER(ThreadSafeQuestion);
41 ADD_SIGNALS_IMPL_WRAPPER(InitMessage);
42 ADD_SIGNALS_IMPL_WRAPPER(InitWallet);
43 ADD_SIGNALS_IMPL_WRAPPER(NotifyNumConnectionsChanged);
44 ADD_SIGNALS_IMPL_WRAPPER(NotifyNetworkActiveChanged);
45 ADD_SIGNALS_IMPL_WRAPPER(NotifyNetworkLocalChanged);
46 ADD_SIGNALS_IMPL_WRAPPER(NotifyAlertChanged);
47 ADD_SIGNALS_IMPL_WRAPPER(ShowProgress);
48 ADD_SIGNALS_IMPL_WRAPPER(NotifyBlockTip);
49 ADD_SIGNALS_IMPL_WRAPPER(NotifyHeaderTip);
50 ADD_SIGNALS_IMPL_WRAPPER(BannedListChanged);
51
52 bool CClientUIInterface::ThreadSafeMessageBox(const bilingual_str& message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeMessageBox(message, caption, style).value_or(false);}
53 bool CClientUIInterface::ThreadSafeQuestion(const bilingual_str& message, const std::string& non_interactive_message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeQuestion(message, non_interactive_message, caption, style).value_or(false);}
54 void CClientUIInterface::InitMessage(const std::string& message) { return g_ui_signals.InitMessage(message); }
55 void CClientUIInterface::InitWallet() { return g_ui_signals.InitWallet(); }
56 void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { return g_ui_signals.NotifyNumConnectionsChanged(newNumConnections); }
57 void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
58 void CClientUIInterface::NotifyNetworkLocalChanged() { return g_ui_signals.NotifyNetworkLocalChanged(); }
59 void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
60 void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
61 void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
62 void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); }
63 void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
64
65 bool InitError(const bilingual_str& str)
66 {
67 uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR);
68 return false;
69 }
70
71 bool InitError(const bilingual_str& str, const std::vector<std::string>& details)
72 {
73 // For now just flatten the list of error details into a string to pass to
74 // the base InitError overload. In the future, if more init code provides
75 // error details, the details could be passed separately from the main
76 // message for rich display in the GUI. But currently the only init
77 // functions which provide error details are ones that run during early init
78 // before the GUI uiInterface is registered, so there's no point passing
79 // main messages and details separately to uiInterface yet.
80 return InitError(details.empty() ? str : str + Untranslated(strprintf(":\n%s", MakeUnorderedList(details))));
81 }
82
83 void InitWarning(const bilingual_str& str)
84 {
85 uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_WARNING);
86 }
87