Example #1
0
        public static void LoadSettings()
        {
            Current = new SetSettings();
            PlatformID ptId = DetectOS.GetRealPlatformID();

            FileStream   prefsFs = null;
            StreamReader prefsSr = null;

            try
            {
                switch (ptId)
                {
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");

                    string preferencesFilePath =
                        Path.Combine(preferencesPath, "com.claunia.museum.apprepodbmgr.plist");

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

                    prefsFs = new FileStream(preferencesFilePath, FileMode.Open);
                    var parsedPreferences = (NSDictionary)BinaryPropertyListParser.Parse(prefsFs);

                    if (parsedPreferences != null)
                    {
                        Current.TemporaryFolder = parsedPreferences.TryGetValue("TemporaryFolder", out NSObject obj)
                                                          ? ((NSString)obj).ToString() : Path.GetTempPath();

                        Current.DatabasePath = parsedPreferences.TryGetValue("DatabasePath", out obj)
                                                       ? ((NSString)obj).ToString()
                                                       : Path.
                                               Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                       "apprepodbmgr.db");

                        Current.RepositoryPath = parsedPreferences.TryGetValue("RepositoryPath", out obj)
                                                         ? ((NSString)obj).ToString()
                                                         : Path.
                                                 Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                         "apprepo");

                        Current.UnArchiverPath = parsedPreferences.TryGetValue("UnArchiverPath", out obj)
                                                         ? ((NSString)obj).ToString() : null;

                        if (parsedPreferences.TryGetValue("CompressionAlgorithm", out obj))
                        {
                            if (!Enum.TryParse(((NSString)obj).ToString(), true, out Current.CompressionAlgorithm))
                            {
                                Current.CompressionAlgorithm = AlgoEnum.GZip;
                            }
                        }
                        else
                        {
                            Current.CompressionAlgorithm = AlgoEnum.GZip;
                        }

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

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

                        Current.ClamdHost = parsedPreferences.TryGetValue("ClamdHost", out obj)
                                                    ? ((NSString)obj).ToString() : null;

                        if (parsedPreferences.TryGetValue("ClamdPort", out obj))
                        {
                            Current.ClamdPort = (ushort)((NSNumber)obj).ToLong();
                        }
                        else
                        {
                            Current.ClamdPort = 3310;
                        }

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

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

                        Current.ClamdHost = parsedPreferences.TryGetValue("VirusTotalKey", out obj)
                                                    ? ((NSString)obj).ToString() : null;

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

                        SetDefaultSettings();
                        SaveSettings();
                    }
                }

                break;

                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE")?.
                                            OpenSubKey("Canary Islands Computer Museum");

                    if (parentKey == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    RegistryKey key = parentKey.OpenSubKey("AppRepoDBMgr");

                    if (key == null)
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    Current.TemporaryFolder = (string)key.GetValue("TemporaryFolder");
                    Current.DatabasePath    = (string)key.GetValue("DatabasePath");
                    Current.RepositoryPath  = (string)key.GetValue("RepositoryPath");
                    Current.UnArchiverPath  = (string)key.GetValue("UnArchiverPath");

                    if (!Enum.TryParse((string)key.GetValue("CompressionAlgorithm"), true,
                                       out Current.CompressionAlgorithm))
                    {
                        Current.CompressionAlgorithm = AlgoEnum.GZip;
                    }

                    Current.UseAntivirus  = bool.Parse((string)key.GetValue("UseAntivirus"));
                    Current.UseClamd      = bool.Parse((string)key.GetValue("UseClamd"));
                    Current.ClamdHost     = (string)key.GetValue("ClamdHost");
                    Current.ClamdPort     = ushort.Parse((string)key.GetValue("ClamdPort"));
                    Current.ClamdIsLocal  = bool.Parse((string)key.GetValue("ClamdIsLocal"));
                    Current.UseVirusTotal = bool.Parse((string)key.GetValue("UseVirusTotal"));
                    Current.VirusTotalKey = (string)key.GetValue("VirusTotalKey");
                }

                break;

                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");

                    string settingsPath = Path.Combine(configPath, "AppRepoDBMgr.xml");

                    if (!Directory.Exists(configPath))
                    {
                        SetDefaultSettings();
                        SaveSettings();

                        return;
                    }

                    var xs = new XmlSerializer(Current.GetType());
                    prefsSr = new StreamReader(settingsPath);
                    Current = (SetSettings)xs.Deserialize(prefsSr);
                    prefsSr.Close();
                }

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

                SetDefaultSettings();
                SaveSettings();
            }
        }
