Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the autostart mainnet/testnet aware #1671

Merged
merged 4 commits into from
May 13, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 112 additions & 6 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "bitcoinunits.h"
#include "util.h"
#include "init.h"
#include <codecvt>

#include <QString>
#include <QDateTime>
Expand Down Expand Up @@ -376,23 +377,93 @@ bool WindowContextHelpButtonHintFilter::eventFilter (QObject *obj, QEvent *event
return QObject::eventFilter (obj, event);
}


struct AutoStartupArguments
{
std::string link_name_suffix;
boost::filesystem::path data_dir;
std::string arguments;
};

AutoStartupArguments GetAutoStartupArguments()
{
// This helper function checks for the presence of certain startup arguments
// to the current running instance that should be relevant for automatic restart
// (currently testnet, datadir, scraper, explorer, usenewnn). It adds -testnet
// to the link name as a suffix if -testnet is specified otherwise adds mainnet,
// and then adds the other three as arguments if they were specified for the
// running instance. This allows two different automatic startups, one for
// mainnet, and the other for testnet, and each of them can have different datadir,
// scraper, explorer, and/or usenewnn arguments.

AutoStartupArguments result;

// We can't use GetDataDir() here because it appends /testnet for
// testnet, which is correct for internal use, but not what we
// need for the command line argument. I think the fNetSpecific flag
// for GetDataDir is not coded right.
// TODO: Review GetDataDir() fix and replace this.
jamescowens marked this conversation as resolved.
Show resolved Hide resolved
if (mapArgs.count("-datadir"))
{
result.data_dir = fs::system_complete(mapArgs["-datadir"]);
if (!fs::is_directory(result.data_dir))
{
result.data_dir = "";
}
}

result.arguments = "-min";

if (fTestNet)
{
result.link_name_suffix += "-testnet";
result.arguments += " -testnet";
}
else
{
result.link_name_suffix += "-mainnet";
}

if (GetBoolArg("-scraper")) result.arguments += " -scraper";

if (GetBoolArg("-explorer")) result.arguments += " -explorer";

if (GetBoolArg("-usenewnn")) result.arguments += " -usenewnn";
jamescowens marked this conversation as resolved.
Show resolved Hide resolved

return result;
}

#ifdef WIN32
boost::filesystem::path static StartupShortcutPath()
boost::filesystem::path static StartupShortcutLegacyPath()
{
return GetSpecialFolderPath(CSIDL_STARTUP) / "Gridcoin.lnk";
}

boost::filesystem::path static StartupShortcutPath()
{
std::string link_name_suffix = GetAutoStartupArguments().link_name_suffix;
std::string link_name_root = "Gridcoin";

return GetSpecialFolderPath(CSIDL_STARTUP) / (link_name_root + link_name_suffix + ".lnk");
}

bool GetStartOnSystemStartup()
{
// check for Bitcoin.lnk
// check for Gridcoin.lnk
return boost::filesystem::exists(StartupShortcutPath());
}

bool SetStartOnSystemStartup(bool fAutoStart)
{
// Remove the legacy shortcut unconditionally.
boost::filesystem::remove(StartupShortcutLegacyPath());

// If the shortcut exists already, remove it for updating
boost::filesystem::remove(StartupShortcutPath());

// Get auto startup arguments
AutoStartupArguments autostartup = GetAutoStartupArguments();

if (fAutoStart)
{
CoInitialize(NULL);
Expand All @@ -409,7 +480,19 @@ bool SetStartOnSystemStartup(bool fAutoStart)
WCHAR pszExePath[MAX_PATH];
GetModuleFileNameW(NULL, pszExePath, sizeof(pszExePath));

TCHAR pszArgs[5] = TEXT("-min");
std::wstring autostartup_arguments;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;

if (!autostartup.data_dir.empty())
{
autostartup_arguments = converter.from_bytes("-datadir=")
+ autostartup.data_dir.wstring()
+ converter.from_bytes(" ");
jamescowens marked this conversation as resolved.
Show resolved Hide resolved
}

autostartup_arguments += converter.from_bytes(autostartup.arguments);

LPCTSTR pszArgs = autostartup_arguments.c_str();

// Set the path to the shortcut target
psl->SetPath(pszExePath);
Expand Down Expand Up @@ -456,11 +539,19 @@ boost::filesystem::path static GetAutostartDir()
return fs::path();
}

boost::filesystem::path static GetAutostartFilePath()
boost::filesystem::path static GetAutostartLegacyFilePath()
{
return GetAutostartDir() / "gridcoin.desktop";
}

boost::filesystem::path static GetAutostartFilePath()
{
std::string link_name_suffix = GetAutoStartupArguments().link_name_suffix;
std::string link_name_root = "gridcoin";

return GetAutostartDir() / (link_name_root + link_name_suffix + ".desktop");
}

bool GetStartOnSystemStartup()
{
fsbridge::ifstream optionFile(GetAutostartFilePath());
Expand All @@ -482,6 +573,12 @@ bool GetStartOnSystemStartup()

bool SetStartOnSystemStartup(bool fAutoStart)
{
// Remove legacy autostart path if it exists.
if (boost::filesystem::exists(GetAutostartLegacyFilePath()))
{
boost::filesystem::remove(GetAutostartLegacyFilePath());
}

if (!fAutoStart)
boost::filesystem::remove(GetAutostartFilePath());
else
Expand All @@ -491,6 +588,8 @@ bool SetStartOnSystemStartup(bool fAutoStart)
if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
return false;

AutoStartupArguments autostartup = GetAutoStartupArguments();

boost::filesystem::create_directories(GetAutostartDir());

fsbridge::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
Expand All @@ -499,8 +598,15 @@ bool SetStartOnSystemStartup(bool fAutoStart)
// Write a bitcoin.desktop file to the autostart directory:
optionFile << "[Desktop Entry]\n";
optionFile << "Type=Application\n";
optionFile << "Name=Gridcoin\n";
optionFile << "Exec=" << pszExePath << " -min\n";
optionFile << "Name=Gridcoin" + autostartup.link_name_suffix + "\n";
optionFile << "Exec=" << pszExePath;

if (!autostartup.data_dir.empty())
{
optionFile << " -datadir=" << autostartup.data_dir;
jamescowens marked this conversation as resolved.
Show resolved Hide resolved
}

optionFile << " " << autostartup.arguments << "\n";
optionFile << "Terminal=false\n";
optionFile << "Hidden=false\n";
optionFile.close();
Expand Down