Esempio n. 1
0
 /// <summary>Sets default settings as all statistics, share everything</summary>
 static void SetDefaultSettings() => Current = new DicSettings
 {
     SaveReportsGlobally = true, ShareReports = true, GdprCompliance = 0, Stats = new StatsSettings
     {
         CommandStats   = true, DeviceStats = true, FilesystemStats = true, MediaImageStats = true,
         MediaScanStats = true, FilterStats = true, MediaStats = true, PartitionStats = true,
         ShareStats     = true, VerifyStats = true
     }
 };
Esempio n. 2
0
 /// <summary>
 ///     Sets default settings as all statistics, share everything
 /// </summary>
 static void SetDefaultSettings()
 {
     Current = new DicSettings
     {
         SaveReportsGlobally = true,
         ShareReports        = true,
         Stats = new StatsSettings
         {
             BenchmarkStats  = true,
             CommandStats    = true,
             DeviceStats     = true,
             FilesystemStats = true,
             MediaImageStats = true,
             MediaScanStats  = true,
             FilterStats     = true,
             MediaStats      = true,
             PartitionStats  = true,
             ShareStats      = true,
             VerifyStats     = true
         }
     };
 }
Esempio n. 3
0
        /// <summary>
        ///     Loads settings
        /// </summary>
        public static void LoadSettings()
        {
            Current = new DicSettings();
            PlatformID ptId     = DetectOS.GetRealPlatformID();
            string     homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

            try
            {
                switch (ptId)
                {
                // In case of macOS or iOS statistics and reports will be saved in ~/Library/Application Support/Claunia.com/DiscImageChef
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string appSupportPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Application Support", "Claunia.com");
                    if (!Directory.Exists(appSupportPath))
                    {
                        Directory.CreateDirectory(appSupportPath);
                    }

                    string dicPath = Path.Combine(appSupportPath, "DiscImageChef");
                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;

                // In case of Windows statistics and reports will be saved in %APPDATA%\Claunia.com\DiscImageChef
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    string appSupportPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                     "Claunia.com");
                    if (!Directory.Exists(appSupportPath))
                    {
                        Directory.CreateDirectory(appSupportPath);
                    }

                    string dicPath = Path.Combine(appSupportPath, "DiscImageChef");
                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;

                // Otherwise, statistics and reports will be saved in ~/.claunia.com/DiscImageChef
                default:
                {
                    string xdgDataPath =
                        Path.Combine(homePath,
                                     Environment.GetEnvironmentVariable(XDG_DATA_HOME) ?? XDG_DATA_HOME_RESOLVED);

                    string oldDicPath = Path.Combine(homePath, ".claunia.com", "DiscImageChef");
                    string dicPath    = Path.Combine(xdgDataPath, "DiscImageChef");

                    if (Directory.Exists(oldDicPath) && !Directory.Exists(dicPath))
                    {
                        Directory.Move(oldDicPath, dicPath);
                        Directory.Delete(Path.Combine(homePath, ".claunia.com"));
                    }

                    if (!Directory.Exists(dicPath))
                    {
                        Directory.CreateDirectory(dicPath);
                    }

                    ReportsPath = Path.Combine(dicPath, "Reports");
                    if (!Directory.Exists(ReportsPath))
                    {
                        Directory.CreateDirectory(ReportsPath);
                    }

                    StatsPath = Path.Combine(dicPath, "Statistics");
                    if (!Directory.Exists(StatsPath))
                    {
                        Directory.CreateDirectory(StatsPath);
                    }
                }
                break;
                }
            }
            catch { ReportsPath = null; }

            FileStream   prefsFs = null;
            StreamReader prefsSr = null;

            try
            {
                switch (ptId)
                {
                // In case of macOS or iOS settings will be saved in ~/Library/Preferences/com.claunia.discimagechef.plist
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");
                    string preferencesFilePath = Path.Combine(preferencesPath, "com.claunia.discimagechef.plist");

                    if (!File.Exists(preferencesFilePath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                    }

                    prefsFs = new FileStream(preferencesFilePath, FileMode.Open, FileAccess.Read);

                    NSDictionary parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);
                    if (parsedPreferences != null)
                    {
                        Current.SaveReportsGlobally =
                            parsedPreferences.TryGetValue("SaveReportsGlobally", out NSObject obj) &&
                            ((NSNumber)obj).ToBool();

                        Current.ShareReports = parsedPreferences.TryGetValue("ShareReports", out obj) &&
                                               ((NSNumber)obj).ToBool();

                        if (parsedPreferences.TryGetValue("Stats", out obj))
                        {
                            NSDictionary stats = (NSDictionary)obj;

                            if (stats != null)
                            {
                                Current.Stats = new StatsSettings
                                {
                                    ShareStats =
                                        stats.TryGetValue("ShareStats", out NSObject obj2) &&
                                        ((NSNumber)obj2).ToBool(),
                                    BenchmarkStats =
                                        stats.TryGetValue("BenchmarkStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    CommandStats =
                                        stats.TryGetValue("CommandStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    DeviceStats =
                                        stats.TryGetValue("DeviceStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    FilesystemStats =
                                        stats.TryGetValue("FilesystemStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    FilterStats =
                                        stats.TryGetValue("FilterStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaImageStats =
                                        stats.TryGetValue("MediaImageStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaScanStats =
                                        stats.TryGetValue("MediaScanStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    PartitionStats =
                                        stats.TryGetValue("PartitionStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    MediaStats =
                                        stats.TryGetValue("MediaStats", out obj2) && ((NSNumber)obj2).ToBool(),
                                    VerifyStats =
                                        stats.TryGetValue("VerifyStats", out obj2) && ((NSNumber)obj2).ToBool()
                                }
                            }
                            ;
                        }
                        else
                        {
                            Current.Stats = null;
                        }

                        Current.GdprCompliance = parsedPreferences.TryGetValue("GdprCompliance", out obj)
                                                         ? (ulong)((NSNumber)obj).ToLong()
                                                         : 0;

                        prefsFs.Close();
                    }
                    else
                    {
                        prefsFs.Close();

                        SetDefaultSettings();
                        SaveSettings();
                    }
                }
                break;

#if !NETSTANDARD2_0
                // In case of Windows settings will be saved in the registry: HKLM/SOFTWARE/Claunia.com/DiscImageChef
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.OpenSubKey("Claunia.com");
                    if (parentKey == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    RegistryKey key = parentKey.OpenSubKey("DiscImageChef");
                    if (key == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    Current.SaveReportsGlobally = Convert.ToBoolean(key.GetValue("SaveReportsGlobally"));
                    Current.ShareReports        = Convert.ToBoolean(key.GetValue("ShareReports"));
                    Current.GdprCompliance      = Convert.ToUInt64(key.GetValue("GdprCompliance"));

                    bool stats = Convert.ToBoolean(key.GetValue("Statistics"));
                    if (stats)
                    {
                        Current.Stats = new StatsSettings
                        {
                            ShareStats      = Convert.ToBoolean(key.GetValue("ShareStats")),
                            BenchmarkStats  = Convert.ToBoolean(key.GetValue("BenchmarkStats")),
                            CommandStats    = Convert.ToBoolean(key.GetValue("CommandStats")),
                            DeviceStats     = Convert.ToBoolean(key.GetValue("DeviceStats")),
                            FilesystemStats = Convert.ToBoolean(key.GetValue("FilesystemStats")),
                            FilterStats     = Convert.ToBoolean(key.GetValue("FilterStats")),
                            MediaImageStats = Convert.ToBoolean(key.GetValue("MediaImageStats")),
                            MediaScanStats  = Convert.ToBoolean(key.GetValue("MediaScanStats")),
                            PartitionStats  = Convert.ToBoolean(key.GetValue("PartitionStats")),
                            MediaStats      = Convert.ToBoolean(key.GetValue("MediaStats")),
                            VerifyStats     = Convert.ToBoolean(key.GetValue("VerifyStats"))
                        }
                    }
                    ;
                }

                break;
#endif
                // Otherwise, settings will be saved in ~/.config/DiscImageChef.xml
                default:
                {
                    string oldConfigPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");
                    string oldSettingsPath = Path.Combine(oldConfigPath, "DiscImageChef.xml");

                    string xdgConfigPath =
                        Path.Combine(homePath,
                                     Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ??
                                     XDG_CONFIG_HOME_RESOLVED);
                    string settingsPath = Path.Combine(xdgConfigPath, "DiscImageChef.xml");

                    if (File.Exists(oldSettingsPath) && !File.Exists(settingsPath))
                    {
                        if (!Directory.Exists(xdgConfigPath))
                        {
                            Directory.CreateDirectory(xdgConfigPath);
                        }
                        File.Move(oldSettingsPath, settingsPath);
                    }

                    if (!File.Exists(settingsPath))
                    {
                        SetDefaultSettings();
                        SaveSettings();
                        return;
                    }

                    XmlSerializer xs = new XmlSerializer(Current.GetType());
                    prefsSr = new StreamReader(settingsPath);
                    Current = (DicSettings)xs.Deserialize(prefsSr);
                }

                break;
                }
            }
            catch
            {
                prefsFs?.Close();
                prefsSr?.Close();
                SetDefaultSettings();
                SaveSettings();
            }
        }