2020-05-24 15:36:27 +02:00
|
|
|
#include <QProcessEnvironment>
|
|
|
|
#include <QSettings>
|
2020-08-24 21:36:45 +02:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QDebug>
|
2022-05-30 20:03:40 +02:00
|
|
|
#include <QMimeDatabase>
|
2021-06-12 14:59:58 +02:00
|
|
|
#include "looqsgeneralexception.h"
|
2020-05-23 22:52:42 +02:00
|
|
|
#include "common.h"
|
2022-02-27 23:39:55 +01:00
|
|
|
#include "dbmigrator.h"
|
|
|
|
#include "databasefactory.h"
|
|
|
|
#include "logger.h"
|
2020-05-23 22:52:42 +02:00
|
|
|
|
2020-08-24 21:36:45 +02:00
|
|
|
#define SETTINGS_KEY_DBPATH "dbpath"
|
|
|
|
#define SETTINGS_KEY_FIRSTRUN "firstrun"
|
2022-04-24 12:12:23 +02:00
|
|
|
#define SETTINGS_KEY_IPCSOCKETPATH "ipcsocketpath"
|
2022-04-25 23:01:05 +02:00
|
|
|
#define SETTINGS_KEY_PDFVIEWER "pdfviewer"
|
2022-05-29 15:46:06 +02:00
|
|
|
#define SETTINGS_KEY_EXCLUDEDPATHS "excludedpaths"
|
2020-08-24 21:36:45 +02:00
|
|
|
|
|
|
|
inline void initResources()
|
|
|
|
{
|
2022-02-27 23:39:55 +01:00
|
|
|
Q_INIT_RESOURCE(migrations);
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Common::initSqliteDatabase(QString path)
|
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
|
|
|
db.setDatabaseName(path);
|
|
|
|
if(!db.open())
|
|
|
|
{
|
|
|
|
qDebug() << "failed to open database: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
initResources();
|
2022-02-27 23:39:55 +01:00
|
|
|
DBMigrator migrator{db};
|
|
|
|
migrator.performMigrations();
|
2020-08-24 21:36:45 +02:00
|
|
|
db.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:01:05 +02:00
|
|
|
QString Common::findInPath(QString needle)
|
|
|
|
{
|
|
|
|
QStringList results;
|
|
|
|
QString pathVar = QProcessEnvironment::systemEnvironment().value("PATH", "/usr/bin/:/bin/:");
|
|
|
|
QStringList paths = pathVar.split(":");
|
|
|
|
for(const QString &path : paths)
|
|
|
|
{
|
|
|
|
// TODO: can pass ../ but so be it for now.
|
|
|
|
|
|
|
|
QFileInfo info{path + "/" + needle};
|
|
|
|
if(info.exists())
|
|
|
|
{
|
|
|
|
return info.absoluteFilePath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Common::setPdfViewer()
|
|
|
|
{
|
|
|
|
QString value;
|
|
|
|
|
|
|
|
/* TODO: well, we should query this probably from xdg*/
|
|
|
|
QString okularPath = findInPath("okular");
|
|
|
|
QString evincePath = findInPath("evince");
|
|
|
|
QString qpdfviewPath = findInPath("qpdfview");
|
|
|
|
|
|
|
|
if(okularPath != "")
|
|
|
|
{
|
|
|
|
value = okularPath + " %f -p %p";
|
|
|
|
}
|
|
|
|
else if(evincePath != "")
|
|
|
|
{
|
|
|
|
value = evincePath + "-i %p %f";
|
|
|
|
}
|
|
|
|
else if(qpdfviewPath != "")
|
|
|
|
{
|
|
|
|
value = qpdfviewPath + "%f#%p";
|
|
|
|
}
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
if(value != "")
|
|
|
|
{
|
|
|
|
settings.setValue(SETTINGS_KEY_PDFVIEWER, value);
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 21:36:45 +02:00
|
|
|
void Common::ensureConfigured()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QVariant firstRun = settings.value(SETTINGS_KEY_FIRSTRUN);
|
|
|
|
if(!firstRun.isValid())
|
|
|
|
{
|
|
|
|
QString dbpath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
|
|
|
|
QDir dir;
|
|
|
|
if(!dir.exists(dbpath))
|
|
|
|
{
|
|
|
|
if(!dir.mkpath(dbpath))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Failed to create dbpath directory");
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-12 14:59:58 +02:00
|
|
|
dbpath += "/looqs.sqlite";
|
2020-08-24 21:36:45 +02:00
|
|
|
if(!initSqliteDatabase(dbpath))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Failed to initialize sqlite database");
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
2022-04-25 23:01:05 +02:00
|
|
|
|
2020-08-24 21:36:45 +02:00
|
|
|
settings.setValue(SETTINGS_KEY_FIRSTRUN, false);
|
|
|
|
settings.setValue(SETTINGS_KEY_DBPATH, dbpath);
|
2022-04-25 23:01:05 +02:00
|
|
|
setPdfViewer();
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
QString dbpath = databasePath();
|
|
|
|
if(!QFile::exists(dbpath))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("Database " + dbpath + " was not found");
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
2022-02-27 23:39:55 +01:00
|
|
|
DatabaseFactory factory{dbpath};
|
|
|
|
auto db = factory.forCurrentThread();
|
|
|
|
DBMigrator migrator{db};
|
|
|
|
if(migrator.migrationNeeded())
|
|
|
|
{
|
|
|
|
QFile out;
|
|
|
|
out.open(stderr, QIODevice::WriteOnly);
|
|
|
|
Logger migrationLogger{&out};
|
|
|
|
migrationLogger << "Database is being upgraded, please be patient..." << Qt::endl;
|
|
|
|
QObject::connect(&migrator, &DBMigrator::migrationDone,
|
|
|
|
[&migrationLogger](uint32_t migration)
|
|
|
|
{ migrationLogger << "Progress: Successfully migrated to: " << migration << Qt::endl; });
|
|
|
|
migrator.performMigrations();
|
|
|
|
migrationLogger << "Database upgraded successfully" << Qt::endl;
|
|
|
|
}
|
2020-08-24 21:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 22:52:42 +02:00
|
|
|
void Common::setupAppInfo()
|
|
|
|
{
|
|
|
|
QCoreApplication::setOrganizationName("quitesimple.org");
|
|
|
|
QCoreApplication::setOrganizationDomain("quitesimple.org");
|
2021-06-12 14:59:58 +02:00
|
|
|
QCoreApplication::setApplicationName("looqs");
|
2020-05-23 22:52:42 +02:00
|
|
|
}
|
2020-05-24 15:36:27 +02:00
|
|
|
|
|
|
|
QString Common::databasePath()
|
|
|
|
{
|
2022-04-24 17:19:09 +02:00
|
|
|
QString env = QProcessEnvironment::systemEnvironment().value("LOOQS_DB_OVERRIDE");
|
2020-05-24 15:36:27 +02:00
|
|
|
if(env == "")
|
|
|
|
{
|
|
|
|
QSettings settings;
|
2020-08-24 21:36:45 +02:00
|
|
|
return settings.value(SETTINGS_KEY_DBPATH).toString();
|
2020-05-24 15:36:27 +02:00
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
2022-04-24 12:12:23 +02:00
|
|
|
|
|
|
|
QString Common::ipcSocketPath()
|
|
|
|
{
|
2022-05-29 09:50:47 +02:00
|
|
|
return "/tmp/.looqs/looqs-ipc-socket";
|
|
|
|
|
|
|
|
/* May not a good idea to set it in the settings and probably nobody would ever bother to change it anyway */
|
|
|
|
// QSettings settings;
|
|
|
|
// return settings.value(SETTINGS_KEY_IPCSOCKETPATH, "/tmp/.looqs/looqs-ipc-socket").toString();
|
2022-04-24 12:12:23 +02:00
|
|
|
}
|
2022-05-29 15:46:06 +02:00
|
|
|
|
|
|
|
static QStringList excludedPaths = {"/proc", "/sys", "/dev", "/tmp", "/var/run", "/run"};
|
|
|
|
|
|
|
|
QStringList Common::excludedPaths()
|
|
|
|
{
|
|
|
|
static int ran = false;
|
|
|
|
if(!ran)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QStringList userExcludedPaths = settings.value(SETTINGS_KEY_EXCLUDEDPATHS).toStringList();
|
|
|
|
ran = true;
|
|
|
|
::excludedPaths.append(userExcludedPaths);
|
|
|
|
}
|
|
|
|
return ::excludedPaths;
|
|
|
|
}
|
2022-05-30 20:03:40 +02:00
|
|
|
|
|
|
|
bool Common::isTextFile(QFileInfo fileInfo)
|
|
|
|
{
|
|
|
|
/* TODO: This is not sandboxed yet ... */
|
|
|
|
QMimeDatabase mimeDatabase;
|
|
|
|
QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo);
|
|
|
|
if(mimeType.name().startsWith("text/"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(QString &str : mimeType.allAncestors())
|
|
|
|
{
|
|
|
|
if(str.startsWith("text/"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-31 18:40:40 +02:00
|
|
|
|
|
|
|
QString Common::versionText()
|
|
|
|
{
|
|
|
|
QString commitid = GIT_COMMIT_ID;
|
2022-05-31 18:42:20 +02:00
|
|
|
QString tag = GIT_TAG;
|
|
|
|
return tag + " (" + commitid + ") built " + __DATE__ + " " + __TIME__;
|
2022-05-31 18:40:40 +02:00
|
|
|
}
|