⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatedlg.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:

        case 4: // not installed
            return !rec->downloaded && !rec->installed;

        default:
            return false;
    }
    return false; // doesn't reach here
}

void UpdateDlg::ApplyFilter()
{
    wxTreeCtrl* tree = XRCCTRL(*this, "tvCategories", wxTreeCtrl);

    FillGroups();
    FillFiles(tree->GetSelection());
    EnableButtons();
}

UpdateRec* UpdateDlg::GetRecFromListView()
{
    wxListCtrl* lst = XRCCTRL(*this, "lvFiles", wxListCtrl);
    int index = lst->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
    if (index == -1)
        return 0;
    wxString title = lst->GetItemText(index);
    return FindRecByTitle(title, m_Recs, m_RecsCount);
}

void UpdateDlg::DownloadFile(bool dontInstall)
{
    UpdateStatus(_("Please wait..."));
    UpdateRec* rec = GetRecFromListView();
    if (!rec)
    {
        wxMessageBox(_("No file selected!"), _("Error"), wxICON_ERROR);
        UpdateStatus(_("Ready"), 0, 0);
        return;
    }

    if (rec->version == rec->installed_version)
    {
        if (wxMessageBox(_("You seem to have installed the latest version.\nAre you sure you want to proceed?"), _("Confirmation"), wxICON_QUESTION | wxYES_NO) == wxNO)
            return;
    }

    if (!CreateDirRecursively(GetPackagePath()))
    {
        wxMessageBox(_("Can't create directory ") + GetPackagePath(), _("Error"), wxICON_ERROR);
        return;
    }

    if (wxFileExists(GetPackagePath() + rec->local_file))
    {
        if (wxMessageBox(_("This file already exists!\nAre you sure you want to download it again?"), _("Confirmation"), wxICON_QUESTION | wxYES_NO) == wxNO &&
            rec->installable)
        {
            if (!dontInstall && wxMessageBox(_("Do you want to force-install it?"), _("Confirmation"), wxICON_QUESTION | wxYES_NO) == wxYES)
                InstallFile();
            return;
        }
    }

    m_Net.SetServer(rec->remote_server);

    EnableButtons(false);
    if (!m_Net.DownloadFile(rec->remote_file, GetPackagePath() + rec->local_file))
    {
        rec->downloaded = false;
        UpdateStatus(_("Error downloading file: ") + rec->remote_server + _T(" > ") + rec->remote_file, 0, 0);
        return;
    }
    else
        rec->downloaded = true;
    UpdateStatus(_("Ready"), 0, 0);
    EnableButtons();
}

void UpdateDlg::InstallFile()
{
    UpdateStatus(_("Please wait..."));
    UpdateRec* rec = GetRecFromListView();
    if (!rec)
    {
        wxMessageBox(_("No file selected!"), _("Error"), wxICON_ERROR);
        UpdateStatus(_("Ready"), 0, 0);
        return;
    }
    wxYield();

    if (rec->title == _T("WebUpdate Mirrors list"))
    {
        InstallMirrors(GetPackagePath() + rec->local_file);
        rec->installed = true;
        ApplyFilter();
        UpdateStatus(_("Ready"), 0, 0);
        return;
    }
    else if (!rec->installable)
    {
        UpdateStatus(_("Ready"), 0, 0);
        return;
    }

    if (!CreateDirRecursively(GetPackagePath()))
    {
        UpdateStatus(_("Ready"), 0, 0);
        wxMessageBox(_("Can't create directory ") + GetPackagePath(), _("Error"), wxICON_ERROR);
        return;
    }

    wxArrayString files;
    DevPakInstaller inst;
    if (inst.Install(rec->name, GetPackagePath() + rec->local_file, GetBasePath(), &files))
    {
//        wxFileName fname(GetPackagePath() + rec->local_file);
//        fname.SetExt("entry");
//        fname.SetName(rec->title);
//        CreateEntryFile(rec, fname.GetFullPath(), files);
        CreateEntryFile(rec, GetPackagePath() + rec->entry, files);
        wxMessageBox(_("DevPak installed"), _("Message"), wxICON_INFORMATION);

        // refresh installed_version
        rec->installed = true;
        rec->installed_version = rec->version;
        SetListColumnText(-1, 2, rec->installed_version);
    }
    else
    {
        wxMessageBox(_("DevPak was not installed.\nStatus:\n") + inst.GetStatus(), _("Error"), wxICON_ERROR);
    }
    UpdateStatus(_("Ready"), 0, 0);
}

