78785a808456156558f5c4873ed349a54a8d26b0
[vicar] / src / vicar-config / src / profileswindow.cpp
1 /*
2 @version: 0.5
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         //ui->profilesListWidget->clear();
76         org::maemo::vicar::Profile currentProfile;
77         foreach (currentProfile,profileList){
78             QString strProfileDetails = QString("Number starts with %1 then call via %2")
79                                         .arg(currentProfile.phoneNumberPattern)
80                                         .arg(currentProfile.gatewayNumber);
81             QListWidgetItem *item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
82             item->setData(Qt::UserRole,currentProfile.profileID);
83         }
84     }
85 }
86
87 void ProfilesWindow::on_addButton_clicked()
88 {
89     org::maemo::vicar::Profile newProfile;
90     ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&newProfile);
91     int result = dialog->exec();
92     if (result == QDialog::Accepted){
93         qDebug() << "Inserting new profile record";
94         int result = d->databaseUtility->insertProfile(newProfile);
95         if (result == -1){
96             d->dbusUtility->displayNotification("Unable to create new profile.");
97             qDebug() << "Error inserting new profile record. "<< d->databaseUtility->lastError();
98         }
99         else{
100             QString strProfileDetails = QString("Number starts with %1 then call via %2")
101                                         .arg(newProfile.phoneNumberPattern)
102                                         .arg(newProfile.gatewayNumber);
103             QListWidgetItem *item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
104             item->setData(Qt::UserRole,result);
105         }
106     }
107 }
108
109 void ProfilesWindow::on_editButton_clicked(){
110     QListWidgetItem *item = ui->profilesListWidget->currentItem();
111     if (item != 0){
112         int intProfileID = item->data(Qt::UserRole).toInt();
113         org::maemo::vicar::Profile profile;
114         bool result = d->databaseUtility->selectProfile(intProfileID,&profile);
115         if (!result){
116             d->dbusUtility->displayNotification("Unable to fetch profile details.");
117             qDebug() << "Error fetching profile record for id "<< intProfileID
118                     <<". "<< d->databaseUtility->lastError();
119         }
120         else{
121             ProfileSettingsDialog *dialog = new ProfileSettingsDialog(this,&profile);
122             int response = dialog->exec();
123             if (response == QDialog::Accepted){
124                 qDebug() << "Updating profile record";
125                 bool result = d->databaseUtility->updateProfile(profile);
126                 if (!result){
127                     d->dbusUtility->displayNotification("Unable to update profile.");
128                     qDebug() << "Error fetching profile record for id "<< intProfileID
129                             <<". "<< d->databaseUtility->lastError();
130                 }
131                 else{
132                     QString strProfileDetails = QString("Number starts with %1 then \n call via %2")
133                                                 .arg(profile.phoneNumberPattern)
134                                                 .arg(profile.gatewayNumber);
135                     item->setText(strProfileDetails);
136                     item->setData(Qt::UserRole,result);
137                 }
138             }
139         }
140     }
141     else{
142         d->dbusUtility->displayNotification("Select a profile to edit.");
143     }
144 }
145
146 void ProfilesWindow::on_removeButton_clicked(){
147     QListWidgetItem *item = ui->profilesListWidget->currentItem();
148     if (item != 0){
149         int intProfileID = item->data(Qt::UserRole).toInt();
150         bool result = d->databaseUtility->deleteProfile(intProfileID);
151         if (!result){
152             d->dbusUtility->displayNotification("Unable to delete profile.");
153             qDebug() << "Error deleting profile record for id "<< intProfileID
154                     <<". "<< d->databaseUtility->lastError();
155         }
156         else{
157             //ui->profilesListWidget->removeItemWidget(item);
158             ui->profilesListWidget->clear();
159             this->loadProfilesList();
160         }
161     }
162     else{
163         d->dbusUtility->displayNotification("Select a profile to delete.");
164     }
165 }
166
167 void ProfilesWindow::on_actionRoutingEnabled_triggered(bool checked){
168
169     qDBusRegisterMetaType<org::freedesktop::Telepathy::SimplePresence>();
170
171     bool result;
172
173     if (checked){
174         if (!d->tpUtility->accountExists()){
175             result = d->tpUtility->createAccount();
176             if (!result){
177                 d->dbusUtility->displayNotification("Unable to enable call routing.");
178                 ui->actionRoutingEnabled->setChecked(false);
179             }
180         }
181     }
182     else{
183         if (d->tpUtility->accountExists()){
184             result = d->tpUtility->deleteAccount();
185             if (!result){
186                 d->dbusUtility->displayNotification("Unable to disable call routing.");
187                 ui->actionRoutingEnabled->setChecked(true);
188             }
189         }
190     }
191 }
192
193 void ProfilesWindow::on_actionRouteOnDefault_triggered(bool checked){
194     d->gconfUtility->setGconfValueBoolean("route_on_default",checked);
195 }