📄 gui.cpp
字号:
ext[len] = '\0'; Trut::Group *group = NULL; UINT item = m_ctlGroup.GetCurSel(); if (item == CB_ERR) return; group = (Trut::Group*) m_ctlGroup.GetItemDataPtr(item); g_app.trut->UpdateShared(shared, ext, group);}IMPLEMENT_DYNAMIC(CPrefSheet, CPropertySheet)CPrefSheet::CPrefSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage) :CPropertySheet(nIDCaption, pParentWnd, iSelectPage){}CPrefSheet::CPrefSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage) :CPropertySheet(pszCaption, pParentWnd, iSelectPage){}CPrefSheet::~CPrefSheet(){}BEGIN_MESSAGE_MAP(CPrefSheet, CPropertySheet) //{{AFX_MSG_MAP(CPrefSheet) //}}AFX_MSG_MAPEND_MESSAGE_MAP()BEGIN_MESSAGE_MAP(CTrutellaApp, CWinApp) //{{AFX_MSG_MAP(CTrutellaApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) ON_COMMAND(ID_FILE_PREFS, OnFilePrefs) //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)END_MESSAGE_MAP()CTrutellaApp::CTrutellaApp(){}BOOL CTrutellaApp::InitInstance(){ CWaitCursor c; m_InitFailed = FALSE; AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.#if 0#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL#else // Call this when linking to MFC statically Enable3dControlsStatic();#endif#endif // Change the registry key under which our settings are stored. SetRegistryKey(_T("Intel")); // Load standard INI file options (including MRU) LoadStdProfileSettings(0); // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CTrutellaDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CTrutellaView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) { m_InitFailed = TRUE; return FALSE; } // Create directories CreateDirectory("C:\\Ptl", NULL); CreateDirectory("C:\\Ptl\\Downloads", NULL); SetCurrentDirectory("C:\\Ptl\\Downloads"); // Load certificates store = new PTP::Store(HKEY_CURRENT_USER, "Software\\PTL\\Cert", NULL, NULL); if (store->Load()) { m_InitFailed = TRUE; return FALSE; } // Load identity settings PTP::Identity *local = store->Find(NULL, 1); if (local) m_IdFriendlyName = local->GetName(); trut = new Trut(store); // Start listening on the default port trut->SetOpenCallback(OpenCallback); trut->SetCloseCallback(CloseCallback); trut->AddPort(0); // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); ((CMainFrame *)m_pMainWnd)->GetActiveDocument()->UpdateAllViews(NULL); // Try to re-establish host connections // Load registry settings and restore connections UINT i, n; CString Name, Key; n = GetProfileInt("Hosts", "Count", 0); for(i = 0; i < n; i++) { Key.Format("%d", i); Name = GetProfileString("Hosts", Key, ""); if(Name != "") { SetStatusText("Connecting to " + Name + "..."); Trut::Host *host = trut->AddHost(Name); if(!host) { MessageBox(m_pMainWnd->m_hWnd, "Could not restore connection to " + Name + ".", "Trutella", MB_OK); } } } // Final aesthetics SetStatusText("Ready"); m_pMainWnd->SetFocus(); return TRUE;}int CTrutellaApp::ExitInstance() { if(!m_InitFailed) { WriteProfileInt("Hosts", "Count", hosts.size()); HostList::iterator hostPos; UINT i; for(hostPos = hosts.begin(), i = 0; hostPos != hosts.end(); hostPos++, i++) { CString key; key.Format("%d", i); WriteProfileString("Hosts", key, (*hostPos)->GetUrl()); } } delete trut; delete store; return CWinApp::ExitInstance();}void CTrutellaApp::OpenCallback(Trut::Host *host, void **context){ if (host->GetDirection() == PTP::Net::Connection::OUTBOUND) g_app.hosts.push_back(host);}void CTrutellaApp::CloseCallback(Trut::Host *host, void *context){ if (host->GetDirection() == PTP::Net::Connection::OUTBOUND) g_app.hosts.remove(host);}/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public: CAboutDlg();// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){ //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAPEND_MESSAGE_MAP()// App command to run the dialogvoid CTrutellaApp::OnAppAbout(){ CAboutDlg aboutDlg; aboutDlg.DoModal();}void CTrutellaApp::OnFilePrefs() { // Build and display the preferences dialog CPrefSheet dlg("Trutella Preferences"); CPrefConnectionPage pgConnections; CPrefSharing pgSharing; CPrefGroups pgGroups; dlg.AddPage(&pgConnections); dlg.AddPage(&pgSharing); dlg.AddPage(&pgGroups); if(dlg.DoModal() == IDOK) { // Get data from the dialog objects... } // Update the application window with changes CMainFrame *pMainFrame = (CMainFrame*)GetMainWnd(); pMainFrame->GetActiveDocument()->UpdateAllViews(NULL);}void CTrutellaApp::FormatIPAddr(UINT ip, CString &str){ str.Format("%d.%d.%d.%d", (ip & 0xff000000) >> 24, (ip & 0xff0000) >> 16, (ip & 0xff00) >> 8, ip & 0xff);}void CTrutellaApp::SetStatusText(CString s){ CMainFrame *pMainFrame = (CMainFrame *)AfxGetMainWnd(); if(pMainFrame) pMainFrame->SetStatusText(s);}IMPLEMENT_DYNCREATE(CTrutellaDoc, CDocument)BEGIN_MESSAGE_MAP(CTrutellaDoc, CDocument) //{{AFX_MSG_MAP(CTrutellaDoc) //}}AFX_MSG_MAPEND_MESSAGE_MAP()CTrutellaDoc::CTrutellaDoc(){ // TODO: add one-time construction code here}CTrutellaDoc::~CTrutellaDoc(){}BOOL CTrutellaDoc::OnNewDocument(){ if (!CDocument::OnNewDocument()) return FALSE; SetTitle(g_app.m_IdFriendlyName); return TRUE;}void CTrutellaDoc::Serialize(CArchive& ar){ if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here }}IMPLEMENT_DYNCREATE(CTrutellaView, CFormView)BEGIN_MESSAGE_MAP(CTrutellaView, CFormView) //{{AFX_MSG_MAP(CTrutellaView) ON_BN_CLICKED(IDC_SEARCH, OnSearch) ON_WM_DESTROY() ON_NOTIFY(NM_DBLCLK, IDC_SEARCHRESULTS, OnDblclkSearchresults) ON_BN_CLICKED(IDC_KILLXFER, OnKillxfer) ON_BN_CLICKED(IDC_REMOVEERRORS, OnRemoveerrors) //}}AFX_MSG_MAPEND_MESSAGE_MAP()CTrutellaView::CTrutellaView() : CFormView(CTrutellaView::IDD){ //{{AFX_DATA_INIT(CTrutellaView) m_SearchText = _T(""); m_nGroupID = -1; m_nXferMode = -1; //}}AFX_DATA_INIT // TODO: add construction code here}CTrutellaView::~CTrutellaView(){}void CTrutellaView::DoDataExchange(CDataExchange* pDX){ CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTrutellaView) DDX_Control(pDX, IDC_GROUP, m_ctlGroupCombo); DDX_Control(pDX, IDC_XFERSEL, m_ctlXferMode); DDX_Control(pDX, IDC_XFERS, m_ctlXferList); DDX_Control(pDX, IDC_SEARCHRESULTS, m_ctlSearchResults); DDX_Text(pDX, IDC_SEARCHTEXT, m_SearchText); DDX_CBIndex(pDX, IDC_GROUP, m_nGroupID); DDX_CBIndex(pDX, IDC_XFERSEL, m_nXferMode); //}}AFX_DATA_MAP}BOOL CTrutellaView::PreCreateWindow(CREATESTRUCT& cs){ // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs);}void CTrutellaView::OnInitialUpdate(){ CFormView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); // Set up list view columns DWORD dwStyle; dwStyle = m_ctlSearchResults.GetExtendedStyle(); dwStyle |= LVS_EX_FULLROWSELECT; m_ctlSearchResults.SetExtendedStyle(dwStyle); dwStyle = m_ctlXferList.GetExtendedStyle(); dwStyle |= LVS_EX_FULLROWSELECT; m_ctlXferList.SetExtendedStyle(dwStyle); m_ctlSearchResults.InsertColumn(0, "Path", LVCFMT_LEFT, 250); m_ctlSearchResults.InsertColumn(1, "Size", LVCFMT_LEFT, 100); m_ctlSearchResults.InsertColumn(2, "Group", LVCFMT_LEFT, 150); m_ctlXferList.InsertColumn(0, "Path", LVCFMT_LEFT, 250); m_ctlXferList.InsertColumn(1, "Transferred", LVCFMT_LEFT, 100); m_ctlXferList.InsertColumn(2, "Status", LVCFMT_LEFT, 150); m_ctlXferMode.SetCurSel(0); m_ctlXferMode.EnableWindow(FALSE);}void CTrutellaView::OnSearch() { UpdateData(TRUE); if(m_SearchText == "") return; // Remove current search results CleanSearchResults(); // Get group pointer referenced by current combobox item, // or NULL for the world. UINT nIndex = m_ctlGroupCombo.GetCurSel(); Trut::Group *group = NULL; if(nIndex != CB_ERR) group = (Trut::Group*)m_ctlGroupCombo.GetItemDataPtr(nIndex); g_app.trut->Search(m_SearchText, SearchCallback, &m_ctlSearchResults, group);}void CTrutellaView::SearchCallback(Trut::File *file, void *context){ static CCriticalSection lock; lock.Lock(); CListCtrl *results = (CListCtrl*) context; UINT item = results->GetItemCount(); results->InsertItem(LVIF_TEXT | LVIF_PARAM, item, file->GetName(), 0, 0, 0, (LPARAM) file); CString size; size.Format("%d", file->GetSize()); results->SetItem(item, 1, LVIF_TEXT, size, 0, 0, 0, 0); const char *name = (file->GetGroup() ? file->GetGroup()->GetName():"None (public)"); results->SetItem(item, 2, LVIF_TEXT, name, 0, 0, 0, 0); lock.Unlock();}void CTrutellaView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { GetDocument()->SetTitle(g_app.m_IdFriendlyName); m_ctlGroupCombo.ResetContent(); m_ctlGroupCombo.AddString("All of GnutellaNet"); m_ctlGroupCombo.SetItemDataPtr(0, NULL); CTrutellaApp::GroupList::iterator i; for(i = g_app.groups.begin(); i != g_app.groups.end(); i++) { Trut::Group *group = *i; int item = m_ctlGroupCombo.AddString(group->GetName()); m_ctlGroupCombo.SetItemDataPtr(item, group); } m_ctlGroupCombo.SetCurSel(m_ctlGroupCombo.GetCount() - 1);}void CTrutellaView::CleanSearchResults(){ for(int i = 0; i < (int) m_ctlSearchResults.GetItemCount(); i++) { Trut::File *file = (Trut::File*) m_ctlSearchResults.GetItemData(i); delete file; } m_ctlSearchResults.DeleteAllItems();}void CTrutellaView::OnDestroy() { CFormView::OnDestroy(); CleanSearchResults(); }void CTrutellaView::OnDblclkSearchresults(NMHDR* pNMHDR, LRESULT* pResult) { POSITION pos = m_ctlSearchResults.GetFirstSelectedItemPosition(); if (!pos) return; int selected = m_ctlSearchResults.GetNextSelectedItem(pos); Trut::File *file = (Trut::File *) m_ctlSearchResults.GetItemData(selected); if (!file) return; TransferCallback(file, Trut::GET_OK, NULL, 0, &m_ctlXferList); g_app.trut->Get(file, file->GetName(), TransferCallback, &m_ctlXferList); *pResult = 0;}void CTrutellaView::TransferCallback(Trut::File *file, Trut::GetStatus status, BYTE *data, unsigned long size, void *context){ if(!file || !context) return; static CCriticalSection lock; lock.Lock(); CListCtrl *list = (CListCtrl*)context; LVFINDINFO info; info.flags = LVFI_PARAM; info.lParam = (LPARAM)file; int item = list->FindItem(&info); if(item == -1) { // not found, add a new item item = list->InsertItem( LVIF_TEXT | LVIF_PARAM, list->GetItemCount(), file->GetName(), 0, 0, 0, (LPARAM) file); } // Update subitems CString strSize, strStat; strSize.Format("%d", size); switch(status) { case Trut::GET_OK: strStat = "Transferring"; break; case Trut::GET_DONE: strStat = "Done"; break; default: strStat = "Error"; break; } list->SetItem(item, 1, LVIF_TEXT, strSize, 0, 0, 0, 0); list->SetItem(item, 2, LVIF_TEXT, strStat, 0, 0, 0, 0); // Mark transfer as finished by disassociating it from the Trut::File if(status == Trut::GET_DONE || status == Trut::GET_ERROR) list->SetItemData(item, NULL); lock.Unlock();}void CTrutellaView::OnKillxfer() { POSITION pos = m_ctlXferList.GetFirstSelectedItemPosition(); if(!pos) { MessageBox("Please select the transfer you'd like to stop."); return; } int selected = m_ctlXferList.GetNextSelectedItem(pos); Trut::File *file = (Trut::File*)m_ctlXferList.GetItemData(selected); if(file) { g_app.trut->GetStop(file); m_ctlXferList.DeleteItem(selected); } else { MessageBox("This transfer has already completed."); }}void CTrutellaView::OnRemoveerrors() { CWaitCursor c; int nItem; for(nItem = m_ctlXferList.GetItemCount() - 1; nItem >= 0; nItem--) { Trut::File *file = (Trut::File*) m_ctlXferList.GetItemData(nItem); if(!file) m_ctlXferList.DeleteItem(nItem); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -