Akonadi::MimeTypeChecker Class
class Akonadi::MimeTypeCheckerHelper for checking MIME types of Collections and Items. More...
| Header: | #include <Akonadi/MimeTypeChecker> |
| CMake: | find_package(KPim6 REQUIRED COMPONENTS AkonadiCore)target_link_libraries(mytarget PRIVATE KPim6::AkonadiCore) |
Public Functions
| MimeTypeChecker() | |
| MimeTypeChecker(const Akonadi::MimeTypeChecker &other) | |
| ~MimeTypeChecker() | |
| void | addWantedMimeType(const QString &mimeType) |
| bool | containsWantedMimeType(const QStringList &mimeTypes) const |
| bool | hasWantedMimeTypes() const |
| bool | isWantedCollection(const Akonadi::Collection &collection) const |
| bool | isWantedItem(const Akonadi::Item &item) const |
| bool | isWantedMimeType(const QString &mimeType) const |
| void | removeWantedMimeType(const QString &mimeType) |
| void | setWantedMimeTypes(const QStringList &mimeTypes) |
| QStringList | wantedMimeTypes() const |
| Akonadi::MimeTypeChecker & | operator=(const Akonadi::MimeTypeChecker &other) |
Static Public Members
| bool | isWantedCollection(const Akonadi::Collection &collection, const QString &wantedMimeType) |
| bool | isWantedItem(const Akonadi::Item &item, const QString &wantedMimeType) |
Detailed Description
When it is necessary to decide whether an item has a certain MIME type or whether a collection can contain a certain MIME type, direct string comparison might not render the desired result because MIME types can have aliases and be a node in an "inheritance" hierarchy.
For example a check like this
if ( item.mimeType() == QLatin1StringView( "text/directory" ) )
would fail to detect \ "text/x-vcard" as being the same MIME type.
Note: KDE deals with this inside the KMimeType framework, this class is just a convenience helper for common Akonadi related checks.
Example: Checking whether an Akonadi::Item is contact MIME type
Akonadi::MimeTypeChecker checker; checker.addWantedMimeType( KContacts::Addressee::mimeType() ); if ( checker.isWantedItem( item ) ){ // item.mimeType() is equal KContacts::Addressee::mimeType(), an aliases // or a sub type. }
Example: Checking whether an Akonadi::Collection could contain calendar items
Akonadi::MimeTypeChecker checker; checker.addWantedMimeType( QLatin1StringView( "text/calendar" ) ); if ( checker.isWantedCollection( collection ) ) { // collection.contentMimeTypes() contains \\ "text/calendar" // or a sub type. }
Example: Checking whether an Akonadi::Collection could contain Calendar Event items (i.e. KCal::Event), making use of the respective MIME type "subclassing" provided by Akonadi's MIME type extensions.
Akonadi::MimeTypeChecker checker; checker.addWantedMimeType( QLatin1StringView( "application/x-vnd.akonadi.calendar.event" ) ); if ( checker.isWantedCollection( collection ) ) { // collection.contentMimeTypes() contains \\ "application/x-vnd.akonadi.calendar.event" // or a sub type, but just containing \\ "text/calendar" would not // get here }
Example: Checking for items of more than one MIME type and treat one of them specially.
Akonadi::MimeTypeChecker mimeFilter; mimeFilter.setWantedMimeTypes( QStringList() << KContacts::Addressee::mimeType() << KContacts::ContactGroup::mimeType() ); if ( mimeFilter.isWantedItem( item ) ) { if ( Akonadi::MimeTypeChecker::isWantedItem( item, KContacts::ContactGroup::mimeType() ) { // treat contact group's differently } }
This class is implicitly shared.
Author: Kevin Krammer <kevin.krammer@gmx.at>
Member Function Documentation
MimeTypeChecker::MimeTypeChecker()
Creates an empty MIME type checker.
An empty checker will not report any items or collections as wanted.
MimeTypeChecker::MimeTypeChecker(const Akonadi::MimeTypeChecker &other)
Creates a new MIME type checker from an other.
[noexcept] MimeTypeChecker::~MimeTypeChecker()
Destroys the MIME type checker.
void MimeTypeChecker::addWantedMimeType(const QString &mimeType)
Adds another MIME type to the list of wanted MIME types this instance checks against.
mimeType The MIME types to add to the checklist.
See also setWantedMimeTypes().
bool MimeTypeChecker::containsWantedMimeType(const QStringList &mimeTypes) const
Checks whether any of the given MIME types is covered by one of the wanted MIME types.
mimeTypes The MIME types to check.
Returns \ true if any of the MIME types in mimeTypes is covered by one of the wanted MIME types, \ false otherwise.
bool MimeTypeChecker::hasWantedMimeTypes() const
Checks whether any wanted MIME types are set.
Returns \ true if any wanted MIME types are set, false otherwise.
bool MimeTypeChecker::isWantedCollection(const Akonadi::Collection &collection) const
Checks whether a given collection has one of the wanted MIME types
collection The collection to check the content MIME types of.
Returns \ true if one of the collection content MIME types is one of the wanted ones, \ false if non is, the collection is invalid or has an empty content MIME type list.
See also setWantedMimeTypes() and Collection::contentMimeTypes().
[static] bool MimeTypeChecker::isWantedCollection(const Akonadi::Collection &collection, const QString &wantedMimeType)
Checks whether a given collection has the given MIME type
collection The collection to check the content MIME types of. wantedMimeType The MIME type to check against.
Returns \ true if one of the collection content MIME types is the given wanted one, \ false if it isn't, the collection is invalid or has an empty content MIME type list.
See also setWantedMimeTypes() and Collection::contentMimeTypes().
bool MimeTypeChecker::isWantedItem(const Akonadi::Item &item) const
Checks whether a given item has one of the wanted MIME types
item The item to check the MIME type of.
Returns \ true if the item MIME type is one of the wanted ones, \ false if it isn't, the item is invalid or has an empty MIME type.
See also setWantedMimeTypes() and Item::mimeType().
[static] bool MimeTypeChecker::isWantedItem(const Akonadi::Item &item, const QString &wantedMimeType)
Checks whether a given item has the given wanted MIME type
item The item to check the MIME type of. wantedMimeType The MIME type to check against.
Returns \ true if the item MIME type is the given one, \ false if it isn't, the item is invalid or has an empty MIME type.
See also setWantedMimeTypes() and Item::mimeType().
bool MimeTypeChecker::isWantedMimeType(const QString &mimeType) const
Checks whether a given mime type is covered by one of the wanted MIME types.
mimeType The mime type to check.
Returns \ true if the mime type mimeType is covered by one of the wanted MIME types, \ false otherwise.
void MimeTypeChecker::removeWantedMimeType(const QString &mimeType)
Removes a MIME type from the list of wanted MIME types this instance checks against.
mimeType The MIME type to remove from the checklist.
See also addWantedMimeType().
void MimeTypeChecker::setWantedMimeTypes(const QStringList &mimeTypes)
Sets the list of wanted MIME types this instance checks against.
mimeTypes The list of MIME types to check against.
See also wantedMimeTypes().
QStringList MimeTypeChecker::wantedMimeTypes() const
Returns the list of wanted MIME types this instance checks against.
Note: Don't use this just to check whether there are any wanted mimetypes. It is much faster to call \ hasWantedMimeTypes() instead for that purpose.
See also setWantedMimeTypes() and hasWantedMimeTypes().
Akonadi::MimeTypeChecker &MimeTypeChecker::operator=(const Akonadi::MimeTypeChecker &other)
Assigns the other to this checker and returns a reference to this checker.