Astyle kdelibs
[kdepim.git] / kmail / tag / tagselectdialog.cpp
blob043d08160ecbf65cde6bb8a73f2cbc0b30c6ff5a
1 /*
2 * Copyright (c) 2011, 2012, 2013 Montel Laurent <montel@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * In addition, as a special exception, the copyright holders give
18 * permission to link the code of this program with any edition of
19 * the Qt library by Trolltech AS, Norway (or with modified versions
20 * of Qt that use the same license as Qt), and distribute linked
21 * combinations including the two. You must obey the GNU General
22 * Public License in all respects for all of the code used other than
23 * Qt. If you modify this file, you may extend this exception to
24 * your version of the file, but you are not obligated to do so. If
25 * you do not wish to do so, delete this exception statement from
26 * your version.
29 #include "tagselectdialog.h"
30 #include "tag.h"
31 #include "kmkernel.h"
33 #include <mailcommon/tag/addtagdialog.h>
35 #include <KListWidgetSearchLine>
36 #include <KLocalizedString>
37 #include <QIcon>
38 #include <QDebug>
40 #include <QGridLayout>
41 #include <QListWidget>
42 #include <AkonadiCore/TagFetchJob>
43 #include <AkonadiCore/TagFetchScope>
44 #include <AkonadiCore/TagAttribute>
45 #include <QDialogButtonBox>
46 #include <KConfigGroup>
47 #include <QPushButton>
48 #include <QVBoxLayout>
50 using namespace KMail;
52 TagSelectDialog::TagSelectDialog(QWidget *parent, int numberOfSelectedMessages, const Akonadi::Item &selectedItem)
53 : QDialog(parent),
54 mNumberOfSelectedMessages(numberOfSelectedMessages),
55 mSelectedItem(selectedItem)
57 setWindowTitle(i18n("Select Tags"));
58 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
60 QVBoxLayout *mainLayout = new QVBoxLayout;
61 setLayout(mainLayout);
62 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
63 okButton->setDefault(true);
64 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
65 QPushButton *user1Button = new QPushButton;
66 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
67 connect(buttonBox, &QDialogButtonBox::accepted, this, &TagSelectDialog::accept);
68 connect(buttonBox, &QDialogButtonBox::rejected, this, &TagSelectDialog::reject);
69 user1Button->setText(i18n("Add New Tag..."));
70 okButton->setDefault(true);
71 setModal(true);
73 QWidget *mainWidget = new QWidget(this);
74 mainLayout->addWidget(mainWidget);
75 mainLayout->addWidget(buttonBox);
77 QVBoxLayout *vbox = new QVBoxLayout;
78 mainWidget->setLayout(vbox);
79 mListTag = new QListWidget(this);
80 KListWidgetSearchLine *listWidgetSearchLine = new KListWidgetSearchLine(this, mListTag);
81 listWidgetSearchLine->setPlaceholderText(i18n("Search tag"));
82 listWidgetSearchLine->setClearButtonEnabled(true);
84 vbox->addWidget(listWidgetSearchLine);
85 vbox->addWidget(mListTag);
87 createTagList();
88 connect(user1Button, &QPushButton::clicked, this, &TagSelectDialog::slotAddNewTag);
90 KConfigGroup group(KMKernel::self()->config(), "TagSelectDialog");
91 const QSize size = group.readEntry("Size", QSize(500, 300));
92 if (size.isValid()) {
93 resize(size);
97 TagSelectDialog::~TagSelectDialog()
99 KConfigGroup group(KMKernel::self()->config(), "TagSelectDialog");
100 group.writeEntry("Size", size());
103 void TagSelectDialog::slotAddNewTag()
105 QPointer<MailCommon::AddTagDialog> dialog = new MailCommon::AddTagDialog(QList<KActionCollection *>(), this);
106 dialog->setTags(mTagList);
107 if (dialog->exec()) {
108 mListTag->clear();
109 mTagList.clear();
110 createTagList();
112 delete dialog;
115 void TagSelectDialog::createTagList()
117 Akonadi::TagFetchJob *fetchJob = new Akonadi::TagFetchJob(this);
118 fetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
119 connect(fetchJob, &Akonadi::TagFetchJob::result, this, &TagSelectDialog::slotTagsFetched);
122 void TagSelectDialog::slotTagsFetched(KJob *job)
124 if (job->error()) {
125 qWarning() << "Failed to load tags " << job->errorString();
126 return;
128 Akonadi::TagFetchJob *fetchJob = static_cast<Akonadi::TagFetchJob *>(job);
130 foreach (const Akonadi::Tag &akonadiTag, fetchJob->tags()) {
131 mTagList.append(MailCommon::Tag::fromAkonadi(akonadiTag));
134 qSort(mTagList.begin(), mTagList.end(), MailCommon::Tag::compare);
136 foreach (const MailCommon::Tag::Ptr &tag, mTagList) {
137 QListWidgetItem *item = new QListWidgetItem(QIcon::fromTheme(tag->iconName), tag->tagName, mListTag);
138 item->setData(UrlTag, tag->tag().url().url());
139 item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
140 item->setCheckState(Qt::Unchecked);
141 mListTag->addItem(item);
143 if (mNumberOfSelectedMessages == 1) {
144 const bool hasTag = mSelectedItem.hasTag(tag->tag());
145 item->setCheckState(hasTag ? Qt::Checked : Qt::Unchecked);
146 } else {
147 item->setCheckState(Qt::Unchecked);
152 Akonadi::Tag::List TagSelectDialog::selectedTag() const
154 Akonadi::Tag::List lst;
155 const int numberOfItems(mListTag->count());
156 for (int i = 0; i < numberOfItems; ++i) {
157 QListWidgetItem *item = mListTag->item(i);
158 if (item->checkState() == Qt::Checked) {
159 lst.append(Akonadi::Tag::fromUrl(item->data(UrlTag).toString()));
162 qDebug() << " lst" << lst;
163 return lst;