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