void UpdateDlg::InstallMirrors(const wxString& file)
{
    wxLogNull ln;
    if (!wxCopyFile(file, GetMirrorsFilename(), true))
        wxMessageBox(_("Can't install mirrors file: ") + file, _("Error"), wxICON_ERROR);
    else
    {
        wxRemoveFile(file);
        FillServers();
        m_Net.SetServer(GetCurrentServer()); // update server based on mirrors
        wxMessageBox(_("Mirrors installed"), _("Information"), wxICON_INFORMATION);
    }
}

void UpdateDlg::UninstallFile()
{
    UpdateStatus(_("Please wait..."));
    UpdateRec* rec = GetRecFromListView();
    if (!rec)
    {
        wxMessageBox(_("No file selected!"), _("Error"), wxICON_ERROR);
        UpdateStatus(_("Ready"), 0, 0);
        return;
    }
    wxYield();

    DevPakInstaller inst;
    if (inst.Uninstall(GetPackagePath() + rec->entry))
    {
        wxMessageBox(_("DevPak uninstalled"), _("Message"), wxICON_INFORMATION);

        // refresh installed_version
        rec->installed_version.Clear();
        rec->installed = false;
        SetListColumnText(-1, 2, rec->installed_version);
    }
    else
    {
        wxMessageBox(_("DevPak was not uninstalled.\nStatus:\n") + inst.GetStatus(), _("Error"), wxICON_ERROR);
    }
}

void UpdateDlg::CreateEntryFile(UpdateRec* rec, const wxString& filename, const wxArrayString& files)
{
    wxString entry;
    entry << _T("[Setup]\n");
    entry << _T("AppName=") << rec->name << '\n';
    entry << _T("AppVersion=") << rec->version << '\n';
//    entry << _T("Description=") << rec->name << '\n';
    entry << _T('\n');

    entry << _T("[Files]\n");
    for (unsigned int i = 0; i < files.GetCount(); ++i)
    {
        entry << files[i] << _T('\n');
    }

    wxFile f(filename, wxFile::write);
    if (f.IsOpened())
    {
        f.Write(entry.c_str(), entry.Length());
    }
}

void UpdateDlg::OnFileRightClick(wxListEvent& event)
{
//    LOGSTREAM << "pt.x=" << event.GetPoint().x << ", pt.y=" << event.GetPoint().y << '\n';
    UpdateRec* rec = GetRecFromListView();
    if (!rec)
        return;

    wxMenu popup;
    popup.Append(idPopupDownloadAndInstall, _("Download && install"));
    popup.AppendSeparator();
    popup.Append(idPopupDownload, _("Download"));
    popup.Append(idPopupInstall, _("Install"));
    popup.AppendSeparator();
    popup.Append(idPopupUninstall, _("Uninstall"));

    bool canDl = !rec->downloaded || rec->version != rec->installed_version;
    bool canInst = rec->downloaded && (!rec->installed || rec->version != rec->installed_version);

    popup.Enable(idPopupDownload, canDl);
    popup.Enable(idPopupInstall, canInst);
    popup.Enable(idPopupDownloadAndInstall, canInst || canDl);
    popup.Enable(idPopupUninstall, rec->installed);

    wxListCtrl* lst = XRCCTRL(*this, "lvFiles", wxListCtrl);
    lst->PopupMenu(&popup, event.GetPoint());
}

void UpdateDlg::OnFileDeSelected(wxListEvent& event)
{
    wxListItem id;
    FillFileDetails(id);
    EnableButtons();
}

void UpdateDlg::OnFileSelected(wxListEvent& event)
{
    FillFileDetails(event.GetItem());
    EnableButtons();
}

void UpdateDlg::OnTreeSelChanged(wxTreeEvent& event)
{
    FillFiles(event.GetItem());
    EnableButtons();
}

void UpdateDlg::OnDownload(wxCommandEvent& event)
{
    DownloadFile(true);
}

