3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
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"
15 #include <QDBusMetaType>
17 class ProfilesWindowPrivate
20 ProfilesWindowPrivate(ProfilesWindow * p) :
21 gconfUtility(new GConfUtility(p)),
22 dbusUtility(new DbusUtility(p)),
23 tpUtility(new TelepathyUtility(p)),
24 databaseUtility(new DatabaseUtility(p)),
27 databaseUtility->openDatabase();
31 ~ProfilesWindowPrivate()
33 databaseUtility->closeDatabase();
34 qDebug() << "VICaR: ProfilesWindowPrivate Destructing";
37 GConfUtility * gconfUtility;
38 DbusUtility * dbusUtility;
39 TelepathyUtility *tpUtility;
40 DatabaseUtility *databaseUtility;
41 org::maemo::vicar::Profile *profile;
42 ProfilesWindow * const parent;
45 // ---------------------------------------------------------------------------
47 ProfilesWindow::ProfilesWindow(QWidget *parent) :
49 ui(new Ui::ProfilesWindow),
50 d(new ProfilesWindowPrivate(this))
55 ui->actionRoutingEnabled->setChecked(d->tpUtility->accountExists());
57 bool routeOnDefault = d->gconfUtility->getGconfValueBoolean("route_on_default");
58 ui->actionRouteOnDefault->setChecked(routeOnDefault);
62 ProfilesWindow::~ProfilesWindow()
67 void ProfilesWindow::loadProfilesList(){
68 org::maemo::vicar::ProfileList profileList;
69 bool result = d->databaseUtility->getAllProfiles(&profileList);
71 d->dbusUtility->displayNotification("Unable to get profiles list.");
72 qDebug() << "Error fetching profiles list. "<< d->databaseUtility->lastError();
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);
85 strProfileDetails = QString("No profile found, then call via %1")
86 .arg(currentProfile.gatewayNumber);
88 item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
89 item->setData(Qt::UserRole,currentProfile.profileID);
94 void ProfilesWindow::on_addButton_clicked()
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);
103 d->dbusUtility->displayNotification("Unable to create new profile.");
104 qDebug() << "Error inserting new profile record. "<< d->databaseUtility->lastError();
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);
116 void ProfilesWindow::on_editButton_clicked(){
117 QListWidgetItem *item = ui->profilesListWidget->currentItem();
119 int intProfileID = item->data(Qt::UserRole).toInt();
120 org::maemo::vicar::Profile profile;
121 bool result = d->databaseUtility->selectProfile(intProfileID,&profile);
123 d->dbusUtility->displayNotification("Unable to fetch profile details.");
124 qDebug() << "Error fetching profile record for id "<< intProfileID
125 <<". "<< d->databaseUtility->lastError();
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);
134 d->dbusUtility->displayNotification("Unable to update profile.");
135 qDebug() << "Error fetching profile record for id "<< intProfileID
136 <<". "<< d->databaseUtility->lastError();
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);
146 strProfileDetails = QString("No profile found, then call via %1")
147 .arg(profile.gatewayNumber);
149 item->setText(strProfileDetails);
150 item->setData(Qt::UserRole,profile.profileID);
156 d->dbusUtility->displayNotification("Select a profile to edit.");
160 void ProfilesWindow::on_removeButton_clicked(){
161 QListWidgetItem *item = ui->profilesListWidget->currentItem();
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);
167 d->dbusUtility->displayNotification("Unable to delete profile.");
168 qDebug() << "Error deleting profile record for id "<< intProfileID
169 <<". "<< d->databaseUtility->lastError();
172 //ui->profilesListWidget->removeItemWidget(item);
173 ui->profilesListWidget->clear();
174 this->loadProfilesList();
178 d->dbusUtility->displayNotification("Select a profile to delete.");
182 void ProfilesWindow::on_actionRoutingEnabled_triggered(bool checked){
184 qDBusRegisterMetaType<org::freedesktop::Telepathy::SimplePresence>();
189 if (!d->tpUtility->accountExists()){
190 result = d->tpUtility->createAccount();
192 d->dbusUtility->displayNotification("Unable to enable call routing.");
193 ui->actionRoutingEnabled->setChecked(false);
198 if (d->tpUtility->accountExists()){
199 result = d->tpUtility->deleteAccount();
201 d->dbusUtility->displayNotification("Unable to disable call routing.");
202 ui->actionRoutingEnabled->setChecked(true);
208 void ProfilesWindow::on_actionRouteOnDefault_triggered(bool checked){
209 d->gconfUtility->setGconfValueBoolean("route_on_default",checked);
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;
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);
224 d->dbusUtility->displayNotification("Unable to update default profile.");
225 qDebug() << "Error updating default profile."<< d->databaseUtility->lastError();
228 ui->profilesListWidget->clear();
229 this->loadProfilesList();
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);
242 d->dbusUtility->displayNotification("Unable to create default profile.");
243 qDebug() << "Error inserting new default profile record. "<< d->databaseUtility->lastError();
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);