1 /**************************************************************************
2 *   Copyright (C) 2005-2020 by Oleksandr Shneyder                         *
3 *                              <o.shneyder@phoca-gmbh.de>                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program.  If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 
18 #include "x2gosettings.h"
19 #include "x2goclientconfig.h"
20 #include "x2gologdebug.h"
21 #include "onmainwindow.h"
22 #include <QTemporaryFile>
23 #include <QDir>
24 #include <QProcessEnvironment>
25 
X2goSettings(QString fileContent,QSettings::Format format)26 X2goSettings::X2goSettings(QString fileContent, QSettings::Format format)
27 {
28     cfgFile=new QTemporaryFile();
29     cfgFile->open();
30     QTextStream out(cfgFile);
31     out<<fileContent;
32     cfgFile->close();
33     set=new QSettings ( cfgFile->fileName(),
34                         format );
35 }
36 
37 
X2goSettings(QString group)38 X2goSettings::X2goSettings ( QString group )
39 {
40     cfgFile=0l;
41     if (group=="sessions" && ONMainWindow::getSessionConf().length()>0)
42     {
43         set=new QSettings ( ONMainWindow::getSessionConf(),
44                             QSettings::IniFormat );
45         return;
46     }
47 #ifndef Q_OS_WIN
48     if(!centralSettings())
49     {
50         set=new QSettings ( ONMainWindow::getHomeDirectory() +
51                            "/.x2goclient/"+group,
52                           QSettings::NativeFormat );
53     }
54     else
55     {
56         QString settingPath="/etc/x2goclient/config/"+qgetenv("USER")+"/";
57         QDir d(settingPath);
58         if(!d.exists())
59         {
60             settingPath="/etc/x2goclient/config/All Users/";
61         }
62 //         x2goErrorf(99)<<"CFG PATH:"<<settingPath;
63         set=new QSettings ( settingPath+group,
64                           QSettings::NativeFormat );
65     }
66 #else
67     if ( !ONMainWindow::getPortable() )
68     {
69         if(! centralSettings())
70 	{
71             set=new QSettings ( "Obviously Nice","x2goclient" );
72             set->beginGroup ( group );
73 	}
74 	else
75 	{
76 	    QSettings setroot("HKEY_LOCAL_MACHINE\\SOFTWARE\\x2goclient\\config",QSettings::NativeFormat);
77 	    QString setPath="HKEY_LOCAL_MACHINE\\SOFTWARE\\x2goclient\\config\\All Users";
78 	    QString uname=getenv("USERNAME");
79             foreach(QString group, setroot.childGroups())
80 	    {
81 	        if(group==uname)
82 		{
83 	             setPath="HKEY_LOCAL_MACHINE\\SOFTWARE\\x2goclient\\config\\"+uname;
84 		     break;
85 		}
86             }
87             set=new QSettings ( setPath, QSettings::NativeFormat);
88             set->beginGroup ( group );
89 	}
90     }
91     else
92     {
93         set=new QSettings ( ONMainWindow::getHomeDirectory() +
94                             "/.x2goclient/"+group,
95                             QSettings::IniFormat );
96     }
97 #endif
98 
99 }
100 
101 
~X2goSettings()102 X2goSettings::~X2goSettings()
103 {
104     delete set;
105     if (cfgFile)
106         delete cfgFile;
107 }
108 
centralSettings()109 bool X2goSettings::centralSettings()
110 {
111 #ifndef Q_OS_WIN
112     QDir d("/etc/x2goclient/config");
113     return d.exists();
114 #else
115     QSettings set("HKEY_LOCAL_MACHINE\\SOFTWARE\\x2goclient",QSettings::NativeFormat);
116     foreach(QString group,set.childGroups())
117     {
118       if(group=="config")
119       {
120 	return true;
121       }
122     }
123     return false;
124 #endif
125 }
126