improve .desktop file installed on linux

escape Exec properly so paths with spaces or quotes work, and add Path key so that it starts in the right working directory
This commit is contained in:
jacob1 2015-01-10 00:14:38 -05:00
parent 6eedc7a7a6
commit 4a3a6dee30

View File

@ -344,7 +344,18 @@ bool Client::DoInstallation()
#elif defined(LIN) #elif defined(LIN)
#include "icondoc.h" #include "icondoc.h"
char *currentfilename = exe_name(); std::string filename = exe_name(), pathname = filename.substr(0, filename.rfind('/'));
for (int i = 0; i < filename.size(); i++)
{
if (filename[i] == '\'')
{
filename.insert(i, "'\\'");
i += 3;
}
}
filename.insert(filename.size(), "'");
filename.insert(0, "'");
FILE *f; FILE *f;
const char *mimedata = const char *mimedata =
"<?xml version=\"1.0\"?>\n" "<?xml version=\"1.0\"?>\n"
@ -367,16 +378,14 @@ bool Client::DoInstallation()
"Name=Powder Toy\n" "Name=Powder Toy\n"
"Comment=Physics sandbox game\n" "Comment=Physics sandbox game\n"
"MimeType=x-scheme-handler/ptsave;\n" "MimeType=x-scheme-handler/ptsave;\n"
"NoDisplay=true\n"; "NoDisplay=true\n"
char *protocolfiledata = (char *)malloc(strlen(protocolfiledata_tmp)+strlen(currentfilename)+100); "Categories=Game\n";
strcpy(protocolfiledata, protocolfiledata_tmp); std::stringstream protocolfiledata;
strappend(protocolfiledata, "Exec="); protocolfiledata << protocolfiledata_tmp << "Exec=" << filename <<" ptsave %u\nPath=" << pathname << "\n";
strappend(protocolfiledata, currentfilename);
strappend(protocolfiledata, " ptsave %u\n");
f = fopen("powdertoy-tpt-ptsave.desktop", "wb"); f = fopen("powdertoy-tpt-ptsave.desktop", "wb");
if (!f) if (!f)
return 0; return 0;
fwrite(protocolfiledata, 1, strlen(protocolfiledata), f); fwrite(protocolfiledata.str().c_str(), 1, strlen(protocolfiledata.str().c_str()), f);
fclose(f); fclose(f);
system("xdg-desktop-menu install powdertoy-tpt-ptsave.desktop"); system("xdg-desktop-menu install powdertoy-tpt-ptsave.desktop");
@ -386,16 +395,14 @@ bool Client::DoInstallation()
"Name=Powder Toy\n" "Name=Powder Toy\n"
"Comment=Physics sandbox game\n" "Comment=Physics sandbox game\n"
"MimeType=application/vnd.powdertoy.save;\n" "MimeType=application/vnd.powdertoy.save;\n"
"NoDisplay=true\n"; "NoDisplay=true\n"
char *desktopfiledata = (char *)malloc(strlen(desktopfiledata_tmp)+strlen(currentfilename)+100); "Categories=Game\n";
strcpy(desktopfiledata, desktopfiledata_tmp); std::stringstream desktopfiledata;
strappend(desktopfiledata, "Exec="); desktopfiledata << desktopfiledata_tmp << "Exec=" << filename <<" open %f\nPath=" << pathname << "\n";
strappend(desktopfiledata, currentfilename);
strappend(desktopfiledata, " open %f\n");
f = fopen("powdertoy-tpt.desktop", "wb"); f = fopen("powdertoy-tpt.desktop", "wb");
if (!f) if (!f)
return 0; return 0;
fwrite(desktopfiledata, 1, strlen(desktopfiledata), f); fwrite(desktopfiledata.str().c_str(), 1, strlen(desktopfiledata.str().c_str()), f);
fclose(f); fclose(f);
system("xdg-mime install powdertoy-save.xml"); system("xdg-mime install powdertoy-save.xml");
system("xdg-desktop-menu install powdertoy-tpt.desktop"); system("xdg-desktop-menu install powdertoy-tpt.desktop");