-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.cpp
128 lines (107 loc) · 4.46 KB
/
helper.cpp
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
/* This file was part of smplayer and now part of KokoVP
smplayer, GUI front-end for mplayer.
Copyright (C) 2006-2023 Ricardo Villalba <[email protected]>
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "helper.h"
#include <QApplication>
#include <QFileDialog>
#include <QStandardPaths>
#include "extensions.h"
#include "cache.h"
#include "config.h"
QString Helper::formatTime(int secs) {
bool negative = (secs < 0);
secs = qAbs(secs);
int t = secs;
int hours = (int) t / 3600;
t -= hours*3600;
int minutes = (int) t / 60;
t -= minutes*60;
int seconds = t;
return QString("%1%2:%3:%4").arg(negative ? "-" : "").arg(hours, 2, 10, QChar('0')).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
}
QString Helper::timeForJumps(int secs) {
int minutes = (int) secs / 60;
int seconds = secs % 60;
if (minutes==0) {
return QApplication::translate("Helper", "%n second(s)", "", seconds);
} else {
if (seconds==0)
return QApplication::translate("Helper", "%n minute(s)", "", minutes);
else {
QString m = QApplication::translate("Helper", "%n minute(s)", "", minutes);
QString s = QApplication::translate("Helper", "%n second(s)", "", seconds);
return QApplication::translate("Helper", "%1 and %2").arg(m, s);
}
}
}
const QList<QUrl> Helper::openMediaFiles(QWidget *parent)
{
QList<QUrl> s = QFileDialog::getOpenFileUrls(
parent, QApplication::translate("Helper", "Choose a file"), Cache::i().get("file_open/last_file_dir", QStandardPaths::standardLocations(QStandardPaths::MoviesLocation)).toString(),
QApplication::translate("Helper", "Multimedia") + Extensions.allPlayable().forFilter()+";;" +
QApplication::translate("Helper", "Video") + Extensions.video().forFilter()+";;" +
QApplication::translate("Helper", "Audio") + Extensions.audio().forFilter()+";;" +
QApplication::translate("Helper", "All files") +" (*.*)" );
if (s.count()>0 && QFile::exists(s.at(0).toLocalFile()))
{
Cache::i().set("file_open/last_dir", QFileInfo(s.at(0).toLocalFile()).absolutePath());
}
return s;
}
const QList<QUrl> Helper::openMediaDirectory(QWidget *parent)
{
QString dirPath = QFileDialog::getExistingDirectory(parent, QApplication::translate("Helper", "Choose a directory"),
Cache::i().get("file_open/last_file_dir", QStandardPaths::standardLocations(QStandardPaths::MoviesLocation)).toString());
QDir dir(dirPath);
if (!dir.exists())
return QList<QUrl>();
Cache::i().set("file_open/last_dir", dir.absolutePath());
QStringList paths;
searchWithMaxDepth(paths, Extensions.multimedia().forDirFilter(), dir, Config::i().get("file_open/open_dir_depth", 4).toInt(), true);
return pathsToUrls(paths);
}
void Helper::searchWithMaxDepth(QStringList &outList, const QStringList &filter, QDir dir, int maxDepth, bool searchForFiles, int depth)
{
if (depth>maxDepth)
return;
dir.setNameFilters(filter);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
if (fileList.size()>0)
{
if (searchForFiles)
{
for (auto &f : fileList)
outList.append(f.absoluteFilePath());
}
else
outList.append(dir.path());
}
dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
QFileInfoList dirList = dir.entryInfoList();
for (auto &e : dirList)
searchWithMaxDepth(outList, filter, QDir(e.filePath()), maxDepth, searchForFiles, depth+1);
}
const QList<QUrl> Helper::pathsToUrls(const QStringList &paths)
{
QList<QUrl> ret;
for (auto &arg : paths)
{
QFileInfo f(arg);
if (f.exists())
ret.append(QUrl(f.absoluteFilePath()));
}
return ret;
}