// The contents of this file are subject to the Mozilla Public License // Version 1.0 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License at // http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See // the License for the specific language governing rights and limitations // under the License. // // The Original Code is CPhone, a cross platform voip gui. // // The Initial Developer of the Original Code is Derek Smithies. // // Copyright (C) 2002 Indranet Technologies Ltd, // http://www.indranet-technologies.com // All Rights Reserved. // // Contributor(s): _______________ /* * * $Log: addressbookSub.cxx,v $ * Revision 1.6 2004/04/23 05:09:59 dereksmithies * Fixes so it works with latest openh323 lib, and not lock X display. * * Revision 1.5 2003/09/02 03:37:32 dereksmithies * Add statistics, new icons, toolbuttons (icon + name) * * Revision 1.4 2003/08/26 04:48:02 dereksmithies * Fixes to get statistics working right, add icons, add ok,cancel,apply buttons * * Revision 1.3 2003/04/11 01:08:35 dereksmithies * Add speed dial support for Quicknet cards. * * Revision 1.2 2003/04/04 04:37:49 dereksmithies * Major upgrade. * Ixj & microtelco support added. Fix threading issues. * * Revision 1.1.1.1 2002/05/12 22:55:04 dereksmithies * Initial release. * * * * */ #include "addressbookSub.h" #include "keys.h" #include #include #include #include #include #include #include "images/connect_off.xpm" #include "images/connect_on.xpm" #define CUSTOM_EVENT_SETUP (QEvent::User + 1) PMutex AddressBook::accessMutex; AddressBook::AddressBook() : FormAddressBook( 0, "Address Book", FALSE, WDestructiveClose), controlsSet(FALSE) { move(10, 10); CPhone::Current().SetWindowId(eAddressBook, winId()); controlsSet = FALSE; QApplication::postEvent(this, new QCustomEvent(CUSTOM_EVENT_SETUP)); } /* * Destroys the object and frees any allocated resources */ AddressBook::~AddressBook() { } void AddressBook::Setup() { ListViewAbAllNames->clear(); ListViewAbAllNames->setColumnWidthMode(0, QListView::Maximum); ListViewAbAllNames->setColumnText(0, "Name"); ListViewAbAllNames->setColumnWidthMode(1, QListView::Maximum); ListViewAbAllNames->setColumnText(1, "Speed dial"); ListViewAbAllNames->setColumnWidthMode(2, QListView::Maximum); ListViewAbAllNames->setColumnText(2, "Address"); ListViewAbAllNames->setSelectionMode(QListView::Single); ListViewAbAllNames->setResizeMode(QListView::AllColumns); PStringList abKeys; PStringList addresses; PStringList speedDials; GetKeysSpeedsAddresses(abKeys, speedDials, addresses); for(PINDEX i = 0; i< abKeys.GetSize(); i++) { QListViewItem *item = new QListViewItem(ListViewAbAllNames); item->setExpandable(TRUE); item->setText(0, (const char *)abKeys[i]); item->setText(1, (const char *)speedDials[i]); item->setText(2, (const char *)addresses[i]); } BOOL inACall = !CPhone::GetUi().WaitingForACall(); OnConnectionEstablished(inACall); controlsSet = TRUE; } PString AddressBook::GetAddressForKey(PString key) { PStringList keys, speedDials, addresses; GetKeysSpeedsAddresses(keys, speedDials, addresses); PINDEX i = keys.GetStringsIndex(key); if (i == P_MAX_INDEX) return PString(); return addresses[i]; } void AddressBook::GetNameAddressForSpeedDial(PString &name, PString &address, PString key) { PStringList names, speedDials, addresses; GetKeysSpeedsAddresses(names, speedDials, addresses); PINDEX i = speedDials.GetStringsIndex(key); if (i == P_MAX_INDEX) { name = PString(); address = PString(); return; } address = addresses[i]; name = names[i]; } void AddressBook::GetKeysSpeedsAddresses(PStringList &keys, PStringList &speedDials, PStringList &addresses) { PWaitAndSignal m(accessMutex); PConfig *config = new PConfig(ADDRESSBOOK_CONFIG); keys = config->GetKeys(); for (PINDEX i = 0; i< keys.GetSize(); i++) { PString speedDialAddress=config->GetString(keys[i]); PINDEX pos = speedDialAddress.Find('='); if (pos == P_MAX_INDEX) { addresses.AppendString(speedDialAddress); speedDials.AppendString(PString()); continue; } speedDials.AppendString(speedDialAddress.Left(pos)); addresses.AppendString(speedDialAddress.Mid(pos + 1)); } delete config; } void AddressBook::OnConnectionEstablished(bool isEstablished) { toolButtonAbConnect->setEnabled(!isEstablished); if (isEstablished) toolButtonAbConnect->setPixmap((const char **)connect_off); else toolButtonAbConnect->setPixmap((const char **)connect_on); } void AddressBook::SaveCurrentEntries() { PConfig *config = new PConfig(ADDRESSBOOK_CONFIG); PStringList abKeys = config->GetKeys(); for(PINDEX i=0; i< abKeys.GetSize(); i++) config->DeleteKey(abKeys[i]); QListViewItem *item = ListViewAbAllNames->firstChild(); while (item != NULL) { PString res = PString((const char *)item->text(2)); if (!item->text(1).isEmpty()) res = PString((const char *)item->text(1)) + "=" + res; config->SetString((const char *)item->text(0), res); item = item->nextSibling(); } delete config; } void AddressBook::AbAdd_slot() { if (DuplicateNameCondition()) return; if (DuplicateSpeedDialCondition()) return; if (NullNameAddressCondition()) return; QListViewItem *item = new QListViewItem(ListViewAbAllNames); item->setText(0, LineEditAbName->text()); item->setText(1, LineEditAbSpeedDial->text()); item->setText(2, LineEditAbAddress->text()); } void AddressBook::AbCancelAll_slot() { close (TRUE); } void AddressBook::AbChange_slot() { if (NullNameAddressCondition()) return; QListViewItem *item = ListViewAbAllNames->selectedItem(); if (item==NULL) { CPhone::GetUi().DisplayInfoMessage( "Please have the line to change selected before selecting \"Change\"."); return; } PStringList k, s, a; ReadCurrentDisplayTable(k, s, a); PINDEX kIndex = k.GetStringsIndex((const char *)LineEditAbName->text()); PINDEX sIndex = s.GetStringsIndex((const char *)LineEditAbSpeedDial->text()); if ((sIndex == P_MAX_INDEX) && (kIndex == P_MAX_INDEX)) /* speed dial and name not found*/ goto installAbChange_slot; if ((sIndex != P_MAX_INDEX) && (kIndex != P_MAX_INDEX)) { /*speed dial and name both in list*/ if ((kIndex == sIndex) && (LineEditAbName->text() == item->text(0))) goto installAbChange_slot; goto errAbChange_slot; } if ((kIndex != P_MAX_INDEX) && (sIndex == P_MAX_INDEX)) {/*name found, speed dial not found */ if (LineEditAbName->text() == item->text(0)) goto installAbChange_slot; goto errAbChange_slot; } if ((sIndex != P_MAX_INDEX) && (kIndex == P_MAX_INDEX)) { /*speed dial not found, name found */ if (LineEditAbSpeedDial->text() == item->text(1)) goto installAbChange_slot; goto errAbChange_slot; } installAbChange_slot: item->setText(0, LineEditAbName->text()); item->setText(1, LineEditAbSpeedDial->text()); item->setText(2, LineEditAbAddress->text()); return; errAbChange_slot: QString msg = "The name \n\"" + LineEditAbName->text() + "\"\n" + "or the speed dial \"" + LineEditAbSpeedDial->text() + "\"\n" + "is already in the addressbook.\n\n" + "Please use a different name/speed dial"; CPhone::GetUi().DisplayInfoMessage("Duplication in addressbook" + msg); return; } void AddressBook::AbDeleteItem_slot() { QListViewItem *item = ListViewAbAllNames->selectedItem(); if (item==NULL) { CPhone::GetUi().DisplayInfoMessage( "Please have the line to change selected before selecting \"Delete\"."); return; } delete item; LineEditAbName->setText(""); LineEditAbSpeedDial->setText(""); LineEditAbAddress->setText(""); } void AddressBook::AbListViewSelectionChanged_slot() { if (!controlsSet) return; QListViewItem *item = ListViewAbAllNames->selectedItem(); LineEditAbName->setText(item->text(0)) ; LineEditAbSpeedDial->setText(item->text(1)) ; LineEditAbAddress->setText(item->text(2)) ; } void AddressBook::AbSearchForItem_slot() { QListViewItem *item = ListViewAbAllNames->firstChild(); while (item != NULL) { if (item->text(0).compare(LineEditAbName->text() ) == 0) { ListViewAbAllNames->setSelected(item, TRUE); return; } item = item->nextSibling(); } item = ListViewAbAllNames->firstChild(); while (item != NULL) { if (item->text(1).compare(LineEditAbSpeedDial->text()) == 0) { ListViewAbAllNames->setSelected(item, TRUE); return; } item = item->nextSibling(); } item = ListViewAbAllNames->firstChild(); while (item != NULL) { if (item->text(2).compare(LineEditAbAddress->text()) == 0) { ListViewAbAllNames->setSelected(item, TRUE); return; } item = item->nextSibling(); } } bool AddressBook::DuplicateNameCondition() { PStringList k, s, a; ReadCurrentDisplayTable(k, s, a); for (PINDEX i = 0; i < k.GetSize(); i++) { if (k[i] == ((const char *)LineEditAbName->text())) { QString msg = "The name \n\"" + LineEditAbName->text() + "\"\n" + "is already in the addressbook.\n\n" + "Please use a different name." ; CPhone::GetUi().DisplayInfoMessage(msg); return TRUE; } } return FALSE; } bool AddressBook::DuplicateSpeedDialCondition() { PStringList k, s, a; ReadCurrentDisplayTable(k, s, a); for (PINDEX i = 0; i < k.GetSize(); i++) { if ((s[i] == ((const char *)LineEditAbSpeedDial->text())) && (!s[i].IsEmpty())) { QString msg = "The speed dial \n\"" + LineEditAbSpeedDial->text() + "\"\n" + "is already in the addressbook.\n\n" + "Please use a different speed dial." ; CPhone::GetUi().DisplayInfoMessage(msg); return TRUE; } } return FALSE; } void AddressBook::ReadCurrentDisplayTable(PStringList &keys, PStringList &speeds, PStringList &addresses) { QListViewItem *item = ListViewAbAllNames->firstChild(); while (item != NULL) { keys.AppendString((const char *)item->text(0)); speeds.AppendString((const char *)item->text(1)); addresses.AppendString((const char *)item->text(2)); item = item->nextSibling(); } } bool AddressBook::NullNameAddressCondition() { if (LineEditAbName->text().length()==0) { CPhone::GetUi().DisplayInfoMessage("Empty name Please enter a name"); return TRUE; } if (LineEditAbAddress->text().length()==0) { CPhone::GetUi().DisplayInfoMessage("Empty address Please enter an address"); return TRUE; } return FALSE; } void AddressBook::AbConnectToItem_slot() { if (!CPhone::GetUi().WaitingForACall()) { CPhone::GetUi().DisplayInfoMessage("Failed to connect\n You cannot make a call while still in a call."); return; } CPhone::GetUi().StartCall((const char *)LineEditAbAddress->text()); QCustomEvent *ce = new QCustomEvent(CUSTOM_EVENT_DIE); QApplication::postEvent(this, ce); //Qt will delete "ce" when done. } void AddressBook::AbDoubleClick_slot() { AbConnectToItem_slot(); } void AddressBook::closeEvent(QCloseEvent *e) { CPhone::Current().ForgetExists(eAddressBook, winId()); e->accept(); } void AddressBook::customEvent(QCustomEvent *e) { if (e->type() == CUSTOM_EVENT_DIE) close(TRUE); if (e->type() == CUSTOM_EVENT_SETUP) Setup(); } void AddressBook::AbOkSlot() { SaveCurrentEntries(); close(TRUE); } void AddressBook::AbApplySlot() { SaveCurrentEntries(); }