summaryrefslogtreecommitdiffstats
blob: 86541a055b4405583e33d309e6460ae034d8a994 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <boost/signals2.hpp>

#include <QAbstractItemModel>

#include <Swiften/Elements/StatusShow.h>
#include <Swiften/JID/JID.h>

namespace Swift {

    class SwiftAccountData {
    public:
        struct SwiftAccountDataItem {
            SwiftAccountDataItem(std::string serverID) : serverID_(serverID) {}
            //FIXME eliminate serverID_, the userJID_ will be the ID when we connect with the actual data.
            std::string serverID_;
            QString iconPath_;
            JID userJID_;
            size_t unreadCount_ = 0;
            StatusShow status_ = StatusShow::None;
            boost::signals2::scoped_connection dataChangedConnection_;
            boost::signals2::signal<void ()> onDataChanged;
            void handleChangeStatusRequest(StatusShow::Type show, const std::string& /*statusText*/) {
                status_ = show;
                onDataChanged();
            }
        };
    public:
        SwiftAccountData() {}
        ~SwiftAccountData() {
            for (auto account : accounts_) {
                delete account;
            }
        }
        //FIXME make addAccount with SwiftAccountDataItem, and added after a succesfull connection to the server has been established.
        void addAccount(JID userJID) {
            SwiftAccountDataItem* newItem = new SwiftAccountDataItem(userJID);
            newItem->dataChangedConnection_ = newItem->onDataChanged.connect(boost::bind(&SwiftAccountData::handleDataChanged, this));
            accounts_.push_back(newItem);
        }
        SwiftAccountDataItem* getAccount(int index) {
            if (index >= accounts_.size()) {
                return nullptr;
            }
            return accounts_[index];
        }
        size_t size() {
            return accounts_.size();
        }
    public:
        boost::signals2::signal<void()> onDataChanged;
    private:
        void handleDataChanged() {
            onDataChanged();
        }
    private:
        QList<SwiftAccountDataItem*> accounts_;
    };

    class ServerListModel : public QAbstractItemModel {
        Q_OBJECT
        public:
            ServerListModel();
            ~ServerListModel();

            QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
            QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
            QModelIndex parent(const QModelIndex& index) const override;

            QMimeData* mimeData(const QModelIndexList& indexes) const override;

            virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
            virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;

            void setModelData(SwiftAccountData* data) { modelData_ = data; }
            void handleDataChanged();
        private:
            SwiftAccountData* modelData_;
    };
}