Flush current work
[desktopswitcher.git] / mru.cpp
blob0a07e42aa7f1709d16ccc774c8da0b1ca6d211ca
1 // $Id: mru.cpp,v 1.1 2002/05/29 22:06:50 nedko Exp $
2 //
3 // Desktop Switcher
4 // Copyright (C) 2000,2001,2002 Nedko Arnaudov <nedko@users.sourceforge.net>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ph.h"
21 #include "MRU.h"
23 #define ENTRY_BASE 16
25 CMRUList::CMRUList(unsigned int nMaxEntries, LPCTSTR psz_HKCU_MRUKey, HRESULT& rhr)
27 m_nCount = 0;
28 m_hKey = NULL;
29 m_nCount = 0;
30 m_nMaxEntries = nMaxEntries;
32 if (nMaxEntries == 0 || psz_HKCU_MRUKey == NULL)
34 rhr = E_INVALIDARG;
35 return;
38 m_ppszList = new LPTSTR[nMaxEntries];
39 if (!m_ppszList)
41 rhr = E_OUTOFMEMORY;
42 return;
45 ZeroMemory(m_ppszList,nMaxEntries*sizeof(LPTSTR));
47 LONG nError;
49 // open/create the key
50 nError = RegCreateKeyEx(HKEY_CURRENT_USER, psz_HKCU_MRUKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE|KEY_SET_VALUE,NULL,&m_hKey,NULL);
51 if (nError != ERROR_SUCCESS)
53 rhr = HRESULT_FROM_WIN32(nError);
54 return;
57 TCHAR NameBuffer[256];
59 DWORD dwType;
60 DWORD dwEntrySize;
62 while(m_nCount < m_nMaxEntries)
64 _ultot(m_nCount,NameBuffer,ENTRY_BASE);
65 nError = RegQueryValueEx(m_hKey,NameBuffer,NULL,&dwType,NULL,&dwEntrySize);
66 if (nError != ERROR_SUCCESS || dwType != REG_SZ)
67 break;
69 m_ppszList[m_nCount] = (TCHAR *) new BYTE[dwEntrySize];
70 if (!m_ppszList)
72 rhr = E_OUTOFMEMORY;
73 return;
76 nError = RegQueryValueEx(m_hKey,NameBuffer,NULL,&dwType,(BYTE *)m_ppszList[m_nCount],&dwEntrySize);
77 if (nError != ERROR_SUCCESS)
79 delete m_ppszList[m_nCount];
80 m_ppszList[m_nCount] = NULL;
81 break;
84 m_nCount++;
88 CMRUList::~CMRUList()
90 if (m_ppszList)
92 LPTSTR *ppsz = m_ppszList;
93 while(ppsz)
94 delete ppsz++;
96 delete m_ppszList;
99 if (m_hKey)
101 VERIFY(RegCloseKey(m_hKey) == ERROR_SUCCESS);
102 m_hKey = NULL;
106 HRESULT CMRUList::Add(LPCTSTR pszEntry)
108 if (m_hKey == NULL || m_ppszList == NULL || m_nMaxEntries == 0 || m_nCount > m_nMaxEntries)
110 ASSERT(FALSE); // Add method must be called only after successful construction
111 return E_UNEXPECTED;
114 unsigned int nIndex;
115 LPTSTR pszMoveTemp;
116 HRESULT hr;
118 // existing string ???
119 for (nIndex = 0 ; nIndex < m_nCount ; nIndex++)
120 if (strcmp(m_ppszList[nIndex],pszEntry) == 0)
122 if (nIndex != 0)
123 { // move the found entry to the top
124 pszMoveTemp = m_ppszList[nIndex]; // save the found entry
125 while(nIndex)
127 m_ppszList[nIndex] = m_ppszList[nIndex-1];
128 nIndex--;
131 m_ppszList[0] = pszMoveTemp;
133 hr = WriteToRegistry();
135 return SUCCEEDED(hr)?S_FALSE:hr;
138 return S_FALSE;
142 // copy new entry
143 LPTSTR pszNewEntry = new TCHAR[_tcslen(pszEntry)+1];
144 if (!pszNewEntry)
145 return E_OUTOFMEMORY;
147 _tcscpy(pszNewEntry,pszEntry);
149 nIndex = 0;
150 LPTSTR psz = pszNewEntry;
152 // move old entries
153 while(nIndex < m_nCount)
155 pszMoveTemp = m_ppszList[nIndex];
156 ASSERT(pszMoveTemp); // what we are moving ???
157 m_ppszList[nIndex] = psz;
158 psz = pszMoveTemp;
159 nIndex++;
162 // save last entry if we have not reached max entries count
163 if (nIndex < m_nMaxEntries)
165 m_ppszList[nIndex] = psz;
166 m_nCount++;
170 hr = WriteToRegistry();
172 return SUCCEEDED(hr)?S_OK:hr;
175 HRESULT CMRUList::WriteToRegistry()
177 TCHAR NameBuffer[256];
178 unsigned int nIndex;
180 LONG nLastError = ERROR_SUCCESS;
182 // Write the new MRU list into registry
183 for (nIndex = 0 ; nIndex < m_nCount ; nIndex++)
185 _ultot(nIndex,NameBuffer,ENTRY_BASE);
186 LONG nError = RegSetValueEx(m_hKey,NameBuffer,0,REG_SZ,(const BYTE *)m_ppszList[nIndex],(_tcslen(m_ppszList[nIndex])+1)*sizeof(TCHAR));
187 if (nError != ERROR_SUCCESS)
188 nLastError = nError;
191 return HRESULT_FROM_WIN32(nLastError);
194 unsigned int CMRUList::GetCount()
196 return m_nCount;
199 LPCTSTR CMRUList::GetEntry(unsigned int nIndex)
201 if ((nIndex >= m_nCount)||(m_ppszList == NULL))
203 ASSERT(FALSE);
204 return _T("Error");
207 return m_ppszList[nIndex];