First pass at Bookmarks class

In-memory for starters.
This commit is contained in:
David Rowe 2015-01-13 17:56:34 -08:00
parent dd2421ffcd
commit 7130c579f5
2 changed files with 93 additions and 0 deletions

View file

@ -0,0 +1,54 @@
//
// 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 "Bookmarks.h"
Bookmarks::Bookmarks() {
}
void Bookmarks::insert(QString name, QString address) {
QString key = name.toLower();
if (isValidName(name)) {
QJsonObject bookmark;
bookmark.insert("name", name);
bookmark.insert("address", address);
_bookmarks.insert(key, bookmark);
if (contains(key)) {
qDebug() << "Added bookmark: " << name << ", " << address;
} else {
qDebug() << "Couldn't add bookmark: " << name << ", " << address;
}
} else {
qDebug() << "Invalid bookmark: " << name << ", " << address;
}
}
void Bookmarks::remove(QString name) {
QString key = name.toLower();
_bookmarks.remove(key);
if (!contains(key)) {
qDebug() << "Removed bookmark: " << name;
} else {
qDebug() << "Couldn't remove bookmark: " << name;
}
}
bool Bookmarks::contains(QString name) {
return _bookmarks.contains(name.toLower());
}
bool Bookmarks::isValidName(QString name) {
return _nameRegExp.exactMatch(name);
}

39
interface/src/Bookmarks.h Normal file
View file

@ -0,0 +1,39 @@
//
// Bookmarks.h
// 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
//
#ifndef hifi_Bookmarks_h
#define hifi_Bookmarks_h
#include <QJsonObject>
#include <QDebug>
#include <QMap>
#include <QObject>
#include <QStringList>
class Bookmarks: public QObject {
Q_OBJECT
public:
Bookmarks();
void insert(QString name, QString address); // Overwrites any existing entry with same name.
void remove(QString name);
bool contains(QString name);
bool isValidName(QString name);
private:
QMap<QString, QJsonObject> _bookmarks; // key: { name: string, address: string }
// key is a lowercase copy of name, used to make the bookmarks case insensitive.
const QRegExp _nameRegExp = QRegExp("^[\\w\\-]+$");
};
#endif // hifi_Bookmarks_h