configwidget.cpp 3.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (c) 2008 Volker Krause <vkrause@kde.org>

    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.
*/

20
#include "configwidget.h"
21
#include "settings.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "resources/folderarchivesettings/folderarchivesettingpage.h"
23

Volker Krause's avatar
Volker Krause committed
24 25
#include <maildir.h>

26 27 28
#include <KConfigDialogManager>
#include <KUrlRequester>
#include <KLineEdit>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QVBoxLayout>
Volker Krause's avatar
Volker Krause committed
30 31

using KPIM::Maildir;
32
using namespace Akonadi_Maildir_Resource;
33

34 35
ConfigWidget::ConfigWidget(MaildirSettings *settings, const QString &identifier, QWidget *parent)
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
36 37
    , mSettings(settings)
    , mToplevelIsContainer(false)
38
{
Laurent Montel's avatar
Laurent Montel committed
39
    QWidget *mainWidget = new QWidget(this);
Laurent Montel's avatar
Laurent Montel committed
40
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
41 42
    mainLayout->addWidget(mainWidget);
    ui.setupUi(mainWidget);
Laurent Montel's avatar
Laurent Montel committed
43
    mFolderArchiveSettingPage = new FolderArchiveSettingPage(identifier, this);
Laurent Montel's avatar
Laurent Montel committed
44
    ui.tabWidget->addTab(mFolderArchiveSettingPage, i18n("Archive Folder"));
Laurent Montel's avatar
Laurent Montel committed
45 46 47 48

    ui.kcfg_Path->setMode(KFile::Directory | KFile::ExistingOnly);
    ui.kcfg_Path->setUrl(QUrl::fromLocalFile(mSettings->path()));

49
    connect(ui.kcfg_Path->lineEdit(), &QLineEdit::textChanged, this, &ConfigWidget::checkPath);
Laurent Montel's avatar
Laurent Montel committed
50 51
    ui.kcfg_Path->lineEdit()->setFocus();
    checkPath();
Laurent Montel's avatar
Laurent Montel committed
52 53
}

54
ConfigWidget::~ConfigWidget()
Laurent Montel's avatar
Laurent Montel committed
55
{
Volker Krause's avatar
Volker Krause committed
56 57
}

58
void ConfigWidget::checkPath()
Volker Krause's avatar
Volker Krause committed
59
{
Laurent Montel's avatar
Laurent Montel committed
60 61
    if (ui.kcfg_Path->url().isEmpty()) {
        ui.statusLabel->setText(i18n("The selected path is empty."));
62
        Q_EMIT okEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
63
        return;
Volker Krause's avatar
Volker Krause committed
64
    }
Laurent Montel's avatar
Laurent Montel committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    bool ok = false;
    mToplevelIsContainer = false;
    QDir d(ui.kcfg_Path->url().toLocalFile());

    if (d.exists()) {
        Maildir md(d.path());
        if (!md.isValid(false)) {
            Maildir md2(d.path(), true);
            if (md2.isValid(false)) {
                ui.statusLabel->setText(i18n("The selected path contains valid Maildir folders."));
                mToplevelIsContainer = true;
                ok = true;
            } else {
                ui.statusLabel->setText(md.lastError());
            }
        } else {
            ui.statusLabel->setText(i18n("The selected path is a valid Maildir."));
            ok = true;
        }
Volker Krause's avatar
Volker Krause committed
84
    } else {
Laurent Montel's avatar
Laurent Montel committed
85 86 87 88 89 90 91 92
        d.cdUp();
        if (d.exists()) {
            ui.statusLabel->setText(i18n("The selected path does not exist yet, a new Maildir will be created."));
            mToplevelIsContainer = true;
            ok = true;
        } else {
            ui.statusLabel->setText(i18n("The selected path does not exist."));
        }
Volker Krause's avatar
Volker Krause committed
93
    }
94
    Q_EMIT okEnabled(ok);
95 96
}

97 98 99 100 101 102 103 104
void ConfigWidget::load()
{
    mFolderArchiveSettingPage->loadSettings();
    mManager = new KConfigDialogManager(this, mSettings);
    mManager->updateWidgets();
}

bool ConfigWidget::save() const
105
{
Laurent Montel's avatar
Laurent Montel committed
106 107 108 109 110
    mFolderArchiveSettingPage->writeSettings();
    mManager->updateSettings();
    QString path = ui.kcfg_Path->url().isLocalFile() ? ui.kcfg_Path->url().toLocalFile() : ui.kcfg_Path->url().path();
    mSettings->setPath(path);
    mSettings->setTopLevelIsContainer(mToplevelIsContainer);
111
    mSettings->save();
Laurent Montel's avatar
Laurent Montel committed
112 113 114 115 116 117

    if (ui.kcfg_Path->url().isLocalFile()) {
        QDir d(path);
        if (!d.exists()) {
            d.mkpath(ui.kcfg_Path->url().toLocalFile());
        }
118
    }
119 120

    return true;
121
}