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

#include "btinstallpathdialog.h"

#include "frontend/bookshelfmanager/instbackend.h"

#include "util/ctoolclass.h"
#include "util/dialogutil.h"
#include "util/directoryutil.h"
#include "util/cresmgr.h"

#include <QString>
#include <QDialog>
#include <QDir>
#include <QGridLayout>
#include <QLabel>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QHeaderView>
#include <QDialogButtonBox>

#include <QDebug>

BtInstallPathDialog::BtInstallPathDialog() {
    setWindowTitle(tr("Bookshelf Paths"));

    QVBoxLayout *mainLayout;
    QHBoxLayout *viewLayout;

    mainLayout = new QVBoxLayout(this);
    viewLayout = new QHBoxLayout();

    QString l1 = tr("Works can be installed in one or more directories. After setting up directories here you can choose one of them in Install page.");
    QString l2 = tr("BibleTime and the Sword library find the modules from  all of these directories. If the directory is removed here it still exists in the system with all the works in it. \".sword\" directory in your home directory is always used automatically and can't be removed or added.");

    QLabel* mainLabel = CToolClass::explanationLabel(this,
                        tr("Configure bookshelf paths"), l1 + QString("<small><br><br>") + l2 + QString("</small>"));
    mainLayout->addWidget(mainLabel);

    QString swordConfPath = instbackend::swordConfigFilename();
    QLabel* confPathLabel = new QLabel(tr("Configuration file for the paths is: ").append("<b>%1</b>").arg(swordConfPath), this);
    confPathLabel->setWordWrap(true);
    mainLayout->addWidget(confPathLabel);


    m_swordPathListBox = new QTreeWidget(this);
    m_swordPathListBox->header()->hide();

    QDir swordDir = instbackend::swordDir();
    QStringList targets = instbackend::targetList();
    foreach (QString pathname, targets)  {
        if (pathname.isEmpty() || QDir(pathname) == swordDir) continue;
        new QTreeWidgetItem(m_swordPathListBox, QStringList(pathname) );
    }

    viewLayout->addWidget(m_swordPathListBox);

    QVBoxLayout* buttonLayout = new QVBoxLayout();

    m_addButton = new QPushButton(tr("Add..."), this);
    m_addButton->setToolTip(tr("Add new path"));
    m_addButton->setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::bookshelfmgr::paths::add_icon));
    connect(m_addButton, SIGNAL(clicked()), this, SLOT(slotAddClicked()));
    buttonLayout->addWidget(m_addButton);

    m_editButton = new QPushButton(tr("Edit..."), this);
    m_editButton->setToolTip(tr("Edit the selected path"));
    m_editButton->setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::bookshelfmgr::paths::edit_icon));
    connect(m_editButton, SIGNAL(clicked()), this, SLOT(slotEditClicked()));
    buttonLayout->addWidget(m_editButton);

    m_removeButton = new QPushButton(tr("Remove"), this);
    m_removeButton->setToolTip(tr("Remove the selected path"));
    m_removeButton->setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::bookshelfmgr::paths::remove_icon));
    connect(m_removeButton, SIGNAL(clicked()), this, SLOT(slotRemoveClicked()));
    buttonLayout->addWidget(m_removeButton);

    QSpacerItem* spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    buttonLayout->addItem(spacerItem);

    viewLayout->addLayout(buttonLayout);
    mainLayout->addLayout(viewLayout);

    QDialogButtonBox* buttonBox = new QDialogButtonBox(this);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::NoButton | QDialogButtonBox::Ok);
    util::prepareDialogBox(buttonBox);
    mainLayout->addWidget(buttonBox);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

}

void BtInstallPathDialog::slotEditClicked() {
    if (QTreeWidgetItem* i = m_swordPathListBox->currentItem()) {
        QString dirname = QFileDialog::getExistingDirectory(this, tr("Choose directory"), i->text(0), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

        if (dirname.isEmpty()) { // if user cancelled the dialog
            return;
        }
        QDir dir = QDir(dirname);
        if (dir.isReadable()) {
            const QFileInfo fi( dir.canonicalPath() );
            if (!fi.exists() || !fi.isWritable()) {
                const int result = QMessageBox::warning(this, tr("Use Directory?"), tr("This directory is not writable, so works can not be installed here using BibleTime. Do you want to use this directory instead of the previous value?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
                if (result != QMessageBox::Yes) return;
            }
            i->setText(0, dir.absolutePath()); // absolute, not canonical
        }
    }
}

void BtInstallPathDialog::slotAddClicked() {
    QString dirname = QFileDialog::getExistingDirectory(this, tr("Choose directory"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if (dirname.isEmpty()) { // if user cancelled the dialog
        return;
    }
    QDir dir = QDir(dirname);
    if (dir.isReadable()) {
        const QFileInfo fi( dir.canonicalPath() );
        if (!fi.exists() || !fi.isWritable()) {
            const int result = QMessageBox::warning(this, tr("Warning"), tr("This directory is not writable, so works can not be installed here using BibleTime. Do you still want to add it to the list of bookshelf directories?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
            if (result != QMessageBox::Yes) {
                return;
            }
        }
        new QTreeWidgetItem(m_swordPathListBox, QStringList(dir.canonicalPath()) );
    }
}

void BtInstallPathDialog::slotRemoveClicked() {
    QTreeWidgetItem* i = m_swordPathListBox->currentItem();
    if (i) {
        delete i;
    }
}

void BtInstallPathDialog::writeSwordConfig() {
    qDebug("BtInstallPathDialog::writeSwordConfig");
    if (m_swordPathListBox->topLevelItemCount() >= 0) {
        QStringList targets;
        QTreeWidgetItemIterator it(m_swordPathListBox);
        while (*it) {
            if (!(*it)->text(0).isEmpty()) {
                targets << (*it)->text(0);
            }
            ++it;
        }
        qDebug() << "save the target list" << targets;
        instbackend::setTargetList(targets); //creates new Sword config
    }
}

void BtInstallPathDialog::accept() {
    writeSwordConfig();
    QDialog::accept();
}