KoConfigAuthorPage.cpp 7.53 KB
Newer Older
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
/* This file is part of the KDE project
   Copyright (c) 2000 Simon Hausmann <hausmann@kde.org>
                 2006 Martin Pfeiffer <hubipete@gmx.net>
                 2012 C. Boemann <cbo@boemann.dk>

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

#include "KoConfigAuthorPage.h"

#include "ui_KoConfigAuthorPage.h"

#include <KoGlobal.h>
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
27
#include <KoIcon.h>
28 29 30

#include <klocale.h>
#include <kuser.h>
31
#include <kemailsettings.h>
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

#include <QLineEdit>
#include <QStackedWidget>
#include <QList>
#include <QComboBox>
#include <QGridLayout>
#include <QString>
#include <QStringList>
#include <QToolButton>
#include <QInputDialog>

class KoConfigAuthorPage::Private
{
public:
    QList<Ui::KoConfigAuthorPage *> profileUiList;
    QStackedWidget *stack;
    QComboBox *combo;
    QToolButton *deleteUser;
};


KoConfigAuthorPage::KoConfigAuthorPage()
        : d(new Private)
{
    QGridLayout *layout = new QGridLayout;

    d->combo = new QComboBox;
    layout->addWidget(d->combo, 0, 0);
    QToolButton *newUser = new QToolButton;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
61
    newUser->setIcon(koIcon("list-add-user"));
62 63 64
    newUser->setToolTip(i18n("Add new author profile (starts out as a copy of current)"));
    layout->addWidget(newUser, 0, 1);
    d->deleteUser = new QToolButton;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
65
    d->deleteUser->setIcon(koIcon("list-remove-user"));
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    d->deleteUser->setToolTip(i18n("Delete the author profile"));
    layout->addWidget(d->deleteUser, 0, 2);
    QFrame *f = new QFrame;
    f->setFrameStyle(QFrame::HLine | QFrame::Sunken);
    layout->addWidget(f, 1, 0);
    d->stack = new QStackedWidget();
    layout->addWidget(d->stack, 2, 0, 1, 3);
    setLayout(layout);

    // Add a default profile
    Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage();
    QWidget *w = new QWidget;
    w->setEnabled(false);
    aUi->setupUi(w);
    d->combo->addItem(i18n("Default Author Profile"));
    d->stack->addWidget(w);
    KUser user(KUser::UseRealUserID);
    aUi->leFullName->setText(user.property(KUser::FullName).toString());
84 85 86 87
    aUi->lePhoneWork->setText(user.property(KUser::WorkPhone).toString());
    aUi->lePhoneHome->setText(user.property(KUser::HomePhone).toString());
    KEMailSettings eMailSettings;
    aUi->leEmail->setText(eMailSettings.getSetting(KEMailSettings::EmailAddress));
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
    d->profileUiList.append(aUi);

    // Add all the user defined profiles
    KConfig *config = KoGlobal::calligraConfig();
    KConfigGroup authorGroup(config, "Author");
    QStringList profiles = authorGroup.readEntry("profile-names", QStringList());

    foreach (const QString &profile , profiles) {
        KConfigGroup cgs(&authorGroup, "Author-" + profile);
        aUi = new Ui::KoConfigAuthorPage();
        w = new QWidget;
        aUi->setupUi(w);
        aUi->leFullName->setText(cgs.readEntry("creator"));
        aUi->leInitials->setText(cgs.readEntry("initial"));
        aUi->leTitle->setText(cgs.readEntry("author-title"));
        aUi->leCompany->setText(cgs.readEntry("company"));
        aUi->leEmail->setText(cgs.readEntry("email"));
        aUi->lePhoneWork->setText(cgs.readEntry("telephone-work"));
        aUi->lePhoneHome->setText(cgs.readEntry("telephone"));
        aUi->leFax->setText(cgs.readEntry("fax"));
        aUi->leCountry->setText(cgs.readEntry("country"));
        aUi->lePostal->setText(cgs.readEntry("postal-code"));
        aUi->leCity->setText(cgs.readEntry("city"));
        aUi->leStreet->setText(cgs.readEntry("street"));
        aUi->lePosition->setText(cgs.readEntry("position"));

        d->combo->addItem(profile);
        d->profileUiList.append(aUi);
        d->stack->addWidget(w);
    }

    // Connect slots
    connect(d->combo, SIGNAL(currentIndexChanged(int)), this, SLOT(profileChanged(int)));
    connect(newUser, SIGNAL(clicked(bool)), this, SLOT(addUser()));
    connect(d->deleteUser, SIGNAL(clicked(bool)), this, SLOT(deleteUser()));
    profileChanged(0);
}

KoConfigAuthorPage::~KoConfigAuthorPage()
{
    delete d;
}

void KoConfigAuthorPage::profileChanged(int i)
{
    d->stack->setCurrentIndex(i);
    d->deleteUser->setEnabled(i != 0);
}

void KoConfigAuthorPage::addUser()
{
    bool ok;
    QString profileName = QInputDialog::getText(this, i18n("Name of Profile"), i18n("Name (not duplicate or blank name):"),QLineEdit::Normal, "", &ok);

    if (!ok) {
        return;
    }

    Ui::KoConfigAuthorPage *curUi = d->profileUiList[d->combo->currentIndex()];
    Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage();
    QWidget *w = new QWidget;
    aUi->setupUi(w);

    aUi->leFullName->setText(curUi->leFullName->text());
    aUi->leInitials->setText(curUi->leInitials->text());
    aUi->leTitle->setText(curUi->leTitle->text());
    aUi->leCompany->setText(curUi->leCompany->text());
    aUi->leEmail->setText(curUi->leEmail->text());
    aUi->lePhoneWork->setText(curUi->lePhoneWork->text());
    aUi->lePhoneHome->setText(curUi->lePhoneHome->text());
    aUi->leFax->setText(curUi->leFax->text());
    aUi->leCountry->setText(curUi->leCountry->text());
    aUi->lePostal->setText(curUi->lePostal->text());
    aUi->leCity->setText(curUi->leCity->text());
    aUi->leStreet->setText(curUi->leStreet->text());
    aUi->lePosition->setText(curUi->lePosition->text());

    int index = d->combo->currentIndex() + 1;
    d->combo->insertItem(index, profileName);
    d->profileUiList.insert(index, aUi);
    d->stack->insertWidget(index, w);
    d->combo->setCurrentIndex(index);
}

void KoConfigAuthorPage::deleteUser()
{
    int index = d->combo->currentIndex();
    QWidget *w = d->stack->currentWidget();

    d->stack->removeWidget(w);
    d->profileUiList.removeAt(index);
    d->combo->removeItem(index);
    delete w;
}

void KoConfigAuthorPage::apply()
{
    KConfig *config = KoGlobal::calligraConfig();
    KConfigGroup authorGroup(config, "Author");
    QStringList profiles;

    for (int i = 1; i < d->profileUiList.size(); i++) {
        KConfigGroup cgs(&authorGroup, "Author-" + d->combo->itemText(i));
        profiles.append(d->combo->itemText(i));
        Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
        cgs.writeEntry("creator", aUi->leFullName->text());
        cgs.writeEntry("initial", aUi->leInitials->text());
        cgs.writeEntry("author-title", aUi->leTitle->text());
        cgs.writeEntry("company", aUi->leCompany->text());
        cgs.writeEntry("email", aUi->leEmail->text());
        cgs.writeEntry("telephone-work", aUi->lePhoneWork->text());
        cgs.writeEntry("telephone", aUi->lePhoneHome->text());
        cgs.writeEntry("fax", aUi->leFax->text());
        cgs.writeEntry("country", aUi->leCountry->text());
        cgs.writeEntry("postal-code", aUi->lePostal->text());
        cgs.writeEntry("city", aUi->leCity->text());
        cgs.writeEntry("street", aUi->leStreet->text());
        cgs.writeEntry("position", aUi->lePosition->text());

        cgs.sync();
    }
    authorGroup.writeEntry("profile-names", profiles);
    authorGroup.sync();
}


#include <KoConfigAuthorPage.moc>