Skip to content

Commit 780ca23

Browse files
authored
Fix LT-20393: Importing a view can cause deletion of styles (#564)
This fixes https://jira.sil.org/browse/LT-20393. I had to add add m_validating to avoid errors in the unit tests that were caused by trying to load incomplete built-in styles, and I couldn't figure out how to make the built-in styles complete.
1 parent 314911e commit 780ca23

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

Src/xWorks/DictionaryConfigurationImportController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ internal void DoImport()
142142
if (_view == null) // _view is sometimes null in unit tests, and it's helpful to know what exactly went wrong.
143143
throw new Exception(xWorksStrings.kstidCannotImport, e);
144144
#endif
145+
MessageBox.Show(xWorksStrings.kstidCannotImport + " (" + e.Message + ")");
145146
_view.explanationLabel.Text = xWorksStrings.kstidCannotImport;
146147
}
147148

@@ -189,6 +190,11 @@ internal void DoImport()
189190

190191
private void ImportStyles(string importStylesLocation)
191192
{
193+
// Test for errors before deleting styles (LT-20393).
194+
NonUndoableUnitOfWorkHelper.DoSomehow(_cache.ActionHandlerAccessor, () =>
195+
{
196+
new FlexStylesXmlAccessor(_cache.LangProject.LexDbOA, true, importStylesLocation);
197+
});
192198
var stylesToRemove = _cache.LangProject.StylesOC.Where(style => !UnsupportedStyles.Contains(style.Name));
193199

194200
// For LT-18267, record basedon and next properties of styles not

Src/xWorks/FlexStylesXmlAccessor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ private void WriteStyleXml(ExportStyleInfo style, XmlWriter writer)
242242
writer.WriteAttributeString("userlevel", style.UserLevel.ToString());
243243
writer.WriteAttributeString("context", GetStyleContext(style));
244244
writer.WriteAttributeString("type", GetStyleType(style));
245+
writer.WriteAttributeString("structure", GetStyleStructure(style));
245246

246247
if (GetStyleType(style) == "character" && style.InheritsFrom != null)
247248
{
@@ -267,6 +268,18 @@ private static string GetStyleType(ExportStyleInfo style)
267268
return style.RealStyle.Type.ToString();
268269
}
269270

271+
private static string GetStyleStructure(ExportStyleInfo style)
272+
{
273+
switch (style.RealStyle.Structure)
274+
{
275+
case StructureValues.Heading:
276+
return "heading";
277+
case StructureValues.Body:
278+
return "body";
279+
}
280+
return style.RealStyle.Structure.ToString();
281+
}
282+
270283
///<remarks>The first letter for the context is supposed to be lower case</remarks>
271284
private static string GetStyleContext(ExportStyleInfo style)
272285
{

Src/xWorks/xWorksTests/DictionaryConfigurationImportControllerTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 SIL International
1+
// Copyright (c) 2017 SIL International
22
// This software is licensed under the LGPL, version 2.1 or later
33
// (http://www.gnu.org/licenses/lgpl-2.1.html)
44

@@ -129,17 +129,17 @@ public void Setup()
129129
{
130130
var styleFactory = Cache.ServiceLocator.GetInstance<IStStyleFactory>();
131131
styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Headword",
132-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, true, 2, true);
133-
var testStyle = styleFactory.Create(Cache.LangProject.StylesOC, "TestStyle", ContextValues.InternalConfigureView, StructureValues.Undefined,
134-
FunctionValues.Line, true, 2, false);
132+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, true, 2, true);
133+
var testStyle = styleFactory.Create(Cache.LangProject.StylesOC, "TestStyle", ContextValues.InternalConfigureView, StructureValues.Body,
134+
FunctionValues.Prose, true, 2, false);
135135
testStyle.Usage.set_String(Cache.DefaultAnalWs, "Test Style");
136-
var normalStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Normal", ContextValues.InternalConfigureView, StructureValues.Undefined,
137-
FunctionValues.Line, false, 2, true);
136+
var normalStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Normal", ContextValues.InternalConfigureView, StructureValues.Body,
137+
FunctionValues.Prose, false, 2, true);
138138
var propsBldr = TsStringUtils.MakePropsBldr();
139139
propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, 0x2BACCA); // arbitrary color to create para element
140140
normalStyle.Rules = propsBldr.GetTextProps();
141141
var senseStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Sense",
142-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, false, 2, true);
142+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, false, 2, true);
143143
propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, 0x2BACCA); // arbitrary color to create para element
144144
propsBldr.SetIntPropValues((int)FwTextPropType.ktptForeColor, (int)FwTextPropVar.ktpvDefault, NamedRedBGR);
145145
propsBldr.SetStrPropValue((int)FwTextPropType.ktptFontFamily, "Arial");
@@ -148,15 +148,15 @@ public void Setup()
148148
propsBldr.SetStrPropValue((int)FwTextPropType.ktptBulNumFontInfo, "");
149149
senseStyle.Rules = propsBldr.GetTextProps();
150150
senseStyle.BasedOnRA = normalStyle;
151-
var styleWithNamedColors = styleFactory.Create(Cache.LangProject.StylesOC, "Nominal", ContextValues.InternalConfigureView, StructureValues.Undefined,
152-
FunctionValues.Line, false, 2, false);
151+
var styleWithNamedColors = styleFactory.Create(Cache.LangProject.StylesOC, "Nominal", ContextValues.InternalConfigureView, StructureValues.Body,
152+
FunctionValues.Prose, false, 2, false);
153153
styleWithNamedColors.BasedOnRA = normalStyle;
154154
propsBldr = TsStringUtils.MakePropsBldr();
155155
propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, NamedRedBGR);
156156
propsBldr.SetIntPropValues((int)FwTextPropType.ktptForeColor, (int)FwTextPropVar.ktpvDefault, NamedRedBGR);
157157
styleWithNamedColors.Rules = propsBldr.GetTextProps();
158-
var styleWithCustomColors = styleFactory.Create(Cache.LangProject.StylesOC, "Abnormal", ContextValues.InternalConfigureView, StructureValues.Undefined,
159-
FunctionValues.Line, false, 2, false);
158+
var styleWithCustomColors = styleFactory.Create(Cache.LangProject.StylesOC, "Abnormal", ContextValues.InternalConfigureView, StructureValues.Body,
159+
FunctionValues.Prose, false, 2, false);
160160
styleWithCustomColors.BasedOnRA = normalStyle;
161161
propsBldr = TsStringUtils.MakePropsBldr();
162162
propsBldr.SetIntPropValues((int)FwTextPropType.ktptBackColor, (int)FwTextPropVar.ktpvDefault, CustomRedBGR);
@@ -303,20 +303,20 @@ public void DoImport_UnhandledStylesLeftUnTouched()
303303
// Set up state of flex before the import happens.
304304
var styleFactory = Cache.ServiceLocator.GetInstance<IStStyleFactory>();
305305
bulletStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Bulleted List",
306-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, false, 2, true);
306+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, false, 2, true);
307307
numberStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Numbered List",
308-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, false, 2, true);
308+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, false, 2, true);
309309