void UpdateDlg::OnInstall(wxCommandEvent& event)
{
    InstallFile();
}

void UpdateDlg::OnUninstall(wxCommandEvent& event)
{
    UninstallFile();
}

void UpdateDlg::OnDownloadAndInstall(wxCommandEvent& event)
{
    DownloadFile();
}

void UpdateDlg::OnServerChange(wxCommandEvent& event)
{
    InternetUpdate();
}

void UpdateDlg::OnFilterChange(wxCommandEvent& event)
{
    ApplyFilter();
}

void UpdateDlg::OnConnect(wxCommandEvent& event)
{
    XRCCTRL(*this, "wxID_CANCEL", wxButton)->SetLabel(_("Abort"));
    EnableButtons();
}

void UpdateDlg::OnDisConnect(wxCommandEvent& event)
{
    XRCCTRL(*this, "wxID_CANCEL", wxButton)->SetLabel(_("Close"));
    EnableButtons();
}

void UpdateDlg::OnProgress(wxCommandEvent& event)
{
    int prg = -1;
    if (m_CurrFileSize != 0)
        prg = event.GetInt() * 100 / m_CurrFileSize;
    UpdateStatus(_("Downloading: ") + event.GetString(), prg);

    wxStaticText* lbl = XRCCTRL(*this, "lblProgress", wxStaticText);

    wxString msg;
    msg.Printf(_("%s of %s"), GetSizeString(event.GetInt()).c_str(), GetSizeString(m_CurrFileSize).c_str());
    lbl->SetLabel(msg);
}

void UpdateDlg::OnAborted(wxCommandEvent& event)
{
    UpdateStatus(_("Download aborted: ") + event.GetString(), 0, 0);
    XRCCTRL(*this, "lblProgress", wxStaticText)->SetLabel(_T(""));
    m_LastBlockSize = 0;
}

void UpdateDlg::OnDownloadStarted(wxCommandEvent& event)
{
    m_CurrFileSize = event.GetInt();
    UpdateStatus(_("Download started: ") + event.GetString(), 0, 100);
    XRCCTRL(*this, "lblProgress", wxStaticText)->SetLabel(_T(""));
    m_LastBlockSize = 0;
}

void UpdateDlg::OnDownloadEnded(wxCommandEvent& event)
{
    UpdateStatus(_("Download finished: ") + event.GetString());
    XRCCTRL(*this, "lblProgress", wxStaticText)->SetLabel(_T(""));
    m_LastBlockSize = 0;

    if (m_HasUpdated && event.GetInt() == 0)
    {
        UpdateRec* rec = GetRecFromListView();
        if (rec)
        {
            if (rec->bytes != m_CurrFileSize)
                wxMessageBox(_("File size mismatch for ") + event.GetString() + _("!\n\n"
                            "This, usually, means one of three things:\n"
                            "1) The reported size in the update list is wrong. The DevPak might still be valid.\n"
                            "2) The file's location returned a web error-page. Invalid DevPak...\n"
                            "3) The file is corrupt...\n\n"
                            "You can try to install it anyway. If it is not a valid DevPak, the operation will fail."),
                            _("Warning"), wxICON_WARNING);
        }
        if (rec && rec->installable && wxMessageBox(_("Do you want to install ") + event.GetString() + _(" now?"), _("Confirmation"), wxICON_QUESTION | wxYES_NO) == wxYES)
            InstallFile();
        else if (rec && rec->title == _T("WebUpdate Mirrors list"))
            InstallMirrors(GetPackagePath() + rec->local_file);
    }
    m_CurrFileSize = 0;
}

void UpdateDlg::OnUpdateUI(wxUpdateUIEvent& event)
{
    // hack to display the download message *after* the dialog has been shown...
    if (m_FirstTimeCheck)
    {
        m_FirstTimeCheck = false; // no more, just once
        wxString config = GetConfFilename();
        if (wxFileExists(config))
            InternetUpdate();
        else
        {
            if (wxMessageBox(_("A list of updates needs to be downloaded.\nDo you want to do this now?"), _("Confirmation"), wxICON_QUESTION | wxYES_NO) == wxYES)
                InternetUpdate(true);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -