33 : timestamp( timestamp )
34 , providerId( providerId )
41 : timestamp( QDateTime::currentDateTime() )
59 if ( useMemoryDatabase )
61 createDatabase( QStringLiteral(
":memory:" ), error );
65 if ( !QFile::exists( historyFilename ) )
67 createDatabase( historyFilename, error );
71 openDatabase( historyFilename, error );
78 qDeleteAll( mProviders );
88 if ( mProviders.contains( provider->
id() ) )
91 mProviders.insert( provider->
id(), provider );
97 return mProviders.value(
id );
102 if ( !mProviders.contains(
id ) )
105 delete mProviders.take(
id );
111 return mProviders.keys();
127 const QString entryXml = xmlDoc.toString();
128 const QString dateTime =
entry.
timestamp.toString( QStringLiteral(
"yyyy-MM-dd HH:mm:ss" ) );
130 QString query =
qgs_sqlite3_mprintf(
"INSERT INTO history VALUES (NULL, '%q', '%q', '%q');",
131 entry.
providerId.toUtf8().constData(), entryXml.toUtf8().constData(), dateTime.toUtf8().constData() );
132 if ( !runEmptyQuery( query ) )
134 QgsDebugMsg( QStringLiteral(
"Couldn't story history entry in database!" ) );
138 id =
static_cast< int >( sqlite3_last_insert_rowid( mLocalDB.get() ) );
149 runEmptyQuery( QStringLiteral(
"BEGIN TRANSACTION;" ) );
152 runEmptyQuery( QStringLiteral(
"COMMIT TRANSACTION;" ) );
167 QgsDebugMsg( QStringLiteral(
"Cannot open database to query history entries" ) );
171 QString sql = QStringLiteral(
"SELECT provider_id, xml, timestamp FROM history WHERE id=%1" ).arg(
id );
176 if ( nErr == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
181 QgsDebugMsg( QStringLiteral(
"Cannot read history entry" ) );
188 QDateTime::fromString( statement.
columnAsText( 2 ), QStringLiteral(
"yyyy-MM-dd HH:mm:ss" ) ),
193 QgsDebugMsg( QStringLiteral(
"Cannot find history item with matching ID" ) );
208 const QString entryXml = xmlDoc.toString();
211 entryXml.toUtf8().constData(),
id );
212 if ( !runEmptyQuery( query ) )
214 QgsDebugMsg( QStringLiteral(
"Couldn't update history entry in database!" ) );
225 QList<QgsHistoryEntry> entries;
230 QgsDebugMsg( QStringLiteral(
"Cannot open database to query history entries" ) );
234 QString sql = QStringLiteral(
"SELECT provider_id, xml, timestamp FROM history" );
235 QStringList whereClauses;
236 if ( !providerId.isEmpty() )
238 whereClauses.append( QStringLiteral(
"provider_id='%1'" ).arg( providerId ) );
240 if ( start.isValid() )
242 whereClauses.append( QStringLiteral(
"timestamp>='%1'" ).arg( start.toString( QStringLiteral(
"yyyy-MM-dd HH:mm:ss" ) ) ) );
246 whereClauses.append( QStringLiteral(
"timestamp<='%1'" ).arg( end.toString( QStringLiteral(
"yyyy-MM-dd HH:mm:ss" ) ) ) );
249 if ( !whereClauses.empty() )
250 sql += QStringLiteral(
" WHERE (" ) + whereClauses.join( QLatin1String(
") AND (" ) ) +
')';
255 while ( nErr == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
260 QgsDebugMsg( QStringLiteral(
"Cannot read history entry" ) );
266 QDateTime::fromString( statement.
columnAsText( 2 ), QStringLiteral(
"yyyy-MM-dd HH:mm:ss" ) ),
285 runEmptyQuery( QStringLiteral(
"DELETE from history;" ) );
291bool QgsHistoryProviderRegistry::createDatabase(
const QString &filename, QString &error )
294 if ( !openDatabase( filename, error ) )
305bool QgsHistoryProviderRegistry::openDatabase(
const QString &filename, QString &error )
307 int rc = mLocalDB.
open( filename );
310 error = tr(
"Couldn't open the history database: %1" ).arg( mLocalDB.
errorMessage() );
317void QgsHistoryProviderRegistry::createTables()
320 "id INTEGER PRIMARY KEY,"\
323 "timestamp DATETIME);" \
324 "CREATE INDEX provider_index ON history(provider_id);"\
325 "CREATE INDEX timestamp_index ON history(timestamp);"
328 runEmptyQuery( query );
331bool QgsHistoryProviderRegistry::runEmptyQuery(
const QString &query )
336 char *zErr =
nullptr;
337 int nErr = sqlite3_exec( mLocalDB.get(), query.toUtf8().constData(),
nullptr,
nullptr, &zErr );
339 if ( nErr != SQLITE_OK )
342 sqlite3_free( zErr );
345 return nErr == SQLITE_OK;
HistoryProviderBackend
History provider backends.
@ LocalProfile
Local profile.
Abstract base class for objects which track user history (i.e.
virtual QString id() const =0
Returns the provider's unique id, which is used to associate existing history entries with the provid...
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
Encapsulates a history entry.
QDateTime timestamp
Entry timestamp.
QString providerId
Associated history provider ID.
QgsHistoryEntry(const QString &providerId, const QDateTime ×tamp, const QVariantMap &entry)
Constructor for QgsHistoryEntry entry, with the specified providerId and timestamp.
QVariantMap entry
Entry details.
Contains options for storing history entries.
Qgis::HistoryProviderBackends storageBackends
Target storage backends.
~QgsHistoryProviderRegistry() override
QgsAbstractHistoryProvider * providerById(const QString &id)
Returns the provider with matching id, or nullptr if no matching provider is registered.
bool updateEntry(long long id, const QVariantMap &entry, Qgis::HistoryProviderBackend backend=Qgis::HistoryProviderBackend::LocalProfile)
Updates the existing entry with matching id.
static QString userHistoryDbPath()
Returns the path to user's local history database.
bool addProvider(QgsAbstractHistoryProvider *provider)
Adds a provider to the registry.
bool addEntries(const QList< QgsHistoryEntry > &entries, QgsHistoryProviderRegistry::HistoryEntryOptions options=QgsHistoryProviderRegistry::HistoryEntryOptions())
Adds a list of entries to the history logs.
QgsHistoryProviderRegistry(QObject *parent=nullptr, bool useMemoryDatabase=false)
Creates a new empty history provider registry.
bool removeProvider(const QString &id)
Removes the provider with matching id.
long long addEntry(const QString &providerId, const QVariantMap &entry, bool &ok, QgsHistoryProviderRegistry::HistoryEntryOptions options=QgsHistoryProviderRegistry::HistoryEntryOptions())
Adds an entry to the history logs.
QgsHistoryEntry entry(long long id, bool &ok, Qgis::HistoryProviderBackend backend=Qgis::HistoryProviderBackend::LocalProfile) const
Returns the entry with matching ID, from the specified backend.
QList< QgsHistoryEntry > queryEntries(const QDateTime &start=QDateTime(), const QDateTime &end=QDateTime(), const QString &providerId=QString(), Qgis::HistoryProviderBackends backends=Qgis::HistoryProviderBackend::LocalProfile) const
Queries history entries which occurred between the specified start and end times.
bool clearHistory(Qgis::HistoryProviderBackend backend)
Clears the history for the specified backend.
void addDefaultProviders()
Adds the default history providers to the registry.
QStringList providerIds() const
Returns a list of the registered provider IDs.
History provider for operations performed through the Processing framework.
Scoped object for logging of the runtime for a single operation or group of operations.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
QString errorMessage() const
Returns the most recent error message encountered by the database.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
#define BUILTIN_UNREACHABLE
QString qgs_sqlite3_mprintf(const char *format,...)
Wraps sqlite3_mprintf() by automatically freeing the memory.