
var IMG_BG=IMG_PATH+"filer_bg.png";
var IMG_TRIANGLE=IMG_PATH+"filer_triangle.png";
var UPPER_FOLDER=IMG_PATH+"filer_upper_folder.png";
var NORMAL_FOLDER=IMG_PATH+"filer_folder.png";
var IMG_SDCARD=IMG_PATH+"filer_sd.png";
var IMG_SDCARD_MARK=IMG_PATH+"filer_mark_sd.png";
var IMG_USB=IMG_PATH+"filer_usb.png";
var IMG_USB_MARK=IMG_PATH+"filer_mark_usb.png";
var IMG_RETURN=UPPER_FOLDER;
var PREFIX_SD="file:///mnt";
var PREFIX_USB="file:///tmp/mnt";
var MAX_X=7;
var MAX_Y=4;
function thumbnail(){
this.superClass=container;
this.superClass({});
var me=this;
this.image=new gimage({width:180,height:135,jpeg_app1_thumbnail:true,
onerror:function(obj,status,err){
if(!obj.err){
obj.err=true;
obj.src=IMG_PATH+"filer_prepare.png";}
if(me.callback)me.callback();},
onload:function(){
if(me.callback)me.callback();}});
this.name=new gtext({translate:[0,-94,0],color:[0,0,0,255],
width:180,height:48,
align:CENTER,omitter:"...",font_size:28});
this.components.push(this.image,this.name);
this.callback=NULL_FUNCTION;};
thumbnail.prototype=new container({});
thumbnail.prototype.set_callback=function(func){
this.callback=func;};
thumbnail.prototype.set_image=function(img){
try{
if(this.image.src==img)return;
this.image.err=false;
if(isString(img)){
this.image.src=img;}else{
var len=img.length;
if(len>500*1024)len=500*1024;
img.open("read","binary");
var bin=img.read(len);
img.close();
this.image.src=bin;
bin.length=0;}
}catch(e){
console.log(e.message);}
};
thumbnail.prototype.set_name=function(name){
if(this.name.text!=name)
setf_text(this.name,name);};
thumbnail.prototype.freeResource=function(bool){
this.gimageResource(this.image,bool);
this.gtextResource(this.name,bool);};
thumbnail.prototype.gimageResource=function(target,bool){
if(bool){
target.org_src=target.src;
target.src="";}else{
if(target.org_src){
target.src=target.org_src;
delete target.org_src;}
}
};
thumbnail.prototype.gtextResource=function(target,bool){
if(bool){
target.org_text=target.text;
setf_text(target,"");}else{
if(isString(target.org_text)){
setf_text(target,target.org_text);
delete target.org_text;}
}
};
function gui(){
this.superClass=container;
this.superClass({});
this.bg=new gimage({width:1634,height:950,
translate:[0,-20,0],
draw_type:DIRECT,src:IMG_BG});
this.shadow=new gbox({translate:[6,-26,0],color:[0,0,0,128],
width:1634,height:950});
this.title=new gtext({translate:[-660,415,0],color:[0,0,0,255],
width:260,font_size:44});
this.device_mark=new gimage({translate:[-480,415,0],width:40,height:40});
this.device_name=new gtext({translate:[-400,415,0],color:[0,0,0,255],
width:120,font_size:38});
this.path_name=new gtext({translate:[120,415,0],color:[32,64,255,255],
width:900,omitter:"...",});
this.desc=new gtext({translate:[0,-468,0],width:1600,
font_size:28,align:CENTER});
this.images=new gimage({translate:[680,455,0],width:200,height:140});
this.list_gui=new container({});
this.loading=new gimage({visible_p:false,color:[0,0,0,255],rotation:[0,0,0,1]});
this.components.push(this.shadow,this.bg);
this.components.push(this.title,this.device_mark,this.device_name,this.path_name,this.images);
this.components.push(this.desc,this.list_gui);
this.components.push(this.loading);
this.thumb_list=[];
for(var i=0;i<MAX_Y;i++){
for(var j=0;j<MAX_X;j++){
var thumb=new thumbnail();
thumb.translate=[j*218-670,-i*198+283,0];
this.thumb_list.push(thumb);
this.components.push(thumb);}
}
this.bar_fg=new gbox({translate:[790,130,0],color:[102,102,102,255],
width:5,height:400});
this.bar_up=new gimage({translate:[790,346,0],
src:IMG_TRIANGLE});
this.bar_down=new gimage({translate:[790,-426,0],flip:FLIP_VERTICAL,
rotation:[-120,0,0,1],src:IMG_TRIANGLE});

this.components.push(this.bar_fg,this.bar_up,this.bar_down);};
gui.prototype=new container({});
gui.prototype.init=function(focus_func,action_func){
var ret=this.get_devices();
this.setDisk();
this.setPath();
this.requestFunc=focus_func;
this.action_func=action_func;
this.update_devices(ret,0);}
gui.prototype.freeResource=function(bool){
this.gimageResource(this.bg,bool);
this.gtextResource(this.title,bool);
this.gimageResource(this.device_mark,bool);
this.gtextResource(this.device_name,bool);
this.gtextResource(this.path_name,bool);
this.gtextResource(this.desc,bool);
this.gimageResource(this.images,bool);
if(this.thumb_list){
for(var i=0;i<this.thumb_list.length;i++)
this.thumb_list[i].freeResource(bool);}
this.gimageResource(this.bar_up,bool);
this.gimageResource(this.bar_down,bool);};
gui.prototype.setFilenameExtension=function (ext_array){
if(!ext_array)this.ext_list=null;
for(var i=0;i<ext_array.length;i++)
ext_array[i]=ext_array[i].toUpperCase();
this.ext_list=ext_array;};
gui.prototype.setTitle=function (title){
if(!isString(title))title="";
if(this.title.text!=title){
setf_text(this.title,title);
change_font_size(this.title);}
};
gui.prototype.setDescription=function (desc){
if(!isString(desc))desc="";
if(this.desc.text!=desc)
setf_text(this.desc,desc);};
gui.prototype.setImage=function (image){
if(!image)image="";
if(this.images.src!=image)
this.images.src=image;};
gui.prototype.getFileObj=function(){
return this.file_list[this.index];};
gui.prototype.getFileName=function(){
if(!this.file_list)return "";
return this.file_list[this.index].name;};
gui.prototype.getFilePath=function(){
if(!this.file_list)return "";
return this.file_list[this.index].path;};
gui.prototype.getDisk=function(){
return this.disk;};
gui.prototype.getInternalPath=function(){
if(!this.file_list||!this.disk)return "";
var prefix="";
switch(this.disk.type){
case this.disk.TYPE_SD:
prefix=PREFIX_SD;
break;
case this.disk.TYPE_USB:
prefix=PREFIX_USB;}
return prefix+this.file_list[this.index].path;};
gui.prototype.setDisk=function (disk){
this.disk=disk;
if(!this.disk)
return;

switch(this.disk.type){
case this.disk.TYPE_SD:
this.device_mark.src=IMG_SDCARD_MARK;
setf_text(this.device_name,"SD");
break;
case this.disk.TYPE_USB:
this.device_mark.src=IMG_USB_MARK;
setf_text(this.device_name,"USB");
break;}
}
gui.prototype.setPath=function (path){
if(!path){
this.set_files(path);
return;}
if(path[path.length-1]=="/")
path=path.slice(0,path.length-1);
var local_path=null;
if(!this.disk.is_rec){
local_path=path.match("/[^/]+(/.+)");}else {
local_path=path.match("/[^/]+/[^/]+(/.+)");}
if(!local_path||local_path.length<=1){
setf_text(this.path_name,"/");}else{
setf_text(this.path_name,local_path[1].toString());}
this.set_files(path);
this.update_files(0);};
gui.prototype.setPathObj=function (fobj){
if(!fobj){
this.set_files();
return;}
var path="";
try{
path=fobj.path;
if(path[path.length-1]=="/")
path=path.slice(0,path.length-1);}catch(e){
path="xxx";}
var local_path=null;
if(!this.disk.is_rec){
local_path=path.match("/[^/]+(/.+)");}else {
local_path=path.match("/[^/]+/[^/]+(/.+)");}
if(!local_path||local_path.length<=1){
setf_text(this.path_name,"/");}else{
setf_text(this.path_name,local_path[1].toString());}
var me=this;
this.set_file_obj(fobj,function(){me.update_files(0);});
};
gui.prototype.gimageResource=function(target,bool){
if(bool){
target.org_src=target.src;
target.src="";}else{
if(target.org_src){
target.src=target.org_src;
delete target.org_src;}
}
};
gui.prototype.gtextResource=function(target,bool){
if(bool){
target.org_text=target.text;
setf_text(target,"");}else{
if(isString(target.org_text)){
setf_text(target,target.org_text);
delete target.org_text;}
}
};
gui.prototype.reportError=function (errcode){
var id=this.components.indexOf(this.confirm);
if(id!=-1)this.components.splice(id,1);
var me=this;
var action=function(){
if(me.action_func)me.action_func(null,false);};
var focus=function(translate,size,angle,speed,func){
if(!me.requestFunc)return;
translate[2]+=10;
me.requestFunc(translate,size,angle,speed,func);};
this.confirm=new container({translate:[0,0,10]});
this.confirm.mask=new gbox({width:1920+10,height:1080+10,color:[0,0,0,128]});
this.confirm.panel=new Confirmpanel(0,[action],focus);
this.confirm.key_hook=function(up_down,key){
return this.panel.key_hook(up_down,key);}
this.confirm.components.push(this.confirm.mask,this.confirm.panel);
var msg="";
switch(errcode){
case "LT_CL_READ_ERROR":
msg=LANG("LT_CL_READ_ERROR");
break;
default:
msg=LANG("LT_NO_DEVICE_INSERTED");
break;}
var panel=this.confirm.panel;
panel.setTitle("");
panel.setMessageText(msg);
panel.setButtonLabel(LANG("LT_PM_RETURN"));
panel.setTranslate(0,0,0);
panel.setSize(800,420);
panel.setShow(true);
panel.onFocus(true);
this.components.push(this.confirm);
this.cursor=this.confirm;};
gui.prototype.get_devices=function(){
var devices=[];
var sd_disk=ebus.storage.get_sd_disk();
var usb_disk=ebus.storage.get_usb_disk();
devices=devices.concat(sd_disk);
devices=devices.concat(usb_disk);
var verified_devices=[];
for(var i=0;i<devices.length;i++){
try{
if(new File(devices[i].path).exists)
verified_devices.push(devices[i]);}catch(e){
continue;}
}

return verified_devices;};
gui.prototype.set_files=function(path){
this.file_list=[];
if(!path)
return;
var tmp=path.match("/","g");
if(!tmp)
return;
if(tmp.length<2)
path=path+"/";
try{
var fobj=new File(path);
if(!fobj.exists||fobj.isFile){
this.reportError("cannot access");
console.log("Not exist directory.");
return;}
}catch(e){
this.reportError("cannot access");
return;}
var me=this;
this.set_file_obj(fobj,function(){me.update_files(0);});};
gui.prototype.is_loading=function(){
return this.loading.visible_p;};
gui.prototype.loading_on=function(){
this.loading.visible_p=true;
if(!this.loading.src)
this.loading.src=IMG_PATH+"loading_circle.png";
append_timer(this,100,function(obj){
delete_timer(obj);
obj.loading.rotation[0]-=30;
force_redraw();
obj.loading_on();});};
gui.prototype.loading_off=function(){
this.loading.visible_p=false;
this.loading.src="";
delete_timer(this);
force_redraw();};
gui.prototype.async={};
gui.prototype.set_file_obj=function(fobj,callback){
this.loading_on();
this.file_list=[];
if(!fobj){
this.loading_off();
return;}
var flist=fobj.list();
var dirs=[fobj];
var files=[];
var len=flist.length;
var me=this;
var index=0;
var particle=Math.floor(len/20+1);
if(particle<30)particle=30;
append_timer(this.async,200,function(obj,cnt){
if(cnt%5==0)return;
for(var j=0;j<particle;j++){
index++;
var i=index-1;
if(index>len){
delete_timer(obj);
me.file_list=dirs.concat(files);
me.loading_off();
if(callback)callback();
return;}

if(!flist[i])
continue;
try{
if(!isString(flist[i].name)||flist[i].name.length<=0)
continue;}catch(e){
console.log(e.message);
}
if(!flist[i].exists)
continue;

if(flist[i].isDirectory){
dirs.push(flist[i]);
continue;}
if(!flist[i].isFile)
continue;
if(!me.ext_list){
files.push(flist[i]);
continue;}
var ext="";
try{
ext=flist[i].name.match("\\.([^\.]+$)");}catch(e){
ext=["","jpg"];};
if(ext&&ext.length==2&&me.ext_list.indexOf(ext[1].toUpperCase())!=-1)
files.push(flist[i]);};});};
gui.prototype.redraw_timer={};
gui.prototype.update_devices=function(devices,index){
if(!index)index=0;
if(!devices)return;
var offset=0;
setf_text(this.path_name,LANG("LT_DEVICE_SELECT"));
setf_text(this.device_name,"");
this.device_mark.src="";

this.devices=["go back"].concat(devices);
for(var i=0;i<MAX_X*MAX_Y;i++){
this.thumb_list[i].set_name("");
this.thumb_list[i].set_image("");
if(offset+i==0){
this.thumb_list[i].visible_p=true;
this.thumb_list[i].set_name(LANG("LT_RETURN"));
this.thumb_list[i].set_image(IMG_RETURN);
continue;};
if(this.devices.length<=offset+i){
this.thumb_list[i].visible_p=false;
this.thumb_list[i].set_name("");
this.thumb_list[i].set_image("");
continue;}
var device=this.devices[offset+i];
switch(device.type){
case device.TYPE_SD:
this.thumb_list[i].visible_p=true;
this.thumb_list[i].set_name("SD");
this.thumb_list[i].set_image(IMG_SDCARD);
break;
case device.TYPE_USB:
this.thumb_list[i].visible_p=true;
this.thumb_list[i].set_name(device.product);
this.thumb_list[i].set_image(IMG_USB);
break;
default:
break;}
};
var bool=false;
this.bar_fg.visible_p=bool;
this.bar_up.visible_p=bool;
this.bar_down.visible_p=bool;

this.index=index;
this.cursor=this.thumb_list[index%(MAX_X*MAX_Y)];
if(this.requestFunc)
this.requestFunc(this.cursor.translate,[180+8,135+8],0,128,null);
append_timer(this.redraw_timer,1000,function(obj,cnt){
if(cnt>30)delete_timer(obj);
force_redraw();});};
gui.prototype.update_image_list=[];
gui.prototype.push_thumb=function(func){
this.update_image_list.push(func);};
gui.prototype.next_thumb=function(){
var func=this.update_image_list.shift();
if(func)func();};
gui.prototype.clear_thumb=function(){
this.update_image_list=[];};
gui.prototype.update_files=function(index){
if(!index)index=0;
var me=this;
var status=this.disk.get_mount_status();
if(status!=this.disk.STATE_MOUNT){
this.reportError("cannot access");
return false;}
var offset=Math.floor(index/(MAX_X*MAX_Y))*(MAX_X*MAX_Y);
var file_list=this.file_list;
if(index>=file_list.length)index=file_list.length-1;
this.clear_thumb();
for(var i=0;i<MAX_X*MAX_Y;i++)
{
this.thumb_list[i].set_callback(null);
this.thumb_list[i].set_name("");
this.thumb_list[i].set_image("");}
for(var i=0;i<MAX_X*MAX_Y;i++)
{
var file=file_list[offset+i];
if(offset+i==0){
this.thumb_list[i].set_name(LANG("LT_RETURN"));
this.thumb_list[i].set_image(UPPER_FOLDER);
this.thumb_list[i].visible_p=true;
continue;}
if(file_list.length<=offset+i){
this.thumb_list[i].visible_p=false;
this.thumb_list[i].set_name("");
this.thumb_list[i].set_image("");
continue;}
if(!file.exists)
continue;
if(file.isDirectory){
var name="";
try{
name=file_list[offset+i].name;}catch(e){
name="";};
this.thumb_list[i].set_name(name);
this.thumb_list[i].set_callback(function(){me.next_thumb();});
this.thumb_list[i].set_image(NORMAL_FOLDER);
this.thumb_list[i].visible_p=true;
continue;}
if(file.isFile){
var name;
try{
name=file.name;}catch(e){
name="";}
this.thumb_list[i].set_name(name);
this.thumb_list[i].set_callback(function(){me.next_thumb();});
if(i>2){
(function(){
var thumb=me.thumb_list[i];
var tmp_file=file;
me.push_thumb(function(){thumb.set_image(tmp_file);});})();}else{
this.thumb_list[i].set_image(file);}
this.thumb_list[i].visible_p=true;
continue;}
}
var bool=true;
var page=Math.ceil(file_list.length/(MAX_X*MAX_Y));
if(page==1){
bool=false;}else{
this.pindex=Math.floor(offset/(MAX_X*MAX_Y));
this.bar_fg.height=Math.floor(740/page);
this.bar_fg.translate[1]=330-this.bar_fg.height/2-this.bar_fg.height*this.pindex;}
this.bar_fg.visible_p=bool;
this.bar_up.visible_p=bool;
this.bar_down.visible_p=bool;
this.index=index;
this.cursor=this.thumb_list[index%(MAX_X*MAX_Y)];
if(!this.cursor){
this.index=0;
this.cursor=this.thumb_list[0];}
if(this.requestFunc)
this.requestFunc(this.cursor.translate,[180+8,135+8],0,49,null);
append_timer(this.redraw_timer,1000,function(obj,cnt){
if(cnt>30)delete_timer(obj);
force_redraw();});
return true;};
gui.prototype.roll_up=function (){
this.index-=MAX_X;
var ret=this.update_files(this.index);;if(!ret)this.index+=MAX_X;};
gui.prototype.roll_down=function (){
this.index+=MAX_X;
var ret=this.update_files(this.index);if(!ret)this.index-=MAX_X;};
gui.prototype.device_key_hook=function (up_down,keysym){
var old_index=this.index;
switch(keysym){
case TXK_ENTER:
if(this.index==0){
if(this.action_func)
return this.action_func(null,true);
return false;}
var device=this.devices[this.index];
device.update();
var status=device.get_mount_status();
if(status!=device.STATE_MOUNT){
this.reportError("cannot access");
return true;}
if(device.fs_type==device.FS_TYPE_NTFS||
device.fs_type==device.FS_TYPE_UNKNOWN||
device.fs_type==device.FS_TYPE_INVALID){
for(var name in device)
if(name.indexOf("FS_TYPE_")!=-1&&device.fs_type==device[name])
console.log("ftype: "+name);
this.reportError("LT_CL_READ_ERROR");
return true;}
this.setDisk(this.devices[this.index]);
this.setPath(this.devices[this.index].path);
return true;
case TXK_RETURN:
if(this.action_func)
return this.action_func(null,false);
return false;
case TXK_UP:
if(this.is_loading())return true;
if(this.index-MAX_X<0)return false
this.index-=MAX_X;
break;
case TXK_DOWN:
if(this.is_loading())return true;
if(this.index+MAX_X>=this.devices.length)return false
if(this.index+MAX_X>=MAX_X*MAX_Y)return false
this.index+=MAX_X;
break;
case TXK_LEFT:
if(this.is_loading())return true;
if(this.index%MAX_X<=0||this.index-1<0)return false
this.index--;
break;
case TXK_RIGHT:
if(this.is_loading())return true;
if(this.index%MAX_X>=MAX_X-1)return false;
if(this.index>=this.devices.length-1)return false
this.index++;
break;
default:
break;}
if(old_index!=this.index){
this.cursor=this.thumb_list[this.index%(MAX_X*MAX_Y)];
if(this.requestFunc)
this.requestFunc(this.cursor.translate,[180+8,135+8],0,49,null);
return true;}
return false;}
gui.prototype.files_key_hook=function (up_down,keysym){
var old_index=this.index;
switch(keysym){
case TXK_ENTER:
if(this.index==0){
var parent_path=this.file_list[0].parent;
var isRoot=false;
try{
if(!parent_path||parent_path.path=="/"||
this.disk.is_rec&&parent_path.parent&&parent_path.parent.path=="/")
isRoot=true;}catch(e){
isRoot=false;}
if(isRoot){
this.setDisk();
this.setPath();
var devices=this.get_devices();
this.update_devices(devices,0);
return true;}
this.setPathObj(parent_path);
return true;}
var file=this.file_list[this.index];
if(!file||!file.exists){
this.reportError("LT_CL_READ_ERROR");
return true;}
if(file.isFile){
if(file.length<=0||!file.canRead){
this.reportError("LT_CL_READ_ERROR");
return true;}
if(this.action_func){
return this.action_func(this.file_list[this.index],true);}
return false;}
if(this.file_list[this.index].isDirectory){
this.setPathObj(this.file_list[this.index]);
return true;}
break;
case TXK_RETURN:
var parent_path=this.file_list[0].parent;
var isRoot=false;
try{
if(!parent_path||parent_path.path=="/"||
this.disk.is_rec&&parent_path.parent&&parent_path.parent.path=="/")
isRoot=true;}catch(e){
console.log(e.message);
isRoot=false;}
if(isRoot){
this.setDisk();
this.setPath();
var devices=this.get_devices();
this.update_devices(devices,0);
return true;}
this.setPathObj(parent_path);
return true;
case TXK_UP:
if(this.index-MAX_X<0)return false
if((this.index%(MAX_X*MAX_Y)-MAX_X)<0){
this.roll_up();
break;}
this.index-=MAX_X;
break;
case TXK_DOWN:
if((this.index%(MAX_X*MAX_Y)+MAX_X)>=(MAX_X*MAX_Y)){
if(Math.floor((this.index+MAX_X)/(MAX_X*MAX_Y))
>Math.floor((this.file_list.length-1)/(MAX_X*MAX_Y)))
return false
this.roll_down();
break;}
if(this.index+MAX_X>=this.file_list.length)return false
this.index+=MAX_X;
break;
case TXK_LEFT:
if(this.index%MAX_X<=0||this.index-1<0)return false
this.index--;
break;
case TXK_RIGHT:
if(this.index%MAX_X>=MAX_X-1
||this.index>=this.file_list.length-1)
return false
this.index++;
break;
default:
break;}
if(old_index!=this.index){
this.cursor=this.thumb_list[this.index%(MAX_X*MAX_Y)];
if(this.requestFunc)
this.requestFunc(this.cursor.translate,[180+8,135+8],0,49,null);
return true;}
return false;}
gui.prototype.key_hook=function (up_down,keysym){
if(up_down!=KEY_PRESS)
return false;
if(this.cursor==this.confirm&&this.cursor.key_hook){
this.cursor.key_hook(up_down,keysym);
return true;}
var ret;
if(this.file_list&&this.file_list.length>0){
if(this.files_key_hook(up_down,keysym))
return true;}else if(this.devices){
if(this.device_key_hook(up_down,keysym))
return true;}
return false;};
