// DirectoryList.cpp : implementation file // #include "stdafx.h" #include "PerforceClientService.h" #include "DirectoryList.h" #include "EditDirectory.h" #include "DirectoryWatcher.h" #include "PerforceClientServiceDlg.h" #include <vector> #include <atlbase.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// namespace DirectoryListNamespace { typedef std::vector<CString> Directories; Directories ms_directories; typedef std::vector<DirectoryWatcher *> DirectoryWatchers; DirectoryWatchers ms_directoryWatchers; } using namespace DirectoryListNamespace; ///////////////////////////////////////////////////////////////////////////// // DirectoryList static functions void DirectoryList::install() { CRegKey registryKey; LONG result = registryKey.Create(HKEY_CURRENT_USER, CPerforceClientServiceApp::getRegistryKey(), NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, NULL); if (result != ERROR_SUCCESS) { ::MessageBox(NULL, "Could not load information from the registry", "Registry Error", MB_OK); return; } DWORD numberOfEntries = 0; result = registryKey.QueryDWORDValue("NumberOfDirectories", numberOfEntries); if (result != ERROR_SUCCESS) { ::MessageBox(NULL, "Could not load directory information from the registry", "Registry Error", MB_OK); return; } for (DWORD i = 0; i < numberOfEntries; ++i) { char valueName[32]; _snprintf_s(valueName, sizeof(valueName), _TRUNCATE, "dir%04d", static_cast<int>(i)); char dir[MAX_PATH]; memset(dir, 0, sizeof(dir)); DWORD fromLength = sizeof(dir) - 1; result = registryKey.QueryStringValue(valueName, dir, &fromLength); if (result != ERROR_SUCCESS) { ::MessageBox(NULL, "Could not load information from the registry", "Registry Error", MB_OK); return; } ms_directories.push_back(CString(dir)); } createDirectoryWatchers(); } void DirectoryList::remove() { destroyDirectoryWatchers(); } void DirectoryList::saveToRegistry() { CRegKey registryKey; LONG result = registryKey.Create(HKEY_CURRENT_USER, CPerforceClientServiceApp::getRegistryKey(), NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, NULL); if (result != ERROR_SUCCESS) { char errorMessage[256]; _snprintf_s(errorMessage, sizeof(errorMessage), _TRUNCATE, "Could not create subkey %s", CPerforceClientServiceApp::getRegistryKey()); ::MessageBox(NULL, errorMessage, "Registry Error", MB_OK); return; } result = registryKey.SetDWORDValue("NumberOfDirectories", ms_directories.size()); if (result != ERROR_SUCCESS) { ::MessageBox(NULL, "Could not save NumberOfDirectories to the registry", "Registry Error", MB_OK); return; } // save all the entries for (unsigned int i = 0; i < ms_directories.size(); ++i) { char valueName[32]; _snprintf_s(valueName, sizeof(valueName), _TRUNCATE, "dir%04d", static_cast<int>(i)); result = registryKey.SetStringValue(valueName, ms_directories[i]); if (result != ERROR_SUCCESS) {char errorMessage[256]; _snprintf_s(errorMessage, sizeof(errorMessage), _TRUNCATE, "Could not save string %s to the registry", valueName); ::MessageBox(NULL, errorMessage, "Registry Error", MB_OK); return; } } } void DirectoryList::destroyDirectoryWatchers() { while (!ms_directoryWatchers.empty()) { delete ms_directoryWatchers.back(); ms_directoryWatchers.pop_back(); } } void DirectoryList::createDirectoryWatchers() { for (Directories::iterator i = ms_directories.begin(); i != ms_directories.end(); ++i) { CPerforceClientServiceDlg::Log("Watching directory %s\n", static_cast<char const *>(*i)); ms_directoryWatchers.push_back(new DirectoryWatcher(*i)); } } ///////////////////////////////////////////////////////////////////////////// // DirectoryList dialog DirectoryList::DirectoryList(CWnd* pParent /*=NULL*/) : CDialog(DirectoryList::IDD, pParent), m_changed(false) { //{{AFX_DATA_INIT(DirectoryList) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void DirectoryList::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(DirectoryList) DDX_Control(pDX, IDC_LIST1, m_directoryList); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(DirectoryList, CDialog) //{{AFX_MSG_MAP(DirectoryList) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_MODIFY, OnModify) ON_BN_CLICKED(IDC_DELETE, OnDelete) ON_LBN_DBLCLK(IDC_LIST1, OnModify) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // DirectoryList message handlers BOOL DirectoryList::OnInitDialog() { CDialog::OnInitDialog(); for (Directories::iterator i = ms_directories.begin(); i != ms_directories.end(); ++i) m_directoryList.InsertString(-1, *i); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void DirectoryList::OnAdd() { EditDirectory editDirectory; if (editDirectory.DoModal() == IDOK) { m_directoryList.InsertString(-1, editDirectory.m_directory); m_changed = true; } } void DirectoryList::OnModify() { int const selection = m_directoryList.GetCurSel(); if (selection >= 0) { EditDirectory editDirectory; m_directoryList.GetText(selection, editDirectory.m_directory); if (editDirectory.DoModal() == IDOK) { m_directoryList.DeleteString(selection); m_directoryList.InsertString(selection, editDirectory.m_directory); m_directoryList.SetCurSel(selection); m_changed = true; } } } void DirectoryList::OnDelete() { int const selection = m_directoryList.GetCurSel(); if (selection >= 0) { m_directoryList.DeleteString(selection); m_changed = true; } } void DirectoryList::OnOK() { // save off the directories list if (m_changed) { ms_directories.clear(); for (int i = 0; i < m_directoryList.GetCount(); ++i) { CString s; m_directoryList.GetText(i, s); ms_directories.push_back(s); } saveToRegistry(); destroyDirectoryWatchers(); createDirectoryWatchers(); } CDialog::OnOK(); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 7201 | Jeff Grills | Upgrade to VS2005 and the 2008.2 p4api | ||
#1 | 4963 | Jeff Grills |
Major clean up and code reorganization. Add new dialog to control the directories that are watched. Persist the watched directories in the windows registry. Restore the watched directories when the application is run again. Handle login correctly. Change the way error messages are examined to make them significantly more robust. Issue an "info" command at startup to make sure we can communicate with the server and get logged in. Change the icon to the standard P4 icon. Allow the dialog to be minimized to the systray. Clicking on the systray icon will show the dialog again. |