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

📄 configwizard.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU 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 General Public License for more details.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <KCeasy.h>
#pragma hdrstop

#include "ConfigWizard.h"
#include "ShareSelectDialog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TConfigWizardForm *ConfigWizardForm;
//---------------------------------------------------------------------------

static const struct
{
    char *Name;
    int Down;     // in kbit/sec
    int Up;
} ConnectionTypes[] =
{
    { "56k Modem",               56,    56    },
    { "ISDN",                    64,    64    },
    { "ISDN 2x",                 128,   128   },
    { "DSL",                     256,   128   },
    { "DSL",                     512,   128   },
    { "DSL",                     512,   256   },
    { "DSL (T-DSL, 1&1-DSL)",    768,   128   },
    { "DSL (QDSL, NGI)",         1024,  256   },
    { "DSL (TDSL 1500)",         1500,  192   },
    { "DSL 1600",                1600,  90    },
    { "DSL 2000",                2000,  300   },
    { "DSL",                     2000,  786   },
    { "Cable",                   187,   32    },
    { "Cable",                   187,   64    },
    { "T1",                      1500,  1500  },
    { "T3+",                     32000, 32000 },
    { NULL, 0, 0 }
};

// TODO: localize

static const struct
{
    char *Name;
    char *Descr;
} PluginDescr[] =
{
    { "OpenFT", "A new Open Source network developed by the giFT project." },
    { "FastTrack", "Provides access to the network used by Kazaa, Grokster and iMesh." },
    { "Gnutella", "The public Gnutella network." },
    { "Ares", "A network with over one million users. Good for music and other small files." },
    { NULL, NULL }
};

//---------------------------------------------------------------------------
__fastcall TConfigWizardForm::TConfigWizardForm(TComponent* Owner,
                                                TGiftLauncher* NLauncher,
                                                bool FirstInstall)
:   TForm(Owner),
    Launcher(NLauncher),
    MustReboot(false),
    NeedWin98Patch(false)
{
    TranslateComponent(this);
    Caption = Brand->ReplaceAppName(Caption);
    WelcomeStatic->Caption = Brand->ReplaceAppName(WelcomeStatic->Caption);
    AppLogoImage->Picture = Brand->GetLogo48White();
    WelcomeStatic1->Caption = Brand->ReplaceAppName(WelcomeStatic1->Caption);
    WelcomeStatic2->Caption = Brand->ReplaceAppName(WelcomeStatic2->Caption);
    ConnectionStatic1->Caption = Brand->ReplaceAppName(ConnectionStatic1->Caption);
    Win98PatchStatic1->Caption = Brand->ReplaceAppName(Win98PatchStatic1->Caption);
    Win98PatchStatic2->Caption = Brand->ReplaceAppName(Win98PatchStatic2->Caption);
    NetworksStatic1->Caption = Brand->ReplaceAppName(NetworksStatic1->Caption);
    NodeFileUpdateStatic->Caption = Brand->ReplaceAppName(NodeFileUpdateStatic->Caption);
    FinishedStatic2->Caption = Brand->ReplaceAppName(FinishedStatic2->Caption);

    NodeFileUpdateStatic->Visible = FirstInstall;

    // make all tabs invisible
    for(int i=0; i<PageCtrl->PageCount; i++)
        PageCtrl->Pages[i]->TabVisible = false;

    // init pages
    InitConnectionPage();
    InitWin98PatchPage();
    InitSharedFoldersPage();
    InitNetworksPage();

    // disable cancel if requested
    CancelBtn->Enabled = !FirstInstall;

    // show the first page
    PageCtrl->ActivePageIndex = 0;
    // disable "Back" button for first page
    BackBtn->Enabled = (PageCtrl->ActivePageIndex != 0);
}
//---------------------------------------------------------------------------
// page management

void __fastcall TConfigWizardForm::NextBtnClick(TObject *Sender)
{
    if(PageCtrl->ActivePageIndex < PageCtrl->PageCount - 1) {
//        PageCtrl->SelectNextPage(true, false);
        TTabSheet* Next = PageCtrl->FindNextPage(PageCtrl->ActivePage,true,false);
        while((Next == Win98PatchSheet && !NeedWin98Patch))
        {
            Next = PageCtrl->FindNextPage(Next,true,false);
        }
        PageCtrl->ActivePage = Next;
        PageCtrlChange(NULL); // not triggered automatically if ActivePage is changed
        return;
    }

    // finished
    FinishConnectionPage();
    FinishWin98PatchPage();
    FinishSharedFoldersPage();
    FinishNetworksPage();

    // set up and downstream limits based on connection info
    int Downstream = Config->GetValueInt("gift/max_downstream");
    int Upstream = Config->GetValueInt("gift/max_upstream");
    // stay 4 kb/s below max
    Upstream = (Upstream < 4096) ? 4096 : Upstream - 4096;
    Downstream = (Downstream < 4096) ? 4096 : Downstream - 4096;
    // save to giftd.conf
    Launcher->GetGiftConf()->SetValueInt("bandwidth/downstream", Downstream);
    Launcher->GetGiftConf()->SetValueInt("bandwidth/upstream", Upstream);
    Launcher->GetGiftConf()->Save();

    // return mrOK
    ModalResult = mrOk;
}

