summaryrefslogtreecommitdiffstats
blob: 54cca76218fa888898cf5186ada0cd9f72565c02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * Copyright (c) 2016-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtRecentEmojisGrid.h>

#include <algorithm>

#include <QSettings>
#include <QVector>

#include <SwifTools/EmojiMapper.h>

#include <Swift/QtUI/QtEmojiCell.h>
#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {
    const int QtRecentEmojisGrid::MAX_RECENTS = 50;

    QtRecentEmojisGrid::QtRecentEmojisGrid(QSettings* settings) : QtEmojisGrid(), settings_(settings) {
        loadSettings();
        connect(this, SIGNAL(onEmojiSelected(QString)), this, SLOT(handleEmojiClicked(QString)));
        refresh();
    }

    QtRecentEmojisGrid::~QtRecentEmojisGrid() {
        writeSettings();
    }

    void QtRecentEmojisGrid::handleEmojiClicked(QString emoji) {
        recents_.erase(std::remove(recents_.begin(), recents_.end(), emoji), recents_.end());

        if (recents_.size() > MAX_RECENTS) {
            recents_.resize(MAX_RECENTS - 1);
        }

        recents_.push_front(emoji);
        refresh();
    }

    void QtRecentEmojisGrid::refresh() {
        QtEmojisGrid::setEmojis(recents_);
    }

    void QtRecentEmojisGrid::loadSettings() {
        QByteArray readData = settings_->value("recentEmojis").toByteArray();
        QDataStream readStream(&readData, QIODevice::ReadOnly);
        readStream >> recents_;
    }

    void QtRecentEmojisGrid::writeSettings() {
        QByteArray data;
        QDataStream stream(&data, QIODevice::WriteOnly);
        stream << recents_;
        settings_->setValue("recentEmojis", data);
    }
}