Rating is now updated immediately after pressing rating buttons
[irwi] / src / settingsdlg.cpp
1 #include "settingsdlg.h"
2 #include "advsettingsdlg.h"
3 #include "selectremotedlg.h"
4 #include "aboutdlg.h"
5
6 #include <QHBoxLayout>
7 #include <QVBoxLayout>
8 #include <QWidget>
9 #include <QDialog>
10 #include <QPushButton>
11 #include <QLabel>
12 #include <QDebug>
13 #include <QNetworkConfiguration>
14 #include <QShowEvent>
15
16 SettingsDlg::SettingsDlg(QWidget *parent)
17     : QDialog(parent)
18     , m_busy(true)
19 {
20     QSettings settings(this);
21     m_layout = new QVBoxLayout(this);
22     m_btnLayout = new QHBoxLayout();
23     m_remoteNameLayout = new QHBoxLayout();
24     
25     m_advSettingsBtn = new QPushButton(tr("Advanced"), this);
26     m_selectRemoteBtn = new QPushButton(tr("Select remote"), this);
27     m_aboutBtn = new QPushButton(tr("About"), this);
28     m_rateUpBtn = new QPushButton(
29             QIcon(settings.value("rateUpIcon",
30                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_happy.png").
31                 toString()),
32             "", this);
33     m_rateDownBtn = new QPushButton(
34             QIcon(settings.value("rateDownIcon",
35                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_sad.png").
36                 toString()),
37             "", this);
38     m_rateUpBtn->setMaximumSize(72, 72);
39     m_rateDownBtn->setMaximumSize(72, 72);
40
41     m_btnLayout->addWidget(m_advSettingsBtn);
42     m_btnLayout->addWidget(m_selectRemoteBtn);
43     m_btnLayout->addWidget(m_aboutBtn);
44
45     m_remoteNameLabel = new QLabel(this);
46     m_ratingLabel = new QLabel(this);
47     m_remoteNameLayout->addWidget(m_remoteNameLabel);
48     m_remoteNameLayout->addWidget(m_ratingLabel);
49     m_remoteNameLayout->addWidget(m_rateUpBtn);
50     m_remoteNameLayout->addWidget(m_rateDownBtn);
51
52     connect(m_advSettingsBtn, SIGNAL(clicked()),
53             this, SLOT(showAdvSettingsDlg()));
54     connect(m_selectRemoteBtn, SIGNAL(clicked()),
55             this, SLOT(showSelectRemoteDlg()));
56     connect(m_aboutBtn, SIGNAL(clicked()),
57             this, SLOT(showAboutDlg()));
58     connect(m_rateUpBtn, SIGNAL(clicked()),
59             this, SLOT(rateUpClicked()));
60     connect(m_rateDownBtn, SIGNAL(clicked()),
61             this, SLOT(rateDownClicked()));
62  
63     m_layout->addLayout(m_remoteNameLayout);
64     m_layout->addLayout(m_btnLayout);
65     this->setLayout(m_layout);
66
67     QString selectedRemote = settings.value("remoteName", "").toString();
68     if (selectedRemote == "") {
69         m_remoteNameLabel->setText(tr("No remote selected"));
70         enableRateBtns(false);
71     } else {
72         // Create remote by name and update it's info if online
73         m_remote = Remote(selectedRemote);
74         connect(&m_remote, SIGNAL(infoUpdated()),
75                 this, SLOT(updateRemoteInfo()));
76         m_remoteNameLabel->setText(selectedRemote);
77         m_netConfMan = new QTM_PREPEND_NAMESPACE(
78                 QNetworkConfigurationManager)(this);
79         connect(m_netConfMan, SIGNAL(updateCompleted()),
80                 this, SLOT(onNetworkStatusUpdate()));
81         m_netConfMan->updateConfigurations();
82         enableRateBtns(false);
83     }
84 }
85
86 SettingsDlg::~SettingsDlg()
87 {
88     delete m_advSettingsBtn;
89     delete m_selectRemoteBtn;
90     delete m_rateUpBtn;
91     delete m_rateDownBtn;
92     delete m_aboutBtn;
93     delete m_remoteNameLabel;
94     delete m_ratingLabel;
95     delete m_btnLayout;
96     delete m_remoteNameLayout;
97     delete m_layout;
98     delete m_netConfMan;
99 }
100
101 void SettingsDlg::setBusy(bool busy)
102 {
103     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
104     m_busy = busy;
105 }
106
107 void SettingsDlg::showAdvSettingsDlg()
108 {
109     AdvSettingsDlg dlg;
110     dlg.exec();
111 }
112
113 void SettingsDlg::showSelectRemoteDlg()
114 {
115     SelectRemoteDlg dlg;
116     connect(&dlg, SIGNAL(remoteChanged(Remote)),
117             this, SLOT(setRemote(Remote)));
118     dlg.exec();
119     onNetworkStatusUpdate();
120 }
121
122 void SettingsDlg::showAboutDlg()
123 {
124     AboutDlg dlg;
125     dlg.exec();
126 }
127
128 void SettingsDlg::setRemote(Remote r)
129 {
130     m_remote = r;
131     connect(&m_remote, SIGNAL(infoUpdated()),
132             this, SLOT(updateRemoteInfo()));
133     updateRemoteInfo();
134     enableRateBtns();
135 }
136
137 void SettingsDlg::onNetworkStatusUpdate()
138 {
139     if (m_netConfMan->isOnline()) {
140         m_remote.updateInfo();
141         enableRateBtns();
142     } else {
143         m_ratingLabel->setText(tr("Offline"));
144         setBusy(false);
145     }
146 }
147
148 void SettingsDlg::updateRemoteInfo()
149 {
150     setBusy(false);
151     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
152     m_ratingLabel->setText(tr("Rating") + ": "
153             + QString::number(m_remote.rating()));
154 }
155
156 void SettingsDlg::rateUpClicked()
157 {
158     processRatingSent();
159     m_remote.sendRating(Rating::Up);
160 }
161
162 void SettingsDlg::rateDownClicked()
163 {
164     processRatingSent();
165     m_remote.sendRating(Rating::Down);
166 }
167
168 void SettingsDlg::processRatingSent()
169 {
170     setBusy();
171     enableRateBtns(false);
172 }
173
174 void SettingsDlg::enableRateBtns(bool enable)
175 {
176     m_rateUpBtn->setEnabled(enable);
177     m_rateDownBtn->setEnabled(enable);
178 }
179
180 void SettingsDlg::showEvent(QShowEvent *event)
181 {
182     setBusy(m_busy);
183     QDialog::showEvent(event);
184 }
185
186
187