KoConfigAuthorPage.cpp 17.7 KB
Newer Older
1 2 3 4
/* 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>
5
                 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

   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"

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

30
#include <klocalizedstring.h>
31
#include <kuser.h>
32
#include <kemailsettings.h>
33
#include <kconfiggroup.h>
34
#include <ksharedconfig.h>
35 36

#include <QLineEdit>
37
#include <QCompleter>
38 39 40 41 42 43 44 45
#include <QStackedWidget>
#include <QList>
#include <QComboBox>
#include <QGridLayout>
#include <QString>
#include <QStringList>
#include <QToolButton>
#include <QInputDialog>
46 47 48 49 50 51 52 53
#include <QTableView>
#include <QStandardItem>
#include <QLabel>
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
#include <QDir>
#include <QByteArray>
54

55
class Q_DECL_HIDDEN KoConfigAuthorPage::Private
56 57 58 59
{
public:
    QList<Ui::KoConfigAuthorPage *> profileUiList;
    QStackedWidget *stack;
60 61
    QComboBox *cmbAuthorProfiles;
    QToolButton *bnDeleteUser;
62 63 64 65
    QStringList positions;
    QStringList contactModes;
    QStringList contactKeys;
    QString defaultAuthor;
66 67 68 69 70 71 72 73
};


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

74 75
    d->cmbAuthorProfiles = new QComboBox();
    layout->addWidget(d->cmbAuthorProfiles, 0, 0);
76
    QToolButton *newUser = new QToolButton();
77
    newUser->setIcon(koIcon("list-add"));
78 79
    newUser->setToolTip(i18n("Add new author profile (starts out as a copy of current)"));
    layout->addWidget(newUser, 0, 1);
80 81 82 83
    d->bnDeleteUser = new QToolButton();
    d->bnDeleteUser->setIcon(koIcon("trash-empty"));
    d->bnDeleteUser->setToolTip(i18n("Delete the author profile"));
    layout->addWidget(d->bnDeleteUser, 0, 2);
84
    QFrame *f = new QFrame();
85 86 87 88 89 90
    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);

91
    //list of positions that we can use to provide useful autocompletion.
luz paz's avatar
luz paz committed
92
    d->positions << QString(i18nc("This is a list of suggestions for positions an artist can take, comma-separated","Adapter,Animator,Artist,Art Director,Author,Assistant,"
Yuri Chornoivan's avatar
Yuri Chornoivan committed
93 94
                                 "Editor,Background,Cartoonist,Colorist,Concept Artist,"
                                 "Corrector,Cover Artist,Creator,Designer,Inker,"
95 96 97
                                 "Letterer,Matte Painter,Painter,Penciller,Proofreader,"
                                 "Pixel Artist,Redliner,Sprite Artist,Typographer,Texture Artist,"
                                 "Translator,Writer,Other")).split(",");
98 99 100 101 102 103 104

    //Keep these two in sync!
    d->contactModes << i18n("Homepage") << i18n("Email") << i18n("Post Address") << i18n("Telephone") << i18n("Fax");
    d->contactKeys << "homepage" << "email" << "address" << "telephone" << "fax";
    QStringList headerlabels;
    headerlabels<< i18n("Type") << i18n("Entry");

105
    Ui::KoConfigAuthorPage *aUi = 0;
106
    QWidget *w = new QWidget;
107
    d->defaultAuthor = i18n("Anonymous");
108 109

    QStringList profilesNew;
110
    QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/authorinfo/");
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
    QStringList filters = QStringList() << "*.authorinfo";
    Q_FOREACH(const QString &entry, dir.entryList(filters)) {
        QFile file(dir.absoluteFilePath(entry));
        if (file.exists()) {
            file.open(QFile::ReadOnly);
            QByteArray ba = file.readAll();
            file.close();
            QDomDocument doc = QDomDocument();
            doc.setContent(ba);
            QDomElement root = doc.firstChildElement();
            aUi = new Ui::KoConfigAuthorPage();
            w = new QWidget;
            aUi->setupUi(w);
            QString profile = root.attribute("name");

            QDomElement el = root.firstChildElement("nickname");
            if (!el.isNull()) {
                aUi->leNickName->setText(el.text());
            }
            el = root.firstChildElement("givenname");
            if (!el.isNull()) {
                aUi->leFirstName->setText(el.text());
            }
            el = root.firstChildElement("middlename");
            if (!el.isNull()) {
                aUi->leInitials->setText(el.text());
            }
            el = root.firstChildElement("familyname");
            if (!el.isNull()) {
                aUi->leLastName->setText(el.text());
            }
            el = root.firstChildElement("title");
            if (!el.isNull()) {
                aUi->leTitle->setText(el.text());
            }
            el = root.firstChildElement("position");
            if (!el.isNull()) {
                aUi->lePosition->setText(el.text());
            }
            el = root.firstChildElement("company");
            if (!el.isNull()) {
                aUi->leCompany->setText(el.text());
            }

            aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
            QStandardItemModel *modes = new QStandardItemModel();
            aUi->tblContactInfo->setModel(modes);
            el = root.firstChildElement("contact");
            while (!el.isNull()) {
                QList<QStandardItem *> list;
                QString type = d->contactModes.at(d->contactKeys.indexOf(el.attribute("type")));
                list.append(new QStandardItem(type));
                list.append(new QStandardItem(el.text()));
                modes->appendRow(list);
                el = el.nextSiblingElement("contact");
            }
            modes->setHorizontalHeaderLabels(headerlabels);
168 169 170
            QCompleter *positionSuggestions = new QCompleter(d->positions);
            positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
            aUi->lePosition->setCompleter(positionSuggestions);
171 172 173 174

            connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
            connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));

175
            d->cmbAuthorProfiles->addItem(profile);
176 177 178 179 180 181 182
            profilesNew.append(profile);
            d->profileUiList.append(aUi);
            d->stack->addWidget(w);
        }
    }

    // Add all the user defined profiles (old type)
183
    KConfigGroup authorGroup(KSharedConfig::openConfig(), "Author");
184 185
    QStringList profiles = authorGroup.readEntry("profile-names", QStringList());

186 187

    foreach (const QString &profile , profiles) {
188
        if (!profilesNew.contains(profile)) {
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            KConfigGroup cgs(&authorGroup, "Author-" + profile);
            aUi = new Ui::KoConfigAuthorPage();
            w = new QWidget;
            aUi->setupUi(w);
            aUi->leNickName->setText(cgs.readEntry("creator"));
            aUi->leFirstName->setText(cgs.readEntry("creator-first-name"));
            aUi->leLastName->setText(cgs.readEntry("creator-last-name"));
            aUi->leInitials->setText(cgs.readEntry("initial"));
            aUi->leTitle->setText(cgs.readEntry("author-title"));
            aUi->lePosition->setText(cgs.readEntry("position"));
            QCompleter *positionSuggestions = new QCompleter(d->positions);
            positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
            aUi->lePosition->setCompleter(positionSuggestions);
            aUi->leCompany->setText(cgs.readEntry("company"));

            aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
            QStandardItemModel *modes = new QStandardItemModel();
            aUi->tblContactInfo->setModel(modes);
            if (cgs.hasKey("email")) {
                QList<QStandardItem *> list;
                QString email = d->contactModes.at(d->contactKeys.indexOf("email"));
                list.append(new QStandardItem(email));
                list.append(new QStandardItem(cgs.readEntry("email")));
                modes->appendRow(list);
            }
            if (cgs.hasKey("telephone-work")) {
                QList<QStandardItem *> list;
                QString tel = d->contactModes.at(d->contactKeys.indexOf("telephone"));
                list.append(new QStandardItem(tel));
                list.append(new QStandardItem(cgs.readEntry("telephone-work")));
                modes->appendRow(list);
            }
            if (cgs.hasKey("fax")) {
                QList<QStandardItem *> list;
                QString fax = d->contactModes.at(d->contactKeys.indexOf("fax"));
                list.append(new QStandardItem(fax));
                list.append(new QStandardItem(cgs.readEntry("fax")));
                modes->appendRow(list);
            }
            QStringList postal;
            postal << cgs.readEntry("street") << cgs.readEntry("postal-code") << cgs.readEntry("city") << cgs.readEntry("country");
            QString address;
            Q_FOREACH(QString part, postal) {
                if (!part.isEmpty()) {
                    address+= part + "\n";
                }
            }
            if (!address.isEmpty()) {
                QList<QStandardItem *> list;
                QString add = d->contactModes.at(d->contactKeys.indexOf("address"));
                list.append(new QStandardItem(add));
                list.append(new QStandardItem(address));
                modes->appendRow(list);
            }
            modes->setHorizontalHeaderLabels(headerlabels);
            connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
            connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));

247
            d->cmbAuthorProfiles->addItem(profile);
248 249 250
            d->profileUiList.append(aUi);
            d->stack->addWidget(w);
        }
251 252
    }

253 254 255 256 257 258 259 260 261 262

    // Add a default profile
    aUi = new Ui::KoConfigAuthorPage();
    w = new QWidget;
    if (!profiles.contains(d->defaultAuthor) || profilesNew.contains(d->defaultAuthor)) {
        //w->setEnabled(false);
        aUi->setupUi(w);
        w->setEnabled(false);
        d->cmbAuthorProfiles->insertItem(0, d->defaultAuthor);
        d->stack->insertWidget(0, w);
263
        d->profileUiList.insert(0, aUi);
264 265 266
    }


267
    // Connect slots
268
    connect(d->cmbAuthorProfiles, SIGNAL(currentIndexChanged(int)), this, SLOT(profileChanged(int)));
269
    connect(newUser, SIGNAL(clicked(bool)), this, SLOT(addUser()));
270 271 272
    connect(d->bnDeleteUser, SIGNAL(clicked(bool)), this, SLOT(deleteUser()));

    d->cmbAuthorProfiles->setCurrentIndex(0);
273 274 275 276 277 278 279 280 281 282 283
    profileChanged(0);
}

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

void KoConfigAuthorPage::profileChanged(int i)
{
    d->stack->setCurrentIndex(i);
284 285
    // Profile 0 should never be deleted: it's the anonymous profile.
    d->bnDeleteUser->setEnabled(i > 0);
286 287 288 289 290
}

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

    if (!ok) {
        return;
    }

297
    Ui::KoConfigAuthorPage *curUi = d->profileUiList[d->cmbAuthorProfiles->currentIndex()];
298 299 300 301
    Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage();
    QWidget *w = new QWidget;
    aUi->setupUi(w);

302
    aUi->leNickName->setText(curUi->leNickName->text());
303 304 305
    aUi->leInitials->setText(curUi->leInitials->text());
    aUi->leTitle->setText(curUi->leTitle->text());
    aUi->leCompany->setText(curUi->leCompany->text());
306 307
    aUi->leFirstName->setText(curUi->leFirstName->text());
    aUi->leLastName->setText(curUi->leLastName->text());
308
    aUi->lePosition->setText(curUi->lePosition->text());
309 310 311 312 313 314 315 316 317
    QCompleter *positionSuggestions = new QCompleter(d->positions);
    positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
    aUi->lePosition->setCompleter(positionSuggestions);
    aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
    QStandardItemModel *modes = new QStandardItemModel();
    aUi->tblContactInfo->setModel(modes);

    connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
    connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
318

319 320
    int index = d->cmbAuthorProfiles->currentIndex() + 1;
    d->cmbAuthorProfiles->insertItem(index, profileName);
321 322
    d->profileUiList.insert(index, aUi);
    d->stack->insertWidget(index, w);
323
    d->cmbAuthorProfiles->setCurrentIndex(index);
324 325 326 327
}

void KoConfigAuthorPage::deleteUser()
{
328
    int index = d->cmbAuthorProfiles->currentIndex();
329 330 331 332
    QWidget *w = d->stack->currentWidget();

    d->stack->removeWidget(w);
    d->profileUiList.removeAt(index);
333
    d->cmbAuthorProfiles->removeItem(index);
334 335 336
    delete w;
}

337 338
void KoConfigAuthorPage::addContactEntry()
{
339
    int i = d->cmbAuthorProfiles->currentIndex();
340 341 342 343 344 345 346 347 348 349 350
    Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
    QStandardItemModel *contact = static_cast<QStandardItemModel*>(aUi->tblContactInfo->model());
    QList<QStandardItem*>list;
    list.append(new QStandardItem(d->contactModes.at(0)));
    list.append(new QStandardItem(i18n("New Contact Info")));
    contact->appendRow(list);
    aUi->tblContactInfo->setModel(contact);
}

void KoConfigAuthorPage::removeContactEntry()
{
351
    int i = d->cmbAuthorProfiles->currentIndex();
352 353 354 355 356
    Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
    QModelIndex index = aUi->tblContactInfo->selectionModel()->currentIndex();
    aUi->tblContactInfo->model()->removeRow(index.row());
}

357 358
void KoConfigAuthorPage::apply()
{
359 360 361 362 363 364 365
    QString authorInfo = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/authorinfo/";
    QDir dir(authorInfo);
    if (!dir.mkpath(authorInfo)) {
        qWarning()<<"We can't make an author info directory, and therefore not save!";
        return;
    }
    for (int i = 0; i < d->profileUiList.size(); i++) {
366
        if (d->cmbAuthorProfiles->itemText(i)!= d->defaultAuthor) {
367 368 369 370 371
            QByteArray ba;
            QDomDocument doc = QDomDocument();
            Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];

            QDomElement root = doc.createElement("author");
372
            root.setAttribute("name", d->cmbAuthorProfiles->itemText(i));
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

            QDomElement nickname = doc.createElement("nickname");
            nickname.appendChild(doc.createTextNode(aUi->leNickName->text()));
            root.appendChild(nickname);
            QDomElement givenname = doc.createElement("givenname");
            givenname.appendChild(doc.createTextNode(aUi->leFirstName->text()));
            root.appendChild(givenname);
            QDomElement familyname = doc.createElement("familyname");
            familyname.appendChild(doc.createTextNode(aUi->leLastName->text()));
            root.appendChild(familyname);
            QDomElement middlename = doc.createElement("middlename");
            middlename.appendChild(doc.createTextNode(aUi->leInitials->text()));
            root.appendChild(middlename);
            QDomElement title = doc.createElement("title");
            title.appendChild(doc.createTextNode(aUi->leTitle->text()));
            root.appendChild(title);
            QDomElement company = doc.createElement("company");
            company.appendChild(doc.createTextNode(aUi->leCompany->text()));
            root.appendChild(company);
            QDomElement position = doc.createElement("position");
            position.appendChild(doc.createTextNode(aUi->lePosition->text()));
            root.appendChild(position);
            if (aUi->tblContactInfo) {
                if (aUi->tblContactInfo->model()) {
                    for (int i=0; i<aUi->tblContactInfo->model()->rowCount(); i++) {
                        QModelIndex index = aUi->tblContactInfo->model()->index(i, 1);
                        QModelIndex typeIndex = aUi->tblContactInfo->model()->index(i, 0);
                        QDomElement contactEl = doc.createElement("contact");
                        QString content = QVariant(aUi->tblContactInfo->model()->data(index)).toString();
                        contactEl.appendChild(doc.createTextNode(content));
                        QString type = QVariant(aUi->tblContactInfo->model()->data(typeIndex)).toString();
                        contactEl.setAttribute("type", d->contactKeys.at(d->contactModes.indexOf(type)));
                        root.appendChild(contactEl);
                    }
                }
            }
            doc.appendChild(root);
            ba = doc.toByteArray();

412
            QFile f(authorInfo + d->cmbAuthorProfiles->itemText(i) +".authorinfo");
413
            f.open(QFile::WriteOnly);
414 415 416
            if (f.write(ba) < 0) {
                qWarning()<<"Writing author info went wrong:"<<f.errorString();
            }
417 418 419 420 421 422 423 424 425 426 427 428 429 430
            f.close();
        }
    }
}

KoContactInfoDelegate::KoContactInfoDelegate(QWidget *parent, QStringList contactModes): QStyledItemDelegate(parent), m_contactModes(contactModes)
{
}

KoContactInfoDelegate::~KoContactInfoDelegate()
{

}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
431
QWidget* KoContactInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
432 433 434 435 436 437 438 439
{

    if (index.column() > 0) {
        return new QLineEdit(parent);
    } else {
        QComboBox *box = new QComboBox(parent);
        box->addItems(m_contactModes);
        return box;
440 441
    }
}