2017-07-02 16:12:58 +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/>.
*/
2018-01-07 19:33:18 +02:00
# include "layout.h"
2017-07-02 16:12:58 +03:00
# include <QFile>
# include <KSharedConfig>
namespace Latte {
2018-01-07 19:33:18 +02:00
Layout : : Layout ( QObject * parent , QString layoutFile , QString assignedName )
2017-07-02 16:12:58 +03:00
: QObject ( parent )
{
2017-07-22 10:39:35 +03:00
qDebug ( ) < < " Layout file to create object: " < < layoutFile < < " with name: " < < assignedName ;
2017-07-03 21:33:27 +03:00
2017-07-02 20:19:18 +03:00
if ( QFile ( layoutFile ) . exists ( ) ) {
2017-07-22 10:39:35 +03:00
if ( assignedName . isEmpty ( ) ) {
assignedName = layoutName ( layoutFile ) ;
2017-07-03 19:25:16 +03:00
}
2017-07-02 16:12:58 +03:00
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( layoutFile ) ;
m_layoutGroup = KConfigGroup ( lConfig , " LayoutSettings " ) ;
2017-07-03 19:25:16 +03:00
setFile ( layoutFile ) ;
2017-07-22 10:39:35 +03:00
setName ( assignedName ) ;
2017-07-03 19:50:42 +03:00
loadConfig ( ) ;
2017-07-02 20:19:18 +03:00
init ( ) ;
2017-07-02 16:12:58 +03:00
}
}
2018-01-07 19:33:18 +02:00
Layout : : ~ Layout ( )
2017-07-02 16:12:58 +03:00
{
2017-07-17 22:07:04 +03:00
if ( ! m_layoutFile . isEmpty ( ) ) {
2017-07-20 11:27:22 +03:00
//saveConfig();
2017-07-17 22:07:04 +03:00
m_layoutGroup . sync ( ) ;
}
2017-07-02 16:12:58 +03:00
}
2018-01-07 19:33:18 +02:00
void Layout : : init ( )
2017-07-02 20:19:18 +03:00
{
2018-01-07 19:33:18 +02:00
connect ( this , & Layout : : activitiesChanged , this , & Layout : : saveConfig ) ;
connect ( this , & Layout : : versionChanged , this , & Layout : : saveConfig ) ;
connect ( this , & Layout : : colorChanged , this , & Layout : : saveConfig ) ;
connect ( this , & Layout : : showInMenuChanged , this , & Layout : : saveConfig ) ;
connect ( this , & Layout : : launchersChanged , this , & Layout : : saveConfig ) ;
2017-07-02 20:19:18 +03:00
}
2018-01-07 19:33:18 +02:00
int Layout : : version ( ) const
2017-07-02 20:19:18 +03:00
{
return m_version ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setVersion ( int ver )
2017-07-02 20:19:18 +03:00
{
if ( m_version = = ver ) {
return ;
}
m_version = ver ;
emit versionChanged ( ) ;
}
2018-01-07 19:33:18 +02:00
bool Layout : : showInMenu ( ) const
2017-07-17 18:31:43 +03:00
{
return m_showInMenu ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setShowInMenu ( bool show )
2017-07-17 18:31:43 +03:00
{
if ( m_showInMenu = = show ) {
return ;
}
m_showInMenu = show ;
emit showInMenuChanged ( ) ;
}
2018-01-07 19:33:18 +02:00
QString Layout : : name ( ) const
2017-07-03 19:25:16 +03:00
{
return m_layoutName ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setName ( QString name )
2017-07-03 19:25:16 +03:00
{
if ( m_layoutName = = name ) {
return ;
}
2017-07-03 21:33:27 +03:00
qDebug ( ) < < " Layout name: " < < name ;
2017-07-03 19:25:16 +03:00
m_layoutName = name ;
emit nameChanged ( ) ;
}
2018-01-07 19:33:18 +02:00
QString Layout : : color ( ) const
2017-07-04 16:53:55 +03:00
{
return m_color ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setColor ( QString color )
2017-07-04 16:53:55 +03:00
{
if ( m_color = = color ) {
return ;
}
m_color = color ;
emit colorChanged ( ) ;
}
2017-07-03 19:25:16 +03:00
2018-01-07 19:33:18 +02:00
QString Layout : : file ( ) const
2017-07-03 19:25:16 +03:00
{
return m_layoutFile ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setFile ( QString file )
2017-07-03 19:25:16 +03:00
{
if ( m_layoutFile = = file ) {
return ;
}
2017-07-03 21:33:27 +03:00
qDebug ( ) < < " Layout file: " < < file ;
2017-07-03 19:25:16 +03:00
m_layoutFile = file ;
emit fileChanged ( ) ;
}
2018-01-07 19:33:18 +02:00
QStringList Layout : : launchers ( ) const
2017-07-02 21:02:27 +03:00
{
2017-07-24 17:47:53 +03:00
return m_launchers ;
2017-07-02 21:02:27 +03:00
}
2018-01-07 19:33:18 +02:00
void Layout : : setLaunchers ( QStringList launcherList )
2017-07-02 21:02:27 +03:00
{
2017-07-24 17:56:07 +03:00
if ( m_launchers = = launcherList )
2017-07-02 21:02:27 +03:00
return ;
2017-07-24 17:56:07 +03:00
m_launchers = launcherList ;
2017-07-02 21:02:27 +03:00
2017-07-24 17:47:53 +03:00
emit launchersChanged ( ) ;
2017-07-02 21:02:27 +03:00
}
2018-01-07 19:33:18 +02:00
QStringList Layout : : activities ( ) const
2017-07-17 18:31:43 +03:00
{
return m_activities ;
}
2018-01-07 19:33:18 +02:00
void Layout : : setActivities ( QStringList activities )
2017-07-17 18:31:43 +03:00
{
if ( m_activities = = activities ) {
return ;
}
m_activities = activities ;
emit activitiesChanged ( ) ;
}
2018-01-07 19:33:18 +02:00
bool Layout : : fileIsBroken ( ) const
2017-07-27 22:00:51 +03:00
{
if ( m_layoutFile . isEmpty ( ) | | ! QFile ( m_layoutFile ) . exists ( ) ) {
return false ;
}
KSharedConfigPtr lFile = KSharedConfig : : openConfig ( m_layoutFile ) ;
KConfigGroup containmentsEntries = KConfigGroup ( lFile , " Containments " ) ;
QStringList ids ;
ids < < containmentsEntries . groupList ( ) ;
2017-08-20 23:55:35 +03:00
QStringList conts ;
conts < < ids ;
QStringList applets ;
2017-07-27 22:00:51 +03:00
foreach ( auto cId , containmentsEntries . groupList ( ) ) {
auto appletsEntries = containmentsEntries . group ( cId ) . group ( " Applets " ) ;
ids < < appletsEntries . groupList ( ) ;
2017-08-20 23:55:35 +03:00
applets < < appletsEntries . groupList ( ) ;
2017-07-27 22:00:51 +03:00
}
QSet < QString > idsSet = QSet < QString > : : fromList ( ids ) ;
2017-08-20 23:55:35 +03:00
QMap < QString , int > countOfStrings ;
for ( int i = 0 ; i < ids . count ( ) ; i + + ) {
countOfStrings [ ids [ i ] ] + + ;
}
2017-07-27 22:00:51 +03:00
if ( idsSet . count ( ) ! = ids . count ( ) ) {
2017-08-20 23:55:35 +03:00
qDebug ( ) < < " ---- ERROR: BROKEN LAYOUT ---- " ;
foreach ( QString c , conts ) {
if ( applets . contains ( c ) ) {
qDebug ( ) < < " Error: Same applet and containment id found ::: " < < c ;
}
}
for ( int i = 0 ; i < ids . count ( ) ; + + i ) {
for ( int j = i + 1 ; j < ids . count ( ) ; + + j ) {
if ( ids [ i ] = = ids [ j ] ) {
qDebug ( ) < < " Error: Applets with same id ::: " < < ids [ i ] ;
}
}
}
2017-07-27 22:00:51 +03:00
return true ;
}
return false ;
}
2018-01-07 19:33:18 +02:00
QString Layout : : layoutName ( const QString & fileName )
2017-07-22 10:39:35 +03:00
{
int lastSlash = fileName . lastIndexOf ( " / " ) ;
QString tempLayoutFile = fileName ;
QString layoutName = tempLayoutFile . remove ( 0 , lastSlash + 1 ) ;
int ext = layoutName . lastIndexOf ( " .layout.latte " ) ;
layoutName = layoutName . remove ( ext , 13 ) ;
return layoutName ;
}
2018-01-07 19:33:18 +02:00
void Layout : : loadConfig ( )
2017-07-02 20:19:18 +03:00
{
2017-07-03 19:50:42 +03:00
m_version = m_layoutGroup . readEntry ( " version " , 2 ) ;
2017-07-04 16:53:55 +03:00
m_color = m_layoutGroup . readEntry ( " color " , QString ( " blue " ) ) ;
2017-07-17 18:31:43 +03:00
m_showInMenu = m_layoutGroup . readEntry ( " showInMenu " , false ) ;
m_activities = m_layoutGroup . readEntry ( " activities " , QStringList ( ) ) ;
2017-07-24 17:47:53 +03:00
m_launchers = m_layoutGroup . readEntry ( " launchers " , QStringList ( ) ) ;
2017-07-02 20:19:18 +03:00
}
2018-01-07 19:33:18 +02:00
void Layout : : saveConfig ( )
2017-07-02 20:19:18 +03:00
{
2017-07-03 19:50:42 +03:00
qDebug ( ) < < " layout is saving... for layout: " < < m_layoutName ;
2017-07-02 20:19:18 +03:00
m_layoutGroup . writeEntry ( " version " , m_version ) ;
2017-07-17 18:31:43 +03:00
m_layoutGroup . writeEntry ( " showInMenu " , m_showInMenu ) ;
2017-07-04 16:53:55 +03:00
m_layoutGroup . writeEntry ( " color " , m_color ) ;
2017-07-24 17:47:53 +03:00
m_layoutGroup . writeEntry ( " launchers " , m_launchers ) ;
2017-07-17 18:31:43 +03:00
m_layoutGroup . writeEntry ( " activities " , m_activities ) ;
2017-07-24 18:27:53 +03:00
m_layoutGroup . sync ( ) ;
2017-07-02 20:19:18 +03:00
}
2017-07-02 16:12:58 +03:00
}