#include "datacenter.h" namespace model { DataCenter* DataCenter::instance = nullptr; DataCenter* DataCenter::getInstance() { if (instance == nullptr) { instance = new DataCenter(); } return instance; } DataCenter::~DataCenter() { //释放所有的成员 //此处就不必判断nullptr,直接delete即可 //c++标准中明确规定了,对nullptr进行delete是合法行为,不会有任何副作用 delete myself; delete friendList; delete chatSessionList; delete memberList; delete applyList; delete recentMessages; delete unreadMessageCount; delete searchUserResult; delete searchMessageResult; } DataCenter::DataCenter() : netClient(this) { //此处只是把这几个hash类型的属性进行new出实例,其他的QList都暂时不实例化 //主要是为了使用nullptr表示非法状态 //对于hash来说,不关心整个的QHash是否是nullptr,而是关心某个key对应的value是否存在 //通过key是否存在,也能表示该值是否有效 recentMessages = new QHash>(); memberList = new QHash>(); unreadMessageCount = new QHash(); //加载数据 loadDataFile(); } void DataCenter::initDataFile() { //构造出文件的路径,使用appData存储文件 QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString filePath = basePath + "/ChatClient.json"; QDir dir; if (!dir.exists(basePath)) { //没有则进行目录的创建 dir.mkpath(basePath); } //构造好文件的路径之后,把文件创建出来 //写的方式打开,并且写入初始内容 QFile file(filePath); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { LOG() << "initDataFile open file failed!!!" << file.errorString(); file.close(); return; } //打开成功,写入初始内容 QString data = "{\n\n}"; file.write(data.toUtf8()); file.close(); } void DataCenter::saveDataFile() { QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString filePath = basePath + "/ChatClient.json"; QFile file(filePath); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { LOG() << "saveDataFile open file failed!!!" << file.errorString(); file.close(); return; } //按照json格式进行数据的写入 //这个对象可以当作map一样来使用 QJsonObject jsonObj; jsonObj["loginSessionId"] = loginSessionId; QJsonObject jsonUnread; for (auto it = unreadMessageCount->begin(); it != unreadMessageCount->end(); ++it) { //注意此处的qt迭代器使用和stl有区别(不是使用first和second) jsonUnread[it.key()] = it.value(); } jsonObj["unread"] = jsonUnread; //把json写入文件 QJsonDocument jsonDoc(jsonObj); QString s = jsonDoc.toJson(); file.write(s.toUtf8()); //关闭文件 file.close(); } //加载文件,在DataCenter实例化就应该执行的 void DataCenter::loadDataFile() { //确保加载之前就针对文件进行初始化操作 QString basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString filePath = basePath + "/ChatClient.json"; //判定文件是否存在,不存在则初始化,并创建出空空白的json文件 QFileInfo fileInfo(filePath); if (!fileInfo.exists()) { initDataFile(); } //按读方式打开文件 QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { LOG() << "loadDataFile open file failed!!!" << file.errorString(); return; } //读取文件内容,解析为 JSON对象 QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll()); if (jsonDoc.isNull()) { LOG() << "loadDataFile parsing the json file failed!!!"; file.close(); return; } QJsonObject jsonObj = jsonDoc.object(); this->loginSessionId = jsonObj["loginSessionId"].toString(); this->unreadMessageCount->clear(); QJsonObject jsonUnread = jsonObj["unread"].toObject(); for (auto it = jsonUnread.begin(); it != jsonUnread.end(); ++it) { this->unreadMessageCount->insert(it.key(), it.value().toInt()); } file.close(); } void DataCenter::getMyselfAsync() { //DataCenter 只是负责“处理数据”, //而真正负责网络进行通信,需要通过NetClient netClient.getMyself(loginSessionId); } UserInfo* DataCenter::getMyselfsync() { return myself; } void DataCenter::resetMyself(std::shared_ptr resp) { if (myself == nullptr) { myself = new UserInfo(); } const bite_im::UserInfo& userInfo = resp->userInfo(); myself->load(userInfo); } } //end namespace model