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