/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct InstGetFile = [ PGF_inst : PInstance, PGF_sFiletype : S, PGF_httpReq : ObjCURL ]mkInstGetFile;; var sTmpDownloadPath = "tmp/download/";; fun deleteOb(inst, obstr)= if (obstr.PGF_httpReq == nil) then nil else ( killHttpRequest obstr.PGF_httpReq; set obstr.PGF_httpReq = nil; ); 0;; fun cbDlgOpenFile(dlg, obstr, pfile)= if pfile == nil then nil else ( //Copy file if not in partition let _PtoScol pfile -> file in if (file == nil) then ( let _getpack pfile -> fcont in if (fcont == nil) then ( SendPluginEvent obstr.PGF_inst "Error" nil nil; ) else ( let strreplace _GetFileNameFromP pfile " " "_" -> fname in let strcatn sTmpDownloadPath::"/"::fname::nil -> path in ( _storepack fcont path; SendPluginEvent obstr.PGF_inst "File" path nil; ); ); ) else SendPluginEvent obstr.PGF_inst "File" file nil; ); 0;; fun cbDownloaded(file, wfile, obstr)= let _WtoP wfile -> tmpfile in let _fileSize tmpfile -> size in if (size == 0) then ( SendPluginEvent obstr.PGF_inst "Error" nil nil; 0; ) else ( cbDlgOpenFile nil obstr tmpfile; 0; ); 0;; fun cbFileInfos(url, tag, length, obstr)= if (obstr.PGF_httpReq == nil) then nil else ( killHttpRequest obstr.PGF_httpReq; set obstr.PGF_httpReq = nil; ); //bad response if ((tag == nil) && (length == nil)) then ( SendPluginEvent obstr.PGF_inst "Error" nil nil; 0; ) else ( let getPathFile url "" -> [dir file] in let strcat sTmpDownloadPath file -> tmpfile in let strcat tmpfile ".tag" -> tagfile in let strcatn tag::" "::(itoa length)::nil -> tagcont in if (((_checkpack tmpfile) != nil) && (!strcmp (_getpack _checkpack tagfile) tagcont)) then ( cbDlgOpenFile nil obstr _checkpack tmpfile; 0; ) else ( _storepack tagcont tagfile; let _getmodifypack tmpfile -> wfile in let _createpack "" wfile -> _ in //force empty set obstr.PGF_httpReq = downloadFileW url wfile mkfun3 @cbDownloaded obstr; 0; ); ); 0;; fun cbOs3dFileInfos(url, data, obstr)= if (data != nil) then ( let readCSVdata data "," -> lcont in let hd lcont -> [name [dlurl [size [md5 _]]]] in let strcat sTmpDownloadPath name -> tmpfile in if (((_checkpack tmpfile) != nil) && (!strcmp (_filemd5 _checkpack tmpfile) md5)) then ( cbDlgOpenFile nil obstr _checkpack tmpfile; 0; ) else ( let _getmodifypack tmpfile -> wfile in let _createpack "" wfile -> _ in //force empty set obstr.PGF_httpReq = downloadFileW dlurl wfile mkfun3 @cbDownloaded obstr; 0; ); 0; ) else ( SendPluginEvent obstr.PGF_inst "Error" nil nil; 0; ); 0;; fun downloadFileUrl(obstr, url)= //editor mode if (iPluginMode == 1) then nil else _cacheActivate; if (obstr.PGF_httpReq == nil) then nil else ( killHttpRequest obstr.PGF_httpReq; set obstr.PGF_httpReq = nil; ); let if (strfind "os3dpak.php?dl=" url 0) != nil then 1 else 0 -> os3dpakUrl in if (os3dpakUrl) then ( let strfind "?dl=" url 0 -> fpos in let substr url 0 fpos -> burl in let substr url (fpos + 4) (strlen url) - (fpos + 4) -> fname in let strcatn burl::"?fileinfos="::fname::nil -> iurl in set obstr.PGF_httpReq = getUrl iurl nil mkfun3 @cbOs3dFileInfos obstr; ) else ( set obstr.PGF_httpReq = getUrlContentInfos url mkfun4 @cbFileInfos obstr; ); SendPluginEvent obstr.PGF_inst "Downloading" nil nil; 0;; fun cbOpenFile(inst, from, action, param, reply, obstr)= let if (!strcmp obstr.PGF_sFiletype "*.*") || (! strcmp strtrim obstr.PGF_sFiletype "") then "\0" else let strreplace obstr.PGF_sFiletype " " ";" -> ftype in strcatn "("::obstr.PGF_sFiletype::")\0"::ftype::"\0\0"::nil -> filetype in _DLGrflopen _DLGOpenFile _channel DMSwin "/" "" filetype @cbDlgOpenFile obstr; 0;; fun cbDownloadFile(inst, from, action, param, reply, obstr)= if (strIsUrl param) then ( downloadFileUrl obstr param; ) else ( SendPluginEvent obstr.PGF_inst "Error" nil nil; ); 0;; fun dropFiles(inst, viewstr, x, y, lp, obstr)= if (lp == nil) then nil else cbDlgOpenFile nil obstr (hd lp); 0;; fun newOb(inst)= let getPluginInstanceParam inst "filetype" -> filetype in let if filetype == nil then "*.*" else filetype -> filetype in let atoi (getPluginInstanceParam inst "dragndrop") -> dragndrop in let if dragndrop == nil then 0 else dragndrop -> dragndrop in let mkInstGetFile [inst filetype nil] -> obstr in ( PluginRegisterAction inst "Open file" mkfun6 @cbOpenFile obstr; PluginRegisterAction inst "Download file" mkfun6 @cbDownloadFile obstr; if (!dragndrop) then nil else setPluginInstanceCbDropFile inst mkfun6 @dropFiles obstr; setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;