diff options
author | 2021-04-05 02:11:25 +0530 | |
---|---|---|
committer | 2021-04-05 02:11:25 +0530 | |
commit | 9ea334d08f42f3c362e86499dc3c0ed658bb428c (patch) | |
tree | 85eb15c519bcff8a5e3c4f8406f068b7c412b789 /src/utils.cpp | |
parent | e79b447b31ad9ab1ed42fd232f8789fad38d780b (diff) | |
download | whatsie-9ea334d08f42f3c362e86499dc3c0ed658bb428c.tar.gz whatsie-9ea334d08f42f3c362e86499dc3c0ed658bb428c.zip |
src init
Diffstat (limited to 'src/utils.cpp')
-rw-r--r-- | src/utils.cpp | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..543b837 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,162 @@ +#include "utils.h" +#include <QDateTime> +#include <QRegularExpression> + +utils::utils(QObject *parent) : QObject(parent) +{ + setParent(parent); +} + +utils::~utils() +{ + this->deleteLater(); +} + +//calculate dir size +quint64 utils::dir_size(const QString & directory) +{ + quint64 sizex = 0; + QFileInfo str_info(directory); + if (str_info.isDir()) + { + QDir dir(directory); + QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot); + for (int i = 0; i < list.size(); ++i) + { + QFileInfo fileInfo = list.at(i); + if(fileInfo.isDir()) + { + sizex += dir_size(fileInfo.absoluteFilePath()); + } + else{ + sizex += fileInfo.size(); + } + } + } + return sizex; +} + +//get the size of cache folder in human readble format +QString utils::refreshCacheSize(const QString cache_dir) +{ + qint64 cache_size = dir_size(cache_dir); + QString cache_unit; + if(cache_size > 1024*1024*1024) + { + cache_size = cache_size/(1024*1024*1024); + cache_unit = " GB"; + } + if(cache_size > 1024*1024) + { + cache_size = cache_size/(1024*1024); + cache_unit = " MB"; + } + else if(cache_size > 1024) + { + cache_size = cache_size/(1024); + cache_unit = " kB"; + } + else + { + cache_unit = " B"; + } + return QString::number(cache_size) + cache_unit; +} + +bool utils::delete_cache(const QString cache_dir) +{ + bool deleted = QDir(cache_dir).removeRecursively(); + QDir(cache_dir).mkpath(cache_dir); + return deleted; +} + +//returns string with first letter capitalized +QString utils::toCamelCase(const QString& s) +{ + QStringList parts = s.split(' ', QString::SkipEmptyParts); + for (int i = 0; i < parts.size(); ++i) + parts[i].replace(0, 1, parts[i][0].toUpper()); + return parts.join(" "); +} + +QString utils::generateRandomId(int length){ + + QString str = QUuid::createUuid().toString(); + str.remove(QRegularExpression("{|}|-")); + if(str.length()<length){ + while(str.length() != length){ + int required_char = length - str.length(); + str = str + str.append(genRand(required_char)); + } + } + if(str.length()>length){ + while(str.length() != length){ + str = str.remove(str.length()-1,1); + } + } + return str; +} + +QString utils::genRand(int length) +{ + QDateTime cd = QDateTime::currentDateTime(); + const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+QString::number(cd.currentMSecsSinceEpoch()).remove(QRegExp("[^a-zA-Z\\d\\s]"))); + + const int randomStringLength = length; + QString randomString; + qsrand(cd.toTime_t()); + for(int i=0; i<randomStringLength; ++i) + { + int index = qrand() % possibleCharacters.length(); + QChar nextChar = possibleCharacters.at(index); + randomString.append(nextChar); + } + randomString = randomString.trimmed().simplified().remove(" "); + return randomString; +} + +QString utils::convertSectoDay(qint64 secs) +{ + int day = secs / (24 * 3600); + + secs = secs % (24 * 3600); + int hour = secs / 3600; + + secs %= 3600; + int minutes = secs / 60 ; + + secs %= 60; + int seconds = secs; + + QString days = QString::number(day) + " " + "days " + QString::number(hour) + + " " + "hours " + QString::number(minutes) + " " + + "minutes " + QString::number(seconds) + " " + + "seconds "; + return days; +} + +//static on demand path maker +QString utils::returnPath(QString pathname) +{ + QString _data_path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); + if(!QDir(_data_path+"/"+pathname).exists()){ + QDir d(_data_path+"/"+pathname); + d.mkpath(_data_path+"/"+pathname); + } + return _data_path+"/"+pathname+"/"; +} + + +QString utils::htmlToPlainText(QString str){ + QString out; + QTextDocument text; + text.setHtml(str); + out = text.toPlainText(); + text.deleteLater(); + return out .replace("\\\"","'") + .replace("&","&") + .replace(">",">") + .replace("<","<") + .replace("'","'"); +} + |