Skip to content

Commit

Permalink
add Settings > Backup and File > Other Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ddennedy committed Jan 10, 2024
1 parent f6277ef commit 2db5945
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 14 deletions.
89 changes: 78 additions & 11 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ static bool eventDebugCallback(void **data)
static const int AUTOSAVE_TIMEOUT_MS = 60000;
static const char *kReservedLayoutPrefix = "__%1";
static const char *kLayoutSwitcherName("layoutSwitcherGrid");
static QRegularExpression
kBackupFileRegex("^(.+) ([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9])-([0-5][0-9])-([0-5][0-9]).mlt$");

MainWindow::MainWindow()
: QMainWindow(0)
Expand Down Expand Up @@ -199,6 +201,7 @@ MainWindow::MainWindow()

setupAndConnectDocks();

setupMenuFile();
setupMenuView();

connectVideoWidgetSignals();
Expand Down Expand Up @@ -683,6 +686,36 @@ void MainWindow::setupAndConnectDocks()
resetDockCorners();
}

void MainWindow::setupMenuFile()
{
connect(ui->menuOtherVersions, &QMenu::aboutToShow, this, [&] {
for (auto a : ui->menuOtherVersions->actions())
delete a;
QDir dir(QFileInfo(m_currentFile).absolutePath());
auto name = Util::baseName(m_currentFile, true);
auto match = kBackupFileRegex.match(name);
QStringList filters;
if (match.hasMatch())
filters << match.captured(1) + "*.mlt";
else
filters << QFileInfo(m_currentFile).baseName().split(" - ").first() + "*.mlt";
for (auto &fileInfo : dir.entryInfoList(filters, QDir::Files, QDir::Time))
{
auto filename = fileInfo.fileName();
if (filename != name) {
auto text = filename;
if (!kBackupFileRegex.match(filename).hasMatch())
text += QString::fromLatin1("\t(%1)").arg(fileInfo.lastModified().toString(Qt::ISODate));
ui->menuOtherVersions->addAction(text)->setData(dir.filePath(filename));
}
}
ui->menuOtherVersions->show();
});
connect(ui->menuOtherVersions, &QMenu::triggered, this, [&](QAction * action) {
open(action->data().toString());
});
}

void MainWindow::setupMenuView()
{
ui->menuView->addSeparator();
Expand Down Expand Up @@ -776,6 +809,23 @@ void MainWindow::setupSettingsMenu()
group->addAction(ui->actionYadifSpatial);
group->addAction(ui->actionBwdif);

group = new QActionGroup(this);
ui->actionBackupManually->setData(0);
group->addAction(ui->actionBackupManually);
ui->actionBackupHourly->setData(60);
group->addAction(ui->actionBackupHourly);
ui->actionBackupDaily->setData(24 * 60);
group->addAction(ui->actionBackupDaily);
ui->actionBackupWeekly->setData(7 * 24 * 60);
group->addAction(ui->actionBackupWeekly);
for (auto a : group->actions()) {
if (Settings.backupPeriod() == a->data().toInt())
a->setChecked(true);
}
connect(group, &QActionGroup::triggered, this, [&](QAction * action) {
Settings.setBackupPeriod(action->data().toInt());
});

m_previewScaleGroup = new QActionGroup(this);
m_previewScaleGroup->addAction(ui->actionPreviewNone);
m_previewScaleGroup->addAction(ui->actionPreview360);
Expand Down Expand Up @@ -1496,6 +1546,10 @@ static void autosaveTask(MainWindow *p)
void MainWindow::onAutosaveTimeout()
{
if (isWindowModified()) {
// Automatic backup
backupPeriodically();

// Auto-save to recovery file
auto result = QtConcurrent::run(autosaveTask, this);
}
static QMessageBox *dialog = nullptr;
Expand Down Expand Up @@ -2565,6 +2619,7 @@ bool MainWindow::on_actionSave_triggered()
} else {
if (Util::warnIfNotWritable(m_currentFile, this, tr("Save XML")))
return false;
backupPeriodically();
bool success = saveXML(m_currentFile);
QMutexLocker locker(&m_autosaveMutex);
m_autosaveFile.reset(new AutoSaveFile(m_currentFile));
Expand Down Expand Up @@ -2607,19 +2662,9 @@ void MainWindow::on_actionBackupSave_triggered()
if (m_currentFile.isEmpty()) {
on_actionSave_As_triggered();
} else {
QFileInfo info(m_currentFile);
auto dateTime = info.lastModified().toString(Qt::ISODate);
dateTime.replace(':', '-');
auto filename = QString("%1/%2 %3.mlt").arg(info.canonicalPath(), info.completeBaseName(),
dateTime);
if (Util::warnIfNotWritable(filename, this, tr("Save XML")))
return;
auto result = QFile::copy(m_currentFile, filename);
LOG_INFO() << filename << result;
backup();
if (isWindowModified())
on_actionSave_triggered();
else if (result)
showStatusMessage(tr("Saved backup %1").arg(filename));
}
}

Expand Down Expand Up @@ -3775,6 +3820,28 @@ void MainWindow::restartAfterChangeTheme()
}
}

