[r5083]: tags / V3_12 / RUEList.cpp Maximize Restore History

Download this file

RUEList.cpp    241 lines (189 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright (c) 2003-2008 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/// \file RUEList.cpp
//-----------------------------------------------------------------------------
#include <algorithm> // for find_if
#include "PasswordSafe.h"
#include "ThisMfcApp.h"
#include "DboxMain.h"
#include "RUEList.h"
#include "corelib/PWScore.h"
#include "resource3.h" // String resources
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
RUEntry::RUEntry(const uuid_array_t &aRUEuuid)
{
::memcpy(RUEuuid, aRUEuuid, sizeof(uuid_array_t));
}
bool RUEntry::operator()(const RUEntry &re)
{
return ::memcmp(RUEuuid, re.RUEuuid,
sizeof(uuid_array_t)) == 0;
}
//-----------------------------------------------------------------------------
CRUEList::CRUEList() : m_core(app.m_core), m_maxentries(0), m_pDbx(app.m_maindlg)
{
}
// Accessors
void CRUEList::SetMax(size_t newmax)
{
m_maxentries = newmax;
size_t numentries = m_RUEList.size();
if (newmax < numentries)
m_RUEList.resize(newmax);
}
bool CRUEList::GetAllMenuItemStrings(vector<RUEntryStringImage> &ListofAllMenuStrings) const
{
RUEntryStringImage itemstringimage;
bool retval = false;
RUEListConstIter iter;
for (iter = m_RUEList.begin(); iter != m_RUEList.end(); iter++) {
ItemListConstIter pw_listpos = m_core.Find(iter->RUEuuid);
if (pw_listpos == m_core.GetEntryEndIter()) {
itemstringimage.string = _T("");
itemstringimage.image = -1;
} else {
const CItemData &ci = m_core.GetEntry(pw_listpos);
CMyString group = ci.GetGroup();
CMyString title = ci.GetTitle();
CMyString user = ci.GetUser();
if (group.IsEmpty())
group = _T("*");
if (title.IsEmpty())
title = _T("*");
if (user.IsEmpty())
user = _T("*");
itemstringimage.string = MRE_FS + group + MRE_FS + title + MRE_FS + user + MRE_FS;
itemstringimage.image = m_pDbx->GetEntryImage(ci);
}
ListofAllMenuStrings.push_back(itemstringimage);
retval = true;
}
return retval;
}
bool CRUEList::GetMenuItemString(size_t index, CMyString &itemstring) const
{
if (m_RUEList.empty() || index > m_RUEList.size()) {
itemstring = _T("");
return false;
}
RUEntry m_ruentry = m_RUEList[index];
ItemListConstIter pw_listpos = m_core.Find(m_ruentry.RUEuuid);
if (pw_listpos == m_core.GetEntryEndIter()) {
itemstring = _T("");
return false;
}
const CItemData ci = m_core.GetEntry(pw_listpos);
CMyString group = ci.GetGroup();
CMyString title = ci.GetTitle();
CMyString user = ci.GetUser();
if (group.IsEmpty())
group = _T("*");
if (title.IsEmpty())
title = _T("*");
if (user.IsEmpty())
user = _T("*");
itemstring = MRE_FS + group + MRE_FS + title + MRE_FS + user + MRE_FS;
return true;
}
bool CRUEList::GetMenuItemString(const uuid_array_t &RUEuuid,
CMyString &itemstring) const
{
if (m_RUEList.empty()) {
itemstring = _T("");
return false;
}
ItemListConstIter pw_listpos = m_core.Find(RUEuuid);
if (pw_listpos == m_core.GetEntryEndIter()) {
itemstring = _T("");
return false;
}
const CItemData ci = m_core.GetEntry(pw_listpos);
CMyString group = ci.GetGroup();
CMyString title = ci.GetTitle();
CMyString user = ci.GetUser();
if (group.IsEmpty())
group = _T("*");
if (title.IsEmpty())
title = _T("*");
if (user.IsEmpty())
user = _T("*");
itemstring = MRE_FS + group + MRE_FS + title + MRE_FS + user + MRE_FS;
return true;
}
bool CRUEList::AddRUEntry(const uuid_array_t &RUEuuid)
{
/*
* If the entry's already there, do nothing, return true.
* Otherwise, add it, removing last entry if needed to
* maintain size() <= m_maxentries invariant
*/
if (m_maxentries == 0) return false;
RUEntry match_uuid(RUEuuid);
RUEListIter iter = find_if(m_RUEList.begin(), m_RUEList.end(), match_uuid);
if (iter == m_RUEList.end()) {
if (m_RUEList.size() == m_maxentries) // if already maxed out - delete oldest entry
m_RUEList.pop_back();
m_RUEList.push_front(match_uuid); // put it at the top
} else {
m_RUEList.erase(iter); // take it out
m_RUEList.push_front(match_uuid); // put it at the top
}
return true;
}
bool CRUEList::DeleteRUEntry(size_t index)
{
if ((m_maxentries == 0) ||
m_RUEList.empty() ||
(index > (m_maxentries - 1)) ||
(index > (m_RUEList.size() - 1))) return false;
m_RUEList.erase(m_RUEList.begin() + index);
return true;
}
bool CRUEList::DeleteRUEntry(const uuid_array_t &RUEuuid)
{
if ((m_maxentries == 0) || m_RUEList.empty())
return false;
RUEntry match_uuid(RUEuuid);
RUEListIter iter = find_if(m_RUEList.begin(), m_RUEList.end(), match_uuid);
if (iter != m_RUEList.end()) {
m_RUEList.erase(iter);
}
return true;
}
bool CRUEList::GetPWEntry(size_t index, CItemData &ci){
if ((m_maxentries == 0) ||
m_RUEList.empty() ||
(index > (m_maxentries - 1)) ||
(index > (m_RUEList.size() - 1))) return false;
const RUEntry &re_FoundEntry = m_RUEList[index];
ItemListConstIter pw_listpos = m_core.Find(re_FoundEntry.RUEuuid);
if (pw_listpos == m_core.GetEntryEndIter()) {
// Entry does not exist anymore!
m_RUEList.erase(m_RUEList.begin() + index);
AfxMessageBox(IDS_CANTPROCESSENTRY);
}
if (pw_listpos == m_core.GetEntryEndIter())
return false;
ci = m_core.GetEntry(pw_listpos);
return true;
}
CRUEList& CRUEList::operator=(const CRUEList &that)
{
if (this != &that) {
m_maxentries = that.m_maxentries;
m_RUEList = that.m_RUEList;
}
return *this;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------