Warning, file /office/calligra/libs/main/Calligra2Migration.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  *   Copyright (C) 2016 Dag Andersen <danders@get2net.dk>
0003  * 
0004  *   This library is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU Library General Public
0006  *   License as published by the Free Software Foundation; either
0007  *   version 2 of the License, or (at your option) any later version.
0008  * 
0009  *   This library is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *   Library General Public License for more details.
0013  * 
0014  *   You should have received a copy of the GNU Library General Public License
0015  *   along with this library; see the file COPYING.LIB.  If not, write to
0016  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  *   Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "Calligra2Migration.h"
0021 
0022 #include "KoResourcePaths.h"
0023 
0024 #include <kdelibs4configmigrator.h>
0025 #include <kdelibs4migration.h>
0026 #include <QStandardPaths>
0027 #include <QDir>
0028 #include <QPluginLoader>
0029 #include <QLoggingCategory>
0030 #include <QDebug>
0031 
0032 Q_DECLARE_LOGGING_CATEGORY(CALLIGRA2MIGRATION)
0033 Q_LOGGING_CATEGORY(CALLIGRA2MIGRATION, "calligra.lib.migration")
0034 
0035 Calligra2Migration::Calligra2Migration(const QString &appName, const QString &oldAppName)
0036     : m_newAppName(appName)
0037     , m_oldAppName(oldAppName)
0038 {
0039     qCDebug(CALLIGRA2MIGRATION)<<appName<<oldAppName;
0040 }
0041 
0042 void Calligra2Migration::setConfigFiles(const QStringList &configFiles)
0043 {
0044     m_configFiles = configFiles;
0045 }
0046 
0047 void Calligra2Migration::setUiFiles(const QStringList &uiFiles)
0048 {
0049     m_uiFiles = uiFiles;
0050 }
0051 
0052 void Calligra2Migration::migrate()
0053 {
0054     const QString newdatapath = KoResourcePaths::saveLocation("data", m_newAppName, false);
0055     QDir newdatadir(newdatapath);
0056     if (newdatadir.exists()) {
0057         // assume we have migrated
0058         qCDebug(CALLIGRA2MIGRATION)<<"migration has been done";
0059         return;
0060     }
0061 
0062     // do common calligra in case not yet migrated
0063     Kdelibs4ConfigMigrator m("calligra");
0064     m.setConfigFiles(QStringList() << QStringLiteral("calligrarc"));
0065     m.setUiFiles(QStringList() << QStringLiteral("calligra_shell.rc") << QStringLiteral("osx.stylesheet"));
0066     m.migrate();
0067 
0068     bool didSomething = false;
0069     Kdelibs4ConfigMigrator cm(m_oldAppName.isEmpty() ? m_newAppName : m_oldAppName);
0070     cm.setConfigFiles(m_configFiles);
0071     cm.setUiFiles(m_uiFiles);
0072     if (cm.migrate() && !m_oldAppName.isEmpty()) {
0073         // rename config files to new names
0074         qCDebug(CALLIGRA2MIGRATION)<<"rename config files to new names"<<m_configFiles;
0075         for (const QString &oldname : m_configFiles) {
0076             QString newname = oldname;
0077             newname.replace(m_oldAppName, m_newAppName);
0078             if (oldname == newname) {
0079                 continue;
0080             }
0081             QString oldfile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, oldname);
0082             if (!oldfile.isEmpty()) {
0083                 qCDebug(CALLIGRA2MIGRATION)<<"config rename:"<<oldfile;
0084                 QFile f(oldfile);
0085                 QFileInfo fi(f);
0086                 f.rename(fi.absolutePath() + '/' + newname);
0087                 didSomething = true;
0088                 qCDebug(CALLIGRA2MIGRATION)<<"config renamed:"<<f.fileName();
0089             }
0090         }
0091         // subdirectory must be renamed
0092         // eg: .local/share/kxmlgui5/ + m_oldAppName
0093         // rename to: .local/share/kxmlgui5/ + m_newAppName
0094         const QString loc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kxmlgui5/");
0095         const QString oldui = loc + m_oldAppName;
0096         const QString newui = loc + m_newAppName;
0097         qCDebug(CALLIGRA2MIGRATION)<<"rename ui dir:"<<oldui;
0098         QDir newdir(newui);
0099         if (!newdir.exists()) {
0100             newdir.rename(oldui, newui);
0101             didSomething = true;
0102             qCDebug(CALLIGRA2MIGRATION)<<"renamed ui dir:"<<newui;
0103         }
0104         qCDebug(CALLIGRA2MIGRATION)<<"rename ui files to new names"<<m_uiFiles;
0105         for (const QString &oldname : m_uiFiles) {
0106             QString newname = oldname;
0107             newname.replace(m_oldAppName, m_newAppName);
0108             if (oldname == newname) {
0109                 continue;
0110             }
0111             const QString oldfile = newui + QLatin1Char('/') + oldname;
0112             QFile f(oldfile);
0113             if (f.exists()) {
0114                 const QString newfile = newui + QLatin1Char('/') + newname;
0115                 f.rename(newfile);
0116                 didSomething = true;
0117                 QFileInfo fi(f);
0118                 qCDebug(CALLIGRA2MIGRATION)<<"ui renamed:"<<oldfile<<"->"<<fi.filePath();
0119             }
0120         }
0121     }
0122 
0123     // migrate data dir, must be done after ui files
0124     Kdelibs4Migration md;
0125     if (md.kdeHomeFound()) {
0126         const QString oldpath = md.saveLocation("data", m_oldAppName.isEmpty() ? m_newAppName : m_oldAppName);
0127         if (!oldpath.isEmpty()) {
0128             qCDebug(CALLIGRA2MIGRATION)<<"old data:"<<oldpath;
0129             newdatadir.rename(oldpath, newdatapath);
0130             qCDebug(CALLIGRA2MIGRATION)<<"renamed data:"<<newdatapath;
0131         }
0132     } else {
0133         qCWarning(CALLIGRA2MIGRATION)<<"kde home not found";
0134     }
0135 
0136     // Copied from Kdelibs4ConfigMigrator:
0137     // Trigger KSharedConfig::openConfig()->reparseConfiguration() via the framework integration plugin
0138     if (didSomething) {
0139         qCDebug(CALLIGRA2MIGRATION)<<"reparse configuration";
0140         QPluginLoader lib(QStringLiteral("kf5/FrameworkIntegrationPlugin"));
0141         QObject *rootObj = lib.instance();
0142         if (rootObj) {
0143             QMetaObject::invokeMethod(rootObj, "reparseConfiguration");
0144         }
0145     }
0146 }