Mailcommon

tag.cpp
1/* SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org>
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6#include "tag.h"
7
8#include <Akonadi/TagAttribute>
9#include <QFont>
10#include <QGuiApplication>
11
12#include <QUuid>
13using namespace MailCommon;
14
15Tag::Ptr Tag::createDefaultTag(const QString &name)
16{
17 Tag::Ptr tag(new Tag());
18 tag->tagName = name;
19 tag->iconName = QStringLiteral("mail-tagged");
20
21 tag->priority = -1;
22 tag->inToolbar = false;
23 tag->isImmutable = false;
24 tag->isBold = false;
25 tag->isItalic = false;
26 return tag;
27}
28
29Tag::Ptr Tag::fromAkonadi(const Akonadi::Tag &akonadiTag)
30{
31 Tag::Ptr tag(new Tag());
32 tag->tagName = akonadiTag.name();
33 tag->mTag = akonadiTag;
34 tag->priority = -1;
35 tag->iconName = QStringLiteral("mail-tagged");
36 tag->inToolbar = false;
37 tag->isImmutable = akonadiTag.isImmutable();
38 const auto *attr = akonadiTag.attribute<Akonadi::TagAttribute>();
39 if (attr) {
40 if (!attr->iconName().isEmpty()) {
41 tag->iconName = attr->iconName();
42 }
43 tag->inToolbar = attr->inToolbar();
44 tag->shortcut = QKeySequence(attr->shortcut());
45 tag->textColor = attr->textColor();
46 tag->backgroundColor = attr->backgroundColor();
47 if (!attr->font().isEmpty()) {
48 QFont font;
49 font.fromString(attr->font());
50 tag->isBold = font.bold();
51 tag->isItalic = font.italic();
52 }
53 tag->priority = attr->priority();
54 }
55 return tag;
56}
57
58Akonadi::Tag Tag::saveToAkonadi(Tag::SaveFlags saveFlags) const
59{
60 Akonadi::Tag tag = mTag;
61 if (tag.gid().isEmpty()) {
62 tag.setGid(QUuid::createUuid().toByteArray().mid(1, 36));
63 }
64 if (isImmutable) {
65 tag.setType(Akonadi::Tag::PLAIN);
66 } else {
67 tag.setType(Akonadi::Tag::GENERIC);
68 }
70 attr->setDisplayName(tagName);
71 attr->setIconName(iconName);
72 attr->setInToolbar(inToolbar);
73 attr->setShortcut(shortcut.toString());
74 attr->setPriority(priority);
75
76 if (textColor.isValid() && (saveFlags & TextColor)) {
77 attr->setTextColor(textColor);
78 } else {
79 attr->setTextColor(QColor());
80 }
81
82 if (backgroundColor.isValid() && (saveFlags & BackgroundColor)) {
83 attr->setBackgroundColor(backgroundColor);
84 } else {
85 attr->setBackgroundColor(QColor());
86 }
87
88 if (saveFlags & Font) {
90 font.setBold(isBold);
91 font.setItalic(isItalic);
92 attr->setFont(font.toString());
93 }
94
95 tag.addAttribute(attr);
96 return tag;
97}
98
99bool Tag::compare(const Tag::Ptr &tag1, const Tag::Ptr &tag2)
100{
101 if (tag1->priority < tag2->priority) {
102 return true;
103 } else if (tag1->priority == tag2->priority) {
104 return tag1->tagName < tag2->tagName;
105 } else {
106 return false;
107 }
108}
109
110bool Tag::compareName(const Tag::Ptr &tag1, const Tag::Ptr &tag2)
111{
112 return tag1->tagName < tag2->tagName;
113}
114
115bool Tag::operator==(const Tag &other) const
116{
117#if 0
118 if (mTag.isValid()) {
119 return id() == other.id();
120 }
121#endif
122 return tagName == other.tagName && textColor == other.textColor && backgroundColor == other.backgroundColor && isBold == other.isBold
123 && isItalic == other.isItalic && iconName == other.iconName && inToolbar == other.inToolbar && shortcut.toString() == other.shortcut.toString()
124 && priority == other.priority;
125}
126
127bool Tag::operator!=(const Tag &other) const
128{
129 return !(*this == other);
130}
131
132qint64 Tag::id() const
133{
134 return mTag.id();
135}
136
137QString Tag::name() const
138{
139 return mTag.name();
140}
141
142Akonadi::Tag Tag::tag() const
143{
144 return mTag;
145}
146
147#include "moc_tag.cpp"
void setDisplayName(const QString &name)
Id id() const
bool isImmutable() const
static const char GENERIC[]
void addAttribute(Attribute *attribute)
const T * attribute() const
static const char PLAIN[]
QString name(StandardAction id)
The filter dialog.
bool isValid() const const
bool bold() const const
bool fromString(const QString &descrip)
bool italic() const const
void setBold(bool enable)
void setItalic(bool enable)
QString toString() const const
QString toString(SequenceFormat format) const const
QUuid createUuid()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:49:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.