1 
  2 /**
  3  * @name	CeL function for Windows job
  4  * @fileoverview
  5  * 本檔案包含了 Windows job 的 functions。
  6  * @since	
  7  */
  8 
  9 if (typeof CeL === 'function')
 10 CeL.setup_module('application.OS.Windows.job',
 11 {
 12 require : 'data.code.compatibility.is_DOM',
 13 code : function(library_namespace, load_arguments) {
 14 
 15 //	requiring
 16 var is_DOM;
 17 eval(library_namespace.use_function(this));
 18 
 19 
 20 /**
 21  * null module constructor
 22  * @class	Windows job 的 functions
 23  */
 24 CeL.application.OS.Windows.job
 25 = function() {
 26 	//	null module constructor
 27 };
 28 
 29 /**
 30  * for JSDT: 有 prototype 才會將之當作 Class
 31  */
 32 CeL.application.OS.Windows.job
 33 .prototype = {
 34 };
 35 
 36 
 37 
 38 
 39 //	有關安裝的部分
 40 function install(dir){//move files: input base dir
 41  var f,d,i=0,s=0,c=0,l=getResource("filelist").split(/[\r\n]+/);//.replace(/[\r\n]+/g,"\n").split('\n')
 42  for(;i<l.length;i++)if(l[i]&&l[i].charAt(0)!='#'&&(f=l[i].split('\t'))){
 43   d=turnToPath(f[1]),f=f[0],d=d?isFolder(d)?(isFolder(d)==2?dir:'')+d+f:d:dir+f;
 44   if(f)if(c++,d=mv(f,d))pErr(d);else s++;
 45   else if(d)pLog('フォルダ '+d+' の作成を'+(isFolder(d,1)?'成功':'失敗')+'した');
 46  }
 47  if(c)pLog(s+"/"+c+"filesの移動に成功した");
 48 }
 49 
 50 function rmProg(){
 51  if(typeof getResource=='undefined')return;
 52  var i=0,f,l=getResource("proglist").split(/\r\n/);//.replace(/[\r\n]+/g,"\n").split('\n')
 53  for(;i<l.length;i++)if(l[i]&&(f=l[i].split('\t')[0]))try{fso.DeleteFile(f);}catch(e){}
 54 }
 55 
 56 
 57 //	先決條件測試@.js主檔,當沒問題時return 0。此函數若使用到function.js之(其他)功能需include入!
 58 //	include:getScriptName(),mergeScript()
 59 function preCheck(argumentCount,ver,mFN){	//	argument數,最低版本,若ver<5.1時合併檔名
 60  var SN=getScriptName(),WshShell=WScript.CreateObject("WScript.Shell");
 61  if(!argumentCount)argumentCount=0;
 62 
 63  if(!WScript.Interactive){
 64   WshShell.Popup('This program must run in interactive mode!\n此程式需執行於互動模式!',0,SN,48);
 65   return 5;
 66  }
 67  if(WScript.Arguments.length>argumentCount){
 68   if(typeof WScript.Arguments.ShowUsage=='unknown'||WScript.Arguments.ShowUsage)WScript.Arguments.ShowUsage();
 69   else WshShell.Popup('Error arguments!\n引數錯誤!',0,SN,16);
 70   return 6;
 71  }//else if(2==WshShell.Popup("此程式應用於帳目處理。",0,"確定執行?",1+64))return 4;
 72  //	以上可置於.wsf中。
 73 
 74  if(!ver||ver<5)ver=5;if(!mFN)mFN='process';
 75  if(typeof checkVer=='function')checkVer(ver);	//	5.1起才能用.wsf(windows script file)控制
 76  else if(WScript.Version>5){WshShell.Popup('請執行 '+SN+'.wsf 檔!',0,SN+': 不是執行這個檔喔!',48);return 7;}
 77  else if(mergeScript(mFN+'.js')){WshShell.Popup('合併檔案失敗!',0,SN,16);return 8;}
 78  else{
 79   try{fso.CopyFile(SN+'.ini',mFN+'.ini');}catch(e){}	//	copy .ini
 80   WshShell.Popup('請設定好 '+mFN+'.ini,\n之後您可試試 '+mFN+'.js 檔,但並不一定能順利執行。',0,SN+': 使用的版本過舊!',48);return 9;
 81  }
 82 
 83  return 0;
 84 }
 85 
 86 
 87 
 88 /*	2009/6/18 20:46:1
 89 	更新功能 update function
 90 
 91 可以嘗試將 for_check 合在 install_url 中。
 92 
 93 */
 94 
 95 Update[generateCode.dLK]='getU,getFN,simpleWrite';	//	,Debug_for_include,gDate
 96 
 97 function Update(for_check, install_url) {
 98 	return Update.check(Update.setup(for_check, install_url));
 99 }
100 
101 //	base function
102 
103 Update.check=function(force){
104  if(!this.URL)return;
105 
106  if(force||!this.version){
107   var d=this.parse(getU(this.URL)||'');
108   this.version_get=d;
109   //sl('Update.check: version get: ['+this.version_get+']');
110  }
111  return this.version=[this.version_get,this.version_now,this.URL];
112 };
113 
114 Update.set_URL=function(for_check,install_url){
115  var unchang=1;
116  if(for_check){
117   if(unchang)
118    unchang= this.URL==for_check;
119 
120   //sl('Update.set_URL: 設定檢測 URL: <a href="'+for_check+'">'+for_check+'</a>');
121   this.URL=for_check;
122  }
123  if(install_url){
124   if(unchang)
125    unchang= this.download_URL==install_url;
126 
127   //sl('Update.set_URL: 設定程式下載 URL: <a href="'+install_url+'">'+install_url+'</a>');
128   this.download_URL=install_url;
129  }
130  return !unchang;
131 };
132 
133 /*
134 TODO:
135 date 相同時比較大小
136 */
137 Update.up_to_date=function(){
138  return 0>=this.compare(this.version_now,this.version_get);
139 };
140 
141 Update.install=function(){
142  var p=getFN(decodeURI(location.pathname)),t=p+'.new',b=p+'.old',f=this.after_install;
143  //sl('Update.install: program path: ['+p+']');
144  if(this.download(t)){
145   try{fso.DeleteFile(b);}catch(e){}
146   try{
147    fso.MoveFile(p,b);
148    fso.MoveFile(t,p);
149   }catch(e){f&&f(e,p,t);return;}
150   f&&f();
151   return 1;
152  }
153 };
154 
155 Update.check_string='check_string';
156 Update.download=function(to_where){
157  //sl('Update.download: download [<a href="'+this.download_URL+'">'+this.download_URL+'</a>] to ['+to_where+']');
158  var data=getU(this.download_URL),f=this.after_download;
159  if(data&&(!this.check_string||data.indexOf(this.check_string)!=-1)){
160   simpleWrite(to_where,data,TristateTrue);
161   f&&f(0,data);
162   return data;
163  }else f&&f(1,data);
164 };
165 
166 
167 //	default user function
168 Update.setup=function(for_check,install_url){
169  var v=document.getElementById('version');
170  if(v)v=v.innerHTML.replace(/[\s\n]+$|^[\s\n]+/g,'');
171  this.version_now=new Date(v||document.lastModified);
172  //sl('Update.setup: version now: ['+this.version_now+']');
173 
174  return this.set_URL(for_check,install_url);
175 };
176 
177 Update.parse=function(version_data){
178  return new Date(version_data||0);
179 };
180 
181 Update.compare=function(v1,v2){
182  //sl('Update.compare: ['+v2+'] - ['+v1+'] = '+(v2-v1));
183  return v2-v1;
184 };
185 
186 Update.after_install=function(e,prog,tmp){	//	e: Error object
187  if(e){err(e);err('Update.install: 無法替換程式檔 ['+prog+']。新的程式檔置於 ['+tmp+']。');}
188  else sl('更新完畢。'),warn('您需要<b onclick="history.go(0);">重新讀取</b>以完成更新!');
189 };
190 
191 Update.after_download=function(e,data){	//	e: error code
192  if(e)err('Update.download: 下載 [<a href="'+this.download_URL+'">'+this.download_URL+'</a>] 發生錯誤!');
193 };
194 
195 
196 
197 /*	讀入單行 item=value 之設定
198 	!noComment: '#', ';' 起頭、// 之後及 / *..* / 之間將被省略
199 */
200 function parseData(s,noComment){
201  if(!s/*||s.charAt(0)=='#'*/ || !noComment&&!(s=s.replace(/([#;]|\/\/).*$/g,'')))return;	//	去掉單行註解
202  var t,c,r,i=1;
203  if(t=s.match(/\s*\[(.*)\]/))return t[1];	//	分區
204 
205  try{	//	沒合在一起是為了在較低版本中r之設定可能失敗
206   r=new RegExp('^\\s*([^=]+)\\s*=\\s*([^\\r\\n]*)');
207   t=s.match(r);
208  }catch(e){t=s.match(/^\s*([^=]+)\s*=\s*([^\r\n]*)/);}	//	使用此式可能導致某些問題
209 
210  if(t){//if(t=s.match(/^\s*([^=]+?)\s*=\s*([^\r\n]*)/)){	//	後面的:原先簡潔版
211   if(!t[1])return;
212   var set=[];set[0]=t[1].replace(/\s+$|^\s+/g,''),t=set[1]=t[2];
213   while((c=t.charAt(0))=='"'||c=="'"){
214    if(!(c=t.match(new RegExp('^'+c+'([^'+c+']*)'+c+'[^\'"]*(.*)$'))))break;
215    //alert('['+set[0]+']=\n'+t+'\n'+c[1]+'\n['+c[2]+']');
216    set[i++]=c[1];
217    t=c[2];//if(c=t.match(/^\s+/))t=t.substr(c.length);
218    //alert('['+t+']');
219   }
220   return set;
221  }
222 }
223 
224 /*	readin .ini file
225 	http://en.wikipedia.org/wiki/Ini_file
226 */
227 parseINI[generateCode.dLK]='initWScriptObj,parseData,ForReading,TristateUseDefault';
228 function parseINI(FN,format,INIunBlock,noComment){
229  var INI={},datas;	//	設定值之陣列,未框起(設定區塊)之值
230  if(!INIunBlock)INIunBlock='[unBlock]';	//	未框起(設定區塊)之值
231  INI[INIunBlock]={};
232  try{
233   datas=fso.OpenTextFile(FN,ForReading,false,format||TristateUseDefault);
234  }catch(e){}	//	不用openTemplate():當找不到此.ini檔時pass而不顯示error
235  if(!datas){
236   //if(!INI)INI=[],INI[block=INIunBlock]=[];return 1;
237   //alert('Cannot open:\n'+FN);
238   return 0;
239  }
240 
241  var i,j,index,k,t,block,inC=false;	//	index,temp,區塊,於註解中(in comment)
242  //INI=[],INI[block=INIunBlock]=[];	//	每次執行即重設
243  while(!datas.AtEndOfStream)if(t=datas.ReadLine()){
244   if(!noComment){
245    t=t.replace(/\/\/.*/,'');	//	處理/*..*/前先處理//
246    k=1;
247    while(k){
248     k=0;	//	.replace(/\/\*.*?\*\//g,'') 在 ver5 前會出現錯誤
249     //if(!inC&&(i=t.indexOf('/*'))!=-1)if((j=t.indexOf('*/',i+2))==-1){inC=true,t=t.slice(0,i);break;}else k=1,t=t.slice(0,i)+t.substr(j+2);
250     if(!inC&&(i=t.indexOf('/*'))!=-1){	//	處理註解 /*
251      j=i+2;
252      do{j=t.indexOf('*/',j);if(t.charAt(j-1)=='\\')j+=2;else break;}while(j!=-1);	//	預防「\*/」
253      if(j==-1){inC=true,t=t.slice(0,i);break;}else k=1,t=t.slice(0,i)+t.substr(j+2);
254     }
255     //if(inC)if((i=t.indexOf('*/'))==-1)t='';else inC=false,t=t.substr(i+2),k=1;
256     if(inC){	//	處理*/
257      i=0;
258      do{i=t.indexOf('*/',i);if(t.charAt(i-1)=='\\')i+=2;else break;}while(i!=-1);	//	預防「\*/」
259      if(i==-1)t='';else inC=false,t=t.substr(i+2),k=1;
260     }
261    }
262   }
263   //if(!t)continue;alert(t);
264   t=parseData(t,noComment);if(!t)continue;
265   if(typeof t=='string'&&!INI[block=t])INI[block]={};
266   else if(t.length==2)INI[block][t[0]]=t[1];
267   else for(i=1,INI[block][t[0]]=[];i<t.length;i++)INI[block][t[0]].push(t[i]);//,alert(block+','+t[0]+','+t[i])
268   //if(t[0])alert('INI['+block+']['+t[0]+']='+INI[block][t[0]]);else alert('block='+block);
269  }
270 
271  datas.Close();
272  return INI;
273 }
274 
275 
276 
277 
278 /*
279 	Scriptlet.Typelib 對象的設計用途是幫助您創建“Windows 腳本組件”(實質上,這是一種使您編寫的腳本可以像COM對象那樣工作的方法)。
280 	http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0221.mspx
281 	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rpc/rpc/guid.asp
282 */
283 function tempGUID(){
284  var TypeLib=WScript.CreateObject("Scriptlet.TypeLib"),tGUID;
285 
286  try{tGUID=TypeLib.Guid();}
287  catch(e){return;}
288  finally{TypeLib=null;}	//	即使 try 或 catch 區塊中發生傳回陳述式,或從 catch 區塊中擲出錯誤,仍會執行 finallyStatements 內的程式碼。finallyStatments 保證會永遠執行。
289 
290  return tGUID;
291 }
292 
293 
294 
295 
296 return (
297 	CeL.application.OS.Windows.job
298 );
299 }
300 
301 
302 });
303 
304