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