-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgrouppopup.cpp
More file actions
190 lines (160 loc) · 6.35 KB
/
grouppopup.cpp
File metadata and controls
190 lines (160 loc) · 6.35 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
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QDebug>
#include "grouppopup.h"
#include "ui_grouppopup.h"
/*=================================================================================================================================*/
// class GroupPopUp-Specific Methods
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Constructor |*/
/*'------------------------'*/
/* Purpose: Default constructor
* Postconditions: New create group dialog is created
*/
GroupPopUp::GroupPopUp(QWidget *parent) :
QDialog(parent),
ui(new Ui::GroupPopUp)
{
ui->setupUi(this);
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Destructor |*/
/*'------------------------'*/
/* Purpose: Default destructor
* Postconditions: Destroys this object
*/
GroupPopUp::~GroupPopUp()
{
delete ui;
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Accessors |*/
/*'------------------------'*/
/* Purpose: Sets current user calling dialog box
* Postconditions: myuser = u
*/
void GroupPopUp::setUser(QString u)
{
myuser = u;
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Accessors |*/
/*'------------------------'*/
/* Purpose: Returns string value of newly created
* group's ID
* Postconditions: groupID is returned in type QString
*/
QString GroupPopUp::getNewGroupID()
{
QSqlQuery *query = new QSqlQuery(myconn.db);
QString result;
query->prepare("SELECT MAX(ID) FROM innodb.GROUPS");
query->exec();
query->first();
result = query->value(0).toString();
return result;
}
/*=================================================================================================================================*/
// SLOTS
/*=================================================================================================================================*/
/* Purpose: Initializes add friends list
* Postconditions: addFriendsList is filled with all
* users (other than myuser) from database
* in a checkbox format
* Returns bool indicating initialization of addFriendsList
*/
bool GroupPopUp::loadAddFriendsList()
{
QSqlQuery *query = new QSqlQuery(myconn.db);
QString result;
query->prepare("SELECT username AS \"User\" FROM innodb.USERS WHERE username !='" +myuser+ "'");
/* If query executes,
* Iterate through each query result
* Create new list item init. to username
* Make each new list item a checkbox
* Else
* Output last query error to console
*/
if(query->exec())
{
while(query->next())
{
QListWidgetItem *newItem = new QListWidgetItem(query->value(0).toString(), ui->addFriendsList); // create new list item, add to addFriendsList
newItem->setFlags(newItem->flags() | Qt::ItemIsUserCheckable); // make new item a checkable item
newItem->setCheckState(Qt::Unchecked); // make it unchecked @ load
}
return true;
}
else
{
QMessageBox::critical(this,tr("error::"),query->lastError().text());
return false;
}
}
/* Purpose: Creates new group using group name indicated
* (MUST ALSO-->) and sends invites to all users
* checked from add friends list
* Postconditions: New group name inserted into innodb.GROUPS
* myuser inserted into innodb.GROUP_MEMBERS as first group
* member of new group
* (MUST ALSO-->) Sends invites to all checked users in addFriendsList
*/
void GroupPopUp::on_buttonBox_accepted()
{
QSqlQuery query, memberQuery;
QString groupName = ui->groupName->toPlainText(); // grab new group name
QString newGroupID; // to associate new group with myuser
/* If group name is blank
* Don't create group
* Print error onto console
*/
if(groupName.isEmpty())
{
QMessageBox MsgBox;
MsgBox.setWindowTitle("Where is your Group Name?");
MsgBox.setText("You need a Group Name!");
MsgBox.exec();
qDebug() << "Error: Name not entered. Group not created.";
return;
}
query.exec("INSERT INTO innodb.GROUPS(name) VALUES ('" +groupName+ "')"); // Create new group
newGroupID = getNewGroupID(); // grab new group's groupID
memberQuery.exec("INSERT INTO innodb.GROUP_MEMBERS(username, groupID) VALUES ('" +myuser+ "','" +newGroupID+ "')"); // Insert new entry into GROUP_MEMBERS
/* If queries are active
* Print insertion status onto console
* Else
* Print error onto consol
*/
if (query.isActive() && memberQuery.isActive()) {
qDebug("Inserted event into database.");
}
else {
qDebug() << query.lastError().text();
qDebug() << memberQuery.lastError().text();
}
// Identify users selected in add friends list
// Add users to group
for(int i = 0; i < ui->addFriendsList->count(); i++){
//qDebug() << "i is this: " << i;
QListWidgetItem *item = ui->addFriendsList->item(i);
if(item->checkState()==Qt::Checked){
QString member = ui->addFriendsList->item(i)->text();
memberQuery.exec("INSERT INTO innodb.GROUP_MEMBERS(username, groupID) VALUES ('" +member+ "','" +newGroupID+ "')");
}
}
accepted = true;
GroupPopUp::close();
}
/* Purpose: Closes dialog when Cancel is clicked
* Postcondtions: Sets accepted to false and closes dialog
*/
void GroupPopUp::on_buttonBox_rejected()
{
accepted = false;
GroupPopUp::close();
}