accountdialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <QtGui>
  2. #include "accountdialog.h"
  3. #include "ui_accountdialog.h"
  4. AccountDialog::AccountDialog(QWidget *parent) :
  5. QDialog(parent),
  6. ui(new Ui::AccountDialog)
  7. {
  8. ui->setupUi(this);
  9. connect(this, SIGNAL(accepted()), this, SLOT(writeConfig()));
  10. connect(ui->sofiaExtraParamAddBtn, SIGNAL(clicked()), this, SLOT(addExtraParam()));
  11. connect(ui->sofiaExtraParamRemBtn, SIGNAL(clicked()), this, SLOT(remExtraParam()));
  12. connect(ui->clidSettingsCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(clidSettingsComboChanged(int)));
  13. connect(ui->codecSettingsCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(codecSettingsComboChanged(int)));
  14. ui->sofiaExtraParamTable->horizontalHeader()->setStretchLastSection(true);
  15. ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->codecPage));
  16. ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->clidPage));
  17. }
  18. AccountDialog::~AccountDialog()
  19. {
  20. delete ui;
  21. }
  22. void AccountDialog::codecSettingsComboChanged(int index)
  23. {
  24. if (index == 0)
  25. ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->codecPage));
  26. else
  27. ui->tabWidget->insertTab(1,ui->codecPage,tr("Codecs"));
  28. }
  29. void AccountDialog::clidSettingsComboChanged(int index)
  30. {
  31. if (index == 0)
  32. ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->clidPage));
  33. else
  34. ui->tabWidget->insertTab(1,ui->clidPage,tr("Caller ID"));
  35. }
  36. void AccountDialog::remExtraParam()
  37. {
  38. QList<QTableWidgetSelectionRange> sel = ui->sofiaExtraParamTable->selectedRanges();
  39. foreach(QTableWidgetSelectionRange range, sel)
  40. {
  41. int offset =0;
  42. for(int row = range.topRow(); row<=range.bottomRow(); row++)
  43. {
  44. ui->sofiaExtraParamTable->removeRow(row-offset);
  45. offset++;
  46. }
  47. }
  48. }
  49. void AccountDialog::addExtraParam()
  50. {
  51. bool ok;
  52. QString paramName = QInputDialog::getText(this, tr("Add parameter."),
  53. tr("New parameter name:"), QLineEdit::Normal,
  54. NULL, &ok);
  55. if (!ok)
  56. return;
  57. QString paramVal = QInputDialog::getText(this, tr("Add parameter."),
  58. tr("New parameter value:"), QLineEdit::Normal,
  59. NULL, &ok);
  60. if (!ok)
  61. return;
  62. QTableWidgetItem* paramNameItem = new QTableWidgetItem(paramName);
  63. QTableWidgetItem* paramValItem = new QTableWidgetItem(paramVal);
  64. ui->sofiaExtraParamTable->setRowCount(ui->sofiaExtraParamTable->rowCount()+1);
  65. ui->sofiaExtraParamTable->setItem(ui->sofiaExtraParamTable->rowCount()-1,0,paramNameItem);
  66. ui->sofiaExtraParamTable->setItem(ui->sofiaExtraParamTable->rowCount()-1,1,paramValItem);
  67. ui->sofiaExtraParamTable->resizeColumnsToContents();
  68. ui->sofiaExtraParamTable->resizeRowsToContents();
  69. ui->sofiaExtraParamTable->horizontalHeader()->setStretchLastSection(true);
  70. }
  71. /* TODO: We need to figure out the callerID thing... */
  72. void AccountDialog::readConfig()
  73. {
  74. /* We already know the name of the gateway, so... */
  75. ui->sofiaGwNameEdit->setText(_name);
  76. ISettings settings(this);
  77. QDomElement cfg = settings.getConfigNode("sofia.conf");
  78. QDomNodeList nl = cfg.elementsByTagName("gateway");
  79. for (int i = 0; i < nl.count(); i++) {
  80. QDomElement gw = nl.at(i).toElement();
  81. if (gw.attributeNode("name").value() == _name) {
  82. /* Iterate the params and set the values */
  83. QDomNodeList params = gw.elementsByTagName("param");
  84. int row = 0; /* Used for extra params */
  85. ui->sofiaExtraParamTable->clearContents();
  86. for (int j = 0; j < params.count(); j++) {
  87. QDomElement param = params.at(j).toElement();
  88. QString var = param.attributeNode("name").value();
  89. QString val = param.attributeNode("value").value();
  90. if ( var == "username" ) {
  91. ui->sofiaGwUsernameEdit->setText(val);
  92. } else if ( var == "realm" ) {
  93. ui->sofiaGwRealmEdit->setText(val);
  94. } else if ( var == "password" ) {
  95. ui->sofiaGwPasswordEdit->setText(val);
  96. } else if ( var == "expire-seconds" ) {
  97. ui->sofiaGwExpireSecondsSpin->setValue(val.toInt());
  98. } else if ( var == "register" ) {
  99. ui->sofiaGwRegisterCombo->setCurrentIndex(ui->sofiaGwRegisterCombo->findText(val, Qt::MatchExactly));
  100. } else if ( var == "register-transport" ) {
  101. ui->sofiaGwRegisterTransportCombo->setCurrentIndex(ui->sofiaGwRegisterTransportCombo->findText(val, Qt::MatchExactly));
  102. } else if ( var == "retry-seconds" ) {
  103. ui->sofiaGwRetrySecondsSpin->setValue(val.toInt());
  104. } else {
  105. /* Set custom parameters */
  106. row++;
  107. ui->sofiaExtraParamTable->setRowCount(row);
  108. QTableWidgetItem *varName = new QTableWidgetItem(var);
  109. QTableWidgetItem *varVal = new QTableWidgetItem(val);
  110. ui->sofiaExtraParamTable->setItem(row-1, 0,varName);
  111. ui->sofiaExtraParamTable->setItem(row-1, 1,varVal);
  112. }
  113. }
  114. /* Stop processing the gateway list */
  115. break;
  116. }
  117. }
  118. ui->sofiaExtraParamTable->resizeColumnsToContents();
  119. ui->sofiaExtraParamTable->resizeRowsToContents();
  120. ui->sofiaExtraParamTable->horizontalHeader()->setStretchLastSection(true);
  121. }
  122. /* TODO: Figure out the callerID thing... */
  123. void AccountDialog::writeConfig()
  124. {
  125. /* TODO: This is where we need to figure out the caller ID
  126. if (ui->clidSettingsCombo->currentIndex() == 0)
  127. {
  128. settings->remove("caller_id_name");
  129. settings->remove("caller_id_num");
  130. } else {
  131. settings->setValue("caller_id_name", ui->sofiaCallerIDName->text());
  132. settings->setValue("caller_id_num", ui->sofiaCallerIDNum->text());
  133. }
  134. */
  135. ISettings settings(this);
  136. QDomElement cfg = settings.getConfigNode("sofia.conf");
  137. /* First check to see if we are editing */
  138. if (!_name.isEmpty()) {
  139. /* Find our gateway */
  140. QDomElement gw;
  141. QDomNodeList gws = cfg.elementsByTagName("gateway");
  142. for (int i = 0; i < gws.count(); i++) {
  143. if ( gws.at(i).toElement().attributeNode("name").value() == _name) {
  144. gw = gws.at(i).toElement();
  145. /* Set the new gateway name */
  146. if ( _name != ui->sofiaGwNameEdit->text() ) {
  147. _name = ui->sofiaGwNameEdit->text();
  148. gws.at(i).toElement().attributeNode("name").setValue(ui->sofiaGwNameEdit->text());
  149. }
  150. break;
  151. }
  152. }
  153. if ( gw.isNull() ) {
  154. qDebug() << "Hey, there is no gateway!";
  155. return;
  156. }
  157. /* Found the gateway, now iterate the parameters */
  158. QDomNodeList params = gw.elementsByTagName("param");
  159. for (int i = 0; i < params.count(); i++) {
  160. QDomElement param = params.at(i).toElement();
  161. QString var = param.attributeNode("name").value();
  162. QDomAttr val = param.attributeNode("value");
  163. if ( var == "username" ) {
  164. val.setValue(ui->sofiaGwUsernameEdit->text());
  165. } else if ( var == "realm" ) {
  166. val.setValue(ui->sofiaGwRealmEdit->text());
  167. } else if ( var == "password" ) {
  168. val.setValue(ui->sofiaGwPasswordEdit->text());
  169. } else if ( var == "expire-seconds" ) {
  170. val.setValue(QString::number(ui->sofiaGwExpireSecondsSpin->value()));
  171. } else if ( var == "register" ) {
  172. val.setValue(ui->sofiaGwRegisterCombo->currentText());
  173. } else if ( var == "register-transport" ) {
  174. val.setValue(ui->sofiaGwRegisterTransportCombo->currentText());
  175. } else if ( var == "retry-seconds" ) {
  176. val.setValue(QString::number(ui->sofiaGwRetrySecondsSpin->value()));
  177. }
  178. }
  179. /* Set extra parameters */
  180. QDomDocument d = gw.toDocument();
  181. for (int i = 0; i< ui->sofiaExtraParamTable->rowCount(); i++)
  182. {
  183. QDomElement ePar = d.createElement("param");
  184. QDomAttr var = d.createAttribute(ui->sofiaExtraParamTable->item(i, 0)->text());
  185. ePar.appendChild(var);
  186. QDomAttr val = d.createAttribute(ui->sofiaExtraParamTable->item(i, 1)->text());
  187. ePar.appendChild(val);
  188. gw.appendChild(ePar);
  189. }
  190. } else {
  191. QDomElement gws = cfg.elementsByTagName("gateways").at(0).toElement();
  192. QDomDocument d = gws.toDocument();
  193. QDomElement nGw = d.createElement("gateway");
  194. gws.insertAfter(nGw, QDomNode());
  195. nGw.setAttribute("name",ui->sofiaGwNameEdit->text());
  196. /* Set each one of the parameters */
  197. setParam(nGw, "username", ui->sofiaGwUsernameEdit->text());
  198. setParam(nGw, "password", ui->sofiaGwPasswordEdit->text());
  199. setParam(nGw, "register", ui->sofiaGwRegisterCombo->currentText());
  200. setParam(nGw, "realm", ui->sofiaGwRealmEdit->text());
  201. setParam(nGw, "expire-seconds", QString::number(ui->sofiaGwExpireSecondsSpin->value()));
  202. setParam(nGw, "register-transport", ui->sofiaGwRegisterTransportCombo->currentText());
  203. setParam(nGw, "retry-seconds", QString::number(ui->sofiaGwRetrySecondsSpin->value()));
  204. for (int i = 0; i< ui->sofiaExtraParamTable->rowCount(); i++)
  205. {
  206. setParam(nGw, ui->sofiaExtraParamTable->item(i, 0)->text(), ui->sofiaExtraParamTable->item(i, 1)->text());
  207. }
  208. }
  209. settings.setConfigNode(cfg, "sofia.conf");
  210. emit gwAdded(_name);
  211. }
  212. void AccountDialog::setParam(QDomElement &parent, QString name, QString value) {
  213. QDomDocument d = parent.toDocument();
  214. QDomElement e = d.createElement("param");
  215. e.setAttribute("name", name);
  216. e.setAttribute("value", value);
  217. parent.appendChild(e);
  218. }
  219. void AccountDialog::clear()
  220. {
  221. ui->sofiaExtraParamTable->clearContents();
  222. ui->sofiaExtraParamTable->setRowCount(0);
  223. ui->sofiaGwNameEdit->clear();
  224. ui->sofiaGwUsernameEdit->clear();
  225. ui->sofiaGwRealmEdit->clear();
  226. ui->sofiaGwPasswordEdit->clear();
  227. ui->sofiaGwExpireSecondsSpin->setValue(60);
  228. ui->sofiaGwRegisterCombo->setCurrentIndex(0);
  229. ui->sofiaGwRegisterTransportCombo->setCurrentIndex(0);
  230. ui->sofiaGwRetrySecondsSpin->setValue(30);
  231. }
  232. void AccountDialog::changeEvent(QEvent *e)
  233. {
  234. QDialog::changeEvent(e);
  235. switch (e->type()) {
  236. case QEvent::LanguageChange:
  237. ui->retranslateUi(this);
  238. break;
  239. default:
  240. break;
  241. }
  242. }