<?php

class Desktop{

	private  $file;
	
	public function __construct(){
	 
		$this->file = new File();
		
	}
	
	// -----------------------------------------------------
	// REMOVE SHORTCUT 
	// -----------------------------------------------------
	
	public function removeShortcut($d){
	 
		//$d->name; 
		$retObj = (object) array();

		$fileObj = $this->file->checkPathAndGetPathElements($d->path);
		
		// delete shortcutfile
		$retObj->success = $this->file->deleteFile($fileObj->realPath);
		
		$this->updateDesktopIconPosition($d, false, true);
		
		Response::sendOKResponse($retObj);
	}

	// -----------------------------------------------------
	// CREATE SHORTCUT 
	// -----------------------------------------------------
	
	public function createShortcut($d){
	 
		//$d->name; $d->path; $d->type; $d->icon48; $d->x; $d->y, $d->forceOwervrite;
		
		$retObj = (object) array();
	
		$fileObj = $this->file->checkPathAndGetPathElements($d->path);
		
		if(is_file($fileObj->realPath) && $d->forceOverwrite!=true){
			$retObj->success = false;
			$retObj->exist = true;
			Response::sendOKResponse($retObj);
			exit;
		}
		
		// write shortcut file
		$retObj->success = Util::writeFile($fileObj->realPath, DataSerializer::serialize($d->scObject));
		
		// update position info file
		$this->updateDesktopIconPosition($d->scInfoObject, false);
		
	
		$retObj->fileItem = $this->file->getFileInfo($fileObj->realPath, "m/d/Y H:i");
		$scXmlSrt = Util::readFile($fileObj->realPath);//."/".$FaF[$i]->name);
	 	if($scXmlSrt!==false)
	 		$retObj->fileItem->shortCut = DataSerializer::deserialize($scXmlSrt);
		
		Response::sendOKResponse($retObj);
	}
	
	// -----------------------------------------------------
	// GET SHORTCUT 
	// -----------------------------------------------------
	
	public function getShortCut($d){
	
		$fileObj = $this->file->checkPathAndGetPathElements($d->path);
		Response::sendOKResponse(DataSerializer::deserialize(Util::readFile($fileObj->realPath)));
		
	}
	
	// -----------------------------------------------------
	// UPDATE POSITION OF AN ICON
	// -----------------------------------------------------
	
	public function updateDesktopIconPosition($d, $output=true, $remove=false){
	 	
	 	$infoPath = Config::get("private_system_path")."/config/users/".User::getInstance()->userInfo->username."_dsc.txt";
	 	$infoXmlStr = Util::readFile($infoPath);
		$deskInfo = DataSerializer::deserialize($infoXmlStr);
		
		$retObj = (object) array();
		$retObj->success=false;
		
		$exist=false;
		if(is_array($deskInfo)){
			for($i=0; $i<count($deskInfo); $i++){
				if($deskInfo[$i]->name==$d->name){
				 	if($remove){
				 		array_splice($deskInfo, $i, 1);
				 	}else{
						if(isset($d->x)) $deskInfo[$i]->x = $d->x;
						if(isset($d->y)) $deskInfo[$i]->y = $d->y;
						$exist=true;
					}
				}
			}
		}
		if(!$exist && !$remove){
			$len = count($deskInfo);
			$deskInfo[$len]->name=$d->name;
			$deskInfo[$len]->x=$d->x;
			$deskInfo[$len]->y=$d->y;
		}

		$retObj->success=true;
		
		if($retObj->success=true)
			Util::writeFile($infoPath, DataSerializer::serialize($deskInfo));
		
		if($output==true)
			Response::sendOKResponse($retObj);
		else
			return $retObj;
			
	}
	
	// -----------------------------------------------------
	// RETURN DESKTOP FILES
	// -----------------------------------------------------
	
	public function getDesktopFiles($d){
	 
 	//	$this->file = new File();
 	 
 		$fileObj = $this->file->checkPathAndGetPathElements($d->path);
 	 	$FaF = $this->file->getFoldersAndFiles($fileObj->realPath, false, "m/d/Y H:i");
	 	if($FaF<0){
	 		Response::sendErrorResponse($this->file->getErrorID($FaF), $this->file->getErrorMsg($FaF));
		}

		usort($FaF, array(&$this, "sortFilesFunction"));

		for($i=0; $i<count($FaF); $i++){
		 	$FaF[$i] = (object) $FaF[$i];
			if($FaF[$i]->extension=="sc"){
				$scXmlSrt = Util::readFile($fileObj->realPath."/".$FaF[$i]->name);
	 			if($scXmlSrt!==false){
	 			$FaF[$i]->shortCut = DataSerializer::deserialize($scXmlSrt);
				}
			}
		}
	
		$retObj = (object) array();
		$retObj->FaF = $FaF;
		$infoXmlStr = Util::readFile(Config::get("private_system_path")."/config/users/".User::getInstance()->userInfo->username."_dsc.txt");
		$retObj->info = DataSerializer::deserialize($infoXmlStr);
		Response::sendOKResponse($retObj);
		
	}
	

	static private function sortFilesFunction($a, $b){
	 	if($a["type"]=="dir" && $b["type"]!="file" ) return -1;
	 	elseif($a["type"]!="file" && $b["type"]=="dir" ) return 1;
	 	else return strcmp($a["name"], $b["name"]);
	}   

 
}

?>