singlefileresourceconfigwidgetbase.cpp 6.42 KB
Newer Older
1
2
3
/*
    Copyright (c) 2008 Bertjan Broeksema <b.broeksema@kdemail.org>
    Copyright (c) 2008 Volker Krause <vkrause@kde.org>
4
    Copyright (c) 2010,2011 David Jarvie <djarvie@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

22
#include "singlefileresourceconfigwidgetbase.h"
23

24
#include <KIO/Job>
25
#include <kio_version.h>
Laurent Montel's avatar
Laurent Montel committed
26
#include <QUrl>
27
28
#include <QTimer>

Laurent Montel's avatar
Laurent Montel committed
29
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
30
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
31
32
#include <KConfigDialogManager>

Laurent Montel's avatar
Laurent Montel committed
33
#include <QPushButton>
Allan Sandfeld Jensen's avatar
Allan Sandfeld Jensen committed
34
#include <QTabBar>
Laurent Montel's avatar
Laurent Montel committed
35
#include <QVBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
36

37
38
using namespace Akonadi;

39
40
SingleFileResourceConfigWidgetBase::SingleFileResourceConfigWidgetBase(QWidget *parent)
    : QWidget(parent)
41
{
Laurent Montel's avatar
Laurent Montel committed
42
    QWidget *mainWidget = new QWidget(this);
Laurent Montel's avatar
Laurent Montel committed
43
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
44
    mainLayout->addWidget(mainWidget);
Laurent Montel's avatar
Laurent Montel committed
45
    mainLayout->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
46
47
48
    ui.setupUi(mainWidget);
    ui.kcfg_Path->setMode(KFile::File);
    ui.statusLabel->setText(QString());
49

Laurent Montel's avatar
Laurent Montel committed
50
    ui.tabWidget->tabBar()->hide();
Laurent Montel's avatar
Laurent Montel committed
51

52
53
    connect(ui.kcfg_Path, &KUrlRequester::textChanged, this, &SingleFileResourceConfigWidgetBase::validate);
    connect(ui.kcfg_MonitorFile, &QCheckBox::toggled, this, &SingleFileResourceConfigWidgetBase::validate);
Laurent Montel's avatar
Laurent Montel committed
54
    ui.kcfg_Path->setFocus();
55
    QTimer::singleShot(0, this, &SingleFileResourceConfigWidgetBase::validate);
Laurent Montel's avatar
Laurent Montel committed
56
57
}

58
SingleFileResourceConfigWidgetBase::~SingleFileResourceConfigWidgetBase()
Laurent Montel's avatar
Laurent Montel committed
59
60
61
{
}

62
void SingleFileResourceConfigWidgetBase::addPage(const QString &title, QWidget *page)
63
{
Laurent Montel's avatar
Laurent Montel committed
64
65
    ui.tabWidget->tabBar()->show();
    ui.tabWidget->addTab(page, title);
Laurent Montel's avatar
Laurent Montel committed
66
67
    mManager->addWidget(page);
    mManager->updateWidgets();
68
69
}

70
void SingleFileResourceConfigWidgetBase::setFilter(const QString &filter)
71
{
Laurent Montel's avatar
Laurent Montel committed
72
    ui.kcfg_Path->setFilter(filter);
73
74
}

75
void SingleFileResourceConfigWidgetBase::setMonitorEnabled(bool enable)
76
{
Laurent Montel's avatar
Laurent Montel committed
77
78
    mMonitorEnabled = enable;
    ui.groupBox_MonitorFile->setVisible(mMonitorEnabled);
79
80
}

81
void SingleFileResourceConfigWidgetBase::setUrl(const QUrl &url)
82
{
Laurent Montel's avatar
Laurent Montel committed
83
    ui.kcfg_Path->setUrl(url);
84
85
}

86
QUrl SingleFileResourceConfigWidgetBase::url() const
87
{
Laurent Montel's avatar
Laurent Montel committed
88
    return ui.kcfg_Path->url();
Bertjan Broeksema's avatar
Bertjan Broeksema committed
89
90
}

91
void SingleFileResourceConfigWidgetBase::setLocalFileOnly(bool local)
92
{
Laurent Montel's avatar
Laurent Montel committed
93
94
    mLocalFileOnly = local;
    ui.kcfg_Path->setMode(mLocalFileOnly ? KFile::File | KFile::LocalOnly : KFile::File);
95
96
}

97
void SingleFileResourceConfigWidgetBase::appendWidget(SingleFileValidatingWidget *widget)
98
{
Laurent Montel's avatar
Laurent Montel committed
99
100
    widget->setParent(static_cast<QWidget *>(ui.tab));
    ui.tabLayout->addWidget(widget);
101
    connect(widget, &SingleFileValidatingWidget::changed, this, &SingleFileResourceConfigWidgetBase::validate);
Laurent Montel's avatar
Laurent Montel committed
102
    mAppendedWidget = widget;
103
104
}

105
void SingleFileResourceConfigWidgetBase::validate()
106
{
Laurent Montel's avatar
Laurent Montel committed
107
    if (mAppendedWidget && !mAppendedWidget->validate()) {
108
        Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
109
110
111
112
        return;
    }

    const QUrl currentUrl = ui.kcfg_Path->url();
113
    if (ui.kcfg_Path->text().trimmed().isEmpty() || currentUrl.isEmpty()) {
114
        Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
115
        return;
116
    }
Laurent Montel's avatar
Laurent Montel committed
117
118
119
120
121
122

    if (currentUrl.isLocalFile()) {
        if (mMonitorEnabled) {
            ui.kcfg_MonitorFile->setEnabled(true);
        }
        ui.statusLabel->setText(QString());
123

Laurent Montel's avatar
Laurent Montel committed
124
125
126
127
        // The read-only checkbox used to be disabled if the file is read-only,
        // but it is then impossible to know at a later date if the file
        // permissions change, whether the user actually wanted the resource to be
        // read-only or not. So just leave the read-only checkbox untouched.
128
        Q_EMIT okEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
129
130
131
    } else {
        // Not a local file.
        if (mLocalFileOnly) {
132
            Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
133
134
135
136
137
138
            return;
        }
        if (mMonitorEnabled) {
            ui.kcfg_MonitorFile->setEnabled(false);
        }
        ui.statusLabel->setText(i18nc("@info:status", "Checking file information..."));
139

Laurent Montel's avatar
Laurent Montel committed
140
141
142
        if (mStatJob) {
            mStatJob->kill();
        }
143
144
145
146
147
#if KIO_VERSION < QT_VERSION_CHECK(5, 69, 0)
        mStatJob = KIO::stat(currentUrl, KIO::StatJob::SourceSide, 2, KIO::DefaultFlags | KIO::HideProgressInfo);
#else
        mStatJob = KIO::statDetails(currentUrl, KIO::StatJob::SourceSide, KIO::StatDetail::StatDefaultDetails, KIO::DefaultFlags | KIO::HideProgressInfo);
#endif
148

149
        connect(mStatJob, &KIO::StatJob::result, this, &SingleFileResourceConfigWidgetBase::slotStatJobResult);
150

Laurent Montel's avatar
Laurent Montel committed
151
        // Allow the OK button to be disabled until the MetaJob is finished.
152
        Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
153
    }
154
155
}

156
void SingleFileResourceConfigWidgetBase::slotStatJobResult(KJob *job)
157
{
Laurent Montel's avatar
Laurent Montel committed
158
159
160
    if (job->error() == KIO::ERR_DOES_NOT_EXIST && !mDirUrlChecked) {
        // The file did not exist, so let's see if the directory the file should
        // reside in supports writing.
Laurent Montel's avatar
Laurent Montel committed
161

Laurent Montel's avatar
Laurent Montel committed
162
163
        QUrl dirUrl(ui.kcfg_Path->url());
        dirUrl = KIO::upUrl(dirUrl);
164

165
166
167
168
169
170
#if KIO_VERSION < QT_VERSION_CHECK(5, 69, 0)
        mStatJob = KIO::stat(dirUrl, KIO::StatJob::SourceSide, 2, KIO::DefaultFlags | KIO::HideProgressInfo);
#else
        mStatJob = KIO::statDetails(dirUrl, KIO::StatJob::SourceSide, KIO::StatDetail::StatDefaultDetails, KIO::DefaultFlags | KIO::HideProgressInfo);
#endif

171

172
        connect(mStatJob, &KIO::StatJob::result, this, &SingleFileResourceConfigWidgetBase::slotStatJobResult);
173

Laurent Montel's avatar
Laurent Montel committed
174
175
176
177
178
179
180
        // Make sure we don't check the whole path upwards.
        mDirUrlChecked = true;
        return;
    } else if (job->error()) {
        // It doesn't seem possible to read nor write from the location so leave the
        // ok button disabled
        ui.statusLabel->setText(QString());
181
        Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
182
        mDirUrlChecked = false;
Laurent Montel's avatar
Laurent Montel committed
183
        mStatJob = nullptr;
Laurent Montel's avatar
Laurent Montel committed
184
185
        return;
    }
186

Laurent Montel's avatar
Laurent Montel committed
187
    ui.statusLabel->setText(QString());
188
    Q_EMIT okEnabled(true);
189

Laurent Montel's avatar
Laurent Montel committed
190
    mDirUrlChecked = false;
Laurent Montel's avatar
Laurent Montel committed
191
    mStatJob = nullptr;
192
193
}

Laurent Montel's avatar
Laurent Montel committed
194
195
SingleFileValidatingWidget::SingleFileValidatingWidget(QWidget *parent)
    : QWidget(parent)
Bertjan Broeksema's avatar
Bertjan Broeksema committed
196
{
197
}