Skip to content

Commit

Permalink
Tidy Update Database Config File Loading and Possible fix for Issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishwaldo committed Jun 4, 2020
1 parent 81f9e21 commit 1e7baf0
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 85 deletions.
134 changes: 134 additions & 0 deletions ozwadmin-main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <QLoggingCategory>
#include <QStyleFactory>

#include <qt-openzwave/qt-openzwavedatabase.h>

#include "util.h"
#include "mainwindow.h"

Expand Down Expand Up @@ -80,9 +82,141 @@ int main(int argc, char *argv[])
);
parser.addOption(remoteKey);

QCommandLineOption configDir(QStringList() << "c" << "config-dir",
"Directory containing the OZW Config Files",
"configDir"
);

parser.addOption(configDir);

QCommandLineOption userDir(QStringList() << "u" << "user-dir",
"Directory for the OZW User Files",
"userDir"
);

parser.addOption(userDir);


parser.process(a);

QSettings settings;

QString dbPath, userPath;

if (parser.isSet(configDir)) {
QStringList PossibleDBPaths;
PossibleDBPaths << parser.value(configDir);
QString path;
foreach(path, PossibleDBPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path)).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path).append("/")).absoluteFilePath();
break;
}
}
/* if we dont have a dbPath, Deploy our config files there */
if (dbPath.isEmpty()) {
qCWarning(ozwadmin) << "Configuration Database Does Not Exist - Copying Database to Location " << QFileInfo(parser.value(configDir).append("/")).absoluteFilePath();
QStringList Locations;
Locations << ".";
if (initConfigDatabase(Locations)) {
copyConfigDatabase(QFileInfo(parser.value(configDir).append("/")).absoluteFilePath());
} else {
qCWarning(ozwadmin) << "Cant find qt-openzwavedatabase.rcc";
QCoreApplication::exit(-1);
}
dbPath = QFileInfo(parser.value(configDir).append("/")).absoluteFilePath();
}
} else {
/* search Default Locations for Config Files */
QStringList PossibleDBPaths;
PossibleDBPaths << settings.value("openzwave/ConfigPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleDBPaths << "./config/";
PossibleDBPaths << QDir::toNativeSeparators("../../../config/");
PossibleDBPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
QString path;
foreach(path, PossibleDBPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path)).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path).append("/")).absoluteFilePath();
break;
}
}
/* if we dont have a dbPath, Deploy our config files there */
if (dbPath.isEmpty()) {
dbPath = QFileInfo("./").absoluteFilePath();
qCWarning(ozwadmin) << "Configuration Database Does Not Exist - Copying Database to Default Location " << dbPath;
QStringList Locations;
Locations << ".";
if (initConfigDatabase(Locations)) {
copyConfigDatabase(dbPath);
} else {
qCWarning(ozwadmin) << "Cant find qt-openzwavedatabase.rcc";
QCoreApplication::exit(-1);
}
}
}
settings.setValue("openzwave/ConfigPath", dbPath);

