File indexing completed on 2024-04-28 05:27:04
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org> 0003 SPDX-FileCopyrightText: 2003, 2007 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only 0006 */ 0007 0008 #ifndef MIMETYPEDATA_H 0009 #define MIMETYPEDATA_H 0010 0011 #include <QMimeType> 0012 0013 class KConfigGroup; 0014 0015 /** 0016 * This is a non-gui (data) class, that represents a mimetype. 0017 * It is a QMimeType plus the changes we made to it. 0018 */ 0019 class MimeTypeData 0020 { 0021 public: 0022 // Constructor used for groups 0023 explicit MimeTypeData(const QString &major); 0024 // Real constructor, used for an existing mimetype. 0025 explicit MimeTypeData(const QMimeType &mime); 0026 // Real constructor, used for a new mimetype. 0027 explicit MimeTypeData(const QString &mimeName, bool /*unused, just to distinguish from the other QString ctor*/); 0028 0029 QString name() const 0030 { 0031 return m_isGroup ? m_major : m_major + QLatin1Char('/') + m_minor; 0032 } 0033 0034 QString majorType() const 0035 { 0036 return m_major; 0037 } 0038 0039 QString minorType() const 0040 { 0041 return m_minor; 0042 } 0043 0044 void setMinor(const QString &m) 0045 { 0046 m_minor = m; 0047 } 0048 0049 QString comment() const 0050 { 0051 return m_comment; 0052 } 0053 0054 void setComment(const QString &c) 0055 { 0056 m_comment = c; 0057 } 0058 0059 /** 0060 * Returns true if "this" is a group 0061 */ 0062 bool isMeta() const 0063 { 0064 return m_isGroup; 0065 } 0066 0067 /** 0068 * Returns true if the type is essential, i.e. can't be deleted 0069 * (see KMimeType::checkEssentialMimeTypes) 0070 */ 0071 bool isEssential() const; 0072 QString icon() const; 0073 void setUserSpecifiedIcon(const QString &icon); 0074 QStringList patterns() const 0075 { 0076 return m_patterns; 0077 } 0078 0079 void setPatterns(const QStringList &p); 0080 QStringList appServices() const; 0081 void setAppServices(const QStringList &dsl); 0082 QStringList embedParts() const; 0083 void setEmbedParts(const QStringList &dsl); 0084 0085 enum AutoEmbed { 0086 Yes = 0, 0087 No = 1, 0088 UseGroupSetting = 2, 0089 }; 0090 AutoEmbed autoEmbed() const 0091 { 0092 return m_autoEmbed; 0093 } 0094 0095 void setAutoEmbed(AutoEmbed a) 0096 { 0097 m_autoEmbed = a; 0098 } 0099 0100 const QMimeType &mimeType() const 0101 { 0102 return m_mimetype; 0103 } 0104 0105 bool canUseGroupSetting() const; 0106 0107 void getAskSave(bool &); 0108 void setAskSave(bool); 0109 0110 /** 0111 * Returns true if the mimetype data has any unsaved changes. 0112 */ 0113 bool isDirty() const; 0114 0115 /** 0116 * Returns true if the mimetype data has any unsaved changes in the service list. 0117 */ 0118 bool isServiceListDirty() const; 0119 0120 /** 0121 * Save changes to disk. 0122 * Does not check isDirty(), so the common idiom is if (data.isDirty()) { needUpdate = data.sync(); } 0123 * Returns true if update-mime-database needs to be run afterwards 0124 */ 0125 bool sync(); 0126 /** 0127 * Update m_mimetype from the xml when Apply is pressed 0128 */ 0129 void refresh(); 0130 0131 /** 0132 * Return true if this is a new mimetype, i.e. one that is not yet on disk 0133 */ 0134 bool isNew() const 0135 { 0136 return m_bNewItem; 0137 } 0138 0139 /** 0140 * Helper method for the filtering in the listview 0141 */ 0142 bool matchesFilter(const QString &filter) const; 0143 0144 private: 0145 void initFromQMimeType(); 0146 AutoEmbed readAutoEmbed() const; 0147 void writeAutoEmbed(); 0148 bool isMimeTypeDirty() const; // whether the mimetype definition file needs saving 0149 QStringList getAppOffers() const; 0150 QStringList getPartOffers() const; 0151 void getMyServiceOffers() const; 0152 void syncServices(); 0153 void saveServices(KConfigGroup &config, const QStringList &services); 0154 void saveDefaultApplication(KConfigGroup &config, const QStringList &services); 0155 void saveRemovedServices(KConfigGroup &config, const QStringList &services, const QStringList &oldServices); 0156 0157 QMimeType m_mimetype; 0158 enum AskSave { 0159 AskSaveYes = 0, 0160 AskSaveNo = 1, 0161 AskSaveDefault = 2, 0162 }; 0163 AskSave m_askSave : 3; 0164 AutoEmbed m_autoEmbed : 3; 0165 bool m_bNewItem : 1; 0166 mutable bool m_bFullInit : 1; // lazy init of m_appServices and m_embedServices 0167 bool m_isGroup : 1; 0168 bool m_appServicesModified : 1; 0169 bool m_embedServicesModified : 1; 0170 bool m_userSpecifiedIconModified : 1; 0171 QString m_major, m_minor, m_comment, m_userSpecifiedIcon; 0172 QStringList m_patterns; 0173 mutable QStringList m_appServices; 0174 mutable QStringList m_embedParts; 0175 }; 0176 0177 #endif /* MIMETYPEDATA_H */