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
|
|
|
inline void initResources()
|
|
|
|
{
|
2022-02-27 23:39:55 +01:00
|
|
|
Q_INIT_RESOURCE(migrations);
|
2022-06-29 17:20:34 +02:00
|
|
|
Q_INIT_RESOURCE(plaintexts);
|
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
|
|
|
void Common::setPdfViewer()
|
|
|
|
{
|
|
|
|
QString value;
|
|
|
|
|
|
|
|
/* TODO: well, we should query this probably from xdg*/
|
2022-07-24 18:19:38 +02:00
|
|
|
QString okularPath = QStandardPaths::findExecutable("okular");
|
|
|
|
QString evincePath = QStandardPaths::findExecutable("evince");
|
|
|
|
QString qpdfviewPath = QStandardPaths::findExecutable("qpdfview");
|
2022-04-25 23:01:05 +02:00
|
|
|
|
|
|
|
if(okularPath != "")
|
|
|
|
{
|
|
|
|
value = okularPath + " %f -p %p";
|
|
|
|
}
|
|
|
|
else if(evincePath != "")
|
|
|
|
{
|
2022-06-29 19:29:29 +02:00
|
|
|
value = evincePath + " -i %p %f";
|
2022-04-25 23:01:05 +02:00
|
|
|
}
|
|
|
|
else if(qpdfviewPath != "")
|
|
|
|
{
|
2022-06-29 19:29:29 +02:00
|
|
|
value = qpdfviewPath + " %f#%p";
|
2022-04-25 23:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value != "")
|
|
|
|
{
|
2022-07-24 23:57:38 +02:00
|
|
|
QSettings settings;
|
2022-04-25 23:01:05 +02:00
|
|
|
settings.setValue(SETTINGS_KEY_PDFVIEWER, value);
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 21:36:45 +02:00
|
|
|
void Common::ensureConfigured()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
2022-06-13 20:13:14 +02:00
|
|
|
QString dbpath = databasePath();
|
|
|
|
if(dbpath == "")
|
2020-08-24 21:36:45 +02:00
|
|
|
{
|
2022-06-13 20:13:14 +02:00
|
|
|
dbpath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
|
2020-08-24 21:36:45 +02:00
|
|
|
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";
|
2022-06-13 20:13:14 +02:00
|
|
|
}
|
|
|
|
if(!QFile::exists(dbpath))
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
settings.setValue(SETTINGS_KEY_DBPATH, dbpath);
|
|
|
|
}
|
2022-06-13 20:13:14 +02:00
|
|
|
DatabaseFactory factory{dbpath};
|
|
|
|
auto db = factory.forCurrentThread();
|
|
|
|
DBMigrator migrator{db};
|
|
|
|
if(migrator.migrationNeeded())
|
2020-08-24 21:36:45 +02:00
|
|
|
{
|
2022-06-13 20:13:14 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
QVariant pdfViewer = settings.value(SETTINGS_KEY_PDFVIEWER);
|
|
|
|
if(!pdfViewer.isValid())
|
|
|
|
{
|
|
|
|
setPdfViewer();
|
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
|
|
|
|
2022-06-06 23:18:58 +02:00
|
|
|
bool Common::noSandboxModeRequested()
|
|
|
|
{
|
|
|
|
QString env = getenv("LOOQS_DISABLE_SANDBOX");
|
|
|
|
if(env == "1")
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
QStringList Common::excludedPaths()
|
|
|
|
{
|
|
|
|
static int ran = false;
|
2022-06-13 19:36:12 +02:00
|
|
|
static QStringList excludedPaths;
|
2022-05-29 15:46:06 +02:00
|
|
|
if(!ran)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
2022-06-13 19:36:12 +02:00
|
|
|
QStringList defaults{"/proc", "/sys", "/dev", "/tmp", "/var/run", "/run"};
|
|
|
|
excludedPaths = settings.value(SETTINGS_KEY_EXCLUDEDPATHS, defaults).toStringList();
|
2022-05-29 15:46:06 +02:00
|
|
|
ran = true;
|
|
|
|
}
|
2022-06-13 19:36:12 +02:00
|
|
|
return excludedPaths;
|
2022-05-29 15:46:06 +02:00
|
|
|
}
|
2022-05-30 20:03:40 +02:00
|
|
|
|
2022-06-05 20:53:37 +02:00
|
|
|
QStringList Common::mountPaths()
|
|
|
|
{
|
|
|
|
static int ran = false;
|
|
|
|
static QStringList mountPaths;
|
|
|
|
if(!ran)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
mountPaths = settings.value(SETTINGS_KEY_MOUNTPATHS, QStringList{"/media", "/mnt"}).toStringList();
|
|
|
|
ran = true;
|
|
|
|
}
|
|
|
|
return mountPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Common::isMountPath(QString path)
|
|
|
|
{
|
|
|
|
QStringList mountPaths = Common::mountPaths();
|
|
|
|
for(QString &mountPath : mountPaths)
|
|
|
|
{
|
|
|
|
if(path.startsWith(mountPath))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-07-23 20:16:14 +02:00
|
|
|
|
|
|
|
QString Common::userManualUrl()
|
|
|
|
{
|
|
|
|
return QString("https://github.com/quitesimpleorg/looqs/blob/%1/USAGE.md").arg(QString(GIT_COMMIT_ID));
|
|
|
|
}
|