Skip to content

Commit

Permalink
Copy commands folder before first start to move it away from a read
Browse files Browse the repository at this point in the history
only directory
  • Loading branch information
tim-gromeyer committed May 10, 2023
1 parent 875e7b7 commit e182eae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ static const QString &modelDir()
static const QString dir = dataDir() + QStringLiteral("/models/");
return dir;
}
static const QString &commandsBaseDir()
{
static const QString dir = dataDir() + QStringLiteral("/commands/");
return dir;
}
static const QString &speechToTextPluginDir()
{
static const QString dir = baseDir() + QStringLiteral("/speechtotext/");
return dir;
}
static const QString &commandsBaseDir()
static const QString &commandsInstallBaseDir()
{
static const QString dir = baseDir() + QStringLiteral("/commands/");
return dir;
Expand Down
21 changes: 20 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ MainWindow::MainWindow(QWidget *parent)
player->setAudioOutput(audioOutput);
#endif

// TODO: Detect first startup (after installation) and show a instruction or so
firstSetup();

// Set up recognizer
recognizer = new SpeechToText(STR("vosk"), this);
connect(recognizer, &SpeechToText::stateChanged, this, &MainWindow::onSTTStateChanged);
Expand Down Expand Up @@ -217,6 +220,14 @@ MainWindow::MainWindow(QWidget *parent)
ui->action_Quit->setShortcuts(QKeySequence::Quit);
}

void MainWindow::firstSetup()
{
if (QDir().exists(dir::commandsBaseDir()))
return;

directory::copyRecursively(dir::commandsInstallBaseDir(), dir::commandsBaseDir());
}

void MainWindow::addCommand(const Action &a)
{
instance()->commands.append(a);
Expand Down Expand Up @@ -651,7 +662,12 @@ void MainWindow::loadCommands()
{
commands.clear();

const QString dir = dir::commandsBaseDir() + recognizer->language();
QString dir = dir::commandsBaseDir() + recognizer->language();

QDir testDir;
testDir.setPath(dir);
if (testDir.isEmpty())
dir = dir::commandsInstallBaseDir() + recognizer->language();

QFile jsonFile(dir + STR("/default.json"));
// open the JSON file
Expand Down Expand Up @@ -730,6 +746,9 @@ void MainWindow::loadCommands()
void MainWindow::saveCommands()
{
const QString dir = dir::commandsBaseDir() + recognizer->language();
QDir makeDir;
makeDir.setPath(dir);
makeDir.mkpath(dir);

QJsonArray jsonArray;

Expand Down
1 change: 1 addition & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private Q_SLOTS:

private:
static void applyVolume();
void firstSetup();

Ui::MainWindow *ui;

Expand Down
37 changes: 37 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "utils.h"

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QHash>
#include <QTextStream>

Expand Down Expand Up @@ -100,6 +102,41 @@ int wordToNumber(QString text)
} // namespace numbers
} // namespace utils

namespace directory {
bool copyRecursively(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if (!targetDir.exists()) { /* if directory don't exists, build it */
if (!targetDir.mkdir(targetDir.absolutePath()))
return false;
}

QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach (QFileInfo fileInfo, fileInfoList) {
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
continue;

if (fileInfo.isDir()) { /* if it is directory, copy recursively*/
if (!copyRecursively(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
coverFileIfExist))
return false;
} else { /* if coverFileIfExist == true, remove old file first */
if (coverFileIfExist && targetDir.exists(fileInfo.fileName())) {
targetDir.remove(fileInfo.fileName());
}

// files copy
if (!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) {
return false;
}
}
}
return true;
}
} // namespace directory

namespace file {
QString makeSizeRedalbe(qint64 size)
{
Expand Down
4 changes: 4 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ constexpr QLatin1String L1(const char *str)
} // namespace strings
} // namespace utils

namespace directory {
bool copyRecursively(const QString &fromDir, const QString &toDir, bool coverFileIfExist = false);
} // namespace directory

namespace file {
QString makeSizeRedalbe(qint64);
}
Expand Down

0 comments on commit e182eae

Please sign in to comment.