From b2bfcedca995d130ba670f20e8a2cb88e96ce750 Mon Sep 17 00:00:00 2001 From: keshavbhatt Date: Wed, 7 Apr 2021 21:48:40 +0530 Subject: utils class update --- src/utils.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/utils.h | 55 +++----------------------- 2 files changed, 123 insertions(+), 53 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index 543b837..eb06cf7 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,6 +1,10 @@ #include "utils.h" +#include #include +#include +#include #include +#include utils::utils(QObject *parent) : QObject(parent) { @@ -146,13 +150,60 @@ QString utils::returnPath(QString pathname) return _data_path+"/"+pathname+"/"; } +QString utils::EncodeXML(const QString &encodeMe){ + + QString temp; + int length = encodeMe.size(); + for (int index = 0; index < length; index++) + { + QChar character(encodeMe.at(index)); + + switch (character.unicode()) + { + case '&': + temp += "&"; break; + + case '\'': + temp += "'"; break; + + case '"': + temp += """; break; + + case '<': + temp += "<"; break; + + case '>': + temp += ">"; break; + + default: + temp += character; + break; + } + } + + return temp; +} + +QString utils::DecodeXML(const QString &decodeMe) { + + QString temp(decodeMe); + + temp.replace("&", "&"); + temp.replace("'", "'"); + temp.replace(""", "\""); + temp.replace("<", "<"); + temp.replace(">", ">"); + + return temp; +} + QString utils::htmlToPlainText(QString str){ QString out; - QTextDocument text; - text.setHtml(str); - out = text.toPlainText(); - text.deleteLater(); + QTextDocument text; + text.setHtml(str); + out = text.toPlainText(); + text.deleteLater(); return out .replace("\\\"","'") .replace("&","&") .replace(">",">") @@ -160,3 +211,65 @@ QString utils::htmlToPlainText(QString str){ .replace("'","'"); } + +QString utils::appDebugInfo() +{ + + QStringList debugInfo; + debugInfo << "

"+QApplication::applicationName()+"

" + << "
    " + << "
  • " + QObject::tr("Version") + ": " + QString(VERSIONSTR) + "
  • " + << "
  • " + QObject::tr("Build Date") + ": " + QString::fromLatin1(__DATE__) + "
  • " + << "
  • " + QObject::tr("Build Time") + ": " + QString::fromLatin1(__TIME__) + "
  • " + << "
  • " + QObject::tr("Qt Runtime Version")+ ": " + QString(qVersion()) + "
  • " + << "
  • " + QObject::tr("Qt Compiled Version") + ": " + QString(QT_VERSION_STR) + "
  • " + << "
  • " + QObject::tr("System") + ": " + QSysInfo::prettyProductName() + "
  • " + << "
  • " + QObject::tr("Architecture") + ": " + QSysInfo::currentCpuArchitecture() + "
  • "; + debugInfo << "
"; + return debugInfo.join("\n"); + +} + +void utils::DisplayExceptionErrorDialog(const QString &error_info) +{ + QMessageBox message_box(QApplication::activeWindow()); + message_box.setAttribute(Qt::WA_DeleteOnClose,true); + message_box.setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); + message_box.setModal(true); + message_box.setIcon(QMessageBox::Critical); + message_box.setWindowTitle(QApplication::applicationName()+QObject::tr("Exception")); + //spaces are added to the end because otherwise the dialog is too small + message_box.setText(QApplication::applicationName()+QObject::tr(" has encountered a problem.")); + message_box.setInformativeText(QApplication::applicationName()+QObject::tr(" may need to Restart. Please report the error to developer.")); + message_box.setStandardButtons(QMessageBox::Close); + QStringList detailed_text; + detailed_text << "Error info: " + error_info + << "\nApp version: " + QString(VERSIONSTR) + << "\nQt Runtime Version: " + QString(qVersion()) + << "\nQt Compiled Version: " + QString(QT_VERSION_STR) + << "\nSystem: " + QSysInfo::prettyProductName() + << "\nArchitecture: " + QSysInfo::currentCpuArchitecture(); + message_box.setDetailedText(detailed_text.join("\n")); + message_box.exec(); +} + +// Returns the same number, but rounded to one decimal place +float utils::RoundToOneDecimal(float number) +{ + return QString::number(number, 'f', 1).toFloat(); +} + +// Returns a value for the environment variable name passed; +// if the env var isn't set, it returns an empty string +QString utils::GetEnvironmentVar(const QString &variable_name) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + // The only time this might fall down is on Linux when an + // environment variable holds bytedata. Don't use this + // utility function for retrieval if that's the case. + return qEnvironmentVariable(variable_name.toUtf8().constData(), "").trimmed(); +#else + // This will typically only be used on older Qts on Linux + return QProcessEnvironment::systemEnvironment().value(variable_name, "").trimmed(); +#endif +} diff --git a/src/utils.h b/src/utils.h index 9d2520f..fecaab7 100644 --- a/src/utils.h +++ b/src/utils.h @@ -24,56 +24,13 @@ public slots: static QString genRand(int length); static QString convertSectoDay(qint64 secs); static QString returnPath(QString pathname); - - static QString EncodeXML ( const QString& encodeMe ){ - - QString temp; - int length = encodeMe.size(); - for (int index = 0; index < length; index++) - { - QChar character(encodeMe.at(index)); - - switch (character.unicode()) - { - case '&': - temp += "&"; break; - - case '\'': - temp += "'"; break; - - case '"': - temp += """; break; - - case '<': - temp += "<"; break; - - case '>': - temp += ">"; break; - - default: - temp += character; - break; - } - } - - return temp; - } - - static QString DecodeXML ( const QString& decodeMe ) { - - QString temp(decodeMe); - - temp.replace("&", "&"); - temp.replace("'", "'"); - temp.replace(""", "\""); - temp.replace("<", "<"); - temp.replace(">", ">"); - - return temp; - } - + static QString EncodeXML ( const QString& encodeMe ); + static QString DecodeXML ( const QString& decodeMe ); static QString htmlToPlainText(QString str); - + static QString GetEnvironmentVar(const QString &variable_name); + static float RoundToOneDecimal(float number); + void DisplayExceptionErrorDialog(const QString &error_info); + static QString appDebugInfo(); private slots: //use refreshCacheSize static quint64 dir_size(const QString &directory); -- cgit v1.2.3