2017-07-02 15:02:07 +03:00
/*
2021-05-27 15:01:00 +00:00
SPDX - FileCopyrightText : 2017 Smith AR < audoban @ openmailbox . org >
SPDX - FileCopyrightText : 2019 Michail Vourlakos < mvourlakos @ gmail . com >
SPDX - License - Identifier : GPL - 2.0 - or - later
2017-07-02 15:02:07 +03:00
*/
2019-05-09 17:12:57 +03:00
# include "manager.h"
2018-02-03 11:34:13 +02:00
2018-12-02 02:05:52 +02:00
// local
2019-05-09 17:57:12 +03:00
# include "importer.h"
2021-01-14 20:21:12 +02:00
# include "syncedlaunchers.h"
2019-05-09 17:12:57 +03:00
# include "../infoview.h"
# include "../screenpool.h"
2020-08-12 14:03:38 +03:00
# include "../data/layoutdata.h"
2021-02-05 19:05:04 +02:00
# include "../data/generictable.h"
2019-05-09 17:12:57 +03:00
# include "../layout/abstractlayout.h"
# include "../layout/centrallayout.h"
# include "../settings/universalsettings.h"
2020-08-13 20:42:38 +03:00
# include "../templates/templatesmanager.h"
2021-02-02 22:29:27 +02:00
# include "../tools/commontools.h"
2017-07-26 10:21:37 +03:00
2018-12-02 02:05:52 +02:00
// Qt
2017-07-03 12:58:02 +03:00
# include <QDir>
# include <QFile>
2018-01-15 14:09:41 +02:00
# include <QMessageBox>
2021-05-21 22:00:42 +00:00
# include <QLatin1String>
2017-07-03 12:58:02 +03:00
2018-12-02 02:05:52 +02:00
// KDE
2021-05-01 16:01:39 +03:00
# include <KMessageBox>
2017-07-23 21:05:34 +03:00
# include <KLocalizedString>
# include <KNotification>
2017-07-03 12:58:02 +03:00
2017-07-02 15:02:07 +03:00
namespace Latte {
2019-05-09 17:12:57 +03:00
namespace Layouts {
2017-07-02 15:02:07 +03:00
2019-05-09 17:12:57 +03:00
Manager : : Manager ( QObject * parent )
2017-07-02 15:02:07 +03:00
: QObject ( parent ) ,
2017-07-31 03:53:39 +03:00
m_importer ( new Importer ( this ) ) ,
2021-01-14 20:21:12 +02:00
m_syncedLaunchers ( new SyncedLaunchers ( this ) )
2017-07-02 15:02:07 +03:00
{
2018-12-06 14:35:34 +02:00
m_corona = qobject_cast < Latte : : Corona * > ( parent ) ;
2019-05-11 03:46:06 +03:00
//! needs to be created AFTER corona assignment
m_synchronizer = new Synchronizer ( this ) ;
2017-07-03 19:04:37 +03:00
if ( m_corona ) {
2019-05-11 03:11:50 +03:00
connect ( m_synchronizer , & Synchronizer : : centralLayoutsChanged , this , & Manager : : centralLayoutsChanged ) ;
connect ( m_synchronizer , & Synchronizer : : currentLayoutIsSwitching , this , & Manager : : currentLayoutIsSwitching ) ;
2017-07-03 19:04:37 +03:00
}
2017-07-02 15:02:07 +03:00
}
2019-05-09 17:12:57 +03:00
Manager : : ~ Manager ( )
2017-07-02 15:02:07 +03:00
{
m_importer - > deleteLater ( ) ;
2021-01-14 20:21:12 +02:00
m_syncedLaunchers - > deleteLater ( ) ;
2017-07-03 19:25:16 +03:00
2019-05-11 03:11:50 +03:00
//! no needed because Latte:Corona is calling it at better place
// unload();
2019-04-07 15:46:03 +03:00
2019-05-11 03:11:50 +03:00
m_synchronizer - > deleteLater ( ) ;
2017-07-02 15:02:07 +03:00
}
2020-08-29 20:42:15 +03:00
void Manager : : init ( )
2017-07-02 20:19:18 +03:00
{
2020-08-14 09:33:39 +03:00
QDir layoutsDir ( Layouts : : Importer : : layoutUserDir ( ) ) ;
2020-02-23 09:53:04 +02:00
bool firstRun = ! layoutsDir . exists ( ) ;
2017-07-02 20:19:18 +03:00
int configVer = m_corona - > universalSettings ( ) - > version ( ) ;
qDebug ( ) < < " Universal Settings version : " < < configVer ;
2020-02-23 09:53:04 +02:00
if ( firstRun ) {
m_corona - > universalSettings ( ) - > setVersion ( 2 ) ;
2020-08-28 19:03:23 +03:00
m_corona - > universalSettings ( ) - > setSingleModeLayoutName ( i18n ( " My Layout " ) ) ;
2017-07-03 21:09:16 +03:00
2020-02-23 09:53:04 +02:00
//startup create what is necessary....
if ( ! layoutsDir . exists ( ) ) {
2021-02-02 22:29:27 +02:00
QDir ( Latte : : configPath ( ) ) . mkdir ( " latte " ) ;
2017-07-03 21:09:16 +03:00
}
2020-08-28 19:03:23 +03:00
QString defpath = m_corona - > templatesManager ( ) - > newLayout ( i18n ( " My Layout " ) , i18n ( Templates : : DEFAULTLAYOUTTEMPLATENAME ) ) ;
setOnAllActivities ( Layout : : AbstractLayout : : layoutName ( defpath ) ) ;
2020-08-14 09:57:39 +03:00
m_corona - > templatesManager ( ) - > importSystemLayouts ( ) ;
2020-02-23 09:53:04 +02:00
} else if ( configVer < 2 & & ! firstRun ) {
2017-07-03 21:09:16 +03:00
m_corona - > universalSettings ( ) - > setVersion ( 2 ) ;
2020-02-23 09:53:04 +02:00
bool isOlderVersion = m_importer - > updateOldConfiguration ( ) ;
if ( isOlderVersion ) {
qDebug ( ) < < " Latte is updating its older configuration... " ;
2020-08-14 09:57:39 +03:00
m_corona - > templatesManager ( ) - > importSystemLayouts ( ) ;
2020-02-23 09:53:04 +02:00
} else {
2020-08-28 19:03:23 +03:00
m_corona - > universalSettings ( ) - > setSingleModeLayoutName ( i18n ( " My Layout " ) ) ;
2020-02-23 09:53:04 +02:00
}
2021-02-13 12:57:13 +02:00
}
//! Custom Templates path creation
QDir localTemplatesDir ( Latte : : configPath ( ) + " /latte/templates " ) ;
if ( ! localTemplatesDir . exists ( ) ) {
QDir ( Latte : : configPath ( ) + " /latte " ) . mkdir ( " templates " ) ;
2017-07-02 20:19:18 +03:00
}
2018-01-13 12:00:47 +02:00
//! Check if the multiple-layouts hidden file is present, add it if it isnt
2020-08-14 09:33:39 +03:00
if ( ! QFile ( Layouts : : Importer : : layoutUserFilePath ( Layout : : MULTIPLELAYOUTSHIDDENNAME ) ) . exists ( ) ) {
2020-08-14 09:41:23 +03:00
m_corona - > templatesManager ( ) - > newLayout ( " " , Layout : : MULTIPLELAYOUTSHIDDENNAME ) ;
2018-01-13 12:00:47 +02:00
}
2017-07-02 21:22:37 +03:00
qDebug ( ) < < " Latte is loading its layouts... " ;
2017-07-03 20:40:18 +03:00
2020-08-29 20:42:15 +03:00
m_synchronizer - > initLayouts ( ) ;
2017-07-02 20:19:18 +03:00
}
2019-05-09 17:12:57 +03:00
void Manager : : unload ( )
2018-01-13 03:08:32 +02:00
{
2019-05-11 03:11:50 +03:00
m_synchronizer - > unloadLayouts ( ) ;
2019-05-03 02:28:28 +03:00
}
2019-05-09 17:12:57 +03:00
Latte : : Corona * Manager : : corona ( )
2017-07-02 21:02:27 +03:00
{
return m_corona ;
}
2019-05-09 17:12:57 +03:00
Importer * Manager : : importer ( )
2017-07-22 19:49:28 +03:00
{
return m_importer ;
}
2021-01-14 20:21:12 +02:00
SyncedLaunchers * Manager : : syncedLaunchers ( ) const
2017-07-31 03:53:39 +03:00
{
2021-01-14 20:21:12 +02:00
return m_syncedLaunchers ;
2017-07-31 03:53:39 +03:00
}
2019-05-11 03:11:50 +03:00
Synchronizer * Manager : : synchronizer ( ) const
2017-07-03 20:40:18 +03:00
{
2019-05-11 03:11:50 +03:00
return m_synchronizer ;
}
2017-07-03 20:40:18 +03:00
2020-04-24 12:31:03 +03:00
MemoryUsage : : LayoutsMemory Manager : : memoryUsage ( ) const
2018-01-12 20:11:20 +02:00
{
return m_corona - > universalSettings ( ) - > layoutsMemoryUsage ( ) ;
}
2020-04-24 12:31:03 +03:00
void Manager : : setMemoryUsage ( MemoryUsage : : LayoutsMemory memoryUsage )
2018-01-12 20:11:20 +02:00
{
m_corona - > universalSettings ( ) - > setLayoutsMemoryUsage ( memoryUsage ) ;
}
2019-05-09 17:12:57 +03:00
QStringList Manager : : centralLayoutsNames ( )
2018-01-17 16:45:29 +02:00
{
2019-05-11 03:11:50 +03:00
return m_synchronizer - > centralLayoutsNames ( ) ;
2018-01-17 16:45:29 +02:00
}
2020-08-27 17:10:13 +03:00
QStringList Manager : : currentLayoutsNames ( ) const
{
return m_synchronizer - > currentLayoutsNames ( ) ;
}
2021-02-05 19:05:04 +02:00
QStringList Manager : : viewTemplateNames ( ) const
{
Latte : : Data : : GenericTable < Data : : Generic > viewtemplates = m_corona - > templatesManager ( ) - > viewTemplates ( ) ;
QStringList names ;
for ( int i = 0 ; i < viewtemplates . rowCount ( ) ; + + i ) {
names < < viewtemplates [ i ] . name ;
}
return names ;
}
QStringList Manager : : viewTemplateIds ( ) const
{
Latte : : Data : : GenericTable < Data : : Generic > viewtemplates = m_corona - > templatesManager ( ) - > viewTemplates ( ) ;
QStringList ids ;
for ( int i = 0 ; i < viewtemplates . rowCount ( ) ; + + i ) {
ids < < viewtemplates [ i ] . id ;
}
return ids ;
}
2021-05-15 13:31:56 +03:00
Latte : : Data : : LayoutIcon Manager : : iconForLayout ( const QString & storedLayoutName ) const
{
Data : : Layout l = m_synchronizer - > data ( storedLayoutName ) ;
return iconForLayout ( l ) ;
}
2021-05-15 13:06:54 +03:00
Latte : : Data : : LayoutIcon Manager : : iconForLayout ( const Data : : Layout & layout ) const
{
Latte : : Data : : LayoutIcon _icon ;
if ( ! layout . icon . isEmpty ( ) ) {
//! if there is specific icon set from the user for this layout we draw only that icon
_icon . name = layout . icon ;
_icon . isBackgroundFile = false ;
return _icon ;
}
//! fallback icon: background image
if ( _icon . isEmpty ( ) ) {
QString colorPath = m_corona - > kPackage ( ) . path ( ) + " ../../shells/org.kde.latte.shell/contents/images/canvas/ " ;
if ( layout . backgroundStyle = = Layout : : PatternBackgroundStyle & & layout . background . isEmpty ( ) ) {
colorPath + = " defaultcustomprint.jpg " ;
} else {
colorPath = layout . background . startsWith ( " / " ) ? layout . background : colorPath + layout . color + " print.jpg " ;
}
if ( QFileInfo ( colorPath ) . exists ( ) ) {
_icon . isBackgroundFile = true ;
_icon . name = colorPath ;
return _icon ;
}
}
return Latte : : Data : : LayoutIcon ( ) ;
}
2020-08-25 19:30:33 +03:00
QList < CentralLayout * > Manager : : currentLayouts ( ) const
2019-02-07 09:38:01 +02:00
{
2020-08-25 19:30:33 +03:00
return m_synchronizer - > currentLayouts ( ) ;
2018-01-15 10:17:56 +02:00
}
2020-08-30 14:04:04 +03:00
bool Manager : : switchToLayout ( QString layoutName , MemoryUsage : : LayoutsMemory newMemoryUsage )
2017-07-26 10:30:03 +03:00
{
2020-08-30 14:04:04 +03:00
return m_synchronizer - > switchToLayout ( layoutName , newMemoryUsage ) ;
2017-07-26 10:30:03 +03:00
}
2019-05-09 17:12:57 +03:00
void Manager : : loadLayoutOnStartup ( QString layoutName )
2018-01-15 14:09:41 +02:00
{
2018-01-31 15:27:58 +02:00
QStringList layouts = m_importer - > checkRepairMultipleLayoutsLinkedFile ( ) ;
2018-08-30 08:47:53 +03:00
//! Latte didn't close correctly, maybe a crash
2018-01-31 15:27:58 +02:00
if ( layouts . size ( ) > 0 ) {
2021-05-01 16:01:39 +03:00
QDialog * dialog = new QDialog ( nullptr ) ;
2021-05-01 16:15:25 +03:00
dialog - > setWindowTitle ( i18n ( " Multiple Layouts Startup Warning " ) ) ;
2021-05-01 16:01:39 +03:00
dialog - > setObjectName ( " sorry " ) ;
dialog - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
auto buttonbox = new QDialogButtonBox ( QDialogButtonBox : : Ok ) ;
KMessageBox : : createKMessageBox ( dialog ,
buttonbox ,
QMessageBox : : Warning ,
2021-05-04 10:40:31 +03:00
i18np ( " <b>Multiple Layouts based on Activities</b> mode did not close properly during the last session.<br/><br/>The following layout <b>[ %2 ]</b> had to be updated for consistency! " ,
2021-05-08 19:26:48 +03:00
" <b>Multiple Layouts based on Activities</b> mode did not close properly during the last session.<br/><br/>The following layouts <b>[ %2 ]</b> had to be updated for consistency! " ,
2021-05-04 10:40:31 +03:00
layouts . count ( ) ,
layouts . join ( " , " ) ) ,
2021-05-01 16:01:39 +03:00
QStringList ( ) ,
QString ( ) ,
0 ,
KMessageBox : : NoExec ,
QString ( ) ) ;
dialog - > show ( ) ;
2018-01-15 14:09:41 +02:00
}
2019-05-11 03:11:50 +03:00
m_synchronizer - > switchToLayout ( layoutName ) ;
2018-01-15 14:09:41 +02:00
}
2021-04-27 00:08:16 +03:00
void Manager : : moveView ( QString originLayoutName , uint originViewId , QString destinationLayoutName )
{
if ( memoryUsage ( ) ! = Latte : : MemoryUsage : : MultipleLayouts
| | originLayoutName . isEmpty ( )
| | destinationLayoutName . isEmpty ( )
| | originViewId < = 0
| | originLayoutName = = destinationLayoutName ) {
return ;
}
auto originlayout = m_synchronizer - > layout ( originLayoutName ) ;
auto destinationlayout = m_synchronizer - > layout ( destinationLayoutName ) ;
if ( ! originlayout | | ! destinationlayout | | originlayout = = destinationlayout ) {
return ;
}
Plasma : : Containment * originviewcontainment = originlayout - > containmentForId ( originViewId ) ;
Latte : : View * originview = originlayout - > viewForContainment ( originViewId ) ;
if ( ! originviewcontainment ) {
return ;
}
QList < Plasma : : Containment * > origincontainments = originlayout - > unassignFromLayout ( originviewcontainment ) ;
if ( origincontainments . size ( ) > 0 ) {
destinationlayout - > assignToLayout ( originview , origincontainments ) ;
}
}
2019-05-09 17:12:57 +03:00
void Manager : : loadLatteLayout ( QString layoutPath )
2018-01-13 12:00:47 +02:00
{
2018-01-13 20:00:03 +02:00
qDebug ( ) < < " -------------------------------------------------------------------- " ;
qDebug ( ) < < " -------------------------------------------------------------------- " ;
2018-01-13 12:00:47 +02:00
if ( m_corona - > containments ( ) . size ( ) > 0 ) {
2018-01-13 20:00:03 +02:00
qDebug ( ) < < " LOAD LATTE LAYOUT ::: There are still containments present !!!! :: " < < m_corona - > containments ( ) . size ( ) ;
2018-01-13 12:00:47 +02:00
}
if ( ! layoutPath . isEmpty ( ) & & m_corona - > containments ( ) . size ( ) = = 0 ) {
2018-02-04 02:43:30 +02:00
cleanupOnStartup ( layoutPath ) ;
2018-01-13 20:00:03 +02:00
qDebug ( ) < < " LOADING CORONA LAYOUT: " < < layoutPath ;
m_corona - > loadLayout ( layoutPath ) ;
2018-01-13 12:00:47 +02:00
}
}
2020-08-28 19:03:23 +03:00
void Manager : : setOnAllActivities ( QString layoutName )
{
CentralLayout * central = m_synchronizer - > centralLayout ( layoutName ) ;
if ( central ) {
central - > setActivities ( QStringList ( Data : : Layout : : ALLACTIVITIESID ) ) ;
} else if ( m_importer - > layoutExists ( layoutName ) ) {
CentralLayout storage ( this , m_importer - > layoutUserFilePath ( layoutName ) ) ;
storage . setActivities ( QStringList ( Data : : Layout : : ALLACTIVITIESID ) ) ;
}
}
2020-08-30 20:32:13 +03:00
void Manager : : setOnActivities ( QString layoutName , QStringList activities )
{
CentralLayout * central = m_synchronizer - > centralLayout ( layoutName ) ;
if ( central ) {
central - > setActivities ( activities ) ;
} else if ( m_importer - > layoutExists ( layoutName ) ) {
CentralLayout storage ( this , m_importer - > layoutUserFilePath ( layoutName ) ) ;
storage . setActivities ( activities ) ;
}
}
2019-05-09 17:12:57 +03:00
void Manager : : cleanupOnStartup ( QString path )
2018-02-04 02:43:30 +02:00
{
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( path ) ;
KConfigGroup actionGroups = KConfigGroup ( filePtr , " ActionPlugins " ) ;
QStringList deprecatedActionGroup ;
2019-04-04 23:55:44 +03:00
for ( const auto & actId : actionGroups . groupList ( ) ) {
2018-02-04 02:43:30 +02:00
QString pluginId = actionGroups . group ( actId ) . readEntry ( " RightButton;NoModifier " , " " ) ;
2021-05-21 22:00:42 +00:00
if ( pluginId = = QStringLiteral ( " org.kde.contextmenu " ) ) {
2018-02-04 02:43:30 +02:00
deprecatedActionGroup < < actId ;
}
}
2019-04-04 23:55:44 +03:00
for ( const auto & pId : deprecatedActionGroup ) {
2018-02-04 02:43:30 +02:00
qDebug ( ) < < " !!!!!!!!!!!!!!!! !!!!!!!!!!!! !!!!!!! REMOVING :::: " < < pId ;
actionGroups . group ( pId ) . deleteGroup ( ) ;
}
2018-02-08 17:49:27 +02:00
KConfigGroup containmentGroups = KConfigGroup ( filePtr , " Containments " ) ;
QStringList removeContaimentsList ;
2019-04-04 23:55:44 +03:00
for ( const auto & cId : containmentGroups . groupList ( ) ) {
2018-02-08 17:49:27 +02:00
QString pluginId = containmentGroups . group ( cId ) . readEntry ( " plugin " , " " ) ;
2021-05-21 22:00:42 +00:00
if ( pluginId = = QStringLiteral ( " org.kde.desktopcontainment " ) ) { //!must remove ghost containments first
2018-02-08 17:49:27 +02:00
removeContaimentsList < < cId ;
}
}
2019-04-04 23:55:44 +03:00
for ( const auto & cId : removeContaimentsList ) {
2018-02-08 17:49:27 +02:00
containmentGroups . group ( cId ) . deleteGroup ( ) ;
}
2018-02-04 02:43:30 +02:00
}
2019-05-09 17:12:57 +03:00
void Manager : : showAboutDialog ( )
2018-01-26 19:46:02 +02:00
{
m_corona - > aboutApplication ( ) ;
}
2019-05-09 17:12:57 +03:00
void Manager : : clearUnloadedContainmentsFromLinkedFile ( QStringList containmentsIds , bool bypassChecks )
2018-01-14 17:30:29 +02:00
{
2020-04-24 12:31:03 +03:00
if ( ! m_corona | | ( memoryUsage ( ) = = MemoryUsage : : SingleLayout & & ! bypassChecks ) ) {
2018-01-14 17:30:29 +02:00
return ;
}
auto containments = m_corona - > config ( ) - > group ( " Containments " ) ;
2019-04-04 23:55:44 +03:00
for ( const auto & conId : containmentsIds ) {
2018-01-14 17:30:29 +02:00
qDebug ( ) < < " unloads ::: " < < conId ;
KConfigGroup containment = containments . group ( conId ) ;
containment . deleteGroup ( ) ;
2021-04-22 13:44:22 +03:00
containment . sync ( ) ;
2018-01-14 17:30:29 +02:00
}
2020-08-30 14:04:04 +03:00
containments . sync ( ) ;
2018-01-14 17:30:29 +02:00
}
2019-12-27 23:39:33 +07:00
void Manager : : showLatteSettingsDialog ( int firstPage , bool toggleCurrentPage )
2017-07-07 14:06:30 -05:00
{
2018-02-03 13:34:07 +02:00
if ( ! m_latteSettingsDialog ) {
2020-03-26 20:04:44 +02:00
m_latteSettingsDialog = new Latte : : Settings : : Dialog : : SettingsDialog ( nullptr , m_corona ) ;
2018-01-27 02:05:33 +02:00
}
2018-02-03 13:34:07 +02:00
m_latteSettingsDialog - > show ( ) ;
2017-07-30 19:49:05 +03:00
2018-02-03 13:34:07 +02:00
if ( m_latteSettingsDialog - > isMinimized ( ) ) {
m_latteSettingsDialog - > showNormal ( ) ;
2017-07-30 19:49:05 +03:00
}
2019-12-27 23:39:33 +07:00
if ( toggleCurrentPage ) {
2019-05-12 17:36:15 +03:00
m_latteSettingsDialog - > toggleCurrentPage ( ) ;
2019-12-27 23:39:33 +07:00
} else {
m_latteSettingsDialog - > setCurrentPage ( firstPage ) ;
2019-05-12 17:36:15 +03:00
}
2018-01-27 02:52:31 +02:00
2018-02-03 13:34:07 +02:00
m_latteSettingsDialog - > activateWindow ( ) ;
2017-07-07 14:06:30 -05:00
}
2019-05-09 17:12:57 +03:00
void Manager : : hideLatteSettingsDialog ( )
2018-03-26 20:31:17 +03:00
{
if ( m_latteSettingsDialog ) {
m_latteSettingsDialog - > deleteLater ( ) ;
m_latteSettingsDialog = nullptr ;
}
}
2019-05-09 17:12:57 +03:00
void Manager : : showInfoWindow ( QString info , int duration , QStringList activities )
2017-07-25 14:06:48 +03:00
{
2019-04-04 23:55:44 +03:00
for ( const auto screen : qGuiApp - > screens ( ) ) {
2017-07-26 10:21:37 +03:00
InfoView * infoView = new InfoView ( m_corona , info , screen ) ;
2017-07-25 14:06:48 +03:00
2017-07-26 10:21:37 +03:00
infoView - > show ( ) ;
2018-01-15 00:29:11 +02:00
infoView - > setOnActivities ( activities ) ;
2017-07-25 14:06:48 +03:00
2017-07-26 10:21:37 +03:00
QTimer : : singleShot ( duration , [ this , infoView ] ( ) {
infoView - > deleteLater ( ) ;
} ) ;
}
2017-07-25 14:06:48 +03:00
}
2017-07-02 15:02:07 +03:00
}
2019-05-09 17:12:57 +03:00
}