SIGSEGV Fixed
[vicar] / src / vicar-config / cpp / profileswindow.cpp
1 /*
2 @version: 0.6
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 #include "profileswindow.h"
8 #include "ui_profileswindow.h"
9 #include "profilesettingsdialog.h"
10 #include "dbusutility.h"
11 #include "telepathyutility.h"
12 #include "databaseutility.h"
13 #include <QDebug>
14 #include <QDBusMetaType>
15
16 class ProfilesWindowPrivate
17 {
18 public:
19     ProfilesWindowPrivate(ProfilesWindow * p) :
20             dbusUtility(new DbusUtility(p)),
21             tpUtility(new TelepathyUtility(p)),
22             databaseUtility(new DatabaseUtility(p)),
23             parent(p)
24     {
25         databaseUtility->openDatabase();
26
27     }
28
29     ~ProfilesWindowPrivate()
30     {
31         databaseUtility->closeDatabase();
32         qDebug() << "VICaR: ProfilesWindowPrivate Destructing";
33     }
34
35     DbusUtility * dbusUtility;
36     TelepathyUtility *tpUtility;
37     DatabaseUtility *databaseUtility;
38     org::maemo::vicar::Profile *profile;
39     ProfilesWindow * const parent;    
40 };
41
42 // ---------------------------------------------------------------------------
43
44 ProfilesWindow::ProfilesWindow(QWidget *parent) :
45     QMainWindow(parent),
46     ui(new Ui::ProfilesWindow),
47     d(new ProfilesWindowPrivate(this))
48 {
49     ui->setupUi(this);
50     loadProfilesList();
51
52     ui->actionRoutingEnabled->setChecked(d->tpUtility->accountExists());        
53
54     //bool routeOnDefault = d->gconfUtility->getGconfValueBoolean("route_on_default");
55     QString routeOnDefaultSetting = d->databaseUtility->getSetting("route_on_default");
56     ui->actionRouteOnDefault->setChecked(routeOnDefaultSetting == "1"?true:false);
57 }
58
59 ProfilesWindow::~ProfilesWindow()
60 {
61     delete ui;
62 }
63
64 void ProfilesWindow::loadProfilesList(){
65     org::maemo::vicar::ProfileList profileList;
66     bool result = d->databaseUtility->getAllProfiles(&profileList);
67     if (!result){
68         d->dbusUtility->displayNotification("Unable to get profiles list.");
69         qDebug() << "Error fetching profiles list. "<< d->databaseUtility->lastError();
70     }
71     else{
72         org::maemo::vicar::Profile currentProfile;
73         QString strProfileDetails;
74         QListWidgetItem *item;
75         foreach (currentProfile,profileList){            
76             if (currentProfile.phoneNumberPattern != "%"){
77                 strProfileDetails = QString("Number starts with %1, then call via %2")
78                                             .arg(currentProfile.phoneNumberPattern)
79                                             .arg(currentProfile.gatewayNumber);
80             }
81             else{
82                 strProfileDetails = QString("No profile found, then call via %1")
83                                             .arg(currentProfile.gatewayNumber);
84             }
85             item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
86             item->setData(Qt::UserRole,currentProfile.profileID);
87         }
88     }
89 }
90
91 void ProfilesWindow::on_addButton_clicked()
92 {
93     org::maemo::vicar::Profile newProfile;
94     ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&newProfile);
95     int result = dialog->exec();
96     if (result == QDialog::Accepted){
97         qDebug() << "Inserting new profile record";
98         int result = d->databaseUtility->insertProfile(newProfile);
99         if (result == -1){
100             d->dbusUtility->displayNotification("Unable to create new profile.");
101             qDebug() << "Error inserting new profile record. "<< d->databaseUtility->lastError();
102         }
103         else{
104             QString strProfileDetails = QString("Number starts with %1, then call via %2")
105                                         .arg(newProfile.phoneNumberPattern)
106                                         .arg(newProfile.gatewayNumber);
107             QListWidgetItem *item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
108             item->setData(Qt::UserRole,result);
109         }
110     }
111 }
112
113 void ProfilesWindow::on_editButton_clicked(){
114     QListWidgetItem *item = ui->profilesListWidget->currentItem();
115     if (item != 0){
116         int intProfileID = item->data(Qt::UserRole).toInt();
117         org::maemo::vicar::Profile profile;
118         bool result = d->databaseUtility->selectProfile(intProfileID,&profile);
119         if (!result){
120             d->dbusUtility->displayNotification("Unable to fetch profile details.");
121             qDebug() << "Error fetching profile record for id "<< intProfileID
122                     <<". "<< d->databaseUtility->lastError();
123         }
124         else{
125             ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&profile);
126             int response = dialog->exec();
127             if (response == QDialog::Accepted){
128                 qDebug() << "Updating profile record";
129                 bool result = d->databaseUtility->updateProfile(profile);
130                 if (!result){
131                     d->dbusUtility->displayNotification("Unable to update profile.");
132                     qDebug() << "Error fetching profile record for id "<< intProfileID
133                             <<". "<< d->databaseUtility->lastError();
134                 }
135                 else{
136                     QString strProfileDetails;
137                     if (profile.phoneNumberPattern != "%"){
138                         strProfileDetails = QString("Number starts with %1, then call via %2")
139                                                     .arg(profile.phoneNumberPattern)
140                                                     .arg(profile.gatewayNumber);
141                     }
142                     else{
143                         strProfileDetails = QString("No profile found, then call via %1")
144                                                     .arg(profile.gatewayNumber);
145                     }
146                     item->setText(strProfileDetails);
147                     item->setData(Qt::UserRole,profile.profileID);
148                 }
149             }
150         }
151     }
152     else{
153         d->dbusUtility->displayNotification("Select a profile to edit.");
154     }
155 }
156
157 void ProfilesWindow::on_removeButton_clicked(){
158     QListWidgetItem *item = ui->profilesListWidget->currentItem();
159     if (item != 0){
160         qDebug() << "Selected item data is " << item->data(Qt::UserRole).toInt();
161         int intProfileID = item->data(Qt::UserRole).toInt();
162         bool result = d->databaseUtility->deleteProfile(intProfileID);
163         if (!result){
164             d->dbusUtility->displayNotification("Unable to delete profile.");
165             qDebug() << "Error deleting profile record for id "<< intProfileID
166                     <<". "<< d->databaseUtility->lastError();
167         }
168         else{
169             //ui->profilesListWidget->removeItemWidget(item);
170             ui->profilesListWidget->clear();
171             this->loadProfilesList();
172         }
173     }
174     else{
175         d->dbusUtility->displayNotification("Select a profile to delete.");
176     }
177 }
178
179 void ProfilesWindow::on_actionRoutingEnabled_triggered(bool checked){
180
181     qDBusRegisterMetaType<org::freedesktop::Telepathy::SimplePresence>();
182
183     bool result;
184
185     if (checked){
186         if (!d->tpUtility->accountExists()){
187             result = d->tpUtility->createAccount();
188             if (!result){
189                 d->dbusUtility->displayNotification("Unable to enable call routing.");
190                 ui->actionRoutingEnabled->setChecked(false);
191             }
192         }
193     }
194     else{
195         if (d->tpUtility->accountExists()){
196             result = d->tpUtility->deleteAccount();
197             if (!result){
198                 d->dbusUtility->displayNotification("Unable to disable call routing.");
199                 ui->actionRoutingEnabled->setChecked(true);
200             }
201         }
202     }
203 }
204
205 void ProfilesWindow::on_actionRouteOnDefault_triggered(bool checked){
206     d->databaseUtility->setSetting("route_on_default",checked?"1":"0");
207     //d->gconfUtility->setGconfValueBoolean("route_on_default",checked);
208 }
209
210 void ProfilesWindow::on_actionEditDefaultProfile_triggered(){
211     org::maemo::vicar::Profile profile;
212     bool result = d->databaseUtility->getDefaultProfile(&profile);
213     qDebug() << "Default profile exits? " <<result;
214     if (result){
215         //Default profile does not exist - edit it
216         ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&profile);
217         int response = dialog->exec();
218         if (response == QDialog::Accepted){
219             qDebug() << "Updating default profile record";
220             bool result = d->databaseUtility->updateProfile(profile);
221             if (!result){
222                 d->dbusUtility->displayNotification("Unable to update default profile.");
223                 qDebug() << "Error updating default profile."<< d->databaseUtility->lastError();
224             }
225             else{
226                 ui->profilesListWidget->clear();
227                 this->loadProfilesList();
228             }
229         }
230     }
231     else{
232        //Create new default profile
233         profile.phoneNumberPattern = "%";
234         ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&profile);
235         int result = dialog->exec();
236         if (result == QDialog::Accepted){
237             qDebug() << "Inserting new default profile record";
238             int result = d->databaseUtility->insertProfile(profile);
239             if (result == -1){
240                 d->dbusUtility->displayNotification("Unable to create default profile.");
241                 qDebug() << "Error inserting new default profile record. "<< d->databaseUtility->lastError();
242             }
243             else{
244                 QString strProfileDetails = QString("No profile found, then call via %1")
245                                             .arg(profile.gatewayNumber);
246                 QListWidgetItem *item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
247                 item->setData(Qt::UserRole,result);
248             }
249         }
250     }
251 }