ReactOS  0.4.14-dev-376-gaedba84
ACLCustomMRU.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS browseui
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Custom MRU AutoComplete List
5  * COPYRIGHT: Copyright 2017 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
11  :m_bDirty(false)
12 {
13 }
14 
16 {
17  PersistMRU();
18  m_Key.Close();
19 }
20 
22 {
23  WCHAR Key[2] = { 0, 0 };
24 
25  if (!m_bDirty)
26  return;
27  m_bDirty = false;
28 
29  if (m_Key.m_hKey)
30  {
31  m_Key.SetStringValue(L"MRUList", m_MRUList);
32  for (int Index = 0; Index < m_MRUList.GetLength(); ++Index)
33  {
34  Key[0] = Index + 'a';
36  }
37  }
38 }
39 
40 // *** IACLCustomMRU methods ***
42 {
43  LSTATUS Status = m_Key.Create(HKEY_CURRENT_USER, pwszMRURegKey);
44  if (Status != ERROR_SUCCESS)
45  return HRESULT_FROM_WIN32(Status);
46 
47  m_MRUData.RemoveAll();
48  dwMax = max(0, dwMax);
49  dwMax = min(29, dwMax);
50  while (dwMax--)
51  m_MRUData.Add(CStringW());
52 
53  WCHAR MRUList[40];
54  ULONG nChars = _countof(MRUList);
55 
56  Status = m_Key.QueryStringValue(L"MRUList", MRUList, &nChars);
57  if (Status != ERROR_SUCCESS)
58  return S_OK;
59 
60  if (nChars > 0 && MRUList[nChars-1] == '\0')
61  nChars--;
62 
63  if (nChars > (ULONG)m_MRUData.GetSize())
64  return S_OK;
65 
66  for (ULONG n = 0; n < nChars; ++n)
67  {
68  if (MRUList[n] >= 'a' && MRUList[n] <= '}' && m_MRUList.Find(MRUList[n]) < 0)
69  {
70  WCHAR Key[2] = { MRUList[n], NULL };
71  WCHAR Value[MAX_PATH * 2];
72  ULONG nValueChars = _countof(Value);
73 
74  m_MRUList += MRUList[n];
75  int Index = MRUList[n] - 'a';
76 
77  if (Index < m_MRUData.GetSize())
78  {
79  Status = m_Key.QueryStringValue(Key, Value, &nValueChars);
80  if (Status == ERROR_SUCCESS)
81  {
82  m_MRUData[Index] = CStringW(Value, nValueChars);
83  }
84  }
85  }
86  }
87 
88  return S_OK;
89 }
90 
92 {
93  ATLASSERT(m_MRUData.GetSize() <= m_MRUList.GetLength());
94 
95  m_bDirty = true;
96 
97  CStringW NewElement = pwszEntry;
98  WCHAR Key[2] = { 0, 0 };
99  int Index = m_MRUData.Find(NewElement);
100  if (Index >= 0)
101  {
102  /* Move the key to the front */
103  Key[0] = Index + 'a';
104  m_MRUList.Replace(Key, L"");
105  m_MRUList = Key + m_MRUList;
106  return S_OK;
107  }
108 
109  int TotalLen = m_MRUList.GetLength();
110  if (m_MRUData.GetSize() == TotalLen)
111  {
112  /* Find oldest element, move that to the front */
113  Key[0] = m_MRUList[TotalLen-1];
114  m_MRUList = Key + m_MRUList.Left(TotalLen-1);
115  Index = Key[0] - 'a';
116  }
117  else
118  {
119  /* Find the first empty entry */
120  for (Index = 0; Index < m_MRUData.GetSize(); ++Index)
121  {
122  if (m_MRUData[Index].IsEmpty())
123  break;
124  }
125  Key[0] = Index + 'a';
126  m_MRUList = Key + m_MRUList;
127  }
128  m_MRUData[Index] = NewElement;
129 
130  PersistMRU();
131  return S_OK;
132 }
133 
int GetLength() const
Definition: atlsimpstr.h:356
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
#define max(a, b)
Definition: svc.c:63
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define ATLASSERT(x)
Definition: CComVariant.cpp:9
#define _countof(array)
Definition: fontsub.cpp:30
#define HKEY_CURRENT_USER
Definition: winreg.h:11
GLdouble n
Definition: glext.h:7729
CSimpleArray< CStringW > m_MRUData
Definition: ACLCustomMRU.h:18
LONG Close()
Definition: atlbase.h:1053
HKEY m_hKey
Definition: atlbase.h:1013
virtual HRESULT STDMETHODCALLTYPE Initialize(LPCWSTR pwszMRURegKey, DWORD dwMax)
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
#define MAX_PATH
Definition: compat.h:26
static const UCHAR Index[8]
Definition: usbohci.c:18
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
unsigned long DWORD
Definition: ntddk_ex.h:95
LONG QueryStringValue(LPCTSTR pszValueName, LPTSTR pszValue, ULONG *pnChars)
Definition: atlbase.h:1149
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
static const WCHAR L[]
Definition: oid.c:1250
virtual HRESULT STDMETHODCALLTYPE AddMRUString(LPCWSTR pwszEntry)
LONG Create(HKEY hKeyParent, LPCTSTR lpszKeyName, LPTSTR lpszClass=REG_NONE, DWORD dwOptions=REG_OPTION_NON_VOLATILE, REGSAM samDesired=KEY_READ|KEY_WRITE, LPSECURITY_ATTRIBUTES lpSecAttr=NULL, LPDWORD lpdwDisposition=NULL)
Definition: atlbase.h:1091
#define false
Definition: stdbool.h:38
Status
Definition: gdiplustypes.h:24
int Replace(PCXSTR pszOld, PCXSTR pszNew)
Definition: cstringt.h:696
#define S_OK
Definition: intsafe.h:59
#define min(a, b)
Definition: monoChain.cc:55
int Find(_In_ PCXSTR pszSub, _In_opt_ int iStart=0) const
Definition: cstringt.h:563
unsigned int ULONG
Definition: retypes.h:1
CAtlStringW CStringW
Definition: atlstr.h:130
CRegKey m_Key
Definition: ACLCustomMRU.h:16
CStringT Left(int nCount) const
Definition: cstringt.h:643
LONG SetStringValue(LPCTSTR pszValueName, LPCTSTR pszValue, DWORD dwType=REG_SZ)
Definition: atlbase.h:1224
CStringW m_MRUList
Definition: ACLCustomMRU.h:17