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/<server name>/<logname>.log and update it every time there's a new chat entry
minor refactor of chatlogpiece
This commit is contained in:
Crystalwarrior 2019-09-16 01:16:37 +03:00
parent 1b36be9dbc
commit bf999f195a
6 changed files with 100 additions and 14 deletions

View File

@ -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;

View File

@ -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

View File

@ -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());

View File

@ -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());

View File

@ -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())

View File

@ -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;