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 "importer.h"
2017-07-02 21:02:27 +03:00
2018-12-02 03:05:52 +03:00
// local
2020-04-24 14:52:16 +03:00
# include <coretypes.h>
2019-05-09 17:57:12 +03:00
# include "manager.h"
# include "../lattecorona.h"
# include "../screenpool.h"
# include "../layout/abstractlayout.h"
# include "../settings/universalsettings.h"
2017-07-02 20:19:18 +03:00
2018-12-02 03:05:52 +03:00
// Qt
2017-07-02 20:19:18 +03:00
# include <QFile>
2017-07-03 00:34:59 +03:00
# include <QTemporaryDir>
2017-07-02 20:19:18 +03:00
2018-12-02 03:05:52 +03:00
// KDE
2017-07-03 00:34:59 +03:00
# include <KArchive/KTar>
2017-07-22 17:59:23 +03:00
# include <KArchive/KArchiveEntry>
# include <KArchive/KArchiveDirectory>
2018-01-15 15:09:41 +03:00
# include <KConfigGroup>
2017-07-22 17:59:23 +03:00
# include <KLocalizedString>
2017-07-22 23:15:54 +03:00
# include <KNotification>
2017-07-22 17:59:23 +03:00
2020-04-22 14:09:42 +03:00
2020-04-18 16:12:29 +03:00
enum SessionType
{
DefaultSession = 0 ,
AlternativeSession
} ;
2017-07-02 15:02:07 +03:00
namespace Latte {
2019-05-09 17:57:12 +03:00
namespace Layouts {
2017-07-02 15:02:07 +03:00
Importer : : Importer ( QObject * parent )
: QObject ( parent )
{
2019-05-09 17:12:57 +03:00
m_manager = qobject_cast < Layouts : : Manager * > ( parent ) ;
2017-07-02 15:02:07 +03:00
}
Importer : : ~ Importer ( )
{
}
2017-07-02 20:19:18 +03:00
bool Importer : : updateOldConfiguration ( )
{
2017-07-02 21:22:37 +03:00
QFile oldAppletsFile ( QDir : : homePath ( ) + " /.config/lattedock-appletsrc " ) ;
if ( ! oldAppletsFile . exists ( ) ) {
return false ;
}
2017-07-02 21:02:27 +03:00
//! import standard old configuration and create the relevant layouts
2017-07-02 20:19:18 +03:00
importOldLayout ( QDir : : homePath ( ) + " /.config/lattedock-appletsrc " , i18n ( " My Layout " ) ) ;
importOldLayout ( QDir : : homePath ( ) + " /.config/lattedock-appletsrc " , i18n ( " Alternative " ) , true ) ;
2017-07-02 21:02:27 +03:00
2017-07-03 00:34:59 +03:00
QFile extFile ( QDir : : homePath ( ) + " /.config/lattedockextrc " ) ;
//! import also the old user layouts into the new architecture
if ( extFile . exists ( ) ) {
KSharedConfigPtr extFileConfig = KSharedConfig : : openConfig ( extFile . fileName ( ) ) ;
KConfigGroup externalSettings = KConfigGroup ( extFileConfig , " External " ) ;
QStringList userLayouts = externalSettings . readEntry ( " userLayouts " , QStringList ( ) ) ;
2019-04-04 23:55:44 +03:00
for ( const auto & userConfig : userLayouts ) {
2017-07-03 00:34:59 +03:00
qDebug ( ) < < " user layout : " < < userConfig ;
importOldConfiguration ( userConfig ) ;
}
}
2017-07-03 10:41:59 +03:00
m_manager - > corona ( ) - > universalSettings ( ) - > setVersion ( 2 ) ;
2020-02-23 10:53:04 +03:00
m_manager - > corona ( ) - > universalSettings ( ) - > setCurrentLayoutName ( i18n ( " My Layout " ) ) ;
2017-07-03 10:41:59 +03:00
2017-07-02 21:22:37 +03:00
return true ;
2017-07-02 20:19:18 +03:00
}
2017-07-22 19:49:28 +03:00
bool Importer : : importOldLayout ( QString oldAppletsPath , QString newName , bool alternative , QString exportDirectory )
2017-07-02 20:19:18 +03:00
{
2017-07-22 19:49:28 +03:00
QString newLayoutPath = layoutCanBeImported ( oldAppletsPath , newName , exportDirectory ) ;
2017-07-02 20:19:18 +03:00
qDebug ( ) < < " New Layout Should be created: " < < newLayoutPath ;
KSharedConfigPtr oldFile = KSharedConfig : : openConfig ( oldAppletsPath ) ;
KSharedConfigPtr newFile = KSharedConfig : : openConfig ( newLayoutPath ) ;
KConfigGroup containments = KConfigGroup ( oldFile , " Containments " ) ;
KConfigGroup copiedContainments = KConfigGroup ( newFile , " Containments " ) ;
QList < int > systrays ;
2017-07-02 21:02:27 +03:00
bool atLeastOneContainmentWasFound { false } ;
2017-07-02 20:19:18 +03:00
//! first copy the latte containments that correspond to the correct session
//! and find also the systrays that should be copied also
2019-04-04 23:55:44 +03:00
for ( const auto & containmentId : containments . groupList ( ) ) {
2017-07-02 20:19:18 +03:00
KConfigGroup containmentGroup = containments . group ( containmentId ) ;
QString plugin = containmentGroup . readEntry ( " plugin " , QString ( ) ) ;
2020-04-18 16:12:29 +03:00
SessionType session = ( SessionType ) containmentGroup . readEntry ( " session " , ( int ) DefaultSession ) ;
2017-07-02 20:19:18 +03:00
bool shouldImport = false ;
2020-04-18 16:12:29 +03:00
if ( plugin = = " org.kde.latte.containment " & & session = = DefaultSession & & ! alternative ) {
2017-07-02 20:19:18 +03:00
qDebug ( ) < < containmentId < < " - " < < plugin < < " - " < < session ;
shouldImport = true ;
2020-04-18 16:12:29 +03:00
} else if ( plugin = = " org.kde.latte.containment " & & session = = AlternativeSession & & alternative ) {
2017-07-02 20:19:18 +03:00
qDebug ( ) < < containmentId < < " - " < < plugin < < " - " < < session ;
shouldImport = true ;
}
// this latte containment should be imported
if ( shouldImport ) {
auto applets = containments . group ( containmentId ) . group ( " Applets " ) ;
2019-04-04 23:55:44 +03:00
for ( const auto & applet : applets . groupList ( ) ) {
2017-07-02 20:19:18 +03:00
KConfigGroup appletSettings = applets . group ( applet ) . group ( " Configuration " ) ;
int systrayId = appletSettings . readEntry ( " SystrayContainmentId " , " -1 " ) . toInt ( ) ;
if ( systrayId ! = - 1 ) {
systrays . append ( systrayId ) ;
qDebug ( ) < < " systray was found in the containment... " ;
break ;
}
}
KConfigGroup newContainment = copiedContainments . group ( containmentId ) ;
containmentGroup . copyTo ( & newContainment ) ;
2017-07-02 21:02:27 +03:00
atLeastOneContainmentWasFound = true ;
2017-07-02 20:19:18 +03:00
}
}
2017-07-02 21:02:27 +03:00
//! not even one latte containment was found for that layout so we must break
//! the code here
if ( ! atLeastOneContainmentWasFound ) {
return false ;
}
2017-07-02 20:19:18 +03:00
//! copy also the systrays that were discovered
2019-04-04 23:55:44 +03:00
for ( const auto & containmentId : containments . groupList ( ) ) {
2017-07-02 20:19:18 +03:00
int cId = containmentId . toInt ( ) ;
if ( systrays . contains ( cId ) ) {
KConfigGroup containmentGroup = containments . group ( containmentId ) ;
KConfigGroup newContainment = copiedContainments . group ( containmentId ) ;
containmentGroup . copyTo ( & newContainment ) ;
}
}
copiedContainments . sync ( ) ;
2017-07-02 21:02:27 +03:00
KConfigGroup oldGeneralSettings = KConfigGroup ( oldFile , " General " ) ;
2017-07-24 17:47:53 +03:00
QStringList layoutLaunchers ;
2017-07-02 21:02:27 +03:00
if ( ! alternative ) {
2017-07-24 17:47:53 +03:00
layoutLaunchers = oldGeneralSettings . readEntry ( " globalLaunchers_default " , QStringList ( ) ) ;
2017-07-02 21:02:27 +03:00
} else {
2017-07-24 17:47:53 +03:00
layoutLaunchers = oldGeneralSettings . readEntry ( " globalLaunchers_alternative " , QStringList ( ) ) ;
2017-07-02 21:02:27 +03:00
}
//! update also the layout settings correctly
2019-04-06 18:56:24 +03:00
Layout : : AbstractLayout newLayout ( this , newLayoutPath , newName ) ;
2017-07-02 21:02:27 +03:00
newLayout . setVersion ( 2 ) ;
2017-07-24 17:47:53 +03:00
newLayout . setLaunchers ( layoutLaunchers ) ;
2017-07-02 21:02:27 +03:00
2019-04-06 18:56:24 +03:00
//newLayout.setShowInMenu(true);
2017-07-17 18:47:48 +03:00
2017-07-04 16:58:08 +03:00
if ( alternative ) {
newLayout . setColor ( " purple " ) ;
2017-07-22 19:49:28 +03:00
} else {
newLayout . setColor ( " blue " ) ;
2017-07-04 16:58:08 +03:00
}
2017-07-02 20:19:18 +03:00
return true ;
}
2019-03-23 18:49:09 +03:00
QStringList Importer : : standardPaths ( bool localfirst )
2019-03-16 14:44:00 +03:00
{
QStringList paths = QStandardPaths : : standardLocations ( QStandardPaths : : GenericDataLocation ) ;
2019-03-23 18:49:09 +03:00
if ( localfirst ) {
return paths ;
} else {
QStringList reversed ;
for ( int i = paths . count ( ) - 1 ; i > = 0 ; i - - ) {
reversed < < paths [ i ] ;
}
return reversed ;
}
}
2019-06-17 16:29:05 +03:00
QStringList Importer : : standardPathsFor ( QString subPath , bool localfirst )
{
QStringList paths = standardPaths ( localfirst ) ;
QString separator = subPath . startsWith ( " / " ) ? " " : " / " ;
for ( int i = 0 ; i < paths . count ( ) ; + + i ) {
paths [ i ] = paths [ i ] + separator + subPath ;
}
return paths ;
}
2019-03-23 18:49:09 +03:00
QString Importer : : standardPath ( QString subPath , bool localfirst )
{
QStringList paths = standardPaths ( localfirst ) ;
2019-07-22 09:49:04 +03:00
for ( const auto & pt : paths ) {
QString ptF = pt + " / " + subPath ;
if ( QFileInfo ( ptF ) . exists ( ) ) {
return ptF ;
2019-03-16 14:44:00 +03:00
}
}
//! in any case that above fails
if ( QFileInfo ( " /usr/share/ " + subPath ) . exists ( ) ) {
return " /usr/share/ " + subPath ;
}
return " " ;
}
2017-07-22 19:49:28 +03:00
QString Importer : : layoutCanBeImported ( QString oldAppletsPath , QString newName , QString exportDirectory )
2017-07-02 20:19:18 +03:00
{
QFile oldAppletsrc ( oldAppletsPath ) ;
2018-08-30 08:47:53 +03:00
//! old file doesn't exist
2017-07-02 20:19:18 +03:00
if ( ! oldAppletsrc . exists ( ) ) {
return QString ( ) ;
}
2017-07-04 21:57:10 +03:00
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( oldAppletsPath ) ;
KConfigGroup m_layoutGroup = KConfigGroup ( lConfig , " LayoutSettings " ) ;
int layoutVersion = m_layoutGroup . readEntry ( " version " , 1 ) ;
2017-07-02 20:19:18 +03:00
//! old file layout appears to not be old as its version is >=2
2017-07-04 21:57:10 +03:00
if ( layoutVersion > = 2 ) {
2017-07-02 20:19:18 +03:00
return QString ( ) ;
}
2017-07-22 19:49:28 +03:00
QDir layoutDir ( exportDirectory . isNull ( ) ? QDir : : homePath ( ) + " /.config/latte " : exportDirectory ) ;
2017-07-02 20:19:18 +03:00
2017-07-22 19:49:28 +03:00
if ( ! layoutDir . exists ( ) & & exportDirectory . isNull ( ) ) {
2017-07-02 20:19:18 +03:00
QDir ( QDir : : homePath ( ) + " /.config " ) . mkdir ( " latte " ) ;
}
//! set up the new layout name
if ( newName . isEmpty ( ) ) {
int extension = oldAppletsrc . fileName ( ) . lastIndexOf ( " .latterc " ) ;
if ( extension > 0 ) {
//! remove the last 8 characters that contain the extension
newName = oldAppletsrc . fileName ( ) . remove ( extension , 8 ) ;
} else {
newName = oldAppletsrc . fileName ( ) ;
}
}
2017-07-04 21:57:10 +03:00
QString newLayoutPath = layoutDir . absolutePath ( ) + " / " + newName + " .layout.latte " ;
QFile newLayoutFile ( newLayoutPath ) ;
2017-07-02 20:19:18 +03:00
QStringList filter ;
filter . append ( QString ( newName + " *.layout.latte " ) ) ;
QStringList files = layoutDir . entryList ( filter , QDir : : Files | QDir : : NoSymLinks ) ;
2018-08-30 08:47:53 +03:00
//! if the newLayout already exists provide a newName that doesn't
2017-07-02 20:19:18 +03:00
if ( files . count ( ) > = 1 ) {
int newCounter = files . count ( ) + 1 ;
newLayoutPath = layoutDir . absolutePath ( ) + " / " + newName + " - " + QString : : number ( newCounter ) + " .layout.latte " ;
}
return newLayoutPath ;
}
2017-07-03 00:34:59 +03:00
bool Importer : : importOldConfiguration ( QString oldConfigPath , QString newName )
{
QFile oldConfigFile ( oldConfigPath ) ;
if ( ! oldConfigFile . exists ( ) ) {
return false ;
}
KTar archive ( oldConfigPath , QStringLiteral ( " application/x-tar " ) ) ;
archive . open ( QIODevice : : ReadOnly ) ;
if ( ! archive . isOpen ( ) ) {
return false ;
}
auto rootDir = archive . directory ( ) ;
QTemporaryDir uniqueTempDir ;
QDir tempDir { uniqueTempDir . path ( ) } ;
qDebug ( ) < < " temp layout directory : " < < tempDir . absolutePath ( ) ;
if ( rootDir ) {
if ( ! tempDir . exists ( ) )
tempDir . mkpath ( tempDir . absolutePath ( ) ) ;
2019-04-04 23:55:44 +03:00
for ( const auto & name : rootDir - > entries ( ) ) {
2017-07-03 00:34:59 +03:00
auto fileEntry = rootDir - > file ( name ) ;
if ( fileEntry & & ( fileEntry - > name ( ) = = " lattedockrc "
| | fileEntry - > name ( ) = = " lattedock-appletsrc " ) ) {
if ( ! fileEntry - > copyTo ( tempDir . absolutePath ( ) ) ) {
2017-09-08 21:29:33 +03:00
qInfo ( ) < < i18nc ( " import/export config " , " The extracted file could not be copied!!! " ) ;
2017-07-03 00:34:59 +03:00
archive . close ( ) ;
return false ;
}
} else {
qInfo ( ) < < i18nc ( " import/export config " , " The file has a wrong format!!! " ) ;
archive . close ( ) ;
return false ;
}
}
} else {
2017-09-08 21:29:33 +03:00
qInfo ( ) < < i18nc ( " import/export config " , " The temp directory could not be created!!! " ) ;
2017-07-03 00:34:59 +03:00
archive . close ( ) ;
return false ;
}
//! only if the above has passed we must process the files
QString appletsPath ( tempDir . absolutePath ( ) + " /lattedock-appletsrc " ) ;
QString screensPath ( tempDir . absolutePath ( ) + " /lattedockrc " ) ;
if ( ! QFile ( appletsPath ) . exists ( ) | | ! QFile ( screensPath ) . exists ( ) ) {
return false ;
}
if ( newName . isEmpty ( ) ) {
int lastSlash = oldConfigPath . lastIndexOf ( " / " ) ;
newName = oldConfigPath . remove ( 0 , lastSlash + 1 ) ;
int ext = newName . lastIndexOf ( " .latterc " ) ;
newName = newName . remove ( ext , 8 ) ;
}
if ( ! importOldLayout ( appletsPath , newName ) ) {
return false ;
}
//! the old configuration contains also screen values, these must be updated also
KSharedConfigPtr oldScreensConfig = KSharedConfig : : openConfig ( screensPath ) ;
KConfigGroup m_screensGroup = KConfigGroup ( oldScreensConfig , " ScreenConnectors " ) ;
//restore the known ids to connector mappings
2019-04-04 23:55:44 +03:00
for ( const QString & key : m_screensGroup . keyList ( ) ) {
2017-07-03 00:34:59 +03:00
QString connector = m_screensGroup . readEntry ( key , QString ( ) ) ;
int id = key . toInt ( ) ;
if ( id > = 10 & & ! m_manager - > corona ( ) - > screenPool ( ) - > knownIds ( ) . contains ( id ) ) {
m_manager - > corona ( ) - > screenPool ( ) - > insertScreenMapping ( id , connector ) ;
}
}
return true ;
}
2017-07-22 22:52:31 +03:00
bool Importer : : exportFullConfiguration ( QString file )
{
if ( QFile : : exists ( file ) & & ! QFile : : remove ( file ) ) {
return false ;
}
KTar archive ( file , QStringLiteral ( " application/x-tar " ) ) ;
if ( ! archive . open ( QIODevice : : WriteOnly ) ) {
return false ;
}
archive . addLocalFile ( QString ( QDir : : homePath ( ) + " /.config/lattedockrc " ) , QStringLiteral ( " lattedockrc " ) ) ;
2018-02-01 01:42:06 +03:00
2019-04-04 23:55:44 +03:00
for ( const auto & layoutName : availableLayouts ( ) ) {
2018-02-01 01:42:06 +03:00
archive . addLocalFile ( layoutFilePath ( layoutName ) , QString ( " latte/ " + layoutName + " .layout.latte " ) ) ;
}
//archive.addLocalDirectory(QString(QDir::homePath() + "/.config/latte"), QStringLiteral("latte"));
2017-07-22 22:52:31 +03:00
archive . close ( ) ;
return true ;
}
2017-07-22 17:59:23 +03:00
Importer : : LatteFileVersion Importer : : fileVersion ( QString file )
{
if ( ! QFile : : exists ( file ) )
return UnknownFileType ;
if ( file . endsWith ( " .layout.latte " ) ) {
2017-12-17 20:08:35 +03:00
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( QFileInfo ( file ) . absoluteFilePath ( ) ) ;
2017-07-22 17:59:23 +03:00
KConfigGroup layoutGroup = KConfigGroup ( lConfig , " LayoutSettings " ) ;
int version = layoutGroup . readEntry ( " version " , 1 ) ;
if ( version = = 2 )
return Importer : : LayoutVersion2 ;
else
return Importer : : UnknownFileType ;
}
if ( ! file . endsWith ( " .latterc " ) ) {
return Importer : : UnknownFileType ;
}
KTar archive ( file , QStringLiteral ( " application/x-tar " ) ) ;
archive . open ( QIODevice : : ReadOnly ) ;
//! if the file isnt a tar archive
if ( ! archive . isOpen ( ) ) {
return Importer : : UnknownFileType ;
}
QTemporaryDir archiveTempDir ;
bool version1rc = false ;
bool version1applets = false ;
bool version2rc = false ;
bool version2LatteDir = false ;
bool version2layout = false ;
2017-07-22 22:52:31 +03:00
archive . directory ( ) - > copyTo ( archiveTempDir . path ( ) ) ;
2017-07-22 17:59:23 +03:00
2017-07-22 22:52:31 +03:00
//rc file
QString rcFile ( archiveTempDir . path ( ) + " /lattedockrc " ) ;
if ( QFile ( rcFile ) . exists ( ) ) {
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( rcFile ) ;
KConfigGroup universalGroup = KConfigGroup ( lConfig , " UniversalSettings " ) ;
int version = universalGroup . readEntry ( " version " , 1 ) ;
if ( version = = 1 ) {
version1rc = true ;
} else if ( version = = 2 ) {
version2rc = true ;
}
}
//applets file
QString appletsFile ( archiveTempDir . path ( ) + " /lattedock-appletsrc " ) ;
if ( QFile ( appletsFile ) . exists ( ) & & version1rc ) {
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( appletsFile ) ;
KConfigGroup generalGroup = KConfigGroup ( lConfig , " LayoutSettings " ) ;
int version = generalGroup . readEntry ( " version " , 1 ) ;
if ( version = = 1 ) {
version1applets = true ;
} else if ( version = = 2 ) {
version2layout = true ;
2017-07-22 17:59:23 +03:00
}
}
2017-07-22 22:52:31 +03:00
//latte directory
QString latteDir ( archiveTempDir . path ( ) + " /latte " ) ;
if ( QDir ( latteDir ) . exists ( ) ) {
version2LatteDir = true ;
}
2020-03-14 15:41:07 +03:00
if ( version1applets ) {
2017-07-22 17:59:23 +03:00
return ConfigVersion1 ;
} else if ( version2rc & & version2LatteDir ) {
return ConfigVersion2 ;
}
return Importer : : UnknownFileType ;
}
2017-07-22 23:15:54 +03:00
bool Importer : : importHelper ( QString fileName )
{
LatteFileVersion version = fileVersion ( fileName ) ;
if ( ( version ! = ConfigVersion1 ) & & ( version ! = ConfigVersion2 ) ) {
return false ;
}
KTar archive ( fileName , QStringLiteral ( " application/x-tar " ) ) ;
archive . open ( QIODevice : : ReadOnly ) ;
if ( ! archive . isOpen ( ) ) {
return false ;
}
QString latteDirPath ( QDir : : homePath ( ) + " /.config/latte " ) ;
QDir latteDir ( latteDirPath ) ;
if ( latteDir . exists ( ) ) {
latteDir . removeRecursively ( ) ;
}
archive . directory ( ) - > copyTo ( QString ( QDir : : homePath ( ) + " /.config " ) ) ;
return true ;
}
2017-12-17 20:08:35 +03:00
QString Importer : : importLayoutHelper ( QString fileName )
{
LatteFileVersion version = fileVersion ( fileName ) ;
if ( version ! = LayoutVersion2 ) {
return QString ( ) ;
}
2019-04-06 18:56:24 +03:00
QString newLayoutName = Layout : : AbstractLayout : : layoutName ( fileName ) ;
2017-12-17 20:08:35 +03:00
newLayoutName = uniqueLayoutName ( newLayoutName ) ;
2018-04-14 21:12:32 +03:00
QString newPath = QDir : : homePath ( ) + " /.config/latte/ " + newLayoutName + " .layout.latte " ;
QFile ( fileName ) . copy ( newPath ) ;
QFileInfo newFileInfo ( newPath ) ;
if ( newFileInfo . exists ( ) & & ! newFileInfo . isWritable ( ) ) {
QFile ( newPath ) . setPermissions ( QFileDevice : : ReadUser | QFileDevice : : WriteUser | QFileDevice : : ReadGroup | QFileDevice : : ReadOther ) ;
}
2017-12-17 20:08:35 +03:00
return newLayoutName ;
}
2017-12-17 19:13:26 +03:00
QStringList Importer : : availableLayouts ( )
{
QDir layoutDir ( QDir : : homePath ( ) + " /.config/latte " ) ;
QStringList filter ;
filter . append ( QString ( " *.layout.latte " ) ) ;
QStringList files = layoutDir . entryList ( filter , QDir : : Files | QDir : : NoSymLinks ) ;
QStringList layoutNames ;
2019-04-04 23:55:44 +03:00
for ( const auto & file : files ) {
2019-04-06 18:56:24 +03:00
layoutNames . append ( Layout : : AbstractLayout : : layoutName ( file ) ) ;
2017-12-17 19:13:26 +03:00
}
return layoutNames ;
}
2017-07-22 23:15:54 +03:00
2017-07-22 19:49:28 +03:00
QString Importer : : nameOfConfigFile ( const QString & fileName )
{
int lastSlash = fileName . lastIndexOf ( " / " ) ;
QString tempLayoutFile = fileName ;
QString layoutName = tempLayoutFile . remove ( 0 , lastSlash + 1 ) ;
int ext = layoutName . lastIndexOf ( " .latterc " ) ;
layoutName = layoutName . remove ( ext , 8 ) ;
return layoutName ;
}
2017-12-17 18:42:49 +03:00
bool Importer : : layoutExists ( QString layoutName )
{
2018-01-15 15:09:41 +03:00
return QFile : : exists ( layoutFilePath ( layoutName ) ) ;
}
QString Importer : : layoutFilePath ( QString layoutName )
{
return QString ( QDir : : homePath ( ) + " /.config/latte/ " + layoutName + " .layout.latte " ) ;
2017-12-17 18:42:49 +03:00
}
QString Importer : : uniqueLayoutName ( QString name )
{
2020-03-16 13:59:50 +03:00
int pos_ = name . lastIndexOf ( QRegExp ( QString ( " - [0-9]+ " ) ) ) ;
2017-12-17 18:42:49 +03:00
if ( layoutExists ( name ) & & pos_ > 0 ) {
name = name . left ( pos_ ) ;
}
int i = 2 ;
QString namePart = name ;
while ( layoutExists ( name ) ) {
2020-03-16 13:59:50 +03:00
name = namePart + " - " + QString : : number ( i ) ;
2017-12-17 18:42:49 +03:00
i + + ;
}
return name ;
}
2018-01-15 15:09:41 +03:00
QStringList Importer : : checkRepairMultipleLayoutsLinkedFile ( )
{
2019-04-06 18:56:24 +03:00
QString linkedFilePath = QDir : : homePath ( ) + " /.config/latte/ " + Layout : : AbstractLayout : : MultipleLayoutsName + " .layout.latte " ;
2018-01-15 15:09:41 +03:00
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( linkedFilePath ) ;
KConfigGroup linkedContainments = KConfigGroup ( filePtr , " Containments " ) ;
//! layoutName and its Containments
QHash < QString , QStringList > linkedLayoutContainmentGroups ;
2019-04-04 23:55:44 +03:00
for ( const auto & cId : linkedContainments . groupList ( ) ) {
2018-01-15 15:09:41 +03:00
QString layoutName = linkedContainments . group ( cId ) . readEntry ( " layoutId " , QString ( ) ) ;
if ( ! layoutName . isEmpty ( ) ) {
qDebug ( ) < < layoutName ;
linkedLayoutContainmentGroups [ layoutName ] . append ( cId ) ;
linkedContainments . group ( cId ) . writeEntry ( " layoutId " , QString ( ) ) ;
}
}
QStringList updatedLayouts ;
2019-04-04 23:55:44 +03:00
for ( const auto & layoutName : linkedLayoutContainmentGroups . uniqueKeys ( ) ) {
2019-04-06 18:56:24 +03:00
if ( layoutName ! = Layout : : AbstractLayout : : MultipleLayoutsName & & layoutExists ( layoutName ) ) {
2018-01-15 15:09:41 +03:00
updatedLayouts < < layoutName ;
KSharedConfigPtr layoutFilePtr = KSharedConfig : : openConfig ( layoutFilePath ( layoutName ) ) ;
KConfigGroup origLayoutContainments = KConfigGroup ( layoutFilePtr , " Containments " ) ;
//Clear old containments
origLayoutContainments . deleteGroup ( ) ;
//Update containments
2019-04-04 23:55:44 +03:00
for ( const auto & cId : linkedLayoutContainmentGroups [ layoutName ] ) {
2018-01-15 15:09:41 +03:00
KConfigGroup newContainment = origLayoutContainments . group ( cId ) ;
linkedContainments . group ( cId ) . copyTo ( & newContainment ) ;
linkedContainments . group ( cId ) . deleteGroup ( ) ;
}
origLayoutContainments . sync ( ) ;
}
}
//! clear all remaining ghost containments
2019-04-04 23:55:44 +03:00
for ( const auto & cId : linkedContainments . groupList ( ) ) {
2018-01-15 15:09:41 +03:00
linkedContainments . group ( cId ) . deleteGroup ( ) ;
}
linkedContainments . sync ( ) ;
return updatedLayouts ;
}
2017-07-02 15:02:07 +03:00
}
2019-05-09 17:57:12 +03:00
}