1 /* Copyright 2005 Guillaume Duhamel
2 Copyright 2005-2006 Theo Berkau
3 Copyright 2008 Filipe Azevedo <pasnox@gmail.com>
4
5 This file is part of Yabause.
6
7 Yabause is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Yabause is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Yabause; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #include "Settings.h"
22
23 #include <QApplication>
24 #include <QDir>
25 #include <QFile>
26 #include <QMainWindow>
27 #include <QDesktopServices>
28
29 QString Settings::mProgramName;
30 QString Settings::mProgramVersion;
31
getDataDirPath()32 QString getDataDirPath()
33 {
34 #if defined Q_OS_WIN
35 // Use some wizardry so we can get our data in AppData
36 QString oldApplicationName = QCoreApplication::applicationName();
37 QCoreApplication::setApplicationName("yabause");
38 #if QT_VERSION >= 0x04FF00
39 QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
40 #else
41 QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
42 #endif
43 QCoreApplication::setApplicationName(oldApplicationName);
44 return path;
45 #elif defined Q_OS_LINUX
46 QString xdgpath = QString( "%1/.local/share/yabause" ).arg( QDir::homePath() );
47 if ( ! QFile::exists( xdgpath ) )
48 {
49 QDir dir;
50 dir.mkpath( xdgpath );
51 }
52 return xdgpath;
53 #else
54 return QApplication::applicationDirPath();
55 #endif
56 }
57
getIniFile(const QString & s)58 QString getIniFile( const QString& s )
59 {
60 #if defined Q_OS_MAC
61 return QString( "%1/../%2.ini" ).arg( QApplication::applicationDirPath() ).arg( s );
62 #elif defined Q_OS_WIN
63 /* We used to store the ini file in the application directory, before moving to the
64 correct location, but some users like it better the old way... so if we find a .ini
65 file in the application directory, we're using it */
66 QString oldinifile = QString( "%1/%2.ini" ).arg( QApplication::applicationDirPath() ).arg( s );
67 if ( QFile::exists( oldinifile )) return oldinifile;
68
69 return QString( "%1/%2.ini" ).arg( getDataDirPath() ).arg(s);
70 #else
71 /*
72 We used to store the ini file in ~/.$SOMETHING/$SOMETHING.ini, were $SOMETHING could
73 be at least yabause or yabause-qt
74 With release 0.9.12 we moved to the XDG compliant location ~/.config/yabause/qt/yabause.ini
75 and we don't want this location to depends on the program name anymore.
76 This code is trying to copy the content from the old location to the new.
77 In the future, we may drop support for the old location and rewrite the following to:
78
79 return QString( "%1/.config/yabause/qt/yabause.ini" ).arg( QDir::homePath() );
80 */
81
82 QString xdginifile = QString( "%1/.config/yabause/qt/yabause.ini" ).arg( QDir::homePath() );
83 QString oldinifile = QString( "%1/.%2/%2.ini" ).arg( QDir::homePath() ).arg( s );
84
85 if ( not QFile::exists( xdginifile ) )
86 {
87 QString xdgpath = QString( "%1/.config/yabause/qt" ).arg( QDir::homePath() );
88 if ( ! QFile::exists( xdgpath ) )
89 {
90 // for some reason, Qt doesn't provide a static mkpath method O_o
91 QDir dir;
92 dir.mkpath( xdgpath );
93 }
94
95 if ( QFile::exists( oldinifile ) )
96 QFile::copy( oldinifile, xdginifile );
97 }
98
99 return xdginifile;
100 #endif
101 }
102
Settings(QObject * o)103 Settings::Settings( QObject* o )
104 : QSettings( QDir::toNativeSeparators( getIniFile( mProgramName ) ), QSettings::IniFormat, o )
105 {
106 /*
107 This used to be "beginGroup( mProgramVersion );" so users would lose their
108 config with each new release...
109 */
110 beginGroup( "0.9.11" );
111 }
112
~Settings()113 Settings::~Settings()
114 { endGroup(); }
115
setIniInformations(const QString & pName,const QString & pVersion)116 void Settings::setIniInformations( const QString& pName, const QString& pVersion )
117 {
118 mProgramName = pName;
119 mProgramVersion = pVersion;
120 }
121
programName()122 QString Settings::programName()
123 { return mProgramName; }
124
programVersion()125 QString Settings::programVersion()
126 { return mProgramVersion; }
127
restoreState(QMainWindow * w)128 void Settings::restoreState( QMainWindow* w )
129 {
130 if ( !w )
131 return;
132 w->restoreState( value( "MainWindow/State" ).toByteArray() );
133 QPoint p = value( "MainWindow/Position" ).toPoint();
134 QSize s = value( "MainWindow/Size" ).toSize();
135 if ( !p.isNull() && !s.isNull() )
136 {
137 w->resize( s );
138 w->move( p );
139 }
140 if ( value( "MainWindow/Maximized", true ).toBool() )
141 w->showMaximized();
142 }
143
saveState(QMainWindow * w)144 void Settings::saveState( QMainWindow* w )
145 {
146 if ( !w )
147 return;
148 setValue( "MainWindow/Maximized", w->isMaximized() );
149 setValue( "MainWindow/Position", w->pos() );
150 setValue( "MainWindow/Size", w->size() );
151 setValue( "MainWindow/State", w->saveState() );
152 }
153
setDefaultSettings()154 void Settings::setDefaultSettings()
155 {
156 }
157