-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuseUp.cpp
executable file
·243 lines (218 loc) · 7.67 KB
/
FuseUp.cpp
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
241
242
243
#include "FuseUp.h"
#include "useredit.h"
#include "uploaddialog.h"
#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QSqlDatabase>
#include <QDir>
#include <QDateTime>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>
#include <QNetworkAccessManager>
FuseUp::FuseUp()
{
ui.setupUi(this);
// Setup shortcuts.
ui.AddButton->setShortcut(QKeySequence(Qt::Key_A));
ui.EditButton->setShortcut(QKeySequence(Qt::Key_F2));
ui.DeleteButton->setShortcut(QKeySequence(Qt::Key_Delete));
setupNewDatabase();
// Connect the buttons to their slots.
connect(ui.AddButton, SIGNAL(pressed()), this, SLOT(showAddDialog()));
connect(ui.EditButton, SIGNAL(pressed()), this, SLOT(editDatabaseUser()));
connect(ui.DeleteButton, SIGNAL(pressed()), this, SLOT(deleteDatabaseUser()));
connect(ui.UploadButton, SIGNAL(pressed()), this, SLOT(uploadDatabase()));
}
void FuseUp::deleteDatabaseUser()
{
foreach(QTableWidgetItem* item, ui.UsersList->selectedItems())
{
if(item->column() == 0)
{
// we found the column.. delete the record...
if(QMessageBox::question(this, "Confirm Deletion",
"Are you sure you want to delete this user?",
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
return;
QSqlQuery query(QSqlDatabase::database("main"));
if(!query.exec("DELETE FROM user WHERE id=\'" + item->text() + "\';"))
{
showQueryError(query);
}
updateUserList();
return;
}
}
}
void FuseUp::editDatabaseUser()
{
FuseMember mem;
foreach(QTableWidgetItem* item, ui.UsersList->selectedItems())
{
switch(item->column())
{
case(0):
mem.userID = item->text();
break;
case(1):
mem.firstName = item->text();
break;
case(2):
mem.lastName = item->text();
break;
case(3):
mem.phoneNumber = item->text();
break;
case(4):
mem.emailAddress = item->text();
break;
case(5):
mem.groups = item->text().split(" ");
break;
case(6):
mem.merch = item->text().split(" ");
break;
}
}
// Delete that ID for editing.
userEdit dlg(this, 0, &mem);
if(dlg.exec() == QDialog::Accepted)
{
QSqlQuery query(QSqlDatabase::database("main"));
if(!query.exec("UPDATE user SET id=\'" + dlg.returnData.userID + "\' ,"
"firstName=\'" + dlg.returnData.firstName + "\', "
"lastName=\'" + dlg.returnData.lastName + "\' , "
"phoneNumber=\'" + dlg.returnData.phoneNumber + "\' , "
"emailAddress=\'" + dlg.returnData.emailAddress + "\' ,"
"groups=\'" + dlg.groupsReturnData + "\' , "
"merch=\'" + dlg.MerchReturnData + "\' "
"WHERE id= \'" + mem.userID + "\';"))
{
showQueryError(query);
}
updateUserList();
}
}
void FuseUp::showAddDialog()
{
userEdit dlg(this);
if(dlg.exec() == QDialog::Accepted)
{
QSqlQuery query(QSqlDatabase::database("main"));
if(!query.exec("INSERT INTO user VALUES (\'" + dlg.returnData.userID + "\' , \'" +
dlg.returnData.firstName + "\', \'" + dlg.returnData.lastName + "\' , \'" +
dlg.returnData.phoneNumber + "\' , \'" + dlg.returnData.emailAddress + "\' , \'" +
dlg.groupsReturnData + "\' , \'" + dlg.MerchReturnData + "\')"))
{
showQueryError(query);
}
updateUserList();
}
}
void FuseUp::uploadDatabase()
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
uploadDialog *dlg = new uploadDialog(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), dlg, SLOT(uploadFinish(QNetworkReply*)));
QFile *file = new QFile(databasePath, this);
file->open(QFile::ReadOnly);
manager->post(QNetworkRequest(QUrl("http://studio.fusefm.co.uk/dbupload/upload.php")), file);
if(dlg->exec() == QDialog::Accepted)
{
// close the database.
QSqlDatabase db = QSqlDatabase::database("main");
db.close();
// Move the database file.
QDir dbDir(QDir::homePath());
// Backup the old database.
if(!dbDir.rename(".member.db", ".member.db." +
QDateTime::currentDateTime().toString(Qt::ISODate)))
QMessageBox::warning(this, "Database move", "Could not backup old database.");
// Remove the old connection.
QSqlDatabase::removeDatabase("main");
// Setup a new database.
setupNewDatabase();
}
file->close();
}
void FuseUp::showQueryError(QSqlQuery& query)
{
QMessageBox::critical(this, "SQL Error",
"Error, there was a SQLITE problem: " +
query.lastError().text());
}
void FuseUp::updateUserList()
{
ui.UsersList->clearContents();
QSqlQuery query(QSqlDatabase::database("main"));
// Populate the view with any retained data.
if(!query.exec("SELECT * FROM user;"))
{
showQueryError(query);
}
int fIDuserID = query.record().indexOf("id");
int fIDFirstName = query.record().indexOf("firstName");
int fIDLastName = query.record().indexOf("lastName");
int fIDPhoneNumber = query.record().indexOf("phoneNumber");
int fIDEmailAddress = query.record().indexOf("emailAddress");
int fIDMerch = query.record().indexOf("merch");
int fIDGroups = query.record().indexOf("groups");
for(int i = 0; query.next(); i++)
{
ui.UsersList->setRowCount(i + 1);
QTableWidgetItem* IDItem = new QTableWidgetItem(query.value(fIDuserID).toString());
QTableWidgetItem* FirstNameItem = new QTableWidgetItem(query.value(fIDFirstName).toString());
QTableWidgetItem* LastNameItem = new QTableWidgetItem(query.value(fIDLastName).toString());
QTableWidgetItem* PhoneNumberItem = new QTableWidgetItem(query.value(fIDPhoneNumber).toString());
QTableWidgetItem* EmailAddressItem = new QTableWidgetItem(query.value(fIDEmailAddress).toString());
QTableWidgetItem* MerchItem = new QTableWidgetItem(query.value(fIDMerch).toString());
QTableWidgetItem* GroupsItem = new QTableWidgetItem(query.value(fIDGroups).toString());
IDItem->setFlags(IDItem->flags() &~ Qt::ItemIsEditable);
FirstNameItem->setFlags(IDItem->flags());
LastNameItem->setFlags(IDItem->flags());
PhoneNumberItem->setFlags(IDItem->flags());
EmailAddressItem->setFlags(IDItem->flags());
MerchItem->setFlags(IDItem->flags());
GroupsItem->setFlags(IDItem->flags());
ui.UsersList->setItem(i, 0, IDItem);
ui.UsersList->setItem(i, 1, FirstNameItem);
ui.UsersList->setItem(i, 2, LastNameItem);
ui.UsersList->setItem(i, 3, PhoneNumberItem);
ui.UsersList->setItem(i, 4, EmailAddressItem);
ui.UsersList->setItem(i, 5, MerchItem);
ui.UsersList->setItem(i, 6, GroupsItem);
}
ui.UsersList->resizeColumnsToContents();
}
void FuseUp::setupNewDatabase()
{
// Create a database.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "main");
databasePath = QDir::homePath() + "//.member.db";
db.setDatabaseName(databasePath);
if(!db.open())
{
QMessageBox::critical(this, "Error opening database.",
"Error, could not open database: " + db.lastError().text());
}
// Create some tables and shiz.
QSqlQuery query(QSqlDatabase::database("main"));
if(!query.exec("CREATE TABLE IF NOT EXISTS user("
"id TEXT,"
"firstName TEXT,"
"lastName TEXT,"
"phoneNumber TEXT,"
"emailAddress TEXT,"
"merch TEXT,"
"groups TEXT);"))
{
showQueryError(query);
}
updateUserList();
}
FuseUp::~FuseUp()
{}
#include "FuseUp.moc"