2
0

prefaccounts.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <QtGui>
  2. #include "prefaccounts.h"
  3. #include "accountdialog.h"
  4. PrefAccounts::PrefAccounts(Ui::PrefDialog *ui) :
  5. _ui(ui)
  6. {
  7. _accDlg = NULL;
  8. connect(_ui->sofiaGwAddBtn, SIGNAL(clicked()), this, SLOT(addAccountBtnClicked()));
  9. connect(_ui->sofiaGwRemBtn, SIGNAL(clicked()), this, SLOT(remAccountBtnClicked()));
  10. connect(_ui->sofiaGwEditBtn, SIGNAL(clicked()), this, SLOT(editAccountBtnClicked()));
  11. _ui->accountsTable->horizontalHeader()->setStretchLastSection(true);
  12. }
  13. void PrefAccounts::addAccountBtnClicked()
  14. {
  15. if (!_accDlg)
  16. {
  17. _accDlg = new AccountDialog();
  18. connect(_accDlg, SIGNAL(gwAdded(QString)), this, SLOT(readConfig()));
  19. }
  20. else
  21. {
  22. /* Needs to be set to empty because we are not editing */
  23. _accDlg->setName(QString());
  24. _accDlg->clear();
  25. }
  26. _accDlg->show();
  27. _accDlg->raise();
  28. _accDlg->activateWindow();
  29. }
  30. void PrefAccounts::editAccountBtnClicked()
  31. {
  32. QList<QTableWidgetSelectionRange> selList = _ui->accountsTable->selectedRanges();
  33. if (selList.isEmpty())
  34. return;
  35. QTableWidgetSelectionRange range = selList[0];
  36. /* Get the selected item */
  37. QString gwName = _ui->accountsTable->item(range.topRow(),0)->text();
  38. if (!_accDlg)
  39. {
  40. /* TODO: We need a way to read this sucker... Might as well just already pass the profile name */
  41. _accDlg = new AccountDialog();
  42. connect(_accDlg, SIGNAL(gwAdded(QString)), this, SLOT(readConfig()));
  43. }
  44. /* TODO: Should pass the profile name someday */
  45. _accDlg->setName(gwName);
  46. _accDlg->readConfig();
  47. _accDlg->show();
  48. _accDlg->raise();
  49. _accDlg->activateWindow();
  50. }
  51. void PrefAccounts::remAccountBtnClicked()
  52. {
  53. QList<QTableWidgetSelectionRange> sel = _ui->accountsTable->selectedRanges();
  54. int offset =0;
  55. foreach(QTableWidgetSelectionRange range, sel)
  56. {
  57. for(int row = range.topRow(); row<=range.bottomRow(); row++)
  58. {
  59. QTableWidgetItem *item = _ui->accountsTable->item(row-offset,0);
  60. ISettings settings(this);
  61. QDomElement cfg = settings.getConfigNode("sofia.conf");
  62. QDomNodeList gws = cfg.elementsByTagName("gateway");
  63. for (int i = 0; i < gws.count(); i++) {
  64. QDomElement gw = gws.at(i).toElement();
  65. if ( gw.attributeNode("name").value() == item->text()) {
  66. cfg.elementsByTagName("gateways").at(0).removeChild(gw);
  67. break;
  68. }
  69. }
  70. settings.setConfigNode(cfg, "sofia.conf");
  71. /* Mark the account to be deleted */
  72. _toDelete.append(item->text());
  73. _ui->accountsTable->removeRow(row-offset);
  74. offset++;
  75. }
  76. }
  77. if (offset)
  78. readConfig();
  79. }
  80. void PrefAccounts::writeConfig()
  81. {
  82. return;
  83. }
  84. void PrefAccounts::postWriteConfig() {
  85. QString res;
  86. if (g_FSHost->sendCmd("sofia", "profile softphone rescan", &res) != SWITCH_STATUS_SUCCESS)
  87. {
  88. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not rescan the softphone profile.\n");
  89. }
  90. foreach (QString gw, _toDelete) {
  91. if (g_FSHost->sendCmd("sofia", QString("profile softphone killgw %1").arg(gw).toAscii().constData(), &res) != SWITCH_STATUS_SUCCESS)
  92. {
  93. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not remove gateway from profile [%s].\n", gw.toAscii().constData());
  94. }
  95. }
  96. }
  97. void PrefAccounts::readConfig()
  98. {
  99. _ui->accountsTable->clearContents();
  100. _ui->accountsTable->setRowCount(0);
  101. ISettings settings(this);
  102. QDomElement cfg = settings.getConfigNode("sofia.conf");
  103. if ( cfg.elementsByTagName("gateways").count() == 0 ) {
  104. QDomElement profile = cfg.elementsByTagName("profile").at(0).toElement();
  105. QDomDocument d = profile.toDocument();
  106. QDomElement gws = d.createElement("gateways");
  107. profile.insertBefore(gws, QDomNode()); /* To make it look nicer */
  108. settings.setConfigNode(cfg, "sofia.conf");
  109. return;
  110. }
  111. QDomNodeList l = cfg.elementsByTagName("gateway");
  112. for (int i = 0; i < l.count(); i++) {
  113. QDomElement gw = l.at(i).toElement();
  114. QTableWidgetItem *item0 = new QTableWidgetItem(gw.attribute("name"));
  115. QTableWidgetItem *item1 = NULL;
  116. /* Iterate until we find what we need */
  117. QDomNodeList params = gw.elementsByTagName("param");
  118. for(int j = 0; i < params.count(); j++) {
  119. QDomElement e = params.at(j).toElement();
  120. QString var = e.attributeNode("name").value();
  121. if (var == "username" ) {
  122. item1 = new QTableWidgetItem(e.attributeNode("value").value());
  123. break; /* We found, so stop looping */
  124. }
  125. }
  126. _ui->accountsTable->setRowCount(_ui->accountsTable->rowCount()+1);
  127. _ui->accountsTable->setItem(_ui->accountsTable->rowCount()-1, 0, item0);
  128. _ui->accountsTable->setItem(_ui->accountsTable->rowCount()-1, 1, item1);
  129. }
  130. _ui->accountsTable->resizeRowsToContents();
  131. _ui->accountsTable->resizeColumnsToContents();
  132. _ui->accountsTable->horizontalHeader()->setStretchLastSection(true);
  133. /*
  134. TODO: We have to figure out what to do with the default account stuff!
  135. if (_ui->accountsTable->rowCount() == 1)
  136. {
  137. QString default_gateway = _settings->value(QString("/FreeSWITCH/conf/sofia.conf/profiles/profile/gateways/%1/gateway/attrs/name").arg(_ui->accountsTable->item(0,0)->data(Qt::UserRole).toString())).toString();
  138. _settings->beginGroup("FreeSWITCH/conf/globals");
  139. _settings->setValue("default_gateway", default_gateway);
  140. _settings->endGroup();
  141. switch_core_set_variable("default_gateway", default_gateway.toAscii().data());
  142. }*/
  143. }