From 262eab0a67b2a54e6af8f3ffaadd80dd180736e5 Mon Sep 17 00:00:00 2001 From: Mahmud Nabil Date: Mon, 30 Oct 2023 01:34:19 +0600 Subject: [PATCH] feat: add option to import OBK *.json layout file --- src/frontend/LayoutConverter.cpp | 16 ++++++++++++++ src/frontend/LayoutConverter.h | 1 + src/frontend/TopBar.cpp | 36 +++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/frontend/LayoutConverter.cpp b/src/frontend/LayoutConverter.cpp index a2683327..f46f757f 100644 --- a/src/frontend/LayoutConverter.cpp +++ b/src/frontend/LayoutConverter.cpp @@ -403,6 +403,22 @@ ConversionResult LayoutConverter::convertAvroLayout(QString path) { return saveLayout(layout, savePath); } +ConversionResult LayoutConverter::saveLayout(QString path) { + QFile jsonFile(path); + if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + return OpenError; + } + QString version = QJsonDocument::fromJson(jsonFile.readAll()).object() + .value("info").toObject() + .value("version").toString(); + if (version == "1") return convertLayoutFormat(path); + if (version != "2") return UnsupportedLayout; + + QFileInfo fileInfo(jsonFile); + QString savePath = folders.getUserLayoutPath() + fileInfo.fileName(); + return jsonFile.copy(savePath) ? Ok : SaveError; +} + ConversionResult LayoutConverter::saveLayout(QJsonObject obj, QString path) { QFile saveFile(path); if (!saveFile.open(QIODevice::WriteOnly)) { diff --git a/src/frontend/LayoutConverter.h b/src/frontend/LayoutConverter.h index e15515b2..2d737453 100644 --- a/src/frontend/LayoutConverter.h +++ b/src/frontend/LayoutConverter.h @@ -39,4 +39,5 @@ class LayoutConverter { public: ConversionResult convertAvroLayout(QString path); ConversionResult convertLayoutFormat(QString path); + ConversionResult saveLayout(QString path); }; diff --git a/src/frontend/TopBar.cpp b/src/frontend/TopBar.cpp index 01c16247..c47ca176 100644 --- a/src/frontend/TopBar.cpp +++ b/src/frontend/TopBar.cpp @@ -41,8 +41,8 @@ static const QString DEFS_URL = "https://raw.githubusercontent.com/OpenBangla/OpenBangla-Keyboard/master/UPDATES.json"; TopBar::TopBar(bool darkIcon, QWidget *parent) : - QMainWindow(parent), - ui(new Ui::TopBar) { + QMainWindow(parent), + ui(new Ui::TopBar) { ui->setupUi(this); gLayout = new Layout(); @@ -86,7 +86,7 @@ TopBar::TopBar(bool darkIcon, QWidget *parent) : // Update the counter to show only the message for the first three times gSettings->setTrayInfoCount(count + 1); } - + #ifndef NO_UPDATE_CHECK updater = QSimpleUpdater::getInstance(); @@ -115,7 +115,7 @@ void TopBar::SetupTopBar() { if (gSettings->getTopBarWindowPosition() == QPoint(0, 0)) { int width = this->frameGeometry().width(); int height = this->frameGeometry().height(); - + QApplication *app = (QApplication *) QApplication::instance(); QScreen *screen = app->primaryScreen(); @@ -214,7 +214,7 @@ void TopBar::SetupTrayIcon() { gSettings->getTopBarVisibility() ? "Hide the TopBar" : "Show the TopBar", this ); - + connect(trayTopBarVisibility, &QAction::triggered, [&]() { if(this->isVisible()) { this->setVisible(false); @@ -236,7 +236,7 @@ void TopBar::SetupTrayIcon() { trayMenu->addSeparator(); trayMenu->addAction(trayTopBarVisibility); trayMenu->addAction(trayQuit); - + tray->setContextMenu(trayMenu); tray->setVisible(true); } @@ -293,9 +293,9 @@ void TopBar::layoutMenuLayouts_clicked() { void TopBar::layoutMenuInstall_clicked() { QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, "Select Keyboard Layout", QDir::homePath(), - "Avro Keyboard 5 Keyboard Layout (*.avrolayout)"); + "All Supported Layouts (*.avrolayout *.json);;Avro Keyboard 5 Keyboard Layout (*.avrolayout);;OpenBangla Keyboard Layout (*.json)"); LayoutConverter conv; - if (fileName.contains(".avrolayout") && fileName != "") { + if (fileName.endsWith(".avrolayout")) { ConversionResult res = conv.convertAvroLayout(fileName); switch (res) { case Ok: @@ -317,6 +317,26 @@ void TopBar::layoutMenuInstall_clicked() { QMessageBox::Ok); break; } + } else if (fileName.endsWith(".json")) { + ConversionResult res = conv.saveLayout(fileName); + switch (res) { + case Ok: + QMessageBox::information(Q_NULLPTR, "OpenBangla Keyboard", "Layout Installed Successfully", + QMessageBox::Ok); + break; + case UnsupportedLayout: + QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Unsupported Layout file!", + QMessageBox::Ok); + break; + case OpenError: + QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", + "An error occurred while opening the layout file!", QMessageBox::Ok); + break; + case SaveError: + QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Error occurred while saving the file!", + QMessageBox::Ok); + break; + } } RefreshLayouts(); }