<?xml version="1.0" encoding="UTF-8"?>
<document>
<comment>
	Allows copying of books on the reader between internal memory and external card.
	Select the book in the menu, choose Utilities, then press Size (doSize).  
	Press Mark (doMark) to confirm the copying or Menu (doMenu) to cancel.  
</comment>
<hackblock>
	<identifier><![CDATA[
		// After modal copy enable
	]]></identifier>
	<replacement><![CDATA[
		this.COPY_BOOK_GROUP.COPY_BOOK.enable(true);
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		// After modal copy target
	]]></identifier>
	<replacement><![CDATA[
		case "COPY_BOOK": target = this.COPY_BOOK_GROUP.COPY_BOOK; break;
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		// Before modal copy disable
	]]></identifier>
	<replacement><![CDATA[
		this.COPY_BOOK_GROUP.COPY_BOOK.enable(false);
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		// Model changed copy hide
	]]></identifier>
	<replacement><![CDATA[
		this.COPY_BOOK_GROUP.show(false)
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		// Model changed copy case
	]]></identifier>
	<replacement><![CDATA[
		case "COPY_BOOK":
					this.COPY_BOOK_GROUP.show(true)
					target = this.COPY_BOOK_GROUP.COPY_BOOK
					break
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		<!-- copy doSize -->
	]]></identifier>
	<replacement><![CDATA[
		<function id="doSize"><![CDATA[
					if ((this.getModel().current != null) 
						&& (this.getModel().currentBook != null) 
						&& (this.getModel().current.kind == 19)) {
						this.current = this.getModel().currentBook.parent;
						this.getModel().STATE = "COPY_BOOK";
						kbook.tableData.setNode(null);
					}
					this.getModel().doBlink();
				]]></function>
	]]></replacement>
</hackblock>
<hackblock>
	<identifier><![CDATA[
		<!-- Copy group -->
	]]></identifier>
	<replacement><![CDATA[
		<group id="COPY_BOOK_GROUP" left="0" right="0" top="0" bottom="0" visible="false">
		<panel skin="kbookReset" left="0" right="0" top="0" bottom="0"/>
		<sprite skin="hard-reset" left="0" width="70" top="0" height="70" active="false"/>
		<kbookLabel skin="title" left="70" right="0" top="0" height="70" align="left" text="Copy Book"/>
		<group id="COPY_BOOK" width="480" height="240">
			<code>
				<function id="getCurrentBook"><![CDATA[
					var cbook = this.getModel().currentBook;
					if (!cbook)
						return null;
					var parent = cbook.parent;
					var id = parent.getIndex(cbook);
					var result = null;
					if(parent.master)
					{
						result = cbook.filter(parent.cache[parent.master]);
						var sort_title = new Object();
						sort_title.by = "indexTitle";
						sort_title.order = xdb.ascending;
						var sort_artist = new Object();
						sort_artist.by = "indexArtist";
						sort_artist.order = xdb.ascending;
						switch(parent.kind)	{
							case 1: // books
								result.sort(sort_title, sort_artist);
								break;
					
							case 6: // books by author
								result.sort(sort_artist, sort_title);
								break;
					
							case 22: // books by date
								var sort_date = new Object();
								sort_date.by = "indexDateTime";
								sort_date.order = -1;//xdb.descending;
								result.sort(sort_date, sort_title);
								break;
					
							default:
								result = null;
								break;
						}
						if (result)
							result = result.getRecord(id);
					}
					else if (parent.playlist) { // collection
						if ((parent.playlist.items[id]) && (parent.playlist.items[id].id))
							result = parent.cache.getRecord(parent.playlist.items[id].id);
					}
					return result;
				]]></function>
				<function id="doMark"><![CDATA[
					try {
						var model = kbook.model;
						var cbook = model.currentBook;
						var current_book = this.getCurrentBook();
						if (!current_book)
							return;
						var parent = cbook.parent;
						var path = current_book.getFilePath(parent.cache);
						this.MSG.setValue("path="+path);
						var lastI = path.lastIndexOf("/");
						this.MSG.setValue("lastI="+lastI);
						var isCard = path.indexOf(":");
						this.MSG.setValue("isCard="+isCard);
						var filename = path.substring(lastI+1);
						this.MSG.setValue("filename=" + filename);
						var dest = "";
						if(isCard != -1) { dest = "/Data/media/books/"+filename; }
						else { dest = "a:/" + filename; }
						this.MSG.setValue("dest="+dest);
						var source = current_book.source;
						this.MSG.setValue("Copying file from\n" + path + "\nto\n" + dest + "\n...");
						FskUI.Window.update.call(model.container.getWindow());
						try { model.onChangeBook(null); } catch(e) {}
						try { FileSystem.copyFile(path, dest); } 
						catch(e) 
						{ 
							if(isCard == -1) 
								try { 
										dest = "b:/" + filename; 
										this.MSG.setValue("Copying file from\n" + path + "\nto\n" + dest + "\n...");
										FileSystem.copyFile(path, dest); 
									} 
								catch(e) {} 
						}
						//parent.cache.deleteRecord(current_book.id);
						//if (parent.playlist) {
						//	var id = parent.getIndex(cbook);
						//	parent.playlist.removeAt[id];
						//}
						///this.MSG.setValue("Done.");
						if (source != null)
							try { source.synchronize(); } catch(e) {}
						///this.MSG.setValue("Done.");
						this.MSG.setValue("Press MARK to copy book\nor MENU to exit.");
						this.bubble("doMenu");
					} catch(e) {}
				]]></function>
			</code>
			<sprite skin="diskSprite" width="140" top="0" height="140" u="5" active="false"/>
			<text active="false" id="MSG" left="0" right="0" top="140" height="360" value="Press MARK to copy book&#13;or MENU to exit."/>
		</group>
		<kbookLabel left="180" right="180" height="30" bottom="0" align="center" text="1 of 1"/>
	</group>
	]]></replacement>
</hackblock>
</document>
