File indexing completed on 2024-05-26 05:25:20

0001 /* SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org>
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "tag.h"
0007 
0008 #include <Akonadi/TagAttribute>
0009 #include <QFont>
0010 #include <QGuiApplication>
0011 
0012 #include <QUuid>
0013 using namespace MailCommon;
0014 
0015 Tag::Ptr Tag::createDefaultTag(const QString &name)
0016 {
0017     Tag::Ptr tag(new Tag());
0018     tag->tagName = name;
0019     tag->iconName = QStringLiteral("mail-tagged");
0020 
0021     tag->priority = -1;
0022     tag->inToolbar = false;
0023     tag->isImmutable = false;
0024     tag->isBold = false;
0025     tag->isItalic = false;
0026     return tag;
0027 }
0028 
0029 Tag::Ptr Tag::fromAkonadi(const Akonadi::Tag &akonadiTag)
0030 {
0031     Tag::Ptr tag(new Tag());
0032     tag->tagName = akonadiTag.name();
0033     tag->mTag = akonadiTag;
0034     tag->priority = -1;
0035     tag->iconName = QStringLiteral("mail-tagged");
0036     tag->inToolbar = false;
0037     tag->isImmutable = akonadiTag.isImmutable();
0038     const auto *attr = akonadiTag.attribute<Akonadi::TagAttribute>();
0039     if (attr) {
0040         if (!attr->iconName().isEmpty()) {
0041             tag->iconName = attr->iconName();
0042         }
0043         tag->inToolbar = attr->inToolbar();
0044         tag->shortcut = QKeySequence(attr->shortcut());
0045         tag->textColor = attr->textColor();
0046         tag->backgroundColor = attr->backgroundColor();
0047         if (!attr->font().isEmpty()) {
0048             QFont font;
0049             font.fromString(attr->font());
0050             tag->isBold = font.bold();
0051             tag->isItalic = font.italic();
0052         }
0053         tag->priority = attr->priority();
0054     }
0055     return tag;
0056 }
0057 
0058 Akonadi::Tag Tag::saveToAkonadi(Tag::SaveFlags saveFlags) const
0059 {
0060     Akonadi::Tag tag = mTag;
0061     if (tag.gid().isEmpty()) {
0062         tag.setGid(QUuid::createUuid().toByteArray().mid(1, 36));
0063     }
0064     if (isImmutable) {
0065         tag.setType(Akonadi::Tag::PLAIN);
0066     } else {
0067         tag.setType(Akonadi::Tag::GENERIC);
0068     }
0069     auto *attr = tag.attribute<Akonadi::TagAttribute>(Akonadi::Tag::AddIfMissing);
0070     attr->setDisplayName(tagName);
0071     attr->setIconName(iconName);
0072     attr->setInToolbar(inToolbar);
0073     attr->setShortcut(shortcut.toString());
0074     attr->setPriority(priority);
0075 
0076     if (textColor.isValid() && (saveFlags & TextColor)) {
0077         attr->setTextColor(textColor);
0078     } else {
0079         attr->setTextColor(QColor());
0080     }
0081 
0082     if (backgroundColor.isValid() && (saveFlags & BackgroundColor)) {
0083         attr->setBackgroundColor(backgroundColor);
0084     } else {
0085         attr->setBackgroundColor(QColor());
0086     }
0087 
0088     if (saveFlags & Font) {
0089         QFont font = QGuiApplication::font();
0090         font.setBold(isBold);
0091         font.setItalic(isItalic);
0092         attr->setFont(font.toString());
0093     }
0094 
0095     tag.addAttribute(attr);
0096     return tag;
0097 }
0098 
0099 bool Tag::compare(const Tag::Ptr &tag1, const Tag::Ptr &tag2)
0100 {
0101     if (tag1->priority < tag2->priority) {
0102         return true;
0103     } else if (tag1->priority == tag2->priority) {
0104         return tag1->tagName < tag2->tagName;
0105     } else {
0106         return false;
0107     }
0108 }
0109 
0110 bool Tag::compareName(const Tag::Ptr &tag1, const Tag::Ptr &tag2)
0111 {
0112     return tag1->tagName < tag2->tagName;
0113 }
0114 
0115 bool Tag::operator==(const Tag &other) const
0116 {
0117 #if 0
0118     if (mTag.isValid()) {
0119         return id() == other.id();
0120     }
0121 #endif
0122     return tagName == other.tagName && textColor == other.textColor && backgroundColor == other.backgroundColor && isBold == other.isBold
0123         && isItalic == other.isItalic && iconName == other.iconName && inToolbar == other.inToolbar && shortcut.toString() == other.shortcut.toString()
0124         && priority == other.priority;
0125 }
0126 
0127 bool Tag::operator!=(const Tag &other) const
0128 {
0129     return !(*this == other);
0130 }
0131 
0132 qint64 Tag::id() const
0133 {
0134     return mTag.id();
0135 }
0136 
0137 QString Tag::name() const
0138 {
0139     return mTag.name();
0140 }
0141 
0142 Akonadi::Tag Tag::tag() const
0143 {
0144     return mTag;
0145 }
0146 
0147 #include "moc_tag.cpp"