27 #include <QCoreApplication> 28 #include <QSqlDatabase> 29 #include <QThreadPool> 54 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Received SIGPIPE interrupt - ignoring"));
58 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Received %1")
59 .arg(Sig == SIGINT ? QStringLiteral(
"SIGINT") : QStringLiteral(
"SIGTERM")));
61 signal(SIGINT, SIG_DFL);
72 if ((Init && gLocalContext->Init()) || !Init)
82 gLocalContext =
nullptr;
89 gLocalContext->
Notify(event);
98 m_dbName(QStringLiteral(
"")),
100 m_localSettingsLock(QReadWriteLock::Recursive),
101 m_adminThread(
nullptr),
106 m_startTime(QDateTime::currentMSecsSinceEpoch()),
107 m_rootSetting(
nullptr)
113 m_dbName = CommandLine->
GetValue(QStringLiteral(
"db")).toString();
121 setObjectName(QStringLiteral(
"LocalContext"));
132 QString logfile = CommandLine->
GetValue(QStringLiteral(
"logfile")).toString();
133 if (logfile.isEmpty())
143 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Dir: Using '%1'")
147 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Version: %1").arg(QStringLiteral(GIT_VERSION)));
148 LOG(VB_GENERAL, LOG_NOTICE,
152 TorcLocalContext::~TorcLocalContext()
160 m_adminThread->quit();
161 m_adminThread->wait();
162 delete m_adminThread;
163 m_adminThread =
nullptr;
171 QThreadPool::globalInstance()->waitForDone();
178 m_rootSetting =
nullptr;
183 m_sqliteDB =
nullptr;
186 qInstallMessageHandler(
nullptr);
194 bool TorcLocalContext::Init(
void)
202 if (!dir.mkpath(configdir))
204 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to create config directory ('%1')")
211 if (m_dbName.isEmpty())
212 m_dbName = configdir +
"/" +
TORC_TORC +
"-settings.sqlite";
216 if (!m_sqliteDB || !m_sqliteDB->
IsValid())
221 QWriteLocker locker(&m_localSettingsLock);
230 QString uuid = QUuid::createUuid().toString();
231 if (uuid.startsWith(
'{'))
233 if (uuid.endsWith(
'}'))
236 m_uuid = uuidsaved->
GetValue().toString();
241 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"UUID: %1").arg(m_uuid));
258 QThread::currentThread()->setPriority(QThread::TimeCriticalPriority);
261 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Qt runtime version '%1' (compiled with '%2')")
262 .arg(qVersion(), QStringLiteral(QT_VERSION_STR)));
267 m_adminThread->start();
277 return GetDBSetting(Name, DefaultValue);
282 QString value = GetDBSetting(Name, DefaultValue ? QStringLiteral(
"1") : QStringLiteral(
"0"));
283 return value.trimmed() == QStringLiteral(
"1");
288 QString value = GetDBSetting(Name, QString::number(DefaultValue));
289 return value.toInt();
294 SetDBSetting(Name, Value);
299 SetDBSetting(Name, Value ? QStringLiteral(
"1") : QStringLiteral(
"0"));
304 SetDBSetting(Name, QString::number(Value));
309 QReadLocker locker(&m_localSettingsLock);
315 QReadLocker locker(&m_localSettingsLock);
321 QReadLocker locker(&m_localSettingsLock);
328 QWriteLocker locker(&m_localSettingsLock);
329 if (Delay < 1 || Delay > 300)
331 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Not setting shutdown delay to %1: must be 1<->300 seconds").arg(Delay));
333 else if (Delay > m_shutdownDelay)
335 m_shutdownDelay = Delay;
336 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Set shutdown delay to %1 seconds").arg(m_shutdownDelay));
342 QReadLocker locker(&m_localSettingsLock);
343 return m_shutdownDelay;
364 (void)HandleShutdown(m_shutdownEvent);
369 QWriteLocker locker(&m_localSettingsLock);
371 if (m_shutdownDelay < 1)
393 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Shutdown already queued - ignoring '%1' event").arg(TorcCoreUtils::EnumToString<Torc::Actions>((
Torc::Actions)newevent)));
397 m_shutdownEvent = newevent;
398 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Queued '%1' event for %2 seconds").arg(TorcCoreUtils::EnumToString<Torc::Actions>((
Torc::Actions)m_shutdownEvent)).arg(m_shutdownDelay));
405 bool TorcLocalContext::HandleShutdown(
int Event)
407 int event = Event ==
Torc::None ? m_shutdownEvent : Event;
411 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Restarting application"));
416 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Stopping application"));
418 QCoreApplication::quit();
458 else if (HandleShutdown(
event))
462 return QObject::event(Event);
472 QReadLocker locker(&m_localSettingsLock);
473 return m_rootSetting;
478 QReadLocker locker(&m_localSettingsLock);
487 QString TorcLocalContext::GetDBSetting(
const QString &Name,
const QString &DefaultValue)
490 QReadLocker locker(&m_localSettingsLock);
491 if (m_localSettings.contains(Name))
492 return m_localSettings.value(Name);
495 SetDBSetting(Name, DefaultValue);
499 void TorcLocalContext::SetDBSetting(
const QString &Name,
const QString &Value)
501 QWriteLocker locker(&m_localSettingsLock);
504 m_localSettings[Name] = Value;
A simple thread to launch helper objects outside of the main loop.
static qint16 Create(TorcCommandLine *CommandLine, bool Init=true)
static void EventLoopEnding(bool Ending)
bool IsValid(void)
Returns true if the datbase has been opened/created.
#define TORC_ROOT_SETTING
void CloseThreadConnection(void)
Close the database connection for the current thread.
TorcLocalContext * gLocalContext
High level group of related settings.
TorcLocalContext is the core Torc object.
A wrapper around a database setting.
int GetEvent(void)
Return the Torc action associated with this event.
Torc command line handler.
QString GetUuid(void) const
virtual bool DownRef(void)
#define TORC_EXIT_RESTART
void SetSetting(const QString &Name, const QString &Value)
int ParseVerboseArgument(const QString &arg)
static void NotifyEvent(int Event)
A class to track and manage language and locale settings and available translations.
#define TORC_EXIT_NO_CONTEXT
static void CreateObjects(void)
Iterates through the list of registered TorcAdminObject's and creates them.
void ShutdownTimeout(void)
static void TearDown(void)
QString GetSetting(const QString &Name, const QString &DefaultValue)
uint GetShutdownDelay(void)
void InitialiseTorcDirectories(TorcCommandLine *CommandLine)
Statically initialise the various directories that Torc uses.
static void SetMainThread(void)
TorcLanguage * GetLanguage(void)
bool event(QEvent *Event) override
void CloseDatabaseConnections(void)
A general purpose event object.
#define LOG(_MASK_, _LEVEL_, _STRING_)
void DeregisterQThread(void)
Deregister a non-Torc QThread from logging and close any database connections.
void RegisterLoggingThread(void)
void AddObserver(QObject *Observer)
brief Register the given object to receive events.
void Notify(const TorcEvent &Event)
Brief Send the given event to each registered listener/observer.
void RegisterQThread(void)
Register a non-Torc QThread for logging and database access.
TorcSetting * GetRootSetting(void)
void StartLogging(const QString &Logfile, int progress=0, int quiet=0, const QString &level=QStringLiteral("info"), bool Propagate=false)
qint64 GetStartTime(void)
void SetShutdownDelay(uint Delay)
void RemoveObserver(QObject *Observer)
brief Deregister the given object.
QVariant GetValue(const QString &Key)
Return the value associated with Key or an invalid QVariant if the option is not present.
bool QueueShutdownEvent(int Event)
static void DestroyObjects(void)
Destroys each created admin object.
void SetSetting(const QString &Name, const QString &Value)
Set the setting Name to the value Value.
void QtMessage(QtMsgType Type, const QMessageLogContext &Context, const QString &Message)
A handler routine for Qt messages.
void DeregisterLoggingThread(void)
QString GetTorcConfigDir(void)
Return the path to the application configuration directory.
void LoadSettings(QMap< QString, QString > &Settings)
Retrieve all persistent settings stored in the database.
SQLite implementation of TorcDB.
static void ExitHandler(int Sig)