-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
160 lines (137 loc) · 4.11 KB
/
Utils.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
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
#include "Utils.h"
#include "X11/Xlib.h"
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#include <QGridLayout>
#include <QSpacerItem>
#include <QStringList>
#include <iostream>
#include <QX11Info>
namespace Utils
{
bool isMovie(QString path)
{
static QStringList extensions;
if (extensions.isEmpty())
{
extensions << ".avi"
<< ".m4v"
<< ".mkv"
<< ".mov"
<< ".mp4"
<< ".ts"
<< ".webm"
<< ".mpg"
<< ".wmv";
}
foreach (auto extension, extensions)
{
if (path.endsWith(extension))
{
return true;
}
}
return false;
}
bool isMovieDiscDirectory(QString name)
{
static QStringList names;
if (names.isEmpty())
{
names << "BDMV"
<< "VIDEO_TS";
}
return names.contains(name);
}
bool isWatched(float progress, float duration)
{
return progress / duration > 0.9 || duration - progress < 60;
}
QString formatDuration(int duration)
{
return QString("%1:%2").arg(duration / 60, 2, 10, QChar('0')).arg(duration % 60, 2, 10, QChar('0'));
}
QStringList listSubdirectories(QDir directory)
{
QStringList list;
foreach (QFileInfo info, directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
{
list.append(info.filePath());
list.append(listSubdirectories(info.filePath()));
}
return list;
}
bool setStyleSheetFromFile(QWidget* widget, QString fileName)
{
QFile qss(fileName);
if (qss.open(QIODevice::ReadOnly | QIODevice::Text))
{
widget->setStyleSheet(qss.readAll());
qss.close();
return true;
}
return false;
}
void resizeMessageBox(QMessageBox* box, int width)
{
QSpacerItem* horizontalSpacer = new QSpacerItem(width, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)box->layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
}
void x11KeyEventForChildren(WId win, bool press, quint32 keysum, quint32 modifiers)
{
auto dpy = QX11Info::display();
Window root;
Window parent;
Window* children;
unsigned int nchildren;
XQueryTree(dpy, win, &root, &parent, &children, &nchildren);
for (unsigned int i = 0; i < nchildren; i++)
{
XKeyEvent event;
event.display = dpy;
event.window = children[i];
event.root = root;
event.subwindow = None;
event.time = CurrentTime;
event.x = 0;
event.y = 0;
event.x_root = 0;
event.y_root = 0;
event.same_screen = True;
event.type = press ? KeyPress : KeyRelease;
event.keycode = XKeysymToKeycode(dpy, keysum);
event.state = modifiers;
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent*)&event);
}
}
double determineDuration(const QString& path)
{
int ret;
AVFormatContext *formatCtx = NULL;
double duration = 0;
av_register_all();
auto pathUtf8 = path.toUtf8();
ret = avformat_open_input(&formatCtx, pathUtf8.constData(), NULL, NULL);
if (ret != 0)
{
goto cleanup;
}
// Retrieve stream information
ret = avformat_find_stream_info(formatCtx, NULL);
if (ret < 0)
{
goto cleanup;
}
duration = static_cast<double>(formatCtx->duration) / AV_TIME_BASE;
cleanup:
if (formatCtx != NULL)
{
avformat_close_input(&formatCtx);
}
return duration;
}
}