mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 11:17:34 +02:00
73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
//
|
|
// Bookmarks.cpp
|
|
// interface/src
|
|
//
|
|
// Created by David Rowe on 13 Jan 2015.
|
|
// Copyright 2015 High Fidelity, Inc.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QStandardPaths>
|
|
|
|
#include "Bookmarks.h"
|
|
|
|
Bookmarks::Bookmarks() {
|
|
_bookmarksFilename = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + BOOKMARKS_FILENAME;
|
|
readFromFile();
|
|
}
|
|
|
|
void Bookmarks::insert(const QString& name, const QString& address) {
|
|
_bookmarks.insert(name, address);
|
|
|
|
if (contains(name)) {
|
|
qDebug() << "Added bookmark:" << name << "," << address;
|
|
persistToFile();
|
|
} else {
|
|
qWarning() << "Couldn't add bookmark: " << name << "," << address;
|
|
}
|
|
}
|
|
|
|
void Bookmarks::remove(const QString& name) {
|
|
_bookmarks.remove(name);
|
|
|
|
if (!contains(name)) {
|
|
qDebug() << "Deleted bookmark:" << name;
|
|
persistToFile();
|
|
} else {
|
|
qWarning() << "Couldn't delete bookmark:" << name;
|
|
}
|
|
}
|
|
|
|
bool Bookmarks::contains(const QString& name) const {
|
|
return _bookmarks.contains(name);
|
|
}
|
|
|
|
void Bookmarks::readFromFile() {
|
|
QFile loadFile(_bookmarksFilename);
|
|
|
|
if (!loadFile.open(QIODevice::ReadOnly)) {
|
|
qWarning("Couldn't open bookmarks file for reading");
|
|
return;
|
|
}
|
|
|
|
QByteArray data = loadFile.readAll();
|
|
QJsonDocument json(QJsonDocument::fromJson(data));
|
|
_bookmarks = json.object().toVariantMap();
|
|
}
|
|
|
|
void Bookmarks::persistToFile() {
|
|
QFile saveFile(_bookmarksFilename);
|
|
|
|
if (!saveFile.open(QIODevice::WriteOnly)) {
|
|
qWarning("Couldn't open bookmarks file for writing");
|
|
return;
|
|
}
|
|
|
|
QJsonDocument json(QJsonObject::fromVariantMap(_bookmarks));
|
|
QByteArray data = json.toJson();
|
|
saveFile.write(data);
|
|
}
|