static path relative_to(const path &from, const path &to, bool fromdir=true) { if(PathIsSameRoot(from.buf, to.buf)) { path p; if(!PathRelativePathTo(p.buf, from.buf, fromdir?FILE_ATTRIBUTE_DIRECTORY:0, to.buf, 0)) { TCHAR chBuf[1024]; swprintf_s(chBuf, _countof(chBuf), L"PathRelativePathTo() from:[%s] to:[%s] failed!!", from.buf, to.buf); TRACEERR("[path] [relative_to]", chBuf, GetLastError()); throw path_error("PathRelativePathTo"); } return p; } else if(to.has_root()) return to; else { TRACEE("[path] [relative_to] * ERROR: unable to create relative path: paths have different roots!!"); throw std::runtime_error("unable to create relative path: paths have different roots"); } }
static void EditList_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch(id) { case IDC_FILE: case IDC_URL: if(codeNotify==EN_UPDATE || codeNotify==CBN_EDITUPDATE || codeNotify==CBN_SELCHANGE) EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(hwndCtl)>0); break; case IDC_ADDFILE: EnableWindow(GetDlgItem(hwnd, IDC_URL), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_FILE), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_BROWSE), TRUE); EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(GetDlgItem(hwnd, IDC_FILE))>0); break; case IDC_ADDURL: EnableWindow(GetDlgItem(hwnd, IDC_FILE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_BROWSE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_URL), TRUE); EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(GetDlgItem(hwnd, IDC_URL))>0); break; case IDC_BROWSE: { TCHAR file[MAX_PATH]={0}; OPENFILENAME ofn={0}; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hwnd; ofn.lpstrFilter=_T("PeerBlock Lists (*.p2p; *.p2b; *.txt)\0*.p2p;*.p2p;*.p2b;*.txt\0")_T("All Files (*.*)\0*.*\0"); ofn.lpstrFile=file; ofn.nMaxFile=MAX_PATH; ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST; if(GetOpenFileName(&ofn)) { path p=path::relative_to(path::base_dir(), file); SetDlgItemText(hwnd, IDC_FILE, p.c_str()); } } break; case IDOK: { List **list=(List**)(LONG_PTR)GetWindowLongPtr(hwnd, DWLP_USER); if(IsDlgButtonChecked(hwnd, IDC_ADDFILE)==BST_CHECKED) { const path file=GetDlgItemText(hwnd, IDC_FILE); if(!path::exists(file.has_root()?file:path::base_dir()/file)) { MessageBox(hwnd, IDS_INVALIDFILETEXT, IDS_INVALIDFILE, MB_ICONERROR|MB_OK); break; } StaticList *l; if(l=dynamic_cast<StaticList*>(*list)) if(l->File!=file) { l->File=file; g_ret|=LISTS_NEEDRELOAD; } else { delete *list; *list=l=new StaticList; l->File=file; g_ret|=LISTS_NEEDRELOAD; } } else { tstring url=GetDlgItemText(hwnd, IDC_URL); if(!(path(url).is_url())) { MessageBox(hwnd, IDS_INVALIDURLTEXT, IDS_INVALIDURL, MB_ICONERROR|MB_OK); break; } DynamicList *l; if(l=dynamic_cast<DynamicList*>(*list)) { if(l->Url!=url) { l->Url=url; g_ret|=LISTS_NEEDRELOAD; } } else { delete *list; *list=l=new DynamicList; l->Url=url; g_ret|=LISTS_NEEDRELOAD; } } List::ListType type=(IsDlgButtonChecked(hwnd, IDC_BLOCK)==BST_CHECKED)?List::Block:List::Allow; if((*list)->Type!=type) { (*list)->Type=type; g_ret|=LISTS_NEEDRELOAD; } (*list)->Description=GetDlgItemText(hwnd, IDC_DESCRIPTION); EndDialog(hwnd, g_ret); } break; case IDCANCEL: EndDialog(hwnd, 0); break; } }
static void AddList_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch(id) { case IDC_FILE: case IDC_URL: if(codeNotify==EN_UPDATE || codeNotify==CBN_EDITUPDATE || codeNotify==CBN_SELCHANGE) EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(hwndCtl)>0); break; case IDC_ADDFILE: EnableWindow(GetDlgItem(hwnd, IDC_URL), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_FILE), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_BROWSE), TRUE); EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(GetDlgItem(hwnd, IDC_FILE))>0); break; case IDC_ADDURL: EnableWindow(GetDlgItem(hwnd, IDC_FILE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_BROWSE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_URL), TRUE); EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(GetDlgItem(hwnd, IDC_URL))>0); break; case IDC_BROWSE: { TCHAR file[MAX_PATH]={0}; OPENFILENAME ofn={0}; ofn.lStructSize=sizeof(ofn); ofn.hwndOwner=hwnd; ofn.lpstrFilter=_T("PeerBlock Lists (*.p2p; *.p2b; *.txt)\0*.p2p;*.p2p;*.p2b;*.txt\0")_T("All Files (*.*)\0*.*\0"); ofn.lpstrFile=file; ofn.nMaxFile=MAX_PATH; ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST; if(GetOpenFileName(&ofn)) { path p=path::relative_to(path::base_dir(), file); SetDlgItemText(hwnd, IDC_FILE, p.c_str()); } } break; case IDOK: { List **list=(List**)(LONG_PTR)GetWindowLongPtr(hwnd, DWLP_USER); bool addedList = false; if(IsDlgButtonChecked(hwnd, IDC_ADDFILE)==BST_CHECKED) { // File-based list const path file=GetDlgItemText(hwnd, IDC_FILE); if(!path::exists(file.has_root()?file:path::base_dir()/file)) { MessageBox(hwnd, IDS_INVALIDFILETEXT, IDS_INVALIDFILE, MB_ICONERROR|MB_OK); break; } StaticList *l=new StaticList; l->File=file; *list=l; addedList = true; } else { TRACEI("[addlistproc] [AddList_OnCommand] sanity-checking URL-based list"); // URL-based list tstring url=GetDlgItemText(hwnd, IDC_URL); if(!(path(url).is_url())) { TRACEE("[addlistproc] [AddList_OnCommand] * ERROR: Not a valid URL!"); MessageBox(hwnd, IDS_INVALIDURLTEXT, IDS_INVALIDURL, MB_ICONERROR|MB_OK); break; } // sanity-check if (g_config.EnableListSanityChecking) url = AddList_SanityCheckUrl(url); if (!url.empty()) { // Finally, add this new list! TRACEI("[addlistproc] [AddList_OnCommand] adding new list"); DynamicList *l=new DynamicList; l->Url=url; *list=l; addedList = true; } else { TRACEI("[addlistproc] [AddList_OnCommand] not adding new list"); DynamicList *l=new DynamicList; l->Url=url; *list=l; } } if (addedList) { TRACEI("[addlistproc] [AddList_OnCommand] finishing list-add"); (*list)->Type=(IsDlgButtonChecked(hwnd, IDC_BLOCK)==BST_CHECKED)?List::Block:List::Allow; (*list)->Description=GetDlgItemText(hwnd, IDC_DESCRIPTION); } EndDialog(hwnd, IDOK); } break; // End of IDOK case IDCANCEL: EndDialog(hwnd, IDCANCEL); break; } } // End of AddList_OnCommand()