void __fastcall TConfigWizardForm::BackBtnClick(TObject *Sender)
{
//    PageCtrl->SelectNextPage(false, false);
    TTabSheet* Next = PageCtrl->FindNextPage(PageCtrl->ActivePage,false,false);
    while((Next == Win98PatchSheet && !NeedWin98Patch))
    {
        Next = PageCtrl->FindNextPage(Next,false,false);
    }
    PageCtrl->ActivePage = Next;
    PageCtrlChange(NULL); // not triggered automatically if ActivePage is changed
    return;
}

void __fastcall TConfigWizardForm::PageCtrlChange(TObject *Sender)
{
    // change caption "Next" button to "Finish" for last page
    if(PageCtrl->ActivePageIndex == PageCtrl->PageCount - 1) {
        // TRANSLATOR: Config wizard button caption for last page.
        NextBtn->Caption = _("Finish");
        // show 'must reboot' message if necessary
        MustReboot = Win98PatchChk->Checked;
        MustRebootStatic->Visible = MustReboot;
    } else
        // TRANSLATOR: Config wizard button caption for next page.
        NextBtn->Caption = _("Next >");

    // focus "Next" button
    NextBtn->SetFocus();

    // disable "Back" button for first page
    BackBtn->Enabled = (PageCtrl->ActivePageIndex != 0);
}
//---------------------------------------------------------------------------

void __fastcall TConfigWizardForm::EditOnlyNumericKeyPress(TObject *Sender,
      char &Key)
{
    // allow only numeric and backspace
    if((Key < '0' || Key > '9') && Key != 8)
        Key = 0;
}
//---------------------------------------------------------------------------
// connection page

bool TConfigWizardForm::InitConnectionPage()
{
    ConnectionListView->Items->Clear();
    TListItem* Item = ConnectionListView->Items->Add();
    // TRANSLATOR: Config wizard custom connection speed.
    Item->Caption = _("Custom");
    // TRANSLATOR: Config wizard custom connection speed.
    Item->SubItems->Add(_("(specify)"));
    // TRANSLATOR: Config wizard custom connection speed.
    Item->SubItems->Add(_("(specify)"));

    for(int i=0; ConnectionTypes[i].Name; i++) {
        TListItem* Item = ConnectionListView->Items->Add();
        Item->Caption = ConnectionTypes[i].Name;
        Item->SubItems->Add(IntToStr(ConnectionTypes[i].Down));
        Item->SubItems->Add(IntToStr(ConnectionTypes[i].Up));
    }

    // select previously configured b/w
    ConnectionListView->ItemIndex = 0;
    DownstreamUpDown->Position = Config->GetValueInt("gift/max_downstream") * 8 / 1024;
    UpstreamUpDown->Position = Config->GetValueInt("gift/max_upstream") * 8 / 1024;

    for(int i=0; i<ConnectionListView->Items->Count; i++) {
        if(string_to_int(ConnectionListView->Items->Item[i]->SubItems->Strings[0].c_str()) == DownstreamUpDown->Position &&
           string_to_int(ConnectionListView->Items->Item[i]->SubItems->Strings[1].c_str()) == UpstreamUpDown->Position)
        {
            ConnectionListView->ItemIndex = i;
            break;
        }
    }

    return true;
}

bool TConfigWizardForm::FinishConnectionPage()
{
    // write connection b/w to kceasy.conf
    Config->SetValueInt("gift/max_downstream", DownstreamUpDown->Position * 1024 / 8);
    Config->SetValueInt("gift/max_upstream", UpstreamUpDown->Position * 1024 / 8);
    Config->Save();

    return true;
}

void __fastcall TConfigWizardForm::ConnectionListViewSelectItem(
      TObject *Sender, TListItem *Item, bool Selected)
{
    if(!Selected)
        return;

    // TRANSLATOR: Config wizard custom connection speed.
    if(Item->Caption == _("Custom")) {
        DownstreamEdit->Enabled = true;
        DownstreamUpDown->Enabled = true;
        UpstreamEdit->Enabled = true;
        UpstreamUpDown->Enabled = true;
    } else {
        DownstreamEdit->Enabled = false;
        DownstreamUpDown->Enabled = false;
        UpstreamEdit->Enabled = false;
        UpstreamUpDown->Enabled = false;

        // set edit fields to selected value
        DownstreamUpDown->Position = StrToInt(Item->SubItems->Strings[0]);
        UpstreamUpDown->Position = StrToInt(Item->SubItems->Strings[1]);
    }
}
//---------------------------------------------------------------------------
// win98 patch page

bool TConfigWizardForm::InitWin98PatchPage()
{
    // check if we need to change connection limit
    if(RunningWindowsNT())
        return true;

    char* Val = ReadRegKey("HKLM\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections");

    if(Val == NULL || _atoi64(Val) < 512) {
        NeedWin98Patch = true;
        Win98PatchChk->Checked = NeedWin98Patch;
    }

    free(Val);
    return true;
}

bool TConfigWizardForm::FinishWin98PatchPage()
{
    // write new limit if requested
    if(!Win98PatchChk->Checked)
        return true;

    OSVERSIONINFO osvi;
    memset (&osvi, 0, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    if(!GetVersionEx(&osvi) || osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
        return false;

    // win95 requires DWORD, others string
#if 0 // apparently only very old versions of win95 use DWORD, on others it leads to problems
    if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
        return WriteRegKeyDWORD("HKLM\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections",512);
    else
#endif
        return WriteRegKey("HKLM\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections","512");
}
//---------------------------------------------------------------------------
// shared folders page

bool TConfigWizardForm::InitSharedFoldersPage()
{
    // load shared folders from giftd.conf
    string root = Launcher->GetGiftConf()->GetValue("sharing/root");
    SharedFolders = string_split(root,":");

    // add folders to list view
    SharedFoldersListView->Items->Clear();
    for(list<string>::iterator itr=SharedFolders.begin(); itr!=SharedFolders.end(); ++itr) {
        (*itr) = UnixToWindowsPath(*itr);
        TListItem* Item = SharedFoldersListView->Items->Add();
        Item->Caption = (*itr).c_str();
        Item->ImageIndex = MainForm->IconManager->GetFolderIconIndex();
    }

    return true;
}

bool TConfigWizardForm::FinishSharedFoldersPage()
{
    // write shared folders to giftd.conf
    for(list<string>::iterator itr=SharedFolders.begin(); itr!=SharedFolders.end(); ++itr)
        (*itr) = WindowsToUnixPath(*itr);
    string root = string_join(SharedFolders,":");

    Launcher->GetGiftConf()->SetValue("sharing/root",root.c_str());
    Launcher->GetGiftConf()->Save();

    return true;
}

void __fastcall TConfigWizardForm::ShareSelectBtnClick(TObject *Sender)
{
    TShareSelectForm* ShareSelectDlg = new TShareSelectForm(MainForm,SharedFolders);

    if(ShareSelectDlg->ShowModal() == mrOk) {
        SharedFolders = ShareSelectDlg->GetFolders();
        // update list view
        SharedFoldersListView->Items->Clear();
        for(list<string>::iterator itr=SharedFolders.begin(); itr!=SharedFolders.end(); ++itr) {
            TListItem* Item = SharedFoldersListView->Items->Add();
            Item->Caption = (*itr).c_str();
            Item->ImageIndex = MainForm->IconManager->GetFolderIconIndex();
        }
    }

    delete ShareSelectDlg;
}
//---------------------------------------------------------------------------
// networks page

bool TConfigWizardForm::InitNetworksPage()
{
    // get loaded plugins
    string plugins = Launcher->GetGiftConf()->GetValue("main/plugins");
    LoadedPlugins = string_split(plugins,":");

    // get available plugins
    WIN32_FIND_DATA FindData;
    HANDLE hFind;

    NetworksListView->Items->Clear();
    string Mask = Launcher->GetPluginPath() + "\\*.dll";

    if((hFind = FindFirstFile(Mask.c_str(),&FindData)) == INVALID_HANDLE_VALUE)
        return false;

    do {
        string Plugin = FindData.cFileName;
        Plugin = Plugin.substr(0, Plugin.rfind('.'));

        TListItem* Item = NetworksListView->Items->Add();
        Item->Caption = Plugin.c_str();
        Item->ImageIndex = MainForm->IconManager->GetNetworkIconIndex(Plugin);

        // find description for plugim
        for(int i=0; PluginDescr[i].Name; i++) {
            if(Plugin == PluginDescr[i].Name) {
                Item->SubItems->Add(PluginDescr[i].Descr);
                break;
            }
        }

        // check loaded plugins
        if(find(LoadedPlugins.begin(),LoadedPlugins.end(),Plugin) != LoadedPlugins.end())
            Item->Checked = true;

    } while(FindNextFile(hFind,&FindData));

    FindClose(hFind);

    return true;
}

bool TConfigWizardForm::FinishNetworksPage()
{
    // write shared folders to giftd.conf
    string plugins = string_join(LoadedPlugins,":");

    Launcher->GetGiftConf()->SetValue("main/plugins",plugins.c_str());
    Launcher->GetGiftConf()->Save();

    return true;
}

void __fastcall TConfigWizardForm::NetworksListViewClick(TObject *Sender)
{
    LoadedPlugins.clear();
    for(int i=0; i<NetworksListView->Items->Count; i++) {
        if(NetworksListView->Items->Item[i]->Checked) {
            string plugin = NetworksListView->Items->Item[i]->Caption.c_str();
            plugin = plugin.substr(0,plugin.find(' '));
            LoadedPlugins.push_back(plugin);
        }
    }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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