collectionviewpage.cpp 5.64 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   SPDX-FileCopyrightText: 2009-2021 Laurent Montel <montel@kde.org>
3
4

   SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5
6
7
*/

#include "collectionviewpage.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include <MailCommon/MailKernel>
Laurent Montel's avatar
Port it    
Laurent Montel committed
9

10
#include <Akonadi/KMime/MessageFolderAttribute>
Laurent Montel's avatar
Laurent Montel committed
11
#include <AkonadiCore/entitydisplayattribute.h>
12

Laurent Montel's avatar
Laurent Montel committed
13
14
15
16
#include "kmail_debug.h"
#include <KIconButton>
#include <KLocalizedString>
#include <QCheckBox>
17
#include <QFormLayout>
Laurent Montel's avatar
Laurent Montel committed
18
19
20
#include <QHBoxLayout>
#include <QLabel>
#include <QVBoxLayout>
21

Laurent Montel's avatar
Laurent Montel committed
22
23
24
25
#include <MessageList/AggregationComboBox>
#include <MessageList/AggregationConfigButton>
#include <MessageList/ThemeComboBox>
#include <MessageList/ThemeConfigButton>
Laurent Montel's avatar
Laurent Montel committed
26

Laurent Montel's avatar
Laurent Montel committed
27
#include <MailCommon/CollectionViewWidget>
Laurent Montel's avatar
Laurent Montel committed
28

Andras Mantia's avatar
Andras Mantia committed
29
30
using namespace MailCommon;

Laurent Montel's avatar
Laurent Montel committed
31
32
CollectionViewPage::CollectionViewPage(QWidget *parent)
    : CollectionPropertiesPage(parent)
Laurent Montel's avatar
Laurent Montel committed
33
{
34
    setObjectName(QStringLiteral("KMail::CollectionViewPage"));
Laurent Montel's avatar
Laurent Montel committed
35
    setPageTitle(i18nc("@title:tab View settings for a folder.", "View"));
Laurent Montel's avatar
Laurent Montel committed
36
37
}

Laurent Montel's avatar
Laurent Montel committed
38
CollectionViewPage::~CollectionViewPage() = default;
Laurent Montel's avatar
Laurent Montel committed
39

Laurent Montel's avatar
Laurent Montel committed
40
void CollectionViewPage::init(const Akonadi::Collection &col)
Laurent Montel's avatar
Laurent Montel committed
41
{
Laurent Montel's avatar
Laurent Montel committed
42
    mFolderCollection = FolderSettings::forCollection(col);
Laurent Montel's avatar
Laurent Montel committed
43
    mIsLocalSystemFolder = CommonKernel->isSystemFolderCollection(col) || mFolderCollection->isStructural() || Kernel::folderIsInbox(col);
Laurent Montel's avatar
Laurent Montel committed
44

45
    auto topLayout = new QVBoxLayout(this);
46
47
48
49

    mCollectionViewWidget = new MailCommon::CollectionViewWidget(this);
    topLayout->addWidget(mCollectionViewWidget);

Laurent Montel's avatar
Laurent Montel committed
50
    // Musn't be able to edit details for non-resource, system folder.
Laurent Montel's avatar
Laurent Montel committed
51
    if (!mIsLocalSystemFolder) {
52
53
54
        auto innerLayout = qobject_cast<QFormLayout *>(mCollectionViewWidget->layout());
        Q_ASSERT(innerLayout != nullptr);

Laurent Montel's avatar
Laurent Montel committed
55
        // icons
Laurent Montel's avatar
Laurent Montel committed
56
57
        mIconsCheckBox = new QCheckBox(i18n("Use custom &icons"), this);
        mIconsCheckBox->setChecked(false);
58
        innerLayout->insertRow(0, QString(), mIconsCheckBox);
Laurent Montel's avatar
Laurent Montel committed
59
60
61
62
63
64
65
66
67
68

        mNormalIconLabel = new QLabel(i18nc("Icon used for folders with no unread messages.", "&Normal:"), this);
        mNormalIconLabel->setEnabled(false);

        mNormalIconButton = new KIconButton(this);
        mNormalIconLabel->setBuddy(mNormalIconButton);
        mNormalIconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place, false);
        mNormalIconButton->setIconSize(16);
        mNormalIconButton->setStrictIconSize(true);
        mNormalIconButton->setFixedSize(28, 28);
Laurent Montel's avatar
Laurent Montel committed
69
        // Can't use iconset here.
70
        mNormalIconButton->setIcon(QStringLiteral("folder"));
Laurent Montel's avatar
Laurent Montel committed
71
72
73
74
75
76
77
78
79
80
81
        mNormalIconButton->setEnabled(false);

        mUnreadIconLabel = new QLabel(i18nc("Icon used for folders which do have unread messages.", "&Unread:"), this);
        mUnreadIconLabel->setEnabled(false);

        mUnreadIconButton = new KIconButton(this);
        mUnreadIconLabel->setBuddy(mUnreadIconButton);
        mUnreadIconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place, false);
        mUnreadIconButton->setIconSize(16);
        mUnreadIconButton->setStrictIconSize(true);
        mUnreadIconButton->setFixedSize(28, 28);
Laurent Montel's avatar
Laurent Montel committed
82
        // Can't use iconset here.
