void Configuration::save()
{
    QJsonArray newConfArray;
    for (QList<SSProfile>::iterator it = profileList.begin(); it != profileList.end(); ++it) {
        QJsonObject json;
        json["backend"] = QJsonValue(it->backend);
        json["type"] = QJsonValue(it->type);
        json["profile"] = QJsonValue(it->profileName);
        json["server"] = QJsonValue(it->server);
        json["server_port"] = QJsonValue(it->server_port);
        json["password"] = QJsonValue(it->password);
        json["local_address"] = QJsonValue(it->local_addr);
        json["local_port"] = QJsonValue(it->local_port);
        json["method"] = QJsonValue(it->method.isEmpty() ? QString("table") : it->method.toLower());//lower-case in config
        json["timeout"] = QJsonValue(it->timeout);
        if (tfo_available) {
            json["fast_open"] = QJsonValue(it->fast_open);
        }
        newConfArray.append(QJsonValue(json));
    }

    QJsonObject JSONObj;
    JSONObj["index"] = QJsonValue(m_index);
    JSONObj["debug"] = QJsonValue(debugLog);
    JSONObj["autoHide"] = QJsonValue(autoHide);
    JSONObj["autoStart"] = QJsonValue(autoStart);
    JSONObj["translucent"] = QJsonValue(translucent);
    JSONObj["configs"] = QJsonValue(newConfArray);

    QJsonDocument JSONDoc(JSONObj);

    QFile JSONFile(m_file);
    JSONFile.open(QIODevice::WriteOnly | QIODevice::Text);
    if (JSONFile.isWritable()) {
        JSONFile.write(JSONDoc.toJson());
    }
    else {
        qWarning() << "Warning: file is not writable!";
    }
    JSONFile.close();
}
void ConfigHelper::exportGuiConfigJson(const QString &file)
{
    QJsonArray confArray;
    int size = model->rowCount();
    for (int i = 0; i < size; ++i) {
        Connection *con = model->getItem(i)->getConnection();
        QJsonObject json;
        json["remarks"] = QJsonValue(con->profile.name);
        json["method"] = QJsonValue(con->profile.method.toLower());
        json["password"] = QJsonValue(con->profile.password);
        json["server_port"] = QJsonValue(con->profile.serverPort);
        json["server"] = QJsonValue(con->profile.serverAddress);
        confArray.append(QJsonValue(json));
    }

    QJsonObject JSONObj;
    JSONObj["configs"] = QJsonValue(confArray);
    JSONObj["localPort"] = QJsonValue(1080);
    JSONObj["shareOverLan"] = QJsonValue(false);

    QJsonDocument JSONDoc(JSONObj);

    QFile JSONFile(file);
    JSONFile.open(QIODevice::WriteOnly | QIODevice::Text);
    if (!JSONFile.isOpen()) {
        qCritical() << "Error: cannot open " << file;
        return;
    }
    if(!JSONFile.isWritable()) {
        qCritical() << "Error: cannot write into " << file;
        return;
    }

    JSONFile.write(JSONDoc.toJson());
    JSONFile.close();
}