File indexing completed on 2024-05-12 05:25:34
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "utils.h" 0008 0009 #include <MailCommon/MailUtil> 0010 #include <PimCommon/PimUtil> 0011 0012 #include <QSettings> 0013 0014 #include <KLocalizedString> 0015 #include <KZip> 0016 #include <QTemporaryFile> 0017 0018 #include <Akonadi/AgentManager> 0019 #include <QStandardPaths> 0020 0021 QString Utils::storeAddressbook() 0022 { 0023 return QStringLiteral("backupaddressbook/"); 0024 } 0025 0026 QString Utils::storeAlarm() 0027 { 0028 return QStringLiteral("backupalarm/"); 0029 } 0030 0031 QString Utils::storeCalendar() 0032 { 0033 return QStringLiteral("backupcalendar/"); 0034 } 0035 0036 QString Utils::backupnote() 0037 { 0038 return QStringLiteral("backupnote/"); 0039 } 0040 0041 QString Utils::storeMails() 0042 { 0043 return QStringLiteral("backupmail/"); 0044 } 0045 0046 QString Utils::exportDataTypeFileName() 0047 { 0048 return QStringLiteral("exportdatatype.xml"); 0049 } 0050 0051 int Utils::currentArchiveVersion() 0052 { 0053 // Increase it when we add major feature! 0054 return 2; 0055 } 0056 0057 QString Utils::transportsPath() 0058 { 0059 return QStringLiteral("transports/"); 0060 } 0061 0062 QString Utils::resourcesPath() 0063 { 0064 return QStringLiteral("resources/"); 0065 } 0066 0067 QString Utils::identitiesPath() 0068 { 0069 return QStringLiteral("identities/"); 0070 } 0071 0072 QString Utils::mailsPath() 0073 { 0074 return QStringLiteral("mails/"); 0075 } 0076 0077 QString Utils::configsPath() 0078 { 0079 return QStringLiteral("configs/"); 0080 } 0081 0082 QString Utils::akonadiPath() 0083 { 0084 return QStringLiteral("akonadi/"); 0085 } 0086 0087 QString Utils::dataPath() 0088 { 0089 return QStringLiteral("data/"); 0090 } 0091 0092 QString Utils::calendarPath() 0093 { 0094 return QStringLiteral("calendar/"); 0095 } 0096 0097 QString Utils::addressbookPath() 0098 { 0099 return QStringLiteral("addressbook/"); 0100 } 0101 0102 QString Utils::alarmPath() 0103 { 0104 return QStringLiteral("alarm/"); 0105 } 0106 0107 QString Utils::notePath() 0108 { 0109 return QStringLiteral("note/"); 0110 } 0111 0112 QString Utils::prefixAkonadiConfigFile() 0113 { 0114 return QStringLiteral("agent_config_"); 0115 } 0116 0117 QString Utils::infoPath() 0118 { 0119 return QStringLiteral("information/"); 0120 } 0121 0122 QString Utils::akonadiAgentName(const QString &configPath) 0123 { 0124 QSettings settings(configPath, QSettings::IniFormat); 0125 const QString name = settings.value(QStringLiteral("Agent/Name")).toString(); 0126 return name; 0127 } 0128 0129 KZip *Utils::openZip(const QString &filename, QString &errorMsg) 0130 { 0131 auto zip = new KZip(filename); 0132 const bool result = zip->open(QIODevice::ReadOnly); 0133 if (!result) { 0134 errorMsg = i18n("Archive cannot be opened in read mode."); 0135 qCWarning(PIMDATAEXPORTERCORE_LOG) << "Impossible to open archive: " << filename << "zip error : " << zip->errorString(); 0136 delete zip; 0137 return nullptr; 0138 } 0139 return zip; 0140 } 0141 0142 void Utils::storeDataExportInfo(KZip *archive, const QString &exportInfoFileName) 0143 { 0144 if (!exportInfoFileName.isEmpty()) { 0145 const bool fileAdded = archive->addLocalFile(exportInfoFileName, Utils::infoPath() + Utils::exportDataTypeFileName()); 0146 if (!fileAdded) { 0147 qCDebug(PIMDATAEXPORTERCORE_LOG) << "storeDataExportInfo can't add to archive" << Utils::infoPath() + Utils::exportDataTypeFileName(); 0148 } 0149 } 0150 } 0151 0152 void Utils::addVersion(KZip *archive) 0153 { 0154 QTemporaryFile tmp; 0155 tmp.open(); 0156 const bool fileAdded = archive->addLocalFile(tmp.fileName(), Utils::infoPath() + QStringLiteral("VERSION_%1").arg(currentArchiveVersion())); 0157 if (!fileAdded) { 0158 // TODO add i18n ? 0159 qCDebug(PIMDATAEXPORTERCORE_LOG) << "version file can not add to archive"; 0160 } 0161 } 0162 0163 int Utils::archiveVersion(KZip *archive) 0164 { 0165 const KArchiveEntry *informationFile = archive->directory()->entry(Utils::infoPath() + QStringLiteral("VERSION_2")); 0166 if (informationFile && informationFile->isFile()) { 0167 return 2; 0168 } 0169 informationFile = archive->directory()->entry(Utils::infoPath() + QStringLiteral("VERSION_1")); 0170 if (informationFile && informationFile->isFile()) { 0171 return 1; 0172 } 0173 // TODO add more version when new version 0174 return 0; 0175 } 0176 0177 QString Utils::appTypeToI18n(AppsType type) 0178 { 0179 switch (type) { 0180 case KMail: 0181 return i18n("KMail"); 0182 case KAddressBook: 0183 return i18n("KAddressBook"); 0184 case KAlarm: 0185 return i18n("KAlarm"); 0186 case KOrganizer: 0187 return i18n("KOrganizer"); 0188 case KNotes: 0189 return i18n("KNotes"); 0190 case Akregator: 0191 return i18n("Akregator"); 0192 case Unknown: 0193 break; 0194 } 0195 qCDebug(PIMDATAEXPORTERCORE_LOG) << " type unknown " << type; 0196 return {}; 0197 } 0198 0199 QString Utils::storedTypeToI18n(StoredType type) 0200 { 0201 switch (type) { 0202 case None: 0203 return {}; 0204 case Identity: 0205 return i18n("Identity"); 0206 case Mails: 0207 return i18n("Mails"); 0208 case MailTransport: 0209 return i18n("Mail Transport"); 0210 case Resources: 0211 return i18n("Resources"); 0212 case Config: 0213 return i18n("Config"); 0214 case Data: 0215 return i18n("Data"); 0216 } 0217 qCDebug(PIMDATAEXPORTERCORE_LOG) << " type unknown " << type; 0218 return {}; 0219 } 0220 0221 QList<Utils::AkonadiInstanceInfo> Utils::listOfResource() 0222 { 0223 QList<Utils::AkonadiInstanceInfo> instanceInfoList; 0224 Akonadi::AgentManager *manager = Akonadi::AgentManager::self(); 0225 const Akonadi::AgentInstance::List list = manager->instances(); 0226 instanceInfoList.reserve(list.count()); 0227 for (const Akonadi::AgentInstance &agent : list) { 0228 Utils::AkonadiInstanceInfo info; 0229 info.identifier = agent.identifier(); 0230 info.mimeTypes = agent.type().mimeTypes(); 0231 info.capabilities = agent.type().capabilities(); 0232 instanceInfoList.append(info); 0233 } 0234 return instanceInfoList; 0235 } 0236 0237 QString Utils::resourceNoteArchiveName() 0238 { 0239 return QStringLiteral("notes.zip"); 0240 } 0241 0242 QString Utils::resourceAddressbookArchiveName() 0243 { 0244 return QStringLiteral("addressbook.zip"); 0245 } 0246 0247 QString Utils::resourceAlarmArchiveName() 0248 { 0249 return QStringLiteral("alarm.zip"); 0250 } 0251 0252 QString Utils::resourceCalendarArchiveName() 0253 { 0254 return QStringLiteral("calendar.zip"); 0255 } 0256 0257 QString Utils::resourceMailArchiveName() 0258 { 0259 return QStringLiteral("mail.zip"); 0260 }