<?xml version="1.0"?>
<!--
    ***** BEGIN LICENSE BLOCK *****
    
    Copyright © 2012 Center for History and New Media
                     George Mason University, Fairfax, Virginia, USA
                     http://zotero.org
    
    This file is part of Zotero.
    
    Zotero is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Zotero is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
-->
<?xml-stylesheet href="chrome://zotero/skin/overlay.css" type="text/css"?>
<!DOCTYPE bindings SYSTEM "chrome://zotero/locale/zotero.dtd">

<bindings xmlns="http://www.mozilla.org/xbl">
	<binding id="file-sync-status">
		<implementation>
			<property name="data"
				onget="return this._data;"
				onset="this._data = val; this.refresh();">
			</property>
			
			<field name="_libraries">[]</field>
			
			<method name="refresh">
				<body>
				<![CDATA[
					var rows = this._id('rows');
					
					// Get libraries with active downloads or uploads
					var newLibraries = [];
					for (var libraryID in this._data) {
						if ((this._data[libraryID].download
								&& !this._data[libraryID].download.finished)
								||
								(this._data[libraryID].upload
								&& !this._data[libraryID].upload.finished)) {
							newLibraries.push(parseInt(libraryID));
						}
					}
					
					// If set of libraries is different, clear and recreate
					var toRemove = Zotero.Utilities.arrayDiff(this._libraries, newLibraries);
					var toAdd = Zotero.Utilities.arrayDiff(newLibraries, this._libraries);
					if (toRemove.length || toAdd.length) {
						while (rows.hasChildNodes()) {
							rows.removeChild(rows.firstChild);
						}
					}
					
					this._libraries = newLibraries;
					
					// Update
					if (rows.hasChildNodes()) {
						for (var libraryID in this._data) {
							var libraryStatus = this._data[libraryID];
							
							// Library is finished
							if (newLibraries.indexOf(parseInt(libraryID)) == -1) {
								continue;
							}
							
							var libraryNameRow = this._id('library-name-row-' + libraryID);
							var downloadsRow = this._id('downloads-row-' + libraryID);
							var uploadsRow = this._id('uploads-row-' + libraryID);
							
							downloadsRow.lastChild.setAttribute('value',
								libraryStatus.download
									? libraryStatus.download.statusString
									: Zotero.getString('sync.storage.none'));
							uploadsRow.lastChild.setAttribute('value',
								libraryStatus.upload
									? libraryStatus.upload.statusString
									: Zotero.getString('sync.storage.none'));
						}
					}
					// Build from scratch
					else {
						// Get ordered list of library names
						var libraryNames = [];
						for (let libraryID of newLibraries) {
							libraryNames.push({
								libraryID: libraryID,
								name: Zotero.Libraries.getName(libraryID)
							});
						}
						var collation = Zotero.getLocaleCollation();
						let userLibraryID = Zotero.Libraries.userLibraryID;
						libraryNames.sort(function (a, b) {
							if (a.libraryID == userLibraryID) {
								return -1;
							}
							if (b.libraryID == userLibraryID) {
								return 1;
							}
							return collation.compareString(1, a.name, b.name);
						});
						
						for (var i in libraryNames) {
							var libraryID = libraryNames[i].libraryID;
							var libraryStatus = this._data[libraryID];
							
							var label = document.createElement('label');
							label.id = 'library-name-row-' + libraryID;
							label.setAttribute('value', libraryNames[i].name);
							rows.appendChild(label);
							
							var row = this._createRow('download',
								libraryStatus.download
									? libraryStatus.download.statusString
									: false);
							row.id = 'downloads-row-' + libraryID;
							rows.appendChild(row);
							
							var row = this._createRow('upload',
								libraryStatus.upload
									? libraryStatus.upload.statusString
									: false);
							row.id = 'uploads-row-' + libraryID;
							rows.appendChild(row);
						}
					}
				]]>
				</body>
			</method>
			
			<method name="_createRow">
				<parameter name="type"/>
				<parameter name="value"/>
				<body>
				<![CDATA[
					var row = document.createElement('row');
					
					var label = document.createElement('label');
					label.setAttribute('value', Zotero.getString('sync.storage.' + type + 's'));
					row.appendChild(label);
					
					label = document.createElement('label');
					label.setAttribute('value',
						value ? value : Zotero.getString('sync.storage.none'));
					row.appendChild(label);
					
					return row;
				]]>
				</body>
			</method>
			
			<method name="_id">
				<parameter name="id"/>
				<body>
				<![CDATA[
					return document.getAnonymousNodes(this)[0].getElementsByAttribute('id', id)[0];
				]]>
				</body>
			</method>
		</implementation>
		
		<content>
			<grid xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1">
				<columns>
					<column/>
					<column/>
				</columns>
				<rows id="rows"/>
			</grid>
		</content>
		
	</binding>
</bindings>
