2222#endif // WIN32
2323
2424#ifdef Q_OS_LINUX
25+ #include < cassert>
2526#include < stdlib.h>
2627#include < unistd.h>
2728#endif // Q_OS_LINUX
3031#include < QDir>
3132#include < QFileDialog>
3233#include < QFileInfo>
34+ #include < QPixmap>
3335#include < QRegExp>
3436#include < QStandardPaths>
3537#include < QString>
@@ -49,11 +51,88 @@ std::string GetExecutablePathAsString()
4951 exe_path[r] = ' \0 ' ;
5052 return exe_path;
5153}
54+
55+ fs::path GetUserApplicationsDir ()
56+ {
57+ const char * home_dir = getenv (" HOME" );
58+ if (!home_dir) return fs::path ();
59+ return fs::path (home_dir) / " .local" / " share" / " applications" ;
60+ }
61+
62+ fs::path GetDesktopFilePath (std::string chain)
63+ {
64+ const auto app_dir = GetUserApplicationsDir ();
65+ if (app_dir.empty ()) return fs::path ();
66+ if (!chain.empty ()) {
67+ chain = " _" + chain;
68+ }
69+ return app_dir / strprintf (" org.bitcoincore.BitcoinQt%s.desktop" , chain);
70+ }
71+
72+ fs::path GetUserIconsDir ()
73+ {
74+ const char * home_dir = getenv (" HOME" );
75+ if (!home_dir) return fs::path ();
76+ return fs::path (home_dir) / " .local" / " share" / " icons" ;
77+ }
78+
79+ fs::path GetIconPath (std::string chain)
80+ {
81+ const auto icons_dir = GetUserIconsDir ();
82+ if (icons_dir.empty ()) return fs::path ();
83+ if (!chain.empty ()) {
84+ chain = " -" + chain;
85+ }
86+ return icons_dir / strprintf (" bitcoin%s.png" , chain);
87+ }
5288#endif // Q_OS_LINUX
5389} // namespace
5490
5591namespace GUIUtil {
5692
93+ #ifdef Q_OS_LINUX
94+ bool IntegrateWithDesktopEnvironment (QIcon icon)
95+ {
96+ std::string chain = gArgs .GetChainName ();
97+ assert (chain == CBaseChainParams::MAIN || chain == CBaseChainParams::TESTNET);
98+ if (chain == CBaseChainParams::MAIN) {
99+ chain.clear ();
100+ }
101+
102+ const auto icon_path = GetIconPath (chain);
103+ if (icon_path.empty () || !icon.pixmap (256 ).save (boostPathToQString (icon_path))) return false ;
104+ const auto exe_path = GetExecutablePathAsString ();
105+ if (exe_path.empty ()) return false ;
106+ const auto desktop_file_path = GetDesktopFilePath (chain);
107+ if (desktop_file_path.empty ()) return false ;
108+ fsbridge::ofstream desktop_file (desktop_file_path, std::ios_base::out | std::ios_base::trunc);
109+ if (!desktop_file.good ()) return false ;
110+
111+ desktop_file << " [Desktop Entry]\n " ;
112+ desktop_file << " Type=Application\n " ;
113+ desktop_file << " Version=1.1\n " ;
114+ desktop_file << " GenericName=Bitcoin full node and wallet\n " ;
115+ desktop_file << " Comment=Connect to the Bitcoin P2P network\n " ;
116+ desktop_file << strprintf (" Icon=%s\n " , icon_path.stem ().string ());
117+ desktop_file << strprintf (" TryExec=%s\n " , exe_path);
118+ desktop_file << " Categories=Network;Office;Finance;\n " ;
119+ if (chain.empty ()) {
120+ desktop_file << " Name=" PACKAGE_NAME " \n " ;
121+ desktop_file << strprintf (" Exec=%s %%u\n " , exe_path);
122+ desktop_file << " Actions=Testnet;\n " ;
123+ desktop_file << " [Desktop Action Testnet]\n " ;
124+ desktop_file << strprintf (" Exec=%s -testnet\n " , exe_path);
125+ desktop_file << " Name=Testnet mode\n " ;
126+ } else {
127+ desktop_file << " Name=" PACKAGE_NAME " - Testnet\n " ;
128+ desktop_file << strprintf (" Exec=%s -testnet %%u\n " , exe_path);
129+ }
130+
131+ desktop_file.close ();
132+ return true ;
133+ }
134+ #endif // Q_OS_LINUX
135+
57136QString getDefaultDataDirectory ()
58137{
59138 return boostPathToQString (GetDefaultDataDir ());
0 commit comments