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"
2018-03-04 12:36:48 +02:00
# include "dockcorona.h"
2017-07-02 15:02:07 +03:00
2018-03-06 22:54:37 +02:00
# include "sortedactivitiesmodel.h"
2017-07-22 23:21:34 +03:00
# include <QDir>
2018-07-23 16:55:51 +03:00
# include <QProcess>
2017-07-22 23:21:34 +03:00
2018-08-30 08:47:53 +03:00
# include <QtDBus>
2018-07-23 18:57:54 +03:00
2018-08-04 02:03:20 +03:00
# include <KDirWatch>
2018-03-06 22:54:37 +02:00
# include <KActivities/Consumer>
2018-08-04 02:03:20 +03:00
# define GLOBALSHORTCUTSCONFIG "kglobalshortcutsrc"
# define KWINMETAFORWARDTOLATTESTRING "org.kde.lattedock, / Latte,org.kde.LatteDock,activateLauncherMenu"
# define KWINMETAFORWARDTOPLASMASTRING "org.kde.plasmashell, / PlasmaShell,org.kde.PlasmaShell,activateLauncherMenu"
2018-07-23 18:57:54 +03:00
2018-08-04 02:03:20 +03:00
namespace Latte {
2018-07-23 16:55:51 +03:00
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
{
2018-05-10 20:26:46 +03:00
connect ( this , & UniversalSettings : : canDisableBordersChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-03 10:41:59 +03:00
connect ( this , & UniversalSettings : : currentLayoutNameChanged , this , & UniversalSettings : : saveConfig ) ;
2018-03-29 23:24:18 +03:00
connect ( this , & UniversalSettings : : downloadWindowSizeChanged , 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 ) ;
2018-03-03 13:03:05 +02:00
connect ( this , & UniversalSettings : : layoutsColumnWidthsChanged , this , & UniversalSettings : : saveConfig ) ;
2018-01-12 20:11:20 +02:00
connect ( this , & UniversalSettings : : layoutsMemoryUsageChanged , this , & UniversalSettings : : saveConfig ) ;
2018-03-03 13:03:05 +02:00
connect ( this , & UniversalSettings : : layoutsWindowSizeChanged , this , & UniversalSettings : : saveConfig ) ;
2018-01-27 02:05:33 +02:00
connect ( this , & UniversalSettings : : mouseSensitivityChanged , this , & UniversalSettings : : saveConfig ) ;
2018-04-01 15:04:52 +03:00
connect ( this , & UniversalSettings : : screenTrackerIntervalChanged , this , & UniversalSettings : : saveConfig ) ;
2017-07-25 14:19:01 +03:00
connect ( this , & UniversalSettings : : showInfoWindowChanged , 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 ( ) ;
2018-03-06 22:54:37 +02:00
if ( m_runningActivitiesModel ) {
m_runningActivitiesModel - > deleteLater ( ) ;
}
2017-07-02 15:02:07 +03:00
}
2018-08-04 02:03:20 +03:00
void UniversalSettings : : initGlobalShortcutsWatcher ( )
{
const QString globalShortcutsFilePath = QDir : : homePath ( ) + " /.config/ " + GLOBALSHORTCUTSCONFIG ;
m_shortcutsConfigPtr = KSharedConfig : : openConfig ( globalShortcutsFilePath ) ;
KDirWatch : : self ( ) - > addFile ( globalShortcutsFilePath ) ;
2018-10-27 11:06:40 +03:00
connect ( KDirWatch : : self ( ) , & KDirWatch : : dirty , this , & UniversalSettings : : shortcutsFileChanged , Qt : : QueuedConnection ) ;
connect ( KDirWatch : : self ( ) , & KDirWatch : : created , this , & UniversalSettings : : shortcutsFileChanged , Qt : : QueuedConnection ) ;
2018-08-04 02:03:20 +03:00
}
void UniversalSettings : : shortcutsFileChanged ( const QString & file )
{
if ( ! file . endsWith ( GLOBALSHORTCUTSCONFIG ) ) {
return ;
}
m_shortcutsConfigPtr - > reparseConfiguration ( ) ;
parseGlobalShortcuts ( ) ;
}
void UniversalSettings : : parseGlobalShortcuts ( )
{
KConfigGroup latteGroup = KConfigGroup ( m_shortcutsConfigPtr , " lattedock " ) ;
//! make sure that latte dock records in global shortcuts where found correctly
bool recordsExist { true } ;
if ( ! latteGroup . exists ( ) ) {
recordsExist = false ;
}
if ( recordsExist ) {
for ( int i = 1 ; i < = 19 ; + + i ) {
QString entry = " activate entry " + QString : : number ( i ) ;
if ( ! latteGroup . hasKey ( entry ) ) {
recordsExist = false ;
break ;
}
}
}
if ( recordsExist ) {
m_badgesForActivate . clear ( ) ;
for ( int i = 1 ; i < = 19 ; + + i ) {
QString entry = " activate entry " + QString : : number ( i ) ;
QStringList records = latteGroup . readEntry ( entry , QStringList ( ) ) ;
QString badge ;
if ( records [ 0 ] ! = " none " ) {
QStringList modifiers = records [ 0 ] . split ( " + " ) ;
if ( modifiers . count ( ) > = 1 ) {
2018-08-04 13:42:50 +03:00
badge = modifiers [ modifiers . count ( ) - 1 ] ;
//! when shortcut follows Meta+"Character" scheme
if ( modifiers . count ( ) = = 2 & & modifiers [ 0 ] = = " Meta " ) {
badge = badge . toLower ( ) ;
} else {
badge = badge . toUpper ( ) ;
}
2018-08-04 02:03:20 +03:00
}
}
m_badgesForActivate < < badge ;
}
2018-08-04 13:42:50 +03:00
emit badgesForActivateChanged ( ) ;
2018-08-04 02:03:20 +03:00
qDebug ( ) < < " badges updated to :: " < < m_badgesForActivate ;
}
}
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 ( ) ;
2018-08-04 02:03:20 +03:00
//! load global shortcuts badges at startup
initGlobalShortcutsWatcher ( ) ;
parseGlobalShortcuts ( ) ;
2017-07-02 20:19:18 +03:00
}
2017-07-25 14:19:01 +03:00
bool UniversalSettings : : showInfoWindow ( ) const
{
return m_showInfoWindow ;
}
void UniversalSettings : : setShowInfoWindow ( bool show )
{
if ( m_showInfoWindow = = show ) {
return ;
}
m_showInfoWindow = show ;
emit showInfoWindowChanged ( ) ;
}
2017-07-02 20:19:18 +03:00
int UniversalSettings : : version ( ) const
{
return m_version ;
}
void UniversalSettings : : setVersion ( int ver )
{
if ( m_version = = ver ) {
return ;
}
m_version = ver ;
emit versionChanged ( ) ;
}
2018-04-01 15:04:52 +03:00
int UniversalSettings : : screenTrackerInterval ( ) const
{
return m_screenTrackerInterval ;
}
void UniversalSettings : : setScreenTrackerInterval ( int duration )
{
if ( m_screenTrackerInterval = = duration ) {
return ;
}
m_screenTrackerInterval = duration ;
emit screenTrackerIntervalChanged ( ) ;
}
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 ( ) ;
}
2018-03-29 23:24:18 +03:00
QSize UniversalSettings : : downloadWindowSize ( ) const
{
return m_downloadWindowSize ;
}
void UniversalSettings : : setDownloadWindowSize ( QSize size )
{
if ( m_downloadWindowSize = = size ) {
return ;
}
m_downloadWindowSize = size ;
emit downloadWindowSizeChanged ( ) ;
}
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 ( ) ;
}
2018-08-04 02:03:20 +03:00
QStringList UniversalSettings : : badgesForActivate ( ) const
{
return m_badgesForActivate ;
}
2018-03-03 13:03:05 +02:00
QStringList UniversalSettings : : layoutsColumnWidths ( ) const
{
return m_layoutsColumnWidths ;
}
void UniversalSettings : : setLayoutsColumnWidths ( QStringList widths )
{
if ( m_layoutsColumnWidths = = widths ) {
return ;
}
m_layoutsColumnWidths = widths ;
emit layoutsColumnWidthsChanged ( ) ;
}
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
2018-08-30 08:47:53 +03:00
//! didn't let the plasma desktop to start
2017-07-04 15:05:54 +03:00
emit autostartChanged ( ) ;
}
}
2018-05-10 20:26:46 +03:00
bool UniversalSettings : : canDisableBorders ( ) const
{
return m_canDisableBorders ;
}
void UniversalSettings : : setCanDisableBorders ( bool enable )
{
if ( m_canDisableBorders = = enable ) {
return ;
}
m_canDisableBorders = enable ;
emit canDisableBordersChanged ( ) ;
}
2018-07-23 16:55:51 +03:00
bool UniversalSettings : : metaForwardedToLatte ( ) const
{
2018-07-23 18:57:54 +03:00
return kwin_metaForwardedToLatte ( ) ;
}
void UniversalSettings : : forwardMetaToLatte ( bool forward )
{
kwin_forwardMetaToLatte ( forward ) ;
2018-07-23 16:55:51 +03:00
}
bool UniversalSettings : : kwin_metaForwardedToLatte ( ) const
{
QProcess process ;
process . start ( " kreadconfig5 --file kwinrc --group ModifierOnlyShortcuts --key Meta " ) ;
process . waitForFinished ( ) ;
QString output ( process . readAllStandardOutput ( ) ) ;
output = output . remove ( " \n " ) ;
2018-08-04 02:03:20 +03:00
return ( output = = KWINMETAFORWARDTOLATTESTRING ) ;
2018-07-23 16:55:51 +03:00
}
2018-07-23 18:57:54 +03:00
void UniversalSettings : : kwin_forwardMetaToLatte ( bool forward )
{
if ( kwin_metaForwardedToLatte ( ) = = forward ) {
return ;
}
QProcess process ;
QStringList parameters ;
parameters < < " --file " < < " kwinrc " < < " --group " < < " ModifierOnlyShortcuts " < < " --key " < < " Meta " ;
if ( forward ) {
2018-08-04 02:03:20 +03:00
parameters < < KWINMETAFORWARDTOLATTESTRING ;
2018-07-23 18:57:54 +03:00
} else {
2018-08-04 02:03:20 +03:00
parameters < < KWINMETAFORWARDTOPLASMASTRING ;
2018-07-23 18:57:54 +03:00
}
process . start ( " kwriteconfig5 " , parameters ) ;
process . waitForFinished ( ) ;
QDBusInterface iface ( " org.kde.KWin " , " /KWin " , " " , QDBusConnection : : sessionBus ( ) ) ;
if ( iface . isValid ( ) ) {
iface . call ( " reconfigure " ) ;
}
}
2018-01-12 20:11:20 +02:00
Dock : : LayoutsMemoryUsage UniversalSettings : : layoutsMemoryUsage ( ) const
{
return m_memoryUsage ;
}
void UniversalSettings : : setLayoutsMemoryUsage ( Dock : : LayoutsMemoryUsage layoutsMemoryUsage )
{
2018-01-17 20:22:50 +02:00
if ( m_memoryUsage = = layoutsMemoryUsage ) {
2018-01-12 20:11:20 +02:00
return ;
}
m_memoryUsage = layoutsMemoryUsage ;
emit layoutsMemoryUsageChanged ( ) ;
}
2018-01-27 02:05:33 +02:00
Dock : : MouseSensitivity UniversalSettings : : mouseSensitivity ( ) const
{
return m_mouseSensitivity ;
}
void UniversalSettings : : setMouseSensitivity ( Dock : : MouseSensitivity sensitivity )
{
if ( m_mouseSensitivity = = sensitivity ) {
return ;
}
m_mouseSensitivity = sensitivity ;
emit mouseSensitivityChanged ( ) ;
}
2017-07-02 20:19:18 +03:00
void UniversalSettings : : loadConfig ( )
{
m_version = m_universalGroup . readEntry ( " version " , 1 ) ;
2018-05-10 20:26:46 +03:00
m_canDisableBorders = m_universalGroup . readEntry ( " canDisableBorders " , false ) ;
2017-07-03 10:41:59 +03:00
m_currentLayoutName = m_universalGroup . readEntry ( " currentLayout " , QString ( ) ) ;
2018-03-29 23:24:18 +03:00
m_downloadWindowSize = m_universalGroup . readEntry ( " downloadWindowSize " , QSize ( 800 , 550 ) ) ;
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 ) ) ;
2018-03-03 13:03:05 +02:00
m_layoutsColumnWidths = m_universalGroup . readEntry ( " layoutsColumnWidths " , QStringList ( ) ) ;
2017-07-24 17:56:07 +03:00
m_launchers = m_universalGroup . readEntry ( " launchers " , QStringList ( ) ) ;
2018-04-01 15:04:52 +03:00
m_screenTrackerInterval = m_universalGroup . readEntry ( " screenTrackerInterval " , 2500 ) ;
2017-07-25 14:19:01 +03:00
m_showInfoWindow = m_universalGroup . readEntry ( " showInfoWindow " , true ) ;
2018-01-12 20:11:20 +02:00
m_memoryUsage = static_cast < Dock : : LayoutsMemoryUsage > ( m_universalGroup . readEntry ( " memoryUsage " , ( int ) Dock : : SingleLayout ) ) ;
2018-01-27 02:05:33 +02:00
m_mouseSensitivity = static_cast < Dock : : MouseSensitivity > ( m_universalGroup . readEntry ( " mouseSensitivity " , ( int ) Dock : : HighSensitivity ) ) ;
2017-07-02 20:19:18 +03:00
}
void UniversalSettings : : saveConfig ( )
{
m_universalGroup . writeEntry ( " version " , m_version ) ;
2018-05-10 20:26:46 +03:00
m_universalGroup . writeEntry ( " canDisableBorders " , m_canDisableBorders ) ;
2017-07-03 10:41:59 +03:00
m_universalGroup . writeEntry ( " currentLayout " , m_currentLayoutName ) ;
2018-03-29 23:24:18 +03:00
m_universalGroup . writeEntry ( " downloadWindowSize " , m_downloadWindowSize ) ;
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 ) ;
2018-03-03 13:03:05 +02:00
m_universalGroup . writeEntry ( " layoutsColumnWidths " , m_layoutsColumnWidths ) ;
2017-07-24 17:56:07 +03:00
m_universalGroup . writeEntry ( " launchers " , m_launchers ) ;
2018-04-01 15:04:52 +03:00
m_universalGroup . writeEntry ( " screenTrackerInterval " , m_screenTrackerInterval ) ;
2017-07-25 14:19:01 +03:00
m_universalGroup . writeEntry ( " showInfoWindow " , m_showInfoWindow ) ;
2018-01-12 20:11:20 +02:00
m_universalGroup . writeEntry ( " memoryUsage " , ( int ) m_memoryUsage ) ;
2018-01-27 02:05:33 +02:00
m_universalGroup . writeEntry ( " mouseSensitivity " , ( int ) m_mouseSensitivity ) ;
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
2018-03-04 12:36:48 +02:00
QString UniversalSettings : : splitterIconPath ( )
{
auto * dockCorona = qobject_cast < DockCorona * > ( parent ( ) ) ;
if ( dockCorona ) {
return dockCorona - > kPackage ( ) . filePath ( " splitter " ) ;
}
return " " ;
}
QString UniversalSettings : : trademarkIconPath ( )
{
auto * dockCorona = qobject_cast < DockCorona * > ( parent ( ) ) ;
if ( dockCorona ) {
return dockCorona - > kPackage ( ) . filePath ( " trademark " ) ;
}
return " " ;
}
2018-03-06 22:54:37 +02:00
QAbstractItemModel * UniversalSettings : : runningActivitiesModel ( ) const
{
return m_runningActivitiesModel ;
}
void UniversalSettings : : setRunningActivitiesModel ( SortedActivitiesModel * model )
{
if ( m_runningActivitiesModel = = model ) {
return ;
}
if ( m_runningActivitiesModel ) {
m_runningActivitiesModel - > deleteLater ( ) ;
}
m_runningActivitiesModel = model ;
emit runningActivitiesModelChanged ( ) ;
}
void UniversalSettings : : enableActivitiesModel ( )
{
if ( ! m_runningActivitiesModel ) {
setRunningActivitiesModel ( new SortedActivitiesModel ( { KActivities : : Info : : Running , KActivities : : Info : : Stopping } , this ) ) ;
}
}
void UniversalSettings : : disableActivitiesModel ( )
{
if ( m_runningActivitiesModel ) {
setRunningActivitiesModel ( nullptr ) ;
}
}
float UniversalSettings : : luminasFromFile ( QString imageFile , int edge )
{
enableActivitiesModel ( ) ;
return m_runningActivitiesModel - > luminasFromFile ( imageFile , edge ) ;
}
2017-07-02 15:02:07 +03:00
}