Initial Release
[vicar] / src / vicar-config / src / configwindow.cpp
1 /*
2 @version: 0.2
3 @author: Sudheer K. <scifi.guy@hotmail.com>
4 @license: GNU General Public License
5 */
6
7 #include "configwindow.h"
8 #include "ui_configwindow.h"
9 #include "gconfutility.h"
10 #include "dbusutility.h"
11 #include <QDebug>
12 #include <QMessageBox>
13
14 ConfigWindow::ConfigWindow(QWidget *parent) :
15     QMainWindow(parent),
16     ui(new Ui::ConfigWindow)
17 {
18     ui->setupUi(this);
19     gconfUtility = new GConfUtility();
20     loadValues();
21 }
22
23 ConfigWindow::~ConfigWindow()
24 {
25     delete ui;
26     delete gconfUtility;
27     gconfUtility = 0;
28 }
29
30 void ConfigWindow::changeEvent(QEvent *e)
31 {
32     QMainWindow::changeEvent(e);
33     switch (e->type()) {
34     case QEvent::LanguageChange:
35         ui->retranslateUi(this);
36         break;
37     default:
38         break;
39     }
40 }
41
42 void ConfigWindow::closeEvent(QCloseEvent *e)
43 {
44     if (verifyConfigData()){
45         qDebug() << "Verification successful. Saving data to gconf";
46         saveConfigData();
47         e->accept();
48     }
49     else{
50         e->ignore();
51     }
52 }
53
54 void ConfigWindow::on_actionSave_triggered()
55 {
56     if (verifyConfigData()){
57         qDebug() << "Verification successful. Saving data to gconf";
58         saveConfigData();
59     }
60
61 }
62
63 void ConfigWindow::on_actionReset_triggered()
64 {
65     ui->checkBoxIntlCallRedirEnabled->setChecked(false);
66     ui->lineEditCallingCardNumber->clear();
67     ui->lineEditCountryCodesToExclude->clear();
68     ui->comboBoxDTMFFormat->clear();
69     ui->comboBoxDTMFSuffix->clear();
70     ui->spinBoxDTMFDelay->setValue(1);
71 }
72
73 void ConfigWindow::loadValues(){
74
75     bool isRoutingEnabled = gconfUtility->getGconfValueBoolean("routing_enabled");
76     QString strCallingCardNumber = gconfUtility->getGconfValueString("calling_card_number");
77     QString strNumbersToExclude = gconfUtility->getGconfValueString("numbers_to_exclude");
78     QString strDTMFFormat = gconfUtility->getGconfValueString("dtmf_format");
79     QString strDTMFSuffix = gconfUtility->getGconfValueString("dtmf_suffix");
80     int intDTMFDelay = gconfUtility->getGconfValueInteger("dtmf_delay");
81
82     ui->checkBoxIntlCallRedirEnabled->setChecked(isRoutingEnabled);
83     ui->lineEditCallingCardNumber->setText(strCallingCardNumber);
84     ui->lineEditCountryCodesToExclude->setText(strNumbersToExclude);
85     int intIndex = ui->comboBoxDTMFFormat->findText(strDTMFFormat);
86     ui->comboBoxDTMFFormat->setCurrentIndex(intIndex);
87     intIndex = ui->comboBoxDTMFSuffix->findText(strDTMFSuffix);
88     ui->comboBoxDTMFSuffix->setCurrentIndex(intIndex);
89     ui->spinBoxDTMFDelay->setValue(intDTMFDelay);
90
91    //Accept numbers only for Calling Card Number field
92     ui->lineEditCallingCardNumber->setValidator(new QRegExpValidator( QRegExp( "^-?\\d\\d*$"), this));
93
94     qDebug() << "Values loaded from GConf";
95
96 }
97
98 bool ConfigWindow::verifyConfigData(){
99     //Verify whether user-input matches application requirements
100     bool isRoutingEnabled = ui->checkBoxIntlCallRedirEnabled->isChecked();
101     QString strCallingCardNumber = ui->lineEditCallingCardNumber->text();
102     QString strNumbersToExclude = ui->lineEditCountryCodesToExclude->text();
103
104
105     QString strMessage = QString("");
106
107     if (isRoutingEnabled){
108         //Call Routing is checked. Now validate other values
109         if (strCallingCardNumber.isEmpty()){
110             strMessage.append("Enter a calling card number\n");
111             ui->lineEditCallingCardNumber->setFocus();
112         }
113
114         if (strCallingCardNumber.startsWith("+")||strCallingCardNumber.startsWith("00")){
115             strMessage.append("Calling card number must be a local number. \nPlease remove the international dialing code.\n");
116             ui->lineEditCallingCardNumber->setFocus();
117         }
118
119     }
120
121     if (!strMessage.isEmpty()){
122         QMessageBox::warning(this,"Invalid Data",strMessage);
123         return false;
124     }
125     else{
126         return true;
127     }
128 }
129
130 void ConfigWindow::saveConfigData(){
131
132         bool isRoutingEnabled = ui->checkBoxIntlCallRedirEnabled->isChecked();
133         QString strCallingCardNumber = ui->lineEditCallingCardNumber->text();
134         QString strNumbersToExclude = ui->lineEditCountryCodesToExclude->text();
135
136         QString strDTMFFormat = ui->comboBoxDTMFFormat->currentText();
137         QString strDTMFSuffix = ui->comboBoxDTMFSuffix->currentText();
138         int intDTMFDelay = ui->spinBoxDTMFDelay->value();
139
140
141         gconfUtility->setGconfValueBoolean("routing_enabled",isRoutingEnabled);
142
143         if (!strCallingCardNumber.isEmpty()){
144             gconfUtility->setGconfValueString("calling_card_number",strCallingCardNumber);
145         }
146
147         if (!strNumbersToExclude.isEmpty()){
148             strNumbersToExclude = strNumbersToExclude.remove(" ");
149             strNumbersToExclude = strNumbersToExclude.remove("\t");
150             strNumbersToExclude = strNumbersToExclude.replace("\n",",");
151         }
152
153         gconfUtility->setGconfValueString("numbers_to_exclude",strNumbersToExclude);
154         gconfUtility->setGconfValueInteger("dtmf_delay",intDTMFDelay);
155         gconfUtility->setGconfValueString("dtmf_format",strDTMFFormat);
156         gconfUtility->setGconfValueString("dtmf_suffix",strDTMFSuffix);
157
158         DbusUtility dbusUtility = DbusUtility();
159
160         qDebug() << "Settings updated";
161         dbusUtility.displayNotification("VICaR: Settings Updated.");
162
163         if (isRoutingEnabled){
164             qDebug() << "Enable call routing immediately";
165             dbusUtility.sendSignal(APPLICATION_DBUS_PATH,APPLICATION_DBUS_INTERFACE,"startOutgoingCallMonitor");
166         }
167         else{
168             qDebug() << "Disable call routing immediately";
169             dbusUtility.sendSignal(APPLICATION_DBUS_PATH,APPLICATION_DBUS_INTERFACE,"stopOutgoingCallMonitor");
170         }
171 }
172