310-
dictionaryHeadwordStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Headword", ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Line, true, 2, true);
310+
dictionaryHeadwordStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Headword", ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, true, 2, true);
311311

312312
// Create a style that we can link to before the import happens. It's not
313313
// important what it's named, just that it also exists in the exported zip
314314
// file made by Setup().
315315
nominalStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Nominal",
316-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, false, 2, false);
316+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, false, 2, false);
317317

318318
homographStyle = styleFactory.Create(Cache.LangProject.StylesOC, "Homograph-Number",
319-
ContextValues.InternalConfigureView, StructureValues.Undefined, FunctionValues.Line, true, 2, true);
319+
ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, true, 2, true);
320320

321321
// Style linking to later examine
322322
homographStyle.BasedOnRA = dictionaryHeadwordStyle;
@@ -694,7 +694,7 @@ public void DoImport_CustomBulletInfoIsImported()
694694
{
695695
// Set up state of flex before the import happens.
696696
var styleFactory = Cache.ServiceLocator.GetInstance<IStStyleFactory>();
697-
styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Sense", ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Line, false, 2, true);
697+
styleFactory.Create(Cache.LangProject.StylesOC, "Dictionary-Sense", ContextValues.InternalConfigureView, StructureValues.Body, FunctionValues.Prose, false, 2, true);
698698
});
699699
Assert.AreEqual(1, Cache.LangProject.StylesOC.Count, "Setup problem. Unexpected number of styles before doing any import activity.");
700700
_controller.PrepareImport(_zipFile);

0 commit comments

Comments
 (0)