QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsuserprofile.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsuserprofile.h
3 --------------------------------------
4 Date : Jul-2017
5 Copyright : (C) 2017 by Nathan Woodrow
6 Email : woodrow.nathan at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsuserprofile.h"
17
18#include <sqlite3.h>
19
20#include "qgsapplication.h"
21#include "qgssqliteutils.h"
22
23#include <QDir>
24#include <QFileInfo>
25#include <QSettings>
26#include <QString>
27#include <QTextStream>
28
29using namespace Qt::StringLiterals;
30
32{
33 mProfileFolder = folder;
34}
35
36const QString QgsUserProfile::folder() const
37{
38 return mProfileFolder;
39}
40
42{
43 QgsError error;
44 if ( !QDir( mProfileFolder ).exists() )
45 {
46 error.append( QObject::tr( "Profile folder doesn't exist" ) );
47 }
48 return error;
49}
50
51const QString QgsUserProfile::name() const
52{
53 const QDir dir( mProfileFolder );
54 return dir.dirName();
55}
56
58{
59 // tell QSettings to use INI format and save the file in custom config path
60 QSettings::setDefaultFormat( QSettings::IniFormat );
61 QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, folder() );
62}
63
64const QString QgsUserProfile::alias() const
65{
66 const QString dbFile = qgisDB();
67 QString profileAlias = name();
68
69 // Looks for qgis.db
70 // If it's not there we can just return name.
71 if ( !QFile::exists( dbFile ) )
72 {
73 return profileAlias;
74 }
75
77
78 //check the db is available
79 int result = database.open( dbFile );
80 if ( result != SQLITE_OK )
81 {
82 return profileAlias;
83 }
84
85 sqlite3_statement_unique_ptr preparedStatement = database.prepare( u"SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'"_s, result );
86 if ( result == SQLITE_OK )
87 {
88 if ( preparedStatement.step() == SQLITE_ROW )
89 {
90 const QString alias = preparedStatement.columnAsText( 0 );
91 if ( !alias.isEmpty() )
92 profileAlias = alias;
93 }
94 }
95 return profileAlias;
96}
97
99{
100 QgsError error;
101 const QString dbFile = qgisDB();
102
103 // Looks for qgis.db
104 // If it's not there we can just return name.
105 if ( !QFile::exists( dbFile ) )
106 {
107 error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
108 return error;
109 }
110
112
113 //check the db is available
114 int result = database.open( dbFile );
115 if ( result != SQLITE_OK )
116 {
117 error.append( QObject::tr( "Unable to open qgis.db for update." ) );
118 return error;
119 }
120
121 const QString sql = u"INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', %1);"_s.arg(
123
124 sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
125 if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
126 {
127 error.append( QObject::tr( "Could not save alias to database: %1" ).arg( database.errorMessage() ) );
128 }
129
130 return error;
131}
132
133const QIcon QgsUserProfile::icon() const
134{
135 const QStringList extensions = {".svg", ".png", ".jpg", ".jpeg", ".gif", ".bmp"};
136 const QString basename = mProfileFolder + QDir::separator() + "icon";
137
138 for ( const QString &extension : extensions )
139 {
140 const QString path = basename + extension;
141 if ( QFileInfo::exists( path ) )
142 return QIcon( path );
143 }
144 return QgsApplication::getThemeIcon( "user.svg" );
145}
146
147QString QgsUserProfile::qgisDB() const
148{
149 return mProfileFolder + QDir::separator() + "qgis.db";
150}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A container for error messages.
Definition qgserror.h:83
void append(const QString &message, const QString &tag)
Append new error message.
Definition qgserror.cpp:43
static QString quotedString(const QString &value)
Returns a quoted string value, surround by ' characters and with special characters correctly escaped...
QgsUserProfile(const QString &folder)
Reference to an existing user profile folder.
const QString folder() const
The base folder for the user profile.
const QString name() const
The name for the user profile.
QgsError setAlias(const QString &alias) const
Set the alias of the profile.
const QString alias() const
Returns the alias for the user profile.
const QIcon icon() const
The icon for the user profile.
QgsError validate() const
Check of the profile is in a valid state.
void initSettings() const
Init the settings from the user folder.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
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.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.