if (parser.isSet(userDir)) {
QStringList PossibleCfgPaths;
PossibleCfgPaths << parser.value(userDir);
QString path;
foreach(path, PossibleCfgPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path)).absoluteFilePath() << " for options.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path).append("/")).absoluteFilePath();
break;
}
}
/* if we dont have a userPath, Deploy our config files there */
if (userPath.isEmpty()) {
qCWarning(ozwadmin) << "User Configuration Path Does Not Exist - Copying Config Files to Location " << QFileInfo(parser.value(userDir).append("/")).absoluteFilePath();
QStringList Locations;
Locations << ".";
if (initConfigDatabase(Locations)) {
copyUserDatabase(QFileInfo(parser.value(userDir).append("/")).absoluteFilePath());
} else {
qCWarning(ozwadmin) << "Cant find qt-openzwavedatabase.rcc";
QCoreApplication::exit(-1);
}
userPath = QFileInfo(parser.value(userDir).append("/")).absoluteFilePath();
}
} else {
/* search Default Locations for Config Files */
QStringList PossibleUserPaths;
PossibleUserPaths << settings.value("openzwave/UserPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleUserPaths << "./config/";
PossibleUserPaths << QDir::toNativeSeparators("../../../config/");
PossibleUserPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
QString path;
foreach(path, PossibleUserPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path)).absoluteFilePath() << " for options.xml";
if (QFileInfo(QDir::toNativeSeparators(path+"/options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path).append("/")).absoluteFilePath();
break;
}
}
/* if we dont have a dbPath, Deploy our config files there */
if (userPath.isEmpty()) {
userPath = QFileInfo("./").absoluteFilePath();
qCWarning(ozwadmin) << "Configuration Database Does Not Exist - Copying Database to Default Location " << dbPath;
QStringList Locations;
Locations << ".";
if (initConfigDatabase(Locations)) {
copyUserDatabase(userPath);
} else {
qCWarning(ozwadmin) << "Cant find qt-openzwavedatabase.rcc";
QCoreApplication::exit(-1);
}
}
}
settings.setValue("openzwave/UserPath", userPath);
finiConfigDatabase();
qCInfo(ozwadmin) << "DBPath: " << dbPath;
qCInfo(ozwadmin) << "userPath: " << userPath;


MainWindow w;
w.show();
if (parser.isSet(serialPort)) {
Expand Down
85 changes: 2 additions & 83 deletions ozwadmin-main/ozwcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,89 +19,8 @@ OZWCore *OZWCore::get() {


void OZWCore::initilize() {
#ifndef Q_OS_WASM
QStringList PossibleDBPaths;
PossibleDBPaths << settings.value("openzwave/ConfigPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleDBPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);

QString path, dbPath, userPath;
foreach(path, PossibleDBPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path + "/config/manufacturer_specific.xml")).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFileInfo(QDir::toNativeSeparators(path + "/config/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path + "/config/manufacturer_specific.xml")).absoluteFilePath();
break;
}
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path + "../config/manufacturer_specific.xml")).absoluteFilePath() << " for manufacturer_specific.xml";
if (QFile(QDir::toNativeSeparators(path + "/../config/manufacturer_specific.xml")).exists()) {
dbPath = QFileInfo(QDir::toNativeSeparators(path + "/../config/manufacturer_specific.xml")).absoluteFilePath();
break;
}
}
PossibleDBPaths.clear();
PossibleDBPaths << settings.value("openzwave/UserPath", QDir::toNativeSeparators("../../../config/")).toString().append("/");
PossibleDBPaths << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);

foreach(path, PossibleDBPaths) {
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path + "/config/Options.xml")).absoluteFilePath() << " for Options.xml";
if (QFileInfo(QDir::toNativeSeparators(path + "/config/Options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path + "/config/Options.xml")).absoluteFilePath();
break;
}
qCDebug(ozwadmin) << "Checking " << QFileInfo(QDir::toNativeSeparators(path + "/../config/Options.xml")).absoluteFilePath() << " for Options.xml";
if (QFile(QDir::toNativeSeparators(path + "/../config/Options.xml")).exists()) {
userPath = QFileInfo(QDir::toNativeSeparators(path + "/../config/Options.xml")).absoluteFilePath();
break;
}
}

qCDebug(ozwadmin) << "DBPath: " << dbPath;
qCDebug(ozwadmin) << "userPath: " << userPath;

if (dbPath.isEmpty()) {
qCInfo(ozwadmin) << "Deploying OZW Database to " << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
QStringList paths;
paths << "." << "../../qt-openzwave/qt-openzwavedatabase/";
if (!initConfigDatabase(paths)) {
qCWarning(ozwadmin) << "The qt-openzwavedatabase.rcc file could not be found";
emit raiseCriticalError("Missing qt-openzwavedatabase.rcc Database File", "The qt-openzwavedatabase.rcc file could not be found");
exit(-1);
}
QString dir = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
if (copyConfigDatabase(QDir(dir).absolutePath())) {
qCInfo(ozwadmin) << "Copied Database to " << dir;
}
else {
qCWarning(ozwadmin) << "Copying Config Database Failed";
emit raiseCriticalError("Copying Config Database Failed", "Copying Config Database Failed");
exit(-1);
}
dbPath = QFileInfo(dir.append("/config/")).absolutePath();
m_configpath.setPath(dbPath);
settings.setValue("openzwave/ConfigPath", m_configpath.absolutePath());
qCInfo(ozwadmin) << "m_configPath set to " << m_configpath.absolutePath();
}
else
{
m_configpath.setPath(QFileInfo(dbPath).absolutePath());
settings.setValue("openzwave/ConfigPath", m_configpath.absolutePath());
qCInfo(ozwadmin) << "Found Existing DB Path" << m_configpath.absolutePath();
}

if (userPath.isEmpty()) {
userPath = dbPath;
m_userpath.setPath(QFileInfo(userPath).absolutePath());
settings.setValue("openzwave/UserPath", m_userpath.absolutePath());
qCInfo(ozwadmin) << "UserPath is Set to DBPath: " << m_userpath.absolutePath();
}
else {
m_userpath.setPath(QFileInfo(userPath).absolutePath());
qCInfo(ozwadmin) << "UserPath is Set from Settings" << m_userpath.absolutePath();
settings.setValue("openzwave/UserPath", m_userpath.absolutePath());
}
#else
m_configpath = ".";
m_userpath = ".";
#endif
m_configpath = QFileInfo(settings.value("openzwave/ConfigPath").toString()).absoluteFilePath();
m_userpath = QFileInfo(settings.value("openzwave/UserPath").toString()).absoluteFilePath();
this->m_openzwave = new QTOpenZwave(this, m_configpath, m_userpath);
this->m_qtozwmanager = this->m_openzwave->GetManager();
}
Expand Down
4 changes: 2 additions & 2 deletions ozwadmin-main/ozwcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public slots:
QTOpenZwave *m_openzwave;
QTOZWManager *m_qtozwmanager;
QString m_serialport;
QDir m_configpath;
QDir m_userpath;
QString m_configpath;
QString m_userpath;
};


Expand Down

0 comments on commit 1e7baf0

Please sign in to comment.