forked from NMAAHC/mkvnote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
528 lines (453 loc) · 16.7 KB
/
Copy pathmainwindow.cpp
File metadata and controls
528 lines (453 loc) · 16.7 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "mainwindow.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QScrollArea>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QProcess>
#include <QTemporaryFile>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDateTime>
#include <QMessageBox>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDir>
#include <QDebug>
#include <QTime>
#include <QMenuBar>
#include <QAction>
#include <QFileDialog>
#include <QMimeData>
#include <QUrl>
#include <QSizePolicy>
MainWindow::MainWindow(const QString &inputFile, QWidget *parent)
: QMainWindow(parent)
{
setAcceptDrops(true);
setMinimumSize(700, 500);
resize(800, 700);
// menu bar
QMenu *fileMenu = menuBar()->addMenu("File");
QAction *openAct = fileMenu->addAction("Open...");
openAct->setShortcut(QKeySequence::Open);
connect(openAct, &QAction::triggered, this, &MainWindow::onOpenFile);
//central widget
QWidget *central = new QWidget(this);
setCentralWidget(central);
central->setStyleSheet("background-color: #2b2b2b; color: white;");
QVBoxLayout *outerLayout = new QVBoxLayout(central);
outerLayout->setContentsMargins(12, 12, 12, 12);
outerLayout->setSpacing(8);
//title
QLabel *title = new QLabel("NMAAHC mkvnote — Matroska Metadata Editor");
title->setStyleSheet("font-size: 16px; font-weight: bold; color: #C97FD4;");
outerLayout->addWidget(title);
//description
QLabel *desc = new QLabel(
"Edit metadata tags. These tags semantically describe the file as a whole "
"and are not intended to refer to a particular track or attachment. "
"Empty tags will be ignored. Existing tags will be overwritten when saved.");
desc->setWordWrap(true);
desc->setStyleSheet("color: white;");
outerLayout->addWidget(desc);
//drop hint
m_dropHint = new QLabel("Drop a Matroska here\nor use File > Open…");
m_dropHint->setAlignment(Qt::AlignCenter);
m_dropHint->setStyleSheet("color: #aaa; font-size: 18px; padding: 60px;");
outerLayout->addWidget(m_dropHint, 1);
//scroll area
m_scrollArea = new QScrollArea;
m_scrollArea->setWidgetResizable(true);
m_scrollArea->setStyleSheet("QScrollArea { border: none; background: transparent; }");
m_scrollArea->hide();
outerLayout->addWidget(m_scrollArea, 1);
m_formContainer = new QWidget;
m_formContainer->setStyleSheet("background-color: #2b2b2b;");
m_formContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_scrollArea->setWidget(m_formContainer);
m_formLayout = new QFormLayout(m_formContainer);
m_formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
m_formLayout->setLabelAlignment(Qt::AlignLeft | Qt::AlignTop);
m_formLayout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);
m_formLayout->setHorizontalSpacing(16);
m_formLayout->setVerticalSpacing(6);
//button row
QHBoxLayout *btnRow = new QHBoxLayout;
QPushButton *btnTagOn = new QPushButton("Tag-On!");
QPushButton *btnRevert = new QPushButton("Revert");
QPushButton *btnCancel = new QPushButton("Cancel");
btnTagOn->setDefault(true);
btnTagOn->setStyleSheet(
"QPushButton { background-color: #7B2891; color: white; "
"padding: 6px 20px; border-radius: 4px; font-weight: bold; }"
"QPushButton:hover { background-color: #9A35B5; }");
btnRevert->setStyleSheet(
"QPushButton { background-color: #888; color: white; "
"padding: 6px 20px; border-radius: 4px; }"
"QPushButton:hover { background-color: #aaa; }");
btnCancel->setStyleSheet(
"QPushButton { background-color: #555; color: white; "
"padding: 6px 20px; border-radius: 4px; }"
"QPushButton:hover { background-color: #777; }");
btnRow->addStretch();
btnRow->addWidget(btnTagOn);
btnRow->addWidget(btnRevert);
btnRow->addWidget(btnCancel);
outerLayout->addLayout(btnRow);
connect(btnTagOn, &QPushButton::clicked, this, &MainWindow::onTagOn);
connect(btnRevert, &QPushButton::clicked, this, &MainWindow::onRevert);
connect(btnCancel, &QPushButton::clicked, this, &MainWindow::onCancel);
setWindowTitle("NMAAHC mkvnote");
if (!inputFile.isEmpty())
loadFile(inputFile);
}
//loadFile
void MainWindow::loadFile(const QString &path)
{
m_inputFile = path;
m_tagOrder.clear();
m_fields.clear();
clearForm();
m_dropHint->hide();
m_scrollArea->show();
setWindowTitle(QString("mkvnote — %1").arg(QFileInfo(path).fileName()));
initLog();
buildForm(m_formContainer, m_formLayout);
populateForm(extractExistingTags());
}
//clearForm
void MainWindow::clearForm()
{
while (m_formLayout->rowCount() > 0)
m_formLayout->removeRow(0);
}
//onOpenFile
void MainWindow::onOpenFile()
{
QString path = QFileDialog::getOpenFileName(
this, "Open MKV File", QDir::homePath(),
"Matroska Files (*.mkv *.mka *.mks);;All Files (*)");
if (!path.isEmpty())
loadFile(path);
}
//drag-n-drop
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
const QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty()) return;
QString path = urls.first().toLocalFile();
if (path.endsWith(".mkv", Qt::CaseInsensitive) ||
path.endsWith(".mka", Qt::CaseInsensitive) ||
path.endsWith(".mks", Qt::CaseInsensitive))
loadFile(path);
}
//buildForm
void MainWindow::buildForm(QWidget * /*container*/, QFormLayout *layout)
{
QMap<QString, QString> existing = extractExistingTags();
auto addRow = [&](const QString &tagName, bool readOnly) {
m_tagOrder.append(tagName);
bool isNmaahc = NMAAHC_TAG_SET.contains(tagName) || RO_TAGS.contains(tagName)
|| tagName == "ATTACHMENTS";
QString labelText = isNmaahc ? tagName : tagName + "*";
if (readOnly) labelText += " [RO]";
QLabel *lbl = new QLabel(labelText);
lbl->setMinimumWidth(200);
lbl->setStyleSheet("color: white;");
if (readOnly) {
QLineEdit *le = new QLineEdit;
le->setReadOnly(true);
le->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
le->setStyleSheet("background-color: #3a3a3a; color: #aaa; border: 1px solid #555;");
layout->addRow(lbl, le);
m_fields[tagName] = le;
} else if (MULTILINE_TAGS.contains(tagName)) {
QTextEdit *te = new QTextEdit;
int h = FIELD_HEIGHTS.value(tagName, 90);
te->setFixedHeight(h);
te->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
te->setStyleSheet("background-color: #3a3a3a; color: white; border: 1px solid #555;");
layout->addRow(lbl, te);
m_fields[tagName] = te;
} else {
QLineEdit *le = new QLineEdit;
le->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
le->setStyleSheet("background-color: #3a3a3a; color: white; border: 1px solid #555;");
layout->addRow(lbl, le);
m_fields[tagName] = le;
}
};
//Section: Technical / Read-Only
layout->addRow(makeSectionHeader("Technical and Hashes"), new QWidget);
for (const QString &tag : RO_TAGS)
addRow(tag, true);
addRow("ATTACHMENTS", true);
//Section: NMAAHC tags already in the file
QStringList embeddedNmaahc;
for (const QString &tag : NMAAHC_TAG_SET)
if (existing.contains(tag)) embeddedNmaahc.append(tag);
if (!embeddedNmaahc.isEmpty()) {
layout->addRow(makeSectionHeader("NMAAHC Tags"), new QWidget);
for (const QString &tag : embeddedNmaahc)
addRow(tag, false);
}
//Section: NMAAHC tags not yet in the file
QStringList emptyNmaahc;
for (const QString &tag : NMAAHC_TAG_SET)
if (!existing.contains(tag)) emptyNmaahc.append(tag);
if (!emptyNmaahc.isEmpty()) {
layout->addRow(makeSectionHeader("Empty NMAAHC Tags"), new QWidget);
for (const QString &tag : emptyNmaahc)
addRow(tag, false);
}
//Section: Extra tags in the file not in the NMAAHC set
QStringList extraTags;
for (const QString &tag : existing.keys())
if (!NMAAHC_TAG_SET.contains(tag) && !RO_TAGS.contains(tag))
extraTags.append(tag);
extraTags.sort();
if (!extraTags.isEmpty()) {
layout->addRow(makeSectionHeader("Extra Existing Tags"), new QWidget);
for (const QString &tag : extraTags)
addRow(tag, false);
}
}
//extractExistingTags
QMap<QString, QString> MainWindow::extractExistingTags()
{
QMap<QString, QString> tags;
if (m_inputFile.isEmpty()) return tags;
QProcess proc;
proc.start("mkvextract", {"tags", m_inputFile});
proc.waitForFinished(30000);
QByteArray xmlData = proc.readAllStandardOutput();
if (xmlData.isEmpty()) return tags;
QXmlStreamReader xml(xmlData);
while (!xml.atEnd()) {
xml.readNext();
if (!xml.isStartElement() || xml.name() != QLatin1String("Tag"))
continue;
QList<QPair<QString,QString>> simples;
bool targetsFound = false;
bool targetsHasChildren = false;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement() && xml.name() == QLatin1String("Tag"))
break;
if (!xml.isStartElement()) continue;
if (xml.name() == QLatin1String("Targets")) {
targetsFound = true;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement() && xml.name() == QLatin1String("Targets"))
break;
if (xml.isStartElement())
targetsHasChildren = true;
}
} else if (xml.name() == QLatin1String("Simple")) {
QString name, value;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement() && xml.name() == QLatin1String("Simple"))
break;
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("Name"))
name = xml.readElementText();
else if (xml.name() == QLatin1String("String"))
value = xml.readElementText();
}
}
if (!name.isEmpty())
simples.append({name, value});
}
}
bool isGlobal = !targetsFound || !targetsHasChildren;
if (isGlobal) {
for (auto &p : simples)
tags[p.first] = p.second;
}
}
return tags;
}
//getAttachments
QString MainWindow::getAttachments()
{
QProcess proc;
proc.start("mediainfo", {"--Output=General;%Attachments%", m_inputFile});
proc.waitForFinished(10000);
QString result = proc.readAllStandardOutput().trimmed();
if (result.isEmpty() || result == "N/A") return "none";
return result.replace(" / ", " ; ");
}
//onTagOn
void MainWindow::onTagOn()
{
if (m_inputFile.isEmpty()) {
QMessageBox::warning(this, "mkvnote", "No file loaded. Please open an MKV file first.");
return;
}
log("INFO", "Processing tags for XML generation");
QMap<QString, QString> tagValues = extractExistingTags();
for (const QString &tag : m_tagOrder) {
if (tag == "ATTACHMENTS") continue;
if (RO_TAGS.contains(tag)) continue;
QWidget *w = m_fields.value(tag);
if (!w) continue;
QString value;
if (auto *te = qobject_cast<QTextEdit*>(w))
value = te->toPlainText();
else if (auto *le = qobject_cast<QLineEdit*>(w))
value = le->text();
if (!value.trimmed().isEmpty())
tagValues[tag] = value;
else
tagValues.remove(tag);
}
if (!writeTagsToMkv(tagValues)) return;
QMessageBox::information(this, "mkvnote",
QString("Tags written successfully to:\n%1").arg(m_inputFile));
close();
}
//onCancel
void MainWindow::onCancel()
{
log("INFO", "User cancelled operation");
close();
}
//populateForm
void MainWindow::populateForm(const QMap<QString, QString> &tags)
{
for (const QString &tag : m_tagOrder) {
QWidget *w = m_fields.value(tag);
if (!w) continue;
if (tag == "ATTACHMENTS") {
if (auto *le = qobject_cast<QLineEdit*>(w))
le->setText(getAttachments());
} else if (RO_TAGS.contains(tag)) {
if (auto *le = qobject_cast<QLineEdit*>(w))
le->setText(tags.value(tag));
} else if (auto *te = qobject_cast<QTextEdit*>(w)) {
te->setPlainText(tags.value(tag));
} else if (auto *le = qobject_cast<QLineEdit*>(w)) {
le->setText(tags.value(tag));
}
}
}
//onRevert
void MainWindow::onRevert()
{
if (m_inputFile.isEmpty()) return;
auto reply = QMessageBox::question(this, "Revert",
"Discard all changes and reload tags from file?",
QMessageBox::Yes | QMessageBox::No);
if (reply != QMessageBox::Yes) return;
log("INFO", "User reverted to saved tags");
populateForm(extractExistingTags());
}
//writeTagsToMkv
bool MainWindow::writeTagsToMkv(const QMap<QString, QString> &tags)
{
QTemporaryFile tmpXml;
tmpXml.setFileTemplate("/tmp/mkvnote_XXXXXX");
tmpXml.setAutoRemove(false);
if (!tmpXml.open()) {
QMessageBox::critical(this, "mkvnote",
QString("Could not create temp XML file: %1").arg(tmpXml.errorString()));
return false;
}
QString xmlContent;
QXmlStreamWriter xml(&xmlContent);
xml.setAutoFormatting(true);
xml.writeStartElement("Tags");
xml.writeStartElement("Tag");
xml.writeEmptyElement("Targets");
for (auto it = tags.constBegin(); it != tags.constEnd(); ++it) {
xml.writeStartElement("Simple");
xml.writeTextElement("Name", it.key());
xml.writeTextElement("String", it.value());
xml.writeEndElement();
}
xml.writeEndElement(); // Tag
xml.writeEndElement(); // Tags
QTextStream out(&tmpXml);
out << xmlContent;
tmpXml.close();
QString xmlPath = tmpXml.fileName();
logXml(xmlContent);
log("INFO", QString("Writing tags to MKV: %1").arg(m_inputFile));
log("INFO", QString("XML temp file: %1").arg(xmlPath));
QProcess proc;
proc.start("mkvpropedit", {"--tags", QString("global:%1").arg(xmlPath), m_inputFile});
proc.waitForFinished(60000);
int exitCode = proc.exitCode();
QString output = proc.readAllStandardOutput() + proc.readAllStandardError();
log("DEBUG", QString("mkvpropedit exit: %1").arg(exitCode));
log("DEBUG", QString("mkvpropedit output: %1").arg(output));
QFile::remove(xmlPath);
if (exitCode != 0) {
log("ERROR", QString("mkvpropedit failed: %1").arg(output));
QMessageBox::critical(this, "mkvnote",
QString("Error writing tags:\n%1").arg(output));
return false;
}
log("INFO", "Tags written successfully");
return true;
}
//initLog
void MainWindow::initLog()
{
QFileInfo fi(m_inputFile);
m_logFile = fi.dir().filePath(fi.completeBaseName() + "_mkvnote_tags.log");
QFile f(m_logFile);
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream s(&f);
s << "# mkvnote log - " << QDateTime::currentDateTimeUtc().toString(Qt::ISODate) << "\n";
s << "# Version: 1.0.0\n";
s << "# Input: " << m_inputFile << "\n";
s << "# ========================================\n";
}
}
//log
void MainWindow::log(const QString &level, const QString &message)
{
if (m_logFile.isEmpty()) return;
QFile f(m_logFile);
if (f.open(QIODevice::Append | QIODevice::Text)) {
QTextStream s(&f);
s << "[" << QTime::currentTime().toString("HH:mm:ss") << "] "
<< "[" << level << "] " << message << "\n";
}
}
//logXml
void MainWindow::logXml(const QString &xmlContent)
{
if (m_logFile.isEmpty()) return;
QFile f(m_logFile);
if (f.open(QIODevice::Append | QIODevice::Text)) {
QTextStream s(&f);
s << "\n# ======== XML CONTENT ========\n";
s << xmlContent;
s << "\n# ======== END XML ========\n";
}
}
//makeSectionHeader
QLabel *MainWindow::makeSectionHeader(const QString &text)
{
QLabel *lbl = new QLabel(QString("<b><span style='color:#C97FD4'>%1</span></b>").arg(text));
lbl->setTextFormat(Qt::RichText);
lbl->setObjectName("sectionHeader");
lbl->setContentsMargins(0, 12, 0, 4);
return lbl;
}