aboutsummaryrefslogtreecommitdiff
path: root/src/utils.cpp
blob: 543b837dd6c45077e4f06ec52dec2ecdb9b0a956 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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("&amp;","&")
            .replace("&gt;",">")
            .replace("&lt;","<")
            .replace("&#39;","'");
}