| 1 | /* |
|---|
| 2 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 3 | * Copyright (C) 2004-2006 Licq developers |
|---|
| 4 | * |
|---|
| 5 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * Licq is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with Licq; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "phonedlg.h" |
|---|
| 21 | |
|---|
| 22 | #include <QByteArray> |
|---|
| 23 | #include <QCheckBox> |
|---|
| 24 | #include <QComboBox> |
|---|
| 25 | #include <QDialogButtonBox> |
|---|
| 26 | #include <QGridLayout> |
|---|
| 27 | #include <QHBoxLayout> |
|---|
| 28 | #include <QLabel> |
|---|
| 29 | #include <QLineEdit> |
|---|
| 30 | #include <QPixmap> |
|---|
| 31 | #include <QPushButton> |
|---|
| 32 | #include <QTextCodec> |
|---|
| 33 | |
|---|
| 34 | #include <licq_countrycodes.h> |
|---|
| 35 | #include <licq_providers.h> |
|---|
| 36 | |
|---|
| 37 | #include "config/iconmanager.h" |
|---|
| 38 | |
|---|
| 39 | #include "core/messagebox.h" |
|---|
| 40 | |
|---|
| 41 | #include "helpers/support.h" |
|---|
| 42 | #include "helpers/usercodec.h" |
|---|
| 43 | |
|---|
| 44 | using namespace LicqQtGui; |
|---|
| 45 | /* TRANSLATOR LicqQtGui::EditPhoneDlg */ |
|---|
| 46 | |
|---|
| 47 | EditPhoneDlg::EditPhoneDlg(QWidget* parent, const struct PhoneBookEntry* pbe, |
|---|
| 48 | int nEntry) |
|---|
| 49 | : QDialog(parent) |
|---|
| 50 | { |
|---|
| 51 | Support::setWidgetProps(this, "EditPhoneDlg"); |
|---|
| 52 | setAttribute(Qt::WA_DeleteOnClose, true); |
|---|
| 53 | setModal(true); |
|---|
| 54 | |
|---|
| 55 | const ICQOwner* o = gUserManager.FetchOwner(LICQ_PPID, LOCK_R); |
|---|
| 56 | if (o == NULL) |
|---|
| 57 | { |
|---|
| 58 | close(); |
|---|
| 59 | return; |
|---|
| 60 | } |
|---|
| 61 | QTextCodec* codec = UserCodec::codecForICQUser(o); |
|---|
| 62 | gUserManager.DropOwner(o); |
|---|
| 63 | |
|---|
| 64 | m_nEntry = nEntry; |
|---|
| 65 | |
|---|
| 66 | QGridLayout* top_lay = new QGridLayout(this); |
|---|
| 67 | |
|---|
| 68 | // ROW 1 |
|---|
| 69 | top_lay->addWidget(new QLabel(tr("Description:")), 0, 0); |
|---|
| 70 | |
|---|
| 71 | cmbDescription = new QComboBox(); |
|---|
| 72 | cmbDescription->setEditable(true); |
|---|
| 73 | cmbDescription->addItem(tr("Home Phone")); |
|---|
| 74 | cmbDescription->addItem(tr("Work Phone")); |
|---|
| 75 | cmbDescription->addItem(tr("Private Cellular")); |
|---|
| 76 | cmbDescription->addItem(tr("Work Cellular")); |
|---|
| 77 | cmbDescription->addItem(tr("Home Fax")); |
|---|
| 78 | cmbDescription->addItem(tr("Work Fax")); |
|---|
| 79 | cmbDescription->addItem(tr("Wireless Pager")); |
|---|
| 80 | cmbDescription->lineEdit()->setMaxLength(MAX_DESCRIPTION_SIZE); |
|---|
| 81 | cmbDescription->setDuplicatesEnabled(false); |
|---|
| 82 | top_lay->addWidget(cmbDescription, 0, 1); |
|---|
| 83 | |
|---|
| 84 | // ROW 2 |
|---|
| 85 | top_lay->addWidget(new QLabel(tr("Type:")), 1, 0); |
|---|
| 86 | |
|---|
| 87 | IconManager* iconman = IconManager::instance(); |
|---|
| 88 | |
|---|
| 89 | cmbType = new QComboBox(); |
|---|
| 90 | cmbType->addItem(iconman->getIcon(IconManager::PSTNIcon), tr("Phone")); |
|---|
| 91 | cmbType->addItem(iconman->getIcon(IconManager::MobileIcon), tr("Cellular")); |
|---|
| 92 | cmbType->addItem(iconman->getIcon(IconManager::SMSIcon), tr("Cellular SMS")); |
|---|
| 93 | cmbType->addItem(iconman->getIcon(IconManager::FaxIcon), tr("Fax")); |
|---|
| 94 | cmbType->addItem(iconman->getIcon(IconManager::PagerIcon), tr("Pager")); |
|---|
| 95 | top_lay->addWidget(cmbType, 1, 1); |
|---|
| 96 | |
|---|
| 97 | // ROW 3 |
|---|
| 98 | top_lay->addWidget(new QLabel(tr("Country:")), 2, 0); |
|---|
| 99 | |
|---|
| 100 | cmbCountry = new QComboBox(); |
|---|
| 101 | for (unsigned short i = 0; i < NUM_COUNTRIES; i++) |
|---|
| 102 | cmbCountry->addItem(GetCountryByIndex(i)->szName); |
|---|
| 103 | top_lay->addWidget(cmbCountry, 2, 1); |
|---|
| 104 | |
|---|
| 105 | // ROWS 4-5 |
|---|
| 106 | QGridLayout* gdlay = new QGridLayout(); |
|---|
| 107 | top_lay->addLayout(gdlay, 3, 0, 1, 2); |
|---|
| 108 | |
|---|
| 109 | gdlay->addWidget(new QLabel(tr("Network #/Area code:")), 0, 0); |
|---|
| 110 | |
|---|
| 111 | leAreaCode = new QLineEdit(); |
|---|
| 112 | leAreaCode->setMaxLength(MAX_AREAxCODE_SIZE); |
|---|
| 113 | gdlay->addWidget(leAreaCode, 1, 0); |
|---|
| 114 | |
|---|
| 115 | gdlay->addWidget(new QLabel(tr("Number:")), 0, 1); |
|---|
| 116 | |
|---|
| 117 | leNumber = new QLineEdit(); |
|---|
| 118 | leNumber->setMaxLength(MAX_PHONExNUMBER_SIZE); |
|---|
| 119 | gdlay->addWidget(leNumber, 1, 1); |
|---|
| 120 | |
|---|
| 121 | gdlay->addWidget(new QLabel(tr("Extension:")), 0, 2); |
|---|
| 122 | |
|---|
| 123 | leExtension = new QLineEdit(); |
|---|
| 124 | leExtension->setMaxLength(MAX_EXTENSION_SIZE); |
|---|
| 125 | gdlay->addWidget(leExtension, 1, 2); |
|---|
| 126 | |
|---|
| 127 | // ROW 6 |
|---|
| 128 | top_lay->addWidget(new QLabel(tr("Provider:")), 4, 0); |
|---|
| 129 | |
|---|
| 130 | cmbProvider = new QComboBox(); |
|---|
| 131 | cmbProvider->addItem(tr("Custom")); |
|---|
| 132 | for (unsigned short i = 0; i < NUM_PROVIDERS; i++) |
|---|
| 133 | cmbProvider->addItem(GetProviderByIndex(i)->szName); |
|---|
| 134 | top_lay->addWidget(cmbProvider, 4, 1); |
|---|
| 135 | |
|---|
| 136 | // ROW 7 |
|---|
| 137 | top_lay->addWidget(new QLabel(tr("E-mail Gateway:")), 5, 0); |
|---|
| 138 | |
|---|
| 139 | leGateway = new QLineEdit(); |
|---|
| 140 | leGateway->setMaxLength(MAX_GATEWAY_SIZE); |
|---|
| 141 | top_lay->addWidget(leGateway, 5, 1); |
|---|
| 142 | |
|---|
| 143 | // ROW 8 |
|---|
| 144 | cbRemove0s = new QCheckBox(tr("Remove leading 0s from Area Code/Network #")); |
|---|
| 145 | top_lay->addWidget(cbRemove0s, 6, 0, 1, 2); |
|---|
| 146 | |
|---|
| 147 | // ROW 9 |
|---|
| 148 | QDialogButtonBox* buttons = new QDialogButtonBox(); |
|---|
| 149 | top_lay->addWidget(buttons, 8, 0, 1, 2); |
|---|
| 150 | |
|---|
| 151 | QPushButton* btnOk = buttons->addButton(QDialogButtonBox::Ok); |
|---|
| 152 | btnOk->setText(tr("&OK")); |
|---|
| 153 | connect(buttons, SIGNAL(accepted()), SLOT(ok())); |
|---|
| 154 | |
|---|
| 155 | QPushButton* btnCancel = buttons->addButton(QDialogButtonBox::Cancel); |
|---|
| 156 | btnCancel->setText(tr("&Cancel")); |
|---|
| 157 | connect(buttons, SIGNAL(rejected()), SLOT(close())); |
|---|
| 158 | |
|---|
| 159 | top_lay->setRowStretch(7, 1); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | if (pbe) |
|---|
| 163 | { |
|---|
| 164 | cmbDescription->addItem(codec->toUnicode(pbe->szDescription)); |
|---|
| 165 | cmbDescription->setCurrentIndex(cmbDescription->count() - 1); |
|---|
| 166 | cmbType->setCurrentIndex(pbe->nType); |
|---|
| 167 | const struct SCountry* c = GetCountryByName(pbe->szCountry); |
|---|
| 168 | if (c) |
|---|
| 169 | cmbCountry->setCurrentIndex(c->nIndex); |
|---|
| 170 | leAreaCode->setText(codec->toUnicode(pbe->szAreaCode)); |
|---|
| 171 | leNumber->setText(codec->toUnicode(pbe->szPhoneNumber)); |
|---|
| 172 | // avoid duplicating the pager number in the extension field |
|---|
| 173 | if (pbe->nType != TYPE_PAGER || |
|---|
| 174 | strcmp(pbe->szPhoneNumber, pbe->szExtension) != 0) |
|---|
| 175 | { |
|---|
| 176 | leExtension->setText(codec->toUnicode(pbe->szExtension)); |
|---|
| 177 | } |
|---|
| 178 | if (pbe->nGatewayType == GATEWAY_BUILTIN) |
|---|
| 179 | { |
|---|
| 180 | const struct SProvider* p = GetProviderByName(pbe->szGateway); |
|---|
| 181 | if (p) |
|---|
| 182 | cmbProvider->setCurrentIndex(p->nIndex + 1); |
|---|
| 183 | else if (pbe->szGateway[0] != '\0') |
|---|
| 184 | leGateway->setText(codec->toUnicode(pbe->szGateway)); |
|---|
| 185 | else |
|---|
| 186 | leGateway->setText(tr("@")); |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | leGateway->setText(codec->toUnicode(pbe->szGateway)); |
|---|
| 190 | |
|---|
| 191 | cbRemove0s->setChecked(pbe->nRemoveLeading0s); |
|---|
| 192 | } |
|---|
| 193 | else |
|---|
| 194 | { |
|---|
| 195 | cbRemove0s->setChecked(true); |
|---|
| 196 | leGateway->setText(tr("@")); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | connect(cmbType, SIGNAL(activated(int)), SLOT(UpdateDlg(int))); |
|---|
| 200 | connect(cmbProvider, SIGNAL(activated(int)), SLOT(ProviderChanged(int))); |
|---|
| 201 | |
|---|
| 202 | UpdateDlg(cmbType->currentIndex()); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | //------------------------------------------------------------------------------ |
|---|
| 206 | |
|---|
| 207 | void EditPhoneDlg::UpdateDlg(int nType) |
|---|
| 208 | { |
|---|
| 209 | leExtension->setEnabled(nType == TYPE_PHONE); |
|---|
| 210 | cmbProvider->setEnabled(nType == TYPE_PAGER); |
|---|
| 211 | leGateway->setEnabled(nType == TYPE_PAGER && cmbProvider->currentIndex() == 0); |
|---|
| 212 | cbRemove0s->setEnabled(nType != TYPE_PAGER); |
|---|
| 213 | leAreaCode->setEnabled(nType != TYPE_PAGER); |
|---|
| 214 | cmbCountry->setEnabled(nType != TYPE_PAGER); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | //------------------------------------------------------------------------------ |
|---|
| 218 | |
|---|
| 219 | void EditPhoneDlg::ProviderChanged(int nIndex) |
|---|
| 220 | { |
|---|
| 221 | if (nIndex == 0 && leGateway->text().isEmpty()) |
|---|
| 222 | leGateway->setText(tr("@")); |
|---|
| 223 | |
|---|
| 224 | UpdateDlg(cmbType->currentIndex()); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | //------------------------------------------------------------------------------ |
|---|
| 228 | |
|---|
| 229 | void EditPhoneDlg::ok() |
|---|
| 230 | { |
|---|
| 231 | //Must at least have specified a phone number |
|---|
| 232 | if (leNumber->text().length() == 0) |
|---|
| 233 | { |
|---|
| 234 | WarnUser(this, tr("Please enter a phone number")); |
|---|
| 235 | return; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | const ICQOwner* o = gUserManager.FetchOwner(LICQ_PPID, LOCK_R); |
|---|
| 239 | if (o == NULL) |
|---|
| 240 | { |
|---|
| 241 | close(); |
|---|
| 242 | return; |
|---|
| 243 | } |
|---|
| 244 | QTextCodec* codec = UserCodec::codecForICQUser(o); |
|---|
| 245 | gUserManager.DropOwner(o); |
|---|
| 246 | |
|---|
| 247 | struct PhoneBookEntry pbe; |
|---|
| 248 | memset(&pbe, 0, sizeof(pbe)); |
|---|
| 249 | |
|---|
| 250 | QByteArray tmp = codec->fromUnicode(cmbDescription->currentText()); |
|---|
| 251 | pbe.szDescription = new char[tmp.length() + 1]; |
|---|
| 252 | memcpy(pbe.szDescription, tmp.data(), tmp.length() + 1); |
|---|
| 253 | |
|---|
| 254 | if (leAreaCode->isEnabled()) |
|---|
| 255 | { |
|---|
| 256 | tmp = codec->fromUnicode(leAreaCode->text()); |
|---|
| 257 | pbe.szAreaCode = new char[tmp.length() + 1]; |
|---|
| 258 | memcpy(pbe.szAreaCode, tmp.data(), tmp.length() + 1); |
|---|
| 259 | } |
|---|
| 260 | else |
|---|
| 261 | { |
|---|
| 262 | pbe.szAreaCode = new char[1]; |
|---|
| 263 | pbe.szAreaCode[0] = '\0'; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | tmp = codec->fromUnicode(leNumber->text()); |
|---|
| 267 | pbe.szPhoneNumber = new char[tmp.length() + 1]; |
|---|
| 268 | memcpy(pbe.szPhoneNumber, tmp.data(), tmp.length() + 1); |
|---|
| 269 | |
|---|
| 270 | pbe.nType = cmbType->currentIndex(); |
|---|
| 271 | |
|---|
| 272 | if (leExtension->isEnabled()) |
|---|
| 273 | { |
|---|
| 274 | tmp = codec->fromUnicode(leExtension->text()); |
|---|
| 275 | pbe.szExtension = new char[tmp.length() + 1]; |
|---|
| 276 | memcpy(pbe.szExtension, tmp.data(), tmp.length() + 1); |
|---|
| 277 | } |
|---|
| 278 | else if (pbe.nType == TYPE_PAGER) |
|---|
| 279 | { |
|---|
| 280 | // need to store the number in extension as well for some reason |
|---|
| 281 | pbe.szExtension = new char[tmp.length() + 1]; |
|---|
| 282 | memcpy(pbe.szExtension, tmp.data(), tmp.length() + 1); |
|---|
| 283 | } |
|---|
| 284 | else |
|---|
| 285 | { |
|---|
| 286 | pbe.szExtension = new char[1]; |
|---|
| 287 | pbe.szExtension[0] = '\0'; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | if (cmbCountry->isEnabled() && cmbCountry->currentIndex() != 0) |
|---|
| 291 | { |
|---|
| 292 | tmp = codec->fromUnicode(cmbCountry->currentText()); |
|---|
| 293 | pbe.szCountry = new char[tmp.length() + 1]; |
|---|
| 294 | memcpy(pbe.szCountry, tmp.data(), tmp.length() + 1); |
|---|
| 295 | } |
|---|
| 296 | else |
|---|
| 297 | { |
|---|
| 298 | pbe.szCountry = new char[1]; |
|---|
| 299 | pbe.szCountry[0] = '\0'; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | if (leGateway->isEnabled()) |
|---|
| 303 | { |
|---|
| 304 | tmp = codec->fromUnicode(leGateway->text()); |
|---|
| 305 | pbe.szGateway = new char[tmp.length() + 1]; |
|---|
| 306 | memcpy(pbe.szGateway, tmp.data(), tmp.length() + 1); |
|---|
| 307 | pbe.nGatewayType = GATEWAY_CUSTOM; |
|---|
| 308 | } |
|---|
| 309 | else if (cmbProvider->isEnabled()) |
|---|
| 310 | { |
|---|
| 311 | tmp = codec->fromUnicode(cmbProvider->currentText()); |
|---|
| 312 | pbe.szGateway = new char[tmp.length() + 1]; |
|---|
| 313 | memcpy(pbe.szGateway, tmp.data(), tmp.length() + 1); |
|---|
| 314 | pbe.nGatewayType = GATEWAY_BUILTIN; |
|---|
| 315 | } |
|---|
| 316 | else |
|---|
| 317 | { |
|---|
| 318 | pbe.szGateway = new char[1]; |
|---|
| 319 | pbe.szGateway[0] = '\0'; |
|---|
| 320 | pbe.nGatewayType = GATEWAY_BUILTIN; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | pbe.nSmsAvailable = (pbe.nType == TYPE_CELLULARxSMS) ? 1 : 0; |
|---|
| 324 | |
|---|
| 325 | if (cbRemove0s->isEnabled() && !cbRemove0s->isChecked()) |
|---|
| 326 | pbe.nRemoveLeading0s = 0; |
|---|
| 327 | else |
|---|
| 328 | pbe.nRemoveLeading0s = 1; |
|---|
| 329 | |
|---|
| 330 | emit updated(pbe, m_nEntry); |
|---|
| 331 | close(); |
|---|
| 332 | } |
|---|