Skip to content

Commit

Permalink
Merge branch 'anytone-settings-extension' of https://github.com/hmatu…
Browse files Browse the repository at this point in the history
…schek/qdmr into anytone-settings-extension
  • Loading branch information
hmatuschek committed Apr 17, 2023
2 parents 5894b2c + 58d0ee7 commit c50d361
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions lib/d868uv_callsigndb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ D868UVCallsignDB::EntryElement::setContent(
{
unsigned addr = 0x0006;
writeASCII(addr, name, 16, 0x00); addr += std::min(16, name.size()); setUInt8(addr, 0); addr++;
writeASCII(addr, city, 16, 0x00); addr += std::min(16, city.size()); setUInt8(addr, 0); addr++;
writeASCII(addr, city, 15, 0x00); addr += std::min(15, city.size()); setUInt8(addr, 0); addr++;
writeASCII(addr, call, 8, 0x00); addr += std::min(8, call.size()); setUInt8(addr, 0); addr++;
writeASCII(addr, state, 16, 0x00); addr += std::min(16, state.size()); setUInt8(addr, 0); addr++;
writeASCII(addr, country, 16, 0x00); addr += std::min(16, country.size()); setUInt8(addr, 0); addr++;
Expand All @@ -81,7 +81,7 @@ unsigned
D868UVCallsignDB::EntryElement::size(const UserDatabase::User &user) {
return 6 // header
+ std::min(16, user.name.size())+1 // name
+ std::min(16, user.city.size())+1 // city
+ std::min(15, user.city.size())+1 // city
+ std::min( 8, user.call.size())+1 // call
+ std::min(16, user.state.size())+1 // state
+ std::min(16, user.country.size())+1 // country
Expand Down
10 changes: 9 additions & 1 deletion lib/userdatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ UserDatabase::load(const QString &filename) {
QByteArray data = file.readAll();
file.close();

QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
if (doc.isEmpty()) {
QString msg = "Failed to load user DB: " + err.errorString();
logError() << msg;
emit error(msg);
return false;
}

if (! doc.isObject()) {
QString msg = "Failed to load user DB: JSON document is not an object!";
logError() << msg;
Expand Down
86 changes: 43 additions & 43 deletions lib/userdatabase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,71 @@
* @ingroup util */
class UserDatabase : public QAbstractTableModel
{
Q_OBJECT
Q_OBJECT

public:
/** Represents the user information within the @c UserDatabase. */
class User {
public:
/** Empty constructor. */
User();
/** Constructs entry from JSON object. */
User(const QJsonObject &obj);
/** Represents the user information within the @c UserDatabase. */
class User {
public:
/** Empty constructor. */
User();
/** Constructs entry from JSON object. */
User(const QJsonObject &obj);

/** Returns @c true if the entry is valid. */
inline bool isValid() const { return 0 != id; }
/** Returns @c true if the entry is valid. */
inline bool isValid() const { return 0 != id; }

/** Returns the "distance" between this user and the given ID. */
unsigned distance(unsigned id) const;

/** The DMR ID of the user. */
unsigned id;
/** The callsign of the user. */
QString call;
/** The name of the user. */
QString name;
/** The surname of the user. */
QString surname;
/** The DMR ID of the user. */
unsigned id;
/** The callsign of the user. */
QString call;
/** The name of the user. */
QString name;
/** The surname of the user. */
QString surname;
/** The city of the user. */
QString city;
/** The state of the user. */
QString state;
/** The country of the user. */
QString country;
/** The country of the user. */
QString country;
/** Some arbitrary comment or text. */
QString comment;
};
};

public:
/** Constructs the user-database.
* The constructor will download the current user database if it was not downloaded yet or
* if the downloaded version is older than @c updatePeriodDays days. */
explicit UserDatabase(unsigned updatePeriodDays=30, QObject *parent=nullptr);
/** Constructs the user-database.
* The constructor will download the current user database if it was not downloaded yet or
* if the downloaded version is older than @c updatePeriodDays days. */
explicit UserDatabase(unsigned updatePeriodDays=30, QObject *parent=nullptr);

/** Returns the number of users. */
qint64 count() const;

/** Loads all entries from the downloaded user database. */
bool load();
/** Loads all entries from the downloaded user database at the specified location. */
bool load(const QString &filename);
/** Loads all entries from the downloaded user database. */
bool load();
/** Loads all entries from the downloaded user database at the specified location. */
bool load(const QString &filename);

/** Sorts users with respect to the distance to the given ID. */
void sortUsers(unsigned id);
/** Sorts users with respect to the minimum distance to the given IDs. */
void sortUsers(const QSet<unsigned> &ids);

/** Returns the user with index @c idx. */
/** Returns the user with index @c idx. */
const User &user(int idx) const;

/** Returns the age of the database in days. */
unsigned dbAge() const;
/** Returns the age of the database in days. */
unsigned dbAge() const;

/** Implements the QAbstractTableModel interface, returns the number of rows (number of entries). */
/** Implements the QAbstractTableModel interface, returns the number of rows (number of entries). */
int rowCount(const QModelIndex &parent=QModelIndex()) const;
/** Implements the QAbstractTableModel interface, returns the number of columns. */
/** Implements the QAbstractTableModel interface, returns the number of columns. */
int columnCount(const QModelIndex &parent=QModelIndex()) const;
/** Implements the QAbstractTableModel interface, return the entry data. */
/** Implements the QAbstractTableModel interface, return the entry data. */
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;

signals:
Expand All @@ -95,18 +95,18 @@ signals:
void error(const QString &msg);

public slots:
/** Starts the download of the user database. */
void download();
/** Starts the download of the user database. */
void download();

private slots:
/** Gets called whenever the download is complete. */
void downloadFinished(QNetworkReply *reply);
/** Gets called whenever the download is complete. */
void downloadFinished(QNetworkReply *reply);

private:
/** Holds all users sorted by their ID. */
QVector<User> _user;
/** The network access used for downloading. */
QNetworkAccessManager _network;
/** Holds all users sorted by their ID. */
QVector<User> _user;
/** The network access used for downloading. */
QNetworkAccessManager _network;
};


Expand Down

0 comments on commit c50d361

Please sign in to comment.