Example #2
0
        public static void SaveSettings()
        {
            try
            {
                PlatformID ptId = DetectOS.GetRealPlatformID();

                switch (ptId)
                {
                case PlatformID.MacOSX:
                case PlatformID.iOS:
                {
                    var root = new NSDictionary
                    {
                        {
                            "TemporaryFolder", Current.TemporaryFolder
                        },
                        {
                            "DatabasePath", Current.DatabasePath
                        },
                        {
                            "RepositoryPath", Current.RepositoryPath
                        },
                        {
                            "UnArchiverPath", Current.UnArchiverPath
                        },
                        {
                            "CompressionAlgorithm", Current.CompressionAlgorithm.ToString()
                        },
                        {
                            "UseAntivirus", Current.UseAntivirus
                        },
                        {
                            "UseClamd", Current.UseClamd
                        },
                        {
                            "ClamdHost", Current.ClamdHost
                        },
                        {
                            "ClamdPort", Current.ClamdPort
                        },
                        {
                            "ClamdIsLocal", Current.ClamdIsLocal
                        },
                        {
                            "UseVirusTotal", Current.UseVirusTotal
                        },
                        {
                            "VirusTotalKey", Current.VirusTotalKey
                        }
                    };

                    string preferencesPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library",
                                     "Preferences");

                    string preferencesFilePath =
                        Path.Combine(preferencesPath, "com.claunia.museum.apprepodbmgr.plist");

                    var fs = new FileStream(preferencesFilePath, FileMode.Create);
                    BinaryPropertyListWriter.Write(fs, root);
                    fs.Close();
                }

                break;

                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.WinCE:
                case PlatformID.WindowsPhone:
                {
                    RegistryKey parentKey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true)?.
                                            CreateSubKey("Canary Islands Computer Museum");

                    RegistryKey key = parentKey?.CreateSubKey("AppRepoDBMgr");

                    if (key != null)
                    {
                        key.SetValue("TemporaryFolder", Current.TemporaryFolder);
                        key.SetValue("DatabasePath", Current.DatabasePath);
                        key.SetValue("RepositoryPath", Current.RepositoryPath);

                        if (Current.UnArchiverPath != null)
                        {
                            key.SetValue("UnArchiverPath", Current.UnArchiverPath);
                        }

                        key.SetValue("CompressionAlgorithm", Current.CompressionAlgorithm);
                        key.SetValue("UseAntivirus", Current.UseAntivirus);
                        key.SetValue("UseClamd", Current.UseClamd);
                        key.SetValue("ClamdHost", Current.ClamdHost == null ? "" : Current.ClamdHost);
                        key.SetValue("ClamdPort", Current.ClamdPort);
                        key.SetValue("ClamdIsLocal", Current.ClamdIsLocal);
                        key.SetValue("UseVirusTotal", Current.UseVirusTotal);
                        key.SetValue("VirusTotalKey", Current.VirusTotalKey == null ? "" : Current.VirusTotalKey);
                    }
                }

                break;

                default:
                {
                    string configPath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");

                    string settingsPath = Path.Combine(configPath, "AppRepoDBMgr.xml");

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

                    var fs = new FileStream(settingsPath, FileMode.Create);
                    var xs = new XmlSerializer(Current.GetType());
                    xs.Serialize(fs, Current);
                    fs.Close();
                }

                break;
                }
            }
            #pragma warning disable RECS0022     // A catch clause that catches System.Exception and has an empty body
            catch
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }
            }
        }