diff --git a/src/frontend/LayoutConverter.cpp b/src/frontend/LayoutConverter.cpp index a268332..f46f757 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 e15515b..2d73745 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 01c1624..c47ca17 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(); }