forked from WorldsOfBabylon/ISIS-Messanger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbmain.cpp
93 lines (64 loc) · 2.55 KB
/
dbmain.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
#include "dbmain.hpp"
#include"aes.h"
#include"config.h"
#include<QMessageBox>
DB_ABSTRACT_CPP(main);
void dbmain::install(void){
this->query(
"CREATE TABLE Friends(id INTEGER PRIMARY KEY AUTOINCREMENT, "
"nick varchar(255),"
" publickey varchar(255) );",
"insert into Friends(nick,publickey) values(\"Self\",'Self'); ",
"CREATE TABLE Messages(id INTEGER PRIMARY KEY AUTOINCREMENT, "
"message varhcar(255),"
" `chat_id` id INTEGER, from_me BOOLEAN );",
"insert into Messages(message, `chat_id`, `from_me`) values('Welcome to ISIS-Mess(e)anger, is your self messages', 1, 1); "
);
installed=true;
}
QList<QString> * dbmain::getMessages(const char * nickname){
AES aes( PassOfGuy, UsernameOfGuy );
QList<QString> * returns = new QList<QString>();
//msgs
QSqlQuery msgQuery(this->db);
msgQuery.prepare("SELECT message, from_me FROM Messages WHERE chat_id=:id ;");
int id = getIDFromNickname(nickname);
msgQuery.bindValue(":id", id);
msgQuery.exec();
qDebug() << msgQuery.lastError();
//QSqlRecord rec;
//rec = q.record();
//index = rec.indexOf("message");
//int from_me = rec.indexOf("from_me");
qDebug() << "To first" << msgQuery.value(0).toString();
while( msgQuery.next() ){
qDebug() << "Add";
qDebug() << msgQuery.value(0).toString() << msgQuery.value(1).toString();
if( msgQuery.value(1).toBool())
returns->append( QString("I: ") + ( id == 1 ? msgQuery.value(0).toString() : (char*)aes.decrypt (msgQuery.value(0).toString().toStdString().c_str() ) ) + "\n");
else
returns->append(QString(nickname) + QString(": ") + ( id == 1 ? msgQuery.value(0).toString() : (char*)aes.decrypt (msgQuery.value(0).toString().toStdString().c_str() ) ) + "\n");
}
return returns;
}
void dbmain::addMessage(const char *message, int chat_id, bool FromMe){
AES aes( PassOfGuy, UsernameOfGuy );
this->q.prepare("INSERT INTO Messages(message, chat_id, from_me) values(:message, :chat_id, :from_me);");
char * msgecnrypted = (char*)aes.encrypt(message);
this->q.bindValue(":message", msgecnrypted);
this->q.bindValue(":chat_id", chat_id);
this->q.bindValue(":from_me", FromMe);
this->q.exec();
delete [] msgecnrypted;
}
dbmain::dbmain(bool installing){
getDB("main.sqlite");
if(installing){
qDebug() << "Installing";
this->install();
}
}
dbmain::dbmain()
{
getDB("main.sqlite");
}