Skip to content

Commit 7586ea9

Browse files
authored
Merge pull request #355 from NabilSnigdho/develop
feat: add option to import OBK *.json layout file
2 parents 780bd40 + 262eab0 commit 7586ea9

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/frontend/LayoutConverter.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,22 @@ ConversionResult LayoutConverter::convertAvroLayout(QString path) {
403403
return saveLayout(layout, savePath);
404404
}
405405

406+
ConversionResult LayoutConverter::saveLayout(QString path) {
407+
QFile jsonFile(path);
408+
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
409+
return OpenError;
410+
}
411+
QString version = QJsonDocument::fromJson(jsonFile.readAll()).object()
412+
.value("info").toObject()
413+
.value("version").toString();
414+
if (version == "1") return convertLayoutFormat(path);
415+
if (version != "2") return UnsupportedLayout;
416+
417+
QFileInfo fileInfo(jsonFile);
418+
QString savePath = folders.getUserLayoutPath() + fileInfo.fileName();
419+
return jsonFile.copy(savePath) ? Ok : SaveError;
420+
}
421+
406422
ConversionResult LayoutConverter::saveLayout(QJsonObject obj, QString path) {
407423
QFile saveFile(path);
408424
if (!saveFile.open(QIODevice::WriteOnly)) {

src/frontend/LayoutConverter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ class LayoutConverter {
3939
public:
4040
ConversionResult convertAvroLayout(QString path);
4141
ConversionResult convertLayoutFormat(QString path);
42+
ConversionResult saveLayout(QString path);
4243
};

src/frontend/TopBar.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
static const QString DEFS_URL = "https://raw.githubusercontent.com/OpenBangla/OpenBangla-Keyboard/master/UPDATES.json";
4242

4343
TopBar::TopBar(bool darkIcon, QWidget *parent) :
44-
QMainWindow(parent),
45-
ui(new Ui::TopBar) {
44+
QMainWindow(parent),
45+
ui(new Ui::TopBar) {
4646
ui->setupUi(this);
4747

4848
gLayout = new Layout();
@@ -86,7 +86,7 @@ TopBar::TopBar(bool darkIcon, QWidget *parent) :
8686
// Update the counter to show only the message for the first three times
8787
gSettings->setTrayInfoCount(count + 1);
8888
}
89-
89+
9090
#ifndef NO_UPDATE_CHECK
9191
updater = QSimpleUpdater::getInstance();
9292

@@ -115,7 +115,7 @@ void TopBar::SetupTopBar() {
115115
if (gSettings->getTopBarWindowPosition() == QPoint(0, 0)) {
116116
int width = this->frameGeometry().width();
117117
int height = this->frameGeometry().height();
118-
118+
119119
QApplication *app = (QApplication *) QApplication::instance();
120120
QScreen *screen = app->primaryScreen();
121121

@@ -214,7 +214,7 @@ void TopBar::SetupTrayIcon() {
214214
gSettings->getTopBarVisibility() ? "Hide the TopBar" : "Show the TopBar",
215215
this
216216
);
217-
217+
218218
connect(trayTopBarVisibility, &QAction::triggered, [&]() {
219219
if(this->isVisible()) {
220220
this->setVisible(false);
@@ -236,7 +236,7 @@ void TopBar::SetupTrayIcon() {
236236
trayMenu->addSeparator();
237237
trayMenu->addAction(trayTopBarVisibility);
238238
trayMenu->addAction(trayQuit);
239-
239+
240240
tray->setContextMenu(trayMenu);
241241
tray->setVisible(true);
242242
}
@@ -293,9 +293,9 @@ void TopBar::layoutMenuLayouts_clicked() {
293293

294294
void TopBar::layoutMenuInstall_clicked() {
295295
QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, "Select Keyboard Layout", QDir::homePath(),
296-
"Avro Keyboard 5 Keyboard Layout (*.avrolayout)");
296+
"All Supported Layouts (*.avrolayout *.json);;Avro Keyboard 5 Keyboard Layout (*.avrolayout);;OpenBangla Keyboard Layout (*.json)");
297297
LayoutConverter conv;
298-
if (fileName.contains(".avrolayout") && fileName != "") {
298+
if (fileName.endsWith(".avrolayout")) {
299299
ConversionResult res = conv.convertAvroLayout(fileName);
300300
switch (res) {
301301
case Ok:
@@ -317,6 +317,26 @@ void TopBar::layoutMenuInstall_clicked() {
317317
QMessageBox::Ok);
318318
break;
319319
}
320+
} else if (fileName.endsWith(".json")) {
321+
ConversionResult res = conv.saveLayout(fileName);
322+
switch (res) {
323+
case Ok:
324+
QMessageBox::information(Q_NULLPTR, "OpenBangla Keyboard", "Layout Installed Successfully",
325+
QMessageBox::Ok);
326+
break;
327+
case UnsupportedLayout:
328+
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Unsupported Layout file!",
329+
QMessageBox::Ok);
330+
break;
331+
case OpenError:
332+
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard",
333+
"An error occurred while opening the layout file!", QMessageBox::Ok);
334+
break;
335+
case SaveError:
336+
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Error occurred while saving the file!",
337+
QMessageBox::Ok);
338+
break;
339+
}
320340
}
321341
RefreshLayouts();
322342
}

0 commit comments

Comments
 (0)