PPgDirectories.cpp
Upload User: hdq0802
Upload Date: 2014-08-06
Package Size: 5971k
Code Size: 10k
Category:

P2P

Development Platform:

Visual C++

  1. //this file is part of eMule
  2. //Copyright (C)2002-2006 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
  3. //
  4. //This program is free software; you can redistribute it and/or
  5. //modify it under the terms of the GNU General Public License
  6. //as published by the Free Software Foundation; either
  7. //version 2 of the License, or (at your option) any later version.
  8. //
  9. //This program is distributed in the hope that it will be useful,
  10. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //GNU General Public License for more details.
  13. //
  14. //You should have received a copy of the GNU General Public License
  15. //along with this program; if not, write to the Free Software
  16. //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. #include "stdafx.h"
  18. #include "emule.h"
  19. #include "emuledlg.h"
  20. #include "SharedFilesWnd.h"
  21. #include "PPgDirectories.h"
  22. #include "otherfunctions.h"
  23. #include "InputBox.h"
  24. #include "SharedFileList.h"
  25. #include "Preferences.h"
  26. #include "HelpIDs.h"
  27. #include "UserMsgs.h"
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33. IMPLEMENT_DYNAMIC(CPPgDirectories, CPropertyPage)
  34. BEGIN_MESSAGE_MAP(CPPgDirectories, CPropertyPage)
  35. ON_BN_CLICKED(IDC_SELTEMPDIR, OnBnClickedSeltempdir)
  36. ON_BN_CLICKED(IDC_SELINCDIR,OnBnClickedSelincdir)
  37. ON_EN_CHANGE(IDC_INCFILES, OnSettingsChange)
  38. ON_EN_CHANGE(IDC_TEMPFILES, OnSettingsChange)
  39. ON_BN_CLICKED(IDC_UNCADD, OnBnClickedAddUNC)
  40. ON_BN_CLICKED(IDC_UNCREM, OnBnClickedRemUNC)
  41. ON_WM_HELPINFO()
  42. ON_BN_CLICKED(IDC_SELTEMPDIRADD, OnBnClickedSeltempdiradd)
  43. END_MESSAGE_MAP()
  44. CPPgDirectories::CPPgDirectories()
  45. : CPropertyPage(CPPgDirectories::IDD)
  46. {
  47. }
  48. CPPgDirectories::~CPPgDirectories()
  49. {
  50. }
  51. void CPPgDirectories::DoDataExchange(CDataExchange* pDX)
  52. {
  53. CPropertyPage::DoDataExchange(pDX);
  54. DDX_Control(pDX, IDC_SHARESELECTOR, m_ShareSelector);
  55. DDX_Control(pDX, IDC_UNCLIST, m_ctlUncPaths);
  56. }
  57. BOOL CPPgDirectories::OnInitDialog()
  58. {
  59. CWaitCursor curWait; // initialization of that dialog may take a while..
  60. CPropertyPage::OnInitDialog();
  61. InitWindowStyles(this);
  62. m_ShareSelector.Init();
  63. ((CEdit*)GetDlgItem(IDC_INCFILES))->SetLimitText(509);
  64. ((CEdit*)GetDlgItem(IDC_TEMPFILES))->SetLimitText(509);
  65. m_ctlUncPaths.InsertColumn(0, GetResString(IDS_UNCFOLDERS), LVCFMT_LEFT, 280, -1); 
  66. m_ctlUncPaths.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  67. GetDlgItem(IDC_SELTEMPDIRADD)->ShowWindow(thePrefs.IsExtControlsEnabled()?SW_SHOW:SW_HIDE);
  68. LoadSettings();
  69. Localize();
  70. return TRUE;  // return TRUE unless you set the focus to a control
  71. // EXCEPTION: OCX Property Pages should return FALSE
  72. }
  73. void CPPgDirectories::LoadSettings(void)
  74. {
  75. GetDlgItem(IDC_INCFILES)->SetWindowText(thePrefs.m_strIncomingDir);
  76. CString tempfolders;
  77. for (int i=0;i<thePrefs.tempdir.GetCount();i++) {
  78. tempfolders.Append(thePrefs.GetTempDir(i));
  79. if (i+1<thePrefs.tempdir.GetCount())
  80. tempfolders.Append(_T("|") );
  81. }
  82. GetDlgItem(IDC_TEMPFILES)->SetWindowText(tempfolders);
  83. m_ShareSelector.SetSharedDirectories(&thePrefs.shareddir_list);
  84. FillUncList();
  85. }
  86. void CPPgDirectories::OnBnClickedSelincdir()
  87. {
  88. TCHAR buffer[MAX_PATH] = {0};
  89. GetDlgItemText(IDC_INCFILES, buffer, ARRSIZE(buffer));
  90. if(SelectDir(GetSafeHwnd(),buffer,GetResString(IDS_SELECT_INCOMINGDIR)))
  91. GetDlgItem(IDC_INCFILES)->SetWindowText(buffer);
  92. }
  93. void CPPgDirectories::OnBnClickedSeltempdir()
  94. {
  95. TCHAR buffer[MAX_PATH] = {0};
  96. GetDlgItemText(IDC_TEMPFILES, buffer, ARRSIZE(buffer));
  97. if(SelectDir(GetSafeHwnd(),buffer,GetResString(IDS_SELECT_TEMPDIR)))
  98. GetDlgItem(IDC_TEMPFILES)->SetWindowText(buffer);
  99. }
  100. BOOL CPPgDirectories::OnApply()
  101. {
  102. bool testtempdirchanged=false;
  103. CString testincdirchanged = thePrefs.GetMuleDirectory(EMULE_INCOMINGDIR);
  104. CString strIncomingDir;
  105. GetDlgItemText(IDC_INCFILES, strIncomingDir);
  106. if (strIncomingDir.IsEmpty()){
  107. strIncomingDir = thePrefs.GetDefaultDirectory(EMULE_INCOMINGDIR, true); // will create the directory here if it doesnt exists
  108. SetDlgItemText(IDC_INCFILES, strIncomingDir);
  109. }
  110. if (thePrefs.IsInstallationDirectory(strIncomingDir)){
  111. AfxMessageBox(GetResString(IDS_WRN_INCFILE_RESERVED));
  112. return FALSE;
  113. }
  114. // checking specified tempdir(s)
  115. CString strTempDir;
  116. GetDlgItemText(IDC_TEMPFILES, strTempDir);
  117. if (strTempDir.IsEmpty()){
  118. strTempDir = thePrefs.GetDefaultDirectory(EMULE_TEMPDIR, true); // will create the directory here if it doesnt exists
  119. SetDlgItemText(IDC_TEMPFILES, strTempDir);
  120. }
  121. int curPos=0;
  122. CStringArray temptempfolders;
  123. CString atmp=strTempDir.Tokenize(_T("|"), curPos);
  124. while (!atmp.IsEmpty())
  125. {
  126. atmp.Trim();
  127. if (!atmp.IsEmpty()) {
  128. if (CompareDirectories(strIncomingDir, atmp)==0){
  129. AfxMessageBox(GetResString(IDS_WRN_INCTEMP_SAME));
  130. return FALSE;
  131. }
  132. if (thePrefs.IsInstallationDirectory(atmp)){
  133. AfxMessageBox(GetResString(IDS_WRN_TEMPFILES_RESERVED));
  134. return FALSE;
  135. }
  136. bool doubled=false;
  137. for (int i=0;i<temptempfolders.GetCount();i++) // avoid double tempdirs
  138. if (temptempfolders.GetAt(i).CompareNoCase(atmp)==0) {
  139. doubled=true;
  140. break;
  141. }
  142. if (!doubled) {
  143. temptempfolders.Add(atmp);
  144. if (thePrefs.tempdir.GetCount()>=temptempfolders.GetCount()) {
  145. if( atmp.CompareNoCase(thePrefs.GetTempDir(temptempfolders.GetCount()-1))!=0 )
  146. testtempdirchanged=true;
  147. } else testtempdirchanged=true;
  148. }
  149. }
  150. atmp = strTempDir.Tokenize(_T("|"), curPos);
  151. }
  152. if (temptempfolders.IsEmpty())
  153. temptempfolders.Add(strTempDir = thePrefs.GetDefaultDirectory(EMULE_TEMPDIR, true));
  154. if (temptempfolders.GetCount()!=thePrefs.tempdir.GetCount())
  155. testtempdirchanged=true;
  156. // applying tempdirs
  157. if (testtempdirchanged) {
  158. thePrefs.tempdir.RemoveAll();
  159. for (int i=0;i<temptempfolders.GetCount();i++) {
  160. CString toadd=temptempfolders.GetAt(i);
  161. MakeFoldername(toadd.GetBuffer(MAX_PATH));
  162. toadd.ReleaseBuffer();
  163. if (!PathFileExists(toadd))
  164. CreateDirectory(toadd,NULL);
  165. if (PathFileExists(toadd))
  166. thePrefs.tempdir.Add(toadd);
  167. }
  168. }
  169. if (thePrefs.tempdir.IsEmpty())
  170. thePrefs.tempdir.Add(thePrefs.GetDefaultDirectory(EMULE_TEMPDIR, true));
  171. // strIncomingDir
  172. thePrefs.m_strIncomingDir = strIncomingDir;
  173. MakeFoldername(thePrefs.m_strIncomingDir);
  174. thePrefs.GetCategory(0)->strIncomingPath = thePrefs.GetMuleDirectory(EMULE_INCOMINGDIR);
  175. thePrefs.shareddir_list.RemoveAll();
  176. m_ShareSelector.GetSharedDirectories(&thePrefs.shareddir_list);
  177. for (int i = 0; i < m_ctlUncPaths.GetItemCount(); i++)
  178. thePrefs.shareddir_list.AddTail(m_ctlUncPaths.GetItemText(i, 0));
  179. // check shared directories for reserved folder names
  180. POSITION pos = thePrefs.shareddir_list.GetHeadPosition();
  181. while (pos){
  182. POSITION posLast = pos;
  183. const CString& rstrDir = thePrefs.shareddir_list.GetNext(pos);
  184. if (!thePrefs.IsShareableDirectory(rstrDir))
  185. thePrefs.shareddir_list.RemoveAt(posLast);
  186. }
  187. if (testtempdirchanged)
  188. AfxMessageBox(GetResString(IDS_SETTINGCHANGED_RESTART));
  189. // on changing incoming dir, update incoming dirs of category of the same path
  190. if (testincdirchanged.CompareNoCase(thePrefs.m_strIncomingDir)!=0) {
  191. CString oldpath;
  192. bool dontaskagain=false;
  193. for (int cat=1; cat<=thePrefs.GetCatCount()-1;cat++){
  194. oldpath=CString(thePrefs.GetCatPath(cat));
  195. if (oldpath.Left(testincdirchanged.GetLength()).CompareNoCase(testincdirchanged)==0) {
  196. if (!dontaskagain) {
  197. dontaskagain=true;
  198. if (AfxMessageBox(GetResString(IDS_UPDATECATINCOMINGDIRS),MB_YESNO)==IDNO)
  199. break;
  200. }
  201. thePrefs.GetCategory(cat)->strIncomingPath = thePrefs.m_strIncomingDir +  oldpath.Mid(testincdirchanged.GetLength());
  202. }
  203. }
  204. }
  205. theApp.emuledlg->sharedfileswnd->Reload();
  206. SetModified(0);
  207. return CPropertyPage::OnApply();
  208. }
  209. BOOL CPPgDirectories::OnCommand(WPARAM wParam, LPARAM lParam)
  210. {
  211. if (wParam == UM_ITEMSTATECHANGED)
  212. SetModified();
  213. else if (wParam == ID_HELP)
  214. {
  215. OnHelp();
  216. return TRUE;
  217. }
  218. return CPropertyPage::OnCommand(wParam, lParam);
  219. }
  220. void CPPgDirectories::Localize(void)
  221. {
  222. if(m_hWnd)
  223. {
  224. SetWindowText(GetResString(IDS_PW_DIR));
  225. GetDlgItem(IDC_INCOMING_FRM)->SetWindowText(GetResString(IDS_PW_INCOMING));
  226. GetDlgItem(IDC_TEMP_FRM)->SetWindowText(GetResString(IDS_PW_TEMP));
  227. GetDlgItem(IDC_SELINCDIR)->SetWindowText(GetResString(IDS_PW_BROWSE));
  228. GetDlgItem(IDC_SELTEMPDIR)->SetWindowText(GetResString(IDS_PW_BROWSE));
  229. GetDlgItem(IDC_SHARED_FRM)->SetWindowText(GetResString(IDS_PW_SHARED));
  230. }
  231. }
  232. void CPPgDirectories::FillUncList(void)
  233. {
  234. m_ctlUncPaths.DeleteAllItems();
  235. for (POSITION pos = thePrefs.shareddir_list.GetHeadPosition(); pos != 0; )
  236. {
  237. CString folder = thePrefs.shareddir_list.GetNext(pos);
  238. if (PathIsUNC(folder))
  239. m_ctlUncPaths.InsertItem(0, folder);
  240. }
  241. }
  242. void CPPgDirectories::OnBnClickedAddUNC()
  243. {
  244. InputBox inputbox;
  245. inputbox.SetLabels(GetResString(IDS_UNCFOLDERS), GetResString(IDS_UNCFOLDERS), _T("\\Server\Share"));
  246. if (inputbox.DoModal() != IDOK)
  247. return;
  248. CString unc=inputbox.GetInput();
  249. // basic unc-check 
  250. if (!PathIsUNC(unc)){
  251. AfxMessageBox(GetResString(IDS_ERR_BADUNC), MB_ICONERROR);
  252. return;
  253. }
  254. if (unc.Right(1) == _T("\"))
  255. unc.Delete(unc.GetLength()-1, 1);
  256. for (POSITION pos = thePrefs.shareddir_list.GetHeadPosition();pos != 0;){
  257. if (unc.CompareNoCase(thePrefs.shareddir_list.GetNext(pos))==0)
  258. return;
  259. }
  260. for (int posi = 0; posi < m_ctlUncPaths.GetItemCount(); posi++){
  261. if (unc.CompareNoCase(m_ctlUncPaths.GetItemText(posi, 0)) == 0)
  262. return;
  263. }
  264. m_ctlUncPaths.InsertItem(m_ctlUncPaths.GetItemCount(), unc);
  265. SetModified();
  266. }
  267. void CPPgDirectories::OnBnClickedRemUNC()
  268. {
  269. int index = m_ctlUncPaths.GetSelectionMark();
  270. if (index == -1 || m_ctlUncPaths.GetSelectedCount() == 0)
  271. return;
  272. m_ctlUncPaths.DeleteItem(index);
  273. SetModified();
  274. }
  275. void CPPgDirectories::OnHelp()
  276. {
  277. theApp.ShowHelp(eMule_FAQ_Preferences_Directories);
  278. }
  279. BOOL CPPgDirectories::OnHelpInfo(HELPINFO* /*pHelpInfo*/)
  280. {
  281. OnHelp();
  282. return TRUE;
  283. }
  284. void CPPgDirectories::OnBnClickedSeltempdiradd()
  285. {
  286. CString paths;
  287. GetDlgItemText(IDC_TEMPFILES, paths);
  288. TCHAR buffer[MAX_PATH] = {0};
  289. //GetDlgItemText(IDC_TEMPFILES, buffer, ARRSIZE(buffer));
  290. if(SelectDir(GetSafeHwnd(),buffer,GetResString(IDS_SELECT_TEMPDIR))) {
  291. paths.Append(_T("|"));
  292. paths.Append(buffer);
  293. SetDlgItemText(IDC_TEMPFILES, paths);
  294. }
  295. }