Conditional compilation for Maemo 5 specific code
[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     , 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("symbolPath",
31                 "/usr/share/irwi/symbols/").
32                 toString() + "symbol_thumbs_up.png"),
33             "", this);
34     m_rateDownBtn = new QPushButton(
35             QIcon(settings.value("symbolPath",
36                 "/usr/share/irwi/symbols/").
37                 toString() + "symbol_thumbs_down.png"),
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 remoteName = settings.value("remoteName", "").toString();
69     if (remoteName == "") {
70         m_remoteNameLabel->setText(tr("No remote selected"));
71     } else {
72         // Create remote by name and update it's info if online
73         m_remote = Remote(remoteName);
74         connect(&m_remote, SIGNAL(infoUpdated()),
75                 this, SLOT(updateRemoteInfo()));
76         m_remoteNameLabel->setText(settings.value("remoteMfg", "").toString()
77                 + " " + remoteName);
78     }
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 SettingsDlg::~SettingsDlg()
88 {
89     delete m_advSettingsBtn;
90     delete m_selectRemoteBtn;
91     delete m_rateUpBtn;
92     delete m_rateDownBtn;
93     delete m_aboutBtn;
94     delete m_remoteNameLabel;
95     delete m_ratingLabel;
96     delete m_btnLayout;
97     delete m_remoteNameLayout;
98     delete m_layout;
99     delete m_netConfMan;
100 }
101
102 void SettingsDlg::setBusy(bool busy)
103 {
104 #ifdef Q_WS_MAEMO_5
105     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
106 #endif
107     m_busy = busy;
108 }
109
110 void SettingsDlg::showAdvSettingsDlg()
111 {
112     AdvSettingsDlg dlg;
113     dlg.exec();
114     if (QSettings(this).value("remoteName", "").toString() == "") {
115         m_remoteNameLabel->setText(tr("No remote selected"));
116         m_ratingLabel->setText("");
117         enableRateBtns(false);
118     }
119 }
120
121 void SettingsDlg::showSelectRemoteDlg()
122 {
123     SelectRemoteDlg dlg;
124     connect(&dlg, SIGNAL(remoteChanged(Remote)),
125             this, SLOT(setRemote(Remote)));
126     if (dlg.exec() == QDialog::Rejected) {
127         onNetworkStatusUpdate();
128     }
129 }
130
131 void SettingsDlg::showAboutDlg()
132 {
133     AboutDlg dlg;
134     dlg.exec();
135 }
136
137 void SettingsDlg::setRemote(Remote r)
138 {
139     m_remote = r;
140     connect(&m_remote, SIGNAL(infoUpdated()),
141             this, SLOT(updateRemoteInfo()));
142     updateRemoteInfo();
143     enableRateBtns();
144 }
145
146 void SettingsDlg::onNetworkStatusUpdate()
147 {
148     if (m_netConfMan->isOnline() &&
149         QSettings(this).value("remoteName", "").toString() != "") {
150         setBusy();
151         m_remote.updateInfo();
152         enableRateBtns();
153     } else if (!m_netConfMan->isOnline()) {
154         m_ratingLabel->setText(tr("Offline"));
155         setBusy(false);
156     } else {
157         setBusy(false);
158     }
159 }
160
161 void SettingsDlg::updateRemoteInfo()
162 {
163     setBusy(false);
164     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
165     m_ratingLabel->setText(tr("Rating") + ": "
166             + QString::number(m_remote.rating()));
167 }
168
169 void SettingsDlg::rateUpClicked()
170 {
171     processRatingSent();
172     m_remote.sendRating(Rating::Up);
173 }
174
175 void SettingsDlg::rateDownClicked()
176 {
177     processRatingSent();
178     m_remote.sendRating(Rating::Down);
179 }
180
181 void SettingsDlg::processRatingSent()
182 {
183     setBusy();
184     enableRateBtns(false);
185 }
186
187 void SettingsDlg::enableRateBtns(bool enable)
188 {
189     m_rateUpBtn->setEnabled(enable);
190     m_rateDownBtn->setEnabled(enable);
191 }
192
193 void SettingsDlg::showEvent(QShowEvent *event)
194 {
195     setBusy(m_busy);
196     QDialog::showEvent(event);
197 }
198
199
200