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

📄 kceasy.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
void __fastcall TMainForm::ConfigWizardMnuClick(TObject *Sender)
{
    if(!Engine->GiftInstalled() || Config->GetValueInt("gift/remote"))
        return;

    TConfigWizardForm* WizardDlg = new TConfigWizardForm(this,Engine->GetLauncher());

    if(WizardDlg->ShowModal() == mrOk) {
        Engine->GetGiftConf()->SetValueInt("main/setup",1);
        Engine->GetGiftConf()->Save();
        if(!Engine->IsOffline() && Engine->IsUsingLocalGift()) {
            // TRANSLATOR: message box when config wizard was used while connected
            MessageDlg(_("{AppName} is currently connected.\n") +
                       _("You will have to disconnect and reconnect in order for the changes to take effect!"),
                         mtInformation,TMsgDlgButtons() << mbOK,0);
        }
    }

    delete WizardDlg;
}

void __fastcall TMainForm::ExitMnuClick(TObject *Sender)
{
    // stop player if running so it can shut down while we terminate giFT
    PlayerForm->StopPlayback();
    // hide us
    MainForm->Hide();
    Engine->SetDebug(false);
    Engine->SetGiftLogging(false);

    // add uptime to config list
    if (Engine->GiftInstalled() && !Config->GetValueInt("gift/remote"))
    {
        list<string> UptimeList = string_split(Config->GetValue("gift/uptime"), ",");
        if(UptimeList.size() >= 20)
            UptimeList.resize(19);
        // add new values if a certain minimum uptime is met
        if (Engine->GetUptime() / 60 > 2)
            UptimeList.push_front(int_to_string(Engine->GetUptime() / 60)); // in minutes
        Config->SetValue("gift/uptime", string_join(UptimeList,","));
    }

    // save window position
    list<string> PosList;
    switch(MainForm->WindowState) {
    case wsNormal:    PosList.push_back("normal");    break;
    case wsMinimized: PosList.push_back("minimized"); break; // does not work for some reason
    case wsMaximized: PosList.push_back("maximized"); break;
    }
    PosList.push_back(int_to_string(MainForm->Left));
    PosList.push_back(int_to_string(MainForm->Top));
    PosList.push_back(int_to_string(MainForm->Width));
    PosList.push_back(int_to_string(MainForm->Height));
    Config->SetValue("gui/window_pos", string_join(PosList,","));

    ShuttingDown = true;
    Engine->TurnOffline();

    // Restore SP2 limit. This is a nop if RemoceSP2Limit was never called.
    RestoreSP2Limit();

    // if we are offline quit here otherwise do it in engine offline callback
    if(Engine->IsOffline())
        Application->Terminate(); // posts quite message
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::RemoveSharesDbMnuClick(TObject *Sender)
{
    if(!Engine->GiftInstalled() || Config->GetValueInt("gift/remote"))
        return;

    if(Engine->IsOnline()) {
        // TRANSLATOR: message box when user tries to delete shares database
        MessageDlg(_("{AppName} must be disconnected to delete the Shares Database."),
                   mtError,TMsgDlgButtons() << mbOK,0);
        return;
    }

    string DbPath = Engine->GetLauncher()->GetConfPath() + "\\shares";

    if(::GetFileAttributes(DbPath.c_str()) == 0xFFFFFFFF) {
        // TRANSLATOR: message box when user tries to delete shares database
        MessageDlg(_("The Shares Database has not been created yet or was already deleted."),
                   mtError,TMsgDlgButtons() << mbOK,0);
        return;
    }

    // TRANSLATOR: message box when user tries to delete shares database
    if(MessageDlg(_("You should only remove the Shares Database as a last measure when it has become corrupted.\n") +
                  _("Are you sure you want to remove it now?"),
                  mtConfirmation,TMsgDlgButtons() <<mbYes << mbNo,0) != mrYes)
    {
        return;
    }

    if(!::DeleteFile(DbPath.c_str())) {
        // TRANSLATOR: message box when user tries to delete shares database but it fails
        MessageDlg(_("Failed to remove Shares Database!"),
                   mtError,TMsgDlgButtons() << mbOK,0);
        return;
    }

    // TRANSLATOR: message box when user successfully deleted the shares database
    MessageDlg(_("The Shares Database has been successfully removed.\n") +
               _("It will be recreated next time you connect."),
                 mtInformation,TMsgDlgButtons() << mbOK,0);
}

void __fastcall TMainForm::UpdateBanlistsMnuClick(TObject *Sender)
{
    if(!Engine->GiftInstalled() || Config->GetValueInt("gift/remote"))
        return;

    if(Engine->IsOnline()) {
        // TRANSLATOR: message box when user tries to update banlists while connected
        MessageDlg(_("{AppName} must be disconnected to update the banlists and node files."),
                   mtError,TMsgDlgButtons() << mbOK,0);
        return;
    }

    TBanlistUpdateForm* BanlistDialog = new TBanlistUpdateForm(this, false);
    BanlistDialog->ShowModal();
    delete BanlistDialog;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::OnlineHelpMnuClick(TObject *Sender)
{
    // open kceasy docs page in browser
    BrowserForm->NavigateTo(Brand->GetOnlineHelpUrl().c_str());
    // switch to browser page
    SetVisiblePage(BrowserForm);
}

void __fastcall TMainForm::CheckForUpdatesMnuClick(TObject *Sender)
{
    new TAutoUpdate(Application, false);

    FILETIME ft;
    LARGE_INTEGER Now;
    ::GetSystemTimeAsFileTime (&ft);
    Now.LowPart = ft.dwLowDateTime;
    Now.HighPart = ft.dwHighDateTime;
    Now.QuadPart /= 10 * 1000 * 1000; // make secs from 100 nsecs
    Now.QuadPart /= 60 * 60; // make hours from secs
    Config->SetValueInt("gui/update_last_check", (int)Now.LowPart);
}

void __fastcall TMainForm::AboutMnuClick(TObject *Sender)
{
    TAboutForm* AboutBox = new TAboutForm(this);
    AboutBox->ShowModal();
    delete AboutBox;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::TrayHideMnuClick(TObject *Sender)
{
    TrayIcon->HideApp();
}

void __fastcall TMainForm::TrayShowMnuClick(TObject *Sender)
{
    TrayIcon->RestoreApp();
}

void __fastcall TMainForm::ShowMsgHandler(TMessage &Message)
{
    if(TrayIcon)// && TrayIcon->IsHidden())
        TrayIcon->RestoreApp();
    else {
        Application->Restore();
        if (Screen->ActiveForm && Screen->ActiveForm->Handle)
            SetForegroundWindow(Screen->ActiveForm->Handle);
    }
}

void __fastcall TMainForm::ShutdownMsgHandler(TMessage &Message)
{
    ExitMnuClick(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::PlayProgressBarMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(Button != mbLeft)
        return;
    PlayerForm->SeekDrag((double)X / PlayProgressBar->ClientWidth);
}

void __fastcall TMainForm::PlayProgressBarMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    if(!PlayerForm->SeekIsDragging())
        return;
    double Pos = (double)X / PlayProgressBar->ClientWidth;
    if(Pos < 0) Pos = 0;
    if(Pos > 1) Pos = 1;
    PlayerForm->SeekDrag(Pos);
}

void __fastcall TMainForm::PlayProgressBarMouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(Button != mbLeft)
        return;
    double Pos = (double)X / PlayProgressBar->ClientWidth;
    if(Pos < 0) Pos = 0;
    if(Pos > 1) Pos = 1;
    PlayerForm->SeekSet(Pos,true);
}
//---------------------------------------------------------------------------
// private

void TMainForm::RemoveSP2Limit()
{
    TTcpIpPatcher Patcher;

    if(PreviousSP2Limit != 0 || Config->GetValueInt("gui/use_tcpip_patcher") == 0)
        return;

    if(!Patcher.NeedsPatching())
        return;

    if(Config->GetValueInt("gui/use_tcpip_patcher") == -1) {
        // tell user what we are going to do from now on
        // TRANSLATOR: message box when first using SP2 limit patcher
        AnsiString Msg = AnsiString::Format(_("It has been detected that your system has the half open connections limit introduced by Windows XP Service Pack 2.\n") +
                                            _("{AppName} will raise this limit to %d while running. On exit the limit will be reset to the previous value.\n") +
                                            _("You can disable this behaviour in the config if you have specific reasons to do so."),
                                            ARRAYOFCONST(((int)TCPIP_PATCHER_LIMIT)));
        MessageDlg(Msg,mtInformation,TMsgDlgButtons() << mbOK,0);
        // only show this notice once
        Config->SetValueInt("gui/use_tcpip_patcher",1);
    }

    if(Patcher.RunningInWow64()) {
        // TRANSLATOR: message box when SP2 limit is detected on 64 bit system
        MessageDlg(_("{AppName} cannot change the half open connections limit on 64 bit systems.\n") +
                   _("Please see http://www.kceasy.com/docs/ on how to change the limit manually."),
                     mtInformation,TMsgDlgButtons() << mbOK,0);
        // don't make further patch attempts
        Config->SetValueInt("gui/use_tcpip_patcher",0);
        return;
    }

    if(!Patcher.LoadDriver()) {
        // TRANSLATOR: message box when driver for changing SP2 limit could not be loaded
        MessageDlg(_("{AppName} could not load the device driver for changing the connection limit.\n") +
                   _("This may be because {AppName} is not running as Administrator.\n") +
                   _("Please see http://www.kceasy.com/docs/ on how to change the limit manually."),
                     mtInformation,TMsgDlgButtons() << mbOK,0);
        // don't make further patch attempts
        Config->SetValueInt("gui/use_tcpip_patcher",0);
        return;
    }

    unsigned int Limit = Patcher.GetLimit();
    if(Limit == 0) {
        // TRANSLATOR: message box when SP2 limit could not be read
        AnsiString Msg = AnsiString::Format(_("{AppName} could not read the current connection limit.\n") +
                                            _("This may be because your tcpip.sys is not supported.\n") +
                                            _("Please report this problem to %s."),
                                            ARRAYOFCONST((BRAND_SUPPORT_EMAIL)));
        MessageDlg(Msg,mtInformation,TMsgDlgButtons() << mbOK,0);
        // don't make further patch attempts
        Config->SetValueInt("gui/use_tcpip_patcher",0);
    } else if(Limit < TCPIP_PATCHER_LIMIT) {
        // save old limit
        PreviousSP2Limit = Limit;
        // set new limit
        if(!Patcher.SetLimit(TCPIP_PATCHER_LIMIT)) {
            // TRANSLATOR: message box when SP2 limit could not be written
            AnsiString Msg = AnsiString::Format(_("{AppName} could not write the new connection limit.\n") +
                                                _("This may be because your tcpip.sys is not supported.\n") +
                                                _("Please report this problem to %s."),
                                                ARRAYOFCONST((BRAND_SUPPORT_EMAIL)));
            MessageDlg(Msg,mtInformation,TMsgDlgButtons() << mbOK,0);
            // don't make further patch attempts
            Config->SetValueInt("gui/use_tcpip_patcher",0);
        }
    }

    // save driver log to disk
    string LogPath = Application->ExeName.c_str();
    LogPath.erase(LogPath.rfind('\\'));
    LogPath += TCPIP_PATCHER_LOG_PATH;
    FILE* fp;
    if((fp = fopen(LogPath.c_str(),"w"))) {
        fprintf(fp,Patcher.GetDriverLog().c_str());
        fclose(fp);
    }

    Patcher.UnloadDriver();
    return;
}

void TMainForm::RestoreSP2Limit()
{
    TTcpIpPatcher Patcher;

    if(PreviousSP2Limit == 0 || Config->GetValueInt("gui/use_tcpip_patcher") == 0)
        return;

    if(!Patcher.LoadDriver())
        return;

    // reset limit
    if(!Patcher.SetLimit(PreviousSP2Limit)) {
        // TRANSLATOR: message box when SP2 limit could not be restored
        AnsiString Msg = AnsiString::Format(_("{AppName} could not restore the previous connection limit.\n") +
                                            _("Please report this problem to %s."),
                                            ARRAYOFCONST((BRAND_SUPPORT_EMAIL)));
        MessageDlg(Msg,mtInformation,TMsgDlgButtons() << mbOK,0);
    }

    // save driver log to disk
    string LogPath = Application->ExeName.c_str();
    LogPath.erase(LogPath.rfind('\\'));
    LogPath += TCPIP_PATCHER_LOG_PATH;
    FILE* fp;
    if((fp = fopen(LogPath.c_str(),"a"))) {
        fprintf(fp,Patcher.GetDriverLog().c_str());
        fclose(fp);
    }

    Patcher.UnloadDriver();
    return;
}
//---------------------------------------------------------------------------

// global _() which does branding after translating
AnsiString _(const AnsiString Str)
{
    return Brand->ReplaceAppName(gettext(Str));
}
//-----------------------------------------------------------------------

⌨️ 快捷键说明

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