aboutsummaryrefslogtreecommitdiff
path: root/src/favouritecollection.cpp.bak
blob: 7b9bec52b86599e387875464a81c1530b7ffa9d4 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "favouritecollection.hpp"

#include <QFile>

FavouriteCollection::FavouriteCollection(QObject *parent) :
    QAbstractListModel(parent)
{

}

void FavouriteCollection::add(QUrl const & url)
{
    if(contains(url))
        return;

    beginInsertRows(QModelIndex{}, items.size(), items.size() + 1);
    items.push_back(url);
    endInsertRows();
}

void FavouriteCollection::remove(QUrl const & url)
{
    for(int i = 0; i < items.size(); i++)
    {
        if(items.at(i) == url) {
            beginRemoveRows(QModelIndex{}, i, i + 1);
            items.removeAt(i);
            endRemoveRows();
            return;
        }
    }
}

bool FavouriteCollection::contains(const QUrl &url)
{
    for(auto const & item : items) {
        if(item == url)
            return true;
    }
    return false;
}

QUrl FavouriteCollection::get(const QModelIndex &index) const
{
    if(index.isValid()) {
        return items.at(index.row());
    } else {
        return QUrl { };
    }
}

bool FavouriteCollection::save(const QString &fileName) const
{
    QFile file(fileName);
    if(not file.open(QFile::WriteOnly))
        return false;

    for(auto const & url: items)
    {
        QByteArray blob = (url.toString() + "\n").toUtf8();

        qint64 offset = 0;
        while(offset < blob.size())
        {
            auto len = file.write(blob.data() + offset, blob.size() - offset);
            if(len <= 0) {
                file.close();
                return false;
            }
            offset += len;
        }
    }

    file.close();
    return true;
}

bool FavouriteCollection::save(QSettings &settings) const
{
    settings.beginWriteArray("favourites", items.size());
    for(int i = 0; i < items.size(); i++)
    {
        settings.setArrayIndex(i);
        settings.setValue("url", items[i].toString());
    }
    settings.endArray();
    return true;
}

bool FavouriteCollection::load(const QString &fileName)
{
    QFile file(fileName);
    if(not file.open(QFile::ReadOnly))
        return false;
    auto data = file.readAll();

    beginResetModel();

    items.clear();
    for(auto line : data.split('\n')) {
        if(line.size() > 0) {
            items.push_back(QUrl(QString::fromUtf8(line)));
        }
    }
    endResetModel();

    return true;
}

bool FavouriteCollection::load(QSettings & settings)
{
    int len = settings.beginReadArray("favourites");
    items.resize(len);
    for(int i = 0; i < items.size(); i++)
    {
        settings.setArrayIndex(i);
        items[i] = settings.value("url").toString();
    }
    settings.endArray();
    return true;
}

int FavouriteCollection::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return items.size();
}

bool FavouriteCollection::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(value)
    Q_UNUSED(index)
    Q_UNUSED(role)
    return false;
}

QVariant FavouriteCollection::data(const QModelIndex &index, int role) const
{
    if(role != Qt::DisplayRole) {
        return QVariant{};
    }
    return items.at(index.row()).toString();
}