installation info: Refactor OS detection, support Ubuntu and GNOME

This commit is contained in:
Hylke Bons 2016-04-10 09:48:18 -07:00
parent 06766a57f6
commit b9efe59200
6 changed files with 47 additions and 39 deletions

View file

@ -197,7 +197,7 @@ namespace SparkleShare {
{
string app_data_path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
if (InstallationInfo.Platform == PlatformID.Unix)
if (InstallationInfo.OperatingSystem != OS.Windows && InstallationInfo.OperatingSystem != OS.Mac)
app_data_path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".config");
string config_path = Path.Combine (app_data_path, "org.sparkleshare.SparkleShare");
@ -216,8 +216,9 @@ namespace SparkleShare {
{
Logger.LogInfo ("Environment", "SparkleShare " + InstallationInfo.Version);
Logger.LogInfo ("Environment", "Git " + Sparkles.Git.GitCommand.GitVersion);
Logger.LogInfo ("Environment", InstallationInfo.Platform + " (" + Environment.OSVersion + ")");
Logger.LogInfo ("Environment", InstallationInfo.OperatingSystem + " (" + Environment.OSVersion + ")");
// todo tostring with nice os version names
Preset.PresetsPath = PresetsPath;
InstallProtocolHandler ();

View file

@ -39,8 +39,8 @@ namespace SparkleShare {
Arguments = args;
if (args.Length != 0 && (args [0].Equals ("help") || args [0].Equals ("version")) &&
InstallationInfo.Platform != PlatformID.MacOSX &&
InstallationInfo.Platform != PlatformID.Win32NT) {
InstallationInfo.OperatingSystem != OS.Mac &&
InstallationInfo.OperatingSystem != OS.Windows) {
string n = Environment.NewLine;

View file

@ -35,7 +35,7 @@ namespace Sparkles {
public string HomePath {
get {
if (InstallationInfo.Platform == PlatformID.Win32NT)
if (InstallationInfo.OperatingSystem == OS.Windows)
return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
@ -113,12 +113,11 @@ namespace Sparkles {
{
string user_name = Environment.UserName;
if (InstallationInfo.Platform == PlatformID.Unix ||
InstallationInfo.Platform == PlatformID.MacOSX) {
if (InstallationInfo.OperatingSystem != OS.Windows) {
if (string.IsNullOrEmpty (user_name))
user_name = "Unknown";
else
// On Unix systems the user name may have commas appended
user_name = user_name.TrimEnd (',');
}

View file

@ -252,7 +252,7 @@ namespace Sparkles.Git {
"push.default matching"
};
if (InstallationInfo.Platform == PlatformID.Win32NT)
if (InstallationInfo.OperatingSystem == OS.Windows)
settings [0] = "core.autocrlf true";
foreach (string setting in settings) {

View file

@ -17,44 +17,52 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Sparkles {
public enum OS
{
Mac,
Windows,
Ubuntu,
GNOME
}
public partial class InstallationInfo {
static OS operating_system = OS.Windows;
public static OS OperatingSystem {
get {
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
return operating_system;
var uname = new Command ("uname", "-a");
string output = uname.StartAndReadStandardOutput ();
// Environment.OSVersion.Platform.PlatformID.MacOSX is broken in Mono
// for historical reasons, so check manually
if (output.StartsWith ("Darwin", StringComparison.InvariantCulture)) {
operating_system = OS.Mac;
} else if (output.Contains ("Ubuntu")) {
operating_system = OS.Ubuntu;
} else {
operating_system = OS.GNOME;
}
return operating_system;
}
}
public static string Version {
get {
string version = "" + Assembly.GetExecutingAssembly ().GetName ().Version;
return version.Substring (0, version.Length - 2);
}
}
// This fixes the PlatformID enumeration for MacOSX in Environment.OSVersion.Platform,
// which is intentionally broken in Mono for historical reasons
public static PlatformID Platform {
get {
IntPtr buf = IntPtr.Zero;
try {
buf = Marshal.AllocHGlobal (8192);
if (uname (buf) == 0 && Marshal.PtrToStringAnsi (buf) == "Darwin")
return PlatformID.MacOSX;
} catch {
} finally {
if (buf != IntPtr.Zero)
Marshal.FreeHGlobal (buf);
}
return Environment.OSVersion.Platform;
}
}
[DllImport ("libc")]
static extern int uname (IntPtr buf);
}
}

View file

@ -65,7 +65,7 @@ namespace Sparkles {
{
string home_path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
if (InstallationInfo.Platform == PlatformID.Win32NT)
if (InstallationInfo.OperatingSystem == OS.Windows)
home_path = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
string crash_report_file_path = Path.Combine (home_path, "SparkleShare", "crash_report.txt");