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 //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);
87 void ProfilesWindow::on_addButton_clicked()
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);
96 d->dbusUtility->displayNotification("Unable to create new profile.");
97 qDebug() << "Error inserting new profile record. "<< d->databaseUtility->lastError();
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);
109 void ProfilesWindow::on_editButton_clicked(){
110 QListWidgetItem *item = ui->profilesListWidget->currentItem();
112 int intProfileID = item->data(Qt::UserRole).toInt();
113 org::maemo::vicar::Profile profile;
114 bool result = d->databaseUtility->selectProfile(intProfileID,&profile);
116 d->dbusUtility->displayNotification("Unable to fetch profile details.");
117 qDebug() << "Error fetching profile record for id "<< intProfileID
118 <<". "<< d->databaseUtility->lastError();
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);
127 d->dbusUtility->displayNotification("Unable to update profile.");
128 qDebug() << "Error fetching profile record for id "<< intProfileID
129 <<". "<< d->databaseUtility->lastError();
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);
142 d->dbusUtility->displayNotification("Select a profile to edit.");
146 void ProfilesWindow::on_removeButton_clicked(){
147 QListWidgetItem *item = ui->profilesListWidget->currentItem();
149 int intProfileID = item->data(Qt::UserRole).toInt();
150 bool result = d->databaseUtility->deleteProfile(intProfileID);
152 d->dbusUtility->displayNotification("Unable to delete profile.");
153 qDebug() << "Error deleting profile record for id "<< intProfileID
154 <<". "<< d->databaseUtility->lastError();
157 //ui->profilesListWidget->removeItemWidget(item);
158 ui->profilesListWidget->clear();
159 this->loadProfilesList();
163 d->dbusUtility->displayNotification("Select a profile to delete.");
167 void ProfilesWindow::on_actionRoutingEnabled_triggered(bool checked){
169 qDBusRegisterMetaType<org::freedesktop::Telepathy::SimplePresence>();
174 if (!d->tpUtility->accountExists()){
175 result = d->tpUtility->createAccount();
177 d->dbusUtility->displayNotification("Unable to enable call routing.");
178 ui->actionRoutingEnabled->setChecked(false);
183 if (d->tpUtility->accountExists()){
184 result = d->tpUtility->deleteAccount();
186 d->dbusUtility->displayNotification("Unable to disable call routing.");
187 ui->actionRoutingEnabled->setChecked(true);
193 void ProfilesWindow::on_actionRouteOnDefault_triggered(bool checked){
194 d->gconfUtility->setGconfValueBoolean("route_on_default",checked);