bool wxWebUpdateActionOpen::Run() const { wxFileName f(m_strFile); wxLogUsrMsg(wxT("wxWebUpdateActionOpen::Run - opening the file [") + m_strFile + wxT("]")); if (!f.FileExists()) { wxLogUsrMsg(wxT("wxWebUpdateActionOpen::Run - the file \"") + m_strFile + wxT("\" does not exist !")); return FALSE; } // a little exception for Web pages: wxWidgets has the better function // wxLaunchDefaultBrowser which has a bteer error-checking and more // fallbacks, so use it if possible if (f.GetExt().StartsWith(wxT("htm")) && m_nExecFlag == wxEXEC_ASYNC) return wxLaunchDefaultBrowser(m_strFile); // get the mime type wxFileType *ft; if (m_strMime.IsEmpty()) if (f.GetExt().IsEmpty()) return FALSE; // how do we get the MIME type without extension ? else ft = wxTheMimeTypesManager->GetFileTypeFromExtension(f.GetExt()); else ft = wxTheMimeTypesManager->GetFileTypeFromMimeType(m_strMime); if (!ft) { wxLogUsrMsg(wxT("wxWebUpdateActionOpen::Run - No default application can open the file [") + m_strFile + wxT("]")); return false; } wxString mt; ft->GetMimeType(&mt); // get the open command wxString cmd; bool ok = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters(m_strFile)); delete ft; if (!ok) { wxLogUsrMsg(wxT("wxWebUpdateActionOpen::Run - Cannot get the OPEN command for [") + m_strFile + wxT("]")); return FALSE; } if (wxExecute (cmd, m_nExecFlag) == -1) { wxLogUsrMsg(wxT("wxWebUpdateActionOpen::Run - Failed to launch application for [") + m_strFile + wxT("]")); return FALSE; } return TRUE; }
unsigned long wxGetSizeOfURI(const wxString &uri) { wxLogDebug(wxT("wxGetSizeOfURI - getting size of [") + uri + wxT("]")); wxInputStream *is = wxGetInputStreamFromURI(uri); if (is == NULL) { wxLogDebug(wxT("wxGetSizeOfURI - aborting; invalid URL !")); return 0; } if (!is->IsOk()) { wxLogDebug(wxT("wxGetSizeOfURI - aborting; invalid URL !")); delete is; // be sure to avoid leaks return 0; } // intercept the 302 HTTP "return code" #if 0 wxProtocol &p = u.GetProtocol(); wxHTTP *http = wxDynamicCast(&p, wxHTTP); if (http != NULL && http->GetResponse() == 302) { wxLogUsrMsg(wxT("wxGetSizeOfURI - can't get the size of the resource located at [") + uri + wxT("] because the request has been redirected... update your URL")); return 0; } #endif unsigned long sz = (unsigned long)is->GetSize(); delete is; // see wxHTTP::GetInputStream docs if (sz == 0xffffffff) sz = 0; return sz; }
bool wxWebUpdateActionMkdir::Run() const { wxArrayString orig, output; wxLogUsrMsg(wxT("wxWebUpdateActionMkdir::Run - going to make the folder [") + m_strTarget + wxT("]")); // wxFileName wants a path separator at the end of directory names wxString dir(m_strTarget); if (dir.Last() != wxFileName::GetPathSeparator()) dir += wxFileName::GetPathSeparator(); wxFileName f(dir); if (f.DirExists()) { wxLogAdvMsg(wxT("wxWebUpdateActionMkdir::Run - the folder \"") + m_strTarget + wxT("\" already exist... proceeding anyway")); return TRUE; } // create it ! if (f.Mkdir(0777, wxPATH_MKDIR_FULL)) wxLogDebug(wxT("wxWebUpdateActionMkdir::Run - created the [") + f.GetPath() + wxT("] folder")); else wxLogDebug(wxT("wxWebUpdateActionMkdir::Run - could not create the [") + f.GetPath() + wxT("] folder.. proceeding anyway")); return TRUE; }
bool wxWebUpdateActionMkfile::Run() const { wxArrayString orig, output; wxLogUsrMsg(wxT("wxWebUpdateActionMkfile::Run - going to make the file [") + m_strTarget + wxT("]")); // do we have to create a folder ? wxFileName f(m_strTarget); if (f.FileExists()) { if (m_bOverwrite) wxLogAdvMsg(wxT("wxWebUpdateActionMkfile::Run - the file \"") + m_strTarget + wxT("\" already exist... proceeding anyway (overwrite=1)")); else return TRUE; // exit } // create it ! wxFileOutputStream out(f.GetFullPath()); // do the encoding conversion wxCSConv converter(m_strEncoding); wxCharBuffer buf = m_strContent.mb_str(converter); const char *data = (const char*) buf; size_t bytes = strlen(data)*sizeof(char); // write if (out.Write(data, bytes).LastWrite() != bytes) { wxLogUsrMsg(wxT("wxWebUpdateActionMkfile::Run - could not create the [") + f.GetFullPath() + wxT("] file.")); //wxDELETEA(data); return FALSE; } //wxDELETEA(data); wxLogUsrMsg(wxT("wxWebUpdateActionMkfile::Run - created the [") + f.GetFullPath() + wxT("] file with content [") + m_strContent + wxT("]...")); return TRUE; }
bool wxWebUpdateActionRun::Run() const { // be sure that the file to run exists wxFileName f(m_strFile); if (!f.FileExists()) { wxLogUsrMsg(_("wxWebUpdateActionRun::Run - the file [%s] does not exist; proceeding anyway (maybe it's in PATH)"), m_strFile.c_str()); // proceed: the executable could be in the system path... } int retcode = wxExecute(m_strFile + wxS(" ") + m_strArgs, m_nExecFlag | wxEXEC_NODISABLE); // FIXME: how do we know if this retcode means success or not ? // (some programs could not respect the 0=success UNIX standard...) if (retcode != 0) wxLogAdvMsg(wxS("wxWebUpdateActionRun::Run - got a non-null return code for ") wxS("the last command; proceeding anyway...")); return TRUE; }
bool wxWebUpdateActionCopy::Run() const { wxArrayString orig, output; wxLogUsrMsg(wxT("wxWebUpdateActionCopy::Run - going to copy the file(s)/folder(s) [") + m_strFrom + wxT("] in [") + m_strTo + wxT("]")); // wxFileName wants a path separator at the end of directory names wxString dir(m_strTo); if (dir.Last() != wxFileName::GetPathSeparator()) dir += wxFileName::GetPathSeparator(); // be sure that the destination directory exists wxFileName f(dir); if (!f.DirExists() && !m_bCreate) { wxLogAdvMsg(wxT("wxWebUpdateActionCopy::Run - the folder \"") + m_strTo + wxT("\" does not exist and create=0 !")); return FALSE; } // do we need to create the destination folder ? if (!f.DirExists()) { wxASSERT_MSG(m_bCreate, wxT("The create=0 case should have been already catched")); if (f.Mkdir(0777, wxPATH_MKDIR_FULL)) wxLogAdvMsg(wxT("wxWebUpdateActionCopy::Run - created the [") + f.GetPath() + wxT("] folder")); else wxLogAdvMsg(wxT("wxWebUpdateActionCopy::Run - could not create the [") + f.GetPath() + wxT("] folder.. proceeding anyway")); } // do the copy // FIXME: how to handle the "m_bMove" flag ?? if (!wxCopyFile(m_strFrom, m_strTo, m_bOverwrite)) return FALSE; return TRUE; }
void *wxDownloadThread::Entry() { // we'll use wxPostEvent to post this event since this is the // only thread-safe way to post events ! wxCommandEvent updatevent(wxEVT_COMMAND_DOWNLOAD_COMPLETE); // begin our loop while (!TestDestroy()) { if (m_nStatus == wxDTS_WAITING) { //wxLogDebug(wxT("wxDownloadThread::Entry - sleeping 1sec")); m_bReady = TRUE; wxThread::Sleep(100); continue; } // reset our variables m_nFinalSize = 0; m_nCurrentSize = 0; // we are starting the download of a file; update our datetime field m_dtStart = wxDateTime::UNow(); wxLogUsrMsg(wxT("wxDownloadThread::Entry - downloading ") + m_strURI); // ensure we can build a wxURL from the given URI wxInputStream *in = wxGetInputStreamFromURI(m_strURI); // check INPUT if (in == NULL) { // something is wrong with the input URL... wxDT_ABORT_DOWNLOAD(wxT("Cannot open the INPUT stream; ") wxT("url is [") + m_strURI + wxT("]")); } if (!in->IsOk()) { delete in; wxDT_ABORT_DOWNLOAD(wxT("Cannot init the INPUT stream")); } // now work on streams; wx docs says that using wxURL::GetInputStream // is deprecated but this is the only way to set advanced info like // proxy, user & password... wxFileOutputStream out(m_strOutput); if (!out.IsOk()) { delete in; wxDT_ABORT_DOWNLOAD(wxT("Cannot open/init the OUPUT stream [") + m_strOutput + wxT("]")); } m_nFinalSize = in->GetSize(); // see wxHTTP docs if (m_nFinalSize == 0xffffffff) m_nFinalSize = 0; // write the downloaded stuff in the output file // without using the // out.Write(*in); // command; that would be easier but would not allow // the program to stop this thread while downloading // the file since the TestDestroy() function would not // be called in that way... char buf[wxDT_BUF_TEMP_SIZE]; while (!TestDestroy() && m_nStatus == wxDTS_DOWNLOADING) { size_t bytes_read = in->Read(buf, WXSIZEOF(buf)).LastRead(); if ( !bytes_read ) break; if ( out.Write(buf, bytes_read).LastWrite() != bytes_read ) break; // update our downloaded bytes var m_nCurrentSize = out.GetSize(); #ifdef __WXDEBUG__ // do not send too many log messages; send a log message // each 20 cycles (i.e. each 20*wxDT_BUF_TEMP_SIZE byte downloaded) if ((m_nCurrentSize % (wxDT_BUF_TEMP_SIZE*20)) == 0) wxLogUsrMsg(wxT("wxDownloadThread::Entry - downloaded %lu bytes"), m_nCurrentSize); #endif } // we don't need the INPUT stream anymore... delete in; // if m_nFinalSize is set to zero, then we cannot trust it; // we must consider the size of the remote file as unavailable // since the wxHTTP protocol does not allow us to get it... if (!out.IsOk() || out.GetSize() == 0 || (out.GetSize() != m_nFinalSize && m_nFinalSize != 0)) wxDT_ABORT_DOWNLOAD(wxT("Output FILE stream size is wrong")); wxLogUsrMsg(wxT("wxDownloadThread::Entry - completed download of %lu bytes"), m_nCurrentSize); // do we have to compute MD5 ? #if wxUSE_MD5 m_mStatus.Lock(); m_nStatus = wxDTS_COMPUTINGMD5; m_mStatus.Unlock(); // get the md5 checksum for the just downloaded file m_strComputedMD5 = wxMD5::GetFileMD5(m_strOutput); #endif // we have successfully download the file m_bSuccess = TRUE; { // go in wait mode wxMutexLocker locker(m_mStatus); m_nStatus = wxDTS_WAITING; } wxPostEvent(m_pHandler, updatevent); m_nFileCount++; // we reset our variables here because there is a delay between the // wxDownloadThread::BeginNewDownload() calls and the execution of the // first statements of this thread... m_nCurrentSize = 0; m_nFinalSize = 0; } return (void*)FALSE; }
bool wxWebUpdateActionExtract::Run() const { wxArrayString orig, output; wxLogUsrMsg(wxT("wxWebUpdateActionExtract::Run - going to extract the file [") + m_strFile + wxT("] of type [") + m_strType + wxT("] in [") + m_strWhere + wxT("]")); // wxFileName wants a path separator at the end of directory names wxString dir(m_strWhere); if (dir.Last() != wxFileName::GetPathSeparator()) dir += wxFileName::GetPathSeparator(); // be sure that the destination directory exists wxFileName f(dir), f2(m_strFile); if (!f.DirExists() || !f2.FileExists()) { wxLogUsrMsg(wxT("wxWebUpdateActionExtract::Run - the folder \"") + m_strWhere + wxT("\" or the file \"") + m_strFile + wxT("\" does not exist !")); return FALSE; } // parse the namemap wxArrayString compressed, extracted; int count = wxWebUpdateInstaller::Get()->ParsePairValueList(m_strNameMap, compressed, extracted); // do the substitutions also on the compressed filenames for (int i=0; i < count; i++) compressed[i] = wxWebUpdateInstaller::Get()->DoSubstitution(compressed[i]); // create the archive factory wxArchiveClassFactory *factory = NULL; if (m_strType == wxT("zip")) factory = new wxZipClassFactory; // extract the package wxArchiveEntryPtr entry; wxFFileInputStream input(m_strFile); wxArchiveInputStream *in = factory->NewStream(input); delete factory; while (entry.reset(in->GetNextEntry()), entry.get() != NULL) { // access meta-data wxString name = entry->GetName(); // is this file/dir registered in the name map ? int idx = compressed.Index(name); if (idx != wxNOT_FOUND) name = extracted[idx]; wxString output = dir + name; // can be different from "dir" if "name" includes a relative path... wxString outputdir = wxFileName(output).GetPath(); // intercept directories... if (!wxDirExists(outputdir)) { if (wxFileName(outputdir).Mkdir(0777, wxPATH_MKDIR_FULL)) wxLogAdvMsg(wxT("wxWebUpdateActionExtract::Run - created the [") + outputdir + wxT("] folder")); else wxLogAdvMsg(wxT("wxWebUpdateActionExtract::Run - could not create the [") + outputdir + wxT("] folder.. proceeding anyway")); continue; // this entry contains no data } // this is a file... wxLogUsrMsg(wxT("wxWebUpdateActionExtract::Run - extracting [") + name + wxT("] as [") + output + wxT("]...")); // now just dump this entry to a new uncompressed file... wxFileOutputStream out(output); if (!out.IsOk() || !out.Write(*in)) { wxLogUsrMsg(wxT("wxWebUpdateActionExtract::Run - couldn't decompress ") + name); delete in; return FALSE; } } delete in; return TRUE; }