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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QString>
#include <QByteArray>
#include <QFileInfo>
#include <QFile>
#include <QSettings>
#include <QCryptographicHash>
#include <QHash>
#include <QSettings>
#include <QDir>
#include <QPixmap>
#include <QTemporaryFile>
#include <QProcess>
#include <QRect>
#include <QWidget>
#include <QDesktopWidget>
#include <QApplication>
#include <stdio.h>
#include "helper.h"
namespace Helper {
const QString mimeType(const QString &path){
QString retval;
magic_t mc = magic_open(MAGIC_MIME_TYPE);
QByteArray name = path.toUtf8();
if(mc){
magic_load(mc, 0);
const char* magic_c = magic_file(mc, name.constData());
retval = QString(magic_c);
magic_close(mc);
}
if(retval.toLower().startsWith("application/octet-stream")){
magic_t mc = magic_open(MAGIC_NONE);
if(mc){
magic_load(mc, 0);
const char* magic_c = magic_file(mc, name.constData());
QString desc(magic_c);
magic_close(mc);
if(desc.toLower().contains("mpeg sequence") || desc.toLower().contains("microsoft asf") || desc.toLower().contains("matroska data")){
retval = "video/";
}
}
}
return retval;
}
const QString md5Sum(const QString &path){
QFileInfo info(path);
if(!info.exists() || !info.isFile()){
return QString();
}
QString retval;
QCryptographicHash h(QCryptographicHash::Md5);
QFile file(path);
file.open(QIODevice::ReadOnly);
qint64 read = 0;
if(info.size() < (5 * 1024 * 1024)){
QByteArray data(4096, '\0');
do {
read = file.read(data.data(), 4096);
h.addData(data.data(), read);
} while (read == 4096);
QByteArray res = h.result();
retval = res.toHex().toLower();
}else{
QByteArray data(512, '\0');
int offset = info.size() / 3;
file.seek(offset);
int numBytes = 512 * 1024;
int readBytes = 0;
do {
read = file.read(data.data(), 512);
readBytes += read;
h.addData(data.data(), read);
} while(readBytes < numBytes);
QByteArray res = h.result();
retval = res.toHex().toLower();
}
return retval;
}
const QString moveToArchive(const QString &path, const QString &md5, bool copy){
QFileInfo info(path);
if(!info.exists()){
return QString();
}
QFileInfo destFile = QFileInfo(createArchivePath(path, md5));
if(destFile.exists()){
destFile = QFileInfo(createArchivePath(path, md5, true));
if(destFile.exists()){
return QString();
}
}
QFileInfo destDir = QFileInfo(destFile.absolutePath());
if(!destDir.exists()){
QDir::root().mkpath(destFile.absolutePath());
}
bool success = false;
if(copy){
success = QFile::copy(path, destFile.absoluteFilePath());
}else{
success = QFile::rename(path, destFile.absoluteFilePath());
}
if(success){
return destFile.absoluteFilePath();
}
return QString();
}
bool removeFromArchive(const QString &fileName, const QString &md5){
QString file = createArchivePath(fileName, md5);
QFileInfo info(file);
if(!info.exists()){
return false;
}
return QFile::remove(file);
}
const QString createArchivePath(const QString &path, const QString &md5, bool withMd5){
QSettings s;
QString archiveDir = s.value("paths/archivedir").toString();
QFileInfo info(path);
QString retval;
if(withMd5){
retval = QString("%1/%2/%3/%4_%5.%6").arg(archiveDir).arg(md5[0]).arg(md5[1]).arg(info.completeBaseName()).arg(md5).arg(info.suffix());
}else{
retval = QString("%1/%2/%3/%4").arg(archiveDir).arg(md5[0]).arg(md5[1]).arg(info.fileName());
}
return retval;
}
QPair<QString, QStringList> programData(const QString &prefix, const QString &preferred){
QSettings s;
QString section = QString("programs_%1").arg(prefix);
QHash<QString, QVariant> data = s.value(QString("%1/data").arg(section)).toHash();
if(data.isEmpty()){
return QPair<QString, QStringList>();
}
QHash<QString, QVariant> programData;
if(!preferred.isEmpty()){
if(data.keys().contains(preferred)){
programData = data.value(preferred).toHash();
return qMakePair(programData.value("path").toString(), programData.value("args").toStringList());
}
return QPair<QString, QStringList>();
}
QString defaultProg = s.value(QString("%1/default").arg(section)).toString();
if(defaultProg.isEmpty()){
return QPair<QString, QStringList>();
}
programData = data.value(defaultProg).toHash();
return qMakePair(programData.value("path").toString(), programData.value("args").toStringList());
}
const QString durationFromSecs(qint64 secs){
int minutes = secs / 60;
int hours = 0;
int seconds = 0;
if(minutes > 60){
hours = minutes / 60;
minutes -= hours * 60;
}
seconds = secs - hours * 60 * 60 - minutes * 60;
seconds = (seconds > 60) ? 59 : seconds;
QByteArray retval(10, '\0');
::snprintf(retval.data(), 9, "%.2d:%.2d:%.2d", hours, minutes, seconds);
return retval;
}
QVariant bytesFromUnit(QVariant number, const QString unit){
QString u = unit.toLower();
quint64 retval = number.toULongLong();
if(u == "k"){
retval = retval * 1024;
}else if(u == "m"){
retval = retval * 1024 * 1024;
}else if(u == "g"){
retval = retval * 1024 * 1024 * 1024;
}
return retval;
}
const QStringList toStringList(const QList<QVariant> &list){
QStringList retval;
foreach(QVariant v, list){
retval << v.toString();
}
return retval;
}
const QString colorToHtml(const QColor &color){
char colString[7];
::snprintf(colString, 7, "%0x%0x%0x", color.red(), color.green(), color.blue());
return QString::fromAscii(colString);
}
bool SortFileInfoList::operator ()(const QFileInfo &lhs, const QFileInfo &rhs) const {
return lhs.fileName().toLower() < rhs.fileName().toLower();
}
bool FileInfoListContains::operator ()(const QFileInfo &info, const QString &file) const {
return info.fileName() == file;
}
void centerWidget(QWidget *widget){
QRect widgetRect = widget->rect();
widgetRect.moveCenter(qApp->desktop()->screenGeometry().center());
widget->move(widgetRect.topLeft());
}
}
|