From bf999f195a0be20519356644df1940cf18d905e7 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 16 Sep 2019 01:16:37 +0300 Subject: [PATCH] Add file reading, writing and appending functions that create folders if bool is true Fix server_address not being properly created in packet distribution Create a log file when you join a server in the logs//.log and update it every time there's a new chat entry minor refactor of chatlogpiece --- include/aoapplication.h | 12 +++++++ include/chatlogpiece.h | 4 +-- src/chatlogpiece.cpp | 15 ++++---- src/courtroom.cpp | 6 ++-- src/packet_distribution.cpp | 9 +++-- src/text_file_functions.cpp | 68 +++++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 14 deletions(-) diff --git a/include/aoapplication.h b/include/aoapplication.h index e63ab8e..24dc35c 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -201,6 +201,15 @@ public: //Returns the list of words in callwords.ini QStringList get_call_words(); + //Process a file and return its text as a QString + QString read_file(QString filename); + + //Write text to file. make_dir would auto-create the directory if it doesn't exist. + bool write_to_file(QString p_text, QString p_file, bool make_dir = false); + + //Append text to the end of the file. make_dir would auto-create the directory if it doesn't exist. + bool append_to_file(QString p_text, QString p_file, bool make_dir = false); + //Appends the argument string to serverlist.txt void write_to_serverlist_txt(QString p_line); @@ -337,6 +346,9 @@ public: // Get the message for the CM for casing alerts. QString get_casing_can_host_cases(); + //The file name of the log file in base/logs. + QString log_filename; + private: const int RELEASE = 2; const int MAJOR_VERSION = 6; diff --git a/include/chatlogpiece.h b/include/chatlogpiece.h index 34c5926..303c235 100644 --- a/include/chatlogpiece.h +++ b/include/chatlogpiece.h @@ -14,7 +14,7 @@ public: QString get_name(); QString get_showname(); QString get_message(); - bool get_is_song(); + bool is_song(); QDateTime get_datetime(); QString get_datetime_as_string(); @@ -25,7 +25,7 @@ private: QString showname; QString message; QDateTime datetime; - bool is_song; + bool p_is_song; }; #endif // CHATLOGPIECE_H diff --git a/src/chatlogpiece.cpp b/src/chatlogpiece.cpp index 6c861f0..eb7c188 100644 --- a/src/chatlogpiece.cpp +++ b/src/chatlogpiece.cpp @@ -5,7 +5,7 @@ chatlogpiece::chatlogpiece() name = "UNKNOWN"; showname = "UNKNOWN"; message = "UNKNOWN"; - is_song = false; + p_is_song = false; datetime = QDateTime::currentDateTime().toUTC(); } @@ -14,7 +14,7 @@ chatlogpiece::chatlogpiece(QString p_name, QString p_showname, QString p_message name = p_name; showname = p_showname; message = p_message; - is_song = p_song; + p_is_song = p_song; datetime = QDateTime::currentDateTime().toUTC(); } @@ -23,7 +23,7 @@ chatlogpiece::chatlogpiece(QString p_name, QString p_showname, QString p_message name = p_name; showname = p_showname; message = p_message; - is_song = p_song; + p_is_song = p_song; datetime = p_datetime.toUTC(); } @@ -47,9 +47,9 @@ QDateTime chatlogpiece::get_datetime() return datetime; } -bool chatlogpiece::get_is_song() +bool chatlogpiece::is_song() { - return is_song; + return p_is_song; } QString chatlogpiece::get_datetime_as_string() @@ -57,18 +57,17 @@ QString chatlogpiece::get_datetime_as_string() return datetime.toString(); } - QString chatlogpiece::get_full() { QString full = "["; full.append(get_datetime_as_string()); - full.append(" UTC] "); + full.append("] "); full.append(get_showname()); full.append(" ("); full.append(get_name()); full.append(")"); - if (is_song) + if (p_is_song) full.append(" has played a song: "); full.append(get_message()); diff --git a/src/courtroom.cpp b/src/courtroom.cpp index f54cc24..433041c 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -1382,6 +1382,7 @@ void Courtroom::handle_chatmessage(QStringList *p_contents) chatlogpiece* temp = new chatlogpiece(ao_app->get_showname(char_list.at(f_char_id).name), f_showname, ": " + m_chatmessage[MESSAGE], false); ic_chatlog_history.append(*temp); + ao_app->append_to_file(temp->get_full(), ao_app->log_filename, true); while(ic_chatlog_history.size() > log_maximum_blocks && log_maximum_blocks > 0) { @@ -2689,6 +2690,7 @@ void Courtroom::handle_song(QStringList *p_contents) { chatlogpiece* temp = new chatlogpiece(str_char, str_show, f_song, true); ic_chatlog_history.append(*temp); + ao_app->append_to_file(temp->get_full(), ao_app->log_filename, true); while(ic_chatlog_history.size() > log_maximum_blocks && log_maximum_blocks > 0) { @@ -3638,14 +3640,14 @@ void Courtroom::on_showname_enable_clicked() foreach (chatlogpiece item, ic_chatlog_history) { if (ui_showname_enable->isChecked()) { - if (item.get_is_song()) + if (item.is_song()) append_ic_text(item.get_message(), item.get_showname(), true); else append_ic_text(item.get_message(), item.get_showname()); } else { - if (item.get_is_song()) + if (item.is_song()) append_ic_text(item.get_message(), item.get_name(), true); else append_ic_text(item.get_message(), item.get_name()); diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 6059edb..72840f7 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -251,7 +251,8 @@ void AOApplication::server_packet_received(AOPacket *p_packet) if (selected_server >= 0 && selected_server < server_list.size()) { auto info = server_list.at(selected_server); server_name = info.name; - server_address = QString("%1:%2").arg(info.ip, info.port); + server_address = QString("%1:%2").arg(info.ip, QString::number(info.port)); + qDebug() << server_address; window_title += ": " + server_name; } } @@ -260,7 +261,8 @@ void AOApplication::server_packet_received(AOPacket *p_packet) if (selected_server >= 0 && selected_server < favorite_list.size()) { auto info = favorite_list.at(selected_server); server_name = info.name; - server_address = info.ip + info.port; + server_address = QString("%1:%2").arg(info.ip, QString::number(info.port)); + qDebug() << server_address; window_title += ": " + server_name; } } @@ -280,6 +282,9 @@ void AOApplication::server_packet_received(AOPacket *p_packet) send_server_packet(f_packet); + //Remove any characters not accepted in folder names for the server_name here + this->log_filename = QDateTime::currentDateTime().toUTC().toString("'logs/" + server_name.remove(QRegExp("[\\\\/:*?\"<>|]")) + "/'ddd MMMM yyyy hh.mm.ss t'.log'"); + this->write_to_file("Joined server " + server_name + " on address " + server_address +" on " + QDateTime::currentDateTime().toUTC().toString(), log_filename, true); QCryptographicHash hash(QCryptographicHash::Algorithm::Sha256); hash.addData(server_address.toUtf8()); if (is_discord_enabled()) diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index d286c03..ae17ac5 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -95,6 +95,74 @@ QStringList AOApplication::get_call_words() return return_value; } +QString AOApplication::read_file(QString filename) +{ + QFile f_log(filename); + + if(!f_log.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qDebug() << "Couldn't open" << filename; + return ""; + } + + QTextStream in(&f_log); + QString text = in.readAll(); + f_log.close(); + return text; +} + +bool AOApplication::write_to_file(QString p_text, QString p_file, bool make_dir) +{ + QString path = QFileInfo(p_file).path(); + if(make_dir) + { + //Create the dir if it doesn't exist yet + QDir dir(path); + if (!dir.exists()) + if (!dir.mkpath(".")) + return false; + } + + QFile f_log(p_file); + if(f_log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) + { + QTextStream out(&f_log); + + out << p_text; + + f_log.flush(); + f_log.close(); + return true; + } + return false; +} + +bool AOApplication::append_to_file(QString p_text, QString p_file, bool make_dir) +{ + QString path = QFileInfo(p_file).path(); + //Create the dir if it doesn't exist yet + if(make_dir) + { + QDir dir(path); + if (!dir.exists()) + if (!dir.mkpath(".")) + return false; + } + + QFile f_log(p_file); + if(f_log.open(QIODevice::WriteOnly | QIODevice::Append)) + { + QTextStream out(&f_log); + + out << "\r\n" << p_text; + + f_log.flush(); + f_log.close(); + return true; + } + return false; +} + void AOApplication::write_to_serverlist_txt(QString p_line) { QFile serverlist_txt;