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 "dbusutility.h"
11 #include "telepathyutility.h"
12 #include "databaseutility.h"
14 #include <QDBusMetaType>
16 class ProfilesWindowPrivate
19 ProfilesWindowPrivate(ProfilesWindow * p) :
20 dbusUtility(new DbusUtility(p)),
21 tpUtility(new TelepathyUtility(p)),
22 databaseUtility(new DatabaseUtility(p)),
25 databaseUtility->openDatabase();
29 ~ProfilesWindowPrivate()
31 databaseUtility->closeDatabase();
32 qDebug() << "VICaR: ProfilesWindowPrivate Destructing";
35 DbusUtility * dbusUtility;
36 TelepathyUtility *tpUtility;
37 DatabaseUtility *databaseUtility;
38 org::maemo::vicar::Profile *profile;
39 ProfilesWindow * const parent;
42 // ---------------------------------------------------------------------------
44 ProfilesWindow::ProfilesWindow(QWidget *parent) :
46 ui(new Ui::ProfilesWindow),
47 d(new ProfilesWindowPrivate(this))
52 ui->actionRoutingEnabled->setChecked(d->tpUtility->accountExists());
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);
59 ProfilesWindow::~ProfilesWindow()
64 void ProfilesWindow::loadProfilesList(){
65 org::maemo::vicar::ProfileList profileList;
66 bool result = d->databaseUtility->getAllProfiles(&profileList);
68 d->dbusUtility->displayNotification("Unable to get profiles list.");
69 qDebug() << "Error fetching profiles list. "<< d->databaseUtility->lastError();
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);
82 strProfileDetails = QString("No profile found, then call via %1")
83 .arg(currentProfile.gatewayNumber);
85 item = new QListWidgetItem(strProfileDetails,ui->profilesListWidget);
86 item->setData(Qt::UserRole,currentProfile.profileID);
91 void ProfilesWindow::on_addButton_clicked()
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);
100 d->dbusUtility->displayNotification("Unable to create new profile.");
101 qDebug() << "Error inserting new profile record. "<< d->databaseUtility->lastError();
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);
113 void ProfilesWindow::on_editButton_clicked(){
114 QListWidgetItem *item = ui->profilesListWidget->currentItem();
116 int intProfileID = item->data(Qt::UserRole).toInt();
117 org::maemo::vicar::Profile profile;
118 bool result = d->databaseUtility->selectProfile(intProfileID,&profile);
120 d->dbusUtility->displayNotification("Unable to fetch profile details.");
121 qDebug() << "Error fetching profile record for id "<< intProfileID
122 <<". "<< d->databaseUtility->lastError();
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);
131 d->dbusUtility->displayNotification("Unable to update profile.");
132 qDebug() << "Error fetching profile record for id "<< intProfileID
133 <<". "<< d->databaseUtility->lastError();
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);
143 strProfileDetails = QString("No profile found, then call via %1")
144 .arg(profile.gatewayNumber);
146 item->setText(strProfileDetails);
147 item->setData(Qt::UserRole,profile.profileID);
153 d->dbusUtility->displayNotification("Select a profile to edit.");
157 void ProfilesWindow::on_removeButton_clicked(){
158 QListWidgetItem *item = ui->profilesListWidget->currentItem();
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);
164 d->dbusUtility->displayNotification("Unable to delete profile.");
165 qDebug() << "Error deleting profile record for id "<< intProfileID
166 <<". "<< d->databaseUtility->lastError();
169 //ui->profilesListWidget->removeItemWidget(item);
170 ui->profilesListWidget->clear();
171 this->loadProfilesList();
175 d->dbusUtility->displayNotification("Select a profile to delete.");
179 void ProfilesWindow::on_actionRoutingEnabled_triggered(bool checked){
181 qDBusRegisterMetaType<org::freedesktop::Telepathy::SimplePresence>();
186 if (!d->tpUtility->accountExists()){
187 result = d->tpUtility->createAccount();
189 d->dbusUtility->displayNotification("Unable to enable call routing.");
190 ui->actionRoutingEnabled->setChecked(false);
195 if (d->tpUtility->accountExists()){
196 result = d->tpUtility->deleteAccount();
198 d->dbusUtility->displayNotification("Unable to disable call routing.");
199 ui->actionRoutingEnabled->setChecked(true);
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);
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;
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);
222 d->dbusUtility->displayNotification("Unable to update default profile.");
223 qDebug() << "Error updating default profile."<< d->databaseUtility->lastError();
226 ui->profilesListWidget->clear();
227 this->loadProfilesList();
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);
240 d->dbusUtility->displayNotification("Unable to create default profile.");
241 qDebug() << "Error inserting new default profile record. "<< d->databaseUtility->lastError();
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);