From 8b36f335135b3d6ed2c17ec1615426429f14d5fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Tue, 24 Apr 2018 12:52:11 +0300 Subject: [PATCH] Ensure the progress bar is hidden if session initialisation fails --- chrome/content/zotero/xpcom/integration.js | 147 +++++++++++---------- 1 file changed, 76 insertions(+), 71 deletions(-) diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index cc6b1b6e1..744876608 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -388,86 +388,91 @@ Zotero.Integration = new function() { * @return {Zotero.Integration.Session} Promise */ this.getSession = async function (app, doc, agent) { - var progressBar = new Zotero.Integration.Progress(4, Zotero.isMac && agent != 'http'); - progressBar.show(); - - var dataString = await doc.getDocumentData(), - data, session; - try { - data = new Zotero.Integration.DocumentData(dataString); - } catch(e) { - data = new Zotero.Integration.DocumentData(); - } - - if (data.prefs.fieldType) { - if (data.dataVersion < DATA_VERSION) { - if (data.dataVersion == 1 - && data.prefs.fieldType == "Field" - && app.primaryFieldType == "ReferenceMark") { - // Converted OOo docs use ReferenceMarks, not fields - data.prefs.fieldType = "ReferenceMark"; - } - - var warning = await doc.displayAlert(Zotero.getString("integration.upgradeWarning", [Zotero.clientName, '5.0']), - DIALOG_ICON_WARNING, DIALOG_BUTTONS_OK_CANCEL); - if (!warning) { - throw new Zotero.Exception.UserCancelled("document upgrade"); - } - // Don't throw for version 4(JSON) during the transition from 4.0 to 5.0 - } else if ((data.dataVersion > DATA_VERSION) && data.dataVersion != 4) { - throw new Zotero.Exception.Alert("integration.error.newerDocumentVersion", - [data.zoteroVersion, Zotero.version], "integration.error.title"); + var progressBar = new Zotero.Integration.Progress(4, Zotero.isMac && agent != 'http'); + progressBar.show(); + + var dataString = await doc.getDocumentData(), + data, session; + + try { + data = new Zotero.Integration.DocumentData(dataString); + } catch(e) { + data = new Zotero.Integration.DocumentData(); } - if (data.prefs.fieldType !== app.primaryFieldType - && data.prefs.fieldType !== app.secondaryFieldType) { - throw new Zotero.Exception.Alert("integration.error.fieldTypeMismatch", - [], "integration.error.title"); + if (data.prefs.fieldType) { + if (data.dataVersion < DATA_VERSION) { + if (data.dataVersion == 1 + && data.prefs.fieldType == "Field" + && app.primaryFieldType == "ReferenceMark") { + // Converted OOo docs use ReferenceMarks, not fields + data.prefs.fieldType = "ReferenceMark"; + } + + var warning = await doc.displayAlert(Zotero.getString("integration.upgradeWarning", [Zotero.clientName, '5.0']), + DIALOG_ICON_WARNING, DIALOG_BUTTONS_OK_CANCEL); + if (!warning) { + throw new Zotero.Exception.UserCancelled("document upgrade"); + } + // Don't throw for version 4(JSON) during the transition from 4.0 to 5.0 + } else if ((data.dataVersion > DATA_VERSION) && data.dataVersion != 4) { + throw new Zotero.Exception.Alert("integration.error.newerDocumentVersion", + [data.zoteroVersion, Zotero.version], "integration.error.title"); + } + + if (data.prefs.fieldType !== app.primaryFieldType + && data.prefs.fieldType !== app.secondaryFieldType) { + throw new Zotero.Exception.Alert("integration.error.fieldTypeMismatch", + [], "integration.error.title"); + } + + session = Zotero.Integration.sessions[data.sessionID]; } + if (!session) { + session = new Zotero.Integration.Session(doc, app); + session.reload = true; + } + try { + await session.setData(data); + } catch(e) { + // make sure style is defined + if (e instanceof Zotero.Exception.Alert && e.name === "integration.error.invalidStyle") { + if (data.style.styleID) { + let trustedSource = /^https?:\/\/(www\.)?(zotero\.org|citationstyles\.org)/.test(data.style.styleID); + let errorString = Zotero.getString("integration.error.styleMissing", data.style.styleID); + if (trustedSource || + await doc.displayAlert(errorString, DIALOG_ICON_WARNING, DIALOG_BUTTONS_YES_NO)) { - session = Zotero.Integration.sessions[data.sessionID]; - } - if (!session) { - session = new Zotero.Integration.Session(doc, app); - session.reload = true; - } - try { - await session.setData(data); - } catch(e) { - // make sure style is defined - if (e instanceof Zotero.Exception.Alert && e.name === "integration.error.invalidStyle") { - if (data.style.styleID) { - let trustedSource = /^https?:\/\/(www\.)?(zotero\.org|citationstyles\.org)/.test(data.style.styleID); - let errorString = Zotero.getString("integration.error.styleMissing", data.style.styleID); - if (trustedSource || - await doc.displayAlert(errorString, DIALOG_ICON_WARNING, DIALOG_BUTTONS_YES_NO)) { - - let installed = false; - try { - await Zotero.Styles.install( - {url: data.style.styleID}, data.style.styleID, true - ); - installed = true; - } - catch (e) { - await doc.displayAlert( - Zotero.getString( - 'integration.error.styleNotFound', data.style.styleID - ), - DIALOG_ICON_WARNING, - DIALOG_BUTTONS_OK - ); - } - if (installed) { - await session.setData(data, true); + let installed = false; + try { + await Zotero.Styles.install( + {url: data.style.styleID}, data.style.styleID, true + ); + installed = true; + } + catch (e) { + await doc.displayAlert( + Zotero.getString( + 'integration.error.styleNotFound', data.style.styleID + ), + DIALOG_ICON_WARNING, + DIALOG_BUTTONS_OK + ); + } + if (installed) { + await session.setData(data, true); + } } } + await session.setDocPrefs(); + } else { + throw e; } - await session.setDocPrefs(); - } else { - throw e; } + } catch (e) { + progressBar.hide(true); + throw e; } session.agent = agent; session._doc = doc;