<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://zotero/skin/upgrade.css" type="text/css"?>

<!DOCTYPE window SYSTEM "chrome://zotero/locale/zotero.dtd">

<wizard id="zotero-schema-upgrade" title="&zotero.upgrade.title;"
		xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
	
	<script>
	<![CDATA[
		var Zotero_Schema_Upgrade = new function() {
			this.init = init;
			this.onAdvance = onAdvance;
			this.doUpgrade = doUpgrade;
			
			var obj = window.arguments[0].wrappedJSObject;
			var Zotero = obj.Zotero;
			var data = obj.data;
			
			function init() {
				var wizard = document.getElementById('zotero-schema-upgrade');
				
				var continueButton = wizard.getButton('next');
				continueButton.focus(); // prevent focusing upgrade instructions link
				
				// Use proper button name in text
				var continueButtonName = continueButton.getAttribute('label');
				var str = Zotero.getString('upgrade.advanceMessage', continueButtonName);
				document.getElementById('zotero-advance-message').setAttribute('value', str);
				
				if (data.majorUpgrade) {
					document.getElementById('zotero-major-upgrade').hidden = false;
					document.getElementById('zotero-upgrade-required').hidden = true;
				}
			}
			
			
			function onAdvance() {
				var wizard = document.getElementById('zotero-schema-upgrade');
				wizard.getButton('cancel').setAttribute('disabled', true);
				wizard.getButton('next').setAttribute('disabled', true);
				wizard.canRewind = false;
				wizard.getButton('finish').focus(); // prevent focusing of changelog link
				Zotero.wait();
			}
			
			
			function doUpgrade() {
				var wizard = document.getElementById('zotero-schema-upgrade');
				
				onAdvance();
				
				try {
					if (data.integrityCheck) {
						if (!Zotero.DB.integrityCheck()) {
							var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
													.getService(Components.interfaces.nsIPromptService);
							var buttonFlags = (ps.BUTTON_POS_0) * (ps.BUTTON_TITLE_IS_STRING)
												+ (ps.BUTTON_POS_1) * (ps.BUTTON_TITLE_CANCEL);
							var index = ps.confirmEx(
								null,
								Zotero.getString('general.error'),
								Zotero.getString('upgrade.integrityCheckFailed'),
								buttonFlags, Zotero.getString('upgrade.loadDBRepairTool'),
								null, null, null, {});
							
							if (index == 0) {
								Zotero.initialURL = 'https://www.zotero.org/utils/dbfix/';
							}
							
							_setStartupError();
							_clickCancel();
							return;
						}
					}
					
					Zotero.Schema.updateSchema();
				}
				catch (e) {
					data.msg = Zotero.getString('upgrade.failed');
					data.e = e;
					
					Components.utils.reportError(e);
					
					_clickCancel();
					return;
				}
				
				data.success = true;
				
				wizard.advance();
			}
			
			
			this.onUpgradeInstructionsLinkClick = function () {
				_setStartupError();
				Zotero.initialURL = 'http://zotero.org/support/upgrade';
				_clickCancel();
			}
			
			
			this.onChangeLogLinkClick = function () {
				Zotero.initialURL = 'http://zotero.org/support/changelog';
				document.getElementById('zotero-schema-upgrade').getButton('finish').click();
			}
			
			
			function _setStartupError() {
				Zotero.startupError = [
					Zotero.getString('upgrade.dbUpdateRequired'), Zotero.getString('general.restartFirefox')
				].join(' ');
			}
			
			
			function _clickCancel() {
				var cancelButton = document.getElementById('zotero-schema-upgrade').getButton('cancel');
				cancelButton.setAttribute('disabled', false);
				cancelButton.click();
			}
		}
	]]>
	</script>
	
	<wizardpage onpageshow="Zotero_Schema_Upgrade.init()" label=" ">
		<description>&zotero.upgrade.newVersionInstalled;</description>
		<description id="zotero-upgrade-required">&zotero.upgrade.upgradeRequired; &zotero.upgrade.autoBackup;</description>
		<description id="zotero-major-upgrade" hidden="true">
			<label value="&zotero.upgrade.majorUpgrade;" style="font-weight: bold"/>
			&zotero.upgrade.majorUpgradeBeforeLink;
			<label class="text-link" value="&zotero.upgrade.majorUpgradeLink;"
				onclick="try { Zotero_Schema_Upgrade.onUpgradeInstructionsLinkClick(); } catch (e) { alert(e); }"/>
			&zotero.upgrade.majorUpgradeAfterLink;
		</description>
		<description id="zotero-advance-message"/>
	</wizardpage>
	
	<wizardpage onpageshow="Zotero_Schema_Upgrade.doUpgrade()" label=" ">
		<description>&zotero.upgrade.upgradeInProgress;</description>
		<progressmeter mode="undetermined"/>
	</wizardpage>
	
	<wizardpage onpageshow="Zotero_Schema_Upgrade.onAdvance()" label=" ">
		<description>&zotero.upgrade.upgradeSucceeded;</description>
		<description>
			&zotero.upgrade.changeLogBeforeLink;
			<label class="text-link" value="&zotero.upgrade.changeLogLink;"
				onclick="Zotero_Schema_Upgrade.onChangeLogLinkClick()"/>
			&zotero.upgrade.changeLogAfterLink;
		</description>
	</wizardpage>
</wizard>
