QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "qgsapplication.h"
18#include "qgssqliteutils.h"
19
20#include <QDir>
21#include <QFileInfo>
22#include <QTextStream>
23#include <QSettings>
24#include <sqlite3.h>
25
26QgsUserProfile::QgsUserProfile( const QString &folder )
27{
28 mProfileFolder = folder;
29}
30
31const QString QgsUserProfile::folder() const
32{
33 return mProfileFolder;
34}
35
37{
38 QgsError error;
39 if ( !QDir( mProfileFolder ).exists() )
40 {
41 error.append( QObject::tr( "Profile folder doesn't exist" ) );
42 }
43 return error;
44}
45
46const QString QgsUserProfile::name() const
47{
48 const QDir dir( mProfileFolder );
49 return dir.dirName();
50}
51
53{
54 // tell QSettings to use INI format and save the file in custom config path
55 QSettings::setDefaultFormat( QSettings::IniFormat );
56 QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, folder() );
57}
58
59const QString QgsUserProfile::alias() const
60{
61 const QString dbFile = qgisDB();
62 QString profileAlias = name();
63
64 // Looks for qgis.db
65 // If it's not there we can just return name.
66 if ( !QFile::exists( dbFile ) )
67 {
68 return profileAlias;
69 }
70
72
73 //check the db is available
74 int result = database.open( dbFile );
75 if ( result != SQLITE_OK )
76 {
77 return profileAlias;
78 }
79
80 sqlite3_statement_unique_ptr preparedStatement = database.prepare( QStringLiteral( "SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'" ), result );
81 if ( result == SQLITE_OK )
82 {
83 if ( preparedStatement.step() == SQLITE_ROW )
84 {
85 const QString alias = preparedStatement.columnAsText( 0 );
86 if ( !alias.isEmpty() )
87 profileAlias = alias;
88 }
89 }
90 return profileAlias;
91}
92
93QgsError QgsUserProfile::setAlias( const QString &alias ) const
94{
95 QgsError error;
96 const QString dbFile = qgisDB();
97
98 // Looks for qgis.db
99 // If it's not there we can just return name.
100 if ( !QFile::exists( dbFile ) )
101 {
102 error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
103 return error;
104 }
105
107
108 //check the db is available
109 int result = database.open( dbFile );
110 if ( result != SQLITE_OK )
111 {
112 error.append( QObject::tr( "Unable to open qgis.db for update." ) );
113 return error;
114 }
115
116 const QString sql = QStringLiteral( "INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', %1);" ).arg(
118
119 sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
120 if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
121 {
122 error.append( QObject::tr( "Could not save alias to database: %1" ).arg( database.errorMessage() ) );
123 }
124
125 return error;
126}
127
128const QIcon QgsUserProfile::icon() const
129{
130 const QStringList extensions = {".svg", ".png", ".jpg", ".jpeg", ".gif", ".bmp"};
131 const QString basename = mProfileFolder + QDir::separator() + "icon";
132
133 for ( const QString &extension : extensions )
134 {
135 const QString path = basename + extension;
136 if ( QFileInfo::exists( path ) )
137 return QIcon( path );
138 }
139 return QgsApplication::getThemeIcon( "user.svg" );
140}
141
142QString QgsUserProfile::qgisDB() const
143{
144 return mProfileFolder + QDir::separator() + "qgis.db";
145}
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:81
void append(const QString &message, const QString &tag)
Append new error message.
Definition qgserror.cpp:39
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 a 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.