void MainWindow::backup()
{
QFileInfo info(m_currentFile);
auto dateTime = info.lastModified().toString(Qt::ISODate);
dateTime.replace(':', '-');
auto filename = QString("%1/%2 %3.mlt").arg(info.canonicalPath(), info.completeBaseName(),
dateTime);
if (!QFile::exists(filename) && !Util::warnIfNotWritable(filename, this, tr("Save XML"))
&& QFile::copy(m_currentFile, filename)) {
showStatusMessage(tr("Saved backup %1").arg(filename));
}
}

void MainWindow::backupPeriodically()
{
auto dateTime = QFileInfo(m_currentFile).lastModified();
if (Settings.backupPeriod() > 0 && !kBackupFileRegex.match(m_currentFile).hasMatch()
&& dateTime.secsTo(QDateTime::currentDateTime()) / 60 > Settings.backupPeriod()) {
backup();
}
}

void MainWindow::on_actionSystemTheme_triggered()
{
Settings.setTheme("system");
Expand Down
5 changes: 4 additions & 1 deletion src/mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2023 Meltytech, LLC
* Copyright (c) 2011-2024 Meltytech, LLC
*
* 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
Expand Down Expand Up @@ -158,6 +158,7 @@ class MainWindow : public QMainWindow
void setupLayoutSwitcher();
void centerLayoutInRemainingToolbarSpace();
void setupAndConnectDocks();
void setupMenuFile();
void setupMenuView();
void connectVideoWidgetSignals();
void setupSettingsMenu();
Expand All @@ -183,6 +184,8 @@ class MainWindow : public QMainWindow
void resetDockCorners();
void showIncompatibleProjectMessage(const QString &shotcutVersion);
void restartAfterChangeTheme();
void backup();
void backupPeriodically();

Ui::MainWindow *ui;
Player *m_player;
Expand Down
52 changes: 52 additions & 0 deletions src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<addaction name="actionExportChapters"/>
<addaction name="actionExportEDL"/>
</widget>
<widget class="QMenu" name="menuOtherVersions">
<property name="title">
<string>Other Versions</string>
</property>
<addaction name="separator"/>
</widget>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionOpenOther"/>
Expand All @@ -63,6 +69,7 @@
<addaction name="actionSave"/>
<addaction name="actionSave_As"/>
<addaction name="actionBackupSave"/>
<addaction name="menuOtherVersions"/>
<addaction name="menuExport"/>
<addaction name="actionClose"/>
<addaction name="separator"/>
Expand Down Expand Up @@ -250,10 +257,20 @@
<addaction name="menuExternal"/>
<addaction name="actionSync"/>
</widget>
<widget class="QMenu" name="menuBackup">
<property name="title">
<string>Backup</string>
</property>
<addaction name="actionBackupManually"/>
<addaction name="actionBackupHourly"/>
<addaction name="actionBackupDaily"/>
<addaction name="actionBackupWeekly"/>
</widget>
<addaction name="actionProject"/>
<addaction name="actionGPU"/>
<addaction name="menuProfile"/>
<addaction name="menuAudioChannels"/>
<addaction name="menuBackup"/>
<addaction name="actionUser_Interface"/>
<addaction name="menuPreviewScaling"/>
<addaction name="menuProxy"/>
Expand Down Expand Up @@ -1290,6 +1307,41 @@
<string>Ctrl+Alt+S</string>
</property>
</action>
<action name="actionBackupManually">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Manually</string>
</property>
</action>
<action name="actionBackupHourly">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Hourly</string>
</property>
</action>
<action name="actionBackupDaily">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Daily</string>
</property>
</action>
<action name="actionBackupWeekly">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Weekly</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
Expand Down
12 changes: 11 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023 Meltytech, LLC
* Copyright (c) 2013-2024 Meltytech, LLC
*
* 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
Expand Down Expand Up @@ -1182,3 +1182,13 @@ bool ShotcutSettings::warnLowMemory() const
{
return settings.value("warnLowMemory", true).toBool();
}

int ShotcutSettings::backupPeriod() const
{
return settings.value("backupPeriod", 24 * 60).toInt();
}

void ShotcutSettings::setBackupPeriod(int seconds)
{
settings.setValue("backupPeriod", seconds);
}
4 changes: 3 additions & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023 Meltytech, LLC
* Copyright (c) 2013-2024 Meltytech, LLC
*
* 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
Expand Down Expand Up @@ -279,6 +279,8 @@ class ShotcutSettings : public QObject
void setExportRangeMarkers(bool);
int undoLimit() const;
bool warnLowMemory() const;
int backupPeriod() const;
void setBackupPeriod(int i);

// proxy
bool proxyEnabled() const;
Expand Down

0 comments on commit 2db5945

Please sign in to comment.