123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- // ShareDirMgrDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "ShareDirMgr.h"
- #include "ShareDirMgrDlg.h"
- #include "SQLite3Interface.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //************************************//
- // [函数]:CheckIPFormat
- // [描述]:检查是否为IP地址格式
- // [参数]:
- // strIP IP地址
- // [返回]:1正确IP,0错误IP
- //************************************//
- int CheckIPFormat(CString& strIP)
- {
- //字符0~9 对应该 ascii码48~57
- char szNum[64] = {0};
- #ifdef _UNICODE
- unicode2acsii(strIP, szNum);
- #else
- strcpy(szNum, strIP.GetBuffer());
- #endif
- int nCount = 0;
- for(int i=0; i<strlen(szNum); i++)
- {
- if(szNum[i] == '.')
- {
- ++nCount;
- continue;
- }
- if(szNum[i] < 48 || szNum[i] > 57)
- return 0;
- }
- if(nCount != 3)
- return 0;
- return 1;
- }
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CShareDirMgrDlg 对话框
- CShareDirMgrDlg::CShareDirMgrDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CShareDirMgrDlg::IDD, pParent)
- , m_strName(_T(""))
- , m_strIP(_T(""))
- , m_strPCName(_T(""))
- , m_strDomain(_T(""))
- , m_strSharePath(_T(""))
- , m_nSelRow(0)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CShareDirMgrDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_INFO_LIST, m_InfoList);
- DDX_Text(pDX, IDC_ShopName_EDIT, m_strName);
- DDX_Text(pDX, IDC_IPADDR_EDIT, m_strIP);
- DDX_Text(pDX, IDC_PCNAME_EDIT, m_strPCName);
- DDX_Text(pDX, IDC_Domain_EDIT, m_strDomain);
- DDX_Text(pDX, IDC_SHAREDIR_EDIT, m_strSharePath);
- }
- BEGIN_MESSAGE_MAP(CShareDirMgrDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_Browse_BUTTON, &CShareDirMgrDlg::OnBnClickedBrowseButton)
- ON_BN_CLICKED(IDC_ADD_BTN, &CShareDirMgrDlg::OnBnClickedAddBtn)
- ON_BN_CLICKED(IDC_Delete_BTN, &CShareDirMgrDlg::OnBnClickedDeleteBtn)
- ON_NOTIFY(NM_CLICK, IDC_INFO_LIST, &CShareDirMgrDlg::OnNMClickInfoList)
- ON_BN_CLICKED(IDC_Refresh_BUTTON, &CShareDirMgrDlg::OnBnClickedRefreshButton)
- END_MESSAGE_MAP()
- // CShareDirMgrDlg 消息处理程序
- BOOL CShareDirMgrDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- //初始化ctrllist
- m_InfoList.InsertColumn(0,_T("域名"));
- m_InfoList.SetColumnWidth(0,200);
- m_InfoList.InsertColumn(1,_T("店名"));
- m_InfoList.SetColumnWidth(1,100);
- m_InfoList.InsertColumn(2,_T("IP地址"));
- m_InfoList.SetColumnWidth(2,100);
- m_InfoList.InsertColumn(3,_T("计算机名"));
- m_InfoList.SetColumnWidth(3,100);
- m_InfoList.InsertColumn(4,_T("共享目录"));
- m_InfoList.SetColumnWidth(4,400);
- m_InfoList.SetExtendedStyle(m_InfoList.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
- //创建并打开数据库
- if(CreateAndOpenDB() == -1)
- {
- MessageBox(_T("打开数据库失败"));
- return FALSE;
- }
- if(CreateTable() == -1)
- {
- MessageBox(_T("创建表失败"));
- return FALSE;
- }
- GetTableAllInfo();
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- void CShareDirMgrDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CShareDirMgrDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作区矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CShareDirMgrDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CShareDirMgrDlg::OnBnClickedBrowseButton()
- {
- // TODO: 在此添加控件通知处理程序代码
- TCHAR szPath[MAX_PATH]; //存放选择的目录路径
- CString str;
- ZeroMemory(szPath, sizeof(szPath));
- BROWSEINFO bi;
- bi.hwndOwner = m_hWnd;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = szPath;
- bi.lpszTitle = _T("请选择需要打开的目录:");
- bi.ulFlags = 0;
- bi.lpfn = NULL;
- bi.lParam = 0;
- bi.iImage = 0;
- //弹出选择目录对话框
- LPITEMIDLIST lp = SHBrowseForFolder(&bi);
- if(lp && SHGetPathFromIDList(lp, szPath))
- {
- UpdateData();
- m_strSharePath = szPath;
- //m_strSharePath += _T("\\") + m_strDomain;
- UpdateData(FALSE);
- }
- else
- AfxMessageBox(_T("无效的目录,请重新选择"));
- }
- void CShareDirMgrDlg::OnDestroy()
- {
- CSQLiter3Interface::GetInstance()->Close();
- CDialog::OnDestroy();
- }
- /************************************************************************/
- /*
- 函数: CreateAndOpenDB
- 描述: 创建并打开DB
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CShareDirMgrDlg::CreateAndOpenDB()
- {
- TCHAR szModule[MAX_PATH] = { 0 };
- TCHAR szModuleFile[MAX_PATH] = { 0 };
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- ::GetModuleFileName(NULL, szModuleFile, sizeof(szModuleFile) / sizeof(TCHAR));
- _stprintf_s(szModule, _T("%s"), szModuleFile);
- _tsplitpath_s(szModule, szDrive, szDir, szFna, szExt);
- _tcscpy_s(szModule, szDrive);
- _tcscat_s(szModule, szDir);
- /*-----------------------------------------------------*/
- WCHAR szdb[MAX_PATH] = {0};
- #ifdef _UNICODE
- _stprintf_s(szdb, _T("%ssharedirdb.db"), szModule);
- // 1.打开数据库,若不存在则创建;
- //string strdb;
- CHAR strdb[MAX_PATH] = {0};
- unicode2acsii(szdb, strdb);
-
- char *psqlite_error = NULL;
- int nRet = CSQLiter3Interface::GetInstance()->OpenSQLite3DB(strdb);
- if(nRet == -1)
- {
- char szError[511] = {0};
- sprintf(szError, "%s", CSQLiter3Interface::GetInstance()->Get_ErrMsg());
- WCHAR wszError[512] = {0};
- Char2WChar(wszError, szError);
- MessageBox(wszError);
- }
- #else
- std::string str = _T("");
- std::string strfiledb = "sharedirdb.db";
- str = szModule;
- str += strfiledb;
- // 1.打开数据库,若不存在则创建;
- char *psqlite_error = NULL;
- int nRet = CSQLiter3Interface::GetInstance()->OpenSQLite3DB(str.c_str());
- if(nRet == -1)
- MessageBox(CSQLiter3Interface::GetInstance()->Get_ErrMsg());
- #endif
-
- return nRet;
- }
- /************************************************************************/
- /*
- 函数: CreateTable
- 描述: 创建表
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CShareDirMgrDlg::CreateTable()
- {
- CString strError = _T("");
- if(CSQLiter3Interface::GetInstance()->CreateTabel(CREATE_DB_TBL_, strError) == -1)
- {
- MessageBox(strError);
- return -1;
- }
- return 0;
- }
- /************************************************************************/
- /*
- 函数: OnBnClickedAddBtn
- 描述: 添加数据
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- void CShareDirMgrDlg::OnBnClickedAddBtn()
- {
- // TODO: 在此添加控件通知处理程序代码
- UpdateData();
- if(m_strName == _T(""))
- {
- MessageBox(_T("没输入店口"));
- return;
- }
- if(m_strIP == _T(""))
- {
- MessageBox(_T("没输入IP"));
- return;
- }
- if(!CheckIPFormat(m_strIP))
- {
- MessageBox(_T("输入的IP有误"));
- return;
- }
- if(m_strPCName == _T(""))
- {
- MessageBox(_T("没输入计算机名"));
- return;
- }
- if(m_strDomain == _T(""))
- {
- MessageBox(_T("没输入域名"));
- return;
- }
- if(m_strSharePath == _T(""))
- {
- MessageBox(_T("没输入共享目录"));
- return;
- }
- #ifdef _UNICODE
- char szFields[512] = {0};
- sprintf(szFields, "domain,shopname,ipaddr,pcname,sharedir");
- CString strValues = _T("");
- CString strTmpPath = m_strSharePath;
- strValues.Format(_T("'%s','%s','%s','%s','%s'"), m_strDomain.GetBuffer(), m_strName.GetBuffer(), m_strIP.GetBuffer(), m_strPCName.GetBuffer(), strTmpPath.GetBuffer());
- char szValues[512] = {0};
- WChar2Char(szValues, strValues);
- strValues.ReleaseBuffer();
- CString strError = _T("");
- if(CSQLiter3Interface::GetInstance()->Insert("remoteinfo", szFields, szValues, strError) == -1)
- {
- MessageBox(strError);
- return;
- }
- #else
- char szFields[512] = {0};
- sprintf(szFields, "domain,shopname,ipaddr,pcname,sharedir");
- char szValues[512] = {0};
- sprintf(szValues, "'%s','%s','%s','%s','%s'", m_strDomain.GetBuffer(), m_strName.GetBuffer(), m_strIP.GetBuffer(), m_strPCName.GetBuffer(), m_strSharePath.GetBuffer());
- CString strError = _T("");
- if(CSQLiter3Interface::GetInstance()->Insert("remoteinfo", szFields, szValues, strError) == -1)
- {
- MessageBox(strError);
- return;
- }
- #endif
- //添加到列表
- int nCount = m_InfoList.GetItemCount();
- int nRow = m_InfoList.InsertItem(nCount, m_strDomain);
- m_InfoList.SetItemText(nRow, 1, m_strName); //插入列
- m_InfoList.SetItemText(nRow, 2, m_strIP);
- m_InfoList.SetItemText(nRow, 3, m_strPCName);
- m_InfoList.SetItemText(nRow, 4, m_strSharePath);
- ResetDate();
- }
- void CShareDirMgrDlg::ResetDate()
- {
- UpdateData();
- m_strName = _T("");
- m_strIP = _T("");
- m_strPCName = _T("");
- m_strDomain = _T("");
- m_strSharePath = _T("");
- UpdateData(FALSE);
- }
- /************************************************************************/
- /*
- 函数: OnBnClickedDeleteBtn
- 描述: 删除数据
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- void CShareDirMgrDlg::OnBnClickedDeleteBtn()
- {
- // TODO: 在此添加控件通知处理程序代码
- if(m_nSelRow < 0 || m_nSelRow >= m_InfoList.GetItemCount())
- {
- MessageBox(_T("错误选项"));
- return;
- }
- CString str = m_InfoList.GetItemText(m_nSelRow, 0);
- if(str == _T(""))
- {
- MessageBox(_T("错误选项"));
- return;
- }
- #ifdef _UNICODE
- CString strDomain = _T("domain='") + str + _T("'");
- char szLimit[256] = {0};
- WChar2Char(szLimit, strDomain);
- CString strError = _T("");
- if(CSQLiter3Interface::GetInstance()->Delete("remoteinfo", szLimit, strError))
- {
- MessageBox(strError);
- return;
- }
- #else
- char szLimit[256] = {0};
- sprintf(szLimit, "domain='%s'", str.GetBuffer());
- CString strError = _T("");
- if(CSQLiter3Interface::GetInstance()->Delete("remoteinfo", szLimit, strError))
- {
- MessageBox(strError);
- return;
- }
- #endif
-
- m_InfoList.DeleteItem(m_nSelRow);
- m_nSelRow = -1;
- UpdateData(TRUE);
- }
- void CShareDirMgrDlg::OnNMClickInfoList(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
- // TODO: 在此添加控件通知处理程序代码
- if(pNMItemActivate->iItem != -1)
- {
- //单击行列pNMListView->iItem, pNMListView->iSubItem
- m_nSelRow = pNMItemActivate->iItem;
- }
- *pResult = 0;
- }
- /************************************************************************/
- /*
- 函数: GetTableAllInfo
- 描述: 获取数据表所有信息
- 参数:
- 返回:
- 注意: 0成功, -1失败;
- */
- /************************************************************************/
- int CShareDirMgrDlg::GetTableAllInfo()
- {
- CString strError = _T("");
- std::vector<TString> vValues;
- if(CSQLiter3Interface::GetInstance()->Select("remoteinfo", "*", NULL, vValues, strError) == -1)
- {
- MessageBox(strError);
- return -1;
- }
-
- if(m_InfoList.GetItemCount() > 0)
- m_InfoList.DeleteAllItems();
- int nRow = 0;
- int nCol = 0;
- int nIdx = 0;
- std::vector<TString>::iterator iter = vValues.begin();
- for(;iter != vValues.end();)
- {
- if(nCol%5 == 0)
- {
- nRow = m_InfoList.InsertItem(nIdx, (*iter).c_str());
- nCol = 0;
- ++nIdx;
- }
- else
- m_InfoList.SetItemText(nRow, nCol, (*iter).c_str()); //插入列
- ++iter;
- ++nCol;
- }
- return 0;
- }
- void CShareDirMgrDlg::OnBnClickedRefreshButton()
- {
- // TODO: 在此添加控件通知处理程序代码
- GetTableAllInfo();
- }
|