2018-10-14 13:54:09 +03:00
/*
* Copyright 2018 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 "schemecolors.h"
2018-12-02 02:05:52 +02:00
// Qt
2018-10-14 13:54:09 +03:00
# include <QDebug>
# include <QDir>
# include <QFileInfo>
2018-12-02 02:05:52 +02:00
// KDE
2018-10-14 13:54:09 +03:00
# include <KConfigGroup>
2018-10-18 18:19:44 +03:00
# include <KDirWatch>
2018-10-14 13:54:09 +03:00
# include <KSharedConfig>
namespace Latte {
2018-11-08 15:04:59 +02:00
SchemeColors : : SchemeColors ( QObject * parent , QString scheme , bool plasmaTheme ) :
QObject ( parent ) ,
m_basedOnPlasmaTheme ( plasmaTheme )
2018-10-14 13:54:09 +03:00
{
QString pSchemeFile = possibleSchemeFile ( scheme ) ;
if ( QFileInfo ( pSchemeFile ) . exists ( ) ) {
2018-10-27 14:27:49 +03:00
setSchemeFile ( pSchemeFile ) ;
2018-11-09 10:44:51 +02:00
m_schemeName = schemeName ( pSchemeFile ) ;
2018-10-18 18:19:44 +03:00
//! track scheme file for changes
KDirWatch : : self ( ) - > addFile ( m_schemeFile ) ;
2018-10-27 11:06:40 +03:00
connect ( KDirWatch : : self ( ) , & KDirWatch : : dirty , this , [ & ] ( const QString & path ) {
if ( path = = m_schemeFile ) {
updateScheme ( ) ;
}
} ) ;
2018-10-14 13:54:09 +03:00
}
updateScheme ( ) ;
}
SchemeColors : : ~ SchemeColors ( )
{
2019-02-22 16:43:31 +02:00
///
2018-10-14 13:54:09 +03:00
}
QColor SchemeColors : : backgroundColor ( ) const
{
2018-11-07 20:20:36 +02:00
return m_activeBackgroundColor ;
2018-10-14 13:54:09 +03:00
}
2018-10-18 18:02:54 +03:00
QColor SchemeColors : : textColor ( ) const
2018-10-14 13:54:09 +03:00
{
2018-11-07 20:20:36 +02:00
return m_activeTextColor ;
}
QColor SchemeColors : : inactiveBackgroundColor ( ) const
{
return m_inactiveBackgroundColor ;
}
QColor SchemeColors : : inactiveTextColor ( ) const
{
return m_inactiveTextColor ;
2018-10-18 18:02:54 +03:00
}
QColor SchemeColors : : highlightColor ( ) const
{
return m_highlightColor ;
}
QColor SchemeColors : : highlightedTextColor ( ) const
{
return m_highlightedTextColor ;
}
2018-11-17 18:12:42 +02:00
QColor SchemeColors : : positiveTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 18:12:42 +02:00
return m_positiveTextColor ;
2018-10-18 18:02:54 +03:00
}
2018-11-17 18:12:42 +02:00
QColor SchemeColors : : neutralTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 18:12:42 +02:00
return m_neutralTextColor ;
2018-10-18 18:02:54 +03:00
}
2018-11-17 18:12:42 +02:00
QColor SchemeColors : : negativeTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 18:12:42 +02:00
return m_negativeTextColor ;
2018-10-14 13:54:09 +03:00
}
2018-10-27 14:27:49 +03:00
QColor SchemeColors : : buttonTextColor ( ) const
{
return m_buttonTextColor ;
}
QColor SchemeColors : : buttonBackgroundColor ( ) const
{
return m_buttonBackgroundColor ;
}
QColor SchemeColors : : buttonHoverColor ( ) const
{
return m_buttonHoverColor ;
}
QColor SchemeColors : : buttonFocusColor ( ) const
{
return m_buttonFocusColor ;
}
QString SchemeColors : : schemeName ( ) const
2018-10-14 13:54:09 +03:00
{
return m_schemeName ;
}
2018-10-27 14:27:49 +03:00
QString SchemeColors : : SchemeColors : : schemeFile ( ) const
2018-10-14 13:54:09 +03:00
{
return m_schemeFile ;
}
2018-10-27 14:27:49 +03:00
void SchemeColors : : setSchemeFile ( QString file )
{
if ( m_schemeFile = = file ) {
return ;
}
m_schemeFile = file ;
emit schemeFileChanged ( ) ;
}
2018-10-14 13:54:09 +03:00
QString SchemeColors : : possibleSchemeFile ( QString scheme )
{
2018-10-27 14:27:49 +03:00
if ( scheme . startsWith ( " / " ) & & scheme . endsWith ( " colors " ) & & QFileInfo ( scheme ) . exists ( ) ) {
2018-10-14 13:54:09 +03:00
return scheme ;
}
QString tempScheme = scheme ;
if ( scheme = = " kdeglobals " ) {
QString settingsFile = QDir : : homePath ( ) + " /.config/kdeglobals " ;
if ( QFileInfo ( settingsFile ) . exists ( ) ) {
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( settingsFile ) ;
KConfigGroup generalGroup = KConfigGroup ( filePtr , " General " ) ;
tempScheme = generalGroup . readEntry ( " ColorScheme " , " " ) ;
}
}
2019-02-22 16:43:31 +02:00
QString localSchemePath = QDir : : homePath ( ) + " /.local/share/color-schemes/ " + tempScheme + " .colors " ;
QString globalSchemePath = " /usr/share/color-schemes/ " + tempScheme + " .colors " ;
2018-10-14 13:54:09 +03:00
2019-02-22 16:43:31 +02:00
if ( ! QFileInfo ( localSchemePath ) . exists ( ) & & ! QFileInfo ( globalSchemePath ) . exists ( ) ) {
//! remove all whitespaces and "-" from scheme in order to access correctly its file
QString schemeNameSimplified = tempScheme . simplified ( ) . remove ( " " ) . remove ( " - " ) ;
localSchemePath = QDir : : homePath ( ) + " /.local/share/color-schemes/ " + schemeNameSimplified + " .colors " ;
globalSchemePath = " /usr/share/color-schemes/ " + schemeNameSimplified + " .colors " ;
}
2018-10-14 13:54:09 +03:00
if ( QFileInfo ( localSchemePath ) . exists ( ) ) {
return localSchemePath ;
} else if ( QFileInfo ( globalSchemePath ) . exists ( ) ) {
return globalSchemePath ;
}
return " " ;
}
2018-11-09 10:44:51 +02:00
QString SchemeColors : : schemeName ( QString originalFile )
{
if ( ! ( originalFile . startsWith ( " / " ) & & originalFile . endsWith ( " colors " ) & & QFileInfo ( originalFile ) . exists ( ) ) ) {
return " " ;
}
QString fileNameNoExt = originalFile ;
int lastSlash = originalFile . lastIndexOf ( " / " ) ;
if ( lastSlash > = 0 ) {
fileNameNoExt . remove ( 0 , lastSlash + 1 ) ;
}
if ( fileNameNoExt . endsWith ( " .colors " ) ) {
fileNameNoExt . remove ( " .colors " ) ;
}
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( originalFile ) ;
KConfigGroup generalGroup = KConfigGroup ( filePtr , " General " ) ;
return generalGroup . readEntry ( " Name " , fileNameNoExt ) ;
}
2018-10-14 13:54:09 +03:00
void SchemeColors : : updateScheme ( )
{
if ( m_schemeFile . isEmpty ( ) | | ! QFileInfo ( m_schemeFile ) . exists ( ) ) {
return ;
}
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( m_schemeFile ) ;
KConfigGroup wmGroup = KConfigGroup ( filePtr , " WM " ) ;
2018-10-18 18:02:54 +03:00
KConfigGroup selGroup = KConfigGroup ( filePtr , " Colors:Selection " ) ;
2018-11-08 15:04:59 +02:00
KConfigGroup viewGroup = KConfigGroup ( filePtr , " Colors:View " ) ;
2018-10-27 14:27:49 +03:00
//KConfigGroup windowGroup = KConfigGroup(filePtr, "Colors:Window");
KConfigGroup buttonGroup = KConfigGroup ( filePtr , " Colors:Button " ) ;
2018-10-14 13:54:09 +03:00
2018-11-08 15:04:59 +02:00
if ( ! m_basedOnPlasmaTheme ) {
m_activeBackgroundColor = wmGroup . readEntry ( " activeBackground " , QColor ( ) ) ;
m_activeTextColor = wmGroup . readEntry ( " activeForeground " , QColor ( ) ) ;
m_inactiveBackgroundColor = wmGroup . readEntry ( " inactiveBackground " , QColor ( ) ) ;
m_inactiveTextColor = wmGroup . readEntry ( " inactiveForeground " , QColor ( ) ) ;
} else {
m_activeBackgroundColor = viewGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_activeTextColor = viewGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
m_inactiveBackgroundColor = viewGroup . readEntry ( " BackgroundAlternate " , QColor ( ) ) ;
m_inactiveTextColor = viewGroup . readEntry ( " ForegroundInactive " , QColor ( ) ) ;
}
2018-10-18 18:02:54 +03:00
m_highlightColor = selGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_highlightedTextColor = selGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
2018-11-17 18:12:42 +02:00
m_positiveTextColor = viewGroup . readEntry ( " ForegroundPositive " , QColor ( ) ) ;
m_neutralTextColor = viewGroup . readEntry ( " ForegroundNeutral " , QColor ( ) ) ; ;
m_negativeTextColor = viewGroup . readEntry ( " ForegroundNegative " , QColor ( ) ) ;
2018-10-14 13:54:09 +03:00
2018-10-27 14:27:49 +03:00
m_buttonTextColor = buttonGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
m_buttonBackgroundColor = buttonGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_buttonHoverColor = buttonGroup . readEntry ( " DecorationHover " , QColor ( ) ) ;
m_buttonFocusColor = buttonGroup . readEntry ( " DecorationFocus " , QColor ( ) ) ;
2018-10-14 13:54:09 +03:00
emit colorsChanged ( ) ;
}
}