internal static void ImportFromExisting(string settingsFilePath)
        {
            if (string.IsNullOrEmpty(settingsFilePath) || !File.Exists(settingsFilePath))
            {
                return;
            }

            var printerInfo = new PrinterInfo
            {
                Name = Path.GetFileNameWithoutExtension(settingsFilePath),
                ID   = Guid.NewGuid().ToString()
            };

            string importType = Path.GetExtension(settingsFilePath).ToLower();

            switch (importType)
            {
            case ".printer":
                var profile = ProfileManager.LoadProfileFromDisk(settingsFilePath);
                profile.ID = printerInfo.ID;

                // TODO: Resolve name conflicts
                profile.SetName(printerInfo.Name);

                Instance.Profiles.Add(printerInfo);

                profile.SaveChanges();
                break;

            case ".ini":
                var settingsToImport = PrinterSettingsLayer.LoadFromIni(settingsFilePath);

                var layeredProfile = new PrinterSettings()
                {
                    ID       = printerInfo.ID,
                    OemLayer = settingsToImport
                };

                // TODO: Resolve name conflicts
                layeredProfile.UserLayer[SettingsKey.printer_name.ToString()] = printerInfo.Name;

                Instance.Profiles.Add(printerInfo);

                layeredProfile.Save();

                break;
            }

            ProfileManager.Instance.Save();
        }
Example #2
0
        internal static bool ImportFromExisting(string settingsFilePath)
        {
            if (string.IsNullOrEmpty(settingsFilePath) || !File.Exists(settingsFilePath))
            {
                return(false);
            }

            var printerInfo = new PrinterInfo
            {
                Name = Path.GetFileNameWithoutExtension(settingsFilePath),
                ID   = Guid.NewGuid().ToString()
            };
            bool   importSuccessful = false;
            string importType       = Path.GetExtension(settingsFilePath).ToLower();

            switch (importType)
            {
            case ProfileManager.ProfileExtension:
                var profile = PrinterSettings.LoadFile(settingsFilePath);
                profile.ID = printerInfo.ID;
                profile.ClearValue(SettingsKey.device_token);
                printerInfo.DeviceToken = "";

                // TODO: Resolve name conflicts
                profile.Helpers.SetName(printerInfo.Name);

                Instance.Profiles.Add(printerInfo);

                profile.Save();
                importSuccessful = true;
                break;

            case ".ini":
                //Scope variables
            {
                var settingsToImport = PrinterSettingsLayer.LoadFromIni(settingsFilePath);
                var layeredProfile   = new PrinterSettings()
                {
                    ID = printerInfo.ID,
                };

                bool containsValidSetting = false;
                var  activeSettings       = layeredProfile;

                foreach (var item in settingsToImport)
                {
                    if (activeSettings.Contains(item.Key))
                    {
                        containsValidSetting = true;
                        string currentValue = activeSettings.GetValue(item.Key).Trim();
                        // Compare the value to import to the layer cascade value and only set if different
                        if (currentValue != item.Value)
                        {
                            activeSettings.OemLayer[item.Key] = item.Value;
                        }
                    }
                }
                if (containsValidSetting)
                {
                    // TODO: Resolve name conflicts
                    layeredProfile.UserLayer[SettingsKey.printer_name] = printerInfo.Name;

                    layeredProfile.ClearValue(SettingsKey.device_token);
                    printerInfo.DeviceToken = "";
                    Instance.Profiles.Add(printerInfo);

                    layeredProfile.Save();
                    importSuccessful = true;
                }
            }
            break;
            }
            return(importSuccessful);
        }
Example #3
0
        internal static bool ImportFromExisting(string settingsFilePath)
        {
            if (string.IsNullOrEmpty(settingsFilePath) || !File.Exists(settingsFilePath))
            {
                return(false);
            }

            string fileName             = Path.GetFileNameWithoutExtension(settingsFilePath);
            var    existingPrinterNames = Instance.ActiveProfiles.Select(p => p.Name);

            var printerInfo = new PrinterInfo
            {
                Name  = agg_basics.GetNonCollidingName(existingPrinterNames, fileName),
                ID    = Guid.NewGuid().ToString(),
                Make  = "Other",
                Model = "Other",
            };

            bool importSuccessful = false;

            string importType = Path.GetExtension(settingsFilePath).ToLower();

            switch (importType)
            {
            case ProfileManager.ProfileExtension:
                // Add the Settings as a profile before performing any actions on it to ensure file paths resolve
            {
                Instance.Profiles.Add(printerInfo);

                var printerSettings = PrinterSettings.LoadFile(settingsFilePath);
                printerSettings.ID = printerInfo.ID;
                printerSettings.ClearValue(SettingsKey.device_token);
                printerInfo.DeviceToken = "";

                // TODO: Resolve name conflicts
                printerSettings.Helpers.SetName(printerInfo.Name);

                if (printerSettings.OemLayer.ContainsKey(SettingsKey.make))
                {
                    printerInfo.Make = printerSettings.OemLayer[SettingsKey.make];
                }

                if (printerSettings.OemLayer.ContainsKey(SettingsKey.model))
                {
                    printerInfo.Model = printerSettings.OemLayer[SettingsKey.model] ?? "Other";
                }

                printerSettings.Save();
                importSuccessful = true;
            }
            break;

            case ".ini":
                //Scope variables
            {
                var settingsToImport = PrinterSettingsLayer.LoadFromIni(settingsFilePath);
                var printerSettings  = new PrinterSettings()
                {
                    ID = printerInfo.ID,
                };

                bool containsValidSetting = false;

                printerSettings.OemLayer = new PrinterSettingsLayer();

                printerSettings.OemLayer[SettingsKey.make]  = "Other";
                printerSettings.OemLayer[SettingsKey.model] = "Other";

                foreach (var item in settingsToImport)
                {
                    if (printerSettings.Contains(item.Key))
                    {
                        containsValidSetting = true;
                        string currentValue = printerSettings.GetValue(item.Key).Trim();
                        // Compare the value to import to the layer cascade value and only set if different
                        if (currentValue != item.Value)
                        {
                            printerSettings.OemLayer[item.Key] = item.Value;
                        }
                    }
                }

                if (containsValidSetting)
                {
                    printerSettings.UserLayer[SettingsKey.printer_name] = printerInfo.Name;

                    printerSettings.ClearValue(SettingsKey.device_token);
                    printerInfo.DeviceToken = "";

                    printerInfo.Make  = printerSettings.OemLayer[SettingsKey.make] ?? "Other";
                    printerInfo.Model = printerSettings.OemLayer[SettingsKey.model] ?? "Other";

                    Instance.Profiles.Add(printerInfo);

                    printerSettings.Helpers.SetName(printerInfo.Name);

                    printerSettings.Save();
                    importSuccessful = true;
                }
            }
            break;
            }
            return(importSuccessful);
        }