| 1 | /* |
| 2 | * chewing-editor: Chewing userphrase editor |
| 3 | * Copyright (C) 2014 Chewing Development Team |
| 4 | |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include "ChewingExporter.h" |
| 21 | |
| 22 | #include <QDebug> |
| 23 | #include <QFile> |
| 24 | #include <QJsonDocument> |
| 25 | #include <QJsonObject> |
| 26 | |
| 27 | ChewingExporter::ChewingExporter(const QString& path) |
| 28 | :UserphraseExporter{path} |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void ChewingExporter::addUserphraseImpl( |
| 33 | const QString& phrase, |
| 34 | const QString& bopomofo) |
| 35 | { |
| 36 | QJsonObject obj; |
| 37 | |
| 38 | obj["phrase"] = phrase; |
| 39 | obj["bopomofo"] = bopomofo; |
| 40 | |
| 41 | array_.append(obj); |
| 42 | |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | bool ChewingExporter::saveImpl() |
| 47 | { |
| 48 | QJsonObject root; |
| 49 | |
| 50 | root["userphrase"] = array_; |
| 51 | |
| 52 | QJsonDocument doc{root}; |
| 53 | |
| 54 | QFile file{path_}; |
| 55 | |
| 56 | if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { |
| 57 | qDebug() << "Cannot open" << path_; |
| 58 | return false; |
| 59 | } |
| 60 | file.write(doc.toJson()); |
| 61 | |
| 62 | return true; |
| 63 | } |