QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  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  QString alias = preparedStatement.columnAsText( 0 );
85  if ( !alias.isEmpty() )
86  profileAlias = alias;
87  }
88  }
89  return profileAlias;
90 }
91 
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(
116  QgsSqliteUtils::quotedString( alias ) );
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  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 }
const QIcon icon() const
The icon for the user profile.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QgsError setAlias(const QString &alias)
Set the alias of the profile.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsError validate() const
Check of the profile is in a valid state.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
void append(const QString &message, const QString &tag)
Append new error message.
Definition: qgserror.cpp:39
int open(const QString &path)
Opens the database at the specified file path.
const QString folder() const
The base folder for the user profile.
const QString name() const
The name for the user profile.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
void initSettings() const
Init the settings from the user folder.
QgsError is container for error messages (report).
Definition: qgserror.h:80
const QString alias() const
Returns the alias for the user profile.
QgsUserProfile(const QString &folder)
Reference to a existing user profile folder.
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
QString errorMessage() const
Returns the most recent error message encountered by the database.
static QString quotedString(const QString &value)
Returns a quoted string value, surround by &#39; characters and with special characters correctly escaped...