Warning, file /office/calligra/libs/widgets/KoConfigAuthorPage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (c) 2000 Simon Hausmann <hausmann@kde.org> 0003 2006 Martin Pfeiffer <hubipete@gmx.net> 0004 2012 C. Boemann <cbo@boemann.dk> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include "KoConfigAuthorPage.h" 0023 0024 #include "ui_KoConfigAuthorPage.h" 0025 0026 #include <KoGlobal.h> 0027 #include <KoIcon.h> 0028 0029 #include <klocalizedstring.h> 0030 #include <kuser.h> 0031 #include <kemailsettings.h> 0032 #include <kconfiggroup.h> 0033 #include <ksharedconfig.h> 0034 0035 #include <QLineEdit> 0036 #include <QStackedWidget> 0037 #include <QList> 0038 #include <QComboBox> 0039 #include <QGridLayout> 0040 #include <QString> 0041 #include <QStringList> 0042 #include <QToolButton> 0043 #include <QInputDialog> 0044 0045 class Q_DECL_HIDDEN KoConfigAuthorPage::Private 0046 { 0047 public: 0048 QList<Ui::KoConfigAuthorPage *> profileUiList; 0049 QStackedWidget *stack; 0050 QComboBox *combo; 0051 QToolButton *deleteUser; 0052 }; 0053 0054 0055 KoConfigAuthorPage::KoConfigAuthorPage() 0056 : d(new Private) 0057 { 0058 QGridLayout *layout = new QGridLayout; 0059 0060 d->combo = new QComboBox; 0061 layout->addWidget(d->combo, 0, 0); 0062 QToolButton *newUser = new QToolButton; 0063 newUser->setIcon(koIcon("list-add-user")); 0064 newUser->setToolTip(i18n("Add new author profile (starts out as a copy of current)")); 0065 layout->addWidget(newUser, 0, 1); 0066 d->deleteUser = new QToolButton; 0067 d->deleteUser->setIcon(koIcon("list-remove-user")); 0068 d->deleteUser->setToolTip(i18n("Delete the author profile")); 0069 layout->addWidget(d->deleteUser, 0, 2); 0070 QFrame *f = new QFrame; 0071 f->setFrameStyle(QFrame::HLine | QFrame::Sunken); 0072 layout->addWidget(f, 1, 0); 0073 d->stack = new QStackedWidget(); 0074 layout->addWidget(d->stack, 2, 0, 1, 3); 0075 setLayout(layout); 0076 0077 // Add a default profile 0078 Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage(); 0079 QWidget *w = new QWidget; 0080 w->setEnabled(false); 0081 aUi->setupUi(w); 0082 d->combo->addItem(i18n("Default Author Profile")); 0083 d->stack->addWidget(w); 0084 KUser user(KUser::UseRealUserID); 0085 aUi->leFullName->setText(user.property(KUser::FullName).toString()); 0086 aUi->lePhoneWork->setText(user.property(KUser::WorkPhone).toString()); 0087 aUi->lePhoneHome->setText(user.property(KUser::HomePhone).toString()); 0088 KEMailSettings eMailSettings; 0089 aUi->leEmail->setText(eMailSettings.getSetting(KEMailSettings::EmailAddress)); 0090 d->profileUiList.append(aUi); 0091 0092 // Add all the user defined profiles 0093 KConfig *config = KoGlobal::calligraConfig(); 0094 KConfigGroup authorGroup(config, "Author"); 0095 QStringList profiles = authorGroup.readEntry("profile-names", QStringList()); 0096 0097 foreach (const QString &profile , profiles) { 0098 KConfigGroup cgs(&authorGroup, "Author-" + profile); 0099 aUi = new Ui::KoConfigAuthorPage(); 0100 w = new QWidget; 0101 aUi->setupUi(w); 0102 aUi->leFullName->setText(cgs.readEntry("creator")); 0103 aUi->leInitials->setText(cgs.readEntry("initial")); 0104 aUi->leTitle->setText(cgs.readEntry("author-title")); 0105 aUi->leCompany->setText(cgs.readEntry("company")); 0106 aUi->leEmail->setText(cgs.readEntry("email")); 0107 aUi->lePhoneWork->setText(cgs.readEntry("telephone-work")); 0108 aUi->lePhoneHome->setText(cgs.readEntry("telephone")); 0109 aUi->leFax->setText(cgs.readEntry("fax")); 0110 aUi->leCountry->setText(cgs.readEntry("country")); 0111 aUi->lePostal->setText(cgs.readEntry("postal-code")); 0112 aUi->leCity->setText(cgs.readEntry("city")); 0113 aUi->leStreet->setText(cgs.readEntry("street")); 0114 aUi->lePosition->setText(cgs.readEntry("position")); 0115 0116 d->combo->addItem(profile); 0117 d->profileUiList.append(aUi); 0118 d->stack->addWidget(w); 0119 } 0120 0121 // Connect slots 0122 connect(d->combo, SIGNAL(currentIndexChanged(int)), this, SLOT(profileChanged(int))); 0123 connect(newUser, SIGNAL(clicked(bool)), this, SLOT(addUser())); 0124 connect(d->deleteUser, SIGNAL(clicked(bool)), this, SLOT(deleteUser())); 0125 profileChanged(0); 0126 } 0127 0128 KoConfigAuthorPage::~KoConfigAuthorPage() 0129 { 0130 delete d; 0131 } 0132 0133 void KoConfigAuthorPage::profileChanged(int i) 0134 { 0135 d->stack->setCurrentIndex(i); 0136 d->deleteUser->setEnabled(i != 0); 0137 } 0138 0139 void KoConfigAuthorPage::addUser() 0140 { 0141 bool ok; 0142 QString profileName = QInputDialog::getText(this, i18n("Name of Profile"), i18n("Name (not duplicate or blank name):"),QLineEdit::Normal, "", &ok); 0143 0144 if (!ok) { 0145 return; 0146 } 0147 0148 Ui::KoConfigAuthorPage *curUi = d->profileUiList[d->combo->currentIndex()]; 0149 Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage(); 0150 QWidget *w = new QWidget; 0151 aUi->setupUi(w); 0152 0153 aUi->leFullName->setText(curUi->leFullName->text()); 0154 aUi->leInitials->setText(curUi->leInitials->text()); 0155 aUi->leTitle->setText(curUi->leTitle->text()); 0156 aUi->leCompany->setText(curUi->leCompany->text()); 0157 aUi->leEmail->setText(curUi->leEmail->text()); 0158 aUi->lePhoneWork->setText(curUi->lePhoneWork->text()); 0159 aUi->lePhoneHome->setText(curUi->lePhoneHome->text()); 0160 aUi->leFax->setText(curUi->leFax->text()); 0161 aUi->leCountry->setText(curUi->leCountry->text()); 0162 aUi->lePostal->setText(curUi->lePostal->text()); 0163 aUi->leCity->setText(curUi->leCity->text()); 0164 aUi->leStreet->setText(curUi->leStreet->text()); 0165 aUi->lePosition->setText(curUi->lePosition->text()); 0166 0167 int index = d->combo->currentIndex() + 1; 0168 d->combo->insertItem(index, profileName); 0169 d->profileUiList.insert(index, aUi); 0170 d->stack->insertWidget(index, w); 0171 d->combo->setCurrentIndex(index); 0172 } 0173 0174 void KoConfigAuthorPage::deleteUser() 0175 { 0176 int index = d->combo->currentIndex(); 0177 QWidget *w = d->stack->currentWidget(); 0178 0179 d->stack->removeWidget(w); 0180 d->profileUiList.removeAt(index); 0181 d->combo->removeItem(index); 0182 delete w; 0183 } 0184 0185 void KoConfigAuthorPage::apply() 0186 { 0187 KConfig *config = KoGlobal::calligraConfig(); 0188 KConfigGroup authorGroup(config, "Author"); 0189 QStringList profiles; 0190 0191 for (int i = 1; i < d->profileUiList.size(); i++) { 0192 KConfigGroup cgs(&authorGroup, "Author-" + d->combo->itemText(i)); 0193 profiles.append(d->combo->itemText(i)); 0194 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i]; 0195 cgs.writeEntry("creator", aUi->leFullName->text()); 0196 cgs.writeEntry("initial", aUi->leInitials->text()); 0197 cgs.writeEntry("author-title", aUi->leTitle->text()); 0198 cgs.writeEntry("company", aUi->leCompany->text()); 0199 cgs.writeEntry("email", aUi->leEmail->text()); 0200 cgs.writeEntry("telephone-work", aUi->lePhoneWork->text()); 0201 cgs.writeEntry("telephone", aUi->lePhoneHome->text()); 0202 cgs.writeEntry("fax", aUi->leFax->text()); 0203 cgs.writeEntry("country", aUi->leCountry->text()); 0204 cgs.writeEntry("postal-code", aUi->lePostal->text()); 0205 cgs.writeEntry("city", aUi->leCity->text()); 0206 cgs.writeEntry("street", aUi->leStreet->text()); 0207 cgs.writeEntry("position", aUi->lePosition->text()); 0208 0209 cgs.sync(); 0210 } 0211 authorGroup.writeEntry("profile-names", profiles); 0212 authorGroup.sync(); 0213 }