summaryrefslogtreecommitdiff
path: root/browser/bookmarkmanager.cpp
blob: d7cc67b9f53e2184c2320f8bc9060acc4d617c88 (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * Copyright (C) 2013, Pelagicore
 *
 * Author: Marcel Schuette <marcel.schuette@pelagicore.com>
 *
 * This file is part of the GENIVI project Browser Proof-Of-Concept
 * For further information, see http://genivi.org/
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "bookmarkmanager.h"

#include <QDebug>


bookmarkmanager::bookmarkmanager(QObject *parent) :
    QObject(parent), lastgivenUID(0)
{
    qDebug() << __PRETTY_FUNCTION__;

    // read stored bookmarks
    lastgivenUID = bookmarksettings.value("lastgivenUID").toInt();

    int size = bookmarksettings.beginReadArray("bookmarks");
    for (int i = 0; i < size; ++i) {
        bookmarksettings.setArrayIndex(i);
        Bookmark *temp_bookmark = new Bookmark();

        temp_bookmark->setUid(bookmarksettings.value("i32Uid").toInt());
        temp_bookmark->setType(bookmarksettings.value("i32Type").toInt());
        temp_bookmark->setParentFolderPath(bookmarksettings.value("strParentFolderPath").toString());
        temp_bookmark->setTitle(bookmarksettings.value("strTitle").toString());
        temp_bookmark->setUrl(bookmarksettings.value("strUrl").toString());
        temp_bookmark->setIconPath(bookmarksettings.value("strIconPath").toString());
        temp_bookmark->setThumbnailPath(bookmarksettings.value("strThumbnailPath").toString());

        bookmarklist.append(temp_bookmark);
    }
    bookmarksettings.endArray();
}

conn::brw::ERROR_IDS bookmarkmanager::addItem(const conn::brw::BookmarkItem & a_oItem) {
    qDebug() << __PRETTY_FUNCTION__ << a_oItem.i32Uid << a_oItem.i32Type << a_oItem.strParentFolderPath << a_oItem.strTitle
                << a_oItem.strUrl << a_oItem.strIconPath << a_oItem.strThumbnailPath;

    // check if item is valid (url is valid)
    if(a_oItem.strUrl != "http://") {
        // check if max items reached
        if(bookmarklist.size() >= 50) {
            return conn::brw::EID_MAX_NUMBER_REACHED;
        }

        // add item
        Bookmark *temp_bookmark = new Bookmark();
        temp_bookmark->setUid(++lastgivenUID);
        temp_bookmark->setType(a_oItem.i32Type);
        temp_bookmark->setParentFolderPath(a_oItem.strParentFolderPath);
        temp_bookmark->setTitle(a_oItem.strTitle);
        temp_bookmark->setUrl(a_oItem.strUrl);
        temp_bookmark->setIconPath(a_oItem.strIconPath);
        temp_bookmark->setThumbnailPath(a_oItem.strThumbnailPath);

        bookmarklist.append(temp_bookmark);

        // store bookmark
        bookmarksettings.setValue("lastgivenUID",lastgivenUID);

        bookmarksettings.beginWriteArray("bookmarks");
        for (int i = 0; i < bookmarklist.size(); ++i) {
            bookmarksettings.setArrayIndex(i);
            bookmarksettings.setValue("i32Uid", bookmarklist.at(i)->uid());
            bookmarksettings.setValue("i32Type", bookmarklist.at(i)->type());
            bookmarksettings.setValue("strParentFolderPath", bookmarklist.at(i)->folderpath());
            bookmarksettings.setValue("strTitle", bookmarklist.at(i)->title());
            bookmarksettings.setValue("strUrl", bookmarklist.at(i)->url());
            bookmarksettings.setValue("strIconPath", bookmarklist.at(i)->iconpath());
            bookmarksettings.setValue("strThumbnailPath", bookmarklist.at(i)->thumbnailpath());
        }
        bookmarksettings.endArray();

        return conn::brw::EID_NO_ERROR;
    }
    return conn::brw::EID_INVALID_ARGUMENT;
}

conn::brw::ERROR_IDS bookmarkmanager::deleteAllItems(int type) {
    qDebug() << __PRETTY_FUNCTION__ << type;

    bool success = false;

    // find bookmarks for type and delete
    for (int i = 0; i < bookmarklist.size(); ++i) {
        if (bookmarklist.at(i)->type() == type) {
            bookmarklist.removeAt(i--);
            success = true;
        }
    }

    if(success) {
        bookmarksettings.clear();
        bookmarksettings.setValue("lastgivenUID",lastgivenUID);
        bookmarksettings.beginWriteArray("bookmarks");
        for (int i = 0; i < bookmarklist.size(); ++i) {
            bookmarksettings.setArrayIndex(i);
            bookmarksettings.setValue("i32Uid", bookmarklist.at(i)->uid());
            bookmarksettings.setValue("i32Type", bookmarklist.at(i)->type());
            bookmarksettings.setValue("strParentFolderPath", bookmarklist.at(i)->folderpath());
            bookmarksettings.setValue("strTitle", bookmarklist.at(i)->title());
            bookmarksettings.setValue("strUrl", bookmarklist.at(i)->url());
            bookmarksettings.setValue("strIconPath", bookmarklist.at(i)->iconpath());
            bookmarksettings.setValue("strThumbnailPath", bookmarklist.at(i)->thumbnailpath());
        }
        bookmarksettings.endArray();
    }
    return success ? conn::brw::EID_NO_ERROR : conn::brw::EID_NOT_EXISTS;
}

conn::brw::ERROR_IDS bookmarkmanager::deleteItem(int uid) {
    qDebug() << __PRETTY_FUNCTION__ << uid;

    // find bookmark for uid
    for (int i = 0; i < bookmarklist.size(); ++i) {
        if (bookmarklist.at(i)->uid() == uid) {
            bookmarklist.removeAt(i);

            bookmarksettings.clear();
            bookmarksettings.setValue("lastgivenUID",lastgivenUID);
            bookmarksettings.beginWriteArray("bookmarks");
            for (int i = 0; i < bookmarklist.size(); ++i) {
                bookmarksettings.setArrayIndex(i);
                bookmarksettings.setValue("i32Uid", bookmarklist.at(i)->uid());
                bookmarksettings.setValue("i32Type", bookmarklist.at(i)->type());
                bookmarksettings.setValue("strParentFolderPath", bookmarklist.at(i)->folderpath());
                bookmarksettings.setValue("strTitle", bookmarklist.at(i)->title());
                bookmarksettings.setValue("strUrl", bookmarklist.at(i)->url());
                bookmarksettings.setValue("strIconPath", bookmarklist.at(i)->iconpath());
                bookmarksettings.setValue("strThumbnailPath", bookmarklist.at(i)->thumbnailpath());
            }
            bookmarksettings.endArray();

            return conn::brw::EID_NO_ERROR;
        }
    }
    return conn::brw::EID_NOT_EXISTS;
}

conn::brw::ERROR_IDS bookmarkmanager::getItems(const QString &path, int type, conn::brw::BOOKMARK_SORT_TYPE a_eSortingOrder,
                                               uint index, uint count, conn::brw::BookmarkItemList &a_oItems) {
    qDebug() << __PRETTY_FUNCTION__ << path << type << a_eSortingOrder << index << count;

    conn::brw::BookmarkItem *temp_bookmark = new conn::brw::BookmarkItem();
    QList<conn::brw::BookmarkItem> bmlist;

    uint added = 0;
    uint found = 0;

    for (int i = 0; i < bookmarklist.size(); ++i) {
        if (bookmarklist.at(i)->folderpath() == path && bookmarklist.at(i)->type() == type && added < count) {

            found++;
            if(found >= index) {
                temp_bookmark->i32Type = bookmarklist.at(i)->type();
                temp_bookmark->i32Uid = bookmarklist.at(i)->uid();
                temp_bookmark->strIconPath = bookmarklist.at(i)->iconpath();
                temp_bookmark->strParentFolderPath = bookmarklist.at(i)->folderpath();
                temp_bookmark->strThumbnailPath = bookmarklist.at(i)->thumbnailpath();
                temp_bookmark->strTitle = bookmarklist.at(i)->title();
                temp_bookmark->strUrl = bookmarklist.at(i)->url();

                bmlist.prepend(*temp_bookmark);

                if(bmlist.count() > 1) {
                    switch (a_eSortingOrder) {
                    case conn::brw::BST_TITLE_ASCENDING:
                        for(int i = 0; i < bmlist.count()-1; i++) {
                            if(bmlist.at(i).strTitle > bmlist.at(i+1).strTitle)
                                bmlist.swap(i, i+1);
                            else
                                break;
                        }
                        break;
                    case conn::brw::BST_TITLE_DESCENDING:
                        for(int i = 0; i < bmlist.count()-1; i++) {
                            if(bmlist.at(i).strTitle < bmlist.at(i+1).strTitle)
                                bmlist.swap(i, i+1);
                            else
                                break;
                        }
                        break;
                    case conn::brw::BST_URL_ASCENDING:
                        for(int i = 0; i < bmlist.count()-1; i++) {
                            if(bmlist.at(i).strUrl > bmlist.at(i+1).strUrl)
                                bmlist.swap(i, i+1);
                            else
                                break;
                        }
                        break;
                    case conn::brw::BST_URL_DESCENDING:
                        for(int i = 0; i < bmlist.count()-1; i++) {
                            if(bmlist.at(i).strUrl < bmlist.at(i+1).strUrl)
                                bmlist.swap(i, i+1);
                            else
                                break;
                        }
                        break;
                    default:
                        break;
                    }
                }
                added++;
            }
        }
    }

    if(added != 0) {
        a_oItems = bmlist;
        return conn::brw::EID_NO_ERROR;
    }

    return conn::brw::EID_NOT_EXISTS;
}