QGIS API Documentation 4.1.0-Master (376402f9aeb)
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 "qgssettingsentry.h"
22#include "qgssqliteutils.h"
23
24#include <QDir>
25#include <QFileInfo>
26#include <QSettings>
27#include <QString>
28#include <QTextStream>
29
30using namespace Qt::StringLiterals;
31
33{
34 mProfileFolder = folder;
35}
36
37const QString QgsUserProfile::folder() const
38{
39 return mProfileFolder;
40}
41
43{
44 QgsError error;
45 if ( !QDir( mProfileFolder ).exists() )
46 {
47 error.append( QObject::tr( "Profile folder doesn't exist" ) );
48 }
49 return error;
50}
51
52const QString QgsUserProfile::name() const
53{
54 const QDir dir( mProfileFolder );
55 return dir.dirName();
56}
57
59{
60#ifndef __EMSCRIPTEN__
62#endif
63}
64
65const QString QgsUserProfile::alias() const
66{
67 const QString dbFile = qgisDB();
68 QString profileAlias = name();
69
70 // Looks for qgis.db
71 // If it's not there we can just return name.
72 if ( !QFile::exists( dbFile ) )
73 {
74 return profileAlias;
75 }
76
78
79 //check the db is available
80 int result = database.open( dbFile );
81 if ( result != SQLITE_OK )
82 {
83 return profileAlias;
84 }
85
86 sqlite3_statement_unique_ptr preparedStatement = database.prepare( u"SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'"_s, result );
87 if ( result == SQLITE_OK )
88 {
89 if ( preparedStatement.step() == SQLITE_ROW )
90 {
91 const QString alias = preparedStatement.columnAsText( 0 );
92 if ( !alias.isEmpty() )
93 profileAlias = alias;
94 }
95 }
96 return profileAlias;
97}
98
100{
101 QgsError error;
102 const QString dbFile = qgisDB();
103
104 // Looks for qgis.db
105 // If it's not there we can just return name.
106 if ( !QFile::exists( dbFile ) )
107 {
108 error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
109 return error;
110 }
111
113
114 //check the db is available
115 int result = database.open( dbFile );
116 if ( result != SQLITE_OK )
117 {
118 error.append( QObject::tr( "Unable to open qgis.db for update." ) );
119 return error;
120 }
121
122 const QString sql = u"INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', %1);"_s.arg( QgsSqliteUtils::quotedString( alias ) );
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:42
static void setupUserSettings(const QString &profilePath)
Configures QSettings to use IniFormat at the given profilePath so that each thread's QSettings instan...
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.