QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
19 #include <QDir>
20 #include <QTextStream>
21 #include <QSettings>
22 #include <QSqlDatabase>
23 #include <QSqlQuery>
24 #include <QSqlError>
25 
26 QgsUserProfile::QgsUserProfile( const QString &folder )
27 {
28  mProfileFolder = folder;
29 }
30 
31 const 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 
46 const QString QgsUserProfile::name() const
47 {
48  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 
59 const QString QgsUserProfile::alias() const
60 {
61  QFile qgisPrivateDbFile( qgisDB() );
62 
63  // Looks for qgis.db
64  // If it's not there we can just return name.
65  if ( !qgisPrivateDbFile.exists() )
66  {
67  return name();
68  }
69 
70  QSqlDatabase db = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), QStringLiteral( "userprofile" ) );
71  db.setDatabaseName( qgisDB() );
72  if ( !db.open() )
73  return name();
74 
75  QSqlQuery query;
76  query.prepare( QStringLiteral( "SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'" ) );
77  QString profileAlias = name();
78  if ( query.exec() )
79  {
80  if ( query.next() )
81  {
82  QString alias = query.value( 0 ).toString();
83  if ( !alias.isEmpty() )
84  profileAlias = alias;
85  }
86  }
87  db.close();
88  return profileAlias;
89 }
90 
92 {
93  QgsError error;
94  QFile qgisPrivateDbFile( qgisDB() );
95 
96  if ( !qgisPrivateDbFile.exists() )
97  {
98  error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
99  return error;
100  }
101 
102  QSqlDatabase db = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), QStringLiteral( "userprofile" ) );
103  db.setDatabaseName( qgisDB() );
104  if ( !db.open() )
105  {
106  error.append( QObject::tr( "Unable to open qgis.db for update." ) );
107  return error;
108  }
109 
110  QSqlQuery query;
111  QString sql = QStringLiteral( "INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', :alias);" );
112  query.prepare( sql );
113  query.bindValue( QStringLiteral( ":alias" ), alias );
114  if ( !query.exec() )
115  {
116  error.append( QObject::tr( "Could not save alias to database: %1" ).arg( query.lastError().text() ) );
117  }
118  db.close();
119  return error;
120 }
121 
122 const QIcon QgsUserProfile::icon() const
123 {
124  QString path = mProfileFolder + QDir::separator() + "icon.svg";
125  if ( !QDir( path ).exists() )
126  {
127  return QgsApplication::getThemeIcon( "user.svg" );
128  }
129  return QIcon( path );
130 }
131 
132 QString QgsUserProfile::qgisDB() const
133 {
134  return mProfileFolder + QDir::separator() + "qgis.db";
135 }
const QString name() const
The name for the user profile.
QgsError setAlias(const QString &alias)
Set the alias of the profile.
QgsError validate() const
Check of the profile is in a valid state.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void append(const QString &message, const QString &tag)
Append new error message.
Definition: qgserror.cpp:39
const QIcon icon() const
The icon for the user profile.
QgsError is container for error messages (report).
Definition: qgserror.h:80
QgsUserProfile(const QString &folder)
Reference to a existing user profile folder.
const QString folder() const
The base folder for the user profile.
const QString alias() const
Returns the alias for the user profile.
void initSettings() const
Init the settings from the user folder.