83
        mUnreadIconButton->setIcon(QStringLiteral("folder-open"));
Laurent Montel's avatar
Laurent Montel committed
84
85
        mUnreadIconButton->setEnabled(false);

86
        auto iconHLayout = new QHBoxLayout();
Laurent Montel's avatar
Laurent Montel committed
87
88
89
90
91
        iconHLayout->addWidget(mNormalIconLabel);
        iconHLayout->addWidget(mNormalIconButton);
        iconHLayout->addWidget(mUnreadIconLabel);
        iconHLayout->addWidget(mUnreadIconButton);
        iconHLayout->addStretch(1);
92
        innerLayout->insertRow(1, QString(), iconHLayout);
Laurent Montel's avatar
Laurent Montel committed
93

Laurent Montel's avatar
Laurent Montel committed
94
95
96
97
98
99
        connect(mIconsCheckBox, &QCheckBox::toggled, mNormalIconLabel, &QLabel::setEnabled);
        connect(mIconsCheckBox, &QCheckBox::toggled, mNormalIconButton, &KIconButton::setEnabled);
        connect(mIconsCheckBox, &QCheckBox::toggled, mUnreadIconButton, &KIconButton::setEnabled);
        connect(mIconsCheckBox, &QCheckBox::toggled, mUnreadIconLabel, &QLabel::setEnabled);

        connect(mNormalIconButton, &KIconButton::iconChanged, this, &CollectionViewPage::slotChangeIcon);
Laurent Montel's avatar
Laurent Montel committed
100
    }
Laurent Montel's avatar
Laurent Montel committed
101

Laurent Montel's avatar
Laurent Montel committed
102
    topLayout->addStretch(100);
Laurent Montel's avatar
Laurent Montel committed
103
104
}

Laurent Montel's avatar
Laurent Montel committed
105
void CollectionViewPage::slotChangeIcon(const QString &icon)
Laurent Montel's avatar
Laurent Montel committed
106
{
Laurent Montel's avatar
Laurent Montel committed
107
    mUnreadIconButton->setIcon(icon);
Laurent Montel's avatar
Laurent Montel committed
108
109
}

Laurent Montel's avatar
Laurent Montel committed
110
void CollectionViewPage::load(const Akonadi::Collection &col)
Laurent Montel's avatar
Laurent Montel committed
111
{
Laurent Montel's avatar
Laurent Montel committed
112
113
    init(col);
    if (!mIsLocalSystemFolder) {
Laurent Montel's avatar
Laurent Montel committed
114
115
116
        QString iconName;
        QString unreadIconName;
        bool iconWasEmpty = false;
Laurent Montel's avatar
Laurent Montel committed
117
        if (col.hasAttribute<Akonadi::EntityDisplayAttribute>()) {
Laurent Montel's avatar
Laurent Montel committed
118
119
120
121
            iconName = col.attribute<Akonadi::EntityDisplayAttribute>()->iconName();
            unreadIconName = col.attribute<Akonadi::EntityDisplayAttribute>()->activeIconName();
        }

Laurent Montel's avatar
Laurent Montel committed
122
        if (iconName.isEmpty()) {
123
            iconName = QStringLiteral("folder");
Laurent Montel's avatar
Laurent Montel committed
124
125
            iconWasEmpty = true;
        }
Laurent Montel's avatar
Laurent Montel committed
126
        mNormalIconButton->setIcon(iconName);
Laurent Montel's avatar
Laurent Montel committed
127

Laurent Montel's avatar
Laurent Montel committed
128
129
130
131
        if (unreadIconName.isEmpty()) {
            mUnreadIconButton->setIcon(iconName);
        } else {
            mUnreadIconButton->setIcon(unreadIconName);
Laurent Montel's avatar
Laurent Montel committed
132
133
        }

Laurent Montel's avatar
Laurent Montel committed
134
        mIconsCheckBox->setChecked(!iconWasEmpty);
135
    }
Laurent Montel's avatar
Laurent Montel committed
136
    mCollectionViewWidget->load(col);
Laurent Montel's avatar
Laurent Montel committed
137
138
}

Laurent Montel's avatar
Laurent Montel committed
139
void CollectionViewPage::save(Akonadi::Collection &col)
Laurent Montel's avatar
Laurent Montel committed
140
{
Laurent Montel's avatar
Laurent Montel committed
141
142
143
144
145
146
147
    if (!mIsLocalSystemFolder) {
        if (mIconsCheckBox->isChecked()) {
            col.attribute<Akonadi::EntityDisplayAttribute>(Akonadi::Collection::AddIfMissing)->setIconName(mNormalIconButton->icon());
            col.attribute<Akonadi::EntityDisplayAttribute>(Akonadi::Collection::AddIfMissing)->setActiveIconName(mUnreadIconButton->icon());
        } else if (col.hasAttribute<Akonadi::EntityDisplayAttribute>()) {
            col.attribute<Akonadi::EntityDisplayAttribute>()->setIconName(QString());
            col.attribute<Akonadi::EntityDisplayAttribute>()->setActiveIconName(QString());
Laurent Montel's avatar
Laurent Montel committed
148
        }
149
    }
Laurent Montel's avatar
Laurent Montel committed
150
    mCollectionViewWidget->save(col);
Laurent Montel's avatar
Laurent Montel committed
151
}