QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 <QTextStream>
22 #include <QSettings>
23 #include <sqlite3.h>
24 
25 QgsUserProfile::QgsUserProfile( const QString &folder )
26 {
27  mProfileFolder = folder;
28 }
29 
30 const QString QgsUserProfile::folder() const
31 {
32  return mProfileFolder;
33 }
34 
36 {
37  QgsError error;
38  if ( !QDir( mProfileFolder ).exists() )
39  {
40  error.append( QObject::tr( "Profile folder doesn't exist" ) );
41  }
42  return error;
43 }
44 
45 const QString QgsUserProfile::name() const
46 {
47  const QDir dir( mProfileFolder );
48  return dir.dirName();
49 }
50 
52 {
53  // tell QSettings to use INI format and save the file in custom config path
54  QSettings::setDefaultFormat( QSettings::IniFormat );
55  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, folder() );
56 }
57 
58 const QString QgsUserProfile::alias() const
59 {
60  const QString dbFile = qgisDB();
61  QString profileAlias = name();
62 
63  // Looks for qgis.db
64  // If it's not there we can just return name.
65  if ( !QFile::exists( dbFile ) )
66  {
67  return profileAlias;
68  }
69 
71 
72  //check the db is available
73  int result = database.open( dbFile );
74  if ( result != SQLITE_OK )
75  {
76  return profileAlias;
77  }
78 
79  sqlite3_statement_unique_ptr preparedStatement = database.prepare( QStringLiteral( "SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'" ), result );
80  if ( result == SQLITE_OK )
81  {
82  if ( preparedStatement.step() == SQLITE_ROW )
83  {
84  const QString alias = preparedStatement.columnAsText( 0 );
85  if ( !alias.isEmpty() )
86  profileAlias = alias;
87  }
88  }
89  return profileAlias;
90 }
91 
92 QgsError QgsUserProfile::setAlias( const QString &alias ) const
93 {
94  QgsError error;
95  const QString dbFile = qgisDB();
96 
97  // Looks for qgis.db
98  // If it's not there we can just return name.
99  if ( !QFile::exists( dbFile ) )
100  {
101  error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
102  return error;
103  }
104 
106 
107  //check the db is available
108  int result = database.open( dbFile );
109  if ( result != SQLITE_OK )
110  {
111  error.append( QObject::tr( "Unable to open qgis.db for update." ) );
112  return error;
113  }
114 
115  const QString sql = QStringLiteral( "INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', %1);" ).arg(
117 
118  sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
119  if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
120  {
121  error.append( QObject::tr( "Could not save alias to database: %1" ).arg( database.errorMessage() ) );
122  }
123 
124  return error;
125 }
126 
127 const QIcon QgsUserProfile::icon() const
128 {
129  const QString path = mProfileFolder + QDir::separator() + "icon.svg";
130  if ( !QDir( path ).exists() )
131  {
132  return QgsApplication::getThemeIcon( "user.svg" );
133  }
134  return QIcon( path );
135 }
136 
137 QString QgsUserProfile::qgisDB() const
138 {
139  return mProfileFolder + QDir::separator() + "qgis.db";
140 }
QgsUserProfile::name
const QString name() const
The name for the user profile.
Definition: qgsuserprofile.cpp:45
sqlite3_database_unique_ptr::open
int open(const QString &path)
Opens the database at the specified file path.
Definition: qgssqliteutils.cpp:78
QgsUserProfile::folder
const QString folder() const
The base folder for the user profile.
Definition: qgsuserprofile.cpp:30
sqlite3_database_unique_ptr::prepare
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
Definition: qgssqliteutils.cpp:99
qgsuserprofile.h
QgsError
QgsError is container for error messages (report). It may contain chain (sort of traceback) of error ...
Definition: qgserror.h:80
QgsSqliteUtils::quotedString
static QString quotedString(const QString &value)
Returns a quoted string value, surround by ' characters and with special characters correctly escaped...
Definition: qgssqliteutils.cpp:249
QgsUserProfile::setAlias
QgsError setAlias(const QString &alias) const
Set the alias of the profile.
Definition: qgsuserprofile.cpp:92
QgsError::append
void append(const QString &message, const QString &tag)
Append new error message.
Definition: qgserror.cpp:39
sqlite3_database_unique_ptr::errorMessage
QString errorMessage() const
Returns the most recent error message encountered by the database.
Definition: qgssqliteutils.cpp:94
sqlite3_statement_unique_ptr::step
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
Definition: qgssqliteutils.cpp:41
qgsapplication.h
QgsUserProfile::icon
const QIcon icon() const
The icon for the user profile.
Definition: qgsuserprofile.cpp:127
QgsUserProfile::initSettings
void initSettings() const
Init the settings from the user folder.
Definition: qgsuserprofile.cpp:51
sqlite3_statement_unique_ptr::columnAsText
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
Definition: qgssqliteutils.cpp:61
QgsUserProfile::QgsUserProfile
QgsUserProfile(const QString &folder)
Reference to a existing user profile folder.
Definition: qgsuserprofile.cpp:25
QgsUserProfile::alias
const QString alias() const
Returns the alias for the user profile.
Definition: qgsuserprofile.cpp:58
QgsUserProfile::validate
QgsError validate() const
Check of the profile is in a valid state.
Definition: qgsuserprofile.cpp:35
qgssqliteutils.h
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
sqlite3_database_unique_ptr
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
Definition: qgssqliteutils.h:118
sqlite3_statement_unique_ptr
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
Definition: qgssqliteutils.h:69