Beispiel #1
0
        /// ------------------------------------------------------------------------------------
        public void Load()
        {
            try
            {
                // Previously was SIL.IO.RobustIO.LoadXElement(SettingsFilePath). However, we had problems with this
                // using some non-roman collection names...specifically, one involving the Northern Pashto
                // localization of 'books' (┌й╪к╪з╪и┘И┘Ж┘З)...see BL-5416. It seems that somewhere in the
                // implementation of Linq.XElement.Load() the path is converted to a URL and then back
                // to a path and something changes in that process so that a valid path passed to Load()
                // raises an invalid path exception. Reading the file directly and then parsing the string
                // works around this problem.
                var settingsContent = RobustFile.ReadAllText(SettingsFilePath, Encoding.UTF8);
                var nameMigrations  = new[]
                {
                    new[] { "LanguageName", "Language1Name" },
                    new[] { "IsShellLibrary", "IsSourceCollection" },
                    new[] { "National1Iso639Code", "Language2Iso639Code" },
                    new[] { "National2Iso639Code", "Language3Iso639Code" },
                    new[] { "IsShellMakingProject", "IsSourceCollection" },
                    new[] { "Local Community", "Local-Community" }                   // migrate for 4.4
                };

                foreach (var fromTo in nameMigrations)
                {
                    settingsContent = settingsContent.Replace(fromTo[0], fromTo[1]);
                }

                var xml = XElement.Parse(settingsContent);
                // The default if we don't find one is the arbitrary ID generated when we initialized
                // the variable (at its declaration).
                CollectionId = ReadString(xml, "CollectionId", CollectionId);

                Language1.ReadFromXml(xml, true, "en");
                Language2.ReadFromXml(xml, true, "self");
                Language3.ReadFromXml(xml, true, Language2.Iso639Code);

                SignLanguageIso639Code = ReadString(xml, "SignLanguageIso639Code",                  /* old name */
                                                    ReadString(xml, "SignLanguageIso639Code", ""));
                XMatterPackName = ReadString(xml, "XMatterPack", "Factory");

                var style = ReadString(xml, "PageNumberStyle", "Decimal");

                //for historical (and maybe future?) reasons, we collect the page number style as one of the
                //CSS counter number styles
                PageNumberStyle           = CssNumberStylesToCultureOrDigits.Keys.Contains(style) ? style : "Decimal";
                OneTimeCheckVersionNumber = ReadInteger(xml, "OneTimeCheckVersionNumber", 0);
                BrandingProjectKey        = ReadString(xml, "BrandingProjectName", "Default");
                SubscriptionCode          = ReadString(xml, "SubscriptionCode", null);

                if (BrandingProjectKey != "Default" && BrandingProjectKey != "Local-Community" && !Program.RunningHarvesterMode)
                {
                    // Validate branding, so things can't be circumvented by just typing something random into settings
                    var expirationDate = CollectionSettingsApi.GetExpirationDate(SubscriptionCode);
                    if (expirationDate < DateTime.Now)                      // no longer require branding files to exist yet
                    {
                        InvalidBranding    = BrandingProjectKey;
                        BrandingProjectKey = "Default";                         // keep the code, but don't use it as active branding.
                    }
                }
                SignLanguageName   = ReadString(xml, "SignLanguageName", GetSignLanguageName_NoCache());
                Country            = ReadString(xml, "Country", "");
                Province           = ReadString(xml, "Province", "");
                District           = ReadString(xml, "District", "");
                AllowNewBooks      = ReadBoolean(xml, "AllowNewBooks", true);
                IsSourceCollection = ReadBoolean(xml, "IsSourceCollection", false);

                string audioRecordingModeStr = ReadString(xml, "AudioRecordingMode", "Unknown");
                TalkingBookApi.AudioRecordingMode parsedAudioRecordingMode;
                if (!Enum.TryParse(audioRecordingModeStr, out parsedAudioRecordingMode))
                {
                    parsedAudioRecordingMode = TalkingBookApi.AudioRecordingMode.Unknown;
                }
                AudioRecordingMode = parsedAudioRecordingMode;
                AudioRecordingTrimEndMilliseconds = ReadInteger(xml, "AudioRecordingTrimEndMilliseconds",
                                                                kDefaultAudioRecordingTrimEndMilliseconds);
                Administrators = ReadString(xml, "Administrators", "")
                                 .Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                var defaultTags         = ReadString(xml, "DefaultBookTags", "").Split(',');
                var defaultBookshelfTag = defaultTags.Where(t => t.StartsWith("bookshelf:")).FirstOrDefault();
                DefaultBookshelf = defaultBookshelfTag == null
                                        ? ""
                                        : defaultBookshelfTag.Substring("bookshelf:".Length);

                var bulkPublishSettingsFromXml = BulkBloomPubPublishSettings.LoadFromXElement(xml);
                if (bulkPublishSettingsFromXml != null)
                {
                    BulkPublishBloomPubSettings = bulkPublishSettingsFromXml;
                }
            }
            catch (Exception)
            {
                string settingsContents;
                try
                {
                    settingsContents = RobustFile.ReadAllText(SettingsFilePath);
                }
                catch (Exception error)
                {
                    settingsContents = error.Message;
                }
                Logger.WriteEvent("Contents of " + SettingsFilePath + ": /r/n" + settingsContents);

                // We used to notify the user of a problem here.
                // But now we decided it is better to catch at a higher level, at OpenProjectWindow(), else we have two different
                // error UI dialogs for the same problem. See BL-9916.
                throw;
            }

            try
            {
                string oldcustomCollectionStylesPath = FolderPath.CombineForPath("collection.css");
                if (RobustFile.Exists(oldcustomCollectionStylesPath))
                {
                    string newcustomCollectionStylesPath = FolderPath.CombineForPath("customCollectionStyles.css");

                    RobustFile.Move(oldcustomCollectionStylesPath, newcustomCollectionStylesPath);
                }
            }
            catch (Exception)
            {
                //ah well, we tried, no big deal, only a couple of beta testers used this old name
            }

            // Check if we need to do a one time check (perhaps migrate to a new Settings value)
            if (OneTimeCheckVersionNumber < kCurrentOneTimeCheckVersionNumber)
            {
                DoOneTimeCheck();
            }

            SetAnalyticsProperties();
        }
        /// ------------------------------------------------------------------------------------
        public void Load()
        {
            try
            {
                // Previously was SIL.IO.RobustIO.LoadXElement(SettingsFilePath). However, we had problems with this
                // using some non-roman collection names...specifically, one involving the Northern Pashti
                // localization of 'books' (┌й╪к╪з╪и┘И┘Ж┘З)...see BL-5416. It seems that somewhere in the
                // implementation of Linq.XElement.Load() the path is converted to a URL and then back
                // to a path and something changes in that process so that a valid path passed to Load()
                // raises an invalid path exception. Reading the file directly and then parsing the string
                // works around this problem.
                var settingsContent = RobustFile.ReadAllText(SettingsFilePath, Encoding.UTF8);
                var nameMigrations  = new[]
                {
                    new[] { "LanguageName", "Language1Name" },
                    new[] { "IsShellLibrary", "IsSourceCollection" },
                    new[] { "National1Iso639Code", "Language2Iso639Code" },
                    new[] { "National2Iso639Code", "Language3Iso639Code" },
                    new[] { "IsShellMakingProject", "IsSourceCollection" },
                    new[] { "Local Community", "Local-Community" }                   // migrate for 4.4
                };

                foreach (var fromTo in nameMigrations)
                {
                    settingsContent = settingsContent.Replace(fromTo[0], fromTo[1]);
                }

                var xml = XElement.Parse(settingsContent);

                Language1.ReadFromXml(xml, true, "en");
                Language2.ReadFromXml(xml, true, "self");
                Language3.ReadFromXml(xml, true, Language2.Iso639Code);

                SignLanguageIso639Code = ReadString(xml, "SignLanguageIso639Code",                  /* old name */
                                                    ReadString(xml, "SignLanguageIso639Code", ""));
                XMatterPackName = ReadString(xml, "XMatterPack", "Factory");

                var style = ReadString(xml, "PageNumberStyle", "Decimal");

                //for historical (and maybe future?) reasons, we collect the page number style as one of the
                //CSS counter number styles
                PageNumberStyle           = CssNumberStylesToCultureOrDigits.Keys.Contains(style) ? style : "Decimal";
                OneTimeCheckVersionNumber = ReadInteger(xml, "OneTimeCheckVersionNumber", 0);
                BrandingProjectKey        = ReadString(xml, "BrandingProjectName", "Default");
                SubscriptionCode          = ReadString(xml, "SubscriptionCode", null);

                if (BrandingProjectKey != "Default" && BrandingProjectKey != "Local-Community" && !Program.RunningHarvesterMode)
                {
                    // Validate branding, so things can't be circumvented by just typing something into settings
                    var expirationDate = CollectionSettingsApi.GetExpirationDate(SubscriptionCode);
                    if (expirationDate < DateTime.Now || BrandingProject.GetProjectChoices().All(bp => bp.Key != BrandingProjectKey))
                    {
                        InvalidBranding    = BrandingProjectKey;
                        BrandingProjectKey = "Default";                         // keep the code, but don't use it as active branding.
                    }
                }
                SignLanguageName   = ReadString(xml, "SignLanguageName", GetSignLanguageName_NoCache());
                Country            = ReadString(xml, "Country", "");
                Province           = ReadString(xml, "Province", "");
                District           = ReadString(xml, "District", "");
                AllowNewBooks      = ReadBoolean(xml, "AllowNewBooks", true);
                IsSourceCollection = ReadBoolean(xml, "IsSourceCollection", false);

                string audioRecordingModeStr = ReadString(xml, "AudioRecordingMode", "Unknown");
                TalkingBookApi.AudioRecordingMode parsedAudioRecordingMode;
                if (!Enum.TryParse(audioRecordingModeStr, out parsedAudioRecordingMode))
                {
                    parsedAudioRecordingMode = TalkingBookApi.AudioRecordingMode.Unknown;
                }
                AudioRecordingMode = parsedAudioRecordingMode;
                AudioRecordingTrimEndMilliseconds = ReadInteger(xml, "AudioRecordingTrimEndMilliseconds",
                                                                kDefaultAudioRecordingTrimEndMilliseconds);
            }
            catch (Exception e)
            {
                string settingsContents = "";
                try
                {
                    settingsContents = RobustFile.ReadAllText(SettingsFilePath);
                }
                catch (Exception error)
                {
                    settingsContents = error.Message;
                }
                Logger.WriteEvent("Contents of " + SettingsFilePath + ": /r/n" + settingsContents);
                SIL.Reporting.ErrorReport.NotifyUserOfProblem(e, "There was an error reading the file {0}.  Please report this error to the developers. To get access to your books, you should make a new collection, then copy your book folders from this broken collection into the new one, then run Bloom again.", SettingsFilePath);
                throw;
            }

            try
            {
                string oldcustomCollectionStylesPath = FolderPath.CombineForPath("collection.css");
                if (RobustFile.Exists(oldcustomCollectionStylesPath))
                {
                    string newcustomCollectionStylesPath = FolderPath.CombineForPath("customCollectionStyles.css");

                    RobustFile.Move(oldcustomCollectionStylesPath, newcustomCollectionStylesPath);
                }
            }
            catch (Exception)
            {
                //ah well, we tried, no big deal, only a couple of beta testers used this old name
            }

            // Check if we need to do a one time check (perhaps migrate to a new Settings value)
            if (OneTimeCheckVersionNumber < kCurrentOneTimeCheckVersionNumber)
            {
                DoOneTimeCheck();
            }

            SetAnalyticsProperties();
        }