ShareDirMgrDlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // ShareDirMgrDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "ShareDirMgr.h"
  5. #include "ShareDirMgrDlg.h"
  6. #include "SQLite3Interface.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. //************************************//
  11. // [函数]:CheckIPFormat
  12. // [描述]:检查是否为IP地址格式
  13. // [参数]:
  14. // strIP IP地址
  15. // [返回]:1正确IP,0错误IP
  16. //************************************//
  17. int CheckIPFormat(CString& strIP)
  18. {
  19. //字符0~9 对应该 ascii码48~57
  20. char szNum[64] = {0};
  21. #ifdef _UNICODE
  22. unicode2acsii(strIP, szNum);
  23. #else
  24. strcpy(szNum, strIP.GetBuffer());
  25. #endif
  26. int nCount = 0;
  27. for(int i=0; i<strlen(szNum); i++)
  28. {
  29. if(szNum[i] == '.')
  30. {
  31. ++nCount;
  32. continue;
  33. }
  34. if(szNum[i] < 48 || szNum[i] > 57)
  35. return 0;
  36. }
  37. if(nCount != 3)
  38. return 0;
  39. return 1;
  40. }
  41. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  42. class CAboutDlg : public CDialog
  43. {
  44. public:
  45. CAboutDlg();
  46. // 对话框数据
  47. enum { IDD = IDD_ABOUTBOX };
  48. protected:
  49. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  50. // 实现
  51. protected:
  52. DECLARE_MESSAGE_MAP()
  53. };
  54. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  55. {
  56. }
  57. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  58. {
  59. CDialog::DoDataExchange(pDX);
  60. }
  61. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  62. END_MESSAGE_MAP()
  63. // CShareDirMgrDlg 对话框
  64. CShareDirMgrDlg::CShareDirMgrDlg(CWnd* pParent /*=NULL*/)
  65. : CDialog(CShareDirMgrDlg::IDD, pParent)
  66. , m_strName(_T(""))
  67. , m_strIP(_T(""))
  68. , m_strPCName(_T(""))
  69. , m_strDomain(_T(""))
  70. , m_strSharePath(_T(""))
  71. , m_nSelRow(0)
  72. {
  73. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  74. }
  75. void CShareDirMgrDlg::DoDataExchange(CDataExchange* pDX)
  76. {
  77. CDialog::DoDataExchange(pDX);
  78. DDX_Control(pDX, IDC_INFO_LIST, m_InfoList);
  79. DDX_Text(pDX, IDC_ShopName_EDIT, m_strName);
  80. DDX_Text(pDX, IDC_IPADDR_EDIT, m_strIP);
  81. DDX_Text(pDX, IDC_PCNAME_EDIT, m_strPCName);
  82. DDX_Text(pDX, IDC_Domain_EDIT, m_strDomain);
  83. DDX_Text(pDX, IDC_SHAREDIR_EDIT, m_strSharePath);
  84. }
  85. BEGIN_MESSAGE_MAP(CShareDirMgrDlg, CDialog)
  86. ON_WM_SYSCOMMAND()
  87. ON_WM_PAINT()
  88. ON_WM_QUERYDRAGICON()
  89. //}}AFX_MSG_MAP
  90. ON_BN_CLICKED(IDC_Browse_BUTTON, &CShareDirMgrDlg::OnBnClickedBrowseButton)
  91. ON_BN_CLICKED(IDC_ADD_BTN, &CShareDirMgrDlg::OnBnClickedAddBtn)
  92. ON_BN_CLICKED(IDC_Delete_BTN, &CShareDirMgrDlg::OnBnClickedDeleteBtn)
  93. ON_NOTIFY(NM_CLICK, IDC_INFO_LIST, &CShareDirMgrDlg::OnNMClickInfoList)
  94. ON_BN_CLICKED(IDC_Refresh_BUTTON, &CShareDirMgrDlg::OnBnClickedRefreshButton)
  95. END_MESSAGE_MAP()
  96. // CShareDirMgrDlg 消息处理程序
  97. BOOL CShareDirMgrDlg::OnInitDialog()
  98. {
  99. CDialog::OnInitDialog();
  100. // 将“关于...”菜单项添加到系统菜单中。
  101. // IDM_ABOUTBOX 必须在系统命令范围内。
  102. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  103. ASSERT(IDM_ABOUTBOX < 0xF000);
  104. CMenu* pSysMenu = GetSystemMenu(FALSE);
  105. if (pSysMenu != NULL)
  106. {
  107. BOOL bNameValid;
  108. CString strAboutMenu;
  109. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  110. ASSERT(bNameValid);
  111. if (!strAboutMenu.IsEmpty())
  112. {
  113. pSysMenu->AppendMenu(MF_SEPARATOR);
  114. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  115. }
  116. }
  117. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  118. // 执行此操作
  119. SetIcon(m_hIcon, TRUE); // 设置大图标
  120. SetIcon(m_hIcon, FALSE); // 设置小图标
  121. // TODO: 在此添加额外的初始化代码
  122. //初始化ctrllist
  123. m_InfoList.InsertColumn(0,_T("域名"));
  124. m_InfoList.SetColumnWidth(0,200);
  125. m_InfoList.InsertColumn(1,_T("店名"));
  126. m_InfoList.SetColumnWidth(1,100);
  127. m_InfoList.InsertColumn(2,_T("IP地址"));
  128. m_InfoList.SetColumnWidth(2,100);
  129. m_InfoList.InsertColumn(3,_T("计算机名"));
  130. m_InfoList.SetColumnWidth(3,100);
  131. m_InfoList.InsertColumn(4,_T("共享目录"));
  132. m_InfoList.SetColumnWidth(4,400);
  133. m_InfoList.SetExtendedStyle(m_InfoList.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
  134. //创建并打开数据库
  135. if(CreateAndOpenDB() == -1)
  136. {
  137. MessageBox(_T("打开数据库失败"));
  138. return FALSE;
  139. }
  140. if(CreateTable() == -1)
  141. {
  142. MessageBox(_T("创建表失败"));
  143. return FALSE;
  144. }
  145. GetTableAllInfo();
  146. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  147. }
  148. void CShareDirMgrDlg::OnSysCommand(UINT nID, LPARAM lParam)
  149. {
  150. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  151. {
  152. CAboutDlg dlgAbout;
  153. dlgAbout.DoModal();
  154. }
  155. else
  156. {
  157. CDialog::OnSysCommand(nID, lParam);
  158. }
  159. }
  160. // 如果向对话框添加最小化按钮,则需要下面的代码
  161. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  162. // 这将由框架自动完成。
  163. void CShareDirMgrDlg::OnPaint()
  164. {
  165. if (IsIconic())
  166. {
  167. CPaintDC dc(this); // 用于绘制的设备上下文
  168. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  169. // 使图标在工作区矩形中居中
  170. int cxIcon = GetSystemMetrics(SM_CXICON);
  171. int cyIcon = GetSystemMetrics(SM_CYICON);
  172. CRect rect;
  173. GetClientRect(&rect);
  174. int x = (rect.Width() - cxIcon + 1) / 2;
  175. int y = (rect.Height() - cyIcon + 1) / 2;
  176. // 绘制图标
  177. dc.DrawIcon(x, y, m_hIcon);
  178. }
  179. else
  180. {
  181. CDialog::OnPaint();
  182. }
  183. }
  184. //当用户拖动最小化窗口时系统调用此函数取得光标
  185. //显示。
  186. HCURSOR CShareDirMgrDlg::OnQueryDragIcon()
  187. {
  188. return static_cast<HCURSOR>(m_hIcon);
  189. }
  190. void CShareDirMgrDlg::OnBnClickedBrowseButton()
  191. {
  192. // TODO: 在此添加控件通知处理程序代码
  193. TCHAR szPath[MAX_PATH]; //存放选择的目录路径
  194. CString str;
  195. ZeroMemory(szPath, sizeof(szPath));
  196. BROWSEINFO bi;
  197. bi.hwndOwner = m_hWnd;
  198. bi.pidlRoot = NULL;
  199. bi.pszDisplayName = szPath;
  200. bi.lpszTitle = _T("请选择需要打开的目录:");
  201. bi.ulFlags = 0;
  202. bi.lpfn = NULL;
  203. bi.lParam = 0;
  204. bi.iImage = 0;
  205. //弹出选择目录对话框
  206. LPITEMIDLIST lp = SHBrowseForFolder(&bi);
  207. if(lp && SHGetPathFromIDList(lp, szPath))
  208. {
  209. UpdateData();
  210. m_strSharePath = szPath;
  211. //m_strSharePath += _T("\\") + m_strDomain;
  212. UpdateData(FALSE);
  213. }
  214. else
  215. AfxMessageBox(_T("无效的目录,请重新选择"));
  216. }
  217. void CShareDirMgrDlg::OnDestroy()
  218. {
  219. CSQLiter3Interface::GetInstance()->Close();
  220. CDialog::OnDestroy();
  221. }
  222. /************************************************************************/
  223. /*
  224. 函数: CreateAndOpenDB
  225. 描述: 创建并打开DB
  226. 参数:
  227. 返回:
  228. 注意: 0成功, -1失败;
  229. */
  230. /************************************************************************/
  231. int CShareDirMgrDlg::CreateAndOpenDB()
  232. {
  233. TCHAR szModule[MAX_PATH] = { 0 };
  234. TCHAR szModuleFile[MAX_PATH] = { 0 };
  235. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  236. TCHAR szDir[_MAX_DIR] = { 0 };
  237. TCHAR szFna[_MAX_DIR] = { 0 };
  238. TCHAR szExt[_MAX_DIR] = { 0 };
  239. ::GetModuleFileName(NULL, szModuleFile, sizeof(szModuleFile) / sizeof(TCHAR));
  240. _stprintf_s(szModule, _T("%s"), szModuleFile);
  241. _tsplitpath_s(szModule, szDrive, szDir, szFna, szExt);
  242. _tcscpy_s(szModule, szDrive);
  243. _tcscat_s(szModule, szDir);
  244. /*-----------------------------------------------------*/
  245. WCHAR szdb[MAX_PATH] = {0};
  246. #ifdef _UNICODE
  247. _stprintf_s(szdb, _T("%ssharedirdb.db"), szModule);
  248. // 1.打开数据库,若不存在则创建;
  249. //string strdb;
  250. CHAR strdb[MAX_PATH] = {0};
  251. unicode2acsii(szdb, strdb);
  252. char *psqlite_error = NULL;
  253. int nRet = CSQLiter3Interface::GetInstance()->OpenSQLite3DB(strdb);
  254. if(nRet == -1)
  255. {
  256. char szError[511] = {0};
  257. sprintf(szError, "%s", CSQLiter3Interface::GetInstance()->Get_ErrMsg());
  258. WCHAR wszError[512] = {0};
  259. Char2WChar(wszError, szError);
  260. MessageBox(wszError);
  261. }
  262. #else
  263. std::string str = _T("");
  264. std::string strfiledb = "sharedirdb.db";
  265. str = szModule;
  266. str += strfiledb;
  267. // 1.打开数据库,若不存在则创建;
  268. char *psqlite_error = NULL;
  269. int nRet = CSQLiter3Interface::GetInstance()->OpenSQLite3DB(str.c_str());
  270. if(nRet == -1)
  271. MessageBox(CSQLiter3Interface::GetInstance()->Get_ErrMsg());
  272. #endif
  273. return nRet;
  274. }
  275. /************************************************************************/
  276. /*
  277. 函数: CreateTable
  278. 描述: 创建表
  279. 参数:
  280. 返回:
  281. 注意: 0成功, -1失败;
  282. */
  283. /************************************************************************/
  284. int CShareDirMgrDlg::CreateTable()
  285. {
  286. CString strError = _T("");
  287. if(CSQLiter3Interface::GetInstance()->CreateTabel(CREATE_DB_TBL_, strError) == -1)
  288. {
  289. MessageBox(strError);
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. /************************************************************************/
  295. /*
  296. 函数: OnBnClickedAddBtn
  297. 描述: 添加数据
  298. 参数:
  299. 返回:
  300. 注意: 0成功, -1失败;
  301. */
  302. /************************************************************************/
  303. void CShareDirMgrDlg::OnBnClickedAddBtn()
  304. {
  305. // TODO: 在此添加控件通知处理程序代码
  306. UpdateData();
  307. if(m_strName == _T(""))
  308. {
  309. MessageBox(_T("没输入店口"));
  310. return;
  311. }
  312. if(m_strIP == _T(""))
  313. {
  314. MessageBox(_T("没输入IP"));
  315. return;
  316. }
  317. if(!CheckIPFormat(m_strIP))
  318. {
  319. MessageBox(_T("输入的IP有误"));
  320. return;
  321. }
  322. if(m_strPCName == _T(""))
  323. {
  324. MessageBox(_T("没输入计算机名"));
  325. return;
  326. }
  327. if(m_strDomain == _T(""))
  328. {
  329. MessageBox(_T("没输入域名"));
  330. return;
  331. }
  332. if(m_strSharePath == _T(""))
  333. {
  334. MessageBox(_T("没输入共享目录"));
  335. return;
  336. }
  337. #ifdef _UNICODE
  338. char szFields[512] = {0};
  339. sprintf(szFields, "domain,shopname,ipaddr,pcname,sharedir");
  340. CString strValues = _T("");
  341. CString strTmpPath = m_strSharePath;
  342. strValues.Format(_T("'%s','%s','%s','%s','%s'"), m_strDomain.GetBuffer(), m_strName.GetBuffer(), m_strIP.GetBuffer(), m_strPCName.GetBuffer(), strTmpPath.GetBuffer());
  343. char szValues[512] = {0};
  344. WChar2Char(szValues, strValues);
  345. strValues.ReleaseBuffer();
  346. CString strError = _T("");
  347. if(CSQLiter3Interface::GetInstance()->Insert("remoteinfo", szFields, szValues, strError) == -1)
  348. {
  349. MessageBox(strError);
  350. return;
  351. }
  352. #else
  353. char szFields[512] = {0};
  354. sprintf(szFields, "domain,shopname,ipaddr,pcname,sharedir");
  355. char szValues[512] = {0};
  356. sprintf(szValues, "'%s','%s','%s','%s','%s'", m_strDomain.GetBuffer(), m_strName.GetBuffer(), m_strIP.GetBuffer(), m_strPCName.GetBuffer(), m_strSharePath.GetBuffer());
  357. CString strError = _T("");
  358. if(CSQLiter3Interface::GetInstance()->Insert("remoteinfo", szFields, szValues, strError) == -1)
  359. {
  360. MessageBox(strError);
  361. return;
  362. }
  363. #endif
  364. //添加到列表
  365. int nCount = m_InfoList.GetItemCount();
  366. int nRow = m_InfoList.InsertItem(nCount, m_strDomain);
  367. m_InfoList.SetItemText(nRow, 1, m_strName); //插入列
  368. m_InfoList.SetItemText(nRow, 2, m_strIP);
  369. m_InfoList.SetItemText(nRow, 3, m_strPCName);
  370. m_InfoList.SetItemText(nRow, 4, m_strSharePath);
  371. ResetDate();
  372. }
  373. void CShareDirMgrDlg::ResetDate()
  374. {
  375. UpdateData();
  376. m_strName = _T("");
  377. m_strIP = _T("");
  378. m_strPCName = _T("");
  379. m_strDomain = _T("");
  380. m_strSharePath = _T("");
  381. UpdateData(FALSE);
  382. }
  383. /************************************************************************/
  384. /*
  385. 函数: OnBnClickedDeleteBtn
  386. 描述: 删除数据
  387. 参数:
  388. 返回:
  389. 注意: 0成功, -1失败;
  390. */
  391. /************************************************************************/
  392. void CShareDirMgrDlg::OnBnClickedDeleteBtn()
  393. {
  394. // TODO: 在此添加控件通知处理程序代码
  395. if(m_nSelRow < 0 || m_nSelRow >= m_InfoList.GetItemCount())
  396. {
  397. MessageBox(_T("错误选项"));
  398. return;
  399. }
  400. CString str = m_InfoList.GetItemText(m_nSelRow, 0);
  401. if(str == _T(""))
  402. {
  403. MessageBox(_T("错误选项"));
  404. return;
  405. }
  406. #ifdef _UNICODE
  407. CString strDomain = _T("domain='") + str + _T("'");
  408. char szLimit[256] = {0};
  409. WChar2Char(szLimit, strDomain);
  410. CString strError = _T("");
  411. if(CSQLiter3Interface::GetInstance()->Delete("remoteinfo", szLimit, strError))
  412. {
  413. MessageBox(strError);
  414. return;
  415. }
  416. #else
  417. char szLimit[256] = {0};
  418. sprintf(szLimit, "domain='%s'", str.GetBuffer());
  419. CString strError = _T("");
  420. if(CSQLiter3Interface::GetInstance()->Delete("remoteinfo", szLimit, strError))
  421. {
  422. MessageBox(strError);
  423. return;
  424. }
  425. #endif
  426. m_InfoList.DeleteItem(m_nSelRow);
  427. m_nSelRow = -1;
  428. UpdateData(TRUE);
  429. }
  430. void CShareDirMgrDlg::OnNMClickInfoList(NMHDR *pNMHDR, LRESULT *pResult)
  431. {
  432. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  433. // TODO: 在此添加控件通知处理程序代码
  434. if(pNMItemActivate->iItem != -1)
  435. {
  436. //单击行列pNMListView->iItem, pNMListView->iSubItem
  437. m_nSelRow = pNMItemActivate->iItem;
  438. }
  439. *pResult = 0;
  440. }
  441. /************************************************************************/
  442. /*
  443. 函数: GetTableAllInfo
  444. 描述: 获取数据表所有信息
  445. 参数:
  446. 返回:
  447. 注意: 0成功, -1失败;
  448. */
  449. /************************************************************************/
  450. int CShareDirMgrDlg::GetTableAllInfo()
  451. {
  452. CString strError = _T("");
  453. std::vector<TString> vValues;
  454. if(CSQLiter3Interface::GetInstance()->Select("remoteinfo", "*", NULL, vValues, strError) == -1)
  455. {
  456. MessageBox(strError);
  457. return -1;
  458. }
  459. if(m_InfoList.GetItemCount() > 0)
  460. m_InfoList.DeleteAllItems();
  461. int nRow = 0;
  462. int nCol = 0;
  463. int nIdx = 0;
  464. std::vector<TString>::iterator iter = vValues.begin();
  465. for(;iter != vValues.end();)
  466. {
  467. if(nCol%5 == 0)
  468. {
  469. nRow = m_InfoList.InsertItem(nIdx, (*iter).c_str());
  470. nCol = 0;
  471. ++nIdx;
  472. }
  473. else
  474. m_InfoList.SetItemText(nRow, nCol, (*iter).c_str()); //插入列
  475. ++iter;
  476. ++nCol;
  477. }
  478. return 0;
  479. }
  480. void CShareDirMgrDlg::OnBnClickedRefreshButton()
  481. {
  482. // TODO: 在此添加控件通知处理程序代码
  483. GetTableAllInfo();
  484. }