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
# include "layoutmanager.h"
2017-07-02 20:19:18 +03:00
# include "layoutsettings.h"
2017-07-03 00:34:59 +03:00
# include "screenpool.h"
2017-07-02 20:19:18 +03:00
# include "../liblattedock/dock.h"
# include <QFile>
2017-07-03 00:34:59 +03:00
# include <QTemporaryDir>
2017-07-02 20:19:18 +03:00
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>
# include <KLocalizedString>
2017-07-22 23:15:54 +03:00
# include <KNotification>
2017-07-22 17:59:23 +03:00
2017-07-02 15:02:07 +03:00
namespace Latte {
Importer : : Importer ( QObject * parent )
: QObject ( parent )
{
2017-07-02 21:02:27 +03:00
m_manager = qobject_cast < LayoutManager * > ( 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 ( ) ) ;
foreach ( auto userConfig , userLayouts ) {
qDebug ( ) < < " user layout : " < < userConfig ;
importOldConfiguration ( userConfig ) ;
}
}
2017-07-03 10:41:59 +03:00
m_manager - > corona ( ) - > universalSettings ( ) - > setCurrentLayoutName ( i18n ( " My Layout " ) ) ;
m_manager - > corona ( ) - > universalSettings ( ) - > setVersion ( 2 ) ;
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
foreach ( auto containmentId , containments . groupList ( ) ) {
KConfigGroup containmentGroup = containments . group ( containmentId ) ;
QString plugin = containmentGroup . readEntry ( " plugin " , QString ( ) ) ;
Dock : : SessionType session = ( Dock : : SessionType ) containmentGroup . readEntry ( " session " , ( int ) Dock : : DefaultSession ) ;
bool shouldImport = false ;
if ( plugin = = " org.kde.latte.containment " & & session = = Dock : : DefaultSession & & ! alternative ) {
qDebug ( ) < < containmentId < < " - " < < plugin < < " - " < < session ;
shouldImport = true ;
} else if ( plugin = = " org.kde.latte.containment " & & session = = Dock : : AlternativeSession & & alternative ) {
qDebug ( ) < < containmentId < < " - " < < plugin < < " - " < < session ;
shouldImport = true ;
}
// this latte containment should be imported
if ( shouldImport ) {
auto applets = containments . group ( containmentId ) . group ( " Applets " ) ;
foreach ( auto applet , applets . groupList ( ) ) {
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
foreach ( auto containmentId , containments . groupList ( ) ) {
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
2017-07-22 19:49:28 +03:00
LayoutSettings 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
2017-07-17 18:47:48 +03:00
newLayout . setShowInMenu ( true ) ;
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 ;
}
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 ) ;
//! old file doesnt exist
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 ) ;
//! if the newLayout already exists provide a newName that doesnt
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 ( ) ) ;
foreach ( auto & name , rootDir - > entries ( ) ) {
auto fileEntry = rootDir - > file ( name ) ;
if ( fileEntry & & ( fileEntry - > name ( ) = = " lattedockrc "
| | fileEntry - > name ( ) = = " lattedock-appletsrc " ) ) {
if ( ! fileEntry - > copyTo ( tempDir . absolutePath ( ) ) ) {
qInfo ( ) < < i18nc ( " import/export config " , " The extracted file coulnd be copied!!! " ) ;
archive . close ( ) ;
return false ;
}
} else {
qInfo ( ) < < i18nc ( " import/export config " , " The file has a wrong format!!! " ) ;
archive . close ( ) ;
return false ;
}
}
} else {
qInfo ( ) < < i18nc ( " import/export config " , " The temp directory couldnt be created!!! " ) ;
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
foreach ( const QString & key , m_screensGroup . keyList ( ) ) {
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 " ) ) ;
archive . addLocalDirectory ( QString ( QDir : : homePath ( ) + " /.config/latte " ) , QStringLiteral ( " latte " ) ) ;
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 " ) ) {
KSharedConfigPtr lConfig = KSharedConfig : : openConfig ( file ) ;
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 ;
}
2017-07-22 17:59:23 +03:00
if ( version1applets & & version1applets ) {
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-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-07-02 15:02:07 +03:00
}