/[H6]/AdminDialog.cpp
ViewVC logotype

Contents of /AdminDialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download)
Wed Aug 30 21:57:57 2006 UTC (17 years, 7 months ago) by torben
File size: 6484 byte(s)
Finished (???) administration part - plus made a dummy offline version of the DatabaseLayer class.
1 // AdminDialog.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "h6-udlånssystem.h"
6 #include "AdminDialog.h"
7 #include ".\admindialog.h"
8
9 #include "databaselayer.h"
10 #include "commonstorage.h"
11 #include "Containers.h"
12
13 #include "UserEditDialog.h"
14 #include "EquipmentEditDialog.h"
15
16 // AdminDialog dialog
17
18 IMPLEMENT_DYNAMIC(AdminDialog, CDialog)
19 AdminDialog::AdminDialog(CWnd* pParent /*=NULL*/)
20 : CDialog(AdminDialog::IDD, pParent)
21 {
22 }
23
24 AdminDialog::~AdminDialog()
25 {
26 }
27
28 void AdminDialog::DoDataExchange(CDataExchange* pDX)
29 {
30 CDialog::DoDataExchange(pDX);
31 }
32
33
34 BEGIN_MESSAGE_MAP(AdminDialog, CDialog)
35 ON_BN_CLICKED(IDC_LOGIN, OnBnClickedLogin)
36 ON_BN_CLICKED(IDC_LOGOUT, OnBnClickedLogout)
37 ON_BN_CLICKED(IDC_RDUSERS, OnBnClickedRdusers)
38 ON_BN_CLICKED(IDC_RDEQUIPMENT, OnBnClickedRdequipment)
39 ON_BN_CLICKED(IDC_NEW, OnBnClickedNew)
40 ON_BN_CLICKED(IDC_MODIFY, OnBnClickedModify)
41 ON_BN_CLICKED(IDC_DELETE, OnBnClickedDelete)
42 END_MESSAGE_MAP()
43
44
45 // AdminDialog message handlers
46
47 void AdminDialog::OnCancel()
48 {
49 // TODO: Add your specialized code here and/or call the base class
50
51 //CDialog::OnCancel();
52 }
53
54 void AdminDialog::OnOK()
55 {
56 // TODO: Add your specialized code here and/or call the base class
57
58 //CDialog::OnOK();
59 }
60
61 void AdminDialog::OnBnClickedLogin()
62 {
63 CString username, password;
64 GetDlgItem(IDC_USERNAME)->GetWindowText(username);
65 GetDlgItem(IDC_PASSWORD)->GetWindowText(password);
66
67 if (username.GetLength()>0)
68 {
69 Person p = CommonStorage::Instance()->getDBLayer()->GetPerson(username);
70 if ( (p.inits.GetLength()>0) && (p.pass == password) && (p.isadmin == 1))
71 {
72 CommonStorage::Instance()->setAdmin(true);
73
74 ShowControls(true);
75 ((CButton*)GetDlgItem(IDC_RDUSERS))->SetCheck(true);
76 LoadUsers();
77 }
78 }
79 GetDlgItem(IDC_USERNAME)->SetWindowText("");
80 GetDlgItem(IDC_PASSWORD)->SetWindowText("");
81 }
82
83 void AdminDialog::ShowControls(bool login)
84 {
85 GetDlgItem(IDC_USERNAME)->ShowWindow(!login);
86 GetDlgItem(IDC_PASSWORD)->ShowWindow(!login);
87 GetDlgItem(IDC_STATIC_USERNAME)->ShowWindow(!login);
88 GetDlgItem(IDC_STATIC_PASSWORD)->ShowWindow(!login);
89 GetDlgItem(IDC_LOGIN)->ShowWindow(!login);
90
91 GetDlgItem(IDC_NEW)->ShowWindow(login);
92 GetDlgItem(IDC_MODIFY)->ShowWindow(login);
93 GetDlgItem(IDC_DELETE)->ShowWindow(login);
94 GetDlgItem(IDC_LOGOUT)->ShowWindow(login);
95 GetDlgItem(IDC_RDUSERS)->ShowWindow(login);
96 GetDlgItem(IDC_RDEQUIPMENT)->ShowWindow(login);
97 GetDlgItem(IDC_LIST)->ShowWindow(login);
98 }
99
100 void AdminDialog::OnBnClickedLogout()
101 {
102 CommonStorage::Instance()->setAdmin(false);
103 ShowControls(false);
104 }
105
106 BOOL AdminDialog::OnInitDialog()
107 {
108 CDialog::OnInitDialog();
109
110 // TODO: Add extra initialization here
111 CListCtrl *list = (CListCtrl *) GetDlgItem(IDC_LIST);
112 list->SetExtendedStyle( LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
113
114
115 return TRUE; // return TRUE unless you set the focus to a control
116 // EXCEPTION: OCX Property Pages should return FALSE
117 }
118
119 void AdminDialog::OnBnClickedRdusers()
120 {
121 GetDlgItem(IDC_DELETE)->EnableWindow(true);
122 LoadUsers();
123 }
124
125 void AdminDialog::OnBnClickedRdequipment()
126 {
127 GetDlgItem(IDC_DELETE)->EnableWindow(false);
128 LoadEquipment();
129 }
130
131 void AdminDialog::LoadEquipment(void)
132 {
133 CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST);
134 list->DeleteAllItems();
135 list->DeleteColumn(0);
136 list->DeleteColumn(1);
137
138 list->InsertColumn(0,"Barcode");
139 list->InsertColumn(1,"Name");
140
141 vector<Equipment> buffer = CommonStorage::Instance()->getDBLayer()->GetEquipmentAll();
142
143 for (unsigned int i=0; i < buffer.size(); i++) {
144 int nItem = list->InsertItem(0, buffer[i].barcode);
145 list->SetItemText(nItem,1,buffer[i].name);
146 }
147
148 list->DeleteColumn(2);
149 list->DeleteColumn(3);
150 list->SetColumnWidth(0, LVSCW_AUTOSIZE);
151 list->SetColumnWidth(1, 200);
152 }
153
154
155
156 void AdminDialog::LoadUsers(void)
157 {
158 CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST);
159 list->DeleteAllItems();
160
161 list->DeleteColumn(0);
162 list->DeleteColumn(1);
163
164
165 list->InsertColumn(0,"Inits");
166 list->InsertColumn(1,"Name");
167
168 vector<Person> buffer = CommonStorage::Instance()->getDBLayer()->GetPersonAll();
169
170 for (unsigned int i=0; i < buffer.size(); i++) {
171 int nItem = list->InsertItem(0, buffer[i].inits);
172 list->SetItemText(nItem,1,buffer[i].name);
173 }
174
175 list->DeleteColumn(2);
176 list->DeleteColumn(3);
177
178 list->SetColumnWidth(0, LVSCW_AUTOSIZE);
179 list->SetColumnWidth(1, 220);
180 }
181
182 void AdminDialog::OnBnClickedNew()
183 {
184 if ( ((CButton*)GetDlgItem(IDC_RDUSERS))->GetCheck() ) {
185 UserEditDialog user;
186
187 if (user.DoModal() == IDOK) {
188 CommonStorage::Instance()->getDBLayer()->AddPerson( user.m_currentPerson );
189 LoadUsers();
190 }
191
192 } else { //equipment section
193 EquipmentEditDialog equipment;
194
195 if (equipment.DoModal() == IDOK) {
196 CommonStorage::Instance()->getDBLayer()->AddEquipment( equipment.m_currentEquipment );
197 LoadEquipment();
198 }
199 }
200 }
201
202 void AdminDialog::OnBnClickedModify()
203 {
204 CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST);
205 int sel = list->GetSelectionMark();
206 if (sel == -1) {
207 MessageBox("You must select an user/equipment to modify");
208 return;
209 }
210
211 DatabaseLayer *dblayer = CommonStorage::Instance()->getDBLayer();
212
213 CString selected = list->GetItemText(sel,0);
214
215 if ( ((CButton*)GetDlgItem(IDC_RDUSERS))->GetCheck() ) {
216 UserEditDialog user;
217 user.m_currentPerson = dblayer->GetPerson(selected);
218
219 if (user.DoModal() == IDOK) {
220 dblayer->UpdatePerson( user.m_currentPerson);
221 LoadUsers();
222 }
223
224 } else { //equipment section
225 EquipmentEditDialog equipment;
226 equipment.m_currentEquipment = dblayer->GetEquipment(selected);
227
228 if (equipment.DoModal() == IDOK) {
229 dblayer->UpdateEquipment( equipment.m_currentEquipment );
230 LoadEquipment();
231 }
232 }
233 }
234
235 void AdminDialog::OnBnClickedDelete()
236 {
237 CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST);
238 int sel = list->GetSelectionMark();
239
240 if (sel == -1) {
241 MessageBox("You must select an user to delete");
242 return;
243 }
244
245 CString inits = list->GetItemText(sel,0);
246 CString name = list->GetItemText(sel,1);
247
248 CString question;
249 question.Format("Are you sure you want to delete %s : %s ?", inits,name);
250 if (MessageBox(question, "Delete",MB_YESNO) == IDYES) {
251 Person p = CommonStorage::Instance()->getDBLayer()->GetPerson(inits);
252 CommonStorage::Instance()->getDBLayer()->DeletePerson(p);
253 LoadUsers();
254 }
255
256 }

  ViewVC Help
Powered by ViewVC 1.1.20