summaryrefslogtreecommitdiff
path: root/src/frontend/bookmarks/btbookmarkfolder.cpp
blob: 3088e9901c9f5b5a26f1f01f7a0eaa518ed20f19 (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/bookmarks/btbookmarkfolder.h"

#include <QFileDialog>
#include "bibletimeapp.h"
#include "frontend/bookmarks/btbookmarkitembase.h"
#include "frontend/bookmarks/btbookmarkitem.h"
#include "frontend/bookmarks/btbookmarkloader.h"
#include "util/cresmgr.h"
#include "util/geticon.h"


BtBookmarkFolder::BtBookmarkFolder(const QString &name, QTreeWidgetItem *parent)
        : BtBookmarkItemBase(parent) {
    setText(0, name);
    setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
}

bool BtBookmarkFolder::enableAction(MenuAction action) {
    if (action == ChangeFolder || action == NewFolder || action == DeleteEntries || action == ImportBookmarks )
        return true;
    if (action == SortFolderBookmarks || action == ExportBookmarks || action == ImportBookmarks )
        return true;
    if ((action == PrintBookmarks) && childCount())
        return true;
    return false;
}

void BtBookmarkFolder::exportBookmarks() {
    QString filter = QObject::tr("BibleTime bookmark files") + QString(" (*.btb);;") + QObject::tr("All files") + QString(" (*.*)");
    QString fileName = QFileDialog::getSaveFileName(0, QObject::tr("Export Bookmarks"), "", filter);

    if (!fileName.isEmpty()) {
        BtBookmarkLoader loader;
        loader.saveTreeFromRootItem(this, fileName, false ); //false: don't overwrite without asking
    };

}

void BtBookmarkFolder::importBookmarks() {
    QString filter = QObject::tr("BibleTime bookmark files") + QString(" (*.btb);;") + QObject::tr("All files") + QString(" (*.*)");
    QString fileName = QFileDialog::getOpenFileName(0, QObject::tr("Import bookmarks"), "", filter);
    if (!fileName.isEmpty()) {
        BtBookmarkLoader loader;
        QList<QTreeWidgetItem*> itemList = loader.loadTree(fileName);
        this->insertChildren(0, itemList);
    };
}

void BtBookmarkFolder::newSubFolder() {
    if (dynamic_cast<BtBookmarkFolder*>(this)) {
        BtBookmarkFolder* f = new BtBookmarkFolder(QObject::tr("New folder"), this);

        treeWidget()->setCurrentItem(f);
        f->update();
        f->rename();
    }
}

QList<QTreeWidgetItem*> BtBookmarkFolder::getChildList() const {
    QList<QTreeWidgetItem*> list;
    for (int i = 0; i < childCount(); i++) {
        list.append(child(i));
    }
    return list;
}

void BtBookmarkFolder::rename() {
    treeWidget()->editItem(this);
}

void BtBookmarkFolder::update() {
    setIcon(0, util::getIcon(isExpanded() && childCount()
                             ? CResMgr::mainIndex::openedFolder::icon
                             : CResMgr::mainIndex::closedFolder::icon));
}

bool BtBookmarkFolder::hasDescendant(QTreeWidgetItem* item) const {
    if (this == item) {
        return true;
    }
    if (getChildList().indexOf(item) > -1) {
        return true;
    }
    foreach(QTreeWidgetItem* childItem, getChildList()) {
        bool subresult = false;
        BtBookmarkFolder* folder = 0;
        if ( (folder = dynamic_cast<BtBookmarkFolder*>(childItem)) ) {
            subresult = folder->hasDescendant(childItem);
        }

        if (subresult == true) {
            return true;
        }
    }
    return false;
}

BtBookmarkFolder* BtBookmarkFolder::deepCopy() {
    BtBookmarkFolder* newFolder = new BtBookmarkFolder(this->text(0));
    foreach(QTreeWidgetItem* subitem, getChildList()) {
        if (BtBookmarkItem* bmItem = dynamic_cast<BtBookmarkItem*>(subitem)) {
            newFolder->addChild(new BtBookmarkItem(*bmItem));
        }
        else {
            if (BtBookmarkFolder* bmFolder = dynamic_cast<BtBookmarkFolder*>(subitem)) {
                newFolder->addChild(bmFolder->deepCopy());
            }
        }
    }
    newFolder->update();
    return newFolder;
}