Mailcommon

tag.cpp
1 /* SPDX-FileCopyrightText: 2010 Thomas McGuire <[email protected]>
2  SPDX-FileCopyrightText: 2012-2023 Laurent Montel <[email protected]>
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>
13 using namespace MailCommon;
14 
15 Tag::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 
29 Tag::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 
58 Akonadi::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 
99 bool 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 
110 bool Tag::compareName(const Tag::Ptr &tag1, const Tag::Ptr &tag2)
111 {
112  return tag1->tagName < tag2->tagName;
113 }
114 
115 bool 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 
127 bool Tag::operator!=(const Tag &other) const
128 {
129  return !(*this == other);
130 }
131 
132 qint64 Tag::id() const
133 {
134  return mTag.id();
135 }
136 
137 QString Tag::name() const
138 {
139  return mTag.name();
140 }
141 
142 Akonadi::Tag Tag::tag() const
143 {
144  return mTag;
145 }
146 
147 #include "moc_tag.cpp"
const QList< QKeySequence > & shortcut(StandardShortcut id)
bool fromString(const QString &descrip)
bool bold() const const
bool italic() const const
QString iconName() const
void setDisplayName(const QString &name)
static const char GENERIC[]
const T * attribute() const
void setItalic(bool enable)
static const char PLAIN[]
QUuid createUuid()
QString toString() const const
void addAttribute(Attribute *attribute)
QString name(StandardShortcut id)
bool isImmutable() const
void setBold(bool enable)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 03:59:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.