2017-07-02 15:02:07 +03:00
/*
* Copyright 2017 Smith AR < audoban @ openmailbox . org >
* Michail Vourlakos < mvourlakos @ gmail . com >
*
* This file is part of Latte - Dock
*
* Latte - Dock is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License , or ( at your option ) any later version .
*
* Latte - Dock is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "universalsettings.h"
2017-07-22 23:21:34 +03:00
# include <QDir>
2017-07-02 15:02:07 +03:00
namespace Latte {
2017-07-02 16:12:58 +03:00
UniversalSettings : : UniversalSettings ( KSharedConfig : : Ptr config , QObject * parent )
: QObject ( parent ) ,
2017-07-02 21:02:27 +03:00
m_config ( config ) ,
2017-07-02 16:12:58 +03:00
m_universalGroup ( KConfigGroup ( config , QStringLiteral ( " UniversalSettings " ) ) )
2017-07-02 15:02:07 +03:00
{
2017-07-03 10:41:59 +03:00
connect ( this , & UniversalSettings : : currentLayoutNameChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-23 21:05:34 +03:00
connect ( this , & UniversalSettings : : lastNonAssignedLayoutNameChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-24 17:56:07 +03:00
connect ( this , & UniversalSettings : : launchersChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-02 20:19:18 +03:00
connect ( this , & UniversalSettings : : versionChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-02 15:02:07 +03:00
}
UniversalSettings : : ~ UniversalSettings ( )
{
2017-07-02 20:19:18 +03:00
saveConfig ( ) ;
2017-07-02 21:02:27 +03:00
cleanupSettings ( ) ;
2017-07-02 15:02:07 +03:00
}
2017-07-02 20:19:18 +03:00
void UniversalSettings : : load ( )
{
2017-07-04 15:05:54 +03:00
//! check if user has set the autostart option
bool autostartUserSet = m_universalGroup . readEntry ( " userConfiguredAutostart " , false ) ;
if ( ! autostartUserSet & & ! autostart ( ) ) {
setAutostart ( true ) ;
}
//! load configuration
2017-07-02 20:19:18 +03:00
loadConfig ( ) ;
}
int UniversalSettings : : version ( ) const
{
return m_version ;
}
void UniversalSettings : : setVersion ( int ver )
{
if ( m_version = = ver ) {
return ;
}
m_version = ver ;
emit versionChanged ( ) ;
}
2017-07-03 10:41:59 +03:00
QString UniversalSettings : : currentLayoutName ( ) const
{
return m_currentLayoutName ;
}
void UniversalSettings : : setCurrentLayoutName ( QString layoutName )
{
if ( m_currentLayoutName = = layoutName ) {
return ;
}
m_currentLayoutName = layoutName ;
emit currentLayoutNameChanged ( ) ;
}
2017-07-23 21:05:34 +03:00
QString UniversalSettings : : lastNonAssignedLayoutName ( ) const
{
return m_lastNonAssignedLayoutName ;
}
void UniversalSettings : : setLastNonAssignedLayoutName ( QString layoutName )
{
if ( m_lastNonAssignedLayoutName = = layoutName ) {
return ;
}
m_lastNonAssignedLayoutName = layoutName ;
emit lastNonAssignedLayoutNameChanged ( ) ;
}
2017-07-19 21:36:05 +03:00
QSize UniversalSettings : : layoutsWindowSize ( ) const
{
return m_layoutsWindowSize ;
}
void UniversalSettings : : setLayoutsWindowSize ( QSize size )
{
if ( m_layoutsWindowSize = = size ) {
return ;
}
m_layoutsWindowSize = size ;
emit layoutsWindowSizeChanged ( ) ;
}
2017-07-24 17:56:07 +03:00
QStringList UniversalSettings : : launchers ( ) const
{
return m_launchers ;
}
void UniversalSettings : : setLaunchers ( QStringList launcherList )
{
if ( m_launchers = = launcherList ) {
return ;
}
m_launchers = launcherList ;
emit launchersChanged ( ) ;
}
2017-07-04 15:05:54 +03:00
bool UniversalSettings : : autostart ( ) const
{
QFile autostartFile ( QDir : : homePath ( ) + " /.config/autostart/org.kde.latte-dock.desktop " ) ;
return autostartFile . exists ( ) ;
}
void UniversalSettings : : setAutostart ( bool state )
{
//! remove old autostart file
QFile oldAutostartFile ( QDir : : homePath ( ) + " /.config/autostart/latte-dock.desktop " ) ;
if ( oldAutostartFile . exists ( ) ) {
oldAutostartFile . remove ( ) ;
}
//! end of removal of old autostart file
QFile autostartFile ( QDir : : homePath ( ) + " /.config/autostart/org.kde.latte-dock.desktop " ) ;
QFile metaFile ( " /usr/share/applications/org.kde.latte-dock.desktop " ) ;
if ( ! state & & autostartFile . exists ( ) ) {
//! the first time that the user disables the autostart, this is recorded
//! and from now own it will not be recreated it in the beginning
if ( ! m_universalGroup . readEntry ( " userConfiguredAutostart " , false ) ) {
m_universalGroup . writeEntry ( " userConfiguredAutostart " , true ) ;
}
autostartFile . remove ( ) ;
emit autostartChanged ( ) ;
} else if ( state & & metaFile . exists ( ) ) {
metaFile . copy ( autostartFile . fileName ( ) ) ;
//! I havent added the flag "OnlyShowIn=KDE;" into the autostart file
//! because I fall onto a Plasma 5.8 case that this flag
//! didnt let the plasma desktop to start
emit autostartChanged ( ) ;
}
}
2017-07-02 20:19:18 +03:00
void UniversalSettings : : loadConfig ( )
{
m_version = m_universalGroup . readEntry ( " version " , 1 ) ;
2017-07-03 10:41:59 +03:00
m_currentLayoutName = m_universalGroup . readEntry ( " currentLayout " , QString ( ) ) ;
2017-07-23 21:05:34 +03:00
m_lastNonAssignedLayoutName = m_universalGroup . readEntry ( " lastNonAssignedLayout " , QString ( ) ) ;
2017-07-19 21:36:05 +03:00
m_layoutsWindowSize = m_universalGroup . readEntry ( " layoutsWindowSize " , QSize ( 700 , 450 ) ) ;
2017-07-24 17:56:07 +03:00
m_launchers = m_universalGroup . readEntry ( " launchers " , QStringList ( ) ) ;
2017-07-02 20:19:18 +03:00
}
void UniversalSettings : : saveConfig ( )
{
m_universalGroup . writeEntry ( " version " , m_version ) ;
2017-07-03 10:41:59 +03:00
m_universalGroup . writeEntry ( " currentLayout " , m_currentLayoutName ) ;
2017-07-23 21:05:34 +03:00
m_universalGroup . writeEntry ( " lastNonAssignedLayout " , m_lastNonAssignedLayoutName ) ;
2017-07-19 21:36:05 +03:00
m_universalGroup . writeEntry ( " layoutsWindowSize " , m_layoutsWindowSize ) ;
2017-07-24 17:56:07 +03:00
m_universalGroup . writeEntry ( " launchers " , m_launchers ) ;
2017-07-02 21:02:27 +03:00
m_universalGroup . sync ( ) ;
2017-07-02 20:19:18 +03:00
}
2017-07-02 21:02:27 +03:00
void UniversalSettings : : cleanupSettings ( )
{
KConfigGroup containments = KConfigGroup ( m_config , QStringLiteral ( " Containments " ) ) ;
containments . deleteGroup ( ) ;
containments . sync ( ) ;
}
2017-07-02 20:19:18 +03:00
2017-07-02 15:02:07